blob: bb13f47d5632c31d2597aabcb532461380338b1f [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 Peterson94b580d2011-08-02 17:30:04 -05001612static PyTypeObject SchedParamType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001613static newfunc structseq_new;
1614
1615static PyObject *
1616statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1617{
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 PyStructSequence *result;
1619 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001620
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 result = (PyStructSequence*)structseq_new(type, args, kwds);
1622 if (!result)
1623 return NULL;
1624 /* If we have been initialized from a tuple,
1625 st_?time might be set to None. Initialize it
1626 from the int slots. */
1627 for (i = 7; i <= 9; i++) {
1628 if (result->ob_item[i+3] == Py_None) {
1629 Py_DECREF(Py_None);
1630 Py_INCREF(result->ob_item[i]);
1631 result->ob_item[i+3] = result->ob_item[i];
1632 }
1633 }
1634 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001635}
1636
1637
1638
1639/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001640static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001641
1642PyDoc_STRVAR(stat_float_times__doc__,
1643"stat_float_times([newval]) -> oldval\n\n\
1644Determine whether os.[lf]stat represents time stamps as float objects.\n\
1645If newval is True, future calls to stat() return floats, if it is False,\n\
1646future calls return ints. \n\
1647If newval is omitted, return the current setting.\n");
1648
1649static PyObject*
1650stat_float_times(PyObject* self, PyObject *args)
1651{
Victor Stinner8c62be82010-05-06 00:08:46 +00001652 int newval = -1;
1653 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1654 return NULL;
1655 if (newval == -1)
1656 /* Return old value */
1657 return PyBool_FromLong(_stat_float_times);
1658 _stat_float_times = newval;
1659 Py_INCREF(Py_None);
1660 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001661}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001662
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001663static void
1664fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1665{
Victor Stinner8c62be82010-05-06 00:08:46 +00001666 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001667#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001669#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001671#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 if (!ival)
1673 return;
1674 if (_stat_float_times) {
1675 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1676 } else {
1677 fval = ival;
1678 Py_INCREF(fval);
1679 }
1680 PyStructSequence_SET_ITEM(v, index, ival);
1681 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001682}
1683
Tim Peters5aa91602002-01-30 05:46:57 +00001684/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001685 (used by posix_stat() and posix_fstat()) */
1686static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001687_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001688{
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 unsigned long ansec, mnsec, cnsec;
1690 PyObject *v = PyStructSequence_New(&StatResultType);
1691 if (v == NULL)
1692 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001693
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001695#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 PyStructSequence_SET_ITEM(v, 1,
1697 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001698#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001700#endif
1701#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 PyStructSequence_SET_ITEM(v, 2,
1703 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001704#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001705 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001706#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001707 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1708 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1709 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001710#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 PyStructSequence_SET_ITEM(v, 6,
1712 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001713#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001714 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001715#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001716
Martin v. Löwis14694662006-02-03 12:54:16 +00001717#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 ansec = st->st_atim.tv_nsec;
1719 mnsec = st->st_mtim.tv_nsec;
1720 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001721#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001722 ansec = st->st_atimespec.tv_nsec;
1723 mnsec = st->st_mtimespec.tv_nsec;
1724 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001725#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 ansec = st->st_atime_nsec;
1727 mnsec = st->st_mtime_nsec;
1728 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001729#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001731#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 fill_time(v, 7, st->st_atime, ansec);
1733 fill_time(v, 8, st->st_mtime, mnsec);
1734 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001735
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001736#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1738 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001739#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001740#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1742 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001743#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001744#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001745 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1746 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001747#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001748#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001749 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1750 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001751#endif
1752#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 {
1754 PyObject *val;
1755 unsigned long bsec,bnsec;
1756 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001757#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001759#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001761#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 if (_stat_float_times) {
1763 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1764 } else {
1765 val = PyLong_FromLong((long)bsec);
1766 }
1767 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1768 val);
1769 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001770#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001771#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001772 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1773 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001774#endif
Fred Drake699f3522000-06-29 21:12:41 +00001775
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 if (PyErr_Occurred()) {
1777 Py_DECREF(v);
1778 return NULL;
1779 }
Fred Drake699f3522000-06-29 21:12:41 +00001780
Victor Stinner8c62be82010-05-06 00:08:46 +00001781 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001782}
1783
Barry Warsaw53699e91996-12-10 23:23:01 +00001784static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001785posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001786 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001787#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001789#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 char *wformat,
1793 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001794{
Victor Stinner8c62be82010-05-06 00:08:46 +00001795 STRUCT_STAT st;
1796 PyObject *opath;
1797 char *path;
1798 int res;
1799 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001800
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001801#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001802 PyUnicodeObject *po;
1803 if (PyArg_ParseTuple(args, wformat, &po)) {
1804 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001805
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 Py_BEGIN_ALLOW_THREADS
1807 /* PyUnicode_AS_UNICODE result OK without
1808 thread lock as it is a simple dereference. */
1809 res = wstatfunc(wpath, &st);
1810 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001811
Victor Stinner8c62be82010-05-06 00:08:46 +00001812 if (res != 0)
1813 return win32_error_unicode("stat", wpath);
1814 return _pystat_fromstructstat(&st);
1815 }
1816 /* Drop the argument parsing error as narrow strings
1817 are also valid. */
1818 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001819#endif
1820
Victor Stinner8c62be82010-05-06 00:08:46 +00001821 if (!PyArg_ParseTuple(args, format,
1822 PyUnicode_FSConverter, &opath))
1823 return NULL;
1824 path = PyBytes_AsString(opath);
1825 Py_BEGIN_ALLOW_THREADS
1826 res = (*statfunc)(path, &st);
1827 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001828
Victor Stinner8c62be82010-05-06 00:08:46 +00001829 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001830#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001832#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001834#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 }
1836 else
1837 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001838
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 Py_DECREF(opath);
1840 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841}
1842
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001843/* POSIX methods */
1844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001846"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001847Use the real uid/gid to test for access to a path. Note that most\n\
1848operations will use the effective uid/gid, therefore this routine can\n\
1849be used in a suid/sgid environment to test if the invoking user has the\n\
1850specified access to the path. The mode argument can be F_OK to test\n\
1851existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001852
1853static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001854posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001855{
Victor Stinner8c62be82010-05-06 00:08:46 +00001856 PyObject *opath;
1857 char *path;
1858 int mode;
1859
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001860#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 DWORD attr;
1862 PyUnicodeObject *po;
1863 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1864 Py_BEGIN_ALLOW_THREADS
1865 /* PyUnicode_AS_UNICODE OK without thread lock as
1866 it is a simple dereference. */
1867 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1868 Py_END_ALLOW_THREADS
1869 goto finish;
1870 }
1871 /* Drop the argument parsing error as narrow strings
1872 are also valid. */
1873 PyErr_Clear();
1874 if (!PyArg_ParseTuple(args, "O&i:access",
1875 PyUnicode_FSConverter, &opath, &mode))
1876 return NULL;
1877 path = PyBytes_AsString(opath);
1878 Py_BEGIN_ALLOW_THREADS
1879 attr = GetFileAttributesA(path);
1880 Py_END_ALLOW_THREADS
1881 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001882finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 if (attr == 0xFFFFFFFF)
1884 /* File does not exist, or cannot read attributes */
1885 return PyBool_FromLong(0);
1886 /* Access is possible if either write access wasn't requested, or
1887 the file isn't read-only, or if it's a directory, as there are
1888 no read-only directories on Windows. */
1889 return PyBool_FromLong(!(mode & 2)
1890 || !(attr & FILE_ATTRIBUTE_READONLY)
1891 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001892#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001893 int res;
1894 if (!PyArg_ParseTuple(args, "O&i:access",
1895 PyUnicode_FSConverter, &opath, &mode))
1896 return NULL;
1897 path = PyBytes_AsString(opath);
1898 Py_BEGIN_ALLOW_THREADS
1899 res = access(path, mode);
1900 Py_END_ALLOW_THREADS
1901 Py_DECREF(opath);
1902 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001904}
1905
Guido van Rossumd371ff11999-01-25 16:12:23 +00001906#ifndef F_OK
1907#define F_OK 0
1908#endif
1909#ifndef R_OK
1910#define R_OK 4
1911#endif
1912#ifndef W_OK
1913#define W_OK 2
1914#endif
1915#ifndef X_OK
1916#define X_OK 1
1917#endif
1918
1919#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001921"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001923
1924static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001925posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001926{
Victor Stinner8c62be82010-05-06 00:08:46 +00001927 int id;
1928 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001929
Victor Stinner8c62be82010-05-06 00:08:46 +00001930 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1931 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001932
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001933#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 /* file descriptor 0 only, the default input device (stdin) */
1935 if (id == 0) {
1936 ret = ttyname();
1937 }
1938 else {
1939 ret = NULL;
1940 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001941#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001942 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001943#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 if (ret == NULL)
1945 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001946 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001947}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001948#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001949
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001950#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001951PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001952"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001954
1955static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001956posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001957{
Victor Stinner8c62be82010-05-06 00:08:46 +00001958 char *ret;
1959 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001960
Greg Wardb48bc172000-03-01 21:51:56 +00001961#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001962 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001963#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001965#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 if (ret == NULL)
1967 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001968 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001969}
1970#endif
1971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001973"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001974Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Barry Warsaw53699e91996-12-10 23:23:01 +00001976static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001977posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001979#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001980 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001982 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001983#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001984 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001985#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001987#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001988}
1989
Fred Drake4d1e64b2002-04-15 19:40:07 +00001990#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001992"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001993Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001995
1996static PyObject *
1997posix_fchdir(PyObject *self, PyObject *fdobj)
1998{
Victor Stinner8c62be82010-05-06 00:08:46 +00001999 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002000}
2001#endif /* HAVE_FCHDIR */
2002
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002005"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002006Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002007
Barry Warsaw53699e91996-12-10 23:23:01 +00002008static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002009posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002010{
Victor Stinner8c62be82010-05-06 00:08:46 +00002011 PyObject *opath = NULL;
2012 char *path = NULL;
2013 int i;
2014 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002015#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 DWORD attr;
2017 PyUnicodeObject *po;
2018 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2019 Py_BEGIN_ALLOW_THREADS
2020 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2021 if (attr != 0xFFFFFFFF) {
2022 if (i & _S_IWRITE)
2023 attr &= ~FILE_ATTRIBUTE_READONLY;
2024 else
2025 attr |= FILE_ATTRIBUTE_READONLY;
2026 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2027 }
2028 else
2029 res = 0;
2030 Py_END_ALLOW_THREADS
2031 if (!res)
2032 return win32_error_unicode("chmod",
2033 PyUnicode_AS_UNICODE(po));
2034 Py_INCREF(Py_None);
2035 return Py_None;
2036 }
2037 /* Drop the argument parsing error as narrow strings
2038 are also valid. */
2039 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002040
Victor Stinner8c62be82010-05-06 00:08:46 +00002041 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2042 &opath, &i))
2043 return NULL;
2044 path = PyBytes_AsString(opath);
2045 Py_BEGIN_ALLOW_THREADS
2046 attr = GetFileAttributesA(path);
2047 if (attr != 0xFFFFFFFF) {
2048 if (i & _S_IWRITE)
2049 attr &= ~FILE_ATTRIBUTE_READONLY;
2050 else
2051 attr |= FILE_ATTRIBUTE_READONLY;
2052 res = SetFileAttributesA(path, attr);
2053 }
2054 else
2055 res = 0;
2056 Py_END_ALLOW_THREADS
2057 if (!res) {
2058 win32_error("chmod", path);
2059 Py_DECREF(opath);
2060 return NULL;
2061 }
2062 Py_DECREF(opath);
2063 Py_INCREF(Py_None);
2064 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002065#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2067 &opath, &i))
2068 return NULL;
2069 path = PyBytes_AsString(opath);
2070 Py_BEGIN_ALLOW_THREADS
2071 res = chmod(path, i);
2072 Py_END_ALLOW_THREADS
2073 if (res < 0)
2074 return posix_error_with_allocated_filename(opath);
2075 Py_DECREF(opath);
2076 Py_INCREF(Py_None);
2077 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002078#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002079}
2080
Christian Heimes4e30a842007-11-30 22:12:06 +00002081#ifdef HAVE_FCHMOD
2082PyDoc_STRVAR(posix_fchmod__doc__,
2083"fchmod(fd, mode)\n\n\
2084Change the access permissions of the file given by file\n\
2085descriptor fd.");
2086
2087static PyObject *
2088posix_fchmod(PyObject *self, PyObject *args)
2089{
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 int fd, mode, res;
2091 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2092 return NULL;
2093 Py_BEGIN_ALLOW_THREADS
2094 res = fchmod(fd, mode);
2095 Py_END_ALLOW_THREADS
2096 if (res < 0)
2097 return posix_error();
2098 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002099}
2100#endif /* HAVE_FCHMOD */
2101
2102#ifdef HAVE_LCHMOD
2103PyDoc_STRVAR(posix_lchmod__doc__,
2104"lchmod(path, mode)\n\n\
2105Change the access permissions of a file. If path is a symlink, this\n\
2106affects the link itself rather than the target.");
2107
2108static PyObject *
2109posix_lchmod(PyObject *self, PyObject *args)
2110{
Victor Stinner8c62be82010-05-06 00:08:46 +00002111 PyObject *opath;
2112 char *path;
2113 int i;
2114 int res;
2115 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2116 &opath, &i))
2117 return NULL;
2118 path = PyBytes_AsString(opath);
2119 Py_BEGIN_ALLOW_THREADS
2120 res = lchmod(path, i);
2121 Py_END_ALLOW_THREADS
2122 if (res < 0)
2123 return posix_error_with_allocated_filename(opath);
2124 Py_DECREF(opath);
2125 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002126}
2127#endif /* HAVE_LCHMOD */
2128
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002129
Thomas Wouterscf297e42007-02-23 15:07:44 +00002130#ifdef HAVE_CHFLAGS
2131PyDoc_STRVAR(posix_chflags__doc__,
2132"chflags(path, flags)\n\n\
2133Set file flags.");
2134
2135static PyObject *
2136posix_chflags(PyObject *self, PyObject *args)
2137{
Victor Stinner8c62be82010-05-06 00:08:46 +00002138 PyObject *opath;
2139 char *path;
2140 unsigned long flags;
2141 int res;
2142 if (!PyArg_ParseTuple(args, "O&k:chflags",
2143 PyUnicode_FSConverter, &opath, &flags))
2144 return NULL;
2145 path = PyBytes_AsString(opath);
2146 Py_BEGIN_ALLOW_THREADS
2147 res = chflags(path, flags);
2148 Py_END_ALLOW_THREADS
2149 if (res < 0)
2150 return posix_error_with_allocated_filename(opath);
2151 Py_DECREF(opath);
2152 Py_INCREF(Py_None);
2153 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002154}
2155#endif /* HAVE_CHFLAGS */
2156
2157#ifdef HAVE_LCHFLAGS
2158PyDoc_STRVAR(posix_lchflags__doc__,
2159"lchflags(path, flags)\n\n\
2160Set file flags.\n\
2161This function will not follow symbolic links.");
2162
2163static PyObject *
2164posix_lchflags(PyObject *self, PyObject *args)
2165{
Victor Stinner8c62be82010-05-06 00:08:46 +00002166 PyObject *opath;
2167 char *path;
2168 unsigned long flags;
2169 int res;
2170 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2171 PyUnicode_FSConverter, &opath, &flags))
2172 return NULL;
2173 path = PyBytes_AsString(opath);
2174 Py_BEGIN_ALLOW_THREADS
2175 res = lchflags(path, flags);
2176 Py_END_ALLOW_THREADS
2177 if (res < 0)
2178 return posix_error_with_allocated_filename(opath);
2179 Py_DECREF(opath);
2180 Py_INCREF(Py_None);
2181 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002182}
2183#endif /* HAVE_LCHFLAGS */
2184
Martin v. Löwis244edc82001-10-04 22:44:26 +00002185#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002186PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002187"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002188Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002189
2190static PyObject *
2191posix_chroot(PyObject *self, PyObject *args)
2192{
Victor Stinner8c62be82010-05-06 00:08:46 +00002193 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002194}
2195#endif
2196
Guido van Rossum21142a01999-01-08 21:05:37 +00002197#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002198PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002199"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002200force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002201
2202static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002203posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002204{
Stefan Krah0e803b32010-11-26 16:16:47 +00002205 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002206}
2207#endif /* HAVE_FSYNC */
2208
Ross Lagerwall7807c352011-03-17 20:20:30 +02002209#ifdef HAVE_SYNC
2210PyDoc_STRVAR(posix_sync__doc__,
2211"sync()\n\n\
2212Force write of everything to disk.");
2213
2214static PyObject *
2215posix_sync(PyObject *self, PyObject *noargs)
2216{
2217 Py_BEGIN_ALLOW_THREADS
2218 sync();
2219 Py_END_ALLOW_THREADS
2220 Py_RETURN_NONE;
2221}
2222#endif
2223
Guido van Rossum21142a01999-01-08 21:05:37 +00002224#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002225
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002226#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002227extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2228#endif
2229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002230PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002231"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002232force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002233 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002234
2235static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002236posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002237{
Stefan Krah0e803b32010-11-26 16:16:47 +00002238 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002239}
2240#endif /* HAVE_FDATASYNC */
2241
2242
Fredrik Lundh10723342000-07-10 16:38:09 +00002243#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002244PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002245"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002246Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002247
Barry Warsaw53699e91996-12-10 23:23:01 +00002248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002249posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250{
Victor Stinner8c62be82010-05-06 00:08:46 +00002251 PyObject *opath;
2252 char *path;
2253 long uid, gid;
2254 int res;
2255 if (!PyArg_ParseTuple(args, "O&ll:chown",
2256 PyUnicode_FSConverter, &opath,
2257 &uid, &gid))
2258 return NULL;
2259 path = PyBytes_AsString(opath);
2260 Py_BEGIN_ALLOW_THREADS
2261 res = chown(path, (uid_t) uid, (gid_t) gid);
2262 Py_END_ALLOW_THREADS
2263 if (res < 0)
2264 return posix_error_with_allocated_filename(opath);
2265 Py_DECREF(opath);
2266 Py_INCREF(Py_None);
2267 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002268}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002269#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002270
Christian Heimes4e30a842007-11-30 22:12:06 +00002271#ifdef HAVE_FCHOWN
2272PyDoc_STRVAR(posix_fchown__doc__,
2273"fchown(fd, uid, gid)\n\n\
2274Change the owner and group id of the file given by file descriptor\n\
2275fd to the numeric uid and gid.");
2276
2277static PyObject *
2278posix_fchown(PyObject *self, PyObject *args)
2279{
Victor Stinner8c62be82010-05-06 00:08:46 +00002280 int fd;
2281 long uid, gid;
2282 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002283 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002284 return NULL;
2285 Py_BEGIN_ALLOW_THREADS
2286 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2287 Py_END_ALLOW_THREADS
2288 if (res < 0)
2289 return posix_error();
2290 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002291}
2292#endif /* HAVE_FCHOWN */
2293
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002294#ifdef HAVE_LCHOWN
2295PyDoc_STRVAR(posix_lchown__doc__,
2296"lchown(path, uid, gid)\n\n\
2297Change the owner and group id of path to the numeric uid and gid.\n\
2298This function will not follow symbolic links.");
2299
2300static PyObject *
2301posix_lchown(PyObject *self, PyObject *args)
2302{
Victor Stinner8c62be82010-05-06 00:08:46 +00002303 PyObject *opath;
2304 char *path;
2305 long uid, gid;
2306 int res;
2307 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2308 PyUnicode_FSConverter, &opath,
2309 &uid, &gid))
2310 return NULL;
2311 path = PyBytes_AsString(opath);
2312 Py_BEGIN_ALLOW_THREADS
2313 res = lchown(path, (uid_t) uid, (gid_t) gid);
2314 Py_END_ALLOW_THREADS
2315 if (res < 0)
2316 return posix_error_with_allocated_filename(opath);
2317 Py_DECREF(opath);
2318 Py_INCREF(Py_None);
2319 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002320}
2321#endif /* HAVE_LCHOWN */
2322
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002323
Guido van Rossum36bc6801995-06-14 22:54:23 +00002324#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002325static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002326posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002327{
Victor Stinner8c62be82010-05-06 00:08:46 +00002328 char buf[1026];
2329 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002331#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002332 if (!use_bytes) {
2333 wchar_t wbuf[1026];
2334 wchar_t *wbuf2 = wbuf;
2335 PyObject *resobj;
2336 DWORD len;
2337 Py_BEGIN_ALLOW_THREADS
2338 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2339 /* If the buffer is large enough, len does not include the
2340 terminating \0. If the buffer is too small, len includes
2341 the space needed for the terminator. */
2342 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2343 wbuf2 = malloc(len * sizeof(wchar_t));
2344 if (wbuf2)
2345 len = GetCurrentDirectoryW(len, wbuf2);
2346 }
2347 Py_END_ALLOW_THREADS
2348 if (!wbuf2) {
2349 PyErr_NoMemory();
2350 return NULL;
2351 }
2352 if (!len) {
2353 if (wbuf2 != wbuf) free(wbuf2);
2354 return win32_error("getcwdu", NULL);
2355 }
2356 resobj = PyUnicode_FromWideChar(wbuf2, len);
2357 if (wbuf2 != wbuf) free(wbuf2);
2358 return resobj;
2359 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002360#endif
2361
Victor Stinner8c62be82010-05-06 00:08:46 +00002362 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002363#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002364 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002365#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002367#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 Py_END_ALLOW_THREADS
2369 if (res == NULL)
2370 return posix_error();
2371 if (use_bytes)
2372 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002373 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002374}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002375
2376PyDoc_STRVAR(posix_getcwd__doc__,
2377"getcwd() -> path\n\n\
2378Return a unicode string representing the current working directory.");
2379
2380static PyObject *
2381posix_getcwd_unicode(PyObject *self)
2382{
2383 return posix_getcwd(0);
2384}
2385
2386PyDoc_STRVAR(posix_getcwdb__doc__,
2387"getcwdb() -> path\n\n\
2388Return a bytes string representing the current working directory.");
2389
2390static PyObject *
2391posix_getcwd_bytes(PyObject *self)
2392{
2393 return posix_getcwd(1);
2394}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002395#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002396
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002397
Guido van Rossumb6775db1994-08-01 11:34:53 +00002398#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002399PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002400"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002401Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002402
Barry Warsaw53699e91996-12-10 23:23:01 +00002403static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002404posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002405{
Victor Stinner8c62be82010-05-06 00:08:46 +00002406 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002407}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002408#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002409
Brian Curtin1b9df392010-11-24 20:24:31 +00002410#ifdef MS_WINDOWS
2411PyDoc_STRVAR(win32_link__doc__,
2412"link(src, dst)\n\n\
2413Create a hard link to a file.");
2414
2415static PyObject *
2416win32_link(PyObject *self, PyObject *args)
2417{
2418 PyObject *osrc, *odst;
2419 char *src, *dst;
2420 BOOL rslt;
2421
Brian Curtinfc889c42010-11-28 23:59:46 +00002422 PyUnicodeObject *usrc, *udst;
2423 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2424 Py_BEGIN_ALLOW_THREADS
2425 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2426 PyUnicode_AS_UNICODE(usrc), NULL);
2427 Py_END_ALLOW_THREADS
2428
2429 if (rslt == 0)
2430 return win32_error("link", NULL);
2431
2432 Py_RETURN_NONE;
2433 }
2434
2435 /* Narrow strings also valid. */
2436 PyErr_Clear();
2437
Brian Curtin1b9df392010-11-24 20:24:31 +00002438 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2439 PyUnicode_FSConverter, &odst))
2440 return NULL;
2441
2442 src = PyBytes_AsString(osrc);
2443 dst = PyBytes_AsString(odst);
2444
2445 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002446 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002447 Py_END_ALLOW_THREADS
2448
Stefan Krah30b341f2010-11-27 11:44:18 +00002449 Py_DECREF(osrc);
2450 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002451 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002452 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002453
2454 Py_RETURN_NONE;
2455}
2456#endif /* MS_WINDOWS */
2457
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002459PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002460"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002461Return a list containing the names of the entries in the directory.\n\
2462\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002463 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002464\n\
2465The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002466entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002467
Barry Warsaw53699e91996-12-10 23:23:01 +00002468static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002469posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002470{
Victor Stinner8c62be82010-05-06 00:08:46 +00002471 /* XXX Should redo this putting the (now four) versions of opendir
2472 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002473#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002474
Victor Stinner8c62be82010-05-06 00:08:46 +00002475 PyObject *d, *v;
2476 HANDLE hFindFile;
2477 BOOL result;
2478 WIN32_FIND_DATA FileData;
2479 PyObject *opath;
2480 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2481 char *bufptr = namebuf;
2482 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002483
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002484 PyObject *po = NULL;
2485 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002487 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002488
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002489 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002490 po_wchars = L".";
2491 len = 1;
2492 } else {
2493 po_wchars = PyUnicode_AS_UNICODE(po);
2494 len = PyUnicode_GET_SIZE(po);
2495 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002496 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002497 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2498 if (!wnamebuf) {
2499 PyErr_NoMemory();
2500 return NULL;
2501 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002502 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002503 if (len > 0) {
2504 Py_UNICODE wch = wnamebuf[len-1];
2505 if (wch != L'/' && wch != L'\\' && wch != L':')
2506 wnamebuf[len++] = L'\\';
2507 wcscpy(wnamebuf + len, L"*.*");
2508 }
2509 if ((d = PyList_New(0)) == NULL) {
2510 free(wnamebuf);
2511 return NULL;
2512 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002513 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002514 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002515 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002516 if (hFindFile == INVALID_HANDLE_VALUE) {
2517 int error = GetLastError();
2518 if (error == ERROR_FILE_NOT_FOUND) {
2519 free(wnamebuf);
2520 return d;
2521 }
2522 Py_DECREF(d);
2523 win32_error_unicode("FindFirstFileW", wnamebuf);
2524 free(wnamebuf);
2525 return NULL;
2526 }
2527 do {
2528 /* Skip over . and .. */
2529 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2530 wcscmp(wFileData.cFileName, L"..") != 0) {
2531 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2532 if (v == NULL) {
2533 Py_DECREF(d);
2534 d = NULL;
2535 break;
2536 }
2537 if (PyList_Append(d, v) != 0) {
2538 Py_DECREF(v);
2539 Py_DECREF(d);
2540 d = NULL;
2541 break;
2542 }
2543 Py_DECREF(v);
2544 }
2545 Py_BEGIN_ALLOW_THREADS
2546 result = FindNextFileW(hFindFile, &wFileData);
2547 Py_END_ALLOW_THREADS
2548 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2549 it got to the end of the directory. */
2550 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2551 Py_DECREF(d);
2552 win32_error_unicode("FindNextFileW", wnamebuf);
2553 FindClose(hFindFile);
2554 free(wnamebuf);
2555 return NULL;
2556 }
2557 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002558
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 if (FindClose(hFindFile) == FALSE) {
2560 Py_DECREF(d);
2561 win32_error_unicode("FindClose", wnamebuf);
2562 free(wnamebuf);
2563 return NULL;
2564 }
2565 free(wnamebuf);
2566 return d;
2567 }
2568 /* Drop the argument parsing error as narrow strings
2569 are also valid. */
2570 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002571
Victor Stinner8c62be82010-05-06 00:08:46 +00002572 if (!PyArg_ParseTuple(args, "O&:listdir",
2573 PyUnicode_FSConverter, &opath))
2574 return NULL;
2575 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2576 PyErr_SetString(PyExc_ValueError, "path too long");
2577 Py_DECREF(opath);
2578 return NULL;
2579 }
2580 strcpy(namebuf, PyBytes_AsString(opath));
2581 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002582 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 if (len > 0) {
2584 char ch = namebuf[len-1];
2585 if (ch != SEP && ch != ALTSEP && ch != ':')
2586 namebuf[len++] = '/';
2587 strcpy(namebuf + len, "*.*");
2588 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002589
Victor Stinner8c62be82010-05-06 00:08:46 +00002590 if ((d = PyList_New(0)) == NULL)
2591 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002592
Antoine Pitroub73caab2010-08-09 23:39:31 +00002593 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002595 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002596 if (hFindFile == INVALID_HANDLE_VALUE) {
2597 int error = GetLastError();
2598 if (error == ERROR_FILE_NOT_FOUND)
2599 return d;
2600 Py_DECREF(d);
2601 return win32_error("FindFirstFile", namebuf);
2602 }
2603 do {
2604 /* Skip over . and .. */
2605 if (strcmp(FileData.cFileName, ".") != 0 &&
2606 strcmp(FileData.cFileName, "..") != 0) {
2607 v = PyBytes_FromString(FileData.cFileName);
2608 if (v == NULL) {
2609 Py_DECREF(d);
2610 d = NULL;
2611 break;
2612 }
2613 if (PyList_Append(d, v) != 0) {
2614 Py_DECREF(v);
2615 Py_DECREF(d);
2616 d = NULL;
2617 break;
2618 }
2619 Py_DECREF(v);
2620 }
2621 Py_BEGIN_ALLOW_THREADS
2622 result = FindNextFile(hFindFile, &FileData);
2623 Py_END_ALLOW_THREADS
2624 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2625 it got to the end of the directory. */
2626 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2627 Py_DECREF(d);
2628 win32_error("FindNextFile", namebuf);
2629 FindClose(hFindFile);
2630 return NULL;
2631 }
2632 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002633
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 if (FindClose(hFindFile) == FALSE) {
2635 Py_DECREF(d);
2636 return win32_error("FindClose", namebuf);
2637 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002638
Victor Stinner8c62be82010-05-06 00:08:46 +00002639 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002640
Tim Peters0bb44a42000-09-15 07:44:49 +00002641#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002642
2643#ifndef MAX_PATH
2644#define MAX_PATH CCHMAXPATH
2645#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002646 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002647 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002648 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002649 PyObject *d, *v;
2650 char namebuf[MAX_PATH+5];
2651 HDIR hdir = 1;
2652 ULONG srchcnt = 1;
2653 FILEFINDBUF3 ep;
2654 APIRET rc;
2655
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002657 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002658 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002659 name = PyBytes_AsString(oname);
2660 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002661 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002662 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002663 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002664 return NULL;
2665 }
2666 strcpy(namebuf, name);
2667 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002668 if (*pt == ALTSEP)
2669 *pt = SEP;
2670 if (namebuf[len-1] != SEP)
2671 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002672 strcpy(namebuf + len, "*.*");
2673
Neal Norwitz6c913782007-10-14 03:23:09 +00002674 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002675 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002676 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002677 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002678
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002679 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2680 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002681 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002682 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2683 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2684 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002685
2686 if (rc != NO_ERROR) {
2687 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002688 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002689 }
2690
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002691 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002692 do {
2693 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002694 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002695 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002696
2697 strcpy(namebuf, ep.achName);
2698
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002699 /* Leave Case of Name Alone -- In Native Form */
2700 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002701
Christian Heimes72b710a2008-05-26 13:28:38 +00002702 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002703 if (v == NULL) {
2704 Py_DECREF(d);
2705 d = NULL;
2706 break;
2707 }
2708 if (PyList_Append(d, v) != 0) {
2709 Py_DECREF(v);
2710 Py_DECREF(d);
2711 d = NULL;
2712 break;
2713 }
2714 Py_DECREF(v);
2715 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2716 }
2717
Victor Stinnerdcb24032010-04-22 12:08:36 +00002718 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002719 return d;
2720#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002721 PyObject *oname;
2722 char *name;
2723 PyObject *d, *v;
2724 DIR *dirp;
2725 struct dirent *ep;
2726 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002727
Victor Stinner8c62be82010-05-06 00:08:46 +00002728 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002729 /* v is never read, so it does not need to be initialized yet. */
2730 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002731 arg_is_unicode = 0;
2732 PyErr_Clear();
2733 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002734 oname = NULL;
2735 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002736 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002737 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002738 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002739 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002740 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002741 Py_BEGIN_ALLOW_THREADS
2742 dirp = opendir(name);
2743 Py_END_ALLOW_THREADS
2744 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002745 return posix_error_with_allocated_filename(oname);
2746 }
2747 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002748 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002750 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 Py_DECREF(oname);
2752 return NULL;
2753 }
2754 for (;;) {
2755 errno = 0;
2756 Py_BEGIN_ALLOW_THREADS
2757 ep = readdir(dirp);
2758 Py_END_ALLOW_THREADS
2759 if (ep == NULL) {
2760 if (errno == 0) {
2761 break;
2762 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002763 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002764 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002765 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002766 Py_DECREF(d);
2767 return posix_error_with_allocated_filename(oname);
2768 }
2769 }
2770 if (ep->d_name[0] == '.' &&
2771 (NAMLEN(ep) == 1 ||
2772 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2773 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002774 if (arg_is_unicode)
2775 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2776 else
2777 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002778 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002779 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002780 break;
2781 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 if (PyList_Append(d, v) != 0) {
2783 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002784 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002785 break;
2786 }
2787 Py_DECREF(v);
2788 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002789 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002790 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002791 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002792 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002793
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002795
Tim Peters0bb44a42000-09-15 07:44:49 +00002796#endif /* which OS */
2797} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002798
Antoine Pitrou8250e232011-02-25 23:41:16 +00002799#ifdef HAVE_FDOPENDIR
2800PyDoc_STRVAR(posix_fdlistdir__doc__,
2801"fdlistdir(fd) -> list_of_strings\n\n\
2802Like listdir(), but uses a file descriptor instead.\n\
2803After succesful execution of this function, fd will be closed.");
2804
2805static PyObject *
2806posix_fdlistdir(PyObject *self, PyObject *args)
2807{
2808 PyObject *d, *v;
2809 DIR *dirp;
2810 struct dirent *ep;
2811 int fd;
2812
2813 errno = 0;
2814 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2815 return NULL;
2816 Py_BEGIN_ALLOW_THREADS
2817 dirp = fdopendir(fd);
2818 Py_END_ALLOW_THREADS
2819 if (dirp == NULL) {
2820 close(fd);
2821 return posix_error();
2822 }
2823 if ((d = PyList_New(0)) == NULL) {
2824 Py_BEGIN_ALLOW_THREADS
2825 closedir(dirp);
2826 Py_END_ALLOW_THREADS
2827 return NULL;
2828 }
2829 for (;;) {
2830 errno = 0;
2831 Py_BEGIN_ALLOW_THREADS
2832 ep = readdir(dirp);
2833 Py_END_ALLOW_THREADS
2834 if (ep == NULL) {
2835 if (errno == 0) {
2836 break;
2837 } else {
2838 Py_BEGIN_ALLOW_THREADS
2839 closedir(dirp);
2840 Py_END_ALLOW_THREADS
2841 Py_DECREF(d);
2842 return posix_error();
2843 }
2844 }
2845 if (ep->d_name[0] == '.' &&
2846 (NAMLEN(ep) == 1 ||
2847 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2848 continue;
2849 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2850 if (v == NULL) {
2851 Py_CLEAR(d);
2852 break;
2853 }
2854 if (PyList_Append(d, v) != 0) {
2855 Py_DECREF(v);
2856 Py_CLEAR(d);
2857 break;
2858 }
2859 Py_DECREF(v);
2860 }
2861 Py_BEGIN_ALLOW_THREADS
2862 closedir(dirp);
2863 Py_END_ALLOW_THREADS
2864
2865 return d;
2866}
2867#endif
2868
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002869#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002870/* A helper function for abspath on win32 */
2871static PyObject *
2872posix__getfullpathname(PyObject *self, PyObject *args)
2873{
Victor Stinner8c62be82010-05-06 00:08:46 +00002874 PyObject *opath;
2875 char *path;
2876 char outbuf[MAX_PATH*2];
2877 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002878#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 PyUnicodeObject *po;
2880 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2881 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2882 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2883 Py_UNICODE *wtemp;
2884 DWORD result;
2885 PyObject *v;
2886 result = GetFullPathNameW(wpath,
2887 sizeof(woutbuf)/sizeof(woutbuf[0]),
2888 woutbuf, &wtemp);
2889 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2890 woutbufp = malloc(result * sizeof(Py_UNICODE));
2891 if (!woutbufp)
2892 return PyErr_NoMemory();
2893 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2894 }
2895 if (result)
2896 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2897 else
2898 v = win32_error_unicode("GetFullPathNameW", wpath);
2899 if (woutbufp != woutbuf)
2900 free(woutbufp);
2901 return v;
2902 }
2903 /* Drop the argument parsing error as narrow strings
2904 are also valid. */
2905 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002906
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002907#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002908 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2909 PyUnicode_FSConverter, &opath))
2910 return NULL;
2911 path = PyBytes_AsString(opath);
2912 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2913 outbuf, &temp)) {
2914 win32_error("GetFullPathName", path);
2915 Py_DECREF(opath);
2916 return NULL;
2917 }
2918 Py_DECREF(opath);
2919 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2920 return PyUnicode_Decode(outbuf, strlen(outbuf),
2921 Py_FileSystemDefaultEncoding, NULL);
2922 }
2923 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002924} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002925
Brian Curtind25aef52011-06-13 15:16:04 -05002926
Brian Curtinf5e76d02010-11-24 13:14:05 +00002927
Brian Curtind40e6f72010-07-08 21:39:08 +00002928/* A helper function for samepath on windows */
2929static PyObject *
2930posix__getfinalpathname(PyObject *self, PyObject *args)
2931{
2932 HANDLE hFile;
2933 int buf_size;
2934 wchar_t *target_path;
2935 int result_length;
2936 PyObject *result;
2937 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002938
Brian Curtin94622b02010-09-24 00:03:39 +00002939 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002940 return NULL;
2941 }
2942
2943 if(!check_GetFinalPathNameByHandle()) {
2944 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2945 NotImplementedError. */
2946 return PyErr_Format(PyExc_NotImplementedError,
2947 "GetFinalPathNameByHandle not available on this platform");
2948 }
2949
2950 hFile = CreateFileW(
2951 path,
2952 0, /* desired access */
2953 0, /* share mode */
2954 NULL, /* security attributes */
2955 OPEN_EXISTING,
2956 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2957 FILE_FLAG_BACKUP_SEMANTICS,
2958 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002959
Brian Curtind40e6f72010-07-08 21:39:08 +00002960 if(hFile == INVALID_HANDLE_VALUE) {
2961 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002962 return PyErr_Format(PyExc_RuntimeError,
2963 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002964 }
2965
2966 /* We have a good handle to the target, use it to determine the
2967 target path name. */
2968 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2969
2970 if(!buf_size)
2971 return win32_error_unicode("GetFinalPathNameByHandle", path);
2972
2973 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2974 if(!target_path)
2975 return PyErr_NoMemory();
2976
2977 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2978 buf_size, VOLUME_NAME_DOS);
2979 if(!result_length)
2980 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2981
2982 if(!CloseHandle(hFile))
2983 return win32_error_unicode("GetFinalPathNameByHandle", path);
2984
2985 target_path[result_length] = 0;
2986 result = PyUnicode_FromUnicode(target_path, result_length);
2987 free(target_path);
2988 return result;
2989
2990} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002991
2992static PyObject *
2993posix__getfileinformation(PyObject *self, PyObject *args)
2994{
2995 HANDLE hFile;
2996 BY_HANDLE_FILE_INFORMATION info;
2997 int fd;
2998
2999 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3000 return NULL;
3001
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003002 if (!_PyVerify_fd(fd))
3003 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003004
3005 hFile = (HANDLE)_get_osfhandle(fd);
3006 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003007 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003008
3009 if (!GetFileInformationByHandle(hFile, &info))
3010 return win32_error("_getfileinformation", NULL);
3011
3012 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3013 info.nFileIndexHigh,
3014 info.nFileIndexLow);
3015}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003016
Brian Curtin95d028f2011-06-09 09:10:38 -05003017PyDoc_STRVAR(posix__isdir__doc__,
3018"Return true if the pathname refers to an existing directory.");
3019
Brian Curtin9c669cc2011-06-08 18:17:18 -05003020static PyObject *
3021posix__isdir(PyObject *self, PyObject *args)
3022{
3023 PyObject *opath;
3024 char *path;
3025 PyUnicodeObject *po;
3026 DWORD attributes;
3027
3028 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3029 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3030
3031 attributes = GetFileAttributesW(wpath);
3032 if (attributes == INVALID_FILE_ATTRIBUTES)
3033 Py_RETURN_FALSE;
3034 goto check;
3035 }
3036 /* Drop the argument parsing error as narrow strings
3037 are also valid. */
3038 PyErr_Clear();
3039
3040 if (!PyArg_ParseTuple(args, "O&:_isdir",
3041 PyUnicode_FSConverter, &opath))
3042 return NULL;
3043
3044 path = PyBytes_AsString(opath);
3045 attributes = GetFileAttributesA(path);
3046 if (attributes == INVALID_FILE_ATTRIBUTES)
3047 Py_RETURN_FALSE;
3048
3049check:
3050 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3051 Py_RETURN_TRUE;
3052 else
3053 Py_RETURN_FALSE;
3054}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003055#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003057PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003058"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003059Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003060
Barry Warsaw53699e91996-12-10 23:23:01 +00003061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003062posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003063{
Victor Stinner8c62be82010-05-06 00:08:46 +00003064 int res;
3065 PyObject *opath;
3066 char *path;
3067 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003068
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003069#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003070 PyUnicodeObject *po;
3071 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3072 Py_BEGIN_ALLOW_THREADS
3073 /* PyUnicode_AS_UNICODE OK without thread lock as
3074 it is a simple dereference. */
3075 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3076 Py_END_ALLOW_THREADS
3077 if (!res)
3078 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3079 Py_INCREF(Py_None);
3080 return Py_None;
3081 }
3082 /* Drop the argument parsing error as narrow strings
3083 are also valid. */
3084 PyErr_Clear();
3085 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3086 PyUnicode_FSConverter, &opath, &mode))
3087 return NULL;
3088 path = PyBytes_AsString(opath);
3089 Py_BEGIN_ALLOW_THREADS
3090 /* PyUnicode_AS_UNICODE OK without thread lock as
3091 it is a simple dereference. */
3092 res = CreateDirectoryA(path, NULL);
3093 Py_END_ALLOW_THREADS
3094 if (!res) {
3095 win32_error("mkdir", path);
3096 Py_DECREF(opath);
3097 return NULL;
3098 }
3099 Py_DECREF(opath);
3100 Py_INCREF(Py_None);
3101 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003102#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003103
Victor Stinner8c62be82010-05-06 00:08:46 +00003104 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3105 PyUnicode_FSConverter, &opath, &mode))
3106 return NULL;
3107 path = PyBytes_AsString(opath);
3108 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003109#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003111#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003112 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003113#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 Py_END_ALLOW_THREADS
3115 if (res < 0)
3116 return posix_error_with_allocated_filename(opath);
3117 Py_DECREF(opath);
3118 Py_INCREF(Py_None);
3119 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003121}
3122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003124/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3125#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003126#include <sys/resource.h>
3127#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003128
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003129
3130#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003131PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003132"nice(inc) -> new_priority\n\n\
3133Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003134
Barry Warsaw53699e91996-12-10 23:23:01 +00003135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003136posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003137{
Victor Stinner8c62be82010-05-06 00:08:46 +00003138 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003139
Victor Stinner8c62be82010-05-06 00:08:46 +00003140 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3141 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003142
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 /* There are two flavours of 'nice': one that returns the new
3144 priority (as required by almost all standards out there) and the
3145 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3146 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003147
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 If we are of the nice family that returns the new priority, we
3149 need to clear errno before the call, and check if errno is filled
3150 before calling posix_error() on a returnvalue of -1, because the
3151 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003152
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 errno = 0;
3154 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003155#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003156 if (value == 0)
3157 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003158#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 if (value == -1 && errno != 0)
3160 /* either nice() or getpriority() returned an error */
3161 return posix_error();
3162 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003163}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003164#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003165
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003166
3167#ifdef HAVE_GETPRIORITY
3168PyDoc_STRVAR(posix_getpriority__doc__,
3169"getpriority(which, who) -> current_priority\n\n\
3170Get program scheduling priority.");
3171
3172static PyObject *
3173posix_getpriority(PyObject *self, PyObject *args)
3174{
3175 int which, who, retval;
3176
3177 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3178 return NULL;
3179 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003180 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003181 if (errno != 0)
3182 return posix_error();
3183 return PyLong_FromLong((long)retval);
3184}
3185#endif /* HAVE_GETPRIORITY */
3186
3187
3188#ifdef HAVE_SETPRIORITY
3189PyDoc_STRVAR(posix_setpriority__doc__,
3190"setpriority(which, who, prio) -> None\n\n\
3191Set program scheduling priority.");
3192
3193static PyObject *
3194posix_setpriority(PyObject *self, PyObject *args)
3195{
3196 int which, who, prio, retval;
3197
3198 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3199 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003200 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003201 if (retval == -1)
3202 return posix_error();
3203 Py_RETURN_NONE;
3204}
3205#endif /* HAVE_SETPRIORITY */
3206
3207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003208PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003209"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211
Barry Warsaw53699e91996-12-10 23:23:01 +00003212static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003213posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003214{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003215#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 PyObject *o1, *o2;
3217 char *p1, *p2;
3218 BOOL result;
3219 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3220 goto error;
3221 if (!convert_to_unicode(&o1))
3222 goto error;
3223 if (!convert_to_unicode(&o2)) {
3224 Py_DECREF(o1);
3225 goto error;
3226 }
3227 Py_BEGIN_ALLOW_THREADS
3228 result = MoveFileW(PyUnicode_AsUnicode(o1),
3229 PyUnicode_AsUnicode(o2));
3230 Py_END_ALLOW_THREADS
3231 Py_DECREF(o1);
3232 Py_DECREF(o2);
3233 if (!result)
3234 return win32_error("rename", NULL);
3235 Py_INCREF(Py_None);
3236 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003237error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003238 PyErr_Clear();
3239 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3240 return NULL;
3241 Py_BEGIN_ALLOW_THREADS
3242 result = MoveFileA(p1, p2);
3243 Py_END_ALLOW_THREADS
3244 if (!result)
3245 return win32_error("rename", NULL);
3246 Py_INCREF(Py_None);
3247 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003248#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003250#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003251}
3252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003254PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003255"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003256Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003257
Barry Warsaw53699e91996-12-10 23:23:01 +00003258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003259posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003260{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003261#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003262 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003263#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003265#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003266}
3267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003269PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003270"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003271Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003272
Barry Warsaw53699e91996-12-10 23:23:01 +00003273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003274posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003275{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003276#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003277 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003278#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003279 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003280#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003281}
3282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003283
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003284#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003285PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003286"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003287Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003288
Barry Warsaw53699e91996-12-10 23:23:01 +00003289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003290posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003291{
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003293#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003294 wchar_t *command;
3295 if (!PyArg_ParseTuple(args, "u:system", &command))
3296 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003297
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 Py_BEGIN_ALLOW_THREADS
3299 sts = _wsystem(command);
3300 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003301#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 PyObject *command_obj;
3303 char *command;
3304 if (!PyArg_ParseTuple(args, "O&:system",
3305 PyUnicode_FSConverter, &command_obj))
3306 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003307
Victor Stinner8c62be82010-05-06 00:08:46 +00003308 command = PyBytes_AsString(command_obj);
3309 Py_BEGIN_ALLOW_THREADS
3310 sts = system(command);
3311 Py_END_ALLOW_THREADS
3312 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003313#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003315}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003316#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003317
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003319PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003320"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003321Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003322
Barry Warsaw53699e91996-12-10 23:23:01 +00003323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003324posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003325{
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 int i;
3327 if (!PyArg_ParseTuple(args, "i:umask", &i))
3328 return NULL;
3329 i = (int)umask(i);
3330 if (i < 0)
3331 return posix_error();
3332 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003333}
3334
Brian Curtind40e6f72010-07-08 21:39:08 +00003335#ifdef MS_WINDOWS
3336
3337/* override the default DeleteFileW behavior so that directory
3338symlinks can be removed with this function, the same as with
3339Unix symlinks */
3340BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3341{
3342 WIN32_FILE_ATTRIBUTE_DATA info;
3343 WIN32_FIND_DATAW find_data;
3344 HANDLE find_data_handle;
3345 int is_directory = 0;
3346 int is_link = 0;
3347
3348 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3349 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003350
Brian Curtind40e6f72010-07-08 21:39:08 +00003351 /* Get WIN32_FIND_DATA structure for the path to determine if
3352 it is a symlink */
3353 if(is_directory &&
3354 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3355 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3356
3357 if(find_data_handle != INVALID_HANDLE_VALUE) {
3358 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3359 FindClose(find_data_handle);
3360 }
3361 }
3362 }
3363
3364 if (is_directory && is_link)
3365 return RemoveDirectoryW(lpFileName);
3366
3367 return DeleteFileW(lpFileName);
3368}
3369#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003371PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003372"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003373Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003376"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003377Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003378
Barry Warsaw53699e91996-12-10 23:23:01 +00003379static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003380posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003381{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003382#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003383 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3384 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003385#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003386 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003387#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003388}
3389
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003390
Guido van Rossumb6775db1994-08-01 11:34:53 +00003391#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003392PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003393"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003394Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003395
Barry Warsaw53699e91996-12-10 23:23:01 +00003396static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003397posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003398{
Victor Stinner8c62be82010-05-06 00:08:46 +00003399 struct utsname u;
3400 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003401
Victor Stinner8c62be82010-05-06 00:08:46 +00003402 Py_BEGIN_ALLOW_THREADS
3403 res = uname(&u);
3404 Py_END_ALLOW_THREADS
3405 if (res < 0)
3406 return posix_error();
3407 return Py_BuildValue("(sssss)",
3408 u.sysname,
3409 u.nodename,
3410 u.release,
3411 u.version,
3412 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003413}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003414#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003415
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003416static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003417extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003418{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003419 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003420 if (PyFloat_Check(t)) {
3421 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003422 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003423 if (!intobj)
3424 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003425#if SIZEOF_TIME_T > SIZEOF_LONG
3426 intval = PyLong_AsUnsignedLongLongMask(intobj);
3427#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003429#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 Py_DECREF(intobj);
3431 if (intval == -1 && PyErr_Occurred())
3432 return -1;
3433 *sec = intval;
3434 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3435 if (*usec < 0)
3436 /* If rounding gave us a negative number,
3437 truncate. */
3438 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003439 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003441#if SIZEOF_TIME_T > SIZEOF_LONG
3442 intval = PyLong_AsUnsignedLongLongMask(t);
3443#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003445#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 if (intval == -1 && PyErr_Occurred())
3447 return -1;
3448 *sec = intval;
3449 *usec = 0;
3450 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003451}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003453PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003454"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003455utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003456Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003457second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003458
Barry Warsaw53699e91996-12-10 23:23:01 +00003459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003460posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003461{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003462#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 PyObject *arg;
3464 PyUnicodeObject *obwpath;
3465 wchar_t *wpath = NULL;
3466 PyObject *oapath;
3467 char *apath;
3468 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003469 time_t atimesec, mtimesec;
3470 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003471 FILETIME atime, mtime;
3472 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003473
Victor Stinner8c62be82010-05-06 00:08:46 +00003474 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3475 wpath = PyUnicode_AS_UNICODE(obwpath);
3476 Py_BEGIN_ALLOW_THREADS
3477 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3478 NULL, OPEN_EXISTING,
3479 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3480 Py_END_ALLOW_THREADS
3481 if (hFile == INVALID_HANDLE_VALUE)
3482 return win32_error_unicode("utime", wpath);
3483 } else
3484 /* Drop the argument parsing error as narrow strings
3485 are also valid. */
3486 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003487
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 if (!wpath) {
3489 if (!PyArg_ParseTuple(args, "O&O:utime",
3490 PyUnicode_FSConverter, &oapath, &arg))
3491 return NULL;
3492 apath = PyBytes_AsString(oapath);
3493 Py_BEGIN_ALLOW_THREADS
3494 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3495 NULL, OPEN_EXISTING,
3496 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3497 Py_END_ALLOW_THREADS
3498 if (hFile == INVALID_HANDLE_VALUE) {
3499 win32_error("utime", apath);
3500 Py_DECREF(oapath);
3501 return NULL;
3502 }
3503 Py_DECREF(oapath);
3504 }
3505
3506 if (arg == Py_None) {
3507 SYSTEMTIME now;
3508 GetSystemTime(&now);
3509 if (!SystemTimeToFileTime(&now, &mtime) ||
3510 !SystemTimeToFileTime(&now, &atime)) {
3511 win32_error("utime", NULL);
3512 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003513 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 }
3515 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3516 PyErr_SetString(PyExc_TypeError,
3517 "utime() arg 2 must be a tuple (atime, mtime)");
3518 goto done;
3519 }
3520 else {
3521 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3522 &atimesec, &ausec) == -1)
3523 goto done;
3524 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3525 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3526 &mtimesec, &musec) == -1)
3527 goto done;
3528 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3529 }
3530 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3531 /* Avoid putting the file name into the error here,
3532 as that may confuse the user into believing that
3533 something is wrong with the file, when it also
3534 could be the time stamp that gives a problem. */
3535 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003536 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003537 }
3538 Py_INCREF(Py_None);
3539 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003540done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003541 CloseHandle(hFile);
3542 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003543#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003544
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 PyObject *opath;
3546 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003547 time_t atime, mtime;
3548 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003549 int res;
3550 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003551
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003552#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003554#define ATIME buf[0].tv_sec
3555#define MTIME buf[1].tv_sec
3556#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003557/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003559#define ATIME buf.actime
3560#define MTIME buf.modtime
3561#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003562#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003564#define ATIME buf[0]
3565#define MTIME buf[1]
3566#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003567#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003568
Mark Hammond817c9292003-12-03 01:22:38 +00003569
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 if (!PyArg_ParseTuple(args, "O&O:utime",
3571 PyUnicode_FSConverter, &opath, &arg))
3572 return NULL;
3573 path = PyBytes_AsString(opath);
3574 if (arg == Py_None) {
3575 /* optional time values not given */
3576 Py_BEGIN_ALLOW_THREADS
3577 res = utime(path, NULL);
3578 Py_END_ALLOW_THREADS
3579 }
3580 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3581 PyErr_SetString(PyExc_TypeError,
3582 "utime() arg 2 must be a tuple (atime, mtime)");
3583 Py_DECREF(opath);
3584 return NULL;
3585 }
3586 else {
3587 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3588 &atime, &ausec) == -1) {
3589 Py_DECREF(opath);
3590 return NULL;
3591 }
3592 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3593 &mtime, &musec) == -1) {
3594 Py_DECREF(opath);
3595 return NULL;
3596 }
3597 ATIME = atime;
3598 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003599#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003600 buf[0].tv_usec = ausec;
3601 buf[1].tv_usec = musec;
3602 Py_BEGIN_ALLOW_THREADS
3603 res = utimes(path, buf);
3604 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003605#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003606 Py_BEGIN_ALLOW_THREADS
3607 res = utime(path, UTIME_ARG);
3608 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003609#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 }
3611 if (res < 0) {
3612 return posix_error_with_allocated_filename(opath);
3613 }
3614 Py_DECREF(opath);
3615 Py_INCREF(Py_None);
3616 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003617#undef UTIME_ARG
3618#undef ATIME
3619#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003620#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003621}
3622
Ross Lagerwall7807c352011-03-17 20:20:30 +02003623#ifdef HAVE_FUTIMES
3624PyDoc_STRVAR(posix_futimes__doc__,
3625"futimes(fd, (atime, mtime))\n\
3626futimes(fd, None)\n\n\
3627Set the access and modified time of the file specified by the file\n\
3628descriptor fd to the given values. If the second form is used, set the\n\
3629access and modified times to the current time.");
3630
3631static PyObject *
3632posix_futimes(PyObject *self, PyObject *args)
3633{
3634 int res, fd;
3635 PyObject* arg;
3636 struct timeval buf[2];
3637 long ausec, musec;
3638
3639 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3640 return NULL;
3641
3642 if (arg == Py_None) {
3643 /* optional time values not given */
3644 Py_BEGIN_ALLOW_THREADS
3645 res = futimes(fd, NULL);
3646 Py_END_ALLOW_THREADS
3647 }
3648 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3649 PyErr_SetString(PyExc_TypeError,
3650 "futimes() arg 2 must be a tuple (atime, mtime)");
3651 return NULL;
3652 }
3653 else {
3654 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3655 &(buf[0].tv_sec), &ausec) == -1) {
3656 return NULL;
3657 }
3658 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3659 &(buf[1].tv_sec), &musec) == -1) {
3660 return NULL;
3661 }
3662 buf[0].tv_usec = ausec;
3663 buf[1].tv_usec = musec;
3664 Py_BEGIN_ALLOW_THREADS
3665 res = futimes(fd, buf);
3666 Py_END_ALLOW_THREADS
3667 }
3668 if (res < 0)
3669 return posix_error();
3670 Py_RETURN_NONE;
3671}
3672#endif
3673
3674#ifdef HAVE_LUTIMES
3675PyDoc_STRVAR(posix_lutimes__doc__,
3676"lutimes(path, (atime, mtime))\n\
3677lutimes(path, None)\n\n\
3678Like utime(), but if path is a symbolic link, it is not dereferenced.");
3679
3680static PyObject *
3681posix_lutimes(PyObject *self, PyObject *args)
3682{
3683 PyObject *opath, *arg;
3684 const char *path;
3685 int res;
3686 struct timeval buf[2];
3687 long ausec, musec;
3688
3689 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3690 PyUnicode_FSConverter, &opath, &arg))
3691 return NULL;
3692 path = PyBytes_AsString(opath);
3693 if (arg == Py_None) {
3694 /* optional time values not given */
3695 Py_BEGIN_ALLOW_THREADS
3696 res = lutimes(path, NULL);
3697 Py_END_ALLOW_THREADS
3698 }
3699 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3700 PyErr_SetString(PyExc_TypeError,
3701 "lutimes() arg 2 must be a tuple (atime, mtime)");
3702 Py_DECREF(opath);
3703 return NULL;
3704 }
3705 else {
3706 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3707 &(buf[0].tv_sec), &ausec) == -1) {
3708 Py_DECREF(opath);
3709 return NULL;
3710 }
3711 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3712 &(buf[1].tv_sec), &musec) == -1) {
3713 Py_DECREF(opath);
3714 return NULL;
3715 }
3716 buf[0].tv_usec = ausec;
3717 buf[1].tv_usec = musec;
3718 Py_BEGIN_ALLOW_THREADS
3719 res = lutimes(path, buf);
3720 Py_END_ALLOW_THREADS
3721 }
3722 Py_DECREF(opath);
3723 if (res < 0)
3724 return posix_error();
3725 Py_RETURN_NONE;
3726}
3727#endif
3728
3729#ifdef HAVE_FUTIMENS
3730PyDoc_STRVAR(posix_futimens__doc__,
3731"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3732futimens(fd, None, None)\n\n\
3733Updates the timestamps of a file specified by the file descriptor fd, with\n\
3734nanosecond precision.\n\
3735The second form sets atime and mtime to the current time.\n\
3736If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3737current time.\n\
3738If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3739
3740static PyObject *
3741posix_futimens(PyObject *self, PyObject *args)
3742{
3743 int res, fd;
3744 PyObject *atime, *mtime;
3745 struct timespec buf[2];
3746
3747 if (!PyArg_ParseTuple(args, "iOO:futimens",
3748 &fd, &atime, &mtime))
3749 return NULL;
3750 if (atime == Py_None && mtime == Py_None) {
3751 /* optional time values not given */
3752 Py_BEGIN_ALLOW_THREADS
3753 res = futimens(fd, NULL);
3754 Py_END_ALLOW_THREADS
3755 }
3756 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3757 PyErr_SetString(PyExc_TypeError,
3758 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3759 return NULL;
3760 }
3761 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3762 PyErr_SetString(PyExc_TypeError,
3763 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3764 return NULL;
3765 }
3766 else {
3767 if (!PyArg_ParseTuple(atime, "ll:futimens",
3768 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3769 return NULL;
3770 }
3771 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3772 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3773 return NULL;
3774 }
3775 Py_BEGIN_ALLOW_THREADS
3776 res = futimens(fd, buf);
3777 Py_END_ALLOW_THREADS
3778 }
3779 if (res < 0)
3780 return posix_error();
3781 Py_RETURN_NONE;
3782}
3783#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003784
Guido van Rossum3b066191991-06-04 19:40:25 +00003785/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003786
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Barry Warsaw53699e91996-12-10 23:23:01 +00003791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003792posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003793{
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 int sts;
3795 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3796 return NULL;
3797 _exit(sts);
3798 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003799}
3800
Martin v. Löwis114619e2002-10-07 06:44:21 +00003801#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3802static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003803free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003804{
Victor Stinner8c62be82010-05-06 00:08:46 +00003805 Py_ssize_t i;
3806 for (i = 0; i < count; i++)
3807 PyMem_Free(array[i]);
3808 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003809}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003810
Antoine Pitrou69f71142009-05-24 21:25:49 +00003811static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003812int fsconvert_strdup(PyObject *o, char**out)
3813{
Victor Stinner8c62be82010-05-06 00:08:46 +00003814 PyObject *bytes;
3815 Py_ssize_t size;
3816 if (!PyUnicode_FSConverter(o, &bytes))
3817 return 0;
3818 size = PyBytes_GET_SIZE(bytes);
3819 *out = PyMem_Malloc(size+1);
3820 if (!*out)
3821 return 0;
3822 memcpy(*out, PyBytes_AsString(bytes), size+1);
3823 Py_DECREF(bytes);
3824 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003825}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003826#endif
3827
Ross Lagerwall7807c352011-03-17 20:20:30 +02003828#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003829static char**
3830parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3831{
Victor Stinner8c62be82010-05-06 00:08:46 +00003832 char **envlist;
3833 Py_ssize_t i, pos, envc;
3834 PyObject *keys=NULL, *vals=NULL;
3835 PyObject *key, *val, *key2, *val2;
3836 char *p, *k, *v;
3837 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003838
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 i = PyMapping_Size(env);
3840 if (i < 0)
3841 return NULL;
3842 envlist = PyMem_NEW(char *, i + 1);
3843 if (envlist == NULL) {
3844 PyErr_NoMemory();
3845 return NULL;
3846 }
3847 envc = 0;
3848 keys = PyMapping_Keys(env);
3849 vals = PyMapping_Values(env);
3850 if (!keys || !vals)
3851 goto error;
3852 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3853 PyErr_Format(PyExc_TypeError,
3854 "env.keys() or env.values() is not a list");
3855 goto error;
3856 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003857
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 for (pos = 0; pos < i; pos++) {
3859 key = PyList_GetItem(keys, pos);
3860 val = PyList_GetItem(vals, pos);
3861 if (!key || !val)
3862 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003863
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 if (PyUnicode_FSConverter(key, &key2) == 0)
3865 goto error;
3866 if (PyUnicode_FSConverter(val, &val2) == 0) {
3867 Py_DECREF(key2);
3868 goto error;
3869 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003870
3871#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003872 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3873 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003874#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003875 k = PyBytes_AsString(key2);
3876 v = PyBytes_AsString(val2);
3877 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003878
Victor Stinner8c62be82010-05-06 00:08:46 +00003879 p = PyMem_NEW(char, len);
3880 if (p == NULL) {
3881 PyErr_NoMemory();
3882 Py_DECREF(key2);
3883 Py_DECREF(val2);
3884 goto error;
3885 }
3886 PyOS_snprintf(p, len, "%s=%s", k, v);
3887 envlist[envc++] = p;
3888 Py_DECREF(key2);
3889 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003890#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003891 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003892#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 }
3894 Py_DECREF(vals);
3895 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003896
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 envlist[envc] = 0;
3898 *envc_ptr = envc;
3899 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003900
3901error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003902 Py_XDECREF(keys);
3903 Py_XDECREF(vals);
3904 while (--envc >= 0)
3905 PyMem_DEL(envlist[envc]);
3906 PyMem_DEL(envlist);
3907 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003908}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003909
Ross Lagerwall7807c352011-03-17 20:20:30 +02003910static char**
3911parse_arglist(PyObject* argv, Py_ssize_t *argc)
3912{
3913 int i;
3914 char **argvlist = PyMem_NEW(char *, *argc+1);
3915 if (argvlist == NULL) {
3916 PyErr_NoMemory();
3917 return NULL;
3918 }
3919 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003920 PyObject* item = PySequence_ITEM(argv, i);
3921 if (item == NULL)
3922 goto fail;
3923 if (!fsconvert_strdup(item, &argvlist[i])) {
3924 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003925 goto fail;
3926 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003927 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003928 }
3929 argvlist[*argc] = NULL;
3930 return argvlist;
3931fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003932 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003933 free_string_array(argvlist, *argc);
3934 return NULL;
3935}
3936#endif
3937
3938#ifdef HAVE_EXECV
3939PyDoc_STRVAR(posix_execv__doc__,
3940"execv(path, args)\n\n\
3941Execute an executable path with arguments, replacing current process.\n\
3942\n\
3943 path: path of executable file\n\
3944 args: tuple or list of strings");
3945
3946static PyObject *
3947posix_execv(PyObject *self, PyObject *args)
3948{
3949 PyObject *opath;
3950 char *path;
3951 PyObject *argv;
3952 char **argvlist;
3953 Py_ssize_t argc;
3954
3955 /* execv has two arguments: (path, argv), where
3956 argv is a list or tuple of strings. */
3957
3958 if (!PyArg_ParseTuple(args, "O&O:execv",
3959 PyUnicode_FSConverter,
3960 &opath, &argv))
3961 return NULL;
3962 path = PyBytes_AsString(opath);
3963 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3964 PyErr_SetString(PyExc_TypeError,
3965 "execv() arg 2 must be a tuple or list");
3966 Py_DECREF(opath);
3967 return NULL;
3968 }
3969 argc = PySequence_Size(argv);
3970 if (argc < 1) {
3971 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3972 Py_DECREF(opath);
3973 return NULL;
3974 }
3975
3976 argvlist = parse_arglist(argv, &argc);
3977 if (argvlist == NULL) {
3978 Py_DECREF(opath);
3979 return NULL;
3980 }
3981
3982 execv(path, argvlist);
3983
3984 /* If we get here it's definitely an error */
3985
3986 free_string_array(argvlist, argc);
3987 Py_DECREF(opath);
3988 return posix_error();
3989}
3990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003991PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003992"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003993Execute a path with arguments and environment, replacing current process.\n\
3994\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 path: path of executable file\n\
3996 args: tuple or list of arguments\n\
3997 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003998
Barry Warsaw53699e91996-12-10 23:23:01 +00003999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004000posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004001{
Victor Stinner8c62be82010-05-06 00:08:46 +00004002 PyObject *opath;
4003 char *path;
4004 PyObject *argv, *env;
4005 char **argvlist;
4006 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004007 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004008
Victor Stinner8c62be82010-05-06 00:08:46 +00004009 /* execve has three arguments: (path, argv, env), where
4010 argv is a list or tuple of strings and env is a dictionary
4011 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004012
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 if (!PyArg_ParseTuple(args, "O&OO:execve",
4014 PyUnicode_FSConverter,
4015 &opath, &argv, &env))
4016 return NULL;
4017 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004018 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004019 PyErr_SetString(PyExc_TypeError,
4020 "execve() arg 2 must be a tuple or list");
4021 goto fail_0;
4022 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004023 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004024 if (!PyMapping_Check(env)) {
4025 PyErr_SetString(PyExc_TypeError,
4026 "execve() arg 3 must be a mapping object");
4027 goto fail_0;
4028 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004029
Ross Lagerwall7807c352011-03-17 20:20:30 +02004030 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 goto fail_0;
4033 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004034
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 envlist = parse_envlist(env, &envc);
4036 if (envlist == NULL)
4037 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004038
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004040
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004042
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004044
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 while (--envc >= 0)
4046 PyMem_DEL(envlist[envc]);
4047 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004048 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004049 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004050 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004051 Py_DECREF(opath);
4052 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004053}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004054#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004055
Ross Lagerwall7807c352011-03-17 20:20:30 +02004056#ifdef HAVE_FEXECVE
4057PyDoc_STRVAR(posix_fexecve__doc__,
4058"fexecve(fd, args, env)\n\n\
4059Execute the program specified by a file descriptor with arguments and\n\
4060environment, replacing the current process.\n\
4061\n\
4062 fd: file descriptor of executable\n\
4063 args: tuple or list of arguments\n\
4064 env: dictionary of strings mapping to strings");
4065
4066static PyObject *
4067posix_fexecve(PyObject *self, PyObject *args)
4068{
4069 int fd;
4070 PyObject *argv, *env;
4071 char **argvlist;
4072 char **envlist;
4073 Py_ssize_t argc, envc;
4074
4075 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4076 &fd, &argv, &env))
4077 return NULL;
4078 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4079 PyErr_SetString(PyExc_TypeError,
4080 "fexecve() arg 2 must be a tuple or list");
4081 return NULL;
4082 }
4083 argc = PySequence_Size(argv);
4084 if (!PyMapping_Check(env)) {
4085 PyErr_SetString(PyExc_TypeError,
4086 "fexecve() arg 3 must be a mapping object");
4087 return NULL;
4088 }
4089
4090 argvlist = parse_arglist(argv, &argc);
4091 if (argvlist == NULL)
4092 return NULL;
4093
4094 envlist = parse_envlist(env, &envc);
4095 if (envlist == NULL)
4096 goto fail;
4097
4098 fexecve(fd, argvlist, envlist);
4099
4100 /* If we get here it's definitely an error */
4101
4102 (void) posix_error();
4103
4104 while (--envc >= 0)
4105 PyMem_DEL(envlist[envc]);
4106 PyMem_DEL(envlist);
4107 fail:
4108 free_string_array(argvlist, argc);
4109 return NULL;
4110}
4111#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004112
Guido van Rossuma1065681999-01-25 23:20:23 +00004113#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004114PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004115"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004116Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004117\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004118 mode: mode of process creation\n\
4119 path: path of executable file\n\
4120 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004121
4122static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004123posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004124{
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 PyObject *opath;
4126 char *path;
4127 PyObject *argv;
4128 char **argvlist;
4129 int mode, i;
4130 Py_ssize_t argc;
4131 Py_intptr_t spawnval;
4132 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004133
Victor Stinner8c62be82010-05-06 00:08:46 +00004134 /* spawnv has three arguments: (mode, path, argv), where
4135 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004136
Victor Stinner8c62be82010-05-06 00:08:46 +00004137 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4138 PyUnicode_FSConverter,
4139 &opath, &argv))
4140 return NULL;
4141 path = PyBytes_AsString(opath);
4142 if (PyList_Check(argv)) {
4143 argc = PyList_Size(argv);
4144 getitem = PyList_GetItem;
4145 }
4146 else if (PyTuple_Check(argv)) {
4147 argc = PyTuple_Size(argv);
4148 getitem = PyTuple_GetItem;
4149 }
4150 else {
4151 PyErr_SetString(PyExc_TypeError,
4152 "spawnv() arg 2 must be a tuple or list");
4153 Py_DECREF(opath);
4154 return NULL;
4155 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004156
Victor Stinner8c62be82010-05-06 00:08:46 +00004157 argvlist = PyMem_NEW(char *, argc+1);
4158 if (argvlist == NULL) {
4159 Py_DECREF(opath);
4160 return PyErr_NoMemory();
4161 }
4162 for (i = 0; i < argc; i++) {
4163 if (!fsconvert_strdup((*getitem)(argv, i),
4164 &argvlist[i])) {
4165 free_string_array(argvlist, i);
4166 PyErr_SetString(
4167 PyExc_TypeError,
4168 "spawnv() arg 2 must contain only strings");
4169 Py_DECREF(opath);
4170 return NULL;
4171 }
4172 }
4173 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004174
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004175#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 Py_BEGIN_ALLOW_THREADS
4177 spawnval = spawnv(mode, path, argvlist);
4178 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004179#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 if (mode == _OLD_P_OVERLAY)
4181 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004182
Victor Stinner8c62be82010-05-06 00:08:46 +00004183 Py_BEGIN_ALLOW_THREADS
4184 spawnval = _spawnv(mode, path, argvlist);
4185 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004186#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004187
Victor Stinner8c62be82010-05-06 00:08:46 +00004188 free_string_array(argvlist, argc);
4189 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004190
Victor Stinner8c62be82010-05-06 00:08:46 +00004191 if (spawnval == -1)
4192 return posix_error();
4193 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004194#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004195 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004196#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004197 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004198#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004199}
4200
4201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004202PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004203"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004204Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004205\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004206 mode: mode of process creation\n\
4207 path: path of executable file\n\
4208 args: tuple or list of arguments\n\
4209 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004210
4211static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004212posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004213{
Victor Stinner8c62be82010-05-06 00:08:46 +00004214 PyObject *opath;
4215 char *path;
4216 PyObject *argv, *env;
4217 char **argvlist;
4218 char **envlist;
4219 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004220 int mode;
4221 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004222 Py_intptr_t spawnval;
4223 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4224 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004225
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 /* spawnve has four arguments: (mode, path, argv, env), where
4227 argv is a list or tuple of strings and env is a dictionary
4228 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004229
Victor Stinner8c62be82010-05-06 00:08:46 +00004230 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4231 PyUnicode_FSConverter,
4232 &opath, &argv, &env))
4233 return NULL;
4234 path = PyBytes_AsString(opath);
4235 if (PyList_Check(argv)) {
4236 argc = PyList_Size(argv);
4237 getitem = PyList_GetItem;
4238 }
4239 else if (PyTuple_Check(argv)) {
4240 argc = PyTuple_Size(argv);
4241 getitem = PyTuple_GetItem;
4242 }
4243 else {
4244 PyErr_SetString(PyExc_TypeError,
4245 "spawnve() arg 2 must be a tuple or list");
4246 goto fail_0;
4247 }
4248 if (!PyMapping_Check(env)) {
4249 PyErr_SetString(PyExc_TypeError,
4250 "spawnve() arg 3 must be a mapping object");
4251 goto fail_0;
4252 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004253
Victor Stinner8c62be82010-05-06 00:08:46 +00004254 argvlist = PyMem_NEW(char *, argc+1);
4255 if (argvlist == NULL) {
4256 PyErr_NoMemory();
4257 goto fail_0;
4258 }
4259 for (i = 0; i < argc; i++) {
4260 if (!fsconvert_strdup((*getitem)(argv, i),
4261 &argvlist[i]))
4262 {
4263 lastarg = i;
4264 goto fail_1;
4265 }
4266 }
4267 lastarg = argc;
4268 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004269
Victor Stinner8c62be82010-05-06 00:08:46 +00004270 envlist = parse_envlist(env, &envc);
4271 if (envlist == NULL)
4272 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004273
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004274#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004275 Py_BEGIN_ALLOW_THREADS
4276 spawnval = spawnve(mode, path, argvlist, envlist);
4277 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004278#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004279 if (mode == _OLD_P_OVERLAY)
4280 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004281
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 Py_BEGIN_ALLOW_THREADS
4283 spawnval = _spawnve(mode, path, argvlist, envlist);
4284 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004285#endif
Tim Peters25059d32001-12-07 20:35:43 +00004286
Victor Stinner8c62be82010-05-06 00:08:46 +00004287 if (spawnval == -1)
4288 (void) posix_error();
4289 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004290#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004292#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004294#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004295
Victor Stinner8c62be82010-05-06 00:08:46 +00004296 while (--envc >= 0)
4297 PyMem_DEL(envlist[envc]);
4298 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004299 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004300 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004301 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 Py_DECREF(opath);
4303 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004304}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004305
4306/* OS/2 supports spawnvp & spawnvpe natively */
4307#if defined(PYOS_OS2)
4308PyDoc_STRVAR(posix_spawnvp__doc__,
4309"spawnvp(mode, file, args)\n\n\
4310Execute the program 'file' in a new process, using the environment\n\
4311search path to find the file.\n\
4312\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 mode: mode of process creation\n\
4314 file: executable file name\n\
4315 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004316
4317static PyObject *
4318posix_spawnvp(PyObject *self, PyObject *args)
4319{
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 PyObject *opath;
4321 char *path;
4322 PyObject *argv;
4323 char **argvlist;
4324 int mode, i, argc;
4325 Py_intptr_t spawnval;
4326 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004327
Victor Stinner8c62be82010-05-06 00:08:46 +00004328 /* spawnvp has three arguments: (mode, path, argv), where
4329 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004330
Victor Stinner8c62be82010-05-06 00:08:46 +00004331 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4332 PyUnicode_FSConverter,
4333 &opath, &argv))
4334 return NULL;
4335 path = PyBytes_AsString(opath);
4336 if (PyList_Check(argv)) {
4337 argc = PyList_Size(argv);
4338 getitem = PyList_GetItem;
4339 }
4340 else if (PyTuple_Check(argv)) {
4341 argc = PyTuple_Size(argv);
4342 getitem = PyTuple_GetItem;
4343 }
4344 else {
4345 PyErr_SetString(PyExc_TypeError,
4346 "spawnvp() arg 2 must be a tuple or list");
4347 Py_DECREF(opath);
4348 return NULL;
4349 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004350
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 argvlist = PyMem_NEW(char *, argc+1);
4352 if (argvlist == NULL) {
4353 Py_DECREF(opath);
4354 return PyErr_NoMemory();
4355 }
4356 for (i = 0; i < argc; i++) {
4357 if (!fsconvert_strdup((*getitem)(argv, i),
4358 &argvlist[i])) {
4359 free_string_array(argvlist, i);
4360 PyErr_SetString(
4361 PyExc_TypeError,
4362 "spawnvp() arg 2 must contain only strings");
4363 Py_DECREF(opath);
4364 return NULL;
4365 }
4366 }
4367 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004368
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004370#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004372#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004376
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 free_string_array(argvlist, argc);
4378 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004379
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 if (spawnval == -1)
4381 return posix_error();
4382 else
4383 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004384}
4385
4386
4387PyDoc_STRVAR(posix_spawnvpe__doc__,
4388"spawnvpe(mode, file, args, env)\n\n\
4389Execute the program 'file' in a new process, using the environment\n\
4390search path to find the file.\n\
4391\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004392 mode: mode of process creation\n\
4393 file: executable file name\n\
4394 args: tuple or list of arguments\n\
4395 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004396
4397static PyObject *
4398posix_spawnvpe(PyObject *self, PyObject *args)
4399{
Victor Stinner8c62be82010-05-06 00:08:46 +00004400 PyObject *opath
4401 char *path;
4402 PyObject *argv, *env;
4403 char **argvlist;
4404 char **envlist;
4405 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004406 int mode;
4407 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004408 Py_intptr_t spawnval;
4409 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4410 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004411
Victor Stinner8c62be82010-05-06 00:08:46 +00004412 /* spawnvpe has four arguments: (mode, path, argv, env), where
4413 argv is a list or tuple of strings and env is a dictionary
4414 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004415
Victor Stinner8c62be82010-05-06 00:08:46 +00004416 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4417 PyUnicode_FSConverter,
4418 &opath, &argv, &env))
4419 return NULL;
4420 path = PyBytes_AsString(opath);
4421 if (PyList_Check(argv)) {
4422 argc = PyList_Size(argv);
4423 getitem = PyList_GetItem;
4424 }
4425 else if (PyTuple_Check(argv)) {
4426 argc = PyTuple_Size(argv);
4427 getitem = PyTuple_GetItem;
4428 }
4429 else {
4430 PyErr_SetString(PyExc_TypeError,
4431 "spawnvpe() arg 2 must be a tuple or list");
4432 goto fail_0;
4433 }
4434 if (!PyMapping_Check(env)) {
4435 PyErr_SetString(PyExc_TypeError,
4436 "spawnvpe() arg 3 must be a mapping object");
4437 goto fail_0;
4438 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004439
Victor Stinner8c62be82010-05-06 00:08:46 +00004440 argvlist = PyMem_NEW(char *, argc+1);
4441 if (argvlist == NULL) {
4442 PyErr_NoMemory();
4443 goto fail_0;
4444 }
4445 for (i = 0; i < argc; i++) {
4446 if (!fsconvert_strdup((*getitem)(argv, i),
4447 &argvlist[i]))
4448 {
4449 lastarg = i;
4450 goto fail_1;
4451 }
4452 }
4453 lastarg = argc;
4454 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004455
Victor Stinner8c62be82010-05-06 00:08:46 +00004456 envlist = parse_envlist(env, &envc);
4457 if (envlist == NULL)
4458 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004459
Victor Stinner8c62be82010-05-06 00:08:46 +00004460 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004461#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004462 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004463#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004465#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 if (spawnval == -1)
4469 (void) posix_error();
4470 else
4471 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004472
Victor Stinner8c62be82010-05-06 00:08:46 +00004473 while (--envc >= 0)
4474 PyMem_DEL(envlist[envc]);
4475 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004476 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004477 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004478 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004479 Py_DECREF(opath);
4480 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004481}
4482#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004483#endif /* HAVE_SPAWNV */
4484
4485
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004486#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004487PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004488"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004489Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4490\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004491Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004492
4493static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004494posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004495{
Victor Stinner8c62be82010-05-06 00:08:46 +00004496 pid_t pid;
4497 int result = 0;
4498 _PyImport_AcquireLock();
4499 pid = fork1();
4500 if (pid == 0) {
4501 /* child: this clobbers and resets the import lock. */
4502 PyOS_AfterFork();
4503 } else {
4504 /* parent: release the import lock. */
4505 result = _PyImport_ReleaseLock();
4506 }
4507 if (pid == -1)
4508 return posix_error();
4509 if (result < 0) {
4510 /* Don't clobber the OSError if the fork failed. */
4511 PyErr_SetString(PyExc_RuntimeError,
4512 "not holding the import lock");
4513 return NULL;
4514 }
4515 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004516}
4517#endif
4518
4519
Guido van Rossumad0ee831995-03-01 10:34:45 +00004520#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004521PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004522"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004523Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004524Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004525
Barry Warsaw53699e91996-12-10 23:23:01 +00004526static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004527posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004528{
Victor Stinner8c62be82010-05-06 00:08:46 +00004529 pid_t pid;
4530 int result = 0;
4531 _PyImport_AcquireLock();
4532 pid = fork();
4533 if (pid == 0) {
4534 /* child: this clobbers and resets the import lock. */
4535 PyOS_AfterFork();
4536 } else {
4537 /* parent: release the import lock. */
4538 result = _PyImport_ReleaseLock();
4539 }
4540 if (pid == -1)
4541 return posix_error();
4542 if (result < 0) {
4543 /* Don't clobber the OSError if the fork failed. */
4544 PyErr_SetString(PyExc_RuntimeError,
4545 "not holding the import lock");
4546 return NULL;
4547 }
4548 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004549}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004550#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004551
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004552#ifdef HAVE_SCHED_H
4553
4554PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4555"sched_get_priority_max(policy)\n\n\
4556Get the maximum scheduling priority for *policy*.");
4557
4558static PyObject *
4559posix_sched_get_priority_max(PyObject *self, PyObject *args)
4560{
4561 int policy, max;
4562
4563 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4564 return NULL;
4565 max = sched_get_priority_max(policy);
4566 if (max < 0)
4567 return posix_error();
4568 return PyLong_FromLong(max);
4569}
4570
4571PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4572"sched_get_priority_min(policy)\n\n\
4573Get the minimum scheduling priority for *policy*.");
4574
4575static PyObject *
4576posix_sched_get_priority_min(PyObject *self, PyObject *args)
4577{
4578 int policy, min;
4579
4580 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4581 return NULL;
4582 min = sched_get_priority_min(policy);
4583 if (min < 0)
4584 return posix_error();
4585 return PyLong_FromLong(min);
4586}
4587
4588PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4589"sched_getscheduler(pid)\n\n\
4590Get the scheduling policy for the process with a PID of *pid*.\n\
4591Passing a PID of 0 returns the scheduling policy for the calling process.");
4592
4593static PyObject *
4594posix_sched_getscheduler(PyObject *self, PyObject *args)
4595{
4596 pid_t pid;
4597 int policy;
4598
4599 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4600 return NULL;
4601 policy = sched_getscheduler(pid);
4602 if (policy < 0)
4603 return posix_error();
4604 return PyLong_FromLong(policy);
4605}
4606
4607static PyObject *
4608sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4609{
4610 PyObject *res, *priority;
4611 static char *kwlist[] = {"sched_priority"};
4612
4613 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4614 return NULL;
4615 res = PyStructSequence_New(type);
4616 if (!res)
4617 return NULL;
4618 Py_INCREF(priority);
4619 PyStructSequence_SET_ITEM(res, 0, priority);
4620 return res;
4621}
4622
4623PyDoc_STRVAR(sched_param__doc__,
4624"sched_param(sched_priority): A scheduling parameter.\n\n\
4625Current has only one field: sched_priority");
4626
4627static PyStructSequence_Field sched_param_fields[] = {
4628 {"sched_priority", "the scheduling priority"},
4629 {0}
4630};
4631
4632static PyStructSequence_Desc sched_param_desc = {
4633 "sched_param", /* name */
4634 sched_param__doc__, /* doc */
4635 sched_param_fields,
4636 1
4637};
4638
4639static int
4640convert_sched_param(PyObject *param, struct sched_param *res)
4641{
4642 long priority;
4643
4644 if (Py_TYPE(param) != &SchedParamType) {
4645 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4646 return 0;
4647 }
4648 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4649 if (priority == -1 && PyErr_Occurred())
4650 return 0;
4651 if (priority > INT_MAX || priority < INT_MIN) {
4652 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4653 return 0;
4654 }
4655 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4656 return 1;
4657}
4658
4659PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4660"sched_setscheduler(pid, policy, param)\n\n\
4661Set the scheduling policy, *policy*, for *pid*.\n\
4662If *pid* is 0, the calling process is changed.\n\
4663*param* is an instance of sched_param.");
4664
4665static PyObject *
4666posix_sched_setscheduler(PyObject *self, PyObject *args)
4667{
4668 pid_t pid;
4669 int policy;
4670 struct sched_param param;
4671
4672 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4673 &pid, &policy, &convert_sched_param, &param))
4674 return NULL;
4675 if (sched_setscheduler(pid, policy, &param))
4676 return posix_error();
4677 Py_RETURN_NONE;
4678}
4679
4680PyDoc_STRVAR(posix_sched_getparam__doc__,
4681"sched_getparam(pid) -> sched_param\n\n\
4682Returns scheduling parameters for the process with *pid* as an instance of the\n\
4683sched_param class. A PID of 0 means the calling process.");
4684
4685static PyObject *
4686posix_sched_getparam(PyObject *self, PyObject *args)
4687{
4688 pid_t pid;
4689 struct sched_param param;
4690 PyObject *res, *priority;
4691
4692 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4693 return NULL;
4694 if (sched_getparam(pid, &param))
4695 return posix_error();
4696 res = PyStructSequence_New(&SchedParamType);
4697 if (!res)
4698 return NULL;
4699 priority = PyLong_FromLong(param.sched_priority);
4700 if (!priority) {
4701 Py_DECREF(res);
4702 return NULL;
4703 }
4704 PyStructSequence_SET_ITEM(res, 0, priority);
4705 return res;
4706}
4707
4708PyDoc_STRVAR(posix_sched_setparam__doc__,
4709"sched_setparam(pid, param)\n\n\
4710Set scheduling parameters for a process with PID *pid*.\n\
4711A PID of 0 means the calling process.");
4712
4713static PyObject *
4714posix_sched_setparam(PyObject *self, PyObject *args)
4715{
4716 pid_t pid;
4717 struct sched_param param;
4718
4719 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4720 &pid, &convert_sched_param, &param))
4721 return NULL;
4722 if (sched_setparam(pid, &param))
4723 return posix_error();
4724 Py_RETURN_NONE;
4725}
4726
4727PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4728"sched_rr_get_interval(pid) -> float\n\n\
4729Return the round-robin quantum for the process with PID *pid* in seconds.");
4730
4731static PyObject *
4732posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4733{
4734 pid_t pid;
4735 struct timespec interval;
4736
4737 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4738 return NULL;
4739 if (sched_rr_get_interval(pid, &interval))
4740 return posix_error();
4741 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4742}
4743
4744PyDoc_STRVAR(posix_sched_yield__doc__,
4745"sched_yield()\n\n\
4746Voluntarily relinquish the CPU.");
4747
4748static PyObject *
4749posix_sched_yield(PyObject *self, PyObject *noargs)
4750{
4751 if (sched_yield())
4752 return posix_error();
4753 Py_RETURN_NONE;
4754}
4755
Benjamin Peterson2740af82011-08-02 17:41:34 -05004756#ifdef HAVE_SCHED_SETAFFINITY
4757
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004758typedef struct {
4759 PyObject_HEAD;
4760 Py_ssize_t size;
4761 int ncpus;
4762 cpu_set_t *set;
4763} Py_cpu_set;
4764
4765static PyTypeObject cpu_set_type;
4766
4767static void
4768cpu_set_dealloc(Py_cpu_set *set)
4769{
4770 assert(set->set);
4771 CPU_FREE(set->set);
4772 Py_TYPE(set)->tp_free(set);
4773}
4774
4775static Py_cpu_set *
4776make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4777{
4778 Py_cpu_set *set;
4779
4780 if (size < 0) {
4781 PyErr_SetString(PyExc_ValueError, "negative size");
4782 return NULL;
4783 }
4784 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4785 if (!set)
4786 return NULL;
4787 set->ncpus = size;
4788 set->size = CPU_ALLOC_SIZE(size);
4789 set->set = CPU_ALLOC(size);
4790 if (!set->set) {
4791 type->tp_free(set);
4792 PyErr_NoMemory();
4793 return NULL;
4794 }
4795 CPU_ZERO_S(set->size, set->set);
4796 return set;
4797}
4798
4799static PyObject *
4800cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4801{
4802 int size;
4803
4804 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4805 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4806 return NULL;
4807 return (PyObject *)make_new_cpu_set(type, size);
4808}
4809
4810static PyObject *
4811cpu_set_repr(Py_cpu_set *set)
4812{
4813 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4814}
4815
4816static Py_ssize_t
4817cpu_set_len(Py_cpu_set *set)
4818{
4819 return set->ncpus;
4820}
4821
4822static int
4823_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4824{
4825 int cpu;
4826 if (!PyArg_ParseTuple(args, requester, &cpu))
4827 return -1;
4828 if (cpu < 0) {
4829 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4830 return -1;
4831 }
4832 if (cpu >= set->ncpus) {
4833 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4834 return -1;
4835 }
4836 return cpu;
4837}
4838
4839PyDoc_STRVAR(cpu_set_set_doc,
4840"cpu_set.set(i)\n\n\
4841Add CPU *i* to the set.");
4842
4843static PyObject *
4844cpu_set_set(Py_cpu_set *set, PyObject *args)
4845{
4846 int cpu = _get_cpu(set, "i|set", args);
4847 if (cpu == -1)
4848 return NULL;
4849 CPU_SET_S(cpu, set->size, set->set);
4850 Py_RETURN_NONE;
4851}
4852
4853PyDoc_STRVAR(cpu_set_count_doc,
4854"cpu_set.count() -> int\n\n\
4855Return the number of CPUs active in the set.");
4856
4857static PyObject *
4858cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4859{
4860 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
4861}
4862
4863PyDoc_STRVAR(cpu_set_clear_doc,
4864"cpu_set.clear(i)\n\n\
4865Remove CPU *i* from the set.");
4866
4867static PyObject *
4868cpu_set_clear(Py_cpu_set *set, PyObject *args)
4869{
4870 int cpu = _get_cpu(set, "i|clear", args);
4871 if (cpu == -1)
4872 return NULL;
4873 CPU_CLR_S(cpu, set->size, set->set);
4874 Py_RETURN_NONE;
4875}
4876
4877PyDoc_STRVAR(cpu_set_isset_doc,
4878"cpu_set.isset(i) -> bool\n\n\
4879Test if CPU *i* is in the set.");
4880
4881static PyObject *
4882cpu_set_isset(Py_cpu_set *set, PyObject *args)
4883{
4884 int cpu = _get_cpu(set, "i|isset", args);
4885 if (cpu == -1)
4886 return NULL;
4887 if (CPU_ISSET_S(cpu, set->size, set->set))
4888 Py_RETURN_TRUE;
4889 Py_RETURN_FALSE;
4890}
4891
4892PyDoc_STRVAR(cpu_set_zero_doc,
4893"cpu_set.zero()\n\n\
4894Clear the cpu_set.");
4895
4896static PyObject *
4897cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
4898{
4899 CPU_ZERO_S(set->size, set->set);
4900 Py_RETURN_NONE;
4901}
4902
4903static PyObject *
4904cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
4905{
4906 int eq;
4907
4908 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) {
4909 Py_INCREF(Py_NotImplemented);
4910 return Py_NotImplemented;
4911 }
4912 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
4913 if ((op == Py_EQ) ? eq : !eq)
4914 Py_RETURN_TRUE;
4915 else
4916 Py_RETURN_FALSE;
4917}
4918
4919#define CPU_SET_BINOP(name, op) \
4920 static PyObject * \
4921 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
4922 if (res) { \
4923 Py_INCREF(res); \
4924 } \
4925 else { \
4926 res = make_new_cpu_set(&cpu_set_type, left->size); \
4927 if (!res) \
4928 return NULL; \
4929 } \
4930 if (Py_TYPE(right) != &cpu_set_type || left->size != right->size) { \
4931 Py_DECREF(res); \
4932 Py_INCREF(Py_NotImplemented); \
4933 return Py_NotImplemented; \
4934 } \
4935 assert(left->size == right->size == res->size); \
4936 op(res->size, res->set, left->set, right->set); \
4937 return (PyObject *)res; \
4938 } \
4939 static PyObject * \
4940 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
4941 return do_cpu_set_##name(left, right, NULL); \
4942 } \
4943 static PyObject * \
4944 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
4945 return do_cpu_set_##name(left, right, left); \
4946 } \
4947
4948CPU_SET_BINOP(and, CPU_AND_S)
4949CPU_SET_BINOP(or, CPU_OR_S)
4950CPU_SET_BINOP(xor, CPU_XOR_S)
4951#undef CPU_SET_BINOP
4952
4953PyDoc_STRVAR(cpu_set_doc,
4954"cpu_set(size)\n\n\
4955Create an empty mask of CPUs.");
4956
4957static PyNumberMethods cpu_set_as_number = {
4958 0, /*nb_add*/
4959 0, /*nb_subtract*/
4960 0, /*nb_multiply*/
4961 0, /*nb_remainder*/
4962 0, /*nb_divmod*/
4963 0, /*nb_power*/
4964 0, /*nb_negative*/
4965 0, /*nb_positive*/
4966 0, /*nb_absolute*/
4967 0, /*nb_bool*/
4968 0, /*nb_invert*/
4969 0, /*nb_lshift*/
4970 0, /*nb_rshift*/
4971 (binaryfunc)cpu_set_and, /*nb_and*/
4972 (binaryfunc)cpu_set_xor, /*nb_xor*/
4973 (binaryfunc)cpu_set_or, /*nb_or*/
4974 0, /*nb_int*/
4975 0, /*nb_reserved*/
4976 0, /*nb_float*/
4977 0, /*nb_inplace_add*/
4978 0, /*nb_inplace_subtract*/
4979 0, /*nb_inplace_multiply*/
4980 0, /*nb_inplace_remainder*/
4981 0, /*nb_inplace_power*/
4982 0, /*nb_inplace_lshift*/
4983 0, /*nb_inplace_rshift*/
4984 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
4985 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
4986 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
4987};
4988
4989static PySequenceMethods cpu_set_as_sequence = {
4990 (lenfunc)cpu_set_len, /* sq_length */
4991};
4992
4993static PyMethodDef cpu_set_methods[] = {
4994 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
4995 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
4996 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
4997 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
4998 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
4999 {NULL, NULL} /* sentinel */
5000};
5001
5002static PyTypeObject cpu_set_type = {
5003 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5004 "posix.cpu_set", /* tp_name */
5005 sizeof(Py_cpu_set), /* tp_basicsize */
5006 0, /* tp_itemsize */
5007 /* methods */
5008 (destructor)cpu_set_dealloc, /* tp_dealloc */
5009 0, /* tp_print */
5010 0, /* tp_getattr */
5011 0, /* tp_setattr */
5012 0, /* tp_reserved */
5013 (reprfunc)cpu_set_repr, /* tp_repr */
5014 &cpu_set_as_number, /* tp_as_number */
5015 &cpu_set_as_sequence, /* tp_as_sequence */
5016 0, /* tp_as_mapping */
5017 PyObject_HashNotImplemented, /* tp_hash */
5018 0, /* tp_call */
5019 0, /* tp_str */
5020 PyObject_GenericGetAttr, /* tp_getattro */
5021 0, /* tp_setattro */
5022 0, /* tp_as_buffer */
5023 Py_TPFLAGS_DEFAULT, /* tp_flags */
5024 cpu_set_doc, /* tp_doc */
5025 0, /* tp_traverse */
5026 0, /* tp_clear */
5027 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5028 0, /* tp_weaklistoffset */
5029 0, /* tp_iter */
5030 0, /* tp_iternext */
5031 cpu_set_methods, /* tp_methods */
5032 0, /* tp_members */
5033 0, /* tp_getset */
5034 0, /* tp_base */
5035 0, /* tp_dict */
5036 0, /* tp_descr_get */
5037 0, /* tp_descr_set */
5038 0, /* tp_dictoffset */
5039 0, /* tp_init */
5040 PyType_GenericAlloc, /* tp_alloc */
5041 cpu_set_new, /* tp_new */
5042 PyObject_Del, /* tp_free */
5043};
5044
5045PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5046"sched_setaffinity(pid, cpu_set)\n\n\
5047Set the affinity of the process with PID *pid* to *cpu_set*.");
5048
5049static PyObject *
5050posix_sched_setaffinity(PyObject *self, PyObject *args)
5051{
5052 pid_t pid;
5053 Py_cpu_set *cpu_set;
5054
5055 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!|sched_setaffinity",
5056 &pid, &cpu_set_type, &cpu_set))
5057 return NULL;
5058 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5059 return posix_error();
5060 Py_RETURN_NONE;
5061}
5062
5063PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5064"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5065Return the affinity of the process with PID *pid*.\n\
5066The returned cpu_set will be of size *ncpus*.");
5067
5068static PyObject *
5069posix_sched_getaffinity(PyObject *self, PyObject *args)
5070{
5071 pid_t pid;
5072 int ncpus;
5073 Py_cpu_set *res;
5074
5075 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|sched_getaffinity",
5076 &pid, &ncpus))
5077 return NULL;
5078 res = make_new_cpu_set(&cpu_set_type, ncpus);
5079 if (!res)
5080 return NULL;
5081 if (sched_getaffinity(pid, res->size, res->set)) {
5082 Py_DECREF(res);
5083 return posix_error();
5084 }
5085 return (PyObject *)res;
5086}
5087
Benjamin Peterson2740af82011-08-02 17:41:34 -05005088#endif /* HAVE_SCHED_SETAFFINITY */
5089
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005090#endif /* HAVE_SCHED_H */
5091
Neal Norwitzb59798b2003-03-21 01:43:31 +00005092/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005093/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5094#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005095#define DEV_PTY_FILE "/dev/ptc"
5096#define HAVE_DEV_PTMX
5097#else
5098#define DEV_PTY_FILE "/dev/ptmx"
5099#endif
5100
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005101#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005102#ifdef HAVE_PTY_H
5103#include <pty.h>
5104#else
5105#ifdef HAVE_LIBUTIL_H
5106#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005107#else
5108#ifdef HAVE_UTIL_H
5109#include <util.h>
5110#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005111#endif /* HAVE_LIBUTIL_H */
5112#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005113#ifdef HAVE_STROPTS_H
5114#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005115#endif
5116#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005117
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005118#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005119PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005120"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005121Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005122
5123static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005124posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005125{
Victor Stinner8c62be82010-05-06 00:08:46 +00005126 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005127#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005128 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005129#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005130#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005131 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005132#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005133 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005134#endif
5135#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005136
Thomas Wouters70c21a12000-07-14 14:28:33 +00005137#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005138 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5139 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005140#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005141 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5142 if (slave_name == NULL)
5143 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005144
Victor Stinner8c62be82010-05-06 00:08:46 +00005145 slave_fd = open(slave_name, O_RDWR);
5146 if (slave_fd < 0)
5147 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005148#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005149 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5150 if (master_fd < 0)
5151 return posix_error();
5152 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5153 /* change permission of slave */
5154 if (grantpt(master_fd) < 0) {
5155 PyOS_setsig(SIGCHLD, sig_saved);
5156 return posix_error();
5157 }
5158 /* unlock slave */
5159 if (unlockpt(master_fd) < 0) {
5160 PyOS_setsig(SIGCHLD, sig_saved);
5161 return posix_error();
5162 }
5163 PyOS_setsig(SIGCHLD, sig_saved);
5164 slave_name = ptsname(master_fd); /* get name of slave */
5165 if (slave_name == NULL)
5166 return posix_error();
5167 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5168 if (slave_fd < 0)
5169 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005170#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005171 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5172 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005173#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005174 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005175#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005176#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005177#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005178
Victor Stinner8c62be82010-05-06 00:08:46 +00005179 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005180
Fred Drake8cef4cf2000-06-28 16:40:38 +00005181}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005182#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005183
5184#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005185PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005186"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005187Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5188Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005189To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005190
5191static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005192posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005193{
Victor Stinner8c62be82010-05-06 00:08:46 +00005194 int master_fd = -1, result = 0;
5195 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005196
Victor Stinner8c62be82010-05-06 00:08:46 +00005197 _PyImport_AcquireLock();
5198 pid = forkpty(&master_fd, NULL, NULL, NULL);
5199 if (pid == 0) {
5200 /* child: this clobbers and resets the import lock. */
5201 PyOS_AfterFork();
5202 } else {
5203 /* parent: release the import lock. */
5204 result = _PyImport_ReleaseLock();
5205 }
5206 if (pid == -1)
5207 return posix_error();
5208 if (result < 0) {
5209 /* Don't clobber the OSError if the fork failed. */
5210 PyErr_SetString(PyExc_RuntimeError,
5211 "not holding the import lock");
5212 return NULL;
5213 }
5214 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005215}
5216#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005217
Ross Lagerwall7807c352011-03-17 20:20:30 +02005218
Guido van Rossumad0ee831995-03-01 10:34:45 +00005219#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005220PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005221"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005222Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005223
Barry Warsaw53699e91996-12-10 23:23:01 +00005224static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005225posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005226{
Victor Stinner8c62be82010-05-06 00:08:46 +00005227 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005228}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005229#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005230
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005231
Guido van Rossumad0ee831995-03-01 10:34:45 +00005232#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005233PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005234"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005235Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005236
Barry Warsaw53699e91996-12-10 23:23:01 +00005237static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005238posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005239{
Victor Stinner8c62be82010-05-06 00:08:46 +00005240 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005241}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005242#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005243
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005244
Guido van Rossumad0ee831995-03-01 10:34:45 +00005245#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005246PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005247"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005248Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005249
Barry Warsaw53699e91996-12-10 23:23:01 +00005250static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005251posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005252{
Victor Stinner8c62be82010-05-06 00:08:46 +00005253 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005254}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005255#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005258PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005259"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005260Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005261
Barry Warsaw53699e91996-12-10 23:23:01 +00005262static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005263posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005264{
Victor Stinner8c62be82010-05-06 00:08:46 +00005265 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005266}
5267
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005268#ifdef HAVE_GETGROUPLIST
5269PyDoc_STRVAR(posix_getgrouplist__doc__,
5270"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5271Returns a list of groups to which a user belongs.\n\n\
5272 user: username to lookup\n\
5273 group: base group id of the user");
5274
5275static PyObject *
5276posix_getgrouplist(PyObject *self, PyObject *args)
5277{
5278#ifdef NGROUPS_MAX
5279#define MAX_GROUPS NGROUPS_MAX
5280#else
5281 /* defined to be 16 on Solaris7, so this should be a small number */
5282#define MAX_GROUPS 64
5283#endif
5284
5285 const char *user;
5286 int i, ngroups;
5287 PyObject *list;
5288#ifdef __APPLE__
5289 int *groups, basegid;
5290#else
5291 gid_t *groups, basegid;
5292#endif
5293 ngroups = MAX_GROUPS;
5294
5295 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5296 return NULL;
5297
5298#ifdef __APPLE__
5299 groups = PyMem_Malloc(ngroups * sizeof(int));
5300#else
5301 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5302#endif
5303 if (groups == NULL)
5304 return PyErr_NoMemory();
5305
5306 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5307 PyMem_Del(groups);
5308 return posix_error();
5309 }
5310
5311 list = PyList_New(ngroups);
5312 if (list == NULL) {
5313 PyMem_Del(groups);
5314 return NULL;
5315 }
5316
5317 for (i = 0; i < ngroups; i++) {
5318 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5319 if (o == NULL) {
5320 Py_DECREF(list);
5321 PyMem_Del(groups);
5322 return NULL;
5323 }
5324 PyList_SET_ITEM(list, i, o);
5325 }
5326
5327 PyMem_Del(groups);
5328
5329 return list;
5330}
5331#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005332
Fred Drakec9680921999-12-13 16:37:25 +00005333#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005334PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005335"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005336Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005337
5338static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005339posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005340{
5341 PyObject *result = NULL;
5342
Fred Drakec9680921999-12-13 16:37:25 +00005343#ifdef NGROUPS_MAX
5344#define MAX_GROUPS NGROUPS_MAX
5345#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005346 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005347#define MAX_GROUPS 64
5348#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005349 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005350
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005351 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005352 * This is a helper variable to store the intermediate result when
5353 * that happens.
5354 *
5355 * To keep the code readable the OSX behaviour is unconditional,
5356 * according to the POSIX spec this should be safe on all unix-y
5357 * systems.
5358 */
5359 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005360 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005361
Victor Stinner8c62be82010-05-06 00:08:46 +00005362 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005363 if (n < 0) {
5364 if (errno == EINVAL) {
5365 n = getgroups(0, NULL);
5366 if (n == -1) {
5367 return posix_error();
5368 }
5369 if (n == 0) {
5370 /* Avoid malloc(0) */
5371 alt_grouplist = grouplist;
5372 } else {
5373 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5374 if (alt_grouplist == NULL) {
5375 errno = EINVAL;
5376 return posix_error();
5377 }
5378 n = getgroups(n, alt_grouplist);
5379 if (n == -1) {
5380 PyMem_Free(alt_grouplist);
5381 return posix_error();
5382 }
5383 }
5384 } else {
5385 return posix_error();
5386 }
5387 }
5388 result = PyList_New(n);
5389 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005390 int i;
5391 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005392 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005393 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005394 Py_DECREF(result);
5395 result = NULL;
5396 break;
Fred Drakec9680921999-12-13 16:37:25 +00005397 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005398 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005399 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005400 }
5401
5402 if (alt_grouplist != grouplist) {
5403 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005404 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005405
Fred Drakec9680921999-12-13 16:37:25 +00005406 return result;
5407}
5408#endif
5409
Antoine Pitroub7572f02009-12-02 20:46:48 +00005410#ifdef HAVE_INITGROUPS
5411PyDoc_STRVAR(posix_initgroups__doc__,
5412"initgroups(username, gid) -> None\n\n\
5413Call the system initgroups() to initialize the group access list with all of\n\
5414the groups of which the specified username is a member, plus the specified\n\
5415group id.");
5416
5417static PyObject *
5418posix_initgroups(PyObject *self, PyObject *args)
5419{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005420 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005421 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005422 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005423 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005424
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005425 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5426 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005427 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005428 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005429
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005430 res = initgroups(username, (gid_t) gid);
5431 Py_DECREF(oname);
5432 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005433 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005434
Victor Stinner8c62be82010-05-06 00:08:46 +00005435 Py_INCREF(Py_None);
5436 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005437}
5438#endif
5439
Martin v. Löwis606edc12002-06-13 21:09:11 +00005440#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005441PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005442"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005443Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005444
5445static PyObject *
5446posix_getpgid(PyObject *self, PyObject *args)
5447{
Victor Stinner8c62be82010-05-06 00:08:46 +00005448 pid_t pid, pgid;
5449 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5450 return NULL;
5451 pgid = getpgid(pid);
5452 if (pgid < 0)
5453 return posix_error();
5454 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005455}
5456#endif /* HAVE_GETPGID */
5457
5458
Guido van Rossumb6775db1994-08-01 11:34:53 +00005459#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005460PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005461"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005462Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005463
Barry Warsaw53699e91996-12-10 23:23:01 +00005464static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005465posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005466{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005467#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005468 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005469#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005470 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005471#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005472}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005473#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005474
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005475
Guido van Rossumb6775db1994-08-01 11:34:53 +00005476#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005477PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005478"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005479Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005480
Barry Warsaw53699e91996-12-10 23:23:01 +00005481static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005482posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005483{
Guido van Rossum64933891994-10-20 21:56:42 +00005484#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005485 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005486#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005488#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005489 return posix_error();
5490 Py_INCREF(Py_None);
5491 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005492}
5493
Guido van Rossumb6775db1994-08-01 11:34:53 +00005494#endif /* HAVE_SETPGRP */
5495
Guido van Rossumad0ee831995-03-01 10:34:45 +00005496#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005497
5498#ifdef MS_WINDOWS
5499#include <tlhelp32.h>
5500
5501static PyObject*
5502win32_getppid()
5503{
5504 HANDLE snapshot;
5505 pid_t mypid;
5506 PyObject* result = NULL;
5507 BOOL have_record;
5508 PROCESSENTRY32 pe;
5509
5510 mypid = getpid(); /* This function never fails */
5511
5512 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5513 if (snapshot == INVALID_HANDLE_VALUE)
5514 return PyErr_SetFromWindowsErr(GetLastError());
5515
5516 pe.dwSize = sizeof(pe);
5517 have_record = Process32First(snapshot, &pe);
5518 while (have_record) {
5519 if (mypid == (pid_t)pe.th32ProcessID) {
5520 /* We could cache the ulong value in a static variable. */
5521 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5522 break;
5523 }
5524
5525 have_record = Process32Next(snapshot, &pe);
5526 }
5527
5528 /* If our loop exits and our pid was not found (result will be NULL)
5529 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5530 * error anyway, so let's raise it. */
5531 if (!result)
5532 result = PyErr_SetFromWindowsErr(GetLastError());
5533
5534 CloseHandle(snapshot);
5535
5536 return result;
5537}
5538#endif /*MS_WINDOWS*/
5539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005540PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005541"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005542Return the parent's process id. If the parent process has already exited,\n\
5543Windows machines will still return its id; others systems will return the id\n\
5544of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005545
Barry Warsaw53699e91996-12-10 23:23:01 +00005546static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005547posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005548{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005549#ifdef MS_WINDOWS
5550 return win32_getppid();
5551#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005552 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005553#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005554}
5555#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005557
Fred Drake12c6e2d1999-12-14 21:25:03 +00005558#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005560"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005561Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005562
5563static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005564posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005565{
Victor Stinner8c62be82010-05-06 00:08:46 +00005566 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005567#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005568 wchar_t user_name[UNLEN + 1];
5569 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5570
5571 if (GetUserNameW(user_name, &num_chars)) {
5572 /* num_chars is the number of unicode chars plus null terminator */
5573 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005574 }
5575 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005576 result = PyErr_SetFromWindowsErr(GetLastError());
5577#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005578 char *name;
5579 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005580
Victor Stinner8c62be82010-05-06 00:08:46 +00005581 errno = 0;
5582 name = getlogin();
5583 if (name == NULL) {
5584 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005585 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005586 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005587 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005588 }
5589 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005590 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005591 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005592#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005593 return result;
5594}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005595#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005596
Guido van Rossumad0ee831995-03-01 10:34:45 +00005597#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005599"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005600Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005601
Barry Warsaw53699e91996-12-10 23:23:01 +00005602static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005603posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005604{
Victor Stinner8c62be82010-05-06 00:08:46 +00005605 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005606}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005607#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005608
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005609
Guido van Rossumad0ee831995-03-01 10:34:45 +00005610#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005611PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005612"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005613Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005614
Barry Warsaw53699e91996-12-10 23:23:01 +00005615static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005616posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005617{
Victor Stinner8c62be82010-05-06 00:08:46 +00005618 pid_t pid;
5619 int sig;
5620 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5621 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005622#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005623 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5624 APIRET rc;
5625 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005626 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005627
5628 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5629 APIRET rc;
5630 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005631 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005632
5633 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005634 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005635#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005636 if (kill(pid, sig) == -1)
5637 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005638#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005639 Py_INCREF(Py_None);
5640 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005641}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005642#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005643
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005644#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005645PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005646"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005647Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005648
5649static PyObject *
5650posix_killpg(PyObject *self, PyObject *args)
5651{
Victor Stinner8c62be82010-05-06 00:08:46 +00005652 int sig;
5653 pid_t pgid;
5654 /* XXX some man pages make the `pgid` parameter an int, others
5655 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5656 take the same type. Moreover, pid_t is always at least as wide as
5657 int (else compilation of this module fails), which is safe. */
5658 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5659 return NULL;
5660 if (killpg(pgid, sig) == -1)
5661 return posix_error();
5662 Py_INCREF(Py_None);
5663 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005664}
5665#endif
5666
Brian Curtineb24d742010-04-12 17:16:38 +00005667#ifdef MS_WINDOWS
5668PyDoc_STRVAR(win32_kill__doc__,
5669"kill(pid, sig)\n\n\
5670Kill a process with a signal.");
5671
5672static PyObject *
5673win32_kill(PyObject *self, PyObject *args)
5674{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005675 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005676 DWORD pid, sig, err;
5677 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005678
Victor Stinner8c62be82010-05-06 00:08:46 +00005679 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5680 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005681
Victor Stinner8c62be82010-05-06 00:08:46 +00005682 /* Console processes which share a common console can be sent CTRL+C or
5683 CTRL+BREAK events, provided they handle said events. */
5684 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5685 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5686 err = GetLastError();
5687 PyErr_SetFromWindowsErr(err);
5688 }
5689 else
5690 Py_RETURN_NONE;
5691 }
Brian Curtineb24d742010-04-12 17:16:38 +00005692
Victor Stinner8c62be82010-05-06 00:08:46 +00005693 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5694 attempt to open and terminate the process. */
5695 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5696 if (handle == NULL) {
5697 err = GetLastError();
5698 return PyErr_SetFromWindowsErr(err);
5699 }
Brian Curtineb24d742010-04-12 17:16:38 +00005700
Victor Stinner8c62be82010-05-06 00:08:46 +00005701 if (TerminateProcess(handle, sig) == 0) {
5702 err = GetLastError();
5703 result = PyErr_SetFromWindowsErr(err);
5704 } else {
5705 Py_INCREF(Py_None);
5706 result = Py_None;
5707 }
Brian Curtineb24d742010-04-12 17:16:38 +00005708
Victor Stinner8c62be82010-05-06 00:08:46 +00005709 CloseHandle(handle);
5710 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005711}
5712#endif /* MS_WINDOWS */
5713
Guido van Rossumc0125471996-06-28 18:55:32 +00005714#ifdef HAVE_PLOCK
5715
5716#ifdef HAVE_SYS_LOCK_H
5717#include <sys/lock.h>
5718#endif
5719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005720PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005721"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005722Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005723
Barry Warsaw53699e91996-12-10 23:23:01 +00005724static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005725posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005726{
Victor Stinner8c62be82010-05-06 00:08:46 +00005727 int op;
5728 if (!PyArg_ParseTuple(args, "i:plock", &op))
5729 return NULL;
5730 if (plock(op) == -1)
5731 return posix_error();
5732 Py_INCREF(Py_None);
5733 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005734}
5735#endif
5736
Guido van Rossumb6775db1994-08-01 11:34:53 +00005737#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005738PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005739"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005740Set the current process's user id.");
5741
Barry Warsaw53699e91996-12-10 23:23:01 +00005742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005743posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005744{
Victor Stinner8c62be82010-05-06 00:08:46 +00005745 long uid_arg;
5746 uid_t uid;
5747 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5748 return NULL;
5749 uid = uid_arg;
5750 if (uid != uid_arg) {
5751 PyErr_SetString(PyExc_OverflowError, "user id too big");
5752 return NULL;
5753 }
5754 if (setuid(uid) < 0)
5755 return posix_error();
5756 Py_INCREF(Py_None);
5757 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005758}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005759#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005760
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005761
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005762#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005763PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005764"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005765Set the current process's effective user id.");
5766
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005767static PyObject *
5768posix_seteuid (PyObject *self, PyObject *args)
5769{
Victor Stinner8c62be82010-05-06 00:08:46 +00005770 long euid_arg;
5771 uid_t euid;
5772 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5773 return NULL;
5774 euid = euid_arg;
5775 if (euid != euid_arg) {
5776 PyErr_SetString(PyExc_OverflowError, "user id too big");
5777 return NULL;
5778 }
5779 if (seteuid(euid) < 0) {
5780 return posix_error();
5781 } else {
5782 Py_INCREF(Py_None);
5783 return Py_None;
5784 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005785}
5786#endif /* HAVE_SETEUID */
5787
5788#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005789PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005790"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005791Set the current process's effective group id.");
5792
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005793static PyObject *
5794posix_setegid (PyObject *self, PyObject *args)
5795{
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 long egid_arg;
5797 gid_t egid;
5798 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5799 return NULL;
5800 egid = egid_arg;
5801 if (egid != egid_arg) {
5802 PyErr_SetString(PyExc_OverflowError, "group id too big");
5803 return NULL;
5804 }
5805 if (setegid(egid) < 0) {
5806 return posix_error();
5807 } else {
5808 Py_INCREF(Py_None);
5809 return Py_None;
5810 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005811}
5812#endif /* HAVE_SETEGID */
5813
5814#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005816"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005817Set the current process's real and effective user ids.");
5818
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005819static PyObject *
5820posix_setreuid (PyObject *self, PyObject *args)
5821{
Victor Stinner8c62be82010-05-06 00:08:46 +00005822 long ruid_arg, euid_arg;
5823 uid_t ruid, euid;
5824 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5825 return NULL;
5826 if (ruid_arg == -1)
5827 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5828 else
5829 ruid = ruid_arg; /* otherwise, assign from our long */
5830 if (euid_arg == -1)
5831 euid = (uid_t)-1;
5832 else
5833 euid = euid_arg;
5834 if ((euid_arg != -1 && euid != euid_arg) ||
5835 (ruid_arg != -1 && ruid != ruid_arg)) {
5836 PyErr_SetString(PyExc_OverflowError, "user id too big");
5837 return NULL;
5838 }
5839 if (setreuid(ruid, euid) < 0) {
5840 return posix_error();
5841 } else {
5842 Py_INCREF(Py_None);
5843 return Py_None;
5844 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005845}
5846#endif /* HAVE_SETREUID */
5847
5848#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005849PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005850"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005851Set the current process's real and effective group ids.");
5852
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005853static PyObject *
5854posix_setregid (PyObject *self, PyObject *args)
5855{
Victor Stinner8c62be82010-05-06 00:08:46 +00005856 long rgid_arg, egid_arg;
5857 gid_t rgid, egid;
5858 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5859 return NULL;
5860 if (rgid_arg == -1)
5861 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5862 else
5863 rgid = rgid_arg; /* otherwise, assign from our long */
5864 if (egid_arg == -1)
5865 egid = (gid_t)-1;
5866 else
5867 egid = egid_arg;
5868 if ((egid_arg != -1 && egid != egid_arg) ||
5869 (rgid_arg != -1 && rgid != rgid_arg)) {
5870 PyErr_SetString(PyExc_OverflowError, "group id too big");
5871 return NULL;
5872 }
5873 if (setregid(rgid, egid) < 0) {
5874 return posix_error();
5875 } else {
5876 Py_INCREF(Py_None);
5877 return Py_None;
5878 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005879}
5880#endif /* HAVE_SETREGID */
5881
Guido van Rossumb6775db1994-08-01 11:34:53 +00005882#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005883PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005884"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005885Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005886
Barry Warsaw53699e91996-12-10 23:23:01 +00005887static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005888posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005889{
Victor Stinner8c62be82010-05-06 00:08:46 +00005890 long gid_arg;
5891 gid_t gid;
5892 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5893 return NULL;
5894 gid = gid_arg;
5895 if (gid != gid_arg) {
5896 PyErr_SetString(PyExc_OverflowError, "group id too big");
5897 return NULL;
5898 }
5899 if (setgid(gid) < 0)
5900 return posix_error();
5901 Py_INCREF(Py_None);
5902 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005903}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005904#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005905
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005906#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005907PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005908"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005909Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005910
5911static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005912posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005913{
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 int i, len;
5915 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005916
Victor Stinner8c62be82010-05-06 00:08:46 +00005917 if (!PySequence_Check(groups)) {
5918 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5919 return NULL;
5920 }
5921 len = PySequence_Size(groups);
5922 if (len > MAX_GROUPS) {
5923 PyErr_SetString(PyExc_ValueError, "too many groups");
5924 return NULL;
5925 }
5926 for(i = 0; i < len; i++) {
5927 PyObject *elem;
5928 elem = PySequence_GetItem(groups, i);
5929 if (!elem)
5930 return NULL;
5931 if (!PyLong_Check(elem)) {
5932 PyErr_SetString(PyExc_TypeError,
5933 "groups must be integers");
5934 Py_DECREF(elem);
5935 return NULL;
5936 } else {
5937 unsigned long x = PyLong_AsUnsignedLong(elem);
5938 if (PyErr_Occurred()) {
5939 PyErr_SetString(PyExc_TypeError,
5940 "group id too big");
5941 Py_DECREF(elem);
5942 return NULL;
5943 }
5944 grouplist[i] = x;
5945 /* read back the value to see if it fitted in gid_t */
5946 if (grouplist[i] != x) {
5947 PyErr_SetString(PyExc_TypeError,
5948 "group id too big");
5949 Py_DECREF(elem);
5950 return NULL;
5951 }
5952 }
5953 Py_DECREF(elem);
5954 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005955
Victor Stinner8c62be82010-05-06 00:08:46 +00005956 if (setgroups(len, grouplist) < 0)
5957 return posix_error();
5958 Py_INCREF(Py_None);
5959 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005960}
5961#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005962
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005963#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5964static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005965wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005966{
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 PyObject *result;
5968 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005969
Victor Stinner8c62be82010-05-06 00:08:46 +00005970 if (pid == -1)
5971 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005972
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 if (struct_rusage == NULL) {
5974 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5975 if (m == NULL)
5976 return NULL;
5977 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5978 Py_DECREF(m);
5979 if (struct_rusage == NULL)
5980 return NULL;
5981 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005982
Victor Stinner8c62be82010-05-06 00:08:46 +00005983 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5984 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5985 if (!result)
5986 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005987
5988#ifndef doubletime
5989#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5990#endif
5991
Victor Stinner8c62be82010-05-06 00:08:46 +00005992 PyStructSequence_SET_ITEM(result, 0,
5993 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5994 PyStructSequence_SET_ITEM(result, 1,
5995 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005996#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005997 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5998 SET_INT(result, 2, ru->ru_maxrss);
5999 SET_INT(result, 3, ru->ru_ixrss);
6000 SET_INT(result, 4, ru->ru_idrss);
6001 SET_INT(result, 5, ru->ru_isrss);
6002 SET_INT(result, 6, ru->ru_minflt);
6003 SET_INT(result, 7, ru->ru_majflt);
6004 SET_INT(result, 8, ru->ru_nswap);
6005 SET_INT(result, 9, ru->ru_inblock);
6006 SET_INT(result, 10, ru->ru_oublock);
6007 SET_INT(result, 11, ru->ru_msgsnd);
6008 SET_INT(result, 12, ru->ru_msgrcv);
6009 SET_INT(result, 13, ru->ru_nsignals);
6010 SET_INT(result, 14, ru->ru_nvcsw);
6011 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006012#undef SET_INT
6013
Victor Stinner8c62be82010-05-06 00:08:46 +00006014 if (PyErr_Occurred()) {
6015 Py_DECREF(result);
6016 return NULL;
6017 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006018
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006020}
6021#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6022
6023#ifdef HAVE_WAIT3
6024PyDoc_STRVAR(posix_wait3__doc__,
6025"wait3(options) -> (pid, status, rusage)\n\n\
6026Wait for completion of a child process.");
6027
6028static PyObject *
6029posix_wait3(PyObject *self, PyObject *args)
6030{
Victor Stinner8c62be82010-05-06 00:08:46 +00006031 pid_t pid;
6032 int options;
6033 struct rusage ru;
6034 WAIT_TYPE status;
6035 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006036
Victor Stinner8c62be82010-05-06 00:08:46 +00006037 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6038 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006039
Victor Stinner8c62be82010-05-06 00:08:46 +00006040 Py_BEGIN_ALLOW_THREADS
6041 pid = wait3(&status, options, &ru);
6042 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006043
Victor Stinner8c62be82010-05-06 00:08:46 +00006044 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006045}
6046#endif /* HAVE_WAIT3 */
6047
6048#ifdef HAVE_WAIT4
6049PyDoc_STRVAR(posix_wait4__doc__,
6050"wait4(pid, options) -> (pid, status, rusage)\n\n\
6051Wait for completion of a given child process.");
6052
6053static PyObject *
6054posix_wait4(PyObject *self, PyObject *args)
6055{
Victor Stinner8c62be82010-05-06 00:08:46 +00006056 pid_t pid;
6057 int options;
6058 struct rusage ru;
6059 WAIT_TYPE status;
6060 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006061
Victor Stinner8c62be82010-05-06 00:08:46 +00006062 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6063 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006064
Victor Stinner8c62be82010-05-06 00:08:46 +00006065 Py_BEGIN_ALLOW_THREADS
6066 pid = wait4(pid, &status, options, &ru);
6067 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006068
Victor Stinner8c62be82010-05-06 00:08:46 +00006069 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006070}
6071#endif /* HAVE_WAIT4 */
6072
Ross Lagerwall7807c352011-03-17 20:20:30 +02006073#if defined(HAVE_WAITID) && !defined(__APPLE__)
6074PyDoc_STRVAR(posix_waitid__doc__,
6075"waitid(idtype, id, options) -> waitid_result\n\n\
6076Wait for the completion of one or more child processes.\n\n\
6077idtype can be P_PID, P_PGID or P_ALL.\n\
6078id specifies the pid to wait on.\n\
6079options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6080or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6081Returns either waitid_result or None if WNOHANG is specified and there are\n\
6082no children in a waitable state.");
6083
6084static PyObject *
6085posix_waitid(PyObject *self, PyObject *args)
6086{
6087 PyObject *result;
6088 idtype_t idtype;
6089 id_t id;
6090 int options, res;
6091 siginfo_t si;
6092 si.si_pid = 0;
6093 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6094 return NULL;
6095 Py_BEGIN_ALLOW_THREADS
6096 res = waitid(idtype, id, &si, options);
6097 Py_END_ALLOW_THREADS
6098 if (res == -1)
6099 return posix_error();
6100
6101 if (si.si_pid == 0)
6102 Py_RETURN_NONE;
6103
6104 result = PyStructSequence_New(&WaitidResultType);
6105 if (!result)
6106 return NULL;
6107
6108 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6109 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6110 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6111 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6112 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6113 if (PyErr_Occurred()) {
6114 Py_DECREF(result);
6115 return NULL;
6116 }
6117
6118 return result;
6119}
6120#endif
6121
Guido van Rossumb6775db1994-08-01 11:34:53 +00006122#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006123PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006124"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006125Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006126
Barry Warsaw53699e91996-12-10 23:23:01 +00006127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006128posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006129{
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 pid_t pid;
6131 int options;
6132 WAIT_TYPE status;
6133 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006134
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6136 return NULL;
6137 Py_BEGIN_ALLOW_THREADS
6138 pid = waitpid(pid, &status, options);
6139 Py_END_ALLOW_THREADS
6140 if (pid == -1)
6141 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006142
Victor Stinner8c62be82010-05-06 00:08:46 +00006143 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006144}
6145
Tim Petersab034fa2002-02-01 11:27:43 +00006146#elif defined(HAVE_CWAIT)
6147
6148/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006149PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006150"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006151"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006152
6153static PyObject *
6154posix_waitpid(PyObject *self, PyObject *args)
6155{
Victor Stinner8c62be82010-05-06 00:08:46 +00006156 Py_intptr_t pid;
6157 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006158
Victor Stinner8c62be82010-05-06 00:08:46 +00006159 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6160 return NULL;
6161 Py_BEGIN_ALLOW_THREADS
6162 pid = _cwait(&status, pid, options);
6163 Py_END_ALLOW_THREADS
6164 if (pid == -1)
6165 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006166
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 /* shift the status left a byte so this is more like the POSIX waitpid */
6168 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006169}
6170#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006171
Guido van Rossumad0ee831995-03-01 10:34:45 +00006172#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006173PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006174"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006175Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006176
Barry Warsaw53699e91996-12-10 23:23:01 +00006177static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006178posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006179{
Victor Stinner8c62be82010-05-06 00:08:46 +00006180 pid_t pid;
6181 WAIT_TYPE status;
6182 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006183
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 Py_BEGIN_ALLOW_THREADS
6185 pid = wait(&status);
6186 Py_END_ALLOW_THREADS
6187 if (pid == -1)
6188 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006189
Victor Stinner8c62be82010-05-06 00:08:46 +00006190 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006191}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006192#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006193
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006195PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006196"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006197Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006198
Barry Warsaw53699e91996-12-10 23:23:01 +00006199static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006200posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006201{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006202#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006204#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006205#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006206 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006207 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006208#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006210#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006211#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006212}
6213
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006214
Guido van Rossumb6775db1994-08-01 11:34:53 +00006215#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006216PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006217"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006218Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006219
Barry Warsaw53699e91996-12-10 23:23:01 +00006220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006221posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006222{
Victor Stinner8c62be82010-05-06 00:08:46 +00006223 PyObject* v;
6224 char buf[MAXPATHLEN];
6225 PyObject *opath;
6226 char *path;
6227 int n;
6228 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006229
Victor Stinner8c62be82010-05-06 00:08:46 +00006230 if (!PyArg_ParseTuple(args, "O&:readlink",
6231 PyUnicode_FSConverter, &opath))
6232 return NULL;
6233 path = PyBytes_AsString(opath);
6234 v = PySequence_GetItem(args, 0);
6235 if (v == NULL) {
6236 Py_DECREF(opath);
6237 return NULL;
6238 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006239
Victor Stinner8c62be82010-05-06 00:08:46 +00006240 if (PyUnicode_Check(v)) {
6241 arg_is_unicode = 1;
6242 }
6243 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006244
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 Py_BEGIN_ALLOW_THREADS
6246 n = readlink(path, buf, (int) sizeof buf);
6247 Py_END_ALLOW_THREADS
6248 if (n < 0)
6249 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006250
Victor Stinner8c62be82010-05-06 00:08:46 +00006251 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006252 if (arg_is_unicode)
6253 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6254 else
6255 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006256}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006257#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006258
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006259
Brian Curtin52173d42010-12-02 18:29:18 +00006260#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006261PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006262"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006263Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006264
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006265static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006266posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006267{
Victor Stinner8c62be82010-05-06 00:08:46 +00006268 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006269}
6270#endif /* HAVE_SYMLINK */
6271
Brian Curtind40e6f72010-07-08 21:39:08 +00006272#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6273
6274PyDoc_STRVAR(win_readlink__doc__,
6275"readlink(path) -> path\n\n\
6276Return a string representing the path to which the symbolic link points.");
6277
Brian Curtind40e6f72010-07-08 21:39:08 +00006278/* Windows readlink implementation */
6279static PyObject *
6280win_readlink(PyObject *self, PyObject *args)
6281{
6282 wchar_t *path;
6283 DWORD n_bytes_returned;
6284 DWORD io_result;
6285 PyObject *result;
6286 HANDLE reparse_point_handle;
6287
6288 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6289 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6290 wchar_t *print_name;
6291
6292 if (!PyArg_ParseTuple(args,
6293 "u:readlink",
6294 &path))
6295 return NULL;
6296
6297 /* First get a handle to the reparse point */
6298 Py_BEGIN_ALLOW_THREADS
6299 reparse_point_handle = CreateFileW(
6300 path,
6301 0,
6302 0,
6303 0,
6304 OPEN_EXISTING,
6305 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6306 0);
6307 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006308
Brian Curtind40e6f72010-07-08 21:39:08 +00006309 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6310 {
6311 return win32_error_unicode("readlink", path);
6312 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006313
Brian Curtind40e6f72010-07-08 21:39:08 +00006314 Py_BEGIN_ALLOW_THREADS
6315 /* New call DeviceIoControl to read the reparse point */
6316 io_result = DeviceIoControl(
6317 reparse_point_handle,
6318 FSCTL_GET_REPARSE_POINT,
6319 0, 0, /* in buffer */
6320 target_buffer, sizeof(target_buffer),
6321 &n_bytes_returned,
6322 0 /* we're not using OVERLAPPED_IO */
6323 );
6324 CloseHandle(reparse_point_handle);
6325 Py_END_ALLOW_THREADS
6326
6327 if (io_result==0)
6328 {
6329 return win32_error_unicode("readlink", path);
6330 }
6331
6332 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6333 {
6334 PyErr_SetString(PyExc_ValueError,
6335 "not a symbolic link");
6336 return NULL;
6337 }
Brian Curtin74e45612010-07-09 15:58:59 +00006338 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6339 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6340
6341 result = PyUnicode_FromWideChar(print_name,
6342 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006343 return result;
6344}
6345
6346#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6347
Brian Curtin52173d42010-12-02 18:29:18 +00006348#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006349
6350/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6351static int has_CreateSymbolicLinkW = 0;
6352static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6353static int
6354check_CreateSymbolicLinkW()
6355{
6356 HINSTANCE hKernel32;
6357 /* only recheck */
6358 if (has_CreateSymbolicLinkW)
6359 return has_CreateSymbolicLinkW;
6360 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006361 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6362 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006363 if (Py_CreateSymbolicLinkW)
6364 has_CreateSymbolicLinkW = 1;
6365 return has_CreateSymbolicLinkW;
6366}
6367
6368PyDoc_STRVAR(win_symlink__doc__,
6369"symlink(src, dst, target_is_directory=False)\n\n\
6370Create a symbolic link pointing to src named dst.\n\
6371target_is_directory is required if the target is to be interpreted as\n\
6372a directory.\n\
6373This function requires Windows 6.0 or greater, and raises a\n\
6374NotImplementedError otherwise.");
6375
6376static PyObject *
6377win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6378{
6379 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6380 PyObject *src, *dest;
6381 int target_is_directory = 0;
6382 DWORD res;
6383 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006384
Brian Curtind40e6f72010-07-08 21:39:08 +00006385 if (!check_CreateSymbolicLinkW())
6386 {
6387 /* raise NotImplementedError */
6388 return PyErr_Format(PyExc_NotImplementedError,
6389 "CreateSymbolicLinkW not found");
6390 }
6391 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6392 kwlist, &src, &dest, &target_is_directory))
6393 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006394
6395 if (win32_can_symlink == 0)
6396 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6397
Brian Curtind40e6f72010-07-08 21:39:08 +00006398 if (!convert_to_unicode(&src)) { return NULL; }
6399 if (!convert_to_unicode(&dest)) {
6400 Py_DECREF(src);
6401 return NULL;
6402 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006403
Brian Curtind40e6f72010-07-08 21:39:08 +00006404 /* if src is a directory, ensure target_is_directory==1 */
6405 if(
6406 GetFileAttributesExW(
6407 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6408 ))
6409 {
6410 target_is_directory = target_is_directory ||
6411 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6412 }
6413
6414 Py_BEGIN_ALLOW_THREADS
6415 res = Py_CreateSymbolicLinkW(
6416 PyUnicode_AsUnicode(dest),
6417 PyUnicode_AsUnicode(src),
6418 target_is_directory);
6419 Py_END_ALLOW_THREADS
6420 Py_DECREF(src);
6421 Py_DECREF(dest);
6422 if (!res)
6423 {
6424 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6425 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006426
Brian Curtind40e6f72010-07-08 21:39:08 +00006427 Py_INCREF(Py_None);
6428 return Py_None;
6429}
Brian Curtin52173d42010-12-02 18:29:18 +00006430#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006431
6432#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006433#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6434static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006435system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006436{
6437 ULONG value = 0;
6438
6439 Py_BEGIN_ALLOW_THREADS
6440 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6441 Py_END_ALLOW_THREADS
6442
6443 return value;
6444}
6445
6446static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006447posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006448{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006449 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006450 return Py_BuildValue("ddddd",
6451 (double)0 /* t.tms_utime / HZ */,
6452 (double)0 /* t.tms_stime / HZ */,
6453 (double)0 /* t.tms_cutime / HZ */,
6454 (double)0 /* t.tms_cstime / HZ */,
6455 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006456}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006457#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006458#define NEED_TICKS_PER_SECOND
6459static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006460static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006461posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006462{
Victor Stinner8c62be82010-05-06 00:08:46 +00006463 struct tms t;
6464 clock_t c;
6465 errno = 0;
6466 c = times(&t);
6467 if (c == (clock_t) -1)
6468 return posix_error();
6469 return Py_BuildValue("ddddd",
6470 (double)t.tms_utime / ticks_per_second,
6471 (double)t.tms_stime / ticks_per_second,
6472 (double)t.tms_cutime / ticks_per_second,
6473 (double)t.tms_cstime / ticks_per_second,
6474 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006475}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006476#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006477#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006478
6479
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006480#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006481#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006482static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006483posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006484{
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 FILETIME create, exit, kernel, user;
6486 HANDLE hProc;
6487 hProc = GetCurrentProcess();
6488 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6489 /* The fields of a FILETIME structure are the hi and lo part
6490 of a 64-bit value expressed in 100 nanosecond units.
6491 1e7 is one second in such units; 1e-7 the inverse.
6492 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6493 */
6494 return Py_BuildValue(
6495 "ddddd",
6496 (double)(user.dwHighDateTime*429.4967296 +
6497 user.dwLowDateTime*1e-7),
6498 (double)(kernel.dwHighDateTime*429.4967296 +
6499 kernel.dwLowDateTime*1e-7),
6500 (double)0,
6501 (double)0,
6502 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006503}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006504#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006505
6506#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006507PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006508"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006509Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006510#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006511
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006512
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006513#ifdef HAVE_GETSID
6514PyDoc_STRVAR(posix_getsid__doc__,
6515"getsid(pid) -> sid\n\n\
6516Call the system call getsid().");
6517
6518static PyObject *
6519posix_getsid(PyObject *self, PyObject *args)
6520{
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 pid_t pid;
6522 int sid;
6523 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6524 return NULL;
6525 sid = getsid(pid);
6526 if (sid < 0)
6527 return posix_error();
6528 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006529}
6530#endif /* HAVE_GETSID */
6531
6532
Guido van Rossumb6775db1994-08-01 11:34:53 +00006533#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006534PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006535"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006536Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006537
Barry Warsaw53699e91996-12-10 23:23:01 +00006538static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006539posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006540{
Victor Stinner8c62be82010-05-06 00:08:46 +00006541 if (setsid() < 0)
6542 return posix_error();
6543 Py_INCREF(Py_None);
6544 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006545}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006546#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006547
Guido van Rossumb6775db1994-08-01 11:34:53 +00006548#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006549PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006550"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006551Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006552
Barry Warsaw53699e91996-12-10 23:23:01 +00006553static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006554posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006555{
Victor Stinner8c62be82010-05-06 00:08:46 +00006556 pid_t pid;
6557 int pgrp;
6558 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6559 return NULL;
6560 if (setpgid(pid, pgrp) < 0)
6561 return posix_error();
6562 Py_INCREF(Py_None);
6563 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006564}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006565#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006566
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006567
Guido van Rossumb6775db1994-08-01 11:34:53 +00006568#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006569PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006570"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006571Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006572
Barry Warsaw53699e91996-12-10 23:23:01 +00006573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006574posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006575{
Victor Stinner8c62be82010-05-06 00:08:46 +00006576 int fd;
6577 pid_t pgid;
6578 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6579 return NULL;
6580 pgid = tcgetpgrp(fd);
6581 if (pgid < 0)
6582 return posix_error();
6583 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006584}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006585#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006586
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006587
Guido van Rossumb6775db1994-08-01 11:34:53 +00006588#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006589PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006590"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006591Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006592
Barry Warsaw53699e91996-12-10 23:23:01 +00006593static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006594posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006595{
Victor Stinner8c62be82010-05-06 00:08:46 +00006596 int fd;
6597 pid_t pgid;
6598 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6599 return NULL;
6600 if (tcsetpgrp(fd, pgid) < 0)
6601 return posix_error();
6602 Py_INCREF(Py_None);
6603 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006604}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006605#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006606
Guido van Rossum687dd131993-05-17 08:34:16 +00006607/* Functions acting on file descriptors */
6608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006609PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006610"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006611Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006612
Barry Warsaw53699e91996-12-10 23:23:01 +00006613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006614posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006615{
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 PyObject *ofile;
6617 char *file;
6618 int flag;
6619 int mode = 0777;
6620 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006621
6622#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006623 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006624 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 Py_BEGIN_ALLOW_THREADS
6626 /* PyUnicode_AS_UNICODE OK without thread
6627 lock as it is a simple dereference. */
6628 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6629 Py_END_ALLOW_THREADS
6630 if (fd < 0)
6631 return posix_error();
6632 return PyLong_FromLong((long)fd);
6633 }
6634 /* Drop the argument parsing error as narrow strings
6635 are also valid. */
6636 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006637#endif
6638
Victor Stinner26de69d2011-06-17 15:15:38 +02006639 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 PyUnicode_FSConverter, &ofile,
6641 &flag, &mode))
6642 return NULL;
6643 file = PyBytes_AsString(ofile);
6644 Py_BEGIN_ALLOW_THREADS
6645 fd = open(file, flag, mode);
6646 Py_END_ALLOW_THREADS
6647 if (fd < 0)
6648 return posix_error_with_allocated_filename(ofile);
6649 Py_DECREF(ofile);
6650 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006651}
6652
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006654PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006655"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006656Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006657
Barry Warsaw53699e91996-12-10 23:23:01 +00006658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006659posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006660{
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 int fd, res;
6662 if (!PyArg_ParseTuple(args, "i:close", &fd))
6663 return NULL;
6664 if (!_PyVerify_fd(fd))
6665 return posix_error();
6666 Py_BEGIN_ALLOW_THREADS
6667 res = close(fd);
6668 Py_END_ALLOW_THREADS
6669 if (res < 0)
6670 return posix_error();
6671 Py_INCREF(Py_None);
6672 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006673}
6674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006675
Victor Stinner8c62be82010-05-06 00:08:46 +00006676PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006677"closerange(fd_low, fd_high)\n\n\
6678Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6679
6680static PyObject *
6681posix_closerange(PyObject *self, PyObject *args)
6682{
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 int fd_from, fd_to, i;
6684 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6685 return NULL;
6686 Py_BEGIN_ALLOW_THREADS
6687 for (i = fd_from; i < fd_to; i++)
6688 if (_PyVerify_fd(i))
6689 close(i);
6690 Py_END_ALLOW_THREADS
6691 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006692}
6693
6694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006695PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006696"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006697Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006698
Barry Warsaw53699e91996-12-10 23:23:01 +00006699static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006700posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006701{
Victor Stinner8c62be82010-05-06 00:08:46 +00006702 int fd;
6703 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6704 return NULL;
6705 if (!_PyVerify_fd(fd))
6706 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006707 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 if (fd < 0)
6709 return posix_error();
6710 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006711}
6712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006714PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006715"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006716Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006717
Barry Warsaw53699e91996-12-10 23:23:01 +00006718static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006719posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006720{
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 int fd, fd2, res;
6722 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6723 return NULL;
6724 if (!_PyVerify_fd_dup2(fd, fd2))
6725 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 if (res < 0)
6728 return posix_error();
6729 Py_INCREF(Py_None);
6730 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006731}
6732
Ross Lagerwall7807c352011-03-17 20:20:30 +02006733#ifdef HAVE_LOCKF
6734PyDoc_STRVAR(posix_lockf__doc__,
6735"lockf(fd, cmd, len)\n\n\
6736Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6737fd is an open file descriptor.\n\
6738cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6739F_TEST.\n\
6740len specifies the section of the file to lock.");
6741
6742static PyObject *
6743posix_lockf(PyObject *self, PyObject *args)
6744{
6745 int fd, cmd, res;
6746 off_t len;
6747 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6748 &fd, &cmd, _parse_off_t, &len))
6749 return NULL;
6750
6751 Py_BEGIN_ALLOW_THREADS
6752 res = lockf(fd, cmd, len);
6753 Py_END_ALLOW_THREADS
6754
6755 if (res < 0)
6756 return posix_error();
6757
6758 Py_RETURN_NONE;
6759}
6760#endif
6761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006763PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006764"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006765Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006766
Barry Warsaw53699e91996-12-10 23:23:01 +00006767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006768posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006769{
Victor Stinner8c62be82010-05-06 00:08:46 +00006770 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006771#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006772 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006773#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006775#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006776 PyObject *posobj;
6777 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006779#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006780 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6781 switch (how) {
6782 case 0: how = SEEK_SET; break;
6783 case 1: how = SEEK_CUR; break;
6784 case 2: how = SEEK_END; break;
6785 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006786#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006787
Ross Lagerwall8e749672011-03-17 21:54:07 +02006788#if !defined(HAVE_LARGEFILE_SUPPORT)
6789 pos = PyLong_AsLong(posobj);
6790#else
6791 pos = PyLong_AsLongLong(posobj);
6792#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 if (PyErr_Occurred())
6794 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006795
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 if (!_PyVerify_fd(fd))
6797 return posix_error();
6798 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006799#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006801#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006803#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 Py_END_ALLOW_THREADS
6805 if (res < 0)
6806 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006807
6808#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006810#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006812#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006813}
6814
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006816PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006817"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006818Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006819
Barry Warsaw53699e91996-12-10 23:23:01 +00006820static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006821posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006822{
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 int fd, size;
6824 Py_ssize_t n;
6825 PyObject *buffer;
6826 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6827 return NULL;
6828 if (size < 0) {
6829 errno = EINVAL;
6830 return posix_error();
6831 }
6832 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6833 if (buffer == NULL)
6834 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006835 if (!_PyVerify_fd(fd)) {
6836 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006838 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006839 Py_BEGIN_ALLOW_THREADS
6840 n = read(fd, PyBytes_AS_STRING(buffer), size);
6841 Py_END_ALLOW_THREADS
6842 if (n < 0) {
6843 Py_DECREF(buffer);
6844 return posix_error();
6845 }
6846 if (n != size)
6847 _PyBytes_Resize(&buffer, n);
6848 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006849}
6850
Ross Lagerwall7807c352011-03-17 20:20:30 +02006851#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6852 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006853static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006854iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6855{
6856 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006857 Py_ssize_t blen, total = 0;
6858
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006859 *iov = PyMem_New(struct iovec, cnt);
6860 if (*iov == NULL) {
6861 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006862 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006863 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006864
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006865 *buf = PyMem_New(Py_buffer, cnt);
6866 if (*buf == NULL) {
6867 PyMem_Del(*iov);
6868 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006869 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006870 }
6871
6872 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006873 PyObject *item = PySequence_GetItem(seq, i);
6874 if (item == NULL)
6875 goto fail;
6876 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6877 Py_DECREF(item);
6878 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006879 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006880 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006881 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006882 blen = (*buf)[i].len;
6883 (*iov)[i].iov_len = blen;
6884 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006885 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006886 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006887
6888fail:
6889 PyMem_Del(*iov);
6890 for (j = 0; j < i; j++) {
6891 PyBuffer_Release(&(*buf)[j]);
6892 }
6893 PyMem_Del(*buf);
6894 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006895}
6896
6897static void
6898iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6899{
6900 int i;
6901 PyMem_Del(iov);
6902 for (i = 0; i < cnt; i++) {
6903 PyBuffer_Release(&buf[i]);
6904 }
6905 PyMem_Del(buf);
6906}
6907#endif
6908
Ross Lagerwall7807c352011-03-17 20:20:30 +02006909#ifdef HAVE_READV
6910PyDoc_STRVAR(posix_readv__doc__,
6911"readv(fd, buffers) -> bytesread\n\n\
6912Read from a file descriptor into a number of writable buffers. buffers\n\
6913is an arbitrary sequence of writable buffers.\n\
6914Returns the total number of bytes read.");
6915
6916static PyObject *
6917posix_readv(PyObject *self, PyObject *args)
6918{
6919 int fd, cnt;
6920 Py_ssize_t n;
6921 PyObject *seq;
6922 struct iovec *iov;
6923 Py_buffer *buf;
6924
6925 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6926 return NULL;
6927 if (!PySequence_Check(seq)) {
6928 PyErr_SetString(PyExc_TypeError,
6929 "readv() arg 2 must be a sequence");
6930 return NULL;
6931 }
6932 cnt = PySequence_Size(seq);
6933
6934 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6935 return NULL;
6936
6937 Py_BEGIN_ALLOW_THREADS
6938 n = readv(fd, iov, cnt);
6939 Py_END_ALLOW_THREADS
6940
6941 iov_cleanup(iov, buf, cnt);
6942 return PyLong_FromSsize_t(n);
6943}
6944#endif
6945
6946#ifdef HAVE_PREAD
6947PyDoc_STRVAR(posix_pread__doc__,
6948"pread(fd, buffersize, offset) -> string\n\n\
6949Read from a file descriptor, fd, at a position of offset. It will read up\n\
6950to buffersize number of bytes. The file offset remains unchanged.");
6951
6952static PyObject *
6953posix_pread(PyObject *self, PyObject *args)
6954{
6955 int fd, size;
6956 off_t offset;
6957 Py_ssize_t n;
6958 PyObject *buffer;
6959 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6960 return NULL;
6961
6962 if (size < 0) {
6963 errno = EINVAL;
6964 return posix_error();
6965 }
6966 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6967 if (buffer == NULL)
6968 return NULL;
6969 if (!_PyVerify_fd(fd)) {
6970 Py_DECREF(buffer);
6971 return posix_error();
6972 }
6973 Py_BEGIN_ALLOW_THREADS
6974 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6975 Py_END_ALLOW_THREADS
6976 if (n < 0) {
6977 Py_DECREF(buffer);
6978 return posix_error();
6979 }
6980 if (n != size)
6981 _PyBytes_Resize(&buffer, n);
6982 return buffer;
6983}
6984#endif
6985
6986PyDoc_STRVAR(posix_write__doc__,
6987"write(fd, string) -> byteswritten\n\n\
6988Write a string to a file descriptor.");
6989
6990static PyObject *
6991posix_write(PyObject *self, PyObject *args)
6992{
6993 Py_buffer pbuf;
6994 int fd;
6995 Py_ssize_t size, len;
6996
6997 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
6998 return NULL;
6999 if (!_PyVerify_fd(fd)) {
7000 PyBuffer_Release(&pbuf);
7001 return posix_error();
7002 }
7003 len = pbuf.len;
7004 Py_BEGIN_ALLOW_THREADS
7005#if defined(MS_WIN64) || defined(MS_WINDOWS)
7006 if (len > INT_MAX)
7007 len = INT_MAX;
7008 size = write(fd, pbuf.buf, (int)len);
7009#else
7010 size = write(fd, pbuf.buf, len);
7011#endif
7012 Py_END_ALLOW_THREADS
7013 PyBuffer_Release(&pbuf);
7014 if (size < 0)
7015 return posix_error();
7016 return PyLong_FromSsize_t(size);
7017}
7018
7019#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007020PyDoc_STRVAR(posix_sendfile__doc__,
7021"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7022sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7023 -> byteswritten\n\
7024Copy nbytes bytes from file descriptor in to file descriptor out.");
7025
7026static PyObject *
7027posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7028{
7029 int in, out;
7030 Py_ssize_t ret;
7031 off_t offset;
7032
7033#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7034#ifndef __APPLE__
7035 Py_ssize_t len;
7036#endif
7037 PyObject *headers = NULL, *trailers = NULL;
7038 Py_buffer *hbuf, *tbuf;
7039 off_t sbytes;
7040 struct sf_hdtr sf;
7041 int flags = 0;
7042 sf.headers = NULL;
7043 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007044 static char *keywords[] = {"out", "in",
7045 "offset", "count",
7046 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007047
7048#ifdef __APPLE__
7049 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007050 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007051#else
7052 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007053 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007054#endif
7055 &headers, &trailers, &flags))
7056 return NULL;
7057 if (headers != NULL) {
7058 if (!PySequence_Check(headers)) {
7059 PyErr_SetString(PyExc_TypeError,
7060 "sendfile() headers must be a sequence or None");
7061 return NULL;
7062 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007063 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007064 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007065 if (sf.hdr_cnt > 0 &&
7066 !(i = iov_setup(&(sf.headers), &hbuf,
7067 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007068 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007069#ifdef __APPLE__
7070 sbytes += i;
7071#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007072 }
7073 }
7074 if (trailers != NULL) {
7075 if (!PySequence_Check(trailers)) {
7076 PyErr_SetString(PyExc_TypeError,
7077 "sendfile() trailers must be a sequence or None");
7078 return NULL;
7079 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007080 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007081 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007082 if (sf.trl_cnt > 0 &&
7083 !(i = iov_setup(&(sf.trailers), &tbuf,
7084 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007085 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007086#ifdef __APPLE__
7087 sbytes += i;
7088#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007089 }
7090 }
7091
7092 Py_BEGIN_ALLOW_THREADS
7093#ifdef __APPLE__
7094 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7095#else
7096 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7097#endif
7098 Py_END_ALLOW_THREADS
7099
7100 if (sf.headers != NULL)
7101 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7102 if (sf.trailers != NULL)
7103 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7104
7105 if (ret < 0) {
7106 if ((errno == EAGAIN) || (errno == EBUSY)) {
7107 if (sbytes != 0) {
7108 // some data has been sent
7109 goto done;
7110 }
7111 else {
7112 // no data has been sent; upper application is supposed
7113 // to retry on EAGAIN or EBUSY
7114 return posix_error();
7115 }
7116 }
7117 return posix_error();
7118 }
7119 goto done;
7120
7121done:
7122 #if !defined(HAVE_LARGEFILE_SUPPORT)
7123 return Py_BuildValue("l", sbytes);
7124 #else
7125 return Py_BuildValue("L", sbytes);
7126 #endif
7127
7128#else
7129 Py_ssize_t count;
7130 PyObject *offobj;
7131 static char *keywords[] = {"out", "in",
7132 "offset", "count", NULL};
7133 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7134 keywords, &out, &in, &offobj, &count))
7135 return NULL;
7136#ifdef linux
7137 if (offobj == Py_None) {
7138 Py_BEGIN_ALLOW_THREADS
7139 ret = sendfile(out, in, NULL, count);
7140 Py_END_ALLOW_THREADS
7141 if (ret < 0)
7142 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007143 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007144 }
7145#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007146 if (!_parse_off_t(offobj, &offset))
7147 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007148 Py_BEGIN_ALLOW_THREADS
7149 ret = sendfile(out, in, &offset, count);
7150 Py_END_ALLOW_THREADS
7151 if (ret < 0)
7152 return posix_error();
7153 return Py_BuildValue("n", ret);
7154#endif
7155}
7156#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007157
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007158PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007159"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007160Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007161
Barry Warsaw53699e91996-12-10 23:23:01 +00007162static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007163posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007164{
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 int fd;
7166 STRUCT_STAT st;
7167 int res;
7168 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7169 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007170#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 /* on OpenVMS we must ensure that all bytes are written to the file */
7172 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007173#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 if (!_PyVerify_fd(fd))
7175 return posix_error();
7176 Py_BEGIN_ALLOW_THREADS
7177 res = FSTAT(fd, &st);
7178 Py_END_ALLOW_THREADS
7179 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007180#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007182#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007184#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 }
Tim Peters5aa91602002-01-30 05:46:57 +00007186
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007188}
7189
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007190PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007191"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007192Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007193connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007194
7195static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007196posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007197{
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 int fd;
7199 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7200 return NULL;
7201 if (!_PyVerify_fd(fd))
7202 return PyBool_FromLong(0);
7203 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007204}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007205
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007206#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007207PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007208"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007209Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007210
Barry Warsaw53699e91996-12-10 23:23:01 +00007211static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007212posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007213{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007214#if defined(PYOS_OS2)
7215 HFILE read, write;
7216 APIRET rc;
7217
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007218 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007219 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007220 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007221
7222 return Py_BuildValue("(ii)", read, write);
7223#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007224#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 int fds[2];
7226 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007227 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 if (res != 0)
7229 return posix_error();
7230 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007231#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 HANDLE read, write;
7233 int read_fd, write_fd;
7234 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007236 if (!ok)
7237 return win32_error("CreatePipe", NULL);
7238 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7239 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7240 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007241#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007242#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007243}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007244#endif /* HAVE_PIPE */
7245
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007246#ifdef HAVE_PIPE2
7247PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007248"pipe2(flags) -> (read_end, write_end)\n\n\
7249Create a pipe with flags set atomically.\n\
7250flags can be constructed by ORing together one or more of these values:\n\
7251O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007252");
7253
7254static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007255posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007256{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007257 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007258 int fds[2];
7259 int res;
7260
Charles-François Natali368f34b2011-06-06 19:49:47 +02007261 flags = PyLong_AsLong(arg);
7262 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007263 return NULL;
7264
7265 res = pipe2(fds, flags);
7266 if (res != 0)
7267 return posix_error();
7268 return Py_BuildValue("(ii)", fds[0], fds[1]);
7269}
7270#endif /* HAVE_PIPE2 */
7271
Ross Lagerwall7807c352011-03-17 20:20:30 +02007272#ifdef HAVE_WRITEV
7273PyDoc_STRVAR(posix_writev__doc__,
7274"writev(fd, buffers) -> byteswritten\n\n\
7275Write the contents of buffers to a file descriptor, where buffers is an\n\
7276arbitrary sequence of buffers.\n\
7277Returns the total bytes written.");
7278
7279static PyObject *
7280posix_writev(PyObject *self, PyObject *args)
7281{
7282 int fd, cnt;
7283 Py_ssize_t res;
7284 PyObject *seq;
7285 struct iovec *iov;
7286 Py_buffer *buf;
7287 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7288 return NULL;
7289 if (!PySequence_Check(seq)) {
7290 PyErr_SetString(PyExc_TypeError,
7291 "writev() arg 2 must be a sequence");
7292 return NULL;
7293 }
7294 cnt = PySequence_Size(seq);
7295
7296 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7297 return NULL;
7298 }
7299
7300 Py_BEGIN_ALLOW_THREADS
7301 res = writev(fd, iov, cnt);
7302 Py_END_ALLOW_THREADS
7303
7304 iov_cleanup(iov, buf, cnt);
7305 return PyLong_FromSsize_t(res);
7306}
7307#endif
7308
7309#ifdef HAVE_PWRITE
7310PyDoc_STRVAR(posix_pwrite__doc__,
7311"pwrite(fd, string, offset) -> byteswritten\n\n\
7312Write string to a file descriptor, fd, from offset, leaving the file\n\
7313offset unchanged.");
7314
7315static PyObject *
7316posix_pwrite(PyObject *self, PyObject *args)
7317{
7318 Py_buffer pbuf;
7319 int fd;
7320 off_t offset;
7321 Py_ssize_t size;
7322
7323 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7324 return NULL;
7325
7326 if (!_PyVerify_fd(fd)) {
7327 PyBuffer_Release(&pbuf);
7328 return posix_error();
7329 }
7330 Py_BEGIN_ALLOW_THREADS
7331 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7332 Py_END_ALLOW_THREADS
7333 PyBuffer_Release(&pbuf);
7334 if (size < 0)
7335 return posix_error();
7336 return PyLong_FromSsize_t(size);
7337}
7338#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007339
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007340#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007341PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007342"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007343Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007344
Barry Warsaw53699e91996-12-10 23:23:01 +00007345static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007346posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007347{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007348 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007349 char *filename;
7350 int mode = 0666;
7351 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007352 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7353 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007355 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 Py_BEGIN_ALLOW_THREADS
7357 res = mkfifo(filename, mode);
7358 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007359 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007360 if (res < 0)
7361 return posix_error();
7362 Py_INCREF(Py_None);
7363 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007364}
7365#endif
7366
7367
Neal Norwitz11690112002-07-30 01:08:28 +00007368#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007369PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007370"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007371Create a filesystem node (file, device special file or named pipe)\n\
7372named filename. mode specifies both the permissions to use and the\n\
7373type of node to be created, being combined (bitwise OR) with one of\n\
7374S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007375device defines the newly created device special file (probably using\n\
7376os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007377
7378
7379static PyObject *
7380posix_mknod(PyObject *self, PyObject *args)
7381{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007382 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 char *filename;
7384 int mode = 0600;
7385 int device = 0;
7386 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007387 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7388 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007389 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007390 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007391 Py_BEGIN_ALLOW_THREADS
7392 res = mknod(filename, mode, device);
7393 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007394 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007395 if (res < 0)
7396 return posix_error();
7397 Py_INCREF(Py_None);
7398 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007399}
7400#endif
7401
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007402#ifdef HAVE_DEVICE_MACROS
7403PyDoc_STRVAR(posix_major__doc__,
7404"major(device) -> major number\n\
7405Extracts a device major number from a raw device number.");
7406
7407static PyObject *
7408posix_major(PyObject *self, PyObject *args)
7409{
Victor Stinner8c62be82010-05-06 00:08:46 +00007410 int device;
7411 if (!PyArg_ParseTuple(args, "i:major", &device))
7412 return NULL;
7413 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007414}
7415
7416PyDoc_STRVAR(posix_minor__doc__,
7417"minor(device) -> minor number\n\
7418Extracts a device minor number from a raw device number.");
7419
7420static PyObject *
7421posix_minor(PyObject *self, PyObject *args)
7422{
Victor Stinner8c62be82010-05-06 00:08:46 +00007423 int device;
7424 if (!PyArg_ParseTuple(args, "i:minor", &device))
7425 return NULL;
7426 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007427}
7428
7429PyDoc_STRVAR(posix_makedev__doc__,
7430"makedev(major, minor) -> device number\n\
7431Composes a raw device number from the major and minor device numbers.");
7432
7433static PyObject *
7434posix_makedev(PyObject *self, PyObject *args)
7435{
Victor Stinner8c62be82010-05-06 00:08:46 +00007436 int major, minor;
7437 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7438 return NULL;
7439 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007440}
7441#endif /* device macros */
7442
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007443
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007444#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007445PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007446"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007447Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007448
Barry Warsaw53699e91996-12-10 23:23:01 +00007449static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007450posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007451{
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 int fd;
7453 off_t length;
7454 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007455
Ross Lagerwall7807c352011-03-17 20:20:30 +02007456 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007457 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007458
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 Py_BEGIN_ALLOW_THREADS
7460 res = ftruncate(fd, length);
7461 Py_END_ALLOW_THREADS
7462 if (res < 0)
7463 return posix_error();
7464 Py_INCREF(Py_None);
7465 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007466}
7467#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007468
Ross Lagerwall7807c352011-03-17 20:20:30 +02007469#ifdef HAVE_TRUNCATE
7470PyDoc_STRVAR(posix_truncate__doc__,
7471"truncate(path, length)\n\n\
7472Truncate the file given by path to length bytes.");
7473
7474static PyObject *
7475posix_truncate(PyObject *self, PyObject *args)
7476{
7477 PyObject *opath;
7478 const char *path;
7479 off_t length;
7480 int res;
7481
7482 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7483 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7484 return NULL;
7485 path = PyBytes_AsString(opath);
7486
7487 Py_BEGIN_ALLOW_THREADS
7488 res = truncate(path, length);
7489 Py_END_ALLOW_THREADS
7490 Py_DECREF(opath);
7491 if (res < 0)
7492 return posix_error();
7493 Py_RETURN_NONE;
7494}
7495#endif
7496
7497#ifdef HAVE_POSIX_FALLOCATE
7498PyDoc_STRVAR(posix_posix_fallocate__doc__,
7499"posix_fallocate(fd, offset, len)\n\n\
7500Ensures that enough disk space is allocated for the file specified by fd\n\
7501starting from offset and continuing for len bytes.");
7502
7503static PyObject *
7504posix_posix_fallocate(PyObject *self, PyObject *args)
7505{
7506 off_t len, offset;
7507 int res, fd;
7508
7509 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7510 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7511 return NULL;
7512
7513 Py_BEGIN_ALLOW_THREADS
7514 res = posix_fallocate(fd, offset, len);
7515 Py_END_ALLOW_THREADS
7516 if (res != 0) {
7517 errno = res;
7518 return posix_error();
7519 }
7520 Py_RETURN_NONE;
7521}
7522#endif
7523
7524#ifdef HAVE_POSIX_FADVISE
7525PyDoc_STRVAR(posix_posix_fadvise__doc__,
7526"posix_fadvise(fd, offset, len, advice)\n\n\
7527Announces an intention to access data in a specific pattern thus allowing\n\
7528the kernel to make optimizations.\n\
7529The advice applies to the region of the file specified by fd starting at\n\
7530offset and continuing for len bytes.\n\
7531advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7532POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7533POSIX_FADV_DONTNEED.");
7534
7535static PyObject *
7536posix_posix_fadvise(PyObject *self, PyObject *args)
7537{
7538 off_t len, offset;
7539 int res, fd, advice;
7540
7541 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7542 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7543 return NULL;
7544
7545 Py_BEGIN_ALLOW_THREADS
7546 res = posix_fadvise(fd, offset, len, advice);
7547 Py_END_ALLOW_THREADS
7548 if (res != 0) {
7549 errno = res;
7550 return posix_error();
7551 }
7552 Py_RETURN_NONE;
7553}
7554#endif
7555
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007556#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007557PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007558"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007559Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007560
Fred Drake762e2061999-08-26 17:23:54 +00007561/* Save putenv() parameters as values here, so we can collect them when they
7562 * get re-set with another call for the same key. */
7563static PyObject *posix_putenv_garbage;
7564
Tim Peters5aa91602002-01-30 05:46:57 +00007565static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007566posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007567{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007568#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 wchar_t *s1, *s2;
7570 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007571#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007572 PyObject *os1, *os2;
7573 char *s1, *s2;
7574 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007575#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007576 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007578
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007579#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007580 if (!PyArg_ParseTuple(args,
7581 "uu:putenv",
7582 &s1, &s2))
7583 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007584#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007585 if (!PyArg_ParseTuple(args,
7586 "O&O&:putenv",
7587 PyUnicode_FSConverter, &os1,
7588 PyUnicode_FSConverter, &os2))
7589 return NULL;
7590 s1 = PyBytes_AsString(os1);
7591 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007592#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007593
7594#if defined(PYOS_OS2)
7595 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7596 APIRET rc;
7597
Guido van Rossumd48f2521997-12-05 22:19:34 +00007598 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007599 if (rc != NO_ERROR) {
7600 os2_error(rc);
7601 goto error;
7602 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007603
7604 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7605 APIRET rc;
7606
Guido van Rossumd48f2521997-12-05 22:19:34 +00007607 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007608 if (rc != NO_ERROR) {
7609 os2_error(rc);
7610 goto error;
7611 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007612 } else {
7613#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007614 /* XXX This can leak memory -- not easy to fix :-( */
7615 /* len includes space for a trailing \0; the size arg to
7616 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007617#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007618 len = wcslen(s1) + wcslen(s2) + 2;
7619 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007620#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007621 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007623#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007624 if (newstr == NULL) {
7625 PyErr_NoMemory();
7626 goto error;
7627 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007628#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007629 newenv = PyUnicode_AsUnicode(newstr);
7630 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7631 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007633 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007635#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007636 newenv = PyBytes_AS_STRING(newstr);
7637 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7638 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007639 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007640 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007641 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007642#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007643
Victor Stinner8c62be82010-05-06 00:08:46 +00007644 /* Install the first arg and newstr in posix_putenv_garbage;
7645 * this will cause previous value to be collected. This has to
7646 * happen after the real putenv() call because the old value
7647 * was still accessible until then. */
7648 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007649#ifdef MS_WINDOWS
7650 PyTuple_GET_ITEM(args, 0),
7651#else
7652 os1,
7653#endif
7654 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007655 /* really not much we can do; just leak */
7656 PyErr_Clear();
7657 }
7658 else {
7659 Py_DECREF(newstr);
7660 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007661
7662#if defined(PYOS_OS2)
7663 }
7664#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007665
Martin v. Löwis011e8422009-05-05 04:43:17 +00007666#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 Py_DECREF(os1);
7668 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007669#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007670 Py_RETURN_NONE;
7671
7672error:
7673#ifndef MS_WINDOWS
7674 Py_DECREF(os1);
7675 Py_DECREF(os2);
7676#endif
7677 Py_XDECREF(newstr);
7678 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007679}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007680#endif /* putenv */
7681
Guido van Rossumc524d952001-10-19 01:31:59 +00007682#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007683PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007684"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007685Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007686
7687static PyObject *
7688posix_unsetenv(PyObject *self, PyObject *args)
7689{
Victor Stinner84ae1182010-05-06 22:05:07 +00007690#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007691 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007692
Victor Stinner8c62be82010-05-06 00:08:46 +00007693 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7694 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007695#else
7696 PyObject *os1;
7697 char *s1;
7698
7699 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7700 PyUnicode_FSConverter, &os1))
7701 return NULL;
7702 s1 = PyBytes_AsString(os1);
7703#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007704
Victor Stinner8c62be82010-05-06 00:08:46 +00007705 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007706
Victor Stinner8c62be82010-05-06 00:08:46 +00007707 /* Remove the key from posix_putenv_garbage;
7708 * this will cause it to be collected. This has to
7709 * happen after the real unsetenv() call because the
7710 * old value was still accessible until then.
7711 */
7712 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007713#ifdef MS_WINDOWS
7714 PyTuple_GET_ITEM(args, 0)
7715#else
7716 os1
7717#endif
7718 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 /* really not much we can do; just leak */
7720 PyErr_Clear();
7721 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007722
Victor Stinner84ae1182010-05-06 22:05:07 +00007723#ifndef MS_WINDOWS
7724 Py_DECREF(os1);
7725#endif
7726 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007727}
7728#endif /* unsetenv */
7729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007730PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007731"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007732Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007733
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007734static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007735posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007736{
Victor Stinner8c62be82010-05-06 00:08:46 +00007737 int code;
7738 char *message;
7739 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7740 return NULL;
7741 message = strerror(code);
7742 if (message == NULL) {
7743 PyErr_SetString(PyExc_ValueError,
7744 "strerror() argument out of range");
7745 return NULL;
7746 }
7747 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007748}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007749
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007750
Guido van Rossumc9641791998-08-04 15:26:23 +00007751#ifdef HAVE_SYS_WAIT_H
7752
Fred Drake106c1a02002-04-23 15:58:02 +00007753#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007754PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007755"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007756Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007757
7758static PyObject *
7759posix_WCOREDUMP(PyObject *self, PyObject *args)
7760{
Victor Stinner8c62be82010-05-06 00:08:46 +00007761 WAIT_TYPE status;
7762 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007763
Victor Stinner8c62be82010-05-06 00:08:46 +00007764 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7765 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007766
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007768}
7769#endif /* WCOREDUMP */
7770
7771#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007772PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007773"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007774Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007775job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007776
7777static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007778posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007779{
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 WAIT_TYPE status;
7781 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007782
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7784 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007785
Victor Stinner8c62be82010-05-06 00:08:46 +00007786 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007787}
7788#endif /* WIFCONTINUED */
7789
Guido van Rossumc9641791998-08-04 15:26:23 +00007790#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007791PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007792"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007793Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007794
7795static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007796posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007797{
Victor Stinner8c62be82010-05-06 00:08:46 +00007798 WAIT_TYPE status;
7799 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007800
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7802 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007803
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007805}
7806#endif /* WIFSTOPPED */
7807
7808#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007809PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007810"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007811Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007812
7813static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007814posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007815{
Victor Stinner8c62be82010-05-06 00:08:46 +00007816 WAIT_TYPE status;
7817 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007818
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7820 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007821
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007823}
7824#endif /* WIFSIGNALED */
7825
7826#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007827PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007828"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007829Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007830system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007831
7832static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007833posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007834{
Victor Stinner8c62be82010-05-06 00:08:46 +00007835 WAIT_TYPE status;
7836 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007837
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7839 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007840
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007842}
7843#endif /* WIFEXITED */
7844
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007845#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007846PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007847"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007848Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007849
7850static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007851posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007852{
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 WAIT_TYPE status;
7854 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007855
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7857 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007858
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007860}
7861#endif /* WEXITSTATUS */
7862
7863#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007864PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007865"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007866Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007867value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007868
7869static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007870posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007871{
Victor Stinner8c62be82010-05-06 00:08:46 +00007872 WAIT_TYPE status;
7873 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007874
Victor Stinner8c62be82010-05-06 00:08:46 +00007875 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7876 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007877
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007879}
7880#endif /* WTERMSIG */
7881
7882#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007883PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007884"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007885Return the signal that stopped the process that provided\n\
7886the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007887
7888static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007889posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007890{
Victor Stinner8c62be82010-05-06 00:08:46 +00007891 WAIT_TYPE status;
7892 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007893
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7895 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007896
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007898}
7899#endif /* WSTOPSIG */
7900
7901#endif /* HAVE_SYS_WAIT_H */
7902
7903
Thomas Wouters477c8d52006-05-27 19:21:47 +00007904#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007905#ifdef _SCO_DS
7906/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7907 needed definitions in sys/statvfs.h */
7908#define _SVID3
7909#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007910#include <sys/statvfs.h>
7911
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007912static PyObject*
7913_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007914 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7915 if (v == NULL)
7916 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007917
7918#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7920 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7921 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7922 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7923 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7924 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7925 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7926 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7927 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7928 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007929#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7931 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7932 PyStructSequence_SET_ITEM(v, 2,
7933 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7934 PyStructSequence_SET_ITEM(v, 3,
7935 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7936 PyStructSequence_SET_ITEM(v, 4,
7937 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7938 PyStructSequence_SET_ITEM(v, 5,
7939 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7940 PyStructSequence_SET_ITEM(v, 6,
7941 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7942 PyStructSequence_SET_ITEM(v, 7,
7943 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7944 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7945 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007946#endif
7947
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007949}
7950
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007951PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007952"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007953Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007954
7955static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007956posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007957{
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 int fd, res;
7959 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007960
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7962 return NULL;
7963 Py_BEGIN_ALLOW_THREADS
7964 res = fstatvfs(fd, &st);
7965 Py_END_ALLOW_THREADS
7966 if (res != 0)
7967 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007968
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007970}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007971#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007972
7973
Thomas Wouters477c8d52006-05-27 19:21:47 +00007974#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007975#include <sys/statvfs.h>
7976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007977PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007978"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007979Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007980
7981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007982posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007983{
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 char *path;
7985 int res;
7986 struct statvfs st;
7987 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7988 return NULL;
7989 Py_BEGIN_ALLOW_THREADS
7990 res = statvfs(path, &st);
7991 Py_END_ALLOW_THREADS
7992 if (res != 0)
7993 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007994
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007996}
7997#endif /* HAVE_STATVFS */
7998
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02007999#ifdef MS_WINDOWS
8000PyDoc_STRVAR(win32__getdiskusage__doc__,
8001"_getdiskusage(path) -> (total, free)\n\n\
8002Return disk usage statistics about the given path as (total, free) tuple.");
8003
8004static PyObject *
8005win32__getdiskusage(PyObject *self, PyObject *args)
8006{
8007 BOOL retval;
8008 ULARGE_INTEGER _, total, free;
8009 LPCTSTR path;
8010
8011 if (! PyArg_ParseTuple(args, "s", &path))
8012 return NULL;
8013
8014 Py_BEGIN_ALLOW_THREADS
8015 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8016 Py_END_ALLOW_THREADS
8017 if (retval == 0)
8018 return PyErr_SetFromWindowsErr(0);
8019
8020 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8021}
8022#endif
8023
8024
Fred Drakec9680921999-12-13 16:37:25 +00008025/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8026 * It maps strings representing configuration variable names to
8027 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008028 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008029 * rarely-used constants. There are three separate tables that use
8030 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008031 *
8032 * This code is always included, even if none of the interfaces that
8033 * need it are included. The #if hackery needed to avoid it would be
8034 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008035 */
8036struct constdef {
8037 char *name;
8038 long value;
8039};
8040
Fred Drake12c6e2d1999-12-14 21:25:03 +00008041static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008042conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008043 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008044{
Christian Heimes217cfd12007-12-02 14:31:20 +00008045 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008046 *valuep = PyLong_AS_LONG(arg);
8047 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008048 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008049 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008050 /* look up the value in the table using a binary search */
8051 size_t lo = 0;
8052 size_t mid;
8053 size_t hi = tablesize;
8054 int cmp;
8055 const char *confname;
8056 if (!PyUnicode_Check(arg)) {
8057 PyErr_SetString(PyExc_TypeError,
8058 "configuration names must be strings or integers");
8059 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008061 confname = _PyUnicode_AsString(arg);
8062 if (confname == NULL)
8063 return 0;
8064 while (lo < hi) {
8065 mid = (lo + hi) / 2;
8066 cmp = strcmp(confname, table[mid].name);
8067 if (cmp < 0)
8068 hi = mid;
8069 else if (cmp > 0)
8070 lo = mid + 1;
8071 else {
8072 *valuep = table[mid].value;
8073 return 1;
8074 }
8075 }
8076 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8077 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008079}
8080
8081
8082#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8083static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008084#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008085 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008086#endif
8087#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008088 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008089#endif
Fred Drakec9680921999-12-13 16:37:25 +00008090#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008091 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008092#endif
8093#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008094 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008095#endif
8096#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008098#endif
8099#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008101#endif
8102#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008104#endif
8105#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008107#endif
8108#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008110#endif
8111#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008113#endif
8114#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008116#endif
8117#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008119#endif
8120#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008121 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008122#endif
8123#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008124 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008125#endif
8126#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008127 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008128#endif
8129#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008131#endif
8132#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008134#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008135#ifdef _PC_ACL_ENABLED
8136 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8137#endif
8138#ifdef _PC_MIN_HOLE_SIZE
8139 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8140#endif
8141#ifdef _PC_ALLOC_SIZE_MIN
8142 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8143#endif
8144#ifdef _PC_REC_INCR_XFER_SIZE
8145 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8146#endif
8147#ifdef _PC_REC_MAX_XFER_SIZE
8148 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8149#endif
8150#ifdef _PC_REC_MIN_XFER_SIZE
8151 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8152#endif
8153#ifdef _PC_REC_XFER_ALIGN
8154 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8155#endif
8156#ifdef _PC_SYMLINK_MAX
8157 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8158#endif
8159#ifdef _PC_XATTR_ENABLED
8160 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8161#endif
8162#ifdef _PC_XATTR_EXISTS
8163 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8164#endif
8165#ifdef _PC_TIMESTAMP_RESOLUTION
8166 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8167#endif
Fred Drakec9680921999-12-13 16:37:25 +00008168};
8169
Fred Drakec9680921999-12-13 16:37:25 +00008170static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008171conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008172{
8173 return conv_confname(arg, valuep, posix_constants_pathconf,
8174 sizeof(posix_constants_pathconf)
8175 / sizeof(struct constdef));
8176}
8177#endif
8178
8179#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008180PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008181"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008182Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008183If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008184
8185static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008186posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008187{
8188 PyObject *result = NULL;
8189 int name, fd;
8190
Fred Drake12c6e2d1999-12-14 21:25:03 +00008191 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8192 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008193 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008194
Stefan Krah0e803b32010-11-26 16:16:47 +00008195 errno = 0;
8196 limit = fpathconf(fd, name);
8197 if (limit == -1 && errno != 0)
8198 posix_error();
8199 else
8200 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008201 }
8202 return result;
8203}
8204#endif
8205
8206
8207#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008208PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008209"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008210Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008211If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008212
8213static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008214posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008215{
8216 PyObject *result = NULL;
8217 int name;
8218 char *path;
8219
8220 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8221 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008222 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008223
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 errno = 0;
8225 limit = pathconf(path, name);
8226 if (limit == -1 && errno != 0) {
8227 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008228 /* could be a path or name problem */
8229 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008230 else
Stefan Krah99439262010-11-26 12:58:05 +00008231 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 }
8233 else
8234 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008235 }
8236 return result;
8237}
8238#endif
8239
8240#ifdef HAVE_CONFSTR
8241static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008242#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008244#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008245#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008247#endif
8248#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008250#endif
Fred Draked86ed291999-12-15 15:34:33 +00008251#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008253#endif
8254#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008255 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008256#endif
8257#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008259#endif
8260#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008262#endif
Fred Drakec9680921999-12-13 16:37:25 +00008263#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008265#endif
8266#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008267 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008268#endif
8269#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008271#endif
8272#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008274#endif
8275#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008277#endif
8278#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008280#endif
8281#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008282 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008283#endif
8284#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008286#endif
Fred Draked86ed291999-12-15 15:34:33 +00008287#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008288 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008289#endif
Fred Drakec9680921999-12-13 16:37:25 +00008290#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008291 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008292#endif
Fred Draked86ed291999-12-15 15:34:33 +00008293#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008294 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008295#endif
8296#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008297 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008298#endif
8299#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008300 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008301#endif
8302#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008303 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008304#endif
Fred Drakec9680921999-12-13 16:37:25 +00008305#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008306 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008307#endif
8308#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008309 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008310#endif
8311#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008312 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008313#endif
8314#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008315 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008316#endif
8317#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008318 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008319#endif
8320#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008321 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008322#endif
8323#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008324 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008325#endif
8326#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008327 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008328#endif
8329#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008330 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008331#endif
8332#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008334#endif
8335#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008336 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008337#endif
8338#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008340#endif
8341#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008343#endif
8344#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008346#endif
8347#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008349#endif
8350#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008351 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008352#endif
Fred Draked86ed291999-12-15 15:34:33 +00008353#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008354 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008355#endif
8356#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008357 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008358#endif
8359#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008360 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008361#endif
8362#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008363 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008364#endif
8365#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008366 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008367#endif
8368#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008370#endif
8371#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008372 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008373#endif
8374#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008376#endif
8377#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008379#endif
8380#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008381 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008382#endif
8383#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008384 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008385#endif
8386#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008388#endif
8389#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008390 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008391#endif
Fred Drakec9680921999-12-13 16:37:25 +00008392};
8393
8394static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008395conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008396{
8397 return conv_confname(arg, valuep, posix_constants_confstr,
8398 sizeof(posix_constants_confstr)
8399 / sizeof(struct constdef));
8400}
8401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008402PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008403"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008404Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008405
8406static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008407posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008408{
8409 PyObject *result = NULL;
8410 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008411 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008412 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008413
Victor Stinnercb043522010-09-10 23:49:04 +00008414 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8415 return NULL;
8416
8417 errno = 0;
8418 len = confstr(name, buffer, sizeof(buffer));
8419 if (len == 0) {
8420 if (errno) {
8421 posix_error();
8422 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008423 }
8424 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008425 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008426 }
8427 }
Victor Stinnercb043522010-09-10 23:49:04 +00008428
8429 if ((unsigned int)len >= sizeof(buffer)) {
8430 char *buf = PyMem_Malloc(len);
8431 if (buf == NULL)
8432 return PyErr_NoMemory();
8433 confstr(name, buf, len);
8434 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8435 PyMem_Free(buf);
8436 }
8437 else
8438 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008439 return result;
8440}
8441#endif
8442
8443
8444#ifdef HAVE_SYSCONF
8445static struct constdef posix_constants_sysconf[] = {
8446#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008448#endif
8449#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008451#endif
8452#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008453 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008454#endif
8455#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008456 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008457#endif
8458#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008460#endif
8461#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008462 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008463#endif
8464#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008466#endif
8467#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008468 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008469#endif
8470#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008471 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008472#endif
8473#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008475#endif
Fred Draked86ed291999-12-15 15:34:33 +00008476#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008477 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008478#endif
8479#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008480 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008481#endif
Fred Drakec9680921999-12-13 16:37:25 +00008482#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008483 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008484#endif
Fred Drakec9680921999-12-13 16:37:25 +00008485#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008486 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008487#endif
8488#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008490#endif
8491#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008493#endif
8494#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008495 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008496#endif
8497#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008498 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008499#endif
Fred Draked86ed291999-12-15 15:34:33 +00008500#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008501 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008502#endif
Fred Drakec9680921999-12-13 16:37:25 +00008503#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008504 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008505#endif
8506#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008507 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008508#endif
8509#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008510 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008511#endif
8512#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008513 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008514#endif
8515#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008516 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008517#endif
Fred Draked86ed291999-12-15 15:34:33 +00008518#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008519 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008520#endif
Fred Drakec9680921999-12-13 16:37:25 +00008521#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008522 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008523#endif
8524#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008525 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008526#endif
8527#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008528 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008529#endif
8530#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008531 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008532#endif
8533#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008534 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008535#endif
8536#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008537 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008538#endif
8539#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008540 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008541#endif
8542#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008543 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008544#endif
8545#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008546 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008547#endif
8548#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008549 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008550#endif
8551#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008552 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008553#endif
8554#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008555 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008556#endif
8557#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008558 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008559#endif
8560#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008561 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008562#endif
8563#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008564 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008565#endif
8566#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008567 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008568#endif
8569#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008570 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008571#endif
8572#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008573 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008574#endif
8575#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008576 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008577#endif
8578#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008579 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008580#endif
8581#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008582 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008583#endif
8584#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008585 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008586#endif
8587#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008588 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008589#endif
Fred Draked86ed291999-12-15 15:34:33 +00008590#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008591 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008592#endif
Fred Drakec9680921999-12-13 16:37:25 +00008593#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008594 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008595#endif
8596#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008597 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008598#endif
8599#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008600 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008601#endif
Fred Draked86ed291999-12-15 15:34:33 +00008602#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008603 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008604#endif
Fred Drakec9680921999-12-13 16:37:25 +00008605#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008606 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008607#endif
Fred Draked86ed291999-12-15 15:34:33 +00008608#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008609 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008610#endif
8611#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008612 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008613#endif
Fred Drakec9680921999-12-13 16:37:25 +00008614#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008615 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008616#endif
8617#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008618 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008619#endif
8620#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008621 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008622#endif
8623#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008625#endif
Fred Draked86ed291999-12-15 15:34:33 +00008626#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008627 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008628#endif
Fred Drakec9680921999-12-13 16:37:25 +00008629#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008630 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008631#endif
8632#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008633 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008634#endif
8635#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008636 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008637#endif
8638#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008640#endif
8641#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008643#endif
8644#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008645 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008646#endif
8647#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008649#endif
Fred Draked86ed291999-12-15 15:34:33 +00008650#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008651 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008652#endif
Fred Drakec9680921999-12-13 16:37:25 +00008653#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008654 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008655#endif
8656#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008657 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008658#endif
Fred Draked86ed291999-12-15 15:34:33 +00008659#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008660 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008661#endif
Fred Drakec9680921999-12-13 16:37:25 +00008662#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008663 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008664#endif
8665#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008666 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008667#endif
8668#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008670#endif
8671#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008672 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008673#endif
8674#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008675 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008676#endif
8677#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008678 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008679#endif
8680#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008681 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008682#endif
8683#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008684 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008685#endif
8686#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008687 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008688#endif
Fred Draked86ed291999-12-15 15:34:33 +00008689#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008690 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008691#endif
8692#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008693 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008694#endif
Fred Drakec9680921999-12-13 16:37:25 +00008695#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008696 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008697#endif
8698#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008699 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008700#endif
8701#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008702 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008703#endif
8704#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008705 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008706#endif
8707#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008709#endif
8710#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008711 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008712#endif
8713#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008715#endif
8716#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008717 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008718#endif
8719#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008720 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008721#endif
8722#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008723 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008724#endif
8725#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008726 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008727#endif
8728#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008729 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008730#endif
8731#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008732 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008733#endif
8734#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008735 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008736#endif
8737#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008738 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008739#endif
8740#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008741 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008742#endif
8743#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008744 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008745#endif
8746#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008747 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008748#endif
8749#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008750 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008751#endif
8752#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008753 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008754#endif
8755#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008757#endif
8758#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008760#endif
8761#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008762 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008763#endif
8764#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008766#endif
8767#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008768 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008769#endif
8770#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008772#endif
8773#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008775#endif
8776#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008777 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008778#endif
8779#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008780 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008781#endif
8782#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008783 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008784#endif
8785#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008786 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008787#endif
8788#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008789 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008790#endif
8791#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008793#endif
8794#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008796#endif
8797#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008798 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008799#endif
Fred Draked86ed291999-12-15 15:34:33 +00008800#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008802#endif
Fred Drakec9680921999-12-13 16:37:25 +00008803#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008805#endif
8806#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008808#endif
8809#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008811#endif
8812#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008814#endif
8815#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008817#endif
8818#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008820#endif
8821#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
8842#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008844#endif
8845#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008847#endif
8848#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
8851#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008852 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008853#endif
8854#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008855 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008856#endif
8857#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008859#endif
8860#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008861 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008862#endif
8863#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008864 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008865#endif
8866#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008868#endif
8869#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008871#endif
8872#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008874#endif
8875#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008876 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008877#endif
8878#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008879 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008880#endif
8881#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008882 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008883#endif
8884#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008886#endif
8887#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008889#endif
8890#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008892#endif
8893#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008895#endif
8896#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008898#endif
8899#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008901#endif
8902#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008904#endif
8905#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008906 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008907#endif
8908#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008910#endif
8911#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008913#endif
8914#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008915 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008916#endif
8917#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008918 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008919#endif
8920#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008921 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008922#endif
8923#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008924 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008925#endif
8926#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008928#endif
8929#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008930 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008931#endif
8932#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008933 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008934#endif
8935#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008936 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008937#endif
8938};
8939
8940static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008941conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008942{
8943 return conv_confname(arg, valuep, posix_constants_sysconf,
8944 sizeof(posix_constants_sysconf)
8945 / sizeof(struct constdef));
8946}
8947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008948PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008949"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008950Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008951
8952static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008953posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008954{
8955 PyObject *result = NULL;
8956 int name;
8957
8958 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8959 int value;
8960
8961 errno = 0;
8962 value = sysconf(name);
8963 if (value == -1 && errno != 0)
8964 posix_error();
8965 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008966 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008967 }
8968 return result;
8969}
8970#endif
8971
8972
Fred Drakebec628d1999-12-15 18:31:10 +00008973/* This code is used to ensure that the tables of configuration value names
8974 * are in sorted order as required by conv_confname(), and also to build the
8975 * the exported dictionaries that are used to publish information about the
8976 * names available on the host platform.
8977 *
8978 * Sorting the table at runtime ensures that the table is properly ordered
8979 * when used, even for platforms we're not able to test on. It also makes
8980 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008981 */
Fred Drakebec628d1999-12-15 18:31:10 +00008982
8983static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008984cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008985{
8986 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008987 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008988 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008989 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008990
8991 return strcmp(c1->name, c2->name);
8992}
8993
8994static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008995setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00008996 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008997{
Fred Drakebec628d1999-12-15 18:31:10 +00008998 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008999 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009000
9001 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9002 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009003 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009004 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009005
Barry Warsaw3155db32000-04-13 15:20:40 +00009006 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 PyObject *o = PyLong_FromLong(table[i].value);
9008 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9009 Py_XDECREF(o);
9010 Py_DECREF(d);
9011 return -1;
9012 }
9013 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009014 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009015 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009016}
9017
Fred Drakebec628d1999-12-15 18:31:10 +00009018/* Return -1 on failure, 0 on success. */
9019static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009020setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009021{
9022#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009023 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009024 sizeof(posix_constants_pathconf)
9025 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009026 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009027 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009028#endif
9029#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009030 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009031 sizeof(posix_constants_confstr)
9032 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009033 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009034 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009035#endif
9036#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009037 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009038 sizeof(posix_constants_sysconf)
9039 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009040 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009041 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009042#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009043 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009044}
Fred Draked86ed291999-12-15 15:34:33 +00009045
9046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009047PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009048"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009049Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009050in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009051
9052static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009053posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009054{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009055 abort();
9056 /*NOTREACHED*/
9057 Py_FatalError("abort() called from Python code didn't abort!");
9058 return NULL;
9059}
Fred Drakebec628d1999-12-15 18:31:10 +00009060
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009061#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009062PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009063"startfile(filepath [, operation]) - Start a file with its associated\n\
9064application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009065\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009066When \"operation\" is not specified or \"open\", this acts like\n\
9067double-clicking the file in Explorer, or giving the file name as an\n\
9068argument to the DOS \"start\" command: the file is opened with whatever\n\
9069application (if any) its extension is associated.\n\
9070When another \"operation\" is given, it specifies what should be done with\n\
9071the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009072\n\
9073startfile returns as soon as the associated application is launched.\n\
9074There is no option to wait for the application to close, and no way\n\
9075to retrieve the application's exit status.\n\
9076\n\
9077The filepath is relative to the current directory. If you want to use\n\
9078an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009079the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009080
9081static PyObject *
9082win32_startfile(PyObject *self, PyObject *args)
9083{
Victor Stinner8c62be82010-05-06 00:08:46 +00009084 PyObject *ofilepath;
9085 char *filepath;
9086 char *operation = NULL;
9087 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009088
Victor Stinner8c62be82010-05-06 00:08:46 +00009089 PyObject *unipath, *woperation = NULL;
9090 if (!PyArg_ParseTuple(args, "U|s:startfile",
9091 &unipath, &operation)) {
9092 PyErr_Clear();
9093 goto normal;
9094 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009095
Victor Stinner8c62be82010-05-06 00:08:46 +00009096 if (operation) {
9097 woperation = PyUnicode_DecodeASCII(operation,
9098 strlen(operation), NULL);
9099 if (!woperation) {
9100 PyErr_Clear();
9101 operation = NULL;
9102 goto normal;
9103 }
9104 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009105
Victor Stinner8c62be82010-05-06 00:08:46 +00009106 Py_BEGIN_ALLOW_THREADS
9107 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9108 PyUnicode_AS_UNICODE(unipath),
9109 NULL, NULL, SW_SHOWNORMAL);
9110 Py_END_ALLOW_THREADS
9111
9112 Py_XDECREF(woperation);
9113 if (rc <= (HINSTANCE)32) {
9114 PyObject *errval = win32_error_unicode("startfile",
9115 PyUnicode_AS_UNICODE(unipath));
9116 return errval;
9117 }
9118 Py_INCREF(Py_None);
9119 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009120
9121normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009122 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9123 PyUnicode_FSConverter, &ofilepath,
9124 &operation))
9125 return NULL;
9126 filepath = PyBytes_AsString(ofilepath);
9127 Py_BEGIN_ALLOW_THREADS
9128 rc = ShellExecute((HWND)0, operation, filepath,
9129 NULL, NULL, SW_SHOWNORMAL);
9130 Py_END_ALLOW_THREADS
9131 if (rc <= (HINSTANCE)32) {
9132 PyObject *errval = win32_error("startfile", filepath);
9133 Py_DECREF(ofilepath);
9134 return errval;
9135 }
9136 Py_DECREF(ofilepath);
9137 Py_INCREF(Py_None);
9138 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009139}
9140#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009141
Martin v. Löwis438b5342002-12-27 10:16:42 +00009142#ifdef HAVE_GETLOADAVG
9143PyDoc_STRVAR(posix_getloadavg__doc__,
9144"getloadavg() -> (float, float, float)\n\n\
9145Return the number of processes in the system run queue averaged over\n\
9146the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9147was unobtainable");
9148
9149static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009150posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009151{
9152 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009153 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009154 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9155 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009156 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009157 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009158}
9159#endif
9160
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009161#ifdef MS_WINDOWS
9162
9163PyDoc_STRVAR(win32_urandom__doc__,
9164"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009165Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009166
9167typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9168 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9169 DWORD dwFlags );
9170typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9171 BYTE *pbBuffer );
9172
9173static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009174/* This handle is never explicitly released. Instead, the operating
9175 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009176static HCRYPTPROV hCryptProv = 0;
9177
Tim Peters4ad82172004-08-30 17:02:04 +00009178static PyObject*
9179win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009180{
Victor Stinner8c62be82010-05-06 00:08:46 +00009181 int howMany;
9182 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009183
Victor Stinner8c62be82010-05-06 00:08:46 +00009184 /* Read arguments */
9185 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9186 return NULL;
9187 if (howMany < 0)
9188 return PyErr_Format(PyExc_ValueError,
9189 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009190
Victor Stinner8c62be82010-05-06 00:08:46 +00009191 if (hCryptProv == 0) {
9192 HINSTANCE hAdvAPI32 = NULL;
9193 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009194
Victor Stinner8c62be82010-05-06 00:08:46 +00009195 /* Obtain handle to the DLL containing CryptoAPI
9196 This should not fail */
9197 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9198 if(hAdvAPI32 == NULL)
9199 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009200
Victor Stinner8c62be82010-05-06 00:08:46 +00009201 /* Obtain pointers to the CryptoAPI functions
9202 This will fail on some early versions of Win95 */
9203 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9204 hAdvAPI32,
9205 "CryptAcquireContextA");
9206 if (pCryptAcquireContext == NULL)
9207 return PyErr_Format(PyExc_NotImplementedError,
9208 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009209
Victor Stinner8c62be82010-05-06 00:08:46 +00009210 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9211 hAdvAPI32, "CryptGenRandom");
9212 if (pCryptGenRandom == NULL)
9213 return PyErr_Format(PyExc_NotImplementedError,
9214 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009215
Victor Stinner8c62be82010-05-06 00:08:46 +00009216 /* Acquire context */
9217 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9218 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9219 return win32_error("CryptAcquireContext", NULL);
9220 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009221
Victor Stinner8c62be82010-05-06 00:08:46 +00009222 /* Allocate bytes */
9223 result = PyBytes_FromStringAndSize(NULL, howMany);
9224 if (result != NULL) {
9225 /* Get random data */
9226 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9227 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9228 PyBytes_AS_STRING(result))) {
9229 Py_DECREF(result);
9230 return win32_error("CryptGenRandom", NULL);
9231 }
9232 }
9233 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009234}
9235#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009236
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009237PyDoc_STRVAR(device_encoding__doc__,
9238"device_encoding(fd) -> str\n\n\
9239Return a string describing the encoding of the device\n\
9240if the output is a terminal; else return None.");
9241
9242static PyObject *
9243device_encoding(PyObject *self, PyObject *args)
9244{
Victor Stinner8c62be82010-05-06 00:08:46 +00009245 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009246#if defined(MS_WINDOWS) || defined(MS_WIN64)
9247 UINT cp;
9248#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009249 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9250 return NULL;
9251 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9252 Py_INCREF(Py_None);
9253 return Py_None;
9254 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009255#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009256 if (fd == 0)
9257 cp = GetConsoleCP();
9258 else if (fd == 1 || fd == 2)
9259 cp = GetConsoleOutputCP();
9260 else
9261 cp = 0;
9262 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9263 has no console */
9264 if (cp != 0)
9265 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009266#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009267 {
9268 char *codeset = nl_langinfo(CODESET);
9269 if (codeset != NULL && codeset[0] != 0)
9270 return PyUnicode_FromString(codeset);
9271 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009272#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009273 Py_INCREF(Py_None);
9274 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009275}
9276
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009277#ifdef __VMS
9278/* Use openssl random routine */
9279#include <openssl/rand.h>
9280PyDoc_STRVAR(vms_urandom__doc__,
9281"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009282Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009283
9284static PyObject*
9285vms_urandom(PyObject *self, PyObject *args)
9286{
Victor Stinner8c62be82010-05-06 00:08:46 +00009287 int howMany;
9288 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009289
Victor Stinner8c62be82010-05-06 00:08:46 +00009290 /* Read arguments */
9291 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9292 return NULL;
9293 if (howMany < 0)
9294 return PyErr_Format(PyExc_ValueError,
9295 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009296
Victor Stinner8c62be82010-05-06 00:08:46 +00009297 /* Allocate bytes */
9298 result = PyBytes_FromStringAndSize(NULL, howMany);
9299 if (result != NULL) {
9300 /* Get random data */
9301 if (RAND_pseudo_bytes((unsigned char*)
9302 PyBytes_AS_STRING(result),
9303 howMany) < 0) {
9304 Py_DECREF(result);
9305 return PyErr_Format(PyExc_ValueError,
9306 "RAND_pseudo_bytes");
9307 }
9308 }
9309 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009310}
9311#endif
9312
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009313#ifdef HAVE_SETRESUID
9314PyDoc_STRVAR(posix_setresuid__doc__,
9315"setresuid(ruid, euid, suid)\n\n\
9316Set the current process's real, effective, and saved user ids.");
9317
9318static PyObject*
9319posix_setresuid (PyObject *self, PyObject *args)
9320{
Victor Stinner8c62be82010-05-06 00:08:46 +00009321 /* We assume uid_t is no larger than a long. */
9322 long ruid, euid, suid;
9323 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9324 return NULL;
9325 if (setresuid(ruid, euid, suid) < 0)
9326 return posix_error();
9327 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009328}
9329#endif
9330
9331#ifdef HAVE_SETRESGID
9332PyDoc_STRVAR(posix_setresgid__doc__,
9333"setresgid(rgid, egid, sgid)\n\n\
9334Set the current process's real, effective, and saved group ids.");
9335
9336static PyObject*
9337posix_setresgid (PyObject *self, PyObject *args)
9338{
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 /* We assume uid_t is no larger than a long. */
9340 long rgid, egid, sgid;
9341 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9342 return NULL;
9343 if (setresgid(rgid, egid, sgid) < 0)
9344 return posix_error();
9345 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009346}
9347#endif
9348
9349#ifdef HAVE_GETRESUID
9350PyDoc_STRVAR(posix_getresuid__doc__,
9351"getresuid() -> (ruid, euid, suid)\n\n\
9352Get tuple of the current process's real, effective, and saved user ids.");
9353
9354static PyObject*
9355posix_getresuid (PyObject *self, PyObject *noargs)
9356{
Victor Stinner8c62be82010-05-06 00:08:46 +00009357 uid_t ruid, euid, suid;
9358 long l_ruid, l_euid, l_suid;
9359 if (getresuid(&ruid, &euid, &suid) < 0)
9360 return posix_error();
9361 /* Force the values into long's as we don't know the size of uid_t. */
9362 l_ruid = ruid;
9363 l_euid = euid;
9364 l_suid = suid;
9365 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009366}
9367#endif
9368
9369#ifdef HAVE_GETRESGID
9370PyDoc_STRVAR(posix_getresgid__doc__,
9371"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009372Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009373
9374static PyObject*
9375posix_getresgid (PyObject *self, PyObject *noargs)
9376{
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 uid_t rgid, egid, sgid;
9378 long l_rgid, l_egid, l_sgid;
9379 if (getresgid(&rgid, &egid, &sgid) < 0)
9380 return posix_error();
9381 /* Force the values into long's as we don't know the size of uid_t. */
9382 l_rgid = rgid;
9383 l_egid = egid;
9384 l_sgid = sgid;
9385 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009386}
9387#endif
9388
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009389/* Posix *at family of functions:
9390 faccessat, fchmodat, fchownat, fstatat, futimesat,
9391 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9392 unlinkat, utimensat, mkfifoat */
9393
9394#ifdef HAVE_FACCESSAT
9395PyDoc_STRVAR(posix_faccessat__doc__,
9396"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9397Like access() but if path is relative, it is taken as relative to dirfd.\n\
9398flags is optional and can be constructed by ORing together zero or more\n\
9399of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9400If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9401is interpreted relative to the current working directory.");
9402
9403static PyObject *
9404posix_faccessat(PyObject *self, PyObject *args)
9405{
9406 PyObject *opath;
9407 char *path;
9408 int mode;
9409 int res;
9410 int dirfd, flags = 0;
9411 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9412 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9413 return NULL;
9414 path = PyBytes_AsString(opath);
9415 Py_BEGIN_ALLOW_THREADS
9416 res = faccessat(dirfd, path, mode, flags);
9417 Py_END_ALLOW_THREADS
9418 Py_DECREF(opath);
9419 return PyBool_FromLong(res == 0);
9420}
9421#endif
9422
9423#ifdef HAVE_FCHMODAT
9424PyDoc_STRVAR(posix_fchmodat__doc__,
9425"fchmodat(dirfd, path, mode, flags=0)\n\n\
9426Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9427flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9428If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9429is interpreted relative to the current working directory.");
9430
9431static PyObject *
9432posix_fchmodat(PyObject *self, PyObject *args)
9433{
9434 int dirfd, mode, res;
9435 int flags = 0;
9436 PyObject *opath;
9437 char *path;
9438
9439 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9440 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9441 return NULL;
9442
9443 path = PyBytes_AsString(opath);
9444
9445 Py_BEGIN_ALLOW_THREADS
9446 res = fchmodat(dirfd, path, mode, flags);
9447 Py_END_ALLOW_THREADS
9448 Py_DECREF(opath);
9449 if (res < 0)
9450 return posix_error();
9451 Py_RETURN_NONE;
9452}
9453#endif /* HAVE_FCHMODAT */
9454
9455#ifdef HAVE_FCHOWNAT
9456PyDoc_STRVAR(posix_fchownat__doc__,
9457"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9458Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9459flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9460If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9461is interpreted relative to the current working directory.");
9462
9463static PyObject *
9464posix_fchownat(PyObject *self, PyObject *args)
9465{
9466 PyObject *opath;
9467 int dirfd, res;
9468 long uid, gid;
9469 int flags = 0;
9470 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009471
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009472 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9473 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9474 return NULL;
9475
9476 path = PyBytes_AsString(opath);
9477
9478 Py_BEGIN_ALLOW_THREADS
9479 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9480 Py_END_ALLOW_THREADS
9481 Py_DECREF(opath);
9482 if (res < 0)
9483 return posix_error();
9484 Py_RETURN_NONE;
9485}
9486#endif /* HAVE_FCHOWNAT */
9487
9488#ifdef HAVE_FSTATAT
9489PyDoc_STRVAR(posix_fstatat__doc__,
9490"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9491Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9492flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9493If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9494is interpreted relative to the current working directory.");
9495
9496static PyObject *
9497posix_fstatat(PyObject *self, PyObject *args)
9498{
9499 PyObject *opath;
9500 char *path;
9501 STRUCT_STAT st;
9502 int dirfd, res, flags = 0;
9503
9504 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9505 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9506 return NULL;
9507 path = PyBytes_AsString(opath);
9508
9509 Py_BEGIN_ALLOW_THREADS
9510 res = fstatat(dirfd, path, &st, flags);
9511 Py_END_ALLOW_THREADS
9512 Py_DECREF(opath);
9513 if (res != 0)
9514 return posix_error();
9515
9516 return _pystat_fromstructstat(&st);
9517}
9518#endif
9519
9520#ifdef HAVE_FUTIMESAT
9521PyDoc_STRVAR(posix_futimesat__doc__,
9522"futimesat(dirfd, path, (atime, mtime))\n\
9523futimesat(dirfd, path, None)\n\n\
9524Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9525If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9526is interpreted relative to the current working directory.");
9527
9528static PyObject *
9529posix_futimesat(PyObject *self, PyObject *args)
9530{
9531 PyObject *opath;
9532 char *path;
9533 int res, dirfd;
9534 PyObject* arg;
9535
9536 struct timeval buf[2];
9537
9538 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9539 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9540 return NULL;
9541 path = PyBytes_AsString(opath);
9542 if (arg == Py_None) {
9543 /* optional time values not given */
9544 Py_BEGIN_ALLOW_THREADS
9545 res = futimesat(dirfd, path, NULL);
9546 Py_END_ALLOW_THREADS
9547 }
9548 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9549 PyErr_SetString(PyExc_TypeError,
9550 "futimesat() arg 3 must be a tuple (atime, mtime)");
9551 Py_DECREF(opath);
9552 return NULL;
9553 }
9554 else {
9555 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009556 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009557 Py_DECREF(opath);
9558 return NULL;
9559 }
9560 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009561 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009562 Py_DECREF(opath);
9563 return NULL;
9564 }
9565 Py_BEGIN_ALLOW_THREADS
9566 res = futimesat(dirfd, path, buf);
9567 Py_END_ALLOW_THREADS
9568 }
9569 Py_DECREF(opath);
9570 if (res < 0) {
9571 return posix_error();
9572 }
9573 Py_RETURN_NONE;
9574}
9575#endif
9576
9577#ifdef HAVE_LINKAT
9578PyDoc_STRVAR(posix_linkat__doc__,
9579"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9580Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9581and if dstpath is relative, it is taken as relative to dstfd.\n\
9582flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9583If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9584srcpath is interpreted relative to the current working directory. This\n\
9585also applies for dstpath.");
9586
9587static PyObject *
9588posix_linkat(PyObject *self, PyObject *args)
9589{
9590 PyObject *osrc, *odst;
9591 char *src, *dst;
9592 int res, srcfd, dstfd;
9593 int flags = 0;
9594
9595 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9596 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9597 return NULL;
9598 src = PyBytes_AsString(osrc);
9599 dst = PyBytes_AsString(odst);
9600 Py_BEGIN_ALLOW_THREADS
9601 res = linkat(srcfd, src, dstfd, dst, flags);
9602 Py_END_ALLOW_THREADS
9603 Py_DECREF(osrc);
9604 Py_DECREF(odst);
9605 if (res < 0)
9606 return posix_error();
9607 Py_RETURN_NONE;
9608}
9609#endif /* HAVE_LINKAT */
9610
9611#ifdef HAVE_MKDIRAT
9612PyDoc_STRVAR(posix_mkdirat__doc__,
9613"mkdirat(dirfd, path, mode=0o777)\n\n\
9614Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9615If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9616is interpreted relative to the current working directory.");
9617
9618static PyObject *
9619posix_mkdirat(PyObject *self, PyObject *args)
9620{
9621 int res, dirfd;
9622 PyObject *opath;
9623 char *path;
9624 int mode = 0777;
9625
9626 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9627 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9628 return NULL;
9629 path = PyBytes_AsString(opath);
9630 Py_BEGIN_ALLOW_THREADS
9631 res = mkdirat(dirfd, path, mode);
9632 Py_END_ALLOW_THREADS
9633 Py_DECREF(opath);
9634 if (res < 0)
9635 return posix_error();
9636 Py_RETURN_NONE;
9637}
9638#endif
9639
9640#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9641PyDoc_STRVAR(posix_mknodat__doc__,
9642"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9643Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9644If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9645is interpreted relative to the current working directory.");
9646
9647static PyObject *
9648posix_mknodat(PyObject *self, PyObject *args)
9649{
9650 PyObject *opath;
9651 char *filename;
9652 int mode = 0600;
9653 int device = 0;
9654 int res, dirfd;
9655 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9656 PyUnicode_FSConverter, &opath, &mode, &device))
9657 return NULL;
9658 filename = PyBytes_AS_STRING(opath);
9659 Py_BEGIN_ALLOW_THREADS
9660 res = mknodat(dirfd, filename, mode, device);
9661 Py_END_ALLOW_THREADS
9662 Py_DECREF(opath);
9663 if (res < 0)
9664 return posix_error();
9665 Py_RETURN_NONE;
9666}
9667#endif
9668
9669#ifdef HAVE_OPENAT
9670PyDoc_STRVAR(posix_openat__doc__,
9671"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9672Like open() but if path is relative, it is taken as relative to dirfd.\n\
9673If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9674is interpreted relative to the current working directory.");
9675
9676static PyObject *
9677posix_openat(PyObject *self, PyObject *args)
9678{
9679 PyObject *ofile;
9680 char *file;
9681 int flag, dirfd, fd;
9682 int mode = 0777;
9683
9684 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9685 &dirfd, PyUnicode_FSConverter, &ofile,
9686 &flag, &mode))
9687 return NULL;
9688 file = PyBytes_AsString(ofile);
9689 Py_BEGIN_ALLOW_THREADS
9690 fd = openat(dirfd, file, flag, mode);
9691 Py_END_ALLOW_THREADS
9692 Py_DECREF(ofile);
9693 if (fd < 0)
9694 return posix_error();
9695 return PyLong_FromLong((long)fd);
9696}
9697#endif
9698
9699#ifdef HAVE_READLINKAT
9700PyDoc_STRVAR(posix_readlinkat__doc__,
9701"readlinkat(dirfd, path) -> path\n\n\
9702Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9703If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9704is interpreted relative to the current working directory.");
9705
9706static PyObject *
9707posix_readlinkat(PyObject *self, PyObject *args)
9708{
9709 PyObject *v, *opath;
9710 char buf[MAXPATHLEN];
9711 char *path;
9712 int n, dirfd;
9713 int arg_is_unicode = 0;
9714
9715 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9716 &dirfd, PyUnicode_FSConverter, &opath))
9717 return NULL;
9718 path = PyBytes_AsString(opath);
9719 v = PySequence_GetItem(args, 1);
9720 if (v == NULL) {
9721 Py_DECREF(opath);
9722 return NULL;
9723 }
9724
9725 if (PyUnicode_Check(v)) {
9726 arg_is_unicode = 1;
9727 }
9728 Py_DECREF(v);
9729
9730 Py_BEGIN_ALLOW_THREADS
9731 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9732 Py_END_ALLOW_THREADS
9733 Py_DECREF(opath);
9734 if (n < 0)
9735 return posix_error();
9736
9737 if (arg_is_unicode)
9738 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9739 else
9740 return PyBytes_FromStringAndSize(buf, n);
9741}
9742#endif /* HAVE_READLINKAT */
9743
9744#ifdef HAVE_RENAMEAT
9745PyDoc_STRVAR(posix_renameat__doc__,
9746"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9747Like rename() but if oldpath is relative, it is taken as relative to\n\
9748olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9749If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9750oldpath is interpreted relative to the current working directory. This\n\
9751also applies for newpath.");
9752
9753static PyObject *
9754posix_renameat(PyObject *self, PyObject *args)
9755{
9756 int res;
9757 PyObject *opathold, *opathnew;
9758 char *opath, *npath;
9759 int oldfd, newfd;
9760
9761 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9762 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9763 return NULL;
9764 opath = PyBytes_AsString(opathold);
9765 npath = PyBytes_AsString(opathnew);
9766 Py_BEGIN_ALLOW_THREADS
9767 res = renameat(oldfd, opath, newfd, npath);
9768 Py_END_ALLOW_THREADS
9769 Py_DECREF(opathold);
9770 Py_DECREF(opathnew);
9771 if (res < 0)
9772 return posix_error();
9773 Py_RETURN_NONE;
9774}
9775#endif
9776
9777#if HAVE_SYMLINKAT
9778PyDoc_STRVAR(posix_symlinkat__doc__,
9779"symlinkat(src, dstfd, dst)\n\n\
9780Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9781If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9782is interpreted relative to the current working directory.");
9783
9784static PyObject *
9785posix_symlinkat(PyObject *self, PyObject *args)
9786{
9787 int res, dstfd;
9788 PyObject *osrc, *odst;
9789 char *src, *dst;
9790
9791 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9792 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9793 return NULL;
9794 src = PyBytes_AsString(osrc);
9795 dst = PyBytes_AsString(odst);
9796 Py_BEGIN_ALLOW_THREADS
9797 res = symlinkat(src, dstfd, dst);
9798 Py_END_ALLOW_THREADS
9799 Py_DECREF(osrc);
9800 Py_DECREF(odst);
9801 if (res < 0)
9802 return posix_error();
9803 Py_RETURN_NONE;
9804}
9805#endif /* HAVE_SYMLINKAT */
9806
9807#ifdef HAVE_UNLINKAT
9808PyDoc_STRVAR(posix_unlinkat__doc__,
9809"unlinkat(dirfd, path, flags=0)\n\n\
9810Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9811flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9812specified, unlinkat() behaves like rmdir().\n\
9813If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9814is interpreted relative to the current working directory.");
9815
9816static PyObject *
9817posix_unlinkat(PyObject *self, PyObject *args)
9818{
9819 int dirfd, res, flags = 0;
9820 PyObject *opath;
9821 char *path;
9822
9823 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9824 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9825 return NULL;
9826 path = PyBytes_AsString(opath);
9827 Py_BEGIN_ALLOW_THREADS
9828 res = unlinkat(dirfd, path, flags);
9829 Py_END_ALLOW_THREADS
9830 Py_DECREF(opath);
9831 if (res < 0)
9832 return posix_error();
9833 Py_RETURN_NONE;
9834}
9835#endif
9836
9837#ifdef HAVE_UTIMENSAT
9838PyDoc_STRVAR(posix_utimensat__doc__,
9839"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9840 (mtime_sec, mtime_nsec), flags)\n\
9841utimensat(dirfd, path, None, None, flags)\n\n\
9842Updates the timestamps of a file with nanosecond precision. If path is\n\
9843relative, it is taken as relative to dirfd.\n\
9844The second form sets atime and mtime to the current time.\n\
9845flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9846If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9847is interpreted relative to the current working directory.\n\
9848If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9849current time.\n\
9850If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9851
9852static PyObject *
9853posix_utimensat(PyObject *self, PyObject *args)
9854{
9855 PyObject *opath;
9856 char *path;
9857 int res, dirfd, flags = 0;
9858 PyObject *atime, *mtime;
9859
9860 struct timespec buf[2];
9861
9862 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9863 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9864 return NULL;
9865 path = PyBytes_AsString(opath);
9866 if (atime == Py_None && mtime == Py_None) {
9867 /* optional time values not given */
9868 Py_BEGIN_ALLOW_THREADS
9869 res = utimensat(dirfd, path, NULL, flags);
9870 Py_END_ALLOW_THREADS
9871 }
9872 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9873 PyErr_SetString(PyExc_TypeError,
9874 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9875 Py_DECREF(opath);
9876 return NULL;
9877 }
9878 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9879 PyErr_SetString(PyExc_TypeError,
9880 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9881 Py_DECREF(opath);
9882 return NULL;
9883 }
9884 else {
9885 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9886 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9887 Py_DECREF(opath);
9888 return NULL;
9889 }
9890 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9891 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9892 Py_DECREF(opath);
9893 return NULL;
9894 }
9895 Py_BEGIN_ALLOW_THREADS
9896 res = utimensat(dirfd, path, buf, flags);
9897 Py_END_ALLOW_THREADS
9898 }
9899 Py_DECREF(opath);
9900 if (res < 0) {
9901 return posix_error();
9902 }
9903 Py_RETURN_NONE;
9904}
9905#endif
9906
9907#ifdef HAVE_MKFIFOAT
9908PyDoc_STRVAR(posix_mkfifoat__doc__,
9909"mkfifoat(dirfd, path, mode=0o666)\n\n\
9910Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9911If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9912is interpreted relative to the current working directory.");
9913
9914static PyObject *
9915posix_mkfifoat(PyObject *self, PyObject *args)
9916{
9917 PyObject *opath;
9918 char *filename;
9919 int mode = 0666;
9920 int res, dirfd;
9921 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9922 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9923 return NULL;
9924 filename = PyBytes_AS_STRING(opath);
9925 Py_BEGIN_ALLOW_THREADS
9926 res = mkfifoat(dirfd, filename, mode);
9927 Py_END_ALLOW_THREADS
9928 Py_DECREF(opath);
9929 if (res < 0)
9930 return posix_error();
9931 Py_RETURN_NONE;
9932}
9933#endif
9934
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009935static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009936 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009937#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009938 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009939#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009940 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009941#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009942 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009943#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009944 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009945#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009946 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009947#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009948#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009949 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009950#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009951#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009952 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009953#endif /* HAVE_LCHMOD */
9954#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009955 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009956#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009957#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009959#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009960#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009961 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009962#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009963#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009965#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009966#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009967 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009968#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009969#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009970 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9971 METH_NOARGS, posix_getcwd__doc__},
9972 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9973 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009974#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009975#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009976 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009977#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009978 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009979#ifdef HAVE_FDOPENDIR
9980 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9981#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009982 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
9983 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009984#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00009985 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009986#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009987#ifdef HAVE_GETPRIORITY
9988 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
9989#endif /* HAVE_GETPRIORITY */
9990#ifdef HAVE_SETPRIORITY
9991 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
9992#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009993#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009995#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00009996#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009997 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00009998#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009999 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10000 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10001 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010002 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010003#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010004 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010005#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010006#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010007 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010008 win_symlink__doc__},
10009#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010010#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010011 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010012#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010013 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010014#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010015 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010016#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010017 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10018 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10019 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010020#ifdef HAVE_FUTIMES
10021 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10022#endif
10023#ifdef HAVE_LUTIMES
10024 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10025#endif
10026#ifdef HAVE_FUTIMENS
10027 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10028#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010029#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010030 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010031#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010032 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010033#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010034 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10035 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010036#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010037#ifdef HAVE_FEXECVE
10038 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10039#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010040#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010041 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10042 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010043#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010044 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10045 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010046#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010047#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010048#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010049 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010050#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010051#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010052 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010053#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010054#ifdef HAVE_SCHED_H
10055 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10056 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
10057 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
10058 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
10059 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
10060 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
10061 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
10062 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010063#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010064 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10065 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10066#endif
Benjamin Peterson2740af82011-08-02 17:41:34 -050010067#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010068#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010070#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010071#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010073#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010074#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010075 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010076#endif /* HAVE_GETEGID */
10077#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010078 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010079#endif /* HAVE_GETEUID */
10080#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010081 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010082#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010083#ifdef HAVE_GETGROUPLIST
10084 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10085#endif
Fred Drakec9680921999-12-13 16:37:25 +000010086#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010087 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010088#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010089 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010090#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010091 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010092#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010093#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010094 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010095#endif /* HAVE_GETPPID */
10096#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010097 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010098#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010099#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010100 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010101#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010102#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010103 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010104#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010105#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010106 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010107#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010108#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010110#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010111#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010112 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10113 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010114 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010115#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010116#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010117 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010118#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010119#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010121#endif /* HAVE_SETEUID */
10122#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010124#endif /* HAVE_SETEGID */
10125#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010127#endif /* HAVE_SETREUID */
10128#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010129 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010130#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010131#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010132 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010133#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010134#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010135 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010136#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010137#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010138 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010139#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010140#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010141 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010142#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010143#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010144 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010145#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010146#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010148#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010149#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010150 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010151#endif /* HAVE_WAIT3 */
10152#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010153 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010154#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010155#if defined(HAVE_WAITID) && !defined(__APPLE__)
10156 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10157#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010158#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010159 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010160#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010161#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010162 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010163#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010164#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010165 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010166#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010167#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010168 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010169#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010170#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010171 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010172#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010173#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010174 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010175#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010176 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10177 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10178 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10179 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10180 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10181 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010182#ifdef HAVE_LOCKF
10183 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10184#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010185 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10186 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010187#ifdef HAVE_READV
10188 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10189#endif
10190#ifdef HAVE_PREAD
10191 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10192#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010193 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010194#ifdef HAVE_WRITEV
10195 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10196#endif
10197#ifdef HAVE_PWRITE
10198 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10199#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010200#ifdef HAVE_SENDFILE
10201 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10202 posix_sendfile__doc__},
10203#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010204 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10205 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010206#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010207 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010208#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010209#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010210 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010211#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010212#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010213 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010214#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010215#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010216 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010217#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010218#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010219 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10220 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10221 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010222#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010223#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010224 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010225#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010226#ifdef HAVE_TRUNCATE
10227 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10228#endif
10229#ifdef HAVE_POSIX_FALLOCATE
10230 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10231#endif
10232#ifdef HAVE_POSIX_FADVISE
10233 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10234#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010235#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010236 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010237#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010238#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010239 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010240#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010241 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010242#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010243 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010244#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010245#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010246 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010247#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010248#ifdef HAVE_SYNC
10249 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10250#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010251#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010252 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010253#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010254#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010255#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010256 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010257#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010258#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010259 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010260#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010261#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010262 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010263#endif /* WIFSTOPPED */
10264#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010265 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010266#endif /* WIFSIGNALED */
10267#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010268 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010269#endif /* WIFEXITED */
10270#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010271 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010272#endif /* WEXITSTATUS */
10273#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010274 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010275#endif /* WTERMSIG */
10276#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010277 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010278#endif /* WSTOPSIG */
10279#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010280#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010281 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010282#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010283#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010284 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010285#endif
Fred Drakec9680921999-12-13 16:37:25 +000010286#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010287 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010288#endif
10289#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010290 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010291#endif
10292#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010293 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010294#endif
10295#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010296 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010297#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010298 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010299#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010300 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010301 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010302 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010303 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010304 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010305#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010306#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010307 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010308#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010309 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010310 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010311 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010312 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010313 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010314 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010315#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010316 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010317#endif
10318#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010319 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010320#endif
10321#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010322 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010323#endif
10324#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010325 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010326#endif
10327
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010328/* posix *at family of functions */
10329#ifdef HAVE_FACCESSAT
10330 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10331#endif
10332#ifdef HAVE_FCHMODAT
10333 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10334#endif /* HAVE_FCHMODAT */
10335#ifdef HAVE_FCHOWNAT
10336 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10337#endif /* HAVE_FCHOWNAT */
10338#ifdef HAVE_FSTATAT
10339 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10340#endif
10341#ifdef HAVE_FUTIMESAT
10342 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10343#endif
10344#ifdef HAVE_LINKAT
10345 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10346#endif /* HAVE_LINKAT */
10347#ifdef HAVE_MKDIRAT
10348 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10349#endif
10350#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10351 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10352#endif
10353#ifdef HAVE_OPENAT
10354 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10355#endif
10356#ifdef HAVE_READLINKAT
10357 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10358#endif /* HAVE_READLINKAT */
10359#ifdef HAVE_RENAMEAT
10360 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10361#endif
10362#if HAVE_SYMLINKAT
10363 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10364#endif /* HAVE_SYMLINKAT */
10365#ifdef HAVE_UNLINKAT
10366 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10367#endif
10368#ifdef HAVE_UTIMENSAT
10369 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10370#endif
10371#ifdef HAVE_MKFIFOAT
10372 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10373#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010374 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010375};
10376
10377
Barry Warsaw4a342091996-12-19 23:50:02 +000010378static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010379ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010380{
Victor Stinner8c62be82010-05-06 00:08:46 +000010381 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010382}
10383
Guido van Rossumd48f2521997-12-05 22:19:34 +000010384#if defined(PYOS_OS2)
10385/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010386static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010387{
10388 APIRET rc;
10389 ULONG values[QSV_MAX+1];
10390 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010391 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010392
10393 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010394 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010395 Py_END_ALLOW_THREADS
10396
10397 if (rc != NO_ERROR) {
10398 os2_error(rc);
10399 return -1;
10400 }
10401
Fred Drake4d1e64b2002-04-15 19:40:07 +000010402 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10403 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10404 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10405 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10406 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10407 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10408 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010409
10410 switch (values[QSV_VERSION_MINOR]) {
10411 case 0: ver = "2.00"; break;
10412 case 10: ver = "2.10"; break;
10413 case 11: ver = "2.11"; break;
10414 case 30: ver = "3.00"; break;
10415 case 40: ver = "4.00"; break;
10416 case 50: ver = "5.00"; break;
10417 default:
Tim Peters885d4572001-11-28 20:27:42 +000010418 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010419 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010420 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010421 ver = &tmp[0];
10422 }
10423
10424 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010425 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010426 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010427
10428 /* Add Indicator of Which Drive was Used to Boot the System */
10429 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10430 tmp[1] = ':';
10431 tmp[2] = '\0';
10432
Fred Drake4d1e64b2002-04-15 19:40:07 +000010433 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010434}
10435#endif
10436
Brian Curtin52173d42010-12-02 18:29:18 +000010437#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010438static int
Brian Curtin52173d42010-12-02 18:29:18 +000010439enable_symlink()
10440{
10441 HANDLE tok;
10442 TOKEN_PRIVILEGES tok_priv;
10443 LUID luid;
10444 int meth_idx = 0;
10445
10446 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010447 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010448
10449 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010450 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010451
10452 tok_priv.PrivilegeCount = 1;
10453 tok_priv.Privileges[0].Luid = luid;
10454 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10455
10456 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10457 sizeof(TOKEN_PRIVILEGES),
10458 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010459 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010460
Brian Curtin3b4499c2010-12-28 14:31:47 +000010461 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10462 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010463}
10464#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10465
Barry Warsaw4a342091996-12-19 23:50:02 +000010466static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010467all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010468{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010469#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010471#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010472#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010473 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010474#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010475#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010476 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010477#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010478#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010479 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010480#endif
Fred Drakec9680921999-12-13 16:37:25 +000010481#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010482 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010483#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010484#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010485 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010486#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010487#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010489#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010490#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010492#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010493#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010495#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010496#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010498#endif
10499#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010501#endif
10502#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010504#endif
10505#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010507#endif
10508#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010510#endif
10511#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010513#endif
10514#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010516#endif
10517#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010519#endif
10520#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010522#endif
10523#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010525#endif
10526#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010528#endif
10529#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010531#endif
10532#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010534#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000010535#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010537#endif
10538#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000010539 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010540#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010541#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010543#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000010544#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010546#endif
10547#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010549#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010550#ifdef PRIO_PROCESS
10551 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
10552#endif
10553#ifdef PRIO_PGRP
10554 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
10555#endif
10556#ifdef PRIO_USER
10557 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
10558#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010559#ifdef O_CLOEXEC
10560 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
10561#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010562/* posix - constants for *at functions */
10563#ifdef AT_SYMLINK_NOFOLLOW
10564 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
10565#endif
10566#ifdef AT_EACCESS
10567 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
10568#endif
10569#ifdef AT_FDCWD
10570 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
10571#endif
10572#ifdef AT_REMOVEDIR
10573 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
10574#endif
10575#ifdef AT_SYMLINK_FOLLOW
10576 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
10577#endif
10578#ifdef UTIME_NOW
10579 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10580#endif
10581#ifdef UTIME_OMIT
10582 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10583#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010584
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010585
Tim Peters5aa91602002-01-30 05:46:57 +000010586/* MS Windows */
10587#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010588 /* Don't inherit in child processes. */
10589 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010590#endif
10591#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000010592 /* Optimize for short life (keep in memory). */
10593 /* MS forgot to define this one with a non-underscore form too. */
10594 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010595#endif
10596#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010597 /* Automatically delete when last handle is closed. */
10598 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010599#endif
10600#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000010601 /* Optimize for random access. */
10602 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010603#endif
10604#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 /* Optimize for sequential access. */
10606 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010607#endif
10608
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010609/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010610#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010611 /* Send a SIGIO signal whenever input or output
10612 becomes available on file descriptor */
10613 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010614#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010615#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010616 /* Direct disk access. */
10617 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010618#endif
10619#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 /* Must be a directory. */
10621 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010622#endif
10623#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000010624 /* Do not follow links. */
10625 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010626#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010627#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010628 /* Do not update the access time. */
10629 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010630#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000010631
Victor Stinner8c62be82010-05-06 00:08:46 +000010632 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010633#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010634 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010635#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010636#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010637 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010638#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010639#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010640 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010641#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010642#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010643 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010644#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010645#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000010646 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010647#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010648#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000010649 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010650#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010651#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010653#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010654#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000010655 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010656#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010657#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010658 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010659#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010660#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010662#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010663#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010665#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010666#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010667 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010668#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010669#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010671#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010672#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000010673 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010674#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010675#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010676 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010677#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010678#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010679 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010680#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010681#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000010682 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010683#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010684
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010685 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010686#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010687 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010688#endif /* ST_RDONLY */
10689#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010690 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010691#endif /* ST_NOSUID */
10692
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010693 /* FreeBSD sendfile() constants */
10694#ifdef SF_NODISKIO
10695 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
10696#endif
10697#ifdef SF_MNOWAIT
10698 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
10699#endif
10700#ifdef SF_SYNC
10701 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
10702#endif
10703
Ross Lagerwall7807c352011-03-17 20:20:30 +020010704 /* constants for posix_fadvise */
10705#ifdef POSIX_FADV_NORMAL
10706 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
10707#endif
10708#ifdef POSIX_FADV_SEQUENTIAL
10709 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
10710#endif
10711#ifdef POSIX_FADV_RANDOM
10712 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
10713#endif
10714#ifdef POSIX_FADV_NOREUSE
10715 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
10716#endif
10717#ifdef POSIX_FADV_WILLNEED
10718 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
10719#endif
10720#ifdef POSIX_FADV_DONTNEED
10721 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
10722#endif
10723
10724 /* constants for waitid */
10725#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
10726 if (ins(d, "P_PID", (long)P_PID)) return -1;
10727 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
10728 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
10729#endif
10730#ifdef WEXITED
10731 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
10732#endif
10733#ifdef WNOWAIT
10734 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
10735#endif
10736#ifdef WSTOPPED
10737 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
10738#endif
10739#ifdef CLD_EXITED
10740 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
10741#endif
10742#ifdef CLD_DUMPED
10743 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
10744#endif
10745#ifdef CLD_TRAPPED
10746 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
10747#endif
10748#ifdef CLD_CONTINUED
10749 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
10750#endif
10751
10752 /* constants for lockf */
10753#ifdef F_LOCK
10754 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
10755#endif
10756#ifdef F_TLOCK
10757 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
10758#endif
10759#ifdef F_ULOCK
10760 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
10761#endif
10762#ifdef F_TEST
10763 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10764#endif
10765
10766 /* constants for futimens */
10767#ifdef UTIME_NOW
10768 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10769#endif
10770#ifdef UTIME_OMIT
10771 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10772#endif
10773
Guido van Rossum246bc171999-02-01 23:54:31 +000010774#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010775#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10777 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10778 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10779 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10780 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10781 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10782 if (ins(d, "P_PM", (long)P_PM)) return -1;
10783 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10784 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10785 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10786 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10787 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10788 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10789 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10790 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10791 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10792 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10793 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10794 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10795 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010796#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10798 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10799 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10800 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10801 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010802#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010803#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010804
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010805#ifdef HAVE_SCHED_H
10806 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
10807 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
10808#ifdef SCHED_SPORADIC
10809 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
10810#endif
10811 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
10812#ifdef SCHED_BATCH
10813 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
10814#endif
10815#ifdef SCHED_IDLE
10816 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
10817#endif
10818#ifdef SCHED_RESET_ON_FORK
10819 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
10820#endif
10821#endif
10822
Guido van Rossumd48f2521997-12-05 22:19:34 +000010823#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010825#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010826 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010827}
10828
10829
Tim Peters5aa91602002-01-30 05:46:57 +000010830#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010831#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010832#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010833
10834#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010835#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010836#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010837
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010838#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010839#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010840#define MODNAME "posix"
10841#endif
10842
Martin v. Löwis1a214512008-06-11 05:26:20 +000010843static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010844 PyModuleDef_HEAD_INIT,
10845 MODNAME,
10846 posix__doc__,
10847 -1,
10848 posix_methods,
10849 NULL,
10850 NULL,
10851 NULL,
10852 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010853};
10854
10855
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010856PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010857INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010858{
Victor Stinner8c62be82010-05-06 00:08:46 +000010859 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010860
Brian Curtin52173d42010-12-02 18:29:18 +000010861#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010862 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010863#endif
10864
Victor Stinner8c62be82010-05-06 00:08:46 +000010865 m = PyModule_Create(&posixmodule);
10866 if (m == NULL)
10867 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010868
Victor Stinner8c62be82010-05-06 00:08:46 +000010869 /* Initialize environ dictionary */
10870 v = convertenviron();
10871 Py_XINCREF(v);
10872 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10873 return NULL;
10874 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010875
Victor Stinner8c62be82010-05-06 00:08:46 +000010876 if (all_ins(m))
10877 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010878
Victor Stinner8c62be82010-05-06 00:08:46 +000010879 if (setup_confname_tables(m))
10880 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010881
Victor Stinner8c62be82010-05-06 00:08:46 +000010882 Py_INCREF(PyExc_OSError);
10883 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010884
Benjamin Peterson2740af82011-08-02 17:41:34 -050010885#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010886 if (PyType_Ready(&cpu_set_type) < 0)
10887 return NULL;
10888 Py_INCREF(&cpu_set_type);
10889 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050010890#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010891
Guido van Rossumb3d39562000-01-31 18:41:26 +000010892#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 if (posix_putenv_garbage == NULL)
10894 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010895#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010896
Victor Stinner8c62be82010-05-06 00:08:46 +000010897 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010898#if defined(HAVE_WAITID) && !defined(__APPLE__)
10899 waitid_result_desc.name = MODNAME ".waitid_result";
10900 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10901#endif
10902
Victor Stinner8c62be82010-05-06 00:08:46 +000010903 stat_result_desc.name = MODNAME ".stat_result";
10904 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10905 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10906 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10907 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10908 structseq_new = StatResultType.tp_new;
10909 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010910
Victor Stinner8c62be82010-05-06 00:08:46 +000010911 statvfs_result_desc.name = MODNAME ".statvfs_result";
10912 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010913#ifdef NEED_TICKS_PER_SECOND
10914# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010915 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010916# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010917 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010918# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010919 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010920# endif
10921#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010922
10923#ifdef HAVE_SCHED_H
10924 sched_param_desc.name = MODNAME ".sched_param";
10925 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
10926 SchedParamType.tp_new = sched_param_new;
10927#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010928 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010929#if defined(HAVE_WAITID) && !defined(__APPLE__)
10930 Py_INCREF((PyObject*) &WaitidResultType);
10931 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10932#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010933 Py_INCREF((PyObject*) &StatResultType);
10934 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10935 Py_INCREF((PyObject*) &StatVFSResultType);
10936 PyModule_AddObject(m, "statvfs_result",
10937 (PyObject*) &StatVFSResultType);
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010938 Py_INCREF(&SchedParamType);
10939 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010940 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010941
10942#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 /*
10944 * Step 2 of weak-linking support on Mac OS X.
10945 *
10946 * The code below removes functions that are not available on the
10947 * currently active platform.
10948 *
10949 * This block allow one to use a python binary that was build on
10950 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10951 * OSX 10.4.
10952 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010953#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010954 if (fstatvfs == NULL) {
10955 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10956 return NULL;
10957 }
10958 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010959#endif /* HAVE_FSTATVFS */
10960
10961#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010962 if (statvfs == NULL) {
10963 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10964 return NULL;
10965 }
10966 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010967#endif /* HAVE_STATVFS */
10968
10969# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 if (lchown == NULL) {
10971 if (PyObject_DelAttrString(m, "lchown") == -1) {
10972 return NULL;
10973 }
10974 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010975#endif /* HAVE_LCHOWN */
10976
10977
10978#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010980
Guido van Rossumb6775db1994-08-01 11:34:53 +000010981}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010982
10983#ifdef __cplusplus
10984}
10985#endif