blob: 2fc509d315f406bbfe66bcf9e50d53cc3df7ff29 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020031# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#endif /* defined(__VMS) */
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000040"This module provides access to operating system functionality that is\n\
41standardized by the C Standard and the POSIX standard (a thinly\n\
42disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000045
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020047#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Ross Lagerwall4d076da2011-03-18 06:56:53 +020062#ifdef HAVE_SYS_UIO_H
63#include <sys/uio.h>
64#endif
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_TYPES_H */
69
70#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000073
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000075#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000079#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000081
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_FCNTL_H
83#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000084#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000085
Guido van Rossuma6535fd2001-10-18 19:44:10 +000086#ifdef HAVE_GRP_H
87#include <grp.h>
88#endif
89
Barry Warsaw5676bd12003-01-07 20:57:09 +000090#ifdef HAVE_SYSEXITS_H
91#include <sysexits.h>
92#endif /* HAVE_SYSEXITS_H */
93
Anthony Baxter8a560de2004-10-13 15:30:56 +000094#ifdef HAVE_SYS_LOADAVG_H
95#include <sys/loadavg.h>
96#endif
97
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000098#ifdef HAVE_LANGINFO_H
99#include <langinfo.h>
100#endif
101
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000102#ifdef HAVE_SYS_SENDFILE_H
103#include <sys/sendfile.h>
104#endif
105
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500106#ifdef HAVE_SCHED_H
107#include <sched.h>
108#endif
109
Benjamin Peterson9428d532011-09-14 11:45:52 -0400110#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
111#define USE_XATTRS
112#endif
113
114#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400115#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400116#endif
117
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
119#ifdef HAVE_SYS_SOCKET_H
120#include <sys/socket.h>
121#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#endif
123
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000125/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000126#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000127#include <process.h>
128#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000129#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#define HAVE_GETCWD 1
131#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000132#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#if defined(__OS2__)
134#define HAVE_EXECV 1
135#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#include <process.h>
138#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140#define HAVE_EXECV 1
141#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_OPENDIR 1
143#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000144#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000145#define HAVE_WAIT 1
146#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000147#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000148#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000149#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000150#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_EXECV 1
153#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000154#define HAVE_SYSTEM 1
155#define HAVE_CWAIT 1
156#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000157#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000158#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000159#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
160/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000161#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162/* Unix functions that the configure script doesn't check for */
163#define HAVE_EXECV 1
164#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000165#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000166#define HAVE_FORK1 1
167#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000168#define HAVE_GETCWD 1
169#define HAVE_GETEGID 1
170#define HAVE_GETEUID 1
171#define HAVE_GETGID 1
172#define HAVE_GETPPID 1
173#define HAVE_GETUID 1
174#define HAVE_KILL 1
175#define HAVE_OPENDIR 1
176#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000177#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000179#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000180#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#endif /* _MSC_VER */
182#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000183#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000185
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000186#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000187
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000188#if defined(__sgi)&&_COMPILER_VERSION>=700
189/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
190 (default) */
191extern char *ctermid_r(char *);
192#endif
193
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000194#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000195#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000197#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000198#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000199extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000200#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000201extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000202#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000203#endif
204#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000205extern int chdir(char *);
206extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000207#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000208extern int chdir(const char *);
209extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000210#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000211#ifdef __BORLANDC__
212extern int chmod(const char *, int);
213#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000214extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000215#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000216/*#ifdef HAVE_FCHMOD
217extern int fchmod(int, mode_t);
218#endif*/
219/*#ifdef HAVE_LCHMOD
220extern int lchmod(const char *, mode_t);
221#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000222extern int chown(const char *, uid_t, gid_t);
223extern char *getcwd(char *, int);
224extern char *strerror(int);
225extern int link(const char *, const char *);
226extern int rename(const char *, const char *);
227extern int stat(const char *, struct stat *);
228extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000230extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000231#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000233extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000234#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000236
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000237#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#ifdef HAVE_UTIME_H
240#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000241#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000243#ifdef HAVE_SYS_UTIME_H
244#include <sys/utime.h>
245#define HAVE_UTIME_H /* pretend we do for the rest of this file */
246#endif /* HAVE_SYS_UTIME_H */
247
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#ifdef HAVE_SYS_TIMES_H
249#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000250#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
252#ifdef HAVE_SYS_PARAM_H
253#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
256#ifdef HAVE_SYS_UTSNAME_H
257#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000260#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000262#define NAMLEN(dirent) strlen((dirent)->d_name)
263#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000264#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000265#include <direct.h>
266#define NAMLEN(dirent) strlen((dirent)->d_name)
267#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000269#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000270#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000271#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#endif
274#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000276#endif
277#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000279#endif
280#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000282#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285#endif
286#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288#endif
289#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000292#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000293#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000294#endif
295#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000296#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000297#endif
298#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000299#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000300#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000301#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000302#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000304#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000305#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000306#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
307#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000308static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000309#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000310#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000311
Guido van Rossumd48f2521997-12-05 22:19:34 +0000312#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000313#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000314#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317#if defined(PATH_MAX) && PATH_MAX > 1024
318#define MAXPATHLEN PATH_MAX
319#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000322#endif /* MAXPATHLEN */
323
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000324#ifdef UNION_WAIT
325/* Emulate some macros on systems that have a union instead of macros */
326
327#ifndef WIFEXITED
328#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
329#endif
330
331#ifndef WEXITSTATUS
332#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
333#endif
334
335#ifndef WTERMSIG
336#define WTERMSIG(u_wait) ((u_wait).w_termsig)
337#endif
338
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339#define WAIT_TYPE union wait
340#define WAIT_STATUS_INT(s) (s.w_status)
341
342#else /* !UNION_WAIT */
343#define WAIT_TYPE int
344#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000345#endif /* UNION_WAIT */
346
Greg Wardb48bc172000-03-01 21:51:56 +0000347/* Don't use the "_r" form if we don't need it (also, won't have a
348 prototype for it, at least on Solaris -- maybe others as well?). */
349#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
350#define USE_CTERMID_R
351#endif
352
Fred Drake699f3522000-06-29 21:12:41 +0000353/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000354#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000355#undef FSTAT
356#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000357#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000358# define STAT win32_stat
359# define FSTAT win32_fstat
360# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000361#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000362# define STAT stat
363# define FSTAT fstat
364# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000365#endif
366
Tim Peters11b23062003-04-23 02:39:17 +0000367#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000368#include <sys/mkdev.h>
369#else
370#if defined(MAJOR_IN_SYSMACROS)
371#include <sys/sysmacros.h>
372#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000373#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
374#include <sys/mkdev.h>
375#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000376#endif
Fred Drake699f3522000-06-29 21:12:41 +0000377
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200378/* A helper used by a number of POSIX-only functions */
379#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000380static int
381_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000382{
383#if !defined(HAVE_LARGEFILE_SUPPORT)
384 *((off_t*)addr) = PyLong_AsLong(arg);
385#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000386 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000387#endif
388 if (PyErr_Occurred())
389 return 0;
390 return 1;
391}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200392#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000393
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000394#if defined _MSC_VER && _MSC_VER >= 1400
395/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
396 * valid and throw an assertion if it isn't.
397 * Normally, an invalid fd is likely to be a C program error and therefore
398 * an assertion can be useful, but it does contradict the POSIX standard
399 * which for write(2) states:
400 * "Otherwise, -1 shall be returned and errno set to indicate the error."
401 * "[EBADF] The fildes argument is not a valid file descriptor open for
402 * writing."
403 * Furthermore, python allows the user to enter any old integer
404 * as a fd and should merely raise a python exception on error.
405 * The Microsoft CRT doesn't provide an official way to check for the
406 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000407 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408 * internal structures involved.
409 * The structures below must be updated for each version of visual studio
410 * according to the file internal.h in the CRT source, until MS comes
411 * up with a less hacky way to do this.
412 * (all of this is to avoid globally modifying the CRT behaviour using
413 * _set_invalid_parameter_handler() and _CrtSetReportMode())
414 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000415/* The actual size of the structure is determined at runtime.
416 * Only the first items must be present.
417 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000418typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000419 intptr_t osfhnd;
420 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000421} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000423extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000424#define IOINFO_L2E 5
425#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
426#define IOINFO_ARRAYS 64
427#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
428#define FOPEN 0x01
429#define _NO_CONSOLE_FILENO (intptr_t)-2
430
431/* This function emulates what the windows CRT does to validate file handles */
432int
433_PyVerify_fd(int fd)
434{
Victor Stinner8c62be82010-05-06 00:08:46 +0000435 const int i1 = fd >> IOINFO_L2E;
436 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000437
Antoine Pitrou22e41552010-08-15 18:07:50 +0000438 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000439
Victor Stinner8c62be82010-05-06 00:08:46 +0000440 /* Determine the actual size of the ioinfo structure,
441 * as used by the CRT loaded in memory
442 */
443 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
444 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
445 }
446 if (sizeof_ioinfo == 0) {
447 /* This should not happen... */
448 goto fail;
449 }
450
451 /* See that it isn't a special CLEAR fileno */
452 if (fd != _NO_CONSOLE_FILENO) {
453 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
454 * we check pointer validity and other info
455 */
456 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
457 /* finally, check that the file is open */
458 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
459 if (info->osfile & FOPEN) {
460 return 1;
461 }
462 }
463 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000464 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000465 errno = EBADF;
466 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000467}
468
469/* the special case of checking dup2. The target fd must be in a sensible range */
470static int
471_PyVerify_fd_dup2(int fd1, int fd2)
472{
Victor Stinner8c62be82010-05-06 00:08:46 +0000473 if (!_PyVerify_fd(fd1))
474 return 0;
475 if (fd2 == _NO_CONSOLE_FILENO)
476 return 0;
477 if ((unsigned)fd2 < _NHANDLE_)
478 return 1;
479 else
480 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000481}
482#else
483/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
484#define _PyVerify_fd_dup2(A, B) (1)
485#endif
486
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000487#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000488/* The following structure was copied from
489 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
490 include doesn't seem to be present in the Windows SDK (at least as included
491 with Visual Studio Express). */
492typedef struct _REPARSE_DATA_BUFFER {
493 ULONG ReparseTag;
494 USHORT ReparseDataLength;
495 USHORT Reserved;
496 union {
497 struct {
498 USHORT SubstituteNameOffset;
499 USHORT SubstituteNameLength;
500 USHORT PrintNameOffset;
501 USHORT PrintNameLength;
502 ULONG Flags;
503 WCHAR PathBuffer[1];
504 } SymbolicLinkReparseBuffer;
505
506 struct {
507 USHORT SubstituteNameOffset;
508 USHORT SubstituteNameLength;
509 USHORT PrintNameOffset;
510 USHORT PrintNameLength;
511 WCHAR PathBuffer[1];
512 } MountPointReparseBuffer;
513
514 struct {
515 UCHAR DataBuffer[1];
516 } GenericReparseBuffer;
517 };
518} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
519
520#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
521 GenericReparseBuffer)
522#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
523
524static int
Brian Curtind25aef52011-06-13 15:16:04 -0500525win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000526{
527 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
528 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
529 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000530
531 if (0 == DeviceIoControl(
532 reparse_point_handle,
533 FSCTL_GET_REPARSE_POINT,
534 NULL, 0, /* in buffer */
535 target_buffer, sizeof(target_buffer),
536 &n_bytes_returned,
537 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500538 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000539
540 if (reparse_tag)
541 *reparse_tag = rdb->ReparseTag;
542
Brian Curtind25aef52011-06-13 15:16:04 -0500543 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000544}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000545#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000546
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000548#ifdef WITH_NEXT_FRAMEWORK
549/* On Darwin/MacOSX a shared library or framework has no access to
550** environ directly, we must obtain it with _NSGetEnviron().
551*/
552#include <crt_externs.h>
553static char **environ;
554#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000556#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557
Barry Warsaw53699e91996-12-10 23:23:01 +0000558static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000559convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560{
Victor Stinner8c62be82010-05-06 00:08:46 +0000561 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000562#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000563 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000564#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000565 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000566#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000567#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000568 APIRET rc;
569 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
570#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000571
Victor Stinner8c62be82010-05-06 00:08:46 +0000572 d = PyDict_New();
573 if (d == NULL)
574 return NULL;
575#ifdef WITH_NEXT_FRAMEWORK
576 if (environ == NULL)
577 environ = *_NSGetEnviron();
578#endif
579#ifdef MS_WINDOWS
580 /* _wenviron must be initialized in this way if the program is started
581 through main() instead of wmain(). */
582 _wgetenv(L"");
583 if (_wenviron == NULL)
584 return d;
585 /* This part ignores errors */
586 for (e = _wenviron; *e != NULL; e++) {
587 PyObject *k;
588 PyObject *v;
589 wchar_t *p = wcschr(*e, L'=');
590 if (p == NULL)
591 continue;
592 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
593 if (k == NULL) {
594 PyErr_Clear();
595 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000596 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000597 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
598 if (v == NULL) {
599 PyErr_Clear();
600 Py_DECREF(k);
601 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000602 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000603 if (PyDict_GetItem(d, k) == NULL) {
604 if (PyDict_SetItem(d, k, v) != 0)
605 PyErr_Clear();
606 }
607 Py_DECREF(k);
608 Py_DECREF(v);
609 }
610#else
611 if (environ == NULL)
612 return d;
613 /* This part ignores errors */
614 for (e = environ; *e != NULL; e++) {
615 PyObject *k;
616 PyObject *v;
617 char *p = strchr(*e, '=');
618 if (p == NULL)
619 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000620 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000621 if (k == NULL) {
622 PyErr_Clear();
623 continue;
624 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000625 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000626 if (v == NULL) {
627 PyErr_Clear();
628 Py_DECREF(k);
629 continue;
630 }
631 if (PyDict_GetItem(d, k) == NULL) {
632 if (PyDict_SetItem(d, k, v) != 0)
633 PyErr_Clear();
634 }
635 Py_DECREF(k);
636 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000637 }
638#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000639#if defined(PYOS_OS2)
640 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
641 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
642 PyObject *v = PyBytes_FromString(buffer);
643 PyDict_SetItemString(d, "BEGINLIBPATH", v);
644 Py_DECREF(v);
645 }
646 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
647 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
648 PyObject *v = PyBytes_FromString(buffer);
649 PyDict_SetItemString(d, "ENDLIBPATH", v);
650 Py_DECREF(v);
651 }
652#endif
653 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654}
655
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656/* Set a POSIX-specific error from errno, and return NULL */
657
Barry Warsawd58d7641998-07-23 16:14:40 +0000658static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000659posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000660{
Victor Stinner8c62be82010-05-06 00:08:46 +0000661 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662}
Barry Warsawd58d7641998-07-23 16:14:40 +0000663static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000664posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000665{
Victor Stinner8c62be82010-05-06 00:08:46 +0000666 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000667}
668
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000669
Mark Hammondef8b6542001-05-13 08:04:26 +0000670static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000671posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000672{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000673 PyObject *name_str, *rc;
674 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
675 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000676 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000677 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
678 name_str);
679 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000680 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000681}
682
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000683#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000684static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000685win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000686{
Victor Stinner8c62be82010-05-06 00:08:46 +0000687 /* XXX We should pass the function name along in the future.
688 (winreg.c also wants to pass the function name.)
689 This would however require an additional param to the
690 Windows error object, which is non-trivial.
691 */
692 errno = GetLastError();
693 if (filename)
694 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
695 else
696 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000697}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000698
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000699static PyObject *
700win32_error_unicode(char* function, Py_UNICODE* filename)
701{
Victor Stinner8c62be82010-05-06 00:08:46 +0000702 /* XXX - see win32_error for comments on 'function' */
703 errno = GetLastError();
704 if (filename)
705 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
706 else
707 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000708}
709
Thomas Wouters477c8d52006-05-27 19:21:47 +0000710static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000711convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712{
Victor Stinner8c62be82010-05-06 00:08:46 +0000713 if (PyUnicode_CheckExact(*param))
714 Py_INCREF(*param);
715 else if (PyUnicode_Check(*param))
716 /* For a Unicode subtype that's not a Unicode object,
717 return a true Unicode object with the same data. */
718 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
719 PyUnicode_GET_SIZE(*param));
720 else
721 *param = PyUnicode_FromEncodedObject(*param,
722 Py_FileSystemDefaultEncoding,
723 "strict");
724 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000725}
726
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000727#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000728
Guido van Rossumd48f2521997-12-05 22:19:34 +0000729#if defined(PYOS_OS2)
730/**********************************************************************
731 * Helper Function to Trim and Format OS/2 Messages
732 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000733static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000734os2_formatmsg(char *msgbuf, int msglen, char *reason)
735{
736 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
737
738 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
739 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
740
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000741 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000742 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
743 }
744
745 /* Add Optional Reason Text */
746 if (reason) {
747 strcat(msgbuf, " : ");
748 strcat(msgbuf, reason);
749 }
750}
751
752/**********************************************************************
753 * Decode an OS/2 Operating System Error Code
754 *
755 * A convenience function to lookup an OS/2 error code and return a
756 * text message we can use to raise a Python exception.
757 *
758 * Notes:
759 * The messages for errors returned from the OS/2 kernel reside in
760 * the file OSO001.MSG in the \OS2 directory hierarchy.
761 *
762 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000763static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000764os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
765{
766 APIRET rc;
767 ULONG msglen;
768
769 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
770 Py_BEGIN_ALLOW_THREADS
771 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
772 errorcode, "oso001.msg", &msglen);
773 Py_END_ALLOW_THREADS
774
775 if (rc == NO_ERROR)
776 os2_formatmsg(msgbuf, msglen, reason);
777 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000778 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000779 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000780
781 return msgbuf;
782}
783
784/* Set an OS/2-specific error and return NULL. OS/2 kernel
785 errors are not in a global variable e.g. 'errno' nor are
786 they congruent with posix error numbers. */
787
Victor Stinner8c62be82010-05-06 00:08:46 +0000788static PyObject *
789os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000790{
791 char text[1024];
792 PyObject *v;
793
794 os2_strerror(text, sizeof(text), code, "");
795
796 v = Py_BuildValue("(is)", code, text);
797 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000798 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000799 Py_DECREF(v);
800 }
801 return NULL; /* Signal to Python that an Exception is Pending */
802}
803
804#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000805
806/* POSIX generic methods */
807
Barry Warsaw53699e91996-12-10 23:23:01 +0000808static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000809posix_fildes(PyObject *fdobj, int (*func)(int))
810{
Victor Stinner8c62be82010-05-06 00:08:46 +0000811 int fd;
812 int res;
813 fd = PyObject_AsFileDescriptor(fdobj);
814 if (fd < 0)
815 return NULL;
816 if (!_PyVerify_fd(fd))
817 return posix_error();
818 Py_BEGIN_ALLOW_THREADS
819 res = (*func)(fd);
820 Py_END_ALLOW_THREADS
821 if (res < 0)
822 return posix_error();
823 Py_INCREF(Py_None);
824 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000825}
Guido van Rossum21142a01999-01-08 21:05:37 +0000826
827static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000829{
Victor Stinner8c62be82010-05-06 00:08:46 +0000830 PyObject *opath1 = NULL;
831 char *path1;
832 int res;
833 if (!PyArg_ParseTuple(args, format,
834 PyUnicode_FSConverter, &opath1))
835 return NULL;
836 path1 = PyBytes_AsString(opath1);
837 Py_BEGIN_ALLOW_THREADS
838 res = (*func)(path1);
839 Py_END_ALLOW_THREADS
840 if (res < 0)
841 return posix_error_with_allocated_filename(opath1);
842 Py_DECREF(opath1);
843 Py_INCREF(Py_None);
844 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000845}
846
Barry Warsaw53699e91996-12-10 23:23:01 +0000847static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000848posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000849 char *format,
850 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000851{
Victor Stinner8c62be82010-05-06 00:08:46 +0000852 PyObject *opath1 = NULL, *opath2 = NULL;
853 char *path1, *path2;
854 int res;
855 if (!PyArg_ParseTuple(args, format,
856 PyUnicode_FSConverter, &opath1,
857 PyUnicode_FSConverter, &opath2)) {
858 return NULL;
859 }
860 path1 = PyBytes_AsString(opath1);
861 path2 = PyBytes_AsString(opath2);
862 Py_BEGIN_ALLOW_THREADS
863 res = (*func)(path1, path2);
864 Py_END_ALLOW_THREADS
865 Py_DECREF(opath1);
866 Py_DECREF(opath2);
867 if (res != 0)
868 /* XXX how to report both path1 and path2??? */
869 return posix_error();
870 Py_INCREF(Py_None);
871 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872}
873
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000874#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000875static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000876win32_1str(PyObject* args, char* func,
877 char* format, BOOL (__stdcall *funcA)(LPCSTR),
878 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000879{
Victor Stinner8c62be82010-05-06 00:08:46 +0000880 PyObject *uni;
881 char *ansi;
882 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000883
Victor Stinner8c62be82010-05-06 00:08:46 +0000884 if (!PyArg_ParseTuple(args, wformat, &uni))
885 PyErr_Clear();
886 else {
887 Py_BEGIN_ALLOW_THREADS
888 result = funcW(PyUnicode_AsUnicode(uni));
889 Py_END_ALLOW_THREADS
890 if (!result)
891 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
892 Py_INCREF(Py_None);
893 return Py_None;
894 }
895 if (!PyArg_ParseTuple(args, format, &ansi))
896 return NULL;
897 Py_BEGIN_ALLOW_THREADS
898 result = funcA(ansi);
899 Py_END_ALLOW_THREADS
900 if (!result)
901 return win32_error(func, ansi);
902 Py_INCREF(Py_None);
903 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904
905}
906
907/* This is a reimplementation of the C library's chdir function,
908 but one that produces Win32 errors instead of DOS error codes.
909 chdir is essentially a wrapper around SetCurrentDirectory; however,
910 it also needs to set "magic" environment variables indicating
911 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000912static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000913win32_chdir(LPCSTR path)
914{
Victor Stinner8c62be82010-05-06 00:08:46 +0000915 char new_path[MAX_PATH+1];
916 int result;
917 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000918
Victor Stinner8c62be82010-05-06 00:08:46 +0000919 if(!SetCurrentDirectoryA(path))
920 return FALSE;
921 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
922 if (!result)
923 return FALSE;
924 /* In the ANSI API, there should not be any paths longer
925 than MAX_PATH. */
926 assert(result <= MAX_PATH+1);
927 if (strncmp(new_path, "\\\\", 2) == 0 ||
928 strncmp(new_path, "//", 2) == 0)
929 /* UNC path, nothing to do. */
930 return TRUE;
931 env[1] = new_path[0];
932 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000933}
934
935/* The Unicode version differs from the ANSI version
936 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000937static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000938win32_wchdir(LPCWSTR path)
939{
Victor Stinner8c62be82010-05-06 00:08:46 +0000940 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
941 int result;
942 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943
Victor Stinner8c62be82010-05-06 00:08:46 +0000944 if(!SetCurrentDirectoryW(path))
945 return FALSE;
946 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
947 if (!result)
948 return FALSE;
949 if (result > MAX_PATH+1) {
950 new_path = malloc(result * sizeof(wchar_t));
951 if (!new_path) {
952 SetLastError(ERROR_OUTOFMEMORY);
953 return FALSE;
954 }
955 result = GetCurrentDirectoryW(result, new_path);
956 if (!result) {
957 free(new_path);
958 return FALSE;
959 }
960 }
961 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
962 wcsncmp(new_path, L"//", 2) == 0)
963 /* UNC path, nothing to do. */
964 return TRUE;
965 env[1] = new_path[0];
966 result = SetEnvironmentVariableW(env, new_path);
967 if (new_path != _new_path)
968 free(new_path);
969 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000970}
971#endif
972
Martin v. Löwis14694662006-02-03 12:54:16 +0000973#ifdef MS_WINDOWS
974/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
975 - time stamps are restricted to second resolution
976 - file modification times suffer from forth-and-back conversions between
977 UTC and local time
978 Therefore, we implement our own stat, based on the Win32 API directly.
979*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000980#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000981
982struct win32_stat{
983 int st_dev;
984 __int64 st_ino;
985 unsigned short st_mode;
986 int st_nlink;
987 int st_uid;
988 int st_gid;
989 int st_rdev;
990 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000991 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000992 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000993 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000994 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000995 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000996 int st_ctime_nsec;
997};
998
999static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1000
1001static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001002FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001003{
Victor Stinner8c62be82010-05-06 00:08:46 +00001004 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1005 /* Cannot simply cast and dereference in_ptr,
1006 since it might not be aligned properly */
1007 __int64 in;
1008 memcpy(&in, in_ptr, sizeof(in));
1009 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001010 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001011}
1012
Thomas Wouters477c8d52006-05-27 19:21:47 +00001013static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001014time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001015{
Victor Stinner8c62be82010-05-06 00:08:46 +00001016 /* XXX endianness */
1017 __int64 out;
1018 out = time_in + secs_between_epochs;
1019 out = out * 10000000 + nsec_in / 100;
1020 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001021}
1022
Martin v. Löwis14694662006-02-03 12:54:16 +00001023/* Below, we *know* that ugo+r is 0444 */
1024#if _S_IREAD != 0400
1025#error Unsupported C library
1026#endif
1027static int
1028attributes_to_mode(DWORD attr)
1029{
Victor Stinner8c62be82010-05-06 00:08:46 +00001030 int m = 0;
1031 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1032 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1033 else
1034 m |= _S_IFREG;
1035 if (attr & FILE_ATTRIBUTE_READONLY)
1036 m |= 0444;
1037 else
1038 m |= 0666;
1039 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001040}
1041
1042static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001043attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001044{
Victor Stinner8c62be82010-05-06 00:08:46 +00001045 memset(result, 0, sizeof(*result));
1046 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1047 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1048 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1049 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1050 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001051 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001052 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001053 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1054 /* first clear the S_IFMT bits */
1055 result->st_mode ^= (result->st_mode & 0170000);
1056 /* now set the bits that make this a symlink */
1057 result->st_mode |= 0120000;
1058 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001059
Victor Stinner8c62be82010-05-06 00:08:46 +00001060 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001061}
1062
Guido van Rossumd8faa362007-04-27 19:54:29 +00001063static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001064attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001065{
Victor Stinner8c62be82010-05-06 00:08:46 +00001066 HANDLE hFindFile;
1067 WIN32_FIND_DATAA FileData;
1068 hFindFile = FindFirstFileA(pszFile, &FileData);
1069 if (hFindFile == INVALID_HANDLE_VALUE)
1070 return FALSE;
1071 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001072 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001073 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001074 info->dwFileAttributes = FileData.dwFileAttributes;
1075 info->ftCreationTime = FileData.ftCreationTime;
1076 info->ftLastAccessTime = FileData.ftLastAccessTime;
1077 info->ftLastWriteTime = FileData.ftLastWriteTime;
1078 info->nFileSizeHigh = FileData.nFileSizeHigh;
1079 info->nFileSizeLow = FileData.nFileSizeLow;
1080/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001081 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1082 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001083 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001084}
1085
1086static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001087attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001088{
Victor Stinner8c62be82010-05-06 00:08:46 +00001089 HANDLE hFindFile;
1090 WIN32_FIND_DATAW FileData;
1091 hFindFile = FindFirstFileW(pszFile, &FileData);
1092 if (hFindFile == INVALID_HANDLE_VALUE)
1093 return FALSE;
1094 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001095 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001096 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001097 info->dwFileAttributes = FileData.dwFileAttributes;
1098 info->ftCreationTime = FileData.ftCreationTime;
1099 info->ftLastAccessTime = FileData.ftLastAccessTime;
1100 info->ftLastWriteTime = FileData.ftLastWriteTime;
1101 info->nFileSizeHigh = FileData.nFileSizeHigh;
1102 info->nFileSizeLow = FileData.nFileSizeLow;
1103/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001104 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1105 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001106 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001107}
1108
Brian Curtind25aef52011-06-13 15:16:04 -05001109/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1110static int has_GetFinalPathNameByHandle = 0;
1111static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1112 DWORD);
1113static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1114 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001115static int
Brian Curtind25aef52011-06-13 15:16:04 -05001116check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001117{
Brian Curtind25aef52011-06-13 15:16:04 -05001118 HINSTANCE hKernel32;
1119 /* only recheck */
1120 if (!has_GetFinalPathNameByHandle)
1121 {
1122 hKernel32 = GetModuleHandle("KERNEL32");
1123 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1124 "GetFinalPathNameByHandleA");
1125 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1126 "GetFinalPathNameByHandleW");
1127 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1128 Py_GetFinalPathNameByHandleW;
1129 }
1130 return has_GetFinalPathNameByHandle;
1131}
1132
1133static BOOL
1134get_target_path(HANDLE hdl, wchar_t **target_path)
1135{
1136 int buf_size, result_length;
1137 wchar_t *buf;
1138
1139 /* We have a good handle to the target, use it to determine
1140 the target path name (then we'll call lstat on it). */
1141 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1142 VOLUME_NAME_DOS);
1143 if(!buf_size)
1144 return FALSE;
1145
1146 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001147 if (!buf) {
1148 SetLastError(ERROR_OUTOFMEMORY);
1149 return FALSE;
1150 }
1151
Brian Curtind25aef52011-06-13 15:16:04 -05001152 result_length = Py_GetFinalPathNameByHandleW(hdl,
1153 buf, buf_size, VOLUME_NAME_DOS);
1154
1155 if(!result_length) {
1156 free(buf);
1157 return FALSE;
1158 }
1159
1160 if(!CloseHandle(hdl)) {
1161 free(buf);
1162 return FALSE;
1163 }
1164
1165 buf[result_length] = 0;
1166
1167 *target_path = buf;
1168 return TRUE;
1169}
1170
1171static int
1172win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1173 BOOL traverse);
1174static int
1175win32_xstat_impl(const char *path, struct win32_stat *result,
1176 BOOL traverse)
1177{
Victor Stinner26de69d2011-06-17 15:15:38 +02001178 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001179 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001180 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001181 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001182 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001183 const char *dot;
1184
Brian Curtind25aef52011-06-13 15:16:04 -05001185 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001186 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1187 traverse reparse point. */
1188 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001189 }
1190
Brian Curtinf5e76d02010-11-24 13:14:05 +00001191 hFile = CreateFileA(
1192 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001193 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001194 0, /* share mode */
1195 NULL, /* security attributes */
1196 OPEN_EXISTING,
1197 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001198 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1199 Because of this, calls like GetFinalPathNameByHandle will return
1200 the symlink path agin and not the actual final path. */
1201 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1202 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001203 NULL);
1204
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001205 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001206 /* Either the target doesn't exist, or we don't have access to
1207 get a handle to it. If the former, we need to return an error.
1208 If the latter, we can use attributes_from_dir. */
1209 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001210 return -1;
1211 /* Could not get attributes on open file. Fall back to
1212 reading the directory. */
1213 if (!attributes_from_dir(path, &info, &reparse_tag))
1214 /* Very strange. This should not fail now */
1215 return -1;
1216 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1217 if (traverse) {
1218 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001219 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001220 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001221 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001222 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001223 } else {
1224 if (!GetFileInformationByHandle(hFile, &info)) {
1225 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001226 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001227 }
1228 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001229 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1230 return -1;
1231
1232 /* Close the outer open file handle now that we're about to
1233 reopen it with different flags. */
1234 if (!CloseHandle(hFile))
1235 return -1;
1236
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001237 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001238 /* In order to call GetFinalPathNameByHandle we need to open
1239 the file without the reparse handling flag set. */
1240 hFile2 = CreateFileA(
1241 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1242 NULL, OPEN_EXISTING,
1243 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1244 NULL);
1245 if (hFile2 == INVALID_HANDLE_VALUE)
1246 return -1;
1247
1248 if (!get_target_path(hFile2, &target_path))
1249 return -1;
1250
1251 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001252 free(target_path);
1253 return code;
1254 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001255 } else
1256 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001257 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001258 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001259
1260 /* Set S_IEXEC if it is an .exe, .bat, ... */
1261 dot = strrchr(path, '.');
1262 if (dot) {
1263 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1264 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1265 result->st_mode |= 0111;
1266 }
1267 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268}
1269
1270static int
Brian Curtind25aef52011-06-13 15:16:04 -05001271win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1272 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001273{
1274 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001275 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001276 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001277 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001278 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001279 const wchar_t *dot;
1280
Brian Curtind25aef52011-06-13 15:16:04 -05001281 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001282 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1283 traverse reparse point. */
1284 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001285 }
1286
Brian Curtinf5e76d02010-11-24 13:14:05 +00001287 hFile = CreateFileW(
1288 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001289 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001290 0, /* share mode */
1291 NULL, /* security attributes */
1292 OPEN_EXISTING,
1293 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001294 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1295 Because of this, calls like GetFinalPathNameByHandle will return
1296 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001297 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001298 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001299 NULL);
1300
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001301 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001302 /* Either the target doesn't exist, or we don't have access to
1303 get a handle to it. If the former, we need to return an error.
1304 If the latter, we can use attributes_from_dir. */
1305 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001306 return -1;
1307 /* Could not get attributes on open file. Fall back to
1308 reading the directory. */
1309 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1310 /* Very strange. This should not fail now */
1311 return -1;
1312 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1313 if (traverse) {
1314 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001315 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001316 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001317 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001318 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001319 } else {
1320 if (!GetFileInformationByHandle(hFile, &info)) {
1321 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001322 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001323 }
1324 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001325 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1326 return -1;
1327
1328 /* Close the outer open file handle now that we're about to
1329 reopen it with different flags. */
1330 if (!CloseHandle(hFile))
1331 return -1;
1332
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001333 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001334 /* In order to call GetFinalPathNameByHandle we need to open
1335 the file without the reparse handling flag set. */
1336 hFile2 = CreateFileW(
1337 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1338 NULL, OPEN_EXISTING,
1339 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1340 NULL);
1341 if (hFile2 == INVALID_HANDLE_VALUE)
1342 return -1;
1343
1344 if (!get_target_path(hFile2, &target_path))
1345 return -1;
1346
1347 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001348 free(target_path);
1349 return code;
1350 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001351 } else
1352 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001353 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001354 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001355
1356 /* Set S_IEXEC if it is an .exe, .bat, ... */
1357 dot = wcsrchr(path, '.');
1358 if (dot) {
1359 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1360 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1361 result->st_mode |= 0111;
1362 }
1363 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001364}
1365
1366static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001367win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001368{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001369 /* Protocol violation: we explicitly clear errno, instead of
1370 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001371 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001372 errno = 0;
1373 return code;
1374}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001375
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001376static int
1377win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1378{
1379 /* Protocol violation: we explicitly clear errno, instead of
1380 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001381 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001382 errno = 0;
1383 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001384}
Brian Curtind25aef52011-06-13 15:16:04 -05001385/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001386
1387 In Posix, stat automatically traverses symlinks and returns the stat
1388 structure for the target. In Windows, the equivalent GetFileAttributes by
1389 default does not traverse symlinks and instead returns attributes for
1390 the symlink.
1391
1392 Therefore, win32_lstat will get the attributes traditionally, and
1393 win32_stat will first explicitly resolve the symlink target and then will
1394 call win32_lstat on that result.
1395
Ezio Melotti4969f702011-03-15 05:59:46 +02001396 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001397
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001398static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001399win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001400{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001401 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001402}
1403
Victor Stinner8c62be82010-05-06 00:08:46 +00001404static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001405win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001406{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001407 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001408}
1409
1410static int
1411win32_stat(const char* path, struct win32_stat *result)
1412{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001413 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001414}
1415
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001416static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001417win32_stat_w(const wchar_t* path, struct win32_stat *result)
1418{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001419 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001420}
1421
1422static int
1423win32_fstat(int file_number, struct win32_stat *result)
1424{
Victor Stinner8c62be82010-05-06 00:08:46 +00001425 BY_HANDLE_FILE_INFORMATION info;
1426 HANDLE h;
1427 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001428
Victor Stinner8c62be82010-05-06 00:08:46 +00001429 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001430
Victor Stinner8c62be82010-05-06 00:08:46 +00001431 /* Protocol violation: we explicitly clear errno, instead of
1432 setting it to a POSIX error. Callers should use GetLastError. */
1433 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001434
Victor Stinner8c62be82010-05-06 00:08:46 +00001435 if (h == INVALID_HANDLE_VALUE) {
1436 /* This is really a C library error (invalid file handle).
1437 We set the Win32 error to the closes one matching. */
1438 SetLastError(ERROR_INVALID_HANDLE);
1439 return -1;
1440 }
1441 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001442
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 type = GetFileType(h);
1444 if (type == FILE_TYPE_UNKNOWN) {
1445 DWORD error = GetLastError();
1446 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001447 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001448 }
1449 /* else: valid but unknown file */
1450 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001451
Victor Stinner8c62be82010-05-06 00:08:46 +00001452 if (type != FILE_TYPE_DISK) {
1453 if (type == FILE_TYPE_CHAR)
1454 result->st_mode = _S_IFCHR;
1455 else if (type == FILE_TYPE_PIPE)
1456 result->st_mode = _S_IFIFO;
1457 return 0;
1458 }
1459
1460 if (!GetFileInformationByHandle(h, &info)) {
1461 return -1;
1462 }
1463
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001464 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001465 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001466 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1467 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001468}
1469
1470#endif /* MS_WINDOWS */
1471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001473"stat_result: Result from stat or lstat.\n\n\
1474This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001475 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001476or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1477\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001478Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1479or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001480\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001481See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001482
1483static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001484 {"st_mode", "protection bits"},
1485 {"st_ino", "inode"},
1486 {"st_dev", "device"},
1487 {"st_nlink", "number of hard links"},
1488 {"st_uid", "user ID of owner"},
1489 {"st_gid", "group ID of owner"},
1490 {"st_size", "total size, in bytes"},
1491 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1492 {NULL, "integer time of last access"},
1493 {NULL, "integer time of last modification"},
1494 {NULL, "integer time of last change"},
1495 {"st_atime", "time of last access"},
1496 {"st_mtime", "time of last modification"},
1497 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001498#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001499 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001500#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001501#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001503#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001504#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001505 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001506#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001507#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001508 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001509#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001510#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001511 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001512#endif
1513#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001514 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001515#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001516 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001517};
1518
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001519#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001520#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001521#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001522#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001523#endif
1524
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001525#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001526#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1527#else
1528#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1529#endif
1530
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001531#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001532#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1533#else
1534#define ST_RDEV_IDX ST_BLOCKS_IDX
1535#endif
1536
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001537#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1538#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1539#else
1540#define ST_FLAGS_IDX ST_RDEV_IDX
1541#endif
1542
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001543#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001544#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001545#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001546#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001547#endif
1548
1549#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1550#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1551#else
1552#define ST_BIRTHTIME_IDX ST_GEN_IDX
1553#endif
1554
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001555static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 "stat_result", /* name */
1557 stat_result__doc__, /* doc */
1558 stat_result_fields,
1559 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001560};
1561
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001562PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001563"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1564This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001565 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001566or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001567\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001569
1570static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001571 {"f_bsize", },
1572 {"f_frsize", },
1573 {"f_blocks", },
1574 {"f_bfree", },
1575 {"f_bavail", },
1576 {"f_files", },
1577 {"f_ffree", },
1578 {"f_favail", },
1579 {"f_flag", },
1580 {"f_namemax",},
1581 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001582};
1583
1584static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001585 "statvfs_result", /* name */
1586 statvfs_result__doc__, /* doc */
1587 statvfs_result_fields,
1588 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001589};
1590
Ross Lagerwall7807c352011-03-17 20:20:30 +02001591#if defined(HAVE_WAITID) && !defined(__APPLE__)
1592PyDoc_STRVAR(waitid_result__doc__,
1593"waitid_result: Result from waitid.\n\n\
1594This object may be accessed either as a tuple of\n\
1595 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1596or via the attributes si_pid, si_uid, and so on.\n\
1597\n\
1598See os.waitid for more information.");
1599
1600static PyStructSequence_Field waitid_result_fields[] = {
1601 {"si_pid", },
1602 {"si_uid", },
1603 {"si_signo", },
1604 {"si_status", },
1605 {"si_code", },
1606 {0}
1607};
1608
1609static PyStructSequence_Desc waitid_result_desc = {
1610 "waitid_result", /* name */
1611 waitid_result__doc__, /* doc */
1612 waitid_result_fields,
1613 5
1614};
1615static PyTypeObject WaitidResultType;
1616#endif
1617
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001618static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001619static PyTypeObject StatResultType;
1620static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001621#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001622static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001623#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001624static newfunc structseq_new;
1625
1626static PyObject *
1627statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1628{
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 PyStructSequence *result;
1630 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001631
Victor Stinner8c62be82010-05-06 00:08:46 +00001632 result = (PyStructSequence*)structseq_new(type, args, kwds);
1633 if (!result)
1634 return NULL;
1635 /* If we have been initialized from a tuple,
1636 st_?time might be set to None. Initialize it
1637 from the int slots. */
1638 for (i = 7; i <= 9; i++) {
1639 if (result->ob_item[i+3] == Py_None) {
1640 Py_DECREF(Py_None);
1641 Py_INCREF(result->ob_item[i]);
1642 result->ob_item[i+3] = result->ob_item[i];
1643 }
1644 }
1645 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001646}
1647
1648
1649
1650/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001651static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001652
1653PyDoc_STRVAR(stat_float_times__doc__,
1654"stat_float_times([newval]) -> oldval\n\n\
1655Determine whether os.[lf]stat represents time stamps as float objects.\n\
1656If newval is True, future calls to stat() return floats, if it is False,\n\
1657future calls return ints. \n\
1658If newval is omitted, return the current setting.\n");
1659
1660static PyObject*
1661stat_float_times(PyObject* self, PyObject *args)
1662{
Victor Stinner8c62be82010-05-06 00:08:46 +00001663 int newval = -1;
1664 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1665 return NULL;
1666 if (newval == -1)
1667 /* Return old value */
1668 return PyBool_FromLong(_stat_float_times);
1669 _stat_float_times = newval;
1670 Py_INCREF(Py_None);
1671 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001672}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001673
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001674static void
1675fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1676{
Victor Stinner8c62be82010-05-06 00:08:46 +00001677 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001678#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001680#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001682#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 if (!ival)
1684 return;
1685 if (_stat_float_times) {
1686 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1687 } else {
1688 fval = ival;
1689 Py_INCREF(fval);
1690 }
1691 PyStructSequence_SET_ITEM(v, index, ival);
1692 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001693}
1694
Tim Peters5aa91602002-01-30 05:46:57 +00001695/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001696 (used by posix_stat() and posix_fstat()) */
1697static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001698_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001699{
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 unsigned long ansec, mnsec, cnsec;
1701 PyObject *v = PyStructSequence_New(&StatResultType);
1702 if (v == NULL)
1703 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001704
Victor Stinner8c62be82010-05-06 00:08:46 +00001705 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001706#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001707 PyStructSequence_SET_ITEM(v, 1,
1708 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001709#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001710 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001711#endif
1712#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 PyStructSequence_SET_ITEM(v, 2,
1714 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001716 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001717#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1719 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1720 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001721#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001722 PyStructSequence_SET_ITEM(v, 6,
1723 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001724#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001725 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001726#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001727
Martin v. Löwis14694662006-02-03 12:54:16 +00001728#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001729 ansec = st->st_atim.tv_nsec;
1730 mnsec = st->st_mtim.tv_nsec;
1731 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001732#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001733 ansec = st->st_atimespec.tv_nsec;
1734 mnsec = st->st_mtimespec.tv_nsec;
1735 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001736#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 ansec = st->st_atime_nsec;
1738 mnsec = st->st_mtime_nsec;
1739 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001740#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001742#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001743 fill_time(v, 7, st->st_atime, ansec);
1744 fill_time(v, 8, st->st_mtime, mnsec);
1745 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001746
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001747#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001748 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1749 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001750#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001751#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1753 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001754#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001755#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001756 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1757 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001758#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001759#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1761 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001762#endif
1763#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001764 {
1765 PyObject *val;
1766 unsigned long bsec,bnsec;
1767 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001768#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001770#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001771 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001772#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001773 if (_stat_float_times) {
1774 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1775 } else {
1776 val = PyLong_FromLong((long)bsec);
1777 }
1778 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1779 val);
1780 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001781#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001782#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001783 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1784 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001785#endif
Fred Drake699f3522000-06-29 21:12:41 +00001786
Victor Stinner8c62be82010-05-06 00:08:46 +00001787 if (PyErr_Occurred()) {
1788 Py_DECREF(v);
1789 return NULL;
1790 }
Fred Drake699f3522000-06-29 21:12:41 +00001791
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001793}
1794
Barry Warsaw53699e91996-12-10 23:23:01 +00001795static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001796posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001797 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001798#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001800#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001802#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001803 char *wformat,
1804 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001805{
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 STRUCT_STAT st;
1807 PyObject *opath;
1808 char *path;
1809 int res;
1810 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001811
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001812#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001813 PyUnicodeObject *po;
1814 if (PyArg_ParseTuple(args, wformat, &po)) {
1815 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001816
Victor Stinner8c62be82010-05-06 00:08:46 +00001817 Py_BEGIN_ALLOW_THREADS
1818 /* PyUnicode_AS_UNICODE result OK without
1819 thread lock as it is a simple dereference. */
1820 res = wstatfunc(wpath, &st);
1821 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001822
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 if (res != 0)
1824 return win32_error_unicode("stat", wpath);
1825 return _pystat_fromstructstat(&st);
1826 }
1827 /* Drop the argument parsing error as narrow strings
1828 are also valid. */
1829 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001830#endif
1831
Victor Stinner8c62be82010-05-06 00:08:46 +00001832 if (!PyArg_ParseTuple(args, format,
1833 PyUnicode_FSConverter, &opath))
1834 return NULL;
1835 path = PyBytes_AsString(opath);
1836 Py_BEGIN_ALLOW_THREADS
1837 res = (*statfunc)(path, &st);
1838 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001839
Victor Stinner8c62be82010-05-06 00:08:46 +00001840 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001841#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001842 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001843#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001844 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001845#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001846 }
1847 else
1848 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001849
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 Py_DECREF(opath);
1851 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001852}
1853
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001854/* POSIX methods */
1855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001857"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001858Use the real uid/gid to test for access to a path. Note that most\n\
1859operations will use the effective uid/gid, therefore this routine can\n\
1860be used in a suid/sgid environment to test if the invoking user has the\n\
1861specified access to the path. The mode argument can be F_OK to test\n\
1862existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001863
1864static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001865posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001866{
Victor Stinner8c62be82010-05-06 00:08:46 +00001867 PyObject *opath;
1868 char *path;
1869 int mode;
1870
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001871#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001872 DWORD attr;
1873 PyUnicodeObject *po;
1874 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1875 Py_BEGIN_ALLOW_THREADS
1876 /* PyUnicode_AS_UNICODE OK without thread lock as
1877 it is a simple dereference. */
1878 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1879 Py_END_ALLOW_THREADS
1880 goto finish;
1881 }
1882 /* Drop the argument parsing error as narrow strings
1883 are also valid. */
1884 PyErr_Clear();
1885 if (!PyArg_ParseTuple(args, "O&i:access",
1886 PyUnicode_FSConverter, &opath, &mode))
1887 return NULL;
1888 path = PyBytes_AsString(opath);
1889 Py_BEGIN_ALLOW_THREADS
1890 attr = GetFileAttributesA(path);
1891 Py_END_ALLOW_THREADS
1892 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001893finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001894 if (attr == 0xFFFFFFFF)
1895 /* File does not exist, or cannot read attributes */
1896 return PyBool_FromLong(0);
1897 /* Access is possible if either write access wasn't requested, or
1898 the file isn't read-only, or if it's a directory, as there are
1899 no read-only directories on Windows. */
1900 return PyBool_FromLong(!(mode & 2)
1901 || !(attr & FILE_ATTRIBUTE_READONLY)
1902 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001904 int res;
1905 if (!PyArg_ParseTuple(args, "O&i:access",
1906 PyUnicode_FSConverter, &opath, &mode))
1907 return NULL;
1908 path = PyBytes_AsString(opath);
1909 Py_BEGIN_ALLOW_THREADS
1910 res = access(path, mode);
1911 Py_END_ALLOW_THREADS
1912 Py_DECREF(opath);
1913 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001914#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001915}
1916
Guido van Rossumd371ff11999-01-25 16:12:23 +00001917#ifndef F_OK
1918#define F_OK 0
1919#endif
1920#ifndef R_OK
1921#define R_OK 4
1922#endif
1923#ifndef W_OK
1924#define W_OK 2
1925#endif
1926#ifndef X_OK
1927#define X_OK 1
1928#endif
1929
1930#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001931PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001932"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001933Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001934
1935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001936posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001937{
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 int id;
1939 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001940
Victor Stinner8c62be82010-05-06 00:08:46 +00001941 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1942 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001943
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001944#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001945 /* file descriptor 0 only, the default input device (stdin) */
1946 if (id == 0) {
1947 ret = ttyname();
1948 }
1949 else {
1950 ret = NULL;
1951 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001952#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001953 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001954#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001955 if (ret == NULL)
1956 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001957 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001958}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001959#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001960
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001961#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001962PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001963"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001964Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001965
1966static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001967posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001968{
Victor Stinner8c62be82010-05-06 00:08:46 +00001969 char *ret;
1970 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001971
Greg Wardb48bc172000-03-01 21:51:56 +00001972#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001973 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001974#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001975 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001976#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001977 if (ret == NULL)
1978 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001979 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001980}
1981#endif
1982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001983PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001984"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001985Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001986
Barry Warsaw53699e91996-12-10 23:23:01 +00001987static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001988posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001989{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001990#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001991 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001992#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001993 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001994#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001995 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001996#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001997 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001998#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001999}
2000
Fred Drake4d1e64b2002-04-15 19:40:07 +00002001#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002002PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002003"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002004Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002005opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002006
2007static PyObject *
2008posix_fchdir(PyObject *self, PyObject *fdobj)
2009{
Victor Stinner8c62be82010-05-06 00:08:46 +00002010 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002011}
2012#endif /* HAVE_FCHDIR */
2013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002015PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002016"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002017Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002018
Barry Warsaw53699e91996-12-10 23:23:01 +00002019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002020posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002021{
Victor Stinner8c62be82010-05-06 00:08:46 +00002022 PyObject *opath = NULL;
2023 char *path = NULL;
2024 int i;
2025 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002026#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002027 DWORD attr;
2028 PyUnicodeObject *po;
2029 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2030 Py_BEGIN_ALLOW_THREADS
2031 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2032 if (attr != 0xFFFFFFFF) {
2033 if (i & _S_IWRITE)
2034 attr &= ~FILE_ATTRIBUTE_READONLY;
2035 else
2036 attr |= FILE_ATTRIBUTE_READONLY;
2037 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2038 }
2039 else
2040 res = 0;
2041 Py_END_ALLOW_THREADS
2042 if (!res)
2043 return win32_error_unicode("chmod",
2044 PyUnicode_AS_UNICODE(po));
2045 Py_INCREF(Py_None);
2046 return Py_None;
2047 }
2048 /* Drop the argument parsing error as narrow strings
2049 are also valid. */
2050 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002051
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2053 &opath, &i))
2054 return NULL;
2055 path = PyBytes_AsString(opath);
2056 Py_BEGIN_ALLOW_THREADS
2057 attr = GetFileAttributesA(path);
2058 if (attr != 0xFFFFFFFF) {
2059 if (i & _S_IWRITE)
2060 attr &= ~FILE_ATTRIBUTE_READONLY;
2061 else
2062 attr |= FILE_ATTRIBUTE_READONLY;
2063 res = SetFileAttributesA(path, attr);
2064 }
2065 else
2066 res = 0;
2067 Py_END_ALLOW_THREADS
2068 if (!res) {
2069 win32_error("chmod", path);
2070 Py_DECREF(opath);
2071 return NULL;
2072 }
2073 Py_DECREF(opath);
2074 Py_INCREF(Py_None);
2075 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002076#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002077 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2078 &opath, &i))
2079 return NULL;
2080 path = PyBytes_AsString(opath);
2081 Py_BEGIN_ALLOW_THREADS
2082 res = chmod(path, i);
2083 Py_END_ALLOW_THREADS
2084 if (res < 0)
2085 return posix_error_with_allocated_filename(opath);
2086 Py_DECREF(opath);
2087 Py_INCREF(Py_None);
2088 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002089#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002090}
2091
Christian Heimes4e30a842007-11-30 22:12:06 +00002092#ifdef HAVE_FCHMOD
2093PyDoc_STRVAR(posix_fchmod__doc__,
2094"fchmod(fd, mode)\n\n\
2095Change the access permissions of the file given by file\n\
2096descriptor fd.");
2097
2098static PyObject *
2099posix_fchmod(PyObject *self, PyObject *args)
2100{
Victor Stinner8c62be82010-05-06 00:08:46 +00002101 int fd, mode, res;
2102 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2103 return NULL;
2104 Py_BEGIN_ALLOW_THREADS
2105 res = fchmod(fd, mode);
2106 Py_END_ALLOW_THREADS
2107 if (res < 0)
2108 return posix_error();
2109 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002110}
2111#endif /* HAVE_FCHMOD */
2112
2113#ifdef HAVE_LCHMOD
2114PyDoc_STRVAR(posix_lchmod__doc__,
2115"lchmod(path, mode)\n\n\
2116Change the access permissions of a file. If path is a symlink, this\n\
2117affects the link itself rather than the target.");
2118
2119static PyObject *
2120posix_lchmod(PyObject *self, PyObject *args)
2121{
Victor Stinner8c62be82010-05-06 00:08:46 +00002122 PyObject *opath;
2123 char *path;
2124 int i;
2125 int res;
2126 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2127 &opath, &i))
2128 return NULL;
2129 path = PyBytes_AsString(opath);
2130 Py_BEGIN_ALLOW_THREADS
2131 res = lchmod(path, i);
2132 Py_END_ALLOW_THREADS
2133 if (res < 0)
2134 return posix_error_with_allocated_filename(opath);
2135 Py_DECREF(opath);
2136 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002137}
2138#endif /* HAVE_LCHMOD */
2139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002140
Thomas Wouterscf297e42007-02-23 15:07:44 +00002141#ifdef HAVE_CHFLAGS
2142PyDoc_STRVAR(posix_chflags__doc__,
2143"chflags(path, flags)\n\n\
2144Set file flags.");
2145
2146static PyObject *
2147posix_chflags(PyObject *self, PyObject *args)
2148{
Victor Stinner8c62be82010-05-06 00:08:46 +00002149 PyObject *opath;
2150 char *path;
2151 unsigned long flags;
2152 int res;
2153 if (!PyArg_ParseTuple(args, "O&k:chflags",
2154 PyUnicode_FSConverter, &opath, &flags))
2155 return NULL;
2156 path = PyBytes_AsString(opath);
2157 Py_BEGIN_ALLOW_THREADS
2158 res = chflags(path, flags);
2159 Py_END_ALLOW_THREADS
2160 if (res < 0)
2161 return posix_error_with_allocated_filename(opath);
2162 Py_DECREF(opath);
2163 Py_INCREF(Py_None);
2164 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002165}
2166#endif /* HAVE_CHFLAGS */
2167
2168#ifdef HAVE_LCHFLAGS
2169PyDoc_STRVAR(posix_lchflags__doc__,
2170"lchflags(path, flags)\n\n\
2171Set file flags.\n\
2172This function will not follow symbolic links.");
2173
2174static PyObject *
2175posix_lchflags(PyObject *self, PyObject *args)
2176{
Victor Stinner8c62be82010-05-06 00:08:46 +00002177 PyObject *opath;
2178 char *path;
2179 unsigned long flags;
2180 int res;
2181 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2182 PyUnicode_FSConverter, &opath, &flags))
2183 return NULL;
2184 path = PyBytes_AsString(opath);
2185 Py_BEGIN_ALLOW_THREADS
2186 res = lchflags(path, flags);
2187 Py_END_ALLOW_THREADS
2188 if (res < 0)
2189 return posix_error_with_allocated_filename(opath);
2190 Py_DECREF(opath);
2191 Py_INCREF(Py_None);
2192 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002193}
2194#endif /* HAVE_LCHFLAGS */
2195
Martin v. Löwis244edc82001-10-04 22:44:26 +00002196#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002197PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002198"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002199Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002200
2201static PyObject *
2202posix_chroot(PyObject *self, PyObject *args)
2203{
Victor Stinner8c62be82010-05-06 00:08:46 +00002204 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002205}
2206#endif
2207
Guido van Rossum21142a01999-01-08 21:05:37 +00002208#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002209PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002210"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002211force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002212
2213static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002214posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002215{
Stefan Krah0e803b32010-11-26 16:16:47 +00002216 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002217}
2218#endif /* HAVE_FSYNC */
2219
Ross Lagerwall7807c352011-03-17 20:20:30 +02002220#ifdef HAVE_SYNC
2221PyDoc_STRVAR(posix_sync__doc__,
2222"sync()\n\n\
2223Force write of everything to disk.");
2224
2225static PyObject *
2226posix_sync(PyObject *self, PyObject *noargs)
2227{
2228 Py_BEGIN_ALLOW_THREADS
2229 sync();
2230 Py_END_ALLOW_THREADS
2231 Py_RETURN_NONE;
2232}
2233#endif
2234
Guido van Rossum21142a01999-01-08 21:05:37 +00002235#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002236
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002237#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002238extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2239#endif
2240
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002241PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002242"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002243force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002244 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002245
2246static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002247posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002248{
Stefan Krah0e803b32010-11-26 16:16:47 +00002249 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002250}
2251#endif /* HAVE_FDATASYNC */
2252
2253
Fredrik Lundh10723342000-07-10 16:38:09 +00002254#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002255PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002256"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002257Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002258
Barry Warsaw53699e91996-12-10 23:23:01 +00002259static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002260posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002261{
Victor Stinner8c62be82010-05-06 00:08:46 +00002262 PyObject *opath;
2263 char *path;
2264 long uid, gid;
2265 int res;
2266 if (!PyArg_ParseTuple(args, "O&ll:chown",
2267 PyUnicode_FSConverter, &opath,
2268 &uid, &gid))
2269 return NULL;
2270 path = PyBytes_AsString(opath);
2271 Py_BEGIN_ALLOW_THREADS
2272 res = chown(path, (uid_t) uid, (gid_t) gid);
2273 Py_END_ALLOW_THREADS
2274 if (res < 0)
2275 return posix_error_with_allocated_filename(opath);
2276 Py_DECREF(opath);
2277 Py_INCREF(Py_None);
2278 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002279}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002280#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002281
Christian Heimes4e30a842007-11-30 22:12:06 +00002282#ifdef HAVE_FCHOWN
2283PyDoc_STRVAR(posix_fchown__doc__,
2284"fchown(fd, uid, gid)\n\n\
2285Change the owner and group id of the file given by file descriptor\n\
2286fd to the numeric uid and gid.");
2287
2288static PyObject *
2289posix_fchown(PyObject *self, PyObject *args)
2290{
Victor Stinner8c62be82010-05-06 00:08:46 +00002291 int fd;
2292 long uid, gid;
2293 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002294 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002295 return NULL;
2296 Py_BEGIN_ALLOW_THREADS
2297 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2298 Py_END_ALLOW_THREADS
2299 if (res < 0)
2300 return posix_error();
2301 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002302}
2303#endif /* HAVE_FCHOWN */
2304
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002305#ifdef HAVE_LCHOWN
2306PyDoc_STRVAR(posix_lchown__doc__,
2307"lchown(path, uid, gid)\n\n\
2308Change the owner and group id of path to the numeric uid and gid.\n\
2309This function will not follow symbolic links.");
2310
2311static PyObject *
2312posix_lchown(PyObject *self, PyObject *args)
2313{
Victor Stinner8c62be82010-05-06 00:08:46 +00002314 PyObject *opath;
2315 char *path;
2316 long uid, gid;
2317 int res;
2318 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2319 PyUnicode_FSConverter, &opath,
2320 &uid, &gid))
2321 return NULL;
2322 path = PyBytes_AsString(opath);
2323 Py_BEGIN_ALLOW_THREADS
2324 res = lchown(path, (uid_t) uid, (gid_t) gid);
2325 Py_END_ALLOW_THREADS
2326 if (res < 0)
2327 return posix_error_with_allocated_filename(opath);
2328 Py_DECREF(opath);
2329 Py_INCREF(Py_None);
2330 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002331}
2332#endif /* HAVE_LCHOWN */
2333
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002334
Guido van Rossum36bc6801995-06-14 22:54:23 +00002335#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002336static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002337posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002338{
Victor Stinner8c62be82010-05-06 00:08:46 +00002339 char buf[1026];
2340 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002341
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002342#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002343 if (!use_bytes) {
2344 wchar_t wbuf[1026];
2345 wchar_t *wbuf2 = wbuf;
2346 PyObject *resobj;
2347 DWORD len;
2348 Py_BEGIN_ALLOW_THREADS
2349 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2350 /* If the buffer is large enough, len does not include the
2351 terminating \0. If the buffer is too small, len includes
2352 the space needed for the terminator. */
2353 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2354 wbuf2 = malloc(len * sizeof(wchar_t));
2355 if (wbuf2)
2356 len = GetCurrentDirectoryW(len, wbuf2);
2357 }
2358 Py_END_ALLOW_THREADS
2359 if (!wbuf2) {
2360 PyErr_NoMemory();
2361 return NULL;
2362 }
2363 if (!len) {
2364 if (wbuf2 != wbuf) free(wbuf2);
2365 return win32_error("getcwdu", NULL);
2366 }
2367 resobj = PyUnicode_FromWideChar(wbuf2, len);
2368 if (wbuf2 != wbuf) free(wbuf2);
2369 return resobj;
2370 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002371#endif
2372
Victor Stinner8c62be82010-05-06 00:08:46 +00002373 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002374#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002375 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002376#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002377 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002378#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002379 Py_END_ALLOW_THREADS
2380 if (res == NULL)
2381 return posix_error();
2382 if (use_bytes)
2383 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002384 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002385}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002386
2387PyDoc_STRVAR(posix_getcwd__doc__,
2388"getcwd() -> path\n\n\
2389Return a unicode string representing the current working directory.");
2390
2391static PyObject *
2392posix_getcwd_unicode(PyObject *self)
2393{
2394 return posix_getcwd(0);
2395}
2396
2397PyDoc_STRVAR(posix_getcwdb__doc__,
2398"getcwdb() -> path\n\n\
2399Return a bytes string representing the current working directory.");
2400
2401static PyObject *
2402posix_getcwd_bytes(PyObject *self)
2403{
2404 return posix_getcwd(1);
2405}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002406#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002407
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002408
Guido van Rossumb6775db1994-08-01 11:34:53 +00002409#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002410PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002411"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002412Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002413
Barry Warsaw53699e91996-12-10 23:23:01 +00002414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002415posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002416{
Victor Stinner8c62be82010-05-06 00:08:46 +00002417 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002418}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002419#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002420
Brian Curtin1b9df392010-11-24 20:24:31 +00002421#ifdef MS_WINDOWS
2422PyDoc_STRVAR(win32_link__doc__,
2423"link(src, dst)\n\n\
2424Create a hard link to a file.");
2425
2426static PyObject *
2427win32_link(PyObject *self, PyObject *args)
2428{
2429 PyObject *osrc, *odst;
2430 char *src, *dst;
2431 BOOL rslt;
2432
Brian Curtinfc889c42010-11-28 23:59:46 +00002433 PyUnicodeObject *usrc, *udst;
2434 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2435 Py_BEGIN_ALLOW_THREADS
2436 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2437 PyUnicode_AS_UNICODE(usrc), NULL);
2438 Py_END_ALLOW_THREADS
2439
2440 if (rslt == 0)
2441 return win32_error("link", NULL);
2442
2443 Py_RETURN_NONE;
2444 }
2445
2446 /* Narrow strings also valid. */
2447 PyErr_Clear();
2448
Brian Curtin1b9df392010-11-24 20:24:31 +00002449 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2450 PyUnicode_FSConverter, &odst))
2451 return NULL;
2452
2453 src = PyBytes_AsString(osrc);
2454 dst = PyBytes_AsString(odst);
2455
2456 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002457 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002458 Py_END_ALLOW_THREADS
2459
Stefan Krah30b341f2010-11-27 11:44:18 +00002460 Py_DECREF(osrc);
2461 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002462 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002463 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002464
2465 Py_RETURN_NONE;
2466}
2467#endif /* MS_WINDOWS */
2468
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002471"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002472Return a list containing the names of the entries in the directory.\n\
2473\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002474 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002475\n\
2476The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002477entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002478
Barry Warsaw53699e91996-12-10 23:23:01 +00002479static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002480posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002481{
Victor Stinner8c62be82010-05-06 00:08:46 +00002482 /* XXX Should redo this putting the (now four) versions of opendir
2483 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002484#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002485
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 PyObject *d, *v;
2487 HANDLE hFindFile;
2488 BOOL result;
2489 WIN32_FIND_DATA FileData;
2490 PyObject *opath;
2491 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2492 char *bufptr = namebuf;
2493 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002494
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002495 PyObject *po = NULL;
2496 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002497 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002498 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002499
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002500 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002501 po_wchars = L".";
2502 len = 1;
2503 } else {
2504 po_wchars = PyUnicode_AS_UNICODE(po);
2505 len = PyUnicode_GET_SIZE(po);
2506 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002507 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002508 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2509 if (!wnamebuf) {
2510 PyErr_NoMemory();
2511 return NULL;
2512 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002513 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002514 if (len > 0) {
2515 Py_UNICODE wch = wnamebuf[len-1];
2516 if (wch != L'/' && wch != L'\\' && wch != L':')
2517 wnamebuf[len++] = L'\\';
2518 wcscpy(wnamebuf + len, L"*.*");
2519 }
2520 if ((d = PyList_New(0)) == NULL) {
2521 free(wnamebuf);
2522 return NULL;
2523 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002524 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002525 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002526 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002527 if (hFindFile == INVALID_HANDLE_VALUE) {
2528 int error = GetLastError();
2529 if (error == ERROR_FILE_NOT_FOUND) {
2530 free(wnamebuf);
2531 return d;
2532 }
2533 Py_DECREF(d);
2534 win32_error_unicode("FindFirstFileW", wnamebuf);
2535 free(wnamebuf);
2536 return NULL;
2537 }
2538 do {
2539 /* Skip over . and .. */
2540 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2541 wcscmp(wFileData.cFileName, L"..") != 0) {
2542 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2543 if (v == NULL) {
2544 Py_DECREF(d);
2545 d = NULL;
2546 break;
2547 }
2548 if (PyList_Append(d, v) != 0) {
2549 Py_DECREF(v);
2550 Py_DECREF(d);
2551 d = NULL;
2552 break;
2553 }
2554 Py_DECREF(v);
2555 }
2556 Py_BEGIN_ALLOW_THREADS
2557 result = FindNextFileW(hFindFile, &wFileData);
2558 Py_END_ALLOW_THREADS
2559 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2560 it got to the end of the directory. */
2561 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2562 Py_DECREF(d);
2563 win32_error_unicode("FindNextFileW", wnamebuf);
2564 FindClose(hFindFile);
2565 free(wnamebuf);
2566 return NULL;
2567 }
2568 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002569
Victor Stinner8c62be82010-05-06 00:08:46 +00002570 if (FindClose(hFindFile) == FALSE) {
2571 Py_DECREF(d);
2572 win32_error_unicode("FindClose", wnamebuf);
2573 free(wnamebuf);
2574 return NULL;
2575 }
2576 free(wnamebuf);
2577 return d;
2578 }
2579 /* Drop the argument parsing error as narrow strings
2580 are also valid. */
2581 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002582
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 if (!PyArg_ParseTuple(args, "O&:listdir",
2584 PyUnicode_FSConverter, &opath))
2585 return NULL;
2586 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2587 PyErr_SetString(PyExc_ValueError, "path too long");
2588 Py_DECREF(opath);
2589 return NULL;
2590 }
2591 strcpy(namebuf, PyBytes_AsString(opath));
2592 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002593 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 if (len > 0) {
2595 char ch = namebuf[len-1];
2596 if (ch != SEP && ch != ALTSEP && ch != ':')
2597 namebuf[len++] = '/';
2598 strcpy(namebuf + len, "*.*");
2599 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002600
Victor Stinner8c62be82010-05-06 00:08:46 +00002601 if ((d = PyList_New(0)) == NULL)
2602 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002603
Antoine Pitroub73caab2010-08-09 23:39:31 +00002604 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002605 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002606 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002607 if (hFindFile == INVALID_HANDLE_VALUE) {
2608 int error = GetLastError();
2609 if (error == ERROR_FILE_NOT_FOUND)
2610 return d;
2611 Py_DECREF(d);
2612 return win32_error("FindFirstFile", namebuf);
2613 }
2614 do {
2615 /* Skip over . and .. */
2616 if (strcmp(FileData.cFileName, ".") != 0 &&
2617 strcmp(FileData.cFileName, "..") != 0) {
2618 v = PyBytes_FromString(FileData.cFileName);
2619 if (v == NULL) {
2620 Py_DECREF(d);
2621 d = NULL;
2622 break;
2623 }
2624 if (PyList_Append(d, v) != 0) {
2625 Py_DECREF(v);
2626 Py_DECREF(d);
2627 d = NULL;
2628 break;
2629 }
2630 Py_DECREF(v);
2631 }
2632 Py_BEGIN_ALLOW_THREADS
2633 result = FindNextFile(hFindFile, &FileData);
2634 Py_END_ALLOW_THREADS
2635 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2636 it got to the end of the directory. */
2637 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2638 Py_DECREF(d);
2639 win32_error("FindNextFile", namebuf);
2640 FindClose(hFindFile);
2641 return NULL;
2642 }
2643 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002644
Victor Stinner8c62be82010-05-06 00:08:46 +00002645 if (FindClose(hFindFile) == FALSE) {
2646 Py_DECREF(d);
2647 return win32_error("FindClose", namebuf);
2648 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002649
Victor Stinner8c62be82010-05-06 00:08:46 +00002650 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002651
Tim Peters0bb44a42000-09-15 07:44:49 +00002652#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002653
2654#ifndef MAX_PATH
2655#define MAX_PATH CCHMAXPATH
2656#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002657 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002658 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002659 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002660 PyObject *d, *v;
2661 char namebuf[MAX_PATH+5];
2662 HDIR hdir = 1;
2663 ULONG srchcnt = 1;
2664 FILEFINDBUF3 ep;
2665 APIRET rc;
2666
Victor Stinner8c62be82010-05-06 00:08:46 +00002667 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002668 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002669 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002670 name = PyBytes_AsString(oname);
2671 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002672 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002673 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002674 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002675 return NULL;
2676 }
2677 strcpy(namebuf, name);
2678 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002679 if (*pt == ALTSEP)
2680 *pt = SEP;
2681 if (namebuf[len-1] != SEP)
2682 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002683 strcpy(namebuf + len, "*.*");
2684
Neal Norwitz6c913782007-10-14 03:23:09 +00002685 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002686 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002687 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002688 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002689
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002690 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2691 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002692 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002693 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2694 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2695 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002696
2697 if (rc != NO_ERROR) {
2698 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002699 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002700 }
2701
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002702 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002703 do {
2704 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002705 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002706 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002707
2708 strcpy(namebuf, ep.achName);
2709
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002710 /* Leave Case of Name Alone -- In Native Form */
2711 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002712
Christian Heimes72b710a2008-05-26 13:28:38 +00002713 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002714 if (v == NULL) {
2715 Py_DECREF(d);
2716 d = NULL;
2717 break;
2718 }
2719 if (PyList_Append(d, v) != 0) {
2720 Py_DECREF(v);
2721 Py_DECREF(d);
2722 d = NULL;
2723 break;
2724 }
2725 Py_DECREF(v);
2726 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2727 }
2728
Victor Stinnerdcb24032010-04-22 12:08:36 +00002729 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002730 return d;
2731#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002732 PyObject *oname;
2733 char *name;
2734 PyObject *d, *v;
2735 DIR *dirp;
2736 struct dirent *ep;
2737 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002738
Victor Stinner8c62be82010-05-06 00:08:46 +00002739 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002740 /* v is never read, so it does not need to be initialized yet. */
2741 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002742 arg_is_unicode = 0;
2743 PyErr_Clear();
2744 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002745 oname = NULL;
2746 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002747 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002748 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002749 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002750 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002752 Py_BEGIN_ALLOW_THREADS
2753 dirp = opendir(name);
2754 Py_END_ALLOW_THREADS
2755 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002756 return posix_error_with_allocated_filename(oname);
2757 }
2758 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002759 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002760 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002761 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002762 Py_DECREF(oname);
2763 return NULL;
2764 }
2765 for (;;) {
2766 errno = 0;
2767 Py_BEGIN_ALLOW_THREADS
2768 ep = readdir(dirp);
2769 Py_END_ALLOW_THREADS
2770 if (ep == NULL) {
2771 if (errno == 0) {
2772 break;
2773 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002774 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002775 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002776 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002777 Py_DECREF(d);
2778 return posix_error_with_allocated_filename(oname);
2779 }
2780 }
2781 if (ep->d_name[0] == '.' &&
2782 (NAMLEN(ep) == 1 ||
2783 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2784 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002785 if (arg_is_unicode)
2786 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2787 else
2788 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002789 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002790 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002791 break;
2792 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002793 if (PyList_Append(d, v) != 0) {
2794 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002795 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002796 break;
2797 }
2798 Py_DECREF(v);
2799 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002800 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002801 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002802 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002803 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002804
Victor Stinner8c62be82010-05-06 00:08:46 +00002805 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002806
Tim Peters0bb44a42000-09-15 07:44:49 +00002807#endif /* which OS */
2808} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002809
Antoine Pitrou8250e232011-02-25 23:41:16 +00002810#ifdef HAVE_FDOPENDIR
2811PyDoc_STRVAR(posix_fdlistdir__doc__,
2812"fdlistdir(fd) -> list_of_strings\n\n\
2813Like listdir(), but uses a file descriptor instead.\n\
2814After succesful execution of this function, fd will be closed.");
2815
2816static PyObject *
2817posix_fdlistdir(PyObject *self, PyObject *args)
2818{
2819 PyObject *d, *v;
2820 DIR *dirp;
2821 struct dirent *ep;
2822 int fd;
2823
2824 errno = 0;
2825 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2826 return NULL;
2827 Py_BEGIN_ALLOW_THREADS
2828 dirp = fdopendir(fd);
2829 Py_END_ALLOW_THREADS
2830 if (dirp == NULL) {
2831 close(fd);
2832 return posix_error();
2833 }
2834 if ((d = PyList_New(0)) == NULL) {
2835 Py_BEGIN_ALLOW_THREADS
2836 closedir(dirp);
2837 Py_END_ALLOW_THREADS
2838 return NULL;
2839 }
2840 for (;;) {
2841 errno = 0;
2842 Py_BEGIN_ALLOW_THREADS
2843 ep = readdir(dirp);
2844 Py_END_ALLOW_THREADS
2845 if (ep == NULL) {
2846 if (errno == 0) {
2847 break;
2848 } else {
2849 Py_BEGIN_ALLOW_THREADS
2850 closedir(dirp);
2851 Py_END_ALLOW_THREADS
2852 Py_DECREF(d);
2853 return posix_error();
2854 }
2855 }
2856 if (ep->d_name[0] == '.' &&
2857 (NAMLEN(ep) == 1 ||
2858 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2859 continue;
2860 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2861 if (v == NULL) {
2862 Py_CLEAR(d);
2863 break;
2864 }
2865 if (PyList_Append(d, v) != 0) {
2866 Py_DECREF(v);
2867 Py_CLEAR(d);
2868 break;
2869 }
2870 Py_DECREF(v);
2871 }
2872 Py_BEGIN_ALLOW_THREADS
2873 closedir(dirp);
2874 Py_END_ALLOW_THREADS
2875
2876 return d;
2877}
2878#endif
2879
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002880#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002881/* A helper function for abspath on win32 */
2882static PyObject *
2883posix__getfullpathname(PyObject *self, PyObject *args)
2884{
Victor Stinner8c62be82010-05-06 00:08:46 +00002885 PyObject *opath;
2886 char *path;
2887 char outbuf[MAX_PATH*2];
2888 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002889#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002890 PyUnicodeObject *po;
2891 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2892 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2893 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2894 Py_UNICODE *wtemp;
2895 DWORD result;
2896 PyObject *v;
2897 result = GetFullPathNameW(wpath,
2898 sizeof(woutbuf)/sizeof(woutbuf[0]),
2899 woutbuf, &wtemp);
2900 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2901 woutbufp = malloc(result * sizeof(Py_UNICODE));
2902 if (!woutbufp)
2903 return PyErr_NoMemory();
2904 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2905 }
2906 if (result)
2907 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2908 else
2909 v = win32_error_unicode("GetFullPathNameW", wpath);
2910 if (woutbufp != woutbuf)
2911 free(woutbufp);
2912 return v;
2913 }
2914 /* Drop the argument parsing error as narrow strings
2915 are also valid. */
2916 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002917
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002918#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002919 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2920 PyUnicode_FSConverter, &opath))
2921 return NULL;
2922 path = PyBytes_AsString(opath);
2923 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2924 outbuf, &temp)) {
2925 win32_error("GetFullPathName", path);
2926 Py_DECREF(opath);
2927 return NULL;
2928 }
2929 Py_DECREF(opath);
2930 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2931 return PyUnicode_Decode(outbuf, strlen(outbuf),
2932 Py_FileSystemDefaultEncoding, NULL);
2933 }
2934 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002935} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002936
Brian Curtind25aef52011-06-13 15:16:04 -05002937
Brian Curtinf5e76d02010-11-24 13:14:05 +00002938
Brian Curtind40e6f72010-07-08 21:39:08 +00002939/* A helper function for samepath on windows */
2940static PyObject *
2941posix__getfinalpathname(PyObject *self, PyObject *args)
2942{
2943 HANDLE hFile;
2944 int buf_size;
2945 wchar_t *target_path;
2946 int result_length;
2947 PyObject *result;
2948 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002949
Brian Curtin94622b02010-09-24 00:03:39 +00002950 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002951 return NULL;
2952 }
2953
2954 if(!check_GetFinalPathNameByHandle()) {
2955 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2956 NotImplementedError. */
2957 return PyErr_Format(PyExc_NotImplementedError,
2958 "GetFinalPathNameByHandle not available on this platform");
2959 }
2960
2961 hFile = CreateFileW(
2962 path,
2963 0, /* desired access */
2964 0, /* share mode */
2965 NULL, /* security attributes */
2966 OPEN_EXISTING,
2967 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2968 FILE_FLAG_BACKUP_SEMANTICS,
2969 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002970
Brian Curtind40e6f72010-07-08 21:39:08 +00002971 if(hFile == INVALID_HANDLE_VALUE) {
2972 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002973 return PyErr_Format(PyExc_RuntimeError,
2974 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002975 }
2976
2977 /* We have a good handle to the target, use it to determine the
2978 target path name. */
2979 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2980
2981 if(!buf_size)
2982 return win32_error_unicode("GetFinalPathNameByHandle", path);
2983
2984 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2985 if(!target_path)
2986 return PyErr_NoMemory();
2987
2988 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2989 buf_size, VOLUME_NAME_DOS);
2990 if(!result_length)
2991 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2992
2993 if(!CloseHandle(hFile))
2994 return win32_error_unicode("GetFinalPathNameByHandle", path);
2995
2996 target_path[result_length] = 0;
2997 result = PyUnicode_FromUnicode(target_path, result_length);
2998 free(target_path);
2999 return result;
3000
3001} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003002
3003static PyObject *
3004posix__getfileinformation(PyObject *self, PyObject *args)
3005{
3006 HANDLE hFile;
3007 BY_HANDLE_FILE_INFORMATION info;
3008 int fd;
3009
3010 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3011 return NULL;
3012
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003013 if (!_PyVerify_fd(fd))
3014 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003015
3016 hFile = (HANDLE)_get_osfhandle(fd);
3017 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003018 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003019
3020 if (!GetFileInformationByHandle(hFile, &info))
3021 return win32_error("_getfileinformation", NULL);
3022
3023 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3024 info.nFileIndexHigh,
3025 info.nFileIndexLow);
3026}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003027
Brian Curtin95d028f2011-06-09 09:10:38 -05003028PyDoc_STRVAR(posix__isdir__doc__,
3029"Return true if the pathname refers to an existing directory.");
3030
Brian Curtin9c669cc2011-06-08 18:17:18 -05003031static PyObject *
3032posix__isdir(PyObject *self, PyObject *args)
3033{
3034 PyObject *opath;
3035 char *path;
3036 PyUnicodeObject *po;
3037 DWORD attributes;
3038
3039 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3040 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3041
3042 attributes = GetFileAttributesW(wpath);
3043 if (attributes == INVALID_FILE_ATTRIBUTES)
3044 Py_RETURN_FALSE;
3045 goto check;
3046 }
3047 /* Drop the argument parsing error as narrow strings
3048 are also valid. */
3049 PyErr_Clear();
3050
3051 if (!PyArg_ParseTuple(args, "O&:_isdir",
3052 PyUnicode_FSConverter, &opath))
3053 return NULL;
3054
3055 path = PyBytes_AsString(opath);
3056 attributes = GetFileAttributesA(path);
3057 if (attributes == INVALID_FILE_ATTRIBUTES)
3058 Py_RETURN_FALSE;
3059
3060check:
3061 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3062 Py_RETURN_TRUE;
3063 else
3064 Py_RETURN_FALSE;
3065}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003066#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003068PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003069"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003070Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003071
Barry Warsaw53699e91996-12-10 23:23:01 +00003072static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003073posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003074{
Victor Stinner8c62be82010-05-06 00:08:46 +00003075 int res;
3076 PyObject *opath;
3077 char *path;
3078 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003079
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003080#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003081 PyUnicodeObject *po;
3082 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3083 Py_BEGIN_ALLOW_THREADS
3084 /* PyUnicode_AS_UNICODE OK without thread lock as
3085 it is a simple dereference. */
3086 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3087 Py_END_ALLOW_THREADS
3088 if (!res)
3089 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3090 Py_INCREF(Py_None);
3091 return Py_None;
3092 }
3093 /* Drop the argument parsing error as narrow strings
3094 are also valid. */
3095 PyErr_Clear();
3096 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3097 PyUnicode_FSConverter, &opath, &mode))
3098 return NULL;
3099 path = PyBytes_AsString(opath);
3100 Py_BEGIN_ALLOW_THREADS
3101 /* PyUnicode_AS_UNICODE OK without thread lock as
3102 it is a simple dereference. */
3103 res = CreateDirectoryA(path, NULL);
3104 Py_END_ALLOW_THREADS
3105 if (!res) {
3106 win32_error("mkdir", path);
3107 Py_DECREF(opath);
3108 return NULL;
3109 }
3110 Py_DECREF(opath);
3111 Py_INCREF(Py_None);
3112 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003113#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003114
Victor Stinner8c62be82010-05-06 00:08:46 +00003115 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3116 PyUnicode_FSConverter, &opath, &mode))
3117 return NULL;
3118 path = PyBytes_AsString(opath);
3119 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003121 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003122#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003123 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003124#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003125 Py_END_ALLOW_THREADS
3126 if (res < 0)
3127 return posix_error_with_allocated_filename(opath);
3128 Py_DECREF(opath);
3129 Py_INCREF(Py_None);
3130 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003131#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003132}
3133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003134
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003135/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3136#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003137#include <sys/resource.h>
3138#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003139
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003140
3141#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003142PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003143"nice(inc) -> new_priority\n\n\
3144Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003145
Barry Warsaw53699e91996-12-10 23:23:01 +00003146static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003147posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003148{
Victor Stinner8c62be82010-05-06 00:08:46 +00003149 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003150
Victor Stinner8c62be82010-05-06 00:08:46 +00003151 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3152 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003153
Victor Stinner8c62be82010-05-06 00:08:46 +00003154 /* There are two flavours of 'nice': one that returns the new
3155 priority (as required by almost all standards out there) and the
3156 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3157 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003158
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 If we are of the nice family that returns the new priority, we
3160 need to clear errno before the call, and check if errno is filled
3161 before calling posix_error() on a returnvalue of -1, because the
3162 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003163
Victor Stinner8c62be82010-05-06 00:08:46 +00003164 errno = 0;
3165 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003166#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003167 if (value == 0)
3168 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003169#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003170 if (value == -1 && errno != 0)
3171 /* either nice() or getpriority() returned an error */
3172 return posix_error();
3173 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003174}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003175#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003176
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003177
3178#ifdef HAVE_GETPRIORITY
3179PyDoc_STRVAR(posix_getpriority__doc__,
3180"getpriority(which, who) -> current_priority\n\n\
3181Get program scheduling priority.");
3182
3183static PyObject *
3184posix_getpriority(PyObject *self, PyObject *args)
3185{
3186 int which, who, retval;
3187
3188 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3189 return NULL;
3190 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003191 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003192 if (errno != 0)
3193 return posix_error();
3194 return PyLong_FromLong((long)retval);
3195}
3196#endif /* HAVE_GETPRIORITY */
3197
3198
3199#ifdef HAVE_SETPRIORITY
3200PyDoc_STRVAR(posix_setpriority__doc__,
3201"setpriority(which, who, prio) -> None\n\n\
3202Set program scheduling priority.");
3203
3204static PyObject *
3205posix_setpriority(PyObject *self, PyObject *args)
3206{
3207 int which, who, prio, retval;
3208
3209 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3210 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003211 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003212 if (retval == -1)
3213 return posix_error();
3214 Py_RETURN_NONE;
3215}
3216#endif /* HAVE_SETPRIORITY */
3217
3218
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003219PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003220"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222
Barry Warsaw53699e91996-12-10 23:23:01 +00003223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003224posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003225{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003226#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003227 PyObject *o1, *o2;
3228 char *p1, *p2;
3229 BOOL result;
3230 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3231 goto error;
3232 if (!convert_to_unicode(&o1))
3233 goto error;
3234 if (!convert_to_unicode(&o2)) {
3235 Py_DECREF(o1);
3236 goto error;
3237 }
3238 Py_BEGIN_ALLOW_THREADS
3239 result = MoveFileW(PyUnicode_AsUnicode(o1),
3240 PyUnicode_AsUnicode(o2));
3241 Py_END_ALLOW_THREADS
3242 Py_DECREF(o1);
3243 Py_DECREF(o2);
3244 if (!result)
3245 return win32_error("rename", NULL);
3246 Py_INCREF(Py_None);
3247 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003248error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 PyErr_Clear();
3250 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3251 return NULL;
3252 Py_BEGIN_ALLOW_THREADS
3253 result = MoveFileA(p1, p2);
3254 Py_END_ALLOW_THREADS
3255 if (!result)
3256 return win32_error("rename", NULL);
3257 Py_INCREF(Py_None);
3258 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003259#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003260 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003261#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003262}
3263
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003264
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003265PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003266"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003267Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003268
Barry Warsaw53699e91996-12-10 23:23:01 +00003269static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003270posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003271{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003272#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003273 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003274#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003275 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003276#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003277}
3278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003280PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003281"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003282Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003283
Barry Warsaw53699e91996-12-10 23:23:01 +00003284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003285posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003286{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003287#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003288 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003289#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003290 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003291#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003292}
3293
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003294
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003295#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003296PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003297"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003298Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003299
Barry Warsaw53699e91996-12-10 23:23:01 +00003300static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003301posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003302{
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003304#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003305 wchar_t *command;
3306 if (!PyArg_ParseTuple(args, "u:system", &command))
3307 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003308
Victor Stinner8c62be82010-05-06 00:08:46 +00003309 Py_BEGIN_ALLOW_THREADS
3310 sts = _wsystem(command);
3311 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003312#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003313 PyObject *command_obj;
3314 char *command;
3315 if (!PyArg_ParseTuple(args, "O&:system",
3316 PyUnicode_FSConverter, &command_obj))
3317 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003318
Victor Stinner8c62be82010-05-06 00:08:46 +00003319 command = PyBytes_AsString(command_obj);
3320 Py_BEGIN_ALLOW_THREADS
3321 sts = system(command);
3322 Py_END_ALLOW_THREADS
3323 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003324#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003325 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003326}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003327#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003328
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003330PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003331"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003332Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003333
Barry Warsaw53699e91996-12-10 23:23:01 +00003334static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003335posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003336{
Victor Stinner8c62be82010-05-06 00:08:46 +00003337 int i;
3338 if (!PyArg_ParseTuple(args, "i:umask", &i))
3339 return NULL;
3340 i = (int)umask(i);
3341 if (i < 0)
3342 return posix_error();
3343 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003344}
3345
Brian Curtind40e6f72010-07-08 21:39:08 +00003346#ifdef MS_WINDOWS
3347
3348/* override the default DeleteFileW behavior so that directory
3349symlinks can be removed with this function, the same as with
3350Unix symlinks */
3351BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3352{
3353 WIN32_FILE_ATTRIBUTE_DATA info;
3354 WIN32_FIND_DATAW find_data;
3355 HANDLE find_data_handle;
3356 int is_directory = 0;
3357 int is_link = 0;
3358
3359 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3360 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003361
Brian Curtind40e6f72010-07-08 21:39:08 +00003362 /* Get WIN32_FIND_DATA structure for the path to determine if
3363 it is a symlink */
3364 if(is_directory &&
3365 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3366 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3367
3368 if(find_data_handle != INVALID_HANDLE_VALUE) {
3369 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3370 FindClose(find_data_handle);
3371 }
3372 }
3373 }
3374
3375 if (is_directory && is_link)
3376 return RemoveDirectoryW(lpFileName);
3377
3378 return DeleteFileW(lpFileName);
3379}
3380#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003382PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003383"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003384Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003385
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003386PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003387"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003388Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003389
Barry Warsaw53699e91996-12-10 23:23:01 +00003390static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003391posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003392{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003393#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003394 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3395 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003396#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003397 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003398#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003399}
3400
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003401
Guido van Rossumb6775db1994-08-01 11:34:53 +00003402#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003403PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003404"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003405Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003406
Barry Warsaw53699e91996-12-10 23:23:01 +00003407static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003408posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003409{
Victor Stinner8c62be82010-05-06 00:08:46 +00003410 struct utsname u;
3411 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003412
Victor Stinner8c62be82010-05-06 00:08:46 +00003413 Py_BEGIN_ALLOW_THREADS
3414 res = uname(&u);
3415 Py_END_ALLOW_THREADS
3416 if (res < 0)
3417 return posix_error();
3418 return Py_BuildValue("(sssss)",
3419 u.sysname,
3420 u.nodename,
3421 u.release,
3422 u.version,
3423 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003424}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003425#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003426
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003427
3428/*
3429 * Classic POSIX utime functions supported microseconds (1m/sec).
3430 * Newer POSIX functions support nanoseconds (1 billion per sec).
3431 * posixmodule now uses the new functions where possible.
3432 * This improves accuracy in many situations, for example shutil.copy2().
3433 *
3434 * The implementation isn't currently sophisticated enough to handle
3435 * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false.
3436 * Specifically, posix_futimes() would break.
3437 *
3438 * Supporting such a platform wouldn't be impossible; you'd need two
3439 * extract_time() functions, or make its precision a parameter.
3440 * Since such a platform seems unlikely we haven't bothered.
3441 */
3442#if defined(HAVE_UTIMENSAT)
3443#define EXTRACT_TIME_PRECISION (1e9)
3444#if !defined(HAVE_FUTIMENS)
3445#error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment.
3446#endif
3447#else
3448#define EXTRACT_TIME_PRECISION (1e6)
3449#endif
3450
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003451static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003452extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003453{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003454 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 if (PyFloat_Check(t)) {
3456 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003457 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003458 if (!intobj)
3459 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003460#if SIZEOF_TIME_T > SIZEOF_LONG
3461 intval = PyLong_AsUnsignedLongLongMask(intobj);
3462#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003464#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003465 Py_DECREF(intobj);
3466 if (intval == -1 && PyErr_Occurred())
3467 return -1;
3468 *sec = intval;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003469
3470 *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION);
Victor Stinner8c62be82010-05-06 00:08:46 +00003471 if (*usec < 0)
3472 /* If rounding gave us a negative number,
3473 truncate. */
3474 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003475 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003476 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003477#if SIZEOF_TIME_T > SIZEOF_LONG
3478 intval = PyLong_AsUnsignedLongLongMask(t);
3479#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003480 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003481#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003482 if (intval == -1 && PyErr_Occurred())
3483 return -1;
3484 *sec = intval;
3485 *usec = 0;
3486 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003487}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003489PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003490"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003491utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003492Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003493second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003494
Barry Warsaw53699e91996-12-10 23:23:01 +00003495static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003496posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003497{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003498#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 PyObject *arg;
3500 PyUnicodeObject *obwpath;
3501 wchar_t *wpath = NULL;
3502 PyObject *oapath;
3503 char *apath;
3504 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003505 time_t atimesec, mtimesec;
3506 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003507 FILETIME atime, mtime;
3508 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003509
Victor Stinner8c62be82010-05-06 00:08:46 +00003510 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3511 wpath = PyUnicode_AS_UNICODE(obwpath);
3512 Py_BEGIN_ALLOW_THREADS
3513 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3514 NULL, OPEN_EXISTING,
3515 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3516 Py_END_ALLOW_THREADS
3517 if (hFile == INVALID_HANDLE_VALUE)
3518 return win32_error_unicode("utime", wpath);
3519 } else
3520 /* Drop the argument parsing error as narrow strings
3521 are also valid. */
3522 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003523
Victor Stinner8c62be82010-05-06 00:08:46 +00003524 if (!wpath) {
3525 if (!PyArg_ParseTuple(args, "O&O:utime",
3526 PyUnicode_FSConverter, &oapath, &arg))
3527 return NULL;
3528 apath = PyBytes_AsString(oapath);
3529 Py_BEGIN_ALLOW_THREADS
3530 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3531 NULL, OPEN_EXISTING,
3532 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3533 Py_END_ALLOW_THREADS
3534 if (hFile == INVALID_HANDLE_VALUE) {
3535 win32_error("utime", apath);
3536 Py_DECREF(oapath);
3537 return NULL;
3538 }
3539 Py_DECREF(oapath);
3540 }
3541
3542 if (arg == Py_None) {
3543 SYSTEMTIME now;
3544 GetSystemTime(&now);
3545 if (!SystemTimeToFileTime(&now, &mtime) ||
3546 !SystemTimeToFileTime(&now, &atime)) {
3547 win32_error("utime", NULL);
3548 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003549 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003550 }
3551 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3552 PyErr_SetString(PyExc_TypeError,
3553 "utime() arg 2 must be a tuple (atime, mtime)");
3554 goto done;
3555 }
3556 else {
3557 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3558 &atimesec, &ausec) == -1)
3559 goto done;
3560 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3561 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3562 &mtimesec, &musec) == -1)
3563 goto done;
3564 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3565 }
3566 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3567 /* Avoid putting the file name into the error here,
3568 as that may confuse the user into believing that
3569 something is wrong with the file, when it also
3570 could be the time stamp that gives a problem. */
3571 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003572 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003573 }
3574 Py_INCREF(Py_None);
3575 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003576done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003577 CloseHandle(hFile);
3578 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003579#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003580
Victor Stinner8c62be82010-05-06 00:08:46 +00003581 PyObject *opath;
3582 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003583 time_t atime, mtime;
3584 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003585 int res;
3586 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003587
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 if (!PyArg_ParseTuple(args, "O&O:utime",
3589 PyUnicode_FSConverter, &opath, &arg))
3590 return NULL;
3591 path = PyBytes_AsString(opath);
3592 if (arg == Py_None) {
3593 /* optional time values not given */
3594 Py_BEGIN_ALLOW_THREADS
3595 res = utime(path, NULL);
3596 Py_END_ALLOW_THREADS
3597 }
3598 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3599 PyErr_SetString(PyExc_TypeError,
3600 "utime() arg 2 must be a tuple (atime, mtime)");
3601 Py_DECREF(opath);
3602 return NULL;
3603 }
3604 else {
3605 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3606 &atime, &ausec) == -1) {
3607 Py_DECREF(opath);
3608 return NULL;
3609 }
3610 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3611 &mtime, &musec) == -1) {
3612 Py_DECREF(opath);
3613 return NULL;
3614 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003615
3616 Py_BEGIN_ALLOW_THREADS
3617 {
3618#ifdef HAVE_UTIMENSAT
3619 struct timespec buf[2];
3620 buf[0].tv_sec = atime;
3621 buf[0].tv_nsec = ausec;
3622 buf[1].tv_sec = mtime;
3623 buf[1].tv_nsec = musec;
3624 res = utimensat(AT_FDCWD, path, buf, 0);
3625#elif defined(HAVE_UTIMES)
3626 struct timeval buf[2];
3627 buf[0].tv_sec = atime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003628 buf[0].tv_usec = ausec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003629 buf[1].tv_sec = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003630 buf[1].tv_usec = musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003631 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003632#elif defined(HAVE_UTIME_H)
3633 /* XXX should define struct utimbuf instead, above */
3634 struct utimbuf buf;
3635 buf.actime = atime;
3636 buf.modtime = mtime;
3637 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003638#else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003639 time_t buf[2];
3640 buf[0] = atime;
3641 buf[1] = mtime;
3642 res = utime(path, buf);
3643#endif
3644 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003645 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003646 }
3647 if (res < 0) {
3648 return posix_error_with_allocated_filename(opath);
3649 }
3650 Py_DECREF(opath);
3651 Py_INCREF(Py_None);
3652 return Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003653#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003654#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003655}
3656
Ross Lagerwall7807c352011-03-17 20:20:30 +02003657#ifdef HAVE_FUTIMES
3658PyDoc_STRVAR(posix_futimes__doc__,
3659"futimes(fd, (atime, mtime))\n\
3660futimes(fd, None)\n\n\
3661Set the access and modified time of the file specified by the file\n\
3662descriptor fd to the given values. If the second form is used, set the\n\
3663access and modified times to the current time.");
3664
3665static PyObject *
3666posix_futimes(PyObject *self, PyObject *args)
3667{
3668 int res, fd;
3669 PyObject* arg;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003670 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003671 long ausec, musec;
3672
3673 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3674 return NULL;
3675
3676 if (arg == Py_None) {
3677 /* optional time values not given */
3678 Py_BEGIN_ALLOW_THREADS
3679 res = futimes(fd, NULL);
3680 Py_END_ALLOW_THREADS
3681 }
3682 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3683 PyErr_SetString(PyExc_TypeError,
3684 "futimes() arg 2 must be a tuple (atime, mtime)");
3685 return NULL;
3686 }
3687 else {
3688 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003689 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003690 return NULL;
3691 }
3692 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003693 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003694 return NULL;
3695 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003696 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003697 {
3698#ifdef HAVE_FUTIMENS
3699 struct timespec buf[2];
3700 buf[0].tv_sec = atime;
3701 buf[0].tv_nsec = ausec;
3702 buf[1].tv_sec = mtime;
3703 buf[1].tv_nsec = musec;
3704 res = futimens(fd, buf);
3705#else
3706 struct timeval buf[2];
3707 buf[0].tv_sec = atime;
3708 buf[0].tv_usec = ausec;
3709 buf[1].tv_sec = mtime;
3710 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003711 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003712#endif
3713 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003714 Py_END_ALLOW_THREADS
3715 }
3716 if (res < 0)
3717 return posix_error();
3718 Py_RETURN_NONE;
3719}
3720#endif
3721
3722#ifdef HAVE_LUTIMES
3723PyDoc_STRVAR(posix_lutimes__doc__,
3724"lutimes(path, (atime, mtime))\n\
3725lutimes(path, None)\n\n\
3726Like utime(), but if path is a symbolic link, it is not dereferenced.");
3727
3728static PyObject *
3729posix_lutimes(PyObject *self, PyObject *args)
3730{
3731 PyObject *opath, *arg;
3732 const char *path;
3733 int res;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003734 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003735 long ausec, musec;
3736
3737 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3738 PyUnicode_FSConverter, &opath, &arg))
3739 return NULL;
3740 path = PyBytes_AsString(opath);
3741 if (arg == Py_None) {
3742 /* optional time values not given */
3743 Py_BEGIN_ALLOW_THREADS
3744 res = lutimes(path, NULL);
3745 Py_END_ALLOW_THREADS
3746 }
3747 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3748 PyErr_SetString(PyExc_TypeError,
3749 "lutimes() arg 2 must be a tuple (atime, mtime)");
3750 Py_DECREF(opath);
3751 return NULL;
3752 }
3753 else {
3754 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003755 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003756 Py_DECREF(opath);
3757 return NULL;
3758 }
3759 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003760 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003761 Py_DECREF(opath);
3762 return NULL;
3763 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003764 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003765 {
3766#ifdef HAVE_UTIMENSAT
3767 struct timespec buf[2];
3768 buf[0].tv_sec = atime;
3769 buf[0].tv_nsec = ausec;
3770 buf[1].tv_sec = mtime;
3771 buf[1].tv_nsec = musec;
3772 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
3773#else
3774 struct timeval buf[2];
3775 buf[0].tv_sec = atime;
3776 buf[0].tv_usec = ausec;
3777 buf[1].tv_sec = mtime;
3778 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003779 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003780#endif
3781 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003782 Py_END_ALLOW_THREADS
3783 }
3784 Py_DECREF(opath);
3785 if (res < 0)
3786 return posix_error();
3787 Py_RETURN_NONE;
3788}
3789#endif
3790
3791#ifdef HAVE_FUTIMENS
3792PyDoc_STRVAR(posix_futimens__doc__,
3793"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3794futimens(fd, None, None)\n\n\
3795Updates the timestamps of a file specified by the file descriptor fd, with\n\
3796nanosecond precision.\n\
3797The second form sets atime and mtime to the current time.\n\
3798If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3799current time.\n\
3800If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3801
3802static PyObject *
3803posix_futimens(PyObject *self, PyObject *args)
3804{
3805 int res, fd;
3806 PyObject *atime, *mtime;
3807 struct timespec buf[2];
3808
3809 if (!PyArg_ParseTuple(args, "iOO:futimens",
3810 &fd, &atime, &mtime))
3811 return NULL;
3812 if (atime == Py_None && mtime == Py_None) {
3813 /* optional time values not given */
3814 Py_BEGIN_ALLOW_THREADS
3815 res = futimens(fd, NULL);
3816 Py_END_ALLOW_THREADS
3817 }
3818 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3819 PyErr_SetString(PyExc_TypeError,
3820 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3821 return NULL;
3822 }
3823 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3824 PyErr_SetString(PyExc_TypeError,
3825 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3826 return NULL;
3827 }
3828 else {
3829 if (!PyArg_ParseTuple(atime, "ll:futimens",
3830 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3831 return NULL;
3832 }
3833 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3834 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3835 return NULL;
3836 }
3837 Py_BEGIN_ALLOW_THREADS
3838 res = futimens(fd, buf);
3839 Py_END_ALLOW_THREADS
3840 }
3841 if (res < 0)
3842 return posix_error();
3843 Py_RETURN_NONE;
3844}
3845#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003846
Guido van Rossum3b066191991-06-04 19:40:25 +00003847/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003849PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003850"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003851Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003852
Barry Warsaw53699e91996-12-10 23:23:01 +00003853static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003854posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003855{
Victor Stinner8c62be82010-05-06 00:08:46 +00003856 int sts;
3857 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3858 return NULL;
3859 _exit(sts);
3860 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003861}
3862
Martin v. Löwis114619e2002-10-07 06:44:21 +00003863#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3864static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003865free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003866{
Victor Stinner8c62be82010-05-06 00:08:46 +00003867 Py_ssize_t i;
3868 for (i = 0; i < count; i++)
3869 PyMem_Free(array[i]);
3870 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003871}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003872
Antoine Pitrou69f71142009-05-24 21:25:49 +00003873static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003874int fsconvert_strdup(PyObject *o, char**out)
3875{
Victor Stinner8c62be82010-05-06 00:08:46 +00003876 PyObject *bytes;
3877 Py_ssize_t size;
3878 if (!PyUnicode_FSConverter(o, &bytes))
3879 return 0;
3880 size = PyBytes_GET_SIZE(bytes);
3881 *out = PyMem_Malloc(size+1);
3882 if (!*out)
3883 return 0;
3884 memcpy(*out, PyBytes_AsString(bytes), size+1);
3885 Py_DECREF(bytes);
3886 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003887}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003888#endif
3889
Ross Lagerwall7807c352011-03-17 20:20:30 +02003890#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003891static char**
3892parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3893{
Victor Stinner8c62be82010-05-06 00:08:46 +00003894 char **envlist;
3895 Py_ssize_t i, pos, envc;
3896 PyObject *keys=NULL, *vals=NULL;
3897 PyObject *key, *val, *key2, *val2;
3898 char *p, *k, *v;
3899 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003900
Victor Stinner8c62be82010-05-06 00:08:46 +00003901 i = PyMapping_Size(env);
3902 if (i < 0)
3903 return NULL;
3904 envlist = PyMem_NEW(char *, i + 1);
3905 if (envlist == NULL) {
3906 PyErr_NoMemory();
3907 return NULL;
3908 }
3909 envc = 0;
3910 keys = PyMapping_Keys(env);
3911 vals = PyMapping_Values(env);
3912 if (!keys || !vals)
3913 goto error;
3914 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3915 PyErr_Format(PyExc_TypeError,
3916 "env.keys() or env.values() is not a list");
3917 goto error;
3918 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003919
Victor Stinner8c62be82010-05-06 00:08:46 +00003920 for (pos = 0; pos < i; pos++) {
3921 key = PyList_GetItem(keys, pos);
3922 val = PyList_GetItem(vals, pos);
3923 if (!key || !val)
3924 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003925
Victor Stinner8c62be82010-05-06 00:08:46 +00003926 if (PyUnicode_FSConverter(key, &key2) == 0)
3927 goto error;
3928 if (PyUnicode_FSConverter(val, &val2) == 0) {
3929 Py_DECREF(key2);
3930 goto error;
3931 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003932
3933#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003934 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3935 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003936#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003937 k = PyBytes_AsString(key2);
3938 v = PyBytes_AsString(val2);
3939 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003940
Victor Stinner8c62be82010-05-06 00:08:46 +00003941 p = PyMem_NEW(char, len);
3942 if (p == NULL) {
3943 PyErr_NoMemory();
3944 Py_DECREF(key2);
3945 Py_DECREF(val2);
3946 goto error;
3947 }
3948 PyOS_snprintf(p, len, "%s=%s", k, v);
3949 envlist[envc++] = p;
3950 Py_DECREF(key2);
3951 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003952#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003953 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003954#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003955 }
3956 Py_DECREF(vals);
3957 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003958
Victor Stinner8c62be82010-05-06 00:08:46 +00003959 envlist[envc] = 0;
3960 *envc_ptr = envc;
3961 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003962
3963error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003964 Py_XDECREF(keys);
3965 Py_XDECREF(vals);
3966 while (--envc >= 0)
3967 PyMem_DEL(envlist[envc]);
3968 PyMem_DEL(envlist);
3969 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003970}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003971
Ross Lagerwall7807c352011-03-17 20:20:30 +02003972static char**
3973parse_arglist(PyObject* argv, Py_ssize_t *argc)
3974{
3975 int i;
3976 char **argvlist = PyMem_NEW(char *, *argc+1);
3977 if (argvlist == NULL) {
3978 PyErr_NoMemory();
3979 return NULL;
3980 }
3981 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003982 PyObject* item = PySequence_ITEM(argv, i);
3983 if (item == NULL)
3984 goto fail;
3985 if (!fsconvert_strdup(item, &argvlist[i])) {
3986 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003987 goto fail;
3988 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003989 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003990 }
3991 argvlist[*argc] = NULL;
3992 return argvlist;
3993fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003994 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003995 free_string_array(argvlist, *argc);
3996 return NULL;
3997}
3998#endif
3999
4000#ifdef HAVE_EXECV
4001PyDoc_STRVAR(posix_execv__doc__,
4002"execv(path, args)\n\n\
4003Execute an executable path with arguments, replacing current process.\n\
4004\n\
4005 path: path of executable file\n\
4006 args: tuple or list of strings");
4007
4008static PyObject *
4009posix_execv(PyObject *self, PyObject *args)
4010{
4011 PyObject *opath;
4012 char *path;
4013 PyObject *argv;
4014 char **argvlist;
4015 Py_ssize_t argc;
4016
4017 /* execv has two arguments: (path, argv), where
4018 argv is a list or tuple of strings. */
4019
4020 if (!PyArg_ParseTuple(args, "O&O:execv",
4021 PyUnicode_FSConverter,
4022 &opath, &argv))
4023 return NULL;
4024 path = PyBytes_AsString(opath);
4025 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4026 PyErr_SetString(PyExc_TypeError,
4027 "execv() arg 2 must be a tuple or list");
4028 Py_DECREF(opath);
4029 return NULL;
4030 }
4031 argc = PySequence_Size(argv);
4032 if (argc < 1) {
4033 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4034 Py_DECREF(opath);
4035 return NULL;
4036 }
4037
4038 argvlist = parse_arglist(argv, &argc);
4039 if (argvlist == NULL) {
4040 Py_DECREF(opath);
4041 return NULL;
4042 }
4043
4044 execv(path, argvlist);
4045
4046 /* If we get here it's definitely an error */
4047
4048 free_string_array(argvlist, argc);
4049 Py_DECREF(opath);
4050 return posix_error();
4051}
4052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004053PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004054"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004055Execute a path with arguments and environment, replacing current process.\n\
4056\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004057 path: path of executable file\n\
4058 args: tuple or list of arguments\n\
4059 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004060
Barry Warsaw53699e91996-12-10 23:23:01 +00004061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004062posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004063{
Victor Stinner8c62be82010-05-06 00:08:46 +00004064 PyObject *opath;
4065 char *path;
4066 PyObject *argv, *env;
4067 char **argvlist;
4068 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004069 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004070
Victor Stinner8c62be82010-05-06 00:08:46 +00004071 /* execve has three arguments: (path, argv, env), where
4072 argv is a list or tuple of strings and env is a dictionary
4073 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004074
Victor Stinner8c62be82010-05-06 00:08:46 +00004075 if (!PyArg_ParseTuple(args, "O&OO:execve",
4076 PyUnicode_FSConverter,
4077 &opath, &argv, &env))
4078 return NULL;
4079 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004080 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004081 PyErr_SetString(PyExc_TypeError,
4082 "execve() arg 2 must be a tuple or list");
4083 goto fail_0;
4084 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004085 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 if (!PyMapping_Check(env)) {
4087 PyErr_SetString(PyExc_TypeError,
4088 "execve() arg 3 must be a mapping object");
4089 goto fail_0;
4090 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004091
Ross Lagerwall7807c352011-03-17 20:20:30 +02004092 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004093 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004094 goto fail_0;
4095 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004096
Victor Stinner8c62be82010-05-06 00:08:46 +00004097 envlist = parse_envlist(env, &envc);
4098 if (envlist == NULL)
4099 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004100
Victor Stinner8c62be82010-05-06 00:08:46 +00004101 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004102
Victor Stinner8c62be82010-05-06 00:08:46 +00004103 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004104
Victor Stinner8c62be82010-05-06 00:08:46 +00004105 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004106
Victor Stinner8c62be82010-05-06 00:08:46 +00004107 while (--envc >= 0)
4108 PyMem_DEL(envlist[envc]);
4109 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004110 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004111 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004112 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004113 Py_DECREF(opath);
4114 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004115}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004116#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004117
Ross Lagerwall7807c352011-03-17 20:20:30 +02004118#ifdef HAVE_FEXECVE
4119PyDoc_STRVAR(posix_fexecve__doc__,
4120"fexecve(fd, args, env)\n\n\
4121Execute the program specified by a file descriptor with arguments and\n\
4122environment, replacing the current process.\n\
4123\n\
4124 fd: file descriptor of executable\n\
4125 args: tuple or list of arguments\n\
4126 env: dictionary of strings mapping to strings");
4127
4128static PyObject *
4129posix_fexecve(PyObject *self, PyObject *args)
4130{
4131 int fd;
4132 PyObject *argv, *env;
4133 char **argvlist;
4134 char **envlist;
4135 Py_ssize_t argc, envc;
4136
4137 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4138 &fd, &argv, &env))
4139 return NULL;
4140 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4141 PyErr_SetString(PyExc_TypeError,
4142 "fexecve() arg 2 must be a tuple or list");
4143 return NULL;
4144 }
4145 argc = PySequence_Size(argv);
4146 if (!PyMapping_Check(env)) {
4147 PyErr_SetString(PyExc_TypeError,
4148 "fexecve() arg 3 must be a mapping object");
4149 return NULL;
4150 }
4151
4152 argvlist = parse_arglist(argv, &argc);
4153 if (argvlist == NULL)
4154 return NULL;
4155
4156 envlist = parse_envlist(env, &envc);
4157 if (envlist == NULL)
4158 goto fail;
4159
4160 fexecve(fd, argvlist, envlist);
4161
4162 /* If we get here it's definitely an error */
4163
4164 (void) posix_error();
4165
4166 while (--envc >= 0)
4167 PyMem_DEL(envlist[envc]);
4168 PyMem_DEL(envlist);
4169 fail:
4170 free_string_array(argvlist, argc);
4171 return NULL;
4172}
4173#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004174
Guido van Rossuma1065681999-01-25 23:20:23 +00004175#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004176PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004177"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004178Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004179\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 mode: mode of process creation\n\
4181 path: path of executable file\n\
4182 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004183
4184static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004185posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004186{
Victor Stinner8c62be82010-05-06 00:08:46 +00004187 PyObject *opath;
4188 char *path;
4189 PyObject *argv;
4190 char **argvlist;
4191 int mode, i;
4192 Py_ssize_t argc;
4193 Py_intptr_t spawnval;
4194 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004195
Victor Stinner8c62be82010-05-06 00:08:46 +00004196 /* spawnv has three arguments: (mode, path, argv), where
4197 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004198
Victor Stinner8c62be82010-05-06 00:08:46 +00004199 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4200 PyUnicode_FSConverter,
4201 &opath, &argv))
4202 return NULL;
4203 path = PyBytes_AsString(opath);
4204 if (PyList_Check(argv)) {
4205 argc = PyList_Size(argv);
4206 getitem = PyList_GetItem;
4207 }
4208 else if (PyTuple_Check(argv)) {
4209 argc = PyTuple_Size(argv);
4210 getitem = PyTuple_GetItem;
4211 }
4212 else {
4213 PyErr_SetString(PyExc_TypeError,
4214 "spawnv() arg 2 must be a tuple or list");
4215 Py_DECREF(opath);
4216 return NULL;
4217 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004218
Victor Stinner8c62be82010-05-06 00:08:46 +00004219 argvlist = PyMem_NEW(char *, argc+1);
4220 if (argvlist == NULL) {
4221 Py_DECREF(opath);
4222 return PyErr_NoMemory();
4223 }
4224 for (i = 0; i < argc; i++) {
4225 if (!fsconvert_strdup((*getitem)(argv, i),
4226 &argvlist[i])) {
4227 free_string_array(argvlist, i);
4228 PyErr_SetString(
4229 PyExc_TypeError,
4230 "spawnv() arg 2 must contain only strings");
4231 Py_DECREF(opath);
4232 return NULL;
4233 }
4234 }
4235 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004236
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004237#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 Py_BEGIN_ALLOW_THREADS
4239 spawnval = spawnv(mode, path, argvlist);
4240 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004241#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004242 if (mode == _OLD_P_OVERLAY)
4243 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004244
Victor Stinner8c62be82010-05-06 00:08:46 +00004245 Py_BEGIN_ALLOW_THREADS
4246 spawnval = _spawnv(mode, path, argvlist);
4247 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004248#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004249
Victor Stinner8c62be82010-05-06 00:08:46 +00004250 free_string_array(argvlist, argc);
4251 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004252
Victor Stinner8c62be82010-05-06 00:08:46 +00004253 if (spawnval == -1)
4254 return posix_error();
4255 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004256#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004257 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004258#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004259 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004260#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004261}
4262
4263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004264PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004265"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004266Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004267\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004268 mode: mode of process creation\n\
4269 path: path of executable file\n\
4270 args: tuple or list of arguments\n\
4271 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004272
4273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004274posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004275{
Victor Stinner8c62be82010-05-06 00:08:46 +00004276 PyObject *opath;
4277 char *path;
4278 PyObject *argv, *env;
4279 char **argvlist;
4280 char **envlist;
4281 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004282 int mode;
4283 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004284 Py_intptr_t spawnval;
4285 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4286 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004287
Victor Stinner8c62be82010-05-06 00:08:46 +00004288 /* spawnve has four arguments: (mode, path, argv, env), where
4289 argv is a list or tuple of strings and env is a dictionary
4290 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004291
Victor Stinner8c62be82010-05-06 00:08:46 +00004292 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4293 PyUnicode_FSConverter,
4294 &opath, &argv, &env))
4295 return NULL;
4296 path = PyBytes_AsString(opath);
4297 if (PyList_Check(argv)) {
4298 argc = PyList_Size(argv);
4299 getitem = PyList_GetItem;
4300 }
4301 else if (PyTuple_Check(argv)) {
4302 argc = PyTuple_Size(argv);
4303 getitem = PyTuple_GetItem;
4304 }
4305 else {
4306 PyErr_SetString(PyExc_TypeError,
4307 "spawnve() arg 2 must be a tuple or list");
4308 goto fail_0;
4309 }
4310 if (!PyMapping_Check(env)) {
4311 PyErr_SetString(PyExc_TypeError,
4312 "spawnve() arg 3 must be a mapping object");
4313 goto fail_0;
4314 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004315
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 argvlist = PyMem_NEW(char *, argc+1);
4317 if (argvlist == NULL) {
4318 PyErr_NoMemory();
4319 goto fail_0;
4320 }
4321 for (i = 0; i < argc; i++) {
4322 if (!fsconvert_strdup((*getitem)(argv, i),
4323 &argvlist[i]))
4324 {
4325 lastarg = i;
4326 goto fail_1;
4327 }
4328 }
4329 lastarg = argc;
4330 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004331
Victor Stinner8c62be82010-05-06 00:08:46 +00004332 envlist = parse_envlist(env, &envc);
4333 if (envlist == NULL)
4334 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004335
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004336#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004337 Py_BEGIN_ALLOW_THREADS
4338 spawnval = spawnve(mode, path, argvlist, envlist);
4339 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004340#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004341 if (mode == _OLD_P_OVERLAY)
4342 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004343
Victor Stinner8c62be82010-05-06 00:08:46 +00004344 Py_BEGIN_ALLOW_THREADS
4345 spawnval = _spawnve(mode, path, argvlist, envlist);
4346 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004347#endif
Tim Peters25059d32001-12-07 20:35:43 +00004348
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 if (spawnval == -1)
4350 (void) posix_error();
4351 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004352#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004353 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004354#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004355 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004356#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004357
Victor Stinner8c62be82010-05-06 00:08:46 +00004358 while (--envc >= 0)
4359 PyMem_DEL(envlist[envc]);
4360 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004361 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004362 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004363 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004364 Py_DECREF(opath);
4365 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004366}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004367
4368/* OS/2 supports spawnvp & spawnvpe natively */
4369#if defined(PYOS_OS2)
4370PyDoc_STRVAR(posix_spawnvp__doc__,
4371"spawnvp(mode, file, args)\n\n\
4372Execute the program 'file' in a new process, using the environment\n\
4373search path to find the file.\n\
4374\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 mode: mode of process creation\n\
4376 file: executable file name\n\
4377 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004378
4379static PyObject *
4380posix_spawnvp(PyObject *self, PyObject *args)
4381{
Victor Stinner8c62be82010-05-06 00:08:46 +00004382 PyObject *opath;
4383 char *path;
4384 PyObject *argv;
4385 char **argvlist;
4386 int mode, i, argc;
4387 Py_intptr_t spawnval;
4388 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004389
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 /* spawnvp has three arguments: (mode, path, argv), where
4391 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004392
Victor Stinner8c62be82010-05-06 00:08:46 +00004393 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4394 PyUnicode_FSConverter,
4395 &opath, &argv))
4396 return NULL;
4397 path = PyBytes_AsString(opath);
4398 if (PyList_Check(argv)) {
4399 argc = PyList_Size(argv);
4400 getitem = PyList_GetItem;
4401 }
4402 else if (PyTuple_Check(argv)) {
4403 argc = PyTuple_Size(argv);
4404 getitem = PyTuple_GetItem;
4405 }
4406 else {
4407 PyErr_SetString(PyExc_TypeError,
4408 "spawnvp() arg 2 must be a tuple or list");
4409 Py_DECREF(opath);
4410 return NULL;
4411 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004412
Victor Stinner8c62be82010-05-06 00:08:46 +00004413 argvlist = PyMem_NEW(char *, argc+1);
4414 if (argvlist == NULL) {
4415 Py_DECREF(opath);
4416 return PyErr_NoMemory();
4417 }
4418 for (i = 0; i < argc; i++) {
4419 if (!fsconvert_strdup((*getitem)(argv, i),
4420 &argvlist[i])) {
4421 free_string_array(argvlist, i);
4422 PyErr_SetString(
4423 PyExc_TypeError,
4424 "spawnvp() arg 2 must contain only strings");
4425 Py_DECREF(opath);
4426 return NULL;
4427 }
4428 }
4429 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004430
Victor Stinner8c62be82010-05-06 00:08:46 +00004431 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004432#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004433 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004434#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004435 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004436#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004437 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004438
Victor Stinner8c62be82010-05-06 00:08:46 +00004439 free_string_array(argvlist, argc);
4440 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004441
Victor Stinner8c62be82010-05-06 00:08:46 +00004442 if (spawnval == -1)
4443 return posix_error();
4444 else
4445 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004446}
4447
4448
4449PyDoc_STRVAR(posix_spawnvpe__doc__,
4450"spawnvpe(mode, file, args, env)\n\n\
4451Execute the program 'file' in a new process, using the environment\n\
4452search path to find the file.\n\
4453\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004454 mode: mode of process creation\n\
4455 file: executable file name\n\
4456 args: tuple or list of arguments\n\
4457 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004458
4459static PyObject *
4460posix_spawnvpe(PyObject *self, PyObject *args)
4461{
Victor Stinner8c62be82010-05-06 00:08:46 +00004462 PyObject *opath
4463 char *path;
4464 PyObject *argv, *env;
4465 char **argvlist;
4466 char **envlist;
4467 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004468 int mode;
4469 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004470 Py_intptr_t spawnval;
4471 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4472 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004473
Victor Stinner8c62be82010-05-06 00:08:46 +00004474 /* spawnvpe has four arguments: (mode, path, argv, env), where
4475 argv is a list or tuple of strings and env is a dictionary
4476 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004477
Victor Stinner8c62be82010-05-06 00:08:46 +00004478 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4479 PyUnicode_FSConverter,
4480 &opath, &argv, &env))
4481 return NULL;
4482 path = PyBytes_AsString(opath);
4483 if (PyList_Check(argv)) {
4484 argc = PyList_Size(argv);
4485 getitem = PyList_GetItem;
4486 }
4487 else if (PyTuple_Check(argv)) {
4488 argc = PyTuple_Size(argv);
4489 getitem = PyTuple_GetItem;
4490 }
4491 else {
4492 PyErr_SetString(PyExc_TypeError,
4493 "spawnvpe() arg 2 must be a tuple or list");
4494 goto fail_0;
4495 }
4496 if (!PyMapping_Check(env)) {
4497 PyErr_SetString(PyExc_TypeError,
4498 "spawnvpe() arg 3 must be a mapping object");
4499 goto fail_0;
4500 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004501
Victor Stinner8c62be82010-05-06 00:08:46 +00004502 argvlist = PyMem_NEW(char *, argc+1);
4503 if (argvlist == NULL) {
4504 PyErr_NoMemory();
4505 goto fail_0;
4506 }
4507 for (i = 0; i < argc; i++) {
4508 if (!fsconvert_strdup((*getitem)(argv, i),
4509 &argvlist[i]))
4510 {
4511 lastarg = i;
4512 goto fail_1;
4513 }
4514 }
4515 lastarg = argc;
4516 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004517
Victor Stinner8c62be82010-05-06 00:08:46 +00004518 envlist = parse_envlist(env, &envc);
4519 if (envlist == NULL)
4520 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004521
Victor Stinner8c62be82010-05-06 00:08:46 +00004522 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004523#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004524 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004525#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004526 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004527#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004528 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004529
Victor Stinner8c62be82010-05-06 00:08:46 +00004530 if (spawnval == -1)
4531 (void) posix_error();
4532 else
4533 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004534
Victor Stinner8c62be82010-05-06 00:08:46 +00004535 while (--envc >= 0)
4536 PyMem_DEL(envlist[envc]);
4537 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004538 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004539 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004540 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 Py_DECREF(opath);
4542 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004543}
4544#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004545#endif /* HAVE_SPAWNV */
4546
4547
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004548#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004549PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004550"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004551Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4552\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004553Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004554
4555static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004556posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004557{
Victor Stinner8c62be82010-05-06 00:08:46 +00004558 pid_t pid;
4559 int result = 0;
4560 _PyImport_AcquireLock();
4561 pid = fork1();
4562 if (pid == 0) {
4563 /* child: this clobbers and resets the import lock. */
4564 PyOS_AfterFork();
4565 } else {
4566 /* parent: release the import lock. */
4567 result = _PyImport_ReleaseLock();
4568 }
4569 if (pid == -1)
4570 return posix_error();
4571 if (result < 0) {
4572 /* Don't clobber the OSError if the fork failed. */
4573 PyErr_SetString(PyExc_RuntimeError,
4574 "not holding the import lock");
4575 return NULL;
4576 }
4577 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004578}
4579#endif
4580
4581
Guido van Rossumad0ee831995-03-01 10:34:45 +00004582#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004583PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004584"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004585Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004586Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004587
Barry Warsaw53699e91996-12-10 23:23:01 +00004588static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004589posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004590{
Victor Stinner8c62be82010-05-06 00:08:46 +00004591 pid_t pid;
4592 int result = 0;
4593 _PyImport_AcquireLock();
4594 pid = fork();
4595 if (pid == 0) {
4596 /* child: this clobbers and resets the import lock. */
4597 PyOS_AfterFork();
4598 } else {
4599 /* parent: release the import lock. */
4600 result = _PyImport_ReleaseLock();
4601 }
4602 if (pid == -1)
4603 return posix_error();
4604 if (result < 0) {
4605 /* Don't clobber the OSError if the fork failed. */
4606 PyErr_SetString(PyExc_RuntimeError,
4607 "not holding the import lock");
4608 return NULL;
4609 }
4610 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004611}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004612#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004613
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004614#ifdef HAVE_SCHED_H
4615
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004616#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4617
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004618PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4619"sched_get_priority_max(policy)\n\n\
4620Get the maximum scheduling priority for *policy*.");
4621
4622static PyObject *
4623posix_sched_get_priority_max(PyObject *self, PyObject *args)
4624{
4625 int policy, max;
4626
4627 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4628 return NULL;
4629 max = sched_get_priority_max(policy);
4630 if (max < 0)
4631 return posix_error();
4632 return PyLong_FromLong(max);
4633}
4634
4635PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4636"sched_get_priority_min(policy)\n\n\
4637Get the minimum scheduling priority for *policy*.");
4638
4639static PyObject *
4640posix_sched_get_priority_min(PyObject *self, PyObject *args)
4641{
4642 int policy, min;
4643
4644 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4645 return NULL;
4646 min = sched_get_priority_min(policy);
4647 if (min < 0)
4648 return posix_error();
4649 return PyLong_FromLong(min);
4650}
4651
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004652#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4653
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004654#ifdef HAVE_SCHED_SETSCHEDULER
4655
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004656PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4657"sched_getscheduler(pid)\n\n\
4658Get the scheduling policy for the process with a PID of *pid*.\n\
4659Passing a PID of 0 returns the scheduling policy for the calling process.");
4660
4661static PyObject *
4662posix_sched_getscheduler(PyObject *self, PyObject *args)
4663{
4664 pid_t pid;
4665 int policy;
4666
4667 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4668 return NULL;
4669 policy = sched_getscheduler(pid);
4670 if (policy < 0)
4671 return posix_error();
4672 return PyLong_FromLong(policy);
4673}
4674
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004675#endif
4676
4677#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4678
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004679static PyObject *
4680sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4681{
4682 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004683 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004684
4685 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4686 return NULL;
4687 res = PyStructSequence_New(type);
4688 if (!res)
4689 return NULL;
4690 Py_INCREF(priority);
4691 PyStructSequence_SET_ITEM(res, 0, priority);
4692 return res;
4693}
4694
4695PyDoc_STRVAR(sched_param__doc__,
4696"sched_param(sched_priority): A scheduling parameter.\n\n\
4697Current has only one field: sched_priority");
4698
4699static PyStructSequence_Field sched_param_fields[] = {
4700 {"sched_priority", "the scheduling priority"},
4701 {0}
4702};
4703
4704static PyStructSequence_Desc sched_param_desc = {
4705 "sched_param", /* name */
4706 sched_param__doc__, /* doc */
4707 sched_param_fields,
4708 1
4709};
4710
4711static int
4712convert_sched_param(PyObject *param, struct sched_param *res)
4713{
4714 long priority;
4715
4716 if (Py_TYPE(param) != &SchedParamType) {
4717 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4718 return 0;
4719 }
4720 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4721 if (priority == -1 && PyErr_Occurred())
4722 return 0;
4723 if (priority > INT_MAX || priority < INT_MIN) {
4724 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4725 return 0;
4726 }
4727 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4728 return 1;
4729}
4730
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004731#endif
4732
4733#ifdef HAVE_SCHED_SETSCHEDULER
4734
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004735PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4736"sched_setscheduler(pid, policy, param)\n\n\
4737Set the scheduling policy, *policy*, for *pid*.\n\
4738If *pid* is 0, the calling process is changed.\n\
4739*param* is an instance of sched_param.");
4740
4741static PyObject *
4742posix_sched_setscheduler(PyObject *self, PyObject *args)
4743{
4744 pid_t pid;
4745 int policy;
4746 struct sched_param param;
4747
4748 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4749 &pid, &policy, &convert_sched_param, &param))
4750 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02004751
4752 /*
Jesus Cea54b01492011-09-10 01:53:19 +02004753 ** sched_setscheduler() returns 0 in Linux, but the previous
4754 ** scheduling policy under Solaris/Illumos, and others.
4755 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02004756 */
4757 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004758 return posix_error();
4759 Py_RETURN_NONE;
4760}
4761
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004762#endif
4763
4764#ifdef HAVE_SCHED_SETPARAM
4765
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004766PyDoc_STRVAR(posix_sched_getparam__doc__,
4767"sched_getparam(pid) -> sched_param\n\n\
4768Returns scheduling parameters for the process with *pid* as an instance of the\n\
4769sched_param class. A PID of 0 means the calling process.");
4770
4771static PyObject *
4772posix_sched_getparam(PyObject *self, PyObject *args)
4773{
4774 pid_t pid;
4775 struct sched_param param;
4776 PyObject *res, *priority;
4777
4778 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4779 return NULL;
4780 if (sched_getparam(pid, &param))
4781 return posix_error();
4782 res = PyStructSequence_New(&SchedParamType);
4783 if (!res)
4784 return NULL;
4785 priority = PyLong_FromLong(param.sched_priority);
4786 if (!priority) {
4787 Py_DECREF(res);
4788 return NULL;
4789 }
4790 PyStructSequence_SET_ITEM(res, 0, priority);
4791 return res;
4792}
4793
4794PyDoc_STRVAR(posix_sched_setparam__doc__,
4795"sched_setparam(pid, param)\n\n\
4796Set scheduling parameters for a process with PID *pid*.\n\
4797A PID of 0 means the calling process.");
4798
4799static PyObject *
4800posix_sched_setparam(PyObject *self, PyObject *args)
4801{
4802 pid_t pid;
4803 struct sched_param param;
4804
4805 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4806 &pid, &convert_sched_param, &param))
4807 return NULL;
4808 if (sched_setparam(pid, &param))
4809 return posix_error();
4810 Py_RETURN_NONE;
4811}
4812
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004813#endif
4814
4815#ifdef HAVE_SCHED_RR_GET_INTERVAL
4816
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004817PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4818"sched_rr_get_interval(pid) -> float\n\n\
4819Return the round-robin quantum for the process with PID *pid* in seconds.");
4820
4821static PyObject *
4822posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4823{
4824 pid_t pid;
4825 struct timespec interval;
4826
4827 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4828 return NULL;
4829 if (sched_rr_get_interval(pid, &interval))
4830 return posix_error();
4831 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4832}
4833
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004834#endif
4835
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004836PyDoc_STRVAR(posix_sched_yield__doc__,
4837"sched_yield()\n\n\
4838Voluntarily relinquish the CPU.");
4839
4840static PyObject *
4841posix_sched_yield(PyObject *self, PyObject *noargs)
4842{
4843 if (sched_yield())
4844 return posix_error();
4845 Py_RETURN_NONE;
4846}
4847
Benjamin Peterson2740af82011-08-02 17:41:34 -05004848#ifdef HAVE_SCHED_SETAFFINITY
4849
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004850typedef struct {
4851 PyObject_HEAD;
4852 Py_ssize_t size;
4853 int ncpus;
4854 cpu_set_t *set;
4855} Py_cpu_set;
4856
4857static PyTypeObject cpu_set_type;
4858
4859static void
4860cpu_set_dealloc(Py_cpu_set *set)
4861{
4862 assert(set->set);
4863 CPU_FREE(set->set);
4864 Py_TYPE(set)->tp_free(set);
4865}
4866
4867static Py_cpu_set *
4868make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4869{
4870 Py_cpu_set *set;
4871
4872 if (size < 0) {
4873 PyErr_SetString(PyExc_ValueError, "negative size");
4874 return NULL;
4875 }
4876 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4877 if (!set)
4878 return NULL;
4879 set->ncpus = size;
4880 set->size = CPU_ALLOC_SIZE(size);
4881 set->set = CPU_ALLOC(size);
4882 if (!set->set) {
4883 type->tp_free(set);
4884 PyErr_NoMemory();
4885 return NULL;
4886 }
4887 CPU_ZERO_S(set->size, set->set);
4888 return set;
4889}
4890
4891static PyObject *
4892cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4893{
4894 int size;
4895
4896 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4897 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4898 return NULL;
4899 return (PyObject *)make_new_cpu_set(type, size);
4900}
4901
4902static PyObject *
4903cpu_set_repr(Py_cpu_set *set)
4904{
4905 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4906}
4907
4908static Py_ssize_t
4909cpu_set_len(Py_cpu_set *set)
4910{
4911 return set->ncpus;
4912}
4913
4914static int
4915_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4916{
4917 int cpu;
4918 if (!PyArg_ParseTuple(args, requester, &cpu))
4919 return -1;
4920 if (cpu < 0) {
4921 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4922 return -1;
4923 }
4924 if (cpu >= set->ncpus) {
4925 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4926 return -1;
4927 }
4928 return cpu;
4929}
4930
4931PyDoc_STRVAR(cpu_set_set_doc,
4932"cpu_set.set(i)\n\n\
4933Add CPU *i* to the set.");
4934
4935static PyObject *
4936cpu_set_set(Py_cpu_set *set, PyObject *args)
4937{
4938 int cpu = _get_cpu(set, "i|set", args);
4939 if (cpu == -1)
4940 return NULL;
4941 CPU_SET_S(cpu, set->size, set->set);
4942 Py_RETURN_NONE;
4943}
4944
4945PyDoc_STRVAR(cpu_set_count_doc,
4946"cpu_set.count() -> int\n\n\
4947Return the number of CPUs active in the set.");
4948
4949static PyObject *
4950cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4951{
4952 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
4953}
4954
4955PyDoc_STRVAR(cpu_set_clear_doc,
4956"cpu_set.clear(i)\n\n\
4957Remove CPU *i* from the set.");
4958
4959static PyObject *
4960cpu_set_clear(Py_cpu_set *set, PyObject *args)
4961{
4962 int cpu = _get_cpu(set, "i|clear", args);
4963 if (cpu == -1)
4964 return NULL;
4965 CPU_CLR_S(cpu, set->size, set->set);
4966 Py_RETURN_NONE;
4967}
4968
4969PyDoc_STRVAR(cpu_set_isset_doc,
4970"cpu_set.isset(i) -> bool\n\n\
4971Test if CPU *i* is in the set.");
4972
4973static PyObject *
4974cpu_set_isset(Py_cpu_set *set, PyObject *args)
4975{
4976 int cpu = _get_cpu(set, "i|isset", args);
4977 if (cpu == -1)
4978 return NULL;
4979 if (CPU_ISSET_S(cpu, set->size, set->set))
4980 Py_RETURN_TRUE;
4981 Py_RETURN_FALSE;
4982}
4983
4984PyDoc_STRVAR(cpu_set_zero_doc,
4985"cpu_set.zero()\n\n\
4986Clear the cpu_set.");
4987
4988static PyObject *
4989cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
4990{
4991 CPU_ZERO_S(set->size, set->set);
4992 Py_RETURN_NONE;
4993}
4994
4995static PyObject *
4996cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
4997{
4998 int eq;
4999
Brian Curtindfc80e32011-08-10 20:28:54 -05005000 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
5001 Py_RETURN_NOTIMPLEMENTED;
5002
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005003 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
5004 if ((op == Py_EQ) ? eq : !eq)
5005 Py_RETURN_TRUE;
5006 else
5007 Py_RETURN_FALSE;
5008}
5009
5010#define CPU_SET_BINOP(name, op) \
5011 static PyObject * \
5012 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5013 if (res) { \
5014 Py_INCREF(res); \
5015 } \
5016 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005017 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005018 if (!res) \
5019 return NULL; \
5020 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005021 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005022 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005023 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005024 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005025 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005026 op(res->size, res->set, left->set, right->set); \
5027 return (PyObject *)res; \
5028 } \
5029 static PyObject * \
5030 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5031 return do_cpu_set_##name(left, right, NULL); \
5032 } \
5033 static PyObject * \
5034 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5035 return do_cpu_set_##name(left, right, left); \
5036 } \
5037
5038CPU_SET_BINOP(and, CPU_AND_S)
5039CPU_SET_BINOP(or, CPU_OR_S)
5040CPU_SET_BINOP(xor, CPU_XOR_S)
5041#undef CPU_SET_BINOP
5042
5043PyDoc_STRVAR(cpu_set_doc,
5044"cpu_set(size)\n\n\
5045Create an empty mask of CPUs.");
5046
5047static PyNumberMethods cpu_set_as_number = {
5048 0, /*nb_add*/
5049 0, /*nb_subtract*/
5050 0, /*nb_multiply*/
5051 0, /*nb_remainder*/
5052 0, /*nb_divmod*/
5053 0, /*nb_power*/
5054 0, /*nb_negative*/
5055 0, /*nb_positive*/
5056 0, /*nb_absolute*/
5057 0, /*nb_bool*/
5058 0, /*nb_invert*/
5059 0, /*nb_lshift*/
5060 0, /*nb_rshift*/
5061 (binaryfunc)cpu_set_and, /*nb_and*/
5062 (binaryfunc)cpu_set_xor, /*nb_xor*/
5063 (binaryfunc)cpu_set_or, /*nb_or*/
5064 0, /*nb_int*/
5065 0, /*nb_reserved*/
5066 0, /*nb_float*/
5067 0, /*nb_inplace_add*/
5068 0, /*nb_inplace_subtract*/
5069 0, /*nb_inplace_multiply*/
5070 0, /*nb_inplace_remainder*/
5071 0, /*nb_inplace_power*/
5072 0, /*nb_inplace_lshift*/
5073 0, /*nb_inplace_rshift*/
5074 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5075 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5076 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5077};
5078
5079static PySequenceMethods cpu_set_as_sequence = {
5080 (lenfunc)cpu_set_len, /* sq_length */
5081};
5082
5083static PyMethodDef cpu_set_methods[] = {
5084 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5085 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5086 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5087 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5088 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5089 {NULL, NULL} /* sentinel */
5090};
5091
5092static PyTypeObject cpu_set_type = {
5093 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5094 "posix.cpu_set", /* tp_name */
5095 sizeof(Py_cpu_set), /* tp_basicsize */
5096 0, /* tp_itemsize */
5097 /* methods */
5098 (destructor)cpu_set_dealloc, /* tp_dealloc */
5099 0, /* tp_print */
5100 0, /* tp_getattr */
5101 0, /* tp_setattr */
5102 0, /* tp_reserved */
5103 (reprfunc)cpu_set_repr, /* tp_repr */
5104 &cpu_set_as_number, /* tp_as_number */
5105 &cpu_set_as_sequence, /* tp_as_sequence */
5106 0, /* tp_as_mapping */
5107 PyObject_HashNotImplemented, /* tp_hash */
5108 0, /* tp_call */
5109 0, /* tp_str */
5110 PyObject_GenericGetAttr, /* tp_getattro */
5111 0, /* tp_setattro */
5112 0, /* tp_as_buffer */
5113 Py_TPFLAGS_DEFAULT, /* tp_flags */
5114 cpu_set_doc, /* tp_doc */
5115 0, /* tp_traverse */
5116 0, /* tp_clear */
5117 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5118 0, /* tp_weaklistoffset */
5119 0, /* tp_iter */
5120 0, /* tp_iternext */
5121 cpu_set_methods, /* tp_methods */
5122 0, /* tp_members */
5123 0, /* tp_getset */
5124 0, /* tp_base */
5125 0, /* tp_dict */
5126 0, /* tp_descr_get */
5127 0, /* tp_descr_set */
5128 0, /* tp_dictoffset */
5129 0, /* tp_init */
5130 PyType_GenericAlloc, /* tp_alloc */
5131 cpu_set_new, /* tp_new */
5132 PyObject_Del, /* tp_free */
5133};
5134
5135PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5136"sched_setaffinity(pid, cpu_set)\n\n\
5137Set the affinity of the process with PID *pid* to *cpu_set*.");
5138
5139static PyObject *
5140posix_sched_setaffinity(PyObject *self, PyObject *args)
5141{
5142 pid_t pid;
5143 Py_cpu_set *cpu_set;
5144
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005145 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005146 &pid, &cpu_set_type, &cpu_set))
5147 return NULL;
5148 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5149 return posix_error();
5150 Py_RETURN_NONE;
5151}
5152
5153PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5154"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5155Return the affinity of the process with PID *pid*.\n\
5156The returned cpu_set will be of size *ncpus*.");
5157
5158static PyObject *
5159posix_sched_getaffinity(PyObject *self, PyObject *args)
5160{
5161 pid_t pid;
5162 int ncpus;
5163 Py_cpu_set *res;
5164
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005165 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005166 &pid, &ncpus))
5167 return NULL;
5168 res = make_new_cpu_set(&cpu_set_type, ncpus);
5169 if (!res)
5170 return NULL;
5171 if (sched_getaffinity(pid, res->size, res->set)) {
5172 Py_DECREF(res);
5173 return posix_error();
5174 }
5175 return (PyObject *)res;
5176}
5177
Benjamin Peterson2740af82011-08-02 17:41:34 -05005178#endif /* HAVE_SCHED_SETAFFINITY */
5179
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005180#endif /* HAVE_SCHED_H */
5181
Neal Norwitzb59798b2003-03-21 01:43:31 +00005182/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005183/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5184#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005185#define DEV_PTY_FILE "/dev/ptc"
5186#define HAVE_DEV_PTMX
5187#else
5188#define DEV_PTY_FILE "/dev/ptmx"
5189#endif
5190
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005191#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005192#ifdef HAVE_PTY_H
5193#include <pty.h>
5194#else
5195#ifdef HAVE_LIBUTIL_H
5196#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005197#else
5198#ifdef HAVE_UTIL_H
5199#include <util.h>
5200#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005201#endif /* HAVE_LIBUTIL_H */
5202#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005203#ifdef HAVE_STROPTS_H
5204#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005205#endif
5206#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005207
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005208#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005209PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005210"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005211Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005212
5213static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005214posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005215{
Victor Stinner8c62be82010-05-06 00:08:46 +00005216 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005217#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005218 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005219#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005220#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005221 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005222#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005223 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005224#endif
5225#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005226
Thomas Wouters70c21a12000-07-14 14:28:33 +00005227#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005228 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5229 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005230#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005231 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5232 if (slave_name == NULL)
5233 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005234
Victor Stinner8c62be82010-05-06 00:08:46 +00005235 slave_fd = open(slave_name, O_RDWR);
5236 if (slave_fd < 0)
5237 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005238#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005239 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5240 if (master_fd < 0)
5241 return posix_error();
5242 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5243 /* change permission of slave */
5244 if (grantpt(master_fd) < 0) {
5245 PyOS_setsig(SIGCHLD, sig_saved);
5246 return posix_error();
5247 }
5248 /* unlock slave */
5249 if (unlockpt(master_fd) < 0) {
5250 PyOS_setsig(SIGCHLD, sig_saved);
5251 return posix_error();
5252 }
5253 PyOS_setsig(SIGCHLD, sig_saved);
5254 slave_name = ptsname(master_fd); /* get name of slave */
5255 if (slave_name == NULL)
5256 return posix_error();
5257 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5258 if (slave_fd < 0)
5259 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005260#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005261 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5262 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005263#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005264 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005265#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005266#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005267#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005268
Victor Stinner8c62be82010-05-06 00:08:46 +00005269 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005270
Fred Drake8cef4cf2000-06-28 16:40:38 +00005271}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005272#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005273
5274#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005275PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005276"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005277Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5278Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005279To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005280
5281static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005282posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005283{
Victor Stinner8c62be82010-05-06 00:08:46 +00005284 int master_fd = -1, result = 0;
5285 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005286
Victor Stinner8c62be82010-05-06 00:08:46 +00005287 _PyImport_AcquireLock();
5288 pid = forkpty(&master_fd, NULL, NULL, NULL);
5289 if (pid == 0) {
5290 /* child: this clobbers and resets the import lock. */
5291 PyOS_AfterFork();
5292 } else {
5293 /* parent: release the import lock. */
5294 result = _PyImport_ReleaseLock();
5295 }
5296 if (pid == -1)
5297 return posix_error();
5298 if (result < 0) {
5299 /* Don't clobber the OSError if the fork failed. */
5300 PyErr_SetString(PyExc_RuntimeError,
5301 "not holding the import lock");
5302 return NULL;
5303 }
5304 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005305}
5306#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005307
Ross Lagerwall7807c352011-03-17 20:20:30 +02005308
Guido van Rossumad0ee831995-03-01 10:34:45 +00005309#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005310PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005311"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005312Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005313
Barry Warsaw53699e91996-12-10 23:23:01 +00005314static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005315posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005316{
Victor Stinner8c62be82010-05-06 00:08:46 +00005317 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005318}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005319#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005321
Guido van Rossumad0ee831995-03-01 10:34:45 +00005322#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005323PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005324"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005325Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005326
Barry Warsaw53699e91996-12-10 23:23:01 +00005327static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005328posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005329{
Victor Stinner8c62be82010-05-06 00:08:46 +00005330 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005331}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005332#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005333
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005334
Guido van Rossumad0ee831995-03-01 10:34:45 +00005335#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005336PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005337"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005338Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005339
Barry Warsaw53699e91996-12-10 23:23:01 +00005340static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005341posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005342{
Victor Stinner8c62be82010-05-06 00:08:46 +00005343 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005344}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005345#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005346
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005347
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005348PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005349"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005350Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005351
Barry Warsaw53699e91996-12-10 23:23:01 +00005352static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005353posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005354{
Victor Stinner8c62be82010-05-06 00:08:46 +00005355 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005356}
5357
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005358#ifdef HAVE_GETGROUPLIST
5359PyDoc_STRVAR(posix_getgrouplist__doc__,
5360"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5361Returns a list of groups to which a user belongs.\n\n\
5362 user: username to lookup\n\
5363 group: base group id of the user");
5364
5365static PyObject *
5366posix_getgrouplist(PyObject *self, PyObject *args)
5367{
5368#ifdef NGROUPS_MAX
5369#define MAX_GROUPS NGROUPS_MAX
5370#else
5371 /* defined to be 16 on Solaris7, so this should be a small number */
5372#define MAX_GROUPS 64
5373#endif
5374
5375 const char *user;
5376 int i, ngroups;
5377 PyObject *list;
5378#ifdef __APPLE__
5379 int *groups, basegid;
5380#else
5381 gid_t *groups, basegid;
5382#endif
5383 ngroups = MAX_GROUPS;
5384
5385 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5386 return NULL;
5387
5388#ifdef __APPLE__
5389 groups = PyMem_Malloc(ngroups * sizeof(int));
5390#else
5391 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5392#endif
5393 if (groups == NULL)
5394 return PyErr_NoMemory();
5395
5396 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5397 PyMem_Del(groups);
5398 return posix_error();
5399 }
5400
5401 list = PyList_New(ngroups);
5402 if (list == NULL) {
5403 PyMem_Del(groups);
5404 return NULL;
5405 }
5406
5407 for (i = 0; i < ngroups; i++) {
5408 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5409 if (o == NULL) {
5410 Py_DECREF(list);
5411 PyMem_Del(groups);
5412 return NULL;
5413 }
5414 PyList_SET_ITEM(list, i, o);
5415 }
5416
5417 PyMem_Del(groups);
5418
5419 return list;
5420}
5421#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005422
Fred Drakec9680921999-12-13 16:37:25 +00005423#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005424PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005425"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005427
5428static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005429posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005430{
5431 PyObject *result = NULL;
5432
Fred Drakec9680921999-12-13 16:37:25 +00005433#ifdef NGROUPS_MAX
5434#define MAX_GROUPS NGROUPS_MAX
5435#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005436 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005437#define MAX_GROUPS 64
5438#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005439 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005440
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005441 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005442 * This is a helper variable to store the intermediate result when
5443 * that happens.
5444 *
5445 * To keep the code readable the OSX behaviour is unconditional,
5446 * according to the POSIX spec this should be safe on all unix-y
5447 * systems.
5448 */
5449 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005450 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005451
Victor Stinner8c62be82010-05-06 00:08:46 +00005452 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005453 if (n < 0) {
5454 if (errno == EINVAL) {
5455 n = getgroups(0, NULL);
5456 if (n == -1) {
5457 return posix_error();
5458 }
5459 if (n == 0) {
5460 /* Avoid malloc(0) */
5461 alt_grouplist = grouplist;
5462 } else {
5463 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5464 if (alt_grouplist == NULL) {
5465 errno = EINVAL;
5466 return posix_error();
5467 }
5468 n = getgroups(n, alt_grouplist);
5469 if (n == -1) {
5470 PyMem_Free(alt_grouplist);
5471 return posix_error();
5472 }
5473 }
5474 } else {
5475 return posix_error();
5476 }
5477 }
5478 result = PyList_New(n);
5479 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005480 int i;
5481 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005482 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005483 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005484 Py_DECREF(result);
5485 result = NULL;
5486 break;
Fred Drakec9680921999-12-13 16:37:25 +00005487 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005488 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005489 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005490 }
5491
5492 if (alt_grouplist != grouplist) {
5493 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005494 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005495
Fred Drakec9680921999-12-13 16:37:25 +00005496 return result;
5497}
5498#endif
5499
Antoine Pitroub7572f02009-12-02 20:46:48 +00005500#ifdef HAVE_INITGROUPS
5501PyDoc_STRVAR(posix_initgroups__doc__,
5502"initgroups(username, gid) -> None\n\n\
5503Call the system initgroups() to initialize the group access list with all of\n\
5504the groups of which the specified username is a member, plus the specified\n\
5505group id.");
5506
5507static PyObject *
5508posix_initgroups(PyObject *self, PyObject *args)
5509{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005510 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005512 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005513 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005514
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005515 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5516 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005517 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005518 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005519
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005520 res = initgroups(username, (gid_t) gid);
5521 Py_DECREF(oname);
5522 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005523 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005524
Victor Stinner8c62be82010-05-06 00:08:46 +00005525 Py_INCREF(Py_None);
5526 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005527}
5528#endif
5529
Martin v. Löwis606edc12002-06-13 21:09:11 +00005530#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005531PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005532"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005533Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005534
5535static PyObject *
5536posix_getpgid(PyObject *self, PyObject *args)
5537{
Victor Stinner8c62be82010-05-06 00:08:46 +00005538 pid_t pid, pgid;
5539 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5540 return NULL;
5541 pgid = getpgid(pid);
5542 if (pgid < 0)
5543 return posix_error();
5544 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005545}
5546#endif /* HAVE_GETPGID */
5547
5548
Guido van Rossumb6775db1994-08-01 11:34:53 +00005549#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005550PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005551"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005552Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005553
Barry Warsaw53699e91996-12-10 23:23:01 +00005554static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005555posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005556{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005557#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005558 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005559#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005560 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005561#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005562}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005563#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005564
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005565
Guido van Rossumb6775db1994-08-01 11:34:53 +00005566#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005567PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005568"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005569Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005570
Barry Warsaw53699e91996-12-10 23:23:01 +00005571static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005572posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005573{
Guido van Rossum64933891994-10-20 21:56:42 +00005574#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005575 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005576#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005577 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005578#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005579 return posix_error();
5580 Py_INCREF(Py_None);
5581 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005582}
5583
Guido van Rossumb6775db1994-08-01 11:34:53 +00005584#endif /* HAVE_SETPGRP */
5585
Guido van Rossumad0ee831995-03-01 10:34:45 +00005586#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005587
5588#ifdef MS_WINDOWS
5589#include <tlhelp32.h>
5590
5591static PyObject*
5592win32_getppid()
5593{
5594 HANDLE snapshot;
5595 pid_t mypid;
5596 PyObject* result = NULL;
5597 BOOL have_record;
5598 PROCESSENTRY32 pe;
5599
5600 mypid = getpid(); /* This function never fails */
5601
5602 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5603 if (snapshot == INVALID_HANDLE_VALUE)
5604 return PyErr_SetFromWindowsErr(GetLastError());
5605
5606 pe.dwSize = sizeof(pe);
5607 have_record = Process32First(snapshot, &pe);
5608 while (have_record) {
5609 if (mypid == (pid_t)pe.th32ProcessID) {
5610 /* We could cache the ulong value in a static variable. */
5611 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5612 break;
5613 }
5614
5615 have_record = Process32Next(snapshot, &pe);
5616 }
5617
5618 /* If our loop exits and our pid was not found (result will be NULL)
5619 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5620 * error anyway, so let's raise it. */
5621 if (!result)
5622 result = PyErr_SetFromWindowsErr(GetLastError());
5623
5624 CloseHandle(snapshot);
5625
5626 return result;
5627}
5628#endif /*MS_WINDOWS*/
5629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005630PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005631"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005632Return the parent's process id. If the parent process has already exited,\n\
5633Windows machines will still return its id; others systems will return the id\n\
5634of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005635
Barry Warsaw53699e91996-12-10 23:23:01 +00005636static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005637posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005638{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005639#ifdef MS_WINDOWS
5640 return win32_getppid();
5641#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005642 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005643#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005644}
5645#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005646
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005647
Fred Drake12c6e2d1999-12-14 21:25:03 +00005648#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005649PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005650"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005651Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005652
5653static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005654posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005655{
Victor Stinner8c62be82010-05-06 00:08:46 +00005656 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005657#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005658 wchar_t user_name[UNLEN + 1];
5659 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5660
5661 if (GetUserNameW(user_name, &num_chars)) {
5662 /* num_chars is the number of unicode chars plus null terminator */
5663 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005664 }
5665 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005666 result = PyErr_SetFromWindowsErr(GetLastError());
5667#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005668 char *name;
5669 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005670
Victor Stinner8c62be82010-05-06 00:08:46 +00005671 errno = 0;
5672 name = getlogin();
5673 if (name == NULL) {
5674 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005675 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005676 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005677 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 }
5679 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005680 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005681 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005682#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005683 return result;
5684}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005685#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005686
Guido van Rossumad0ee831995-03-01 10:34:45 +00005687#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005688PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005689"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005690Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005691
Barry Warsaw53699e91996-12-10 23:23:01 +00005692static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005693posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005694{
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005696}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005697#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005698
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005699
Guido van Rossumad0ee831995-03-01 10:34:45 +00005700#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005701PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005702"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005703Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005704
Barry Warsaw53699e91996-12-10 23:23:01 +00005705static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005706posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005707{
Victor Stinner8c62be82010-05-06 00:08:46 +00005708 pid_t pid;
5709 int sig;
5710 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5711 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005712#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005713 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5714 APIRET rc;
5715 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005716 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005717
5718 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5719 APIRET rc;
5720 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005721 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005722
5723 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005724 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005725#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005726 if (kill(pid, sig) == -1)
5727 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005728#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005729 Py_INCREF(Py_None);
5730 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005731}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005732#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005733
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005734#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005735PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005736"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005737Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005738
5739static PyObject *
5740posix_killpg(PyObject *self, PyObject *args)
5741{
Victor Stinner8c62be82010-05-06 00:08:46 +00005742 int sig;
5743 pid_t pgid;
5744 /* XXX some man pages make the `pgid` parameter an int, others
5745 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5746 take the same type. Moreover, pid_t is always at least as wide as
5747 int (else compilation of this module fails), which is safe. */
5748 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5749 return NULL;
5750 if (killpg(pgid, sig) == -1)
5751 return posix_error();
5752 Py_INCREF(Py_None);
5753 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005754}
5755#endif
5756
Brian Curtineb24d742010-04-12 17:16:38 +00005757#ifdef MS_WINDOWS
5758PyDoc_STRVAR(win32_kill__doc__,
5759"kill(pid, sig)\n\n\
5760Kill a process with a signal.");
5761
5762static PyObject *
5763win32_kill(PyObject *self, PyObject *args)
5764{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005765 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 DWORD pid, sig, err;
5767 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005768
Victor Stinner8c62be82010-05-06 00:08:46 +00005769 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5770 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005771
Victor Stinner8c62be82010-05-06 00:08:46 +00005772 /* Console processes which share a common console can be sent CTRL+C or
5773 CTRL+BREAK events, provided they handle said events. */
5774 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5775 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5776 err = GetLastError();
5777 PyErr_SetFromWindowsErr(err);
5778 }
5779 else
5780 Py_RETURN_NONE;
5781 }
Brian Curtineb24d742010-04-12 17:16:38 +00005782
Victor Stinner8c62be82010-05-06 00:08:46 +00005783 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5784 attempt to open and terminate the process. */
5785 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5786 if (handle == NULL) {
5787 err = GetLastError();
5788 return PyErr_SetFromWindowsErr(err);
5789 }
Brian Curtineb24d742010-04-12 17:16:38 +00005790
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 if (TerminateProcess(handle, sig) == 0) {
5792 err = GetLastError();
5793 result = PyErr_SetFromWindowsErr(err);
5794 } else {
5795 Py_INCREF(Py_None);
5796 result = Py_None;
5797 }
Brian Curtineb24d742010-04-12 17:16:38 +00005798
Victor Stinner8c62be82010-05-06 00:08:46 +00005799 CloseHandle(handle);
5800 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005801}
5802#endif /* MS_WINDOWS */
5803
Guido van Rossumc0125471996-06-28 18:55:32 +00005804#ifdef HAVE_PLOCK
5805
5806#ifdef HAVE_SYS_LOCK_H
5807#include <sys/lock.h>
5808#endif
5809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005810PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005811"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005812Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005813
Barry Warsaw53699e91996-12-10 23:23:01 +00005814static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005815posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005816{
Victor Stinner8c62be82010-05-06 00:08:46 +00005817 int op;
5818 if (!PyArg_ParseTuple(args, "i:plock", &op))
5819 return NULL;
5820 if (plock(op) == -1)
5821 return posix_error();
5822 Py_INCREF(Py_None);
5823 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005824}
5825#endif
5826
Guido van Rossumb6775db1994-08-01 11:34:53 +00005827#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005828PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005829"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005830Set the current process's user id.");
5831
Barry Warsaw53699e91996-12-10 23:23:01 +00005832static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005833posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005834{
Victor Stinner8c62be82010-05-06 00:08:46 +00005835 long uid_arg;
5836 uid_t uid;
5837 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5838 return NULL;
5839 uid = uid_arg;
5840 if (uid != uid_arg) {
5841 PyErr_SetString(PyExc_OverflowError, "user id too big");
5842 return NULL;
5843 }
5844 if (setuid(uid) < 0)
5845 return posix_error();
5846 Py_INCREF(Py_None);
5847 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005848}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005849#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005850
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005851
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005852#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005853PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005854"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005855Set the current process's effective user id.");
5856
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005857static PyObject *
5858posix_seteuid (PyObject *self, PyObject *args)
5859{
Victor Stinner8c62be82010-05-06 00:08:46 +00005860 long euid_arg;
5861 uid_t euid;
5862 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5863 return NULL;
5864 euid = euid_arg;
5865 if (euid != euid_arg) {
5866 PyErr_SetString(PyExc_OverflowError, "user id too big");
5867 return NULL;
5868 }
5869 if (seteuid(euid) < 0) {
5870 return posix_error();
5871 } else {
5872 Py_INCREF(Py_None);
5873 return Py_None;
5874 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005875}
5876#endif /* HAVE_SETEUID */
5877
5878#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005879PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005880"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005881Set the current process's effective group id.");
5882
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005883static PyObject *
5884posix_setegid (PyObject *self, PyObject *args)
5885{
Victor Stinner8c62be82010-05-06 00:08:46 +00005886 long egid_arg;
5887 gid_t egid;
5888 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5889 return NULL;
5890 egid = egid_arg;
5891 if (egid != egid_arg) {
5892 PyErr_SetString(PyExc_OverflowError, "group id too big");
5893 return NULL;
5894 }
5895 if (setegid(egid) < 0) {
5896 return posix_error();
5897 } else {
5898 Py_INCREF(Py_None);
5899 return Py_None;
5900 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005901}
5902#endif /* HAVE_SETEGID */
5903
5904#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005906"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005907Set the current process's real and effective user ids.");
5908
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005909static PyObject *
5910posix_setreuid (PyObject *self, PyObject *args)
5911{
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 long ruid_arg, euid_arg;
5913 uid_t ruid, euid;
5914 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5915 return NULL;
5916 if (ruid_arg == -1)
5917 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5918 else
5919 ruid = ruid_arg; /* otherwise, assign from our long */
5920 if (euid_arg == -1)
5921 euid = (uid_t)-1;
5922 else
5923 euid = euid_arg;
5924 if ((euid_arg != -1 && euid != euid_arg) ||
5925 (ruid_arg != -1 && ruid != ruid_arg)) {
5926 PyErr_SetString(PyExc_OverflowError, "user id too big");
5927 return NULL;
5928 }
5929 if (setreuid(ruid, euid) < 0) {
5930 return posix_error();
5931 } else {
5932 Py_INCREF(Py_None);
5933 return Py_None;
5934 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005935}
5936#endif /* HAVE_SETREUID */
5937
5938#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005939PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005940"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005941Set the current process's real and effective group ids.");
5942
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005943static PyObject *
5944posix_setregid (PyObject *self, PyObject *args)
5945{
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 long rgid_arg, egid_arg;
5947 gid_t rgid, egid;
5948 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5949 return NULL;
5950 if (rgid_arg == -1)
5951 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5952 else
5953 rgid = rgid_arg; /* otherwise, assign from our long */
5954 if (egid_arg == -1)
5955 egid = (gid_t)-1;
5956 else
5957 egid = egid_arg;
5958 if ((egid_arg != -1 && egid != egid_arg) ||
5959 (rgid_arg != -1 && rgid != rgid_arg)) {
5960 PyErr_SetString(PyExc_OverflowError, "group id too big");
5961 return NULL;
5962 }
5963 if (setregid(rgid, egid) < 0) {
5964 return posix_error();
5965 } else {
5966 Py_INCREF(Py_None);
5967 return Py_None;
5968 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005969}
5970#endif /* HAVE_SETREGID */
5971
Guido van Rossumb6775db1994-08-01 11:34:53 +00005972#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005973PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005974"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005975Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005976
Barry Warsaw53699e91996-12-10 23:23:01 +00005977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005978posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005979{
Victor Stinner8c62be82010-05-06 00:08:46 +00005980 long gid_arg;
5981 gid_t gid;
5982 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5983 return NULL;
5984 gid = gid_arg;
5985 if (gid != gid_arg) {
5986 PyErr_SetString(PyExc_OverflowError, "group id too big");
5987 return NULL;
5988 }
5989 if (setgid(gid) < 0)
5990 return posix_error();
5991 Py_INCREF(Py_None);
5992 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005993}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005994#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005995
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005996#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005997PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005998"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005999Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006000
6001static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006002posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006003{
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 int i, len;
6005 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006006
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 if (!PySequence_Check(groups)) {
6008 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6009 return NULL;
6010 }
6011 len = PySequence_Size(groups);
6012 if (len > MAX_GROUPS) {
6013 PyErr_SetString(PyExc_ValueError, "too many groups");
6014 return NULL;
6015 }
6016 for(i = 0; i < len; i++) {
6017 PyObject *elem;
6018 elem = PySequence_GetItem(groups, i);
6019 if (!elem)
6020 return NULL;
6021 if (!PyLong_Check(elem)) {
6022 PyErr_SetString(PyExc_TypeError,
6023 "groups must be integers");
6024 Py_DECREF(elem);
6025 return NULL;
6026 } else {
6027 unsigned long x = PyLong_AsUnsignedLong(elem);
6028 if (PyErr_Occurred()) {
6029 PyErr_SetString(PyExc_TypeError,
6030 "group id too big");
6031 Py_DECREF(elem);
6032 return NULL;
6033 }
6034 grouplist[i] = x;
6035 /* read back the value to see if it fitted in gid_t */
6036 if (grouplist[i] != x) {
6037 PyErr_SetString(PyExc_TypeError,
6038 "group id too big");
6039 Py_DECREF(elem);
6040 return NULL;
6041 }
6042 }
6043 Py_DECREF(elem);
6044 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006045
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 if (setgroups(len, grouplist) < 0)
6047 return posix_error();
6048 Py_INCREF(Py_None);
6049 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006050}
6051#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006052
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006053#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6054static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00006055wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006056{
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 PyObject *result;
6058 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006059
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 if (pid == -1)
6061 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006062
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 if (struct_rusage == NULL) {
6064 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6065 if (m == NULL)
6066 return NULL;
6067 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6068 Py_DECREF(m);
6069 if (struct_rusage == NULL)
6070 return NULL;
6071 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006072
Victor Stinner8c62be82010-05-06 00:08:46 +00006073 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6074 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6075 if (!result)
6076 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006077
6078#ifndef doubletime
6079#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6080#endif
6081
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 PyStructSequence_SET_ITEM(result, 0,
6083 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6084 PyStructSequence_SET_ITEM(result, 1,
6085 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006086#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006087 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6088 SET_INT(result, 2, ru->ru_maxrss);
6089 SET_INT(result, 3, ru->ru_ixrss);
6090 SET_INT(result, 4, ru->ru_idrss);
6091 SET_INT(result, 5, ru->ru_isrss);
6092 SET_INT(result, 6, ru->ru_minflt);
6093 SET_INT(result, 7, ru->ru_majflt);
6094 SET_INT(result, 8, ru->ru_nswap);
6095 SET_INT(result, 9, ru->ru_inblock);
6096 SET_INT(result, 10, ru->ru_oublock);
6097 SET_INT(result, 11, ru->ru_msgsnd);
6098 SET_INT(result, 12, ru->ru_msgrcv);
6099 SET_INT(result, 13, ru->ru_nsignals);
6100 SET_INT(result, 14, ru->ru_nvcsw);
6101 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006102#undef SET_INT
6103
Victor Stinner8c62be82010-05-06 00:08:46 +00006104 if (PyErr_Occurred()) {
6105 Py_DECREF(result);
6106 return NULL;
6107 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006108
Victor Stinner8c62be82010-05-06 00:08:46 +00006109 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006110}
6111#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6112
6113#ifdef HAVE_WAIT3
6114PyDoc_STRVAR(posix_wait3__doc__,
6115"wait3(options) -> (pid, status, rusage)\n\n\
6116Wait for completion of a child process.");
6117
6118static PyObject *
6119posix_wait3(PyObject *self, PyObject *args)
6120{
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 pid_t pid;
6122 int options;
6123 struct rusage ru;
6124 WAIT_TYPE status;
6125 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006126
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6128 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006129
Victor Stinner8c62be82010-05-06 00:08:46 +00006130 Py_BEGIN_ALLOW_THREADS
6131 pid = wait3(&status, options, &ru);
6132 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006133
Victor Stinner8c62be82010-05-06 00:08:46 +00006134 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006135}
6136#endif /* HAVE_WAIT3 */
6137
6138#ifdef HAVE_WAIT4
6139PyDoc_STRVAR(posix_wait4__doc__,
6140"wait4(pid, options) -> (pid, status, rusage)\n\n\
6141Wait for completion of a given child process.");
6142
6143static PyObject *
6144posix_wait4(PyObject *self, PyObject *args)
6145{
Victor Stinner8c62be82010-05-06 00:08:46 +00006146 pid_t pid;
6147 int options;
6148 struct rusage ru;
6149 WAIT_TYPE status;
6150 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006151
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6153 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006154
Victor Stinner8c62be82010-05-06 00:08:46 +00006155 Py_BEGIN_ALLOW_THREADS
6156 pid = wait4(pid, &status, options, &ru);
6157 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006158
Victor Stinner8c62be82010-05-06 00:08:46 +00006159 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006160}
6161#endif /* HAVE_WAIT4 */
6162
Ross Lagerwall7807c352011-03-17 20:20:30 +02006163#if defined(HAVE_WAITID) && !defined(__APPLE__)
6164PyDoc_STRVAR(posix_waitid__doc__,
6165"waitid(idtype, id, options) -> waitid_result\n\n\
6166Wait for the completion of one or more child processes.\n\n\
6167idtype can be P_PID, P_PGID or P_ALL.\n\
6168id specifies the pid to wait on.\n\
6169options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6170or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6171Returns either waitid_result or None if WNOHANG is specified and there are\n\
6172no children in a waitable state.");
6173
6174static PyObject *
6175posix_waitid(PyObject *self, PyObject *args)
6176{
6177 PyObject *result;
6178 idtype_t idtype;
6179 id_t id;
6180 int options, res;
6181 siginfo_t si;
6182 si.si_pid = 0;
6183 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6184 return NULL;
6185 Py_BEGIN_ALLOW_THREADS
6186 res = waitid(idtype, id, &si, options);
6187 Py_END_ALLOW_THREADS
6188 if (res == -1)
6189 return posix_error();
6190
6191 if (si.si_pid == 0)
6192 Py_RETURN_NONE;
6193
6194 result = PyStructSequence_New(&WaitidResultType);
6195 if (!result)
6196 return NULL;
6197
6198 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6199 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6200 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6201 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6202 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6203 if (PyErr_Occurred()) {
6204 Py_DECREF(result);
6205 return NULL;
6206 }
6207
6208 return result;
6209}
6210#endif
6211
Guido van Rossumb6775db1994-08-01 11:34:53 +00006212#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006213PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006214"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006215Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006216
Barry Warsaw53699e91996-12-10 23:23:01 +00006217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006218posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006219{
Victor Stinner8c62be82010-05-06 00:08:46 +00006220 pid_t pid;
6221 int options;
6222 WAIT_TYPE status;
6223 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006224
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6226 return NULL;
6227 Py_BEGIN_ALLOW_THREADS
6228 pid = waitpid(pid, &status, options);
6229 Py_END_ALLOW_THREADS
6230 if (pid == -1)
6231 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006232
Victor Stinner8c62be82010-05-06 00:08:46 +00006233 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006234}
6235
Tim Petersab034fa2002-02-01 11:27:43 +00006236#elif defined(HAVE_CWAIT)
6237
6238/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006239PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006240"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006241"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006242
6243static PyObject *
6244posix_waitpid(PyObject *self, PyObject *args)
6245{
Victor Stinner8c62be82010-05-06 00:08:46 +00006246 Py_intptr_t pid;
6247 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006248
Victor Stinner8c62be82010-05-06 00:08:46 +00006249 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6250 return NULL;
6251 Py_BEGIN_ALLOW_THREADS
6252 pid = _cwait(&status, pid, options);
6253 Py_END_ALLOW_THREADS
6254 if (pid == -1)
6255 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006256
Victor Stinner8c62be82010-05-06 00:08:46 +00006257 /* shift the status left a byte so this is more like the POSIX waitpid */
6258 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006259}
6260#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006261
Guido van Rossumad0ee831995-03-01 10:34:45 +00006262#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006263PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006264"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006265Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006266
Barry Warsaw53699e91996-12-10 23:23:01 +00006267static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006268posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006269{
Victor Stinner8c62be82010-05-06 00:08:46 +00006270 pid_t pid;
6271 WAIT_TYPE status;
6272 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006273
Victor Stinner8c62be82010-05-06 00:08:46 +00006274 Py_BEGIN_ALLOW_THREADS
6275 pid = wait(&status);
6276 Py_END_ALLOW_THREADS
6277 if (pid == -1)
6278 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006279
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006281}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006282#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006283
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006284
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006285PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006286"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006287Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006288
Barry Warsaw53699e91996-12-10 23:23:01 +00006289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006290posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006291{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006292#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006293 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006294#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006295#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006296 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006297 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006298#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006300#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006301#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006302}
6303
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006304
Guido van Rossumb6775db1994-08-01 11:34:53 +00006305#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006306PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006307"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006308Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006309
Barry Warsaw53699e91996-12-10 23:23:01 +00006310static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006311posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006312{
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 PyObject* v;
6314 char buf[MAXPATHLEN];
6315 PyObject *opath;
6316 char *path;
6317 int n;
6318 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006319
Victor Stinner8c62be82010-05-06 00:08:46 +00006320 if (!PyArg_ParseTuple(args, "O&:readlink",
6321 PyUnicode_FSConverter, &opath))
6322 return NULL;
6323 path = PyBytes_AsString(opath);
6324 v = PySequence_GetItem(args, 0);
6325 if (v == NULL) {
6326 Py_DECREF(opath);
6327 return NULL;
6328 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006329
Victor Stinner8c62be82010-05-06 00:08:46 +00006330 if (PyUnicode_Check(v)) {
6331 arg_is_unicode = 1;
6332 }
6333 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006334
Victor Stinner8c62be82010-05-06 00:08:46 +00006335 Py_BEGIN_ALLOW_THREADS
6336 n = readlink(path, buf, (int) sizeof buf);
6337 Py_END_ALLOW_THREADS
6338 if (n < 0)
6339 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006340
Victor Stinner8c62be82010-05-06 00:08:46 +00006341 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006342 if (arg_is_unicode)
6343 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6344 else
6345 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006346}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006347#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006348
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006349
Brian Curtin52173d42010-12-02 18:29:18 +00006350#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006351PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006352"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006353Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006354
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006355static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006356posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006357{
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006359}
6360#endif /* HAVE_SYMLINK */
6361
Brian Curtind40e6f72010-07-08 21:39:08 +00006362#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6363
6364PyDoc_STRVAR(win_readlink__doc__,
6365"readlink(path) -> path\n\n\
6366Return a string representing the path to which the symbolic link points.");
6367
Brian Curtind40e6f72010-07-08 21:39:08 +00006368/* Windows readlink implementation */
6369static PyObject *
6370win_readlink(PyObject *self, PyObject *args)
6371{
6372 wchar_t *path;
6373 DWORD n_bytes_returned;
6374 DWORD io_result;
6375 PyObject *result;
6376 HANDLE reparse_point_handle;
6377
6378 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6379 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6380 wchar_t *print_name;
6381
6382 if (!PyArg_ParseTuple(args,
6383 "u:readlink",
6384 &path))
6385 return NULL;
6386
6387 /* First get a handle to the reparse point */
6388 Py_BEGIN_ALLOW_THREADS
6389 reparse_point_handle = CreateFileW(
6390 path,
6391 0,
6392 0,
6393 0,
6394 OPEN_EXISTING,
6395 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6396 0);
6397 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006398
Brian Curtind40e6f72010-07-08 21:39:08 +00006399 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6400 {
6401 return win32_error_unicode("readlink", path);
6402 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006403
Brian Curtind40e6f72010-07-08 21:39:08 +00006404 Py_BEGIN_ALLOW_THREADS
6405 /* New call DeviceIoControl to read the reparse point */
6406 io_result = DeviceIoControl(
6407 reparse_point_handle,
6408 FSCTL_GET_REPARSE_POINT,
6409 0, 0, /* in buffer */
6410 target_buffer, sizeof(target_buffer),
6411 &n_bytes_returned,
6412 0 /* we're not using OVERLAPPED_IO */
6413 );
6414 CloseHandle(reparse_point_handle);
6415 Py_END_ALLOW_THREADS
6416
6417 if (io_result==0)
6418 {
6419 return win32_error_unicode("readlink", path);
6420 }
6421
6422 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6423 {
6424 PyErr_SetString(PyExc_ValueError,
6425 "not a symbolic link");
6426 return NULL;
6427 }
Brian Curtin74e45612010-07-09 15:58:59 +00006428 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6429 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6430
6431 result = PyUnicode_FromWideChar(print_name,
6432 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006433 return result;
6434}
6435
6436#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6437
Brian Curtin52173d42010-12-02 18:29:18 +00006438#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006439
6440/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6441static int has_CreateSymbolicLinkW = 0;
6442static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6443static int
6444check_CreateSymbolicLinkW()
6445{
6446 HINSTANCE hKernel32;
6447 /* only recheck */
6448 if (has_CreateSymbolicLinkW)
6449 return has_CreateSymbolicLinkW;
6450 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006451 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6452 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006453 if (Py_CreateSymbolicLinkW)
6454 has_CreateSymbolicLinkW = 1;
6455 return has_CreateSymbolicLinkW;
6456}
6457
6458PyDoc_STRVAR(win_symlink__doc__,
6459"symlink(src, dst, target_is_directory=False)\n\n\
6460Create a symbolic link pointing to src named dst.\n\
6461target_is_directory is required if the target is to be interpreted as\n\
6462a directory.\n\
6463This function requires Windows 6.0 or greater, and raises a\n\
6464NotImplementedError otherwise.");
6465
6466static PyObject *
6467win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6468{
6469 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6470 PyObject *src, *dest;
6471 int target_is_directory = 0;
6472 DWORD res;
6473 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006474
Brian Curtind40e6f72010-07-08 21:39:08 +00006475 if (!check_CreateSymbolicLinkW())
6476 {
6477 /* raise NotImplementedError */
6478 return PyErr_Format(PyExc_NotImplementedError,
6479 "CreateSymbolicLinkW not found");
6480 }
6481 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6482 kwlist, &src, &dest, &target_is_directory))
6483 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006484
6485 if (win32_can_symlink == 0)
6486 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6487
Brian Curtind40e6f72010-07-08 21:39:08 +00006488 if (!convert_to_unicode(&src)) { return NULL; }
6489 if (!convert_to_unicode(&dest)) {
6490 Py_DECREF(src);
6491 return NULL;
6492 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006493
Brian Curtind40e6f72010-07-08 21:39:08 +00006494 /* if src is a directory, ensure target_is_directory==1 */
6495 if(
6496 GetFileAttributesExW(
6497 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6498 ))
6499 {
6500 target_is_directory = target_is_directory ||
6501 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6502 }
6503
6504 Py_BEGIN_ALLOW_THREADS
6505 res = Py_CreateSymbolicLinkW(
6506 PyUnicode_AsUnicode(dest),
6507 PyUnicode_AsUnicode(src),
6508 target_is_directory);
6509 Py_END_ALLOW_THREADS
6510 Py_DECREF(src);
6511 Py_DECREF(dest);
6512 if (!res)
6513 {
6514 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6515 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006516
Brian Curtind40e6f72010-07-08 21:39:08 +00006517 Py_INCREF(Py_None);
6518 return Py_None;
6519}
Brian Curtin52173d42010-12-02 18:29:18 +00006520#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006521
6522#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006523#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6524static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006525system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006526{
6527 ULONG value = 0;
6528
6529 Py_BEGIN_ALLOW_THREADS
6530 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6531 Py_END_ALLOW_THREADS
6532
6533 return value;
6534}
6535
6536static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006537posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006538{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006539 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006540 return Py_BuildValue("ddddd",
6541 (double)0 /* t.tms_utime / HZ */,
6542 (double)0 /* t.tms_stime / HZ */,
6543 (double)0 /* t.tms_cutime / HZ */,
6544 (double)0 /* t.tms_cstime / HZ */,
6545 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006546}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006547#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006548#define NEED_TICKS_PER_SECOND
6549static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006550static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006551posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006552{
Victor Stinner8c62be82010-05-06 00:08:46 +00006553 struct tms t;
6554 clock_t c;
6555 errno = 0;
6556 c = times(&t);
6557 if (c == (clock_t) -1)
6558 return posix_error();
6559 return Py_BuildValue("ddddd",
6560 (double)t.tms_utime / ticks_per_second,
6561 (double)t.tms_stime / ticks_per_second,
6562 (double)t.tms_cutime / ticks_per_second,
6563 (double)t.tms_cstime / ticks_per_second,
6564 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006565}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006566#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006567#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006568
6569
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006570#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006571#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006572static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006573posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006574{
Victor Stinner8c62be82010-05-06 00:08:46 +00006575 FILETIME create, exit, kernel, user;
6576 HANDLE hProc;
6577 hProc = GetCurrentProcess();
6578 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6579 /* The fields of a FILETIME structure are the hi and lo part
6580 of a 64-bit value expressed in 100 nanosecond units.
6581 1e7 is one second in such units; 1e-7 the inverse.
6582 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6583 */
6584 return Py_BuildValue(
6585 "ddddd",
6586 (double)(user.dwHighDateTime*429.4967296 +
6587 user.dwLowDateTime*1e-7),
6588 (double)(kernel.dwHighDateTime*429.4967296 +
6589 kernel.dwLowDateTime*1e-7),
6590 (double)0,
6591 (double)0,
6592 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006593}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006594#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006595
6596#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006597PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006598"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006599Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006600#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006602
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006603#ifdef HAVE_GETSID
6604PyDoc_STRVAR(posix_getsid__doc__,
6605"getsid(pid) -> sid\n\n\
6606Call the system call getsid().");
6607
6608static PyObject *
6609posix_getsid(PyObject *self, PyObject *args)
6610{
Victor Stinner8c62be82010-05-06 00:08:46 +00006611 pid_t pid;
6612 int sid;
6613 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6614 return NULL;
6615 sid = getsid(pid);
6616 if (sid < 0)
6617 return posix_error();
6618 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006619}
6620#endif /* HAVE_GETSID */
6621
6622
Guido van Rossumb6775db1994-08-01 11:34:53 +00006623#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006624PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006625"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006626Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006627
Barry Warsaw53699e91996-12-10 23:23:01 +00006628static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006629posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006630{
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 if (setsid() < 0)
6632 return posix_error();
6633 Py_INCREF(Py_None);
6634 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006635}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006636#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006637
Guido van Rossumb6775db1994-08-01 11:34:53 +00006638#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006639PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006640"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006641Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006642
Barry Warsaw53699e91996-12-10 23:23:01 +00006643static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006644posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006645{
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 pid_t pid;
6647 int pgrp;
6648 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6649 return NULL;
6650 if (setpgid(pid, pgrp) < 0)
6651 return posix_error();
6652 Py_INCREF(Py_None);
6653 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006654}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006655#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006656
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006657
Guido van Rossumb6775db1994-08-01 11:34:53 +00006658#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006659PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006660"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006661Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006662
Barry Warsaw53699e91996-12-10 23:23:01 +00006663static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006664posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006665{
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 int fd;
6667 pid_t pgid;
6668 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6669 return NULL;
6670 pgid = tcgetpgrp(fd);
6671 if (pgid < 0)
6672 return posix_error();
6673 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006674}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006675#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006677
Guido van Rossumb6775db1994-08-01 11:34:53 +00006678#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006679PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006680"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006681Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006682
Barry Warsaw53699e91996-12-10 23:23:01 +00006683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006684posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006685{
Victor Stinner8c62be82010-05-06 00:08:46 +00006686 int fd;
6687 pid_t pgid;
6688 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6689 return NULL;
6690 if (tcsetpgrp(fd, pgid) < 0)
6691 return posix_error();
6692 Py_INCREF(Py_None);
6693 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006694}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006695#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006696
Guido van Rossum687dd131993-05-17 08:34:16 +00006697/* Functions acting on file descriptors */
6698
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006699PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006700"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006701Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006702
Barry Warsaw53699e91996-12-10 23:23:01 +00006703static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006704posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006705{
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 PyObject *ofile;
6707 char *file;
6708 int flag;
6709 int mode = 0777;
6710 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006711
6712#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006713 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006714 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 Py_BEGIN_ALLOW_THREADS
6716 /* PyUnicode_AS_UNICODE OK without thread
6717 lock as it is a simple dereference. */
6718 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6719 Py_END_ALLOW_THREADS
6720 if (fd < 0)
6721 return posix_error();
6722 return PyLong_FromLong((long)fd);
6723 }
6724 /* Drop the argument parsing error as narrow strings
6725 are also valid. */
6726 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006727#endif
6728
Victor Stinner26de69d2011-06-17 15:15:38 +02006729 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 PyUnicode_FSConverter, &ofile,
6731 &flag, &mode))
6732 return NULL;
6733 file = PyBytes_AsString(ofile);
6734 Py_BEGIN_ALLOW_THREADS
6735 fd = open(file, flag, mode);
6736 Py_END_ALLOW_THREADS
6737 if (fd < 0)
6738 return posix_error_with_allocated_filename(ofile);
6739 Py_DECREF(ofile);
6740 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006741}
6742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006744PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006745"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006746Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006747
Barry Warsaw53699e91996-12-10 23:23:01 +00006748static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006749posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006750{
Victor Stinner8c62be82010-05-06 00:08:46 +00006751 int fd, res;
6752 if (!PyArg_ParseTuple(args, "i:close", &fd))
6753 return NULL;
6754 if (!_PyVerify_fd(fd))
6755 return posix_error();
6756 Py_BEGIN_ALLOW_THREADS
6757 res = close(fd);
6758 Py_END_ALLOW_THREADS
6759 if (res < 0)
6760 return posix_error();
6761 Py_INCREF(Py_None);
6762 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006763}
6764
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006765
Victor Stinner8c62be82010-05-06 00:08:46 +00006766PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006767"closerange(fd_low, fd_high)\n\n\
6768Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6769
6770static PyObject *
6771posix_closerange(PyObject *self, PyObject *args)
6772{
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 int fd_from, fd_to, i;
6774 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6775 return NULL;
6776 Py_BEGIN_ALLOW_THREADS
6777 for (i = fd_from; i < fd_to; i++)
6778 if (_PyVerify_fd(i))
6779 close(i);
6780 Py_END_ALLOW_THREADS
6781 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006782}
6783
6784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006785PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006786"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006787Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006788
Barry Warsaw53699e91996-12-10 23:23:01 +00006789static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006790posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006791{
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 int fd;
6793 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6794 return NULL;
6795 if (!_PyVerify_fd(fd))
6796 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006797 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 if (fd < 0)
6799 return posix_error();
6800 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006801}
6802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006803
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006804PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006805"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006806Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006807
Barry Warsaw53699e91996-12-10 23:23:01 +00006808static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006809posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006810{
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 int fd, fd2, res;
6812 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6813 return NULL;
6814 if (!_PyVerify_fd_dup2(fd, fd2))
6815 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 if (res < 0)
6818 return posix_error();
6819 Py_INCREF(Py_None);
6820 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006821}
6822
Ross Lagerwall7807c352011-03-17 20:20:30 +02006823#ifdef HAVE_LOCKF
6824PyDoc_STRVAR(posix_lockf__doc__,
6825"lockf(fd, cmd, len)\n\n\
6826Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6827fd is an open file descriptor.\n\
6828cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6829F_TEST.\n\
6830len specifies the section of the file to lock.");
6831
6832static PyObject *
6833posix_lockf(PyObject *self, PyObject *args)
6834{
6835 int fd, cmd, res;
6836 off_t len;
6837 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6838 &fd, &cmd, _parse_off_t, &len))
6839 return NULL;
6840
6841 Py_BEGIN_ALLOW_THREADS
6842 res = lockf(fd, cmd, len);
6843 Py_END_ALLOW_THREADS
6844
6845 if (res < 0)
6846 return posix_error();
6847
6848 Py_RETURN_NONE;
6849}
6850#endif
6851
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006853PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006854"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006855Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006856
Barry Warsaw53699e91996-12-10 23:23:01 +00006857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006858posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006859{
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006861#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006863#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006865#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006866 PyObject *posobj;
6867 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006869#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6871 switch (how) {
6872 case 0: how = SEEK_SET; break;
6873 case 1: how = SEEK_CUR; break;
6874 case 2: how = SEEK_END; break;
6875 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006876#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006877
Ross Lagerwall8e749672011-03-17 21:54:07 +02006878#if !defined(HAVE_LARGEFILE_SUPPORT)
6879 pos = PyLong_AsLong(posobj);
6880#else
6881 pos = PyLong_AsLongLong(posobj);
6882#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 if (PyErr_Occurred())
6884 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006885
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 if (!_PyVerify_fd(fd))
6887 return posix_error();
6888 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006889#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006890 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006891#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006893#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 Py_END_ALLOW_THREADS
6895 if (res < 0)
6896 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006897
6898#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006899 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006900#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006902#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006903}
6904
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006906PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006907"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006908Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006909
Barry Warsaw53699e91996-12-10 23:23:01 +00006910static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006911posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006912{
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 int fd, size;
6914 Py_ssize_t n;
6915 PyObject *buffer;
6916 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6917 return NULL;
6918 if (size < 0) {
6919 errno = EINVAL;
6920 return posix_error();
6921 }
6922 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6923 if (buffer == NULL)
6924 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006925 if (!_PyVerify_fd(fd)) {
6926 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006928 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006929 Py_BEGIN_ALLOW_THREADS
6930 n = read(fd, PyBytes_AS_STRING(buffer), size);
6931 Py_END_ALLOW_THREADS
6932 if (n < 0) {
6933 Py_DECREF(buffer);
6934 return posix_error();
6935 }
6936 if (n != size)
6937 _PyBytes_Resize(&buffer, n);
6938 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006939}
6940
Ross Lagerwall7807c352011-03-17 20:20:30 +02006941#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6942 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006943static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006944iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6945{
6946 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006947 Py_ssize_t blen, total = 0;
6948
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006949 *iov = PyMem_New(struct iovec, cnt);
6950 if (*iov == NULL) {
6951 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006952 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006953 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006954
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006955 *buf = PyMem_New(Py_buffer, cnt);
6956 if (*buf == NULL) {
6957 PyMem_Del(*iov);
6958 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006959 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006960 }
6961
6962 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006963 PyObject *item = PySequence_GetItem(seq, i);
6964 if (item == NULL)
6965 goto fail;
6966 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6967 Py_DECREF(item);
6968 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006969 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006970 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006971 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006972 blen = (*buf)[i].len;
6973 (*iov)[i].iov_len = blen;
6974 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006975 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006976 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006977
6978fail:
6979 PyMem_Del(*iov);
6980 for (j = 0; j < i; j++) {
6981 PyBuffer_Release(&(*buf)[j]);
6982 }
6983 PyMem_Del(*buf);
6984 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006985}
6986
6987static void
6988iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6989{
6990 int i;
6991 PyMem_Del(iov);
6992 for (i = 0; i < cnt; i++) {
6993 PyBuffer_Release(&buf[i]);
6994 }
6995 PyMem_Del(buf);
6996}
6997#endif
6998
Ross Lagerwall7807c352011-03-17 20:20:30 +02006999#ifdef HAVE_READV
7000PyDoc_STRVAR(posix_readv__doc__,
7001"readv(fd, buffers) -> bytesread\n\n\
7002Read from a file descriptor into a number of writable buffers. buffers\n\
7003is an arbitrary sequence of writable buffers.\n\
7004Returns the total number of bytes read.");
7005
7006static PyObject *
7007posix_readv(PyObject *self, PyObject *args)
7008{
7009 int fd, cnt;
7010 Py_ssize_t n;
7011 PyObject *seq;
7012 struct iovec *iov;
7013 Py_buffer *buf;
7014
7015 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7016 return NULL;
7017 if (!PySequence_Check(seq)) {
7018 PyErr_SetString(PyExc_TypeError,
7019 "readv() arg 2 must be a sequence");
7020 return NULL;
7021 }
7022 cnt = PySequence_Size(seq);
7023
7024 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7025 return NULL;
7026
7027 Py_BEGIN_ALLOW_THREADS
7028 n = readv(fd, iov, cnt);
7029 Py_END_ALLOW_THREADS
7030
7031 iov_cleanup(iov, buf, cnt);
7032 return PyLong_FromSsize_t(n);
7033}
7034#endif
7035
7036#ifdef HAVE_PREAD
7037PyDoc_STRVAR(posix_pread__doc__,
7038"pread(fd, buffersize, offset) -> string\n\n\
7039Read from a file descriptor, fd, at a position of offset. It will read up\n\
7040to buffersize number of bytes. The file offset remains unchanged.");
7041
7042static PyObject *
7043posix_pread(PyObject *self, PyObject *args)
7044{
7045 int fd, size;
7046 off_t offset;
7047 Py_ssize_t n;
7048 PyObject *buffer;
7049 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7050 return NULL;
7051
7052 if (size < 0) {
7053 errno = EINVAL;
7054 return posix_error();
7055 }
7056 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7057 if (buffer == NULL)
7058 return NULL;
7059 if (!_PyVerify_fd(fd)) {
7060 Py_DECREF(buffer);
7061 return posix_error();
7062 }
7063 Py_BEGIN_ALLOW_THREADS
7064 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7065 Py_END_ALLOW_THREADS
7066 if (n < 0) {
7067 Py_DECREF(buffer);
7068 return posix_error();
7069 }
7070 if (n != size)
7071 _PyBytes_Resize(&buffer, n);
7072 return buffer;
7073}
7074#endif
7075
7076PyDoc_STRVAR(posix_write__doc__,
7077"write(fd, string) -> byteswritten\n\n\
7078Write a string to a file descriptor.");
7079
7080static PyObject *
7081posix_write(PyObject *self, PyObject *args)
7082{
7083 Py_buffer pbuf;
7084 int fd;
7085 Py_ssize_t size, len;
7086
7087 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7088 return NULL;
7089 if (!_PyVerify_fd(fd)) {
7090 PyBuffer_Release(&pbuf);
7091 return posix_error();
7092 }
7093 len = pbuf.len;
7094 Py_BEGIN_ALLOW_THREADS
7095#if defined(MS_WIN64) || defined(MS_WINDOWS)
7096 if (len > INT_MAX)
7097 len = INT_MAX;
7098 size = write(fd, pbuf.buf, (int)len);
7099#else
7100 size = write(fd, pbuf.buf, len);
7101#endif
7102 Py_END_ALLOW_THREADS
7103 PyBuffer_Release(&pbuf);
7104 if (size < 0)
7105 return posix_error();
7106 return PyLong_FromSsize_t(size);
7107}
7108
7109#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007110PyDoc_STRVAR(posix_sendfile__doc__,
7111"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7112sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7113 -> byteswritten\n\
7114Copy nbytes bytes from file descriptor in to file descriptor out.");
7115
7116static PyObject *
7117posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7118{
7119 int in, out;
7120 Py_ssize_t ret;
7121 off_t offset;
7122
7123#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7124#ifndef __APPLE__
7125 Py_ssize_t len;
7126#endif
7127 PyObject *headers = NULL, *trailers = NULL;
7128 Py_buffer *hbuf, *tbuf;
7129 off_t sbytes;
7130 struct sf_hdtr sf;
7131 int flags = 0;
7132 sf.headers = NULL;
7133 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007134 static char *keywords[] = {"out", "in",
7135 "offset", "count",
7136 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007137
7138#ifdef __APPLE__
7139 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007140 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007141#else
7142 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007143 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007144#endif
7145 &headers, &trailers, &flags))
7146 return NULL;
7147 if (headers != NULL) {
7148 if (!PySequence_Check(headers)) {
7149 PyErr_SetString(PyExc_TypeError,
7150 "sendfile() headers must be a sequence or None");
7151 return NULL;
7152 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007153 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007154 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007155 if (sf.hdr_cnt > 0 &&
7156 !(i = iov_setup(&(sf.headers), &hbuf,
7157 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007158 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007159#ifdef __APPLE__
7160 sbytes += i;
7161#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007162 }
7163 }
7164 if (trailers != NULL) {
7165 if (!PySequence_Check(trailers)) {
7166 PyErr_SetString(PyExc_TypeError,
7167 "sendfile() trailers must be a sequence or None");
7168 return NULL;
7169 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007170 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007171 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007172 if (sf.trl_cnt > 0 &&
7173 !(i = iov_setup(&(sf.trailers), &tbuf,
7174 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007175 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007176#ifdef __APPLE__
7177 sbytes += i;
7178#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007179 }
7180 }
7181
7182 Py_BEGIN_ALLOW_THREADS
7183#ifdef __APPLE__
7184 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7185#else
7186 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7187#endif
7188 Py_END_ALLOW_THREADS
7189
7190 if (sf.headers != NULL)
7191 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7192 if (sf.trailers != NULL)
7193 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7194
7195 if (ret < 0) {
7196 if ((errno == EAGAIN) || (errno == EBUSY)) {
7197 if (sbytes != 0) {
7198 // some data has been sent
7199 goto done;
7200 }
7201 else {
7202 // no data has been sent; upper application is supposed
7203 // to retry on EAGAIN or EBUSY
7204 return posix_error();
7205 }
7206 }
7207 return posix_error();
7208 }
7209 goto done;
7210
7211done:
7212 #if !defined(HAVE_LARGEFILE_SUPPORT)
7213 return Py_BuildValue("l", sbytes);
7214 #else
7215 return Py_BuildValue("L", sbytes);
7216 #endif
7217
7218#else
7219 Py_ssize_t count;
7220 PyObject *offobj;
7221 static char *keywords[] = {"out", "in",
7222 "offset", "count", NULL};
7223 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7224 keywords, &out, &in, &offobj, &count))
7225 return NULL;
7226#ifdef linux
7227 if (offobj == Py_None) {
7228 Py_BEGIN_ALLOW_THREADS
7229 ret = sendfile(out, in, NULL, count);
7230 Py_END_ALLOW_THREADS
7231 if (ret < 0)
7232 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007233 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007234 }
7235#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007236 if (!_parse_off_t(offobj, &offset))
7237 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007238 Py_BEGIN_ALLOW_THREADS
7239 ret = sendfile(out, in, &offset, count);
7240 Py_END_ALLOW_THREADS
7241 if (ret < 0)
7242 return posix_error();
7243 return Py_BuildValue("n", ret);
7244#endif
7245}
7246#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007248PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007249"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007250Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007251
Barry Warsaw53699e91996-12-10 23:23:01 +00007252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007253posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007254{
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 int fd;
7256 STRUCT_STAT st;
7257 int res;
7258 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7259 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007260#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 /* on OpenVMS we must ensure that all bytes are written to the file */
7262 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007263#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 if (!_PyVerify_fd(fd))
7265 return posix_error();
7266 Py_BEGIN_ALLOW_THREADS
7267 res = FSTAT(fd, &st);
7268 Py_END_ALLOW_THREADS
7269 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007270#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007272#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007274#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 }
Tim Peters5aa91602002-01-30 05:46:57 +00007276
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007278}
7279
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007280PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007281"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007282Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007283connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007284
7285static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007286posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007287{
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 int fd;
7289 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7290 return NULL;
7291 if (!_PyVerify_fd(fd))
7292 return PyBool_FromLong(0);
7293 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007294}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007295
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007296#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007297PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007298"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007299Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007300
Barry Warsaw53699e91996-12-10 23:23:01 +00007301static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007302posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007303{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007304#if defined(PYOS_OS2)
7305 HFILE read, write;
7306 APIRET rc;
7307
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007308 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007309 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007310 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007311
7312 return Py_BuildValue("(ii)", read, write);
7313#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007314#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 int fds[2];
7316 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 if (res != 0)
7319 return posix_error();
7320 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007321#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007322 HANDLE read, write;
7323 int read_fd, write_fd;
7324 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007325 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 if (!ok)
7327 return win32_error("CreatePipe", NULL);
7328 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7329 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7330 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007331#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007332#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007333}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007334#endif /* HAVE_PIPE */
7335
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007336#ifdef HAVE_PIPE2
7337PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007338"pipe2(flags) -> (read_end, write_end)\n\n\
7339Create a pipe with flags set atomically.\n\
7340flags can be constructed by ORing together one or more of these values:\n\
7341O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007342");
7343
7344static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007345posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007346{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007347 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007348 int fds[2];
7349 int res;
7350
Charles-François Natali368f34b2011-06-06 19:49:47 +02007351 flags = PyLong_AsLong(arg);
7352 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007353 return NULL;
7354
7355 res = pipe2(fds, flags);
7356 if (res != 0)
7357 return posix_error();
7358 return Py_BuildValue("(ii)", fds[0], fds[1]);
7359}
7360#endif /* HAVE_PIPE2 */
7361
Ross Lagerwall7807c352011-03-17 20:20:30 +02007362#ifdef HAVE_WRITEV
7363PyDoc_STRVAR(posix_writev__doc__,
7364"writev(fd, buffers) -> byteswritten\n\n\
7365Write the contents of buffers to a file descriptor, where buffers is an\n\
7366arbitrary sequence of buffers.\n\
7367Returns the total bytes written.");
7368
7369static PyObject *
7370posix_writev(PyObject *self, PyObject *args)
7371{
7372 int fd, cnt;
7373 Py_ssize_t res;
7374 PyObject *seq;
7375 struct iovec *iov;
7376 Py_buffer *buf;
7377 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7378 return NULL;
7379 if (!PySequence_Check(seq)) {
7380 PyErr_SetString(PyExc_TypeError,
7381 "writev() arg 2 must be a sequence");
7382 return NULL;
7383 }
7384 cnt = PySequence_Size(seq);
7385
7386 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7387 return NULL;
7388 }
7389
7390 Py_BEGIN_ALLOW_THREADS
7391 res = writev(fd, iov, cnt);
7392 Py_END_ALLOW_THREADS
7393
7394 iov_cleanup(iov, buf, cnt);
7395 return PyLong_FromSsize_t(res);
7396}
7397#endif
7398
7399#ifdef HAVE_PWRITE
7400PyDoc_STRVAR(posix_pwrite__doc__,
7401"pwrite(fd, string, offset) -> byteswritten\n\n\
7402Write string to a file descriptor, fd, from offset, leaving the file\n\
7403offset unchanged.");
7404
7405static PyObject *
7406posix_pwrite(PyObject *self, PyObject *args)
7407{
7408 Py_buffer pbuf;
7409 int fd;
7410 off_t offset;
7411 Py_ssize_t size;
7412
7413 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7414 return NULL;
7415
7416 if (!_PyVerify_fd(fd)) {
7417 PyBuffer_Release(&pbuf);
7418 return posix_error();
7419 }
7420 Py_BEGIN_ALLOW_THREADS
7421 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7422 Py_END_ALLOW_THREADS
7423 PyBuffer_Release(&pbuf);
7424 if (size < 0)
7425 return posix_error();
7426 return PyLong_FromSsize_t(size);
7427}
7428#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007429
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007430#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007431PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007432"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007433Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007434
Barry Warsaw53699e91996-12-10 23:23:01 +00007435static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007436posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007437{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007438 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007439 char *filename;
7440 int mode = 0666;
7441 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007442 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7443 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007445 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007446 Py_BEGIN_ALLOW_THREADS
7447 res = mkfifo(filename, mode);
7448 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007449 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 if (res < 0)
7451 return posix_error();
7452 Py_INCREF(Py_None);
7453 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007454}
7455#endif
7456
7457
Neal Norwitz11690112002-07-30 01:08:28 +00007458#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007459PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007460"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007461Create a filesystem node (file, device special file or named pipe)\n\
7462named filename. mode specifies both the permissions to use and the\n\
7463type of node to be created, being combined (bitwise OR) with one of\n\
7464S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007465device defines the newly created device special file (probably using\n\
7466os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007467
7468
7469static PyObject *
7470posix_mknod(PyObject *self, PyObject *args)
7471{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007472 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007473 char *filename;
7474 int mode = 0600;
7475 int device = 0;
7476 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007477 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7478 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007480 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007481 Py_BEGIN_ALLOW_THREADS
7482 res = mknod(filename, mode, device);
7483 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007484 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 if (res < 0)
7486 return posix_error();
7487 Py_INCREF(Py_None);
7488 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007489}
7490#endif
7491
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007492#ifdef HAVE_DEVICE_MACROS
7493PyDoc_STRVAR(posix_major__doc__,
7494"major(device) -> major number\n\
7495Extracts a device major number from a raw device number.");
7496
7497static PyObject *
7498posix_major(PyObject *self, PyObject *args)
7499{
Victor Stinner8c62be82010-05-06 00:08:46 +00007500 int device;
7501 if (!PyArg_ParseTuple(args, "i:major", &device))
7502 return NULL;
7503 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007504}
7505
7506PyDoc_STRVAR(posix_minor__doc__,
7507"minor(device) -> minor number\n\
7508Extracts a device minor number from a raw device number.");
7509
7510static PyObject *
7511posix_minor(PyObject *self, PyObject *args)
7512{
Victor Stinner8c62be82010-05-06 00:08:46 +00007513 int device;
7514 if (!PyArg_ParseTuple(args, "i:minor", &device))
7515 return NULL;
7516 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007517}
7518
7519PyDoc_STRVAR(posix_makedev__doc__,
7520"makedev(major, minor) -> device number\n\
7521Composes a raw device number from the major and minor device numbers.");
7522
7523static PyObject *
7524posix_makedev(PyObject *self, PyObject *args)
7525{
Victor Stinner8c62be82010-05-06 00:08:46 +00007526 int major, minor;
7527 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7528 return NULL;
7529 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007530}
7531#endif /* device macros */
7532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007533
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007534#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007535PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007536"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007537Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007538
Barry Warsaw53699e91996-12-10 23:23:01 +00007539static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007540posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007541{
Victor Stinner8c62be82010-05-06 00:08:46 +00007542 int fd;
7543 off_t length;
7544 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007545
Ross Lagerwall7807c352011-03-17 20:20:30 +02007546 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007547 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007548
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 Py_BEGIN_ALLOW_THREADS
7550 res = ftruncate(fd, length);
7551 Py_END_ALLOW_THREADS
7552 if (res < 0)
7553 return posix_error();
7554 Py_INCREF(Py_None);
7555 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007556}
7557#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007558
Ross Lagerwall7807c352011-03-17 20:20:30 +02007559#ifdef HAVE_TRUNCATE
7560PyDoc_STRVAR(posix_truncate__doc__,
7561"truncate(path, length)\n\n\
7562Truncate the file given by path to length bytes.");
7563
7564static PyObject *
7565posix_truncate(PyObject *self, PyObject *args)
7566{
7567 PyObject *opath;
7568 const char *path;
7569 off_t length;
7570 int res;
7571
7572 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7573 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7574 return NULL;
7575 path = PyBytes_AsString(opath);
7576
7577 Py_BEGIN_ALLOW_THREADS
7578 res = truncate(path, length);
7579 Py_END_ALLOW_THREADS
7580 Py_DECREF(opath);
7581 if (res < 0)
7582 return posix_error();
7583 Py_RETURN_NONE;
7584}
7585#endif
7586
7587#ifdef HAVE_POSIX_FALLOCATE
7588PyDoc_STRVAR(posix_posix_fallocate__doc__,
7589"posix_fallocate(fd, offset, len)\n\n\
7590Ensures that enough disk space is allocated for the file specified by fd\n\
7591starting from offset and continuing for len bytes.");
7592
7593static PyObject *
7594posix_posix_fallocate(PyObject *self, PyObject *args)
7595{
7596 off_t len, offset;
7597 int res, fd;
7598
7599 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7600 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7601 return NULL;
7602
7603 Py_BEGIN_ALLOW_THREADS
7604 res = posix_fallocate(fd, offset, len);
7605 Py_END_ALLOW_THREADS
7606 if (res != 0) {
7607 errno = res;
7608 return posix_error();
7609 }
7610 Py_RETURN_NONE;
7611}
7612#endif
7613
7614#ifdef HAVE_POSIX_FADVISE
7615PyDoc_STRVAR(posix_posix_fadvise__doc__,
7616"posix_fadvise(fd, offset, len, advice)\n\n\
7617Announces an intention to access data in a specific pattern thus allowing\n\
7618the kernel to make optimizations.\n\
7619The advice applies to the region of the file specified by fd starting at\n\
7620offset and continuing for len bytes.\n\
7621advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7622POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7623POSIX_FADV_DONTNEED.");
7624
7625static PyObject *
7626posix_posix_fadvise(PyObject *self, PyObject *args)
7627{
7628 off_t len, offset;
7629 int res, fd, advice;
7630
7631 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7632 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7633 return NULL;
7634
7635 Py_BEGIN_ALLOW_THREADS
7636 res = posix_fadvise(fd, offset, len, advice);
7637 Py_END_ALLOW_THREADS
7638 if (res != 0) {
7639 errno = res;
7640 return posix_error();
7641 }
7642 Py_RETURN_NONE;
7643}
7644#endif
7645
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007646#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007647PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007648"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007649Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007650
Fred Drake762e2061999-08-26 17:23:54 +00007651/* Save putenv() parameters as values here, so we can collect them when they
7652 * get re-set with another call for the same key. */
7653static PyObject *posix_putenv_garbage;
7654
Tim Peters5aa91602002-01-30 05:46:57 +00007655static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007656posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007657{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007658#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007659 wchar_t *s1, *s2;
7660 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007661#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007662 PyObject *os1, *os2;
7663 char *s1, *s2;
7664 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007665#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007666 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007668
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007669#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 if (!PyArg_ParseTuple(args,
7671 "uu:putenv",
7672 &s1, &s2))
7673 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007674#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007675 if (!PyArg_ParseTuple(args,
7676 "O&O&:putenv",
7677 PyUnicode_FSConverter, &os1,
7678 PyUnicode_FSConverter, &os2))
7679 return NULL;
7680 s1 = PyBytes_AsString(os1);
7681 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007682#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007683
7684#if defined(PYOS_OS2)
7685 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7686 APIRET rc;
7687
Guido van Rossumd48f2521997-12-05 22:19:34 +00007688 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007689 if (rc != NO_ERROR) {
7690 os2_error(rc);
7691 goto error;
7692 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007693
7694 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7695 APIRET rc;
7696
Guido van Rossumd48f2521997-12-05 22:19:34 +00007697 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007698 if (rc != NO_ERROR) {
7699 os2_error(rc);
7700 goto error;
7701 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007702 } else {
7703#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007704 /* XXX This can leak memory -- not easy to fix :-( */
7705 /* len includes space for a trailing \0; the size arg to
7706 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007707#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007708 len = wcslen(s1) + wcslen(s2) + 2;
7709 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007710#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007711 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007713#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007714 if (newstr == NULL) {
7715 PyErr_NoMemory();
7716 goto error;
7717 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007718#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 newenv = PyUnicode_AsUnicode(newstr);
7720 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7721 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007723 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007724 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007725#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007726 newenv = PyBytes_AS_STRING(newstr);
7727 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7728 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007730 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007732#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007733
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 /* Install the first arg and newstr in posix_putenv_garbage;
7735 * this will cause previous value to be collected. This has to
7736 * happen after the real putenv() call because the old value
7737 * was still accessible until then. */
7738 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007739#ifdef MS_WINDOWS
7740 PyTuple_GET_ITEM(args, 0),
7741#else
7742 os1,
7743#endif
7744 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007745 /* really not much we can do; just leak */
7746 PyErr_Clear();
7747 }
7748 else {
7749 Py_DECREF(newstr);
7750 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007751
7752#if defined(PYOS_OS2)
7753 }
7754#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007755
Martin v. Löwis011e8422009-05-05 04:43:17 +00007756#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 Py_DECREF(os1);
7758 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007759#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007760 Py_RETURN_NONE;
7761
7762error:
7763#ifndef MS_WINDOWS
7764 Py_DECREF(os1);
7765 Py_DECREF(os2);
7766#endif
7767 Py_XDECREF(newstr);
7768 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007769}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007770#endif /* putenv */
7771
Guido van Rossumc524d952001-10-19 01:31:59 +00007772#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007773PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007774"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007775Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007776
7777static PyObject *
7778posix_unsetenv(PyObject *self, PyObject *args)
7779{
Victor Stinner84ae1182010-05-06 22:05:07 +00007780#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007782
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7784 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007785#else
7786 PyObject *os1;
7787 char *s1;
7788
7789 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7790 PyUnicode_FSConverter, &os1))
7791 return NULL;
7792 s1 = PyBytes_AsString(os1);
7793#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007794
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007796
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 /* Remove the key from posix_putenv_garbage;
7798 * this will cause it to be collected. This has to
7799 * happen after the real unsetenv() call because the
7800 * old value was still accessible until then.
7801 */
7802 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007803#ifdef MS_WINDOWS
7804 PyTuple_GET_ITEM(args, 0)
7805#else
7806 os1
7807#endif
7808 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 /* really not much we can do; just leak */
7810 PyErr_Clear();
7811 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007812
Victor Stinner84ae1182010-05-06 22:05:07 +00007813#ifndef MS_WINDOWS
7814 Py_DECREF(os1);
7815#endif
7816 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007817}
7818#endif /* unsetenv */
7819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007820PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007821"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007822Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007823
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007824static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007825posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007826{
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 int code;
7828 char *message;
7829 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7830 return NULL;
7831 message = strerror(code);
7832 if (message == NULL) {
7833 PyErr_SetString(PyExc_ValueError,
7834 "strerror() argument out of range");
7835 return NULL;
7836 }
7837 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007838}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007839
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007840
Guido van Rossumc9641791998-08-04 15:26:23 +00007841#ifdef HAVE_SYS_WAIT_H
7842
Fred Drake106c1a02002-04-23 15:58:02 +00007843#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007844PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007845"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007846Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007847
7848static PyObject *
7849posix_WCOREDUMP(PyObject *self, PyObject *args)
7850{
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 WAIT_TYPE status;
7852 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007853
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7855 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007856
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007858}
7859#endif /* WCOREDUMP */
7860
7861#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007862PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007863"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007864Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007865job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007866
7867static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007868posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007869{
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 WAIT_TYPE status;
7871 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007872
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7874 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007875
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007877}
7878#endif /* WIFCONTINUED */
7879
Guido van Rossumc9641791998-08-04 15:26:23 +00007880#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007881PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007882"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007883Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007884
7885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007886posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007887{
Victor Stinner8c62be82010-05-06 00:08:46 +00007888 WAIT_TYPE status;
7889 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007890
Victor Stinner8c62be82010-05-06 00:08:46 +00007891 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7892 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007893
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007895}
7896#endif /* WIFSTOPPED */
7897
7898#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007899PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007900"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007901Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007902
7903static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007904posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007905{
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 WAIT_TYPE status;
7907 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007908
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7910 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007911
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007913}
7914#endif /* WIFSIGNALED */
7915
7916#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007917PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007918"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007919Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007920system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007921
7922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007923posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007924{
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 WAIT_TYPE status;
7926 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007927
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7929 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007930
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007932}
7933#endif /* WIFEXITED */
7934
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007935#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007936PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007937"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007938Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007939
7940static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007941posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007942{
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 WAIT_TYPE status;
7944 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007945
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7947 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007948
Victor Stinner8c62be82010-05-06 00:08:46 +00007949 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007950}
7951#endif /* WEXITSTATUS */
7952
7953#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007954PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007955"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007956Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007957value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007958
7959static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007960posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007961{
Victor Stinner8c62be82010-05-06 00:08:46 +00007962 WAIT_TYPE status;
7963 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007964
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7966 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007967
Victor Stinner8c62be82010-05-06 00:08:46 +00007968 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007969}
7970#endif /* WTERMSIG */
7971
7972#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007973PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007974"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007975Return the signal that stopped the process that provided\n\
7976the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007977
7978static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007979posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007980{
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 WAIT_TYPE status;
7982 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007983
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7985 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007986
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007988}
7989#endif /* WSTOPSIG */
7990
7991#endif /* HAVE_SYS_WAIT_H */
7992
7993
Thomas Wouters477c8d52006-05-27 19:21:47 +00007994#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007995#ifdef _SCO_DS
7996/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7997 needed definitions in sys/statvfs.h */
7998#define _SVID3
7999#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008000#include <sys/statvfs.h>
8001
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008002static PyObject*
8003_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8005 if (v == NULL)
8006 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008007
8008#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008009 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8010 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8011 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8012 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8013 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8014 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8015 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8016 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8017 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8018 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008019#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8021 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8022 PyStructSequence_SET_ITEM(v, 2,
8023 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8024 PyStructSequence_SET_ITEM(v, 3,
8025 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8026 PyStructSequence_SET_ITEM(v, 4,
8027 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8028 PyStructSequence_SET_ITEM(v, 5,
8029 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8030 PyStructSequence_SET_ITEM(v, 6,
8031 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8032 PyStructSequence_SET_ITEM(v, 7,
8033 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8034 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8035 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008036#endif
8037
Victor Stinner8c62be82010-05-06 00:08:46 +00008038 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008039}
8040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008041PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008042"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008043Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008044
8045static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008046posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008047{
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 int fd, res;
8049 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008050
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8052 return NULL;
8053 Py_BEGIN_ALLOW_THREADS
8054 res = fstatvfs(fd, &st);
8055 Py_END_ALLOW_THREADS
8056 if (res != 0)
8057 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008058
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008060}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008061#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008062
8063
Thomas Wouters477c8d52006-05-27 19:21:47 +00008064#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008065#include <sys/statvfs.h>
8066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008067PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008068"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008069Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008070
8071static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008072posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008073{
Victor Stinner8c62be82010-05-06 00:08:46 +00008074 char *path;
8075 int res;
8076 struct statvfs st;
8077 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
8078 return NULL;
8079 Py_BEGIN_ALLOW_THREADS
8080 res = statvfs(path, &st);
8081 Py_END_ALLOW_THREADS
8082 if (res != 0)
8083 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008084
Victor Stinner8c62be82010-05-06 00:08:46 +00008085 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008086}
8087#endif /* HAVE_STATVFS */
8088
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008089#ifdef MS_WINDOWS
8090PyDoc_STRVAR(win32__getdiskusage__doc__,
8091"_getdiskusage(path) -> (total, free)\n\n\
8092Return disk usage statistics about the given path as (total, free) tuple.");
8093
8094static PyObject *
8095win32__getdiskusage(PyObject *self, PyObject *args)
8096{
8097 BOOL retval;
8098 ULARGE_INTEGER _, total, free;
8099 LPCTSTR path;
8100
8101 if (! PyArg_ParseTuple(args, "s", &path))
8102 return NULL;
8103
8104 Py_BEGIN_ALLOW_THREADS
8105 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8106 Py_END_ALLOW_THREADS
8107 if (retval == 0)
8108 return PyErr_SetFromWindowsErr(0);
8109
8110 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8111}
8112#endif
8113
8114
Fred Drakec9680921999-12-13 16:37:25 +00008115/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8116 * It maps strings representing configuration variable names to
8117 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008118 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008119 * rarely-used constants. There are three separate tables that use
8120 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008121 *
8122 * This code is always included, even if none of the interfaces that
8123 * need it are included. The #if hackery needed to avoid it would be
8124 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008125 */
8126struct constdef {
8127 char *name;
8128 long value;
8129};
8130
Fred Drake12c6e2d1999-12-14 21:25:03 +00008131static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008132conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008133 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008134{
Christian Heimes217cfd12007-12-02 14:31:20 +00008135 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008136 *valuep = PyLong_AS_LONG(arg);
8137 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008138 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008139 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008140 /* look up the value in the table using a binary search */
8141 size_t lo = 0;
8142 size_t mid;
8143 size_t hi = tablesize;
8144 int cmp;
8145 const char *confname;
8146 if (!PyUnicode_Check(arg)) {
8147 PyErr_SetString(PyExc_TypeError,
8148 "configuration names must be strings or integers");
8149 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008151 confname = _PyUnicode_AsString(arg);
8152 if (confname == NULL)
8153 return 0;
8154 while (lo < hi) {
8155 mid = (lo + hi) / 2;
8156 cmp = strcmp(confname, table[mid].name);
8157 if (cmp < 0)
8158 hi = mid;
8159 else if (cmp > 0)
8160 lo = mid + 1;
8161 else {
8162 *valuep = table[mid].value;
8163 return 1;
8164 }
8165 }
8166 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8167 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008169}
8170
8171
8172#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8173static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008174#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008175 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008176#endif
8177#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008178 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008179#endif
Fred Drakec9680921999-12-13 16:37:25 +00008180#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008181 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008182#endif
8183#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008184 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008185#endif
8186#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008187 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008188#endif
8189#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008190 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008191#endif
8192#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008193 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008194#endif
8195#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008197#endif
8198#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008199 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008200#endif
8201#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008202 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008203#endif
8204#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008206#endif
8207#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008208 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008209#endif
8210#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008211 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008212#endif
8213#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008215#endif
8216#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008218#endif
8219#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008221#endif
8222#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008224#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008225#ifdef _PC_ACL_ENABLED
8226 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8227#endif
8228#ifdef _PC_MIN_HOLE_SIZE
8229 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8230#endif
8231#ifdef _PC_ALLOC_SIZE_MIN
8232 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8233#endif
8234#ifdef _PC_REC_INCR_XFER_SIZE
8235 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8236#endif
8237#ifdef _PC_REC_MAX_XFER_SIZE
8238 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8239#endif
8240#ifdef _PC_REC_MIN_XFER_SIZE
8241 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8242#endif
8243#ifdef _PC_REC_XFER_ALIGN
8244 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8245#endif
8246#ifdef _PC_SYMLINK_MAX
8247 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8248#endif
8249#ifdef _PC_XATTR_ENABLED
8250 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8251#endif
8252#ifdef _PC_XATTR_EXISTS
8253 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8254#endif
8255#ifdef _PC_TIMESTAMP_RESOLUTION
8256 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8257#endif
Fred Drakec9680921999-12-13 16:37:25 +00008258};
8259
Fred Drakec9680921999-12-13 16:37:25 +00008260static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008261conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008262{
8263 return conv_confname(arg, valuep, posix_constants_pathconf,
8264 sizeof(posix_constants_pathconf)
8265 / sizeof(struct constdef));
8266}
8267#endif
8268
8269#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008270PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008271"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008272Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008273If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008274
8275static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008276posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008277{
8278 PyObject *result = NULL;
8279 int name, fd;
8280
Fred Drake12c6e2d1999-12-14 21:25:03 +00008281 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8282 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008283 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008284
Stefan Krah0e803b32010-11-26 16:16:47 +00008285 errno = 0;
8286 limit = fpathconf(fd, name);
8287 if (limit == -1 && errno != 0)
8288 posix_error();
8289 else
8290 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008291 }
8292 return result;
8293}
8294#endif
8295
8296
8297#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008298PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008299"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008300Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008301If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008302
8303static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008304posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008305{
8306 PyObject *result = NULL;
8307 int name;
8308 char *path;
8309
8310 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8311 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008312 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008313
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 errno = 0;
8315 limit = pathconf(path, name);
8316 if (limit == -1 && errno != 0) {
8317 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008318 /* could be a path or name problem */
8319 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008320 else
Stefan Krah99439262010-11-26 12:58:05 +00008321 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 }
8323 else
8324 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008325 }
8326 return result;
8327}
8328#endif
8329
8330#ifdef HAVE_CONFSTR
8331static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008332#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008334#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008335#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008336 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008337#endif
8338#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008340#endif
Fred Draked86ed291999-12-15 15:34:33 +00008341#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008343#endif
8344#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008346#endif
8347#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008349#endif
8350#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008351 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008352#endif
Fred Drakec9680921999-12-13 16:37:25 +00008353#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008354 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008355#endif
8356#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008357 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008358#endif
8359#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008360 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008361#endif
8362#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008363 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008364#endif
8365#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008366 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008367#endif
8368#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008370#endif
8371#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008372 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008373#endif
8374#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008376#endif
Fred Draked86ed291999-12-15 15:34:33 +00008377#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008379#endif
Fred Drakec9680921999-12-13 16:37:25 +00008380#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008381 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008382#endif
Fred Draked86ed291999-12-15 15:34:33 +00008383#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008384 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008385#endif
8386#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008388#endif
8389#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008390 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008391#endif
8392#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008393 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008394#endif
Fred Drakec9680921999-12-13 16:37:25 +00008395#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008397#endif
8398#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008399 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008400#endif
8401#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008403#endif
8404#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008405 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008406#endif
8407#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008408 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008409#endif
8410#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008411 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008412#endif
8413#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008415#endif
8416#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008418#endif
8419#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008420 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008421#endif
8422#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008423 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008424#endif
8425#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008426 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008427#endif
8428#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008429 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008430#endif
8431#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008432 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008433#endif
8434#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008435 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008436#endif
8437#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008438 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008439#endif
8440#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008442#endif
Fred Draked86ed291999-12-15 15:34:33 +00008443#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008445#endif
8446#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008448#endif
8449#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008451#endif
8452#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008453 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008454#endif
8455#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008456 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008457#endif
8458#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008460#endif
8461#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008462 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008463#endif
8464#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008466#endif
8467#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008468 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008469#endif
8470#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008471 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008472#endif
8473#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008475#endif
8476#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008477 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008478#endif
8479#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008480 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008481#endif
Fred Drakec9680921999-12-13 16:37:25 +00008482};
8483
8484static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008485conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008486{
8487 return conv_confname(arg, valuep, posix_constants_confstr,
8488 sizeof(posix_constants_confstr)
8489 / sizeof(struct constdef));
8490}
8491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008492PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008493"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008494Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008495
8496static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008497posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008498{
8499 PyObject *result = NULL;
8500 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008501 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008502 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008503
Victor Stinnercb043522010-09-10 23:49:04 +00008504 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8505 return NULL;
8506
8507 errno = 0;
8508 len = confstr(name, buffer, sizeof(buffer));
8509 if (len == 0) {
8510 if (errno) {
8511 posix_error();
8512 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008513 }
8514 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008515 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008516 }
8517 }
Victor Stinnercb043522010-09-10 23:49:04 +00008518
8519 if ((unsigned int)len >= sizeof(buffer)) {
8520 char *buf = PyMem_Malloc(len);
8521 if (buf == NULL)
8522 return PyErr_NoMemory();
8523 confstr(name, buf, len);
8524 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8525 PyMem_Free(buf);
8526 }
8527 else
8528 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008529 return result;
8530}
8531#endif
8532
8533
8534#ifdef HAVE_SYSCONF
8535static struct constdef posix_constants_sysconf[] = {
8536#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008537 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008538#endif
8539#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008540 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008541#endif
8542#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008543 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008544#endif
8545#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008546 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008547#endif
8548#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008549 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008550#endif
8551#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008552 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008553#endif
8554#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008555 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008556#endif
8557#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008558 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008559#endif
8560#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008561 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008562#endif
8563#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008564 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008565#endif
Fred Draked86ed291999-12-15 15:34:33 +00008566#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008567 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008568#endif
8569#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008570 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008571#endif
Fred Drakec9680921999-12-13 16:37:25 +00008572#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008573 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008574#endif
Fred Drakec9680921999-12-13 16:37:25 +00008575#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008576 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008577#endif
8578#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008579 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008580#endif
8581#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008582 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008583#endif
8584#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008585 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008586#endif
8587#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008588 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008589#endif
Fred Draked86ed291999-12-15 15:34:33 +00008590#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008591 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008592#endif
Fred Drakec9680921999-12-13 16:37:25 +00008593#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008594 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008595#endif
8596#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008597 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008598#endif
8599#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008600 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008601#endif
8602#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008603 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008604#endif
8605#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008606 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008607#endif
Fred Draked86ed291999-12-15 15:34:33 +00008608#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008609 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008610#endif
Fred Drakec9680921999-12-13 16:37:25 +00008611#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008612 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008613#endif
8614#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008615 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008616#endif
8617#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008618 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008619#endif
8620#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008621 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008622#endif
8623#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008625#endif
8626#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008627 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008628#endif
8629#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008630 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008631#endif
8632#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008633 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008634#endif
8635#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008636 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008637#endif
8638#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008640#endif
8641#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008643#endif
8644#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008645 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008646#endif
8647#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008649#endif
8650#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008651 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008652#endif
8653#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008654 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008655#endif
8656#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008657 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008658#endif
8659#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008660 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008661#endif
8662#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008663 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008664#endif
8665#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008666 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008667#endif
8668#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008670#endif
8671#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008672 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008673#endif
8674#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008675 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008676#endif
8677#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008678 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008679#endif
Fred Draked86ed291999-12-15 15:34:33 +00008680#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008681 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008682#endif
Fred Drakec9680921999-12-13 16:37:25 +00008683#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008684 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008685#endif
8686#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008687 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008688#endif
8689#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008690 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008691#endif
Fred Draked86ed291999-12-15 15:34:33 +00008692#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008693 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008694#endif
Fred Drakec9680921999-12-13 16:37:25 +00008695#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008696 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008697#endif
Fred Draked86ed291999-12-15 15:34:33 +00008698#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008699 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008700#endif
8701#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008702 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008703#endif
Fred Drakec9680921999-12-13 16:37:25 +00008704#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008705 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008706#endif
8707#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008709#endif
8710#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008711 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008712#endif
8713#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008715#endif
Fred Draked86ed291999-12-15 15:34:33 +00008716#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008717 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008718#endif
Fred Drakec9680921999-12-13 16:37:25 +00008719#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008720 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008721#endif
8722#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008723 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008724#endif
8725#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008726 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008727#endif
8728#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008729 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008730#endif
8731#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008732 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008733#endif
8734#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008735 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008736#endif
8737#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008738 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008739#endif
Fred Draked86ed291999-12-15 15:34:33 +00008740#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008741 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008742#endif
Fred Drakec9680921999-12-13 16:37:25 +00008743#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008744 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008745#endif
8746#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008747 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008748#endif
Fred Draked86ed291999-12-15 15:34:33 +00008749#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008750 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008751#endif
Fred Drakec9680921999-12-13 16:37:25 +00008752#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008753 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008754#endif
8755#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008757#endif
8758#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008760#endif
8761#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008762 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008763#endif
8764#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008766#endif
8767#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008768 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008769#endif
8770#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008772#endif
8773#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008775#endif
8776#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008777 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008778#endif
Fred Draked86ed291999-12-15 15:34:33 +00008779#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008780 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008781#endif
8782#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008783 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008784#endif
Fred Drakec9680921999-12-13 16:37:25 +00008785#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008786 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008787#endif
8788#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008789 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008790#endif
8791#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008793#endif
8794#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008796#endif
8797#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008798 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008799#endif
8800#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008802#endif
8803#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008805#endif
8806#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008808#endif
8809#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008811#endif
8812#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008814#endif
8815#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008817#endif
8818#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008820#endif
8821#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
8842#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008844#endif
8845#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008847#endif
8848#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
8851#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008852 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008853#endif
8854#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008855 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008856#endif
8857#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008859#endif
8860#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008861 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008862#endif
8863#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008864 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008865#endif
8866#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008868#endif
8869#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008871#endif
8872#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008874#endif
8875#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008876 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008877#endif
8878#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008879 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008880#endif
8881#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008882 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008883#endif
8884#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008886#endif
8887#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008889#endif
Fred Draked86ed291999-12-15 15:34:33 +00008890#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008892#endif
Fred Drakec9680921999-12-13 16:37:25 +00008893#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008895#endif
8896#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008898#endif
8899#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008901#endif
8902#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008904#endif
8905#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008906 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008907#endif
8908#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008910#endif
8911#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008913#endif
8914#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008915 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008916#endif
8917#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008918 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008919#endif
8920#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008921 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008922#endif
8923#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008924 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008925#endif
8926#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008928#endif
8929#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008930 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008931#endif
8932#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008933 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008934#endif
8935#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008936 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008937#endif
8938#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008939 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008940#endif
8941#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008942 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008943#endif
8944#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008945 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008946#endif
8947#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008948 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008949#endif
8950#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008951 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008952#endif
8953#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008954 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008955#endif
8956#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008957 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008958#endif
8959#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008960 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008961#endif
8962#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008963 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008964#endif
8965#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008966 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008967#endif
8968#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008969 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008970#endif
8971#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008972 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008973#endif
8974#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008975 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008976#endif
8977#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008978 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008979#endif
8980#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008981 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008982#endif
8983#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008984 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008985#endif
8986#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008987 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008988#endif
8989#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008990 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008991#endif
8992#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008993 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008994#endif
8995#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008996 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008997#endif
8998#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008999 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009000#endif
9001#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009002 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009003#endif
9004#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009005 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009006#endif
9007#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009008 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009009#endif
9010#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009011 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009012#endif
9013#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009014 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009015#endif
9016#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009017 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009018#endif
9019#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009020 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009021#endif
9022#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009023 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009024#endif
9025#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009026 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009027#endif
9028};
9029
9030static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009031conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009032{
9033 return conv_confname(arg, valuep, posix_constants_sysconf,
9034 sizeof(posix_constants_sysconf)
9035 / sizeof(struct constdef));
9036}
9037
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009038PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009039"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009040Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009041
9042static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009043posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009044{
9045 PyObject *result = NULL;
9046 int name;
9047
9048 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9049 int value;
9050
9051 errno = 0;
9052 value = sysconf(name);
9053 if (value == -1 && errno != 0)
9054 posix_error();
9055 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009056 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009057 }
9058 return result;
9059}
9060#endif
9061
9062
Fred Drakebec628d1999-12-15 18:31:10 +00009063/* This code is used to ensure that the tables of configuration value names
9064 * are in sorted order as required by conv_confname(), and also to build the
9065 * the exported dictionaries that are used to publish information about the
9066 * names available on the host platform.
9067 *
9068 * Sorting the table at runtime ensures that the table is properly ordered
9069 * when used, even for platforms we're not able to test on. It also makes
9070 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009071 */
Fred Drakebec628d1999-12-15 18:31:10 +00009072
9073static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009074cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009075{
9076 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009077 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009078 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009079 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009080
9081 return strcmp(c1->name, c2->name);
9082}
9083
9084static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009085setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009086 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009087{
Fred Drakebec628d1999-12-15 18:31:10 +00009088 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009089 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009090
9091 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9092 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009093 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009094 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009095
Barry Warsaw3155db32000-04-13 15:20:40 +00009096 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009097 PyObject *o = PyLong_FromLong(table[i].value);
9098 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9099 Py_XDECREF(o);
9100 Py_DECREF(d);
9101 return -1;
9102 }
9103 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009104 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009105 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009106}
9107
Fred Drakebec628d1999-12-15 18:31:10 +00009108/* Return -1 on failure, 0 on success. */
9109static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009110setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009111{
9112#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009113 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009114 sizeof(posix_constants_pathconf)
9115 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009116 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009117 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009118#endif
9119#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009120 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009121 sizeof(posix_constants_confstr)
9122 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009123 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009124 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009125#endif
9126#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009127 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009128 sizeof(posix_constants_sysconf)
9129 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009130 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009131 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009132#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009133 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009134}
Fred Draked86ed291999-12-15 15:34:33 +00009135
9136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009137PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009138"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009139Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009140in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009141
9142static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009143posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009144{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009145 abort();
9146 /*NOTREACHED*/
9147 Py_FatalError("abort() called from Python code didn't abort!");
9148 return NULL;
9149}
Fred Drakebec628d1999-12-15 18:31:10 +00009150
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009151#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009152PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009153"startfile(filepath [, operation]) - Start a file with its associated\n\
9154application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009155\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009156When \"operation\" is not specified or \"open\", this acts like\n\
9157double-clicking the file in Explorer, or giving the file name as an\n\
9158argument to the DOS \"start\" command: the file is opened with whatever\n\
9159application (if any) its extension is associated.\n\
9160When another \"operation\" is given, it specifies what should be done with\n\
9161the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009162\n\
9163startfile returns as soon as the associated application is launched.\n\
9164There is no option to wait for the application to close, and no way\n\
9165to retrieve the application's exit status.\n\
9166\n\
9167The filepath is relative to the current directory. If you want to use\n\
9168an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009169the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009170
9171static PyObject *
9172win32_startfile(PyObject *self, PyObject *args)
9173{
Victor Stinner8c62be82010-05-06 00:08:46 +00009174 PyObject *ofilepath;
9175 char *filepath;
9176 char *operation = NULL;
9177 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009178
Victor Stinner8c62be82010-05-06 00:08:46 +00009179 PyObject *unipath, *woperation = NULL;
9180 if (!PyArg_ParseTuple(args, "U|s:startfile",
9181 &unipath, &operation)) {
9182 PyErr_Clear();
9183 goto normal;
9184 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009185
Victor Stinner8c62be82010-05-06 00:08:46 +00009186 if (operation) {
9187 woperation = PyUnicode_DecodeASCII(operation,
9188 strlen(operation), NULL);
9189 if (!woperation) {
9190 PyErr_Clear();
9191 operation = NULL;
9192 goto normal;
9193 }
9194 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009195
Victor Stinner8c62be82010-05-06 00:08:46 +00009196 Py_BEGIN_ALLOW_THREADS
9197 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9198 PyUnicode_AS_UNICODE(unipath),
9199 NULL, NULL, SW_SHOWNORMAL);
9200 Py_END_ALLOW_THREADS
9201
9202 Py_XDECREF(woperation);
9203 if (rc <= (HINSTANCE)32) {
9204 PyObject *errval = win32_error_unicode("startfile",
9205 PyUnicode_AS_UNICODE(unipath));
9206 return errval;
9207 }
9208 Py_INCREF(Py_None);
9209 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009210
9211normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009212 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9213 PyUnicode_FSConverter, &ofilepath,
9214 &operation))
9215 return NULL;
9216 filepath = PyBytes_AsString(ofilepath);
9217 Py_BEGIN_ALLOW_THREADS
9218 rc = ShellExecute((HWND)0, operation, filepath,
9219 NULL, NULL, SW_SHOWNORMAL);
9220 Py_END_ALLOW_THREADS
9221 if (rc <= (HINSTANCE)32) {
9222 PyObject *errval = win32_error("startfile", filepath);
9223 Py_DECREF(ofilepath);
9224 return errval;
9225 }
9226 Py_DECREF(ofilepath);
9227 Py_INCREF(Py_None);
9228 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009229}
9230#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009231
Martin v. Löwis438b5342002-12-27 10:16:42 +00009232#ifdef HAVE_GETLOADAVG
9233PyDoc_STRVAR(posix_getloadavg__doc__,
9234"getloadavg() -> (float, float, float)\n\n\
9235Return the number of processes in the system run queue averaged over\n\
9236the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9237was unobtainable");
9238
9239static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009240posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009241{
9242 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009243 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009244 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9245 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009246 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009247 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009248}
9249#endif
9250
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009251#ifdef MS_WINDOWS
9252
9253PyDoc_STRVAR(win32_urandom__doc__,
9254"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009255Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009256
9257typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9258 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9259 DWORD dwFlags );
9260typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9261 BYTE *pbBuffer );
9262
9263static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009264/* This handle is never explicitly released. Instead, the operating
9265 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009266static HCRYPTPROV hCryptProv = 0;
9267
Tim Peters4ad82172004-08-30 17:02:04 +00009268static PyObject*
9269win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009270{
Victor Stinner8c62be82010-05-06 00:08:46 +00009271 int howMany;
9272 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009273
Victor Stinner8c62be82010-05-06 00:08:46 +00009274 /* Read arguments */
9275 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9276 return NULL;
9277 if (howMany < 0)
9278 return PyErr_Format(PyExc_ValueError,
9279 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009280
Victor Stinner8c62be82010-05-06 00:08:46 +00009281 if (hCryptProv == 0) {
9282 HINSTANCE hAdvAPI32 = NULL;
9283 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009284
Victor Stinner8c62be82010-05-06 00:08:46 +00009285 /* Obtain handle to the DLL containing CryptoAPI
9286 This should not fail */
9287 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9288 if(hAdvAPI32 == NULL)
9289 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009290
Victor Stinner8c62be82010-05-06 00:08:46 +00009291 /* Obtain pointers to the CryptoAPI functions
9292 This will fail on some early versions of Win95 */
9293 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9294 hAdvAPI32,
9295 "CryptAcquireContextA");
9296 if (pCryptAcquireContext == NULL)
9297 return PyErr_Format(PyExc_NotImplementedError,
9298 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009299
Victor Stinner8c62be82010-05-06 00:08:46 +00009300 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9301 hAdvAPI32, "CryptGenRandom");
9302 if (pCryptGenRandom == NULL)
9303 return PyErr_Format(PyExc_NotImplementedError,
9304 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009305
Victor Stinner8c62be82010-05-06 00:08:46 +00009306 /* Acquire context */
9307 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9308 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9309 return win32_error("CryptAcquireContext", NULL);
9310 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009311
Victor Stinner8c62be82010-05-06 00:08:46 +00009312 /* Allocate bytes */
9313 result = PyBytes_FromStringAndSize(NULL, howMany);
9314 if (result != NULL) {
9315 /* Get random data */
9316 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9317 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9318 PyBytes_AS_STRING(result))) {
9319 Py_DECREF(result);
9320 return win32_error("CryptGenRandom", NULL);
9321 }
9322 }
9323 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009324}
9325#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009326
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009327PyDoc_STRVAR(device_encoding__doc__,
9328"device_encoding(fd) -> str\n\n\
9329Return a string describing the encoding of the device\n\
9330if the output is a terminal; else return None.");
9331
9332static PyObject *
9333device_encoding(PyObject *self, PyObject *args)
9334{
Victor Stinner8c62be82010-05-06 00:08:46 +00009335 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009336#if defined(MS_WINDOWS) || defined(MS_WIN64)
9337 UINT cp;
9338#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9340 return NULL;
9341 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9342 Py_INCREF(Py_None);
9343 return Py_None;
9344 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009345#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009346 if (fd == 0)
9347 cp = GetConsoleCP();
9348 else if (fd == 1 || fd == 2)
9349 cp = GetConsoleOutputCP();
9350 else
9351 cp = 0;
9352 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9353 has no console */
9354 if (cp != 0)
9355 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009356#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009357 {
9358 char *codeset = nl_langinfo(CODESET);
9359 if (codeset != NULL && codeset[0] != 0)
9360 return PyUnicode_FromString(codeset);
9361 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009362#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009363 Py_INCREF(Py_None);
9364 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009365}
9366
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009367#ifdef __VMS
9368/* Use openssl random routine */
9369#include <openssl/rand.h>
9370PyDoc_STRVAR(vms_urandom__doc__,
9371"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009372Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009373
9374static PyObject*
9375vms_urandom(PyObject *self, PyObject *args)
9376{
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 int howMany;
9378 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009379
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 /* Read arguments */
9381 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9382 return NULL;
9383 if (howMany < 0)
9384 return PyErr_Format(PyExc_ValueError,
9385 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009386
Victor Stinner8c62be82010-05-06 00:08:46 +00009387 /* Allocate bytes */
9388 result = PyBytes_FromStringAndSize(NULL, howMany);
9389 if (result != NULL) {
9390 /* Get random data */
9391 if (RAND_pseudo_bytes((unsigned char*)
9392 PyBytes_AS_STRING(result),
9393 howMany) < 0) {
9394 Py_DECREF(result);
9395 return PyErr_Format(PyExc_ValueError,
9396 "RAND_pseudo_bytes");
9397 }
9398 }
9399 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009400}
9401#endif
9402
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009403#ifdef HAVE_SETRESUID
9404PyDoc_STRVAR(posix_setresuid__doc__,
9405"setresuid(ruid, euid, suid)\n\n\
9406Set the current process's real, effective, and saved user ids.");
9407
9408static PyObject*
9409posix_setresuid (PyObject *self, PyObject *args)
9410{
Victor Stinner8c62be82010-05-06 00:08:46 +00009411 /* We assume uid_t is no larger than a long. */
9412 long ruid, euid, suid;
9413 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9414 return NULL;
9415 if (setresuid(ruid, euid, suid) < 0)
9416 return posix_error();
9417 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009418}
9419#endif
9420
9421#ifdef HAVE_SETRESGID
9422PyDoc_STRVAR(posix_setresgid__doc__,
9423"setresgid(rgid, egid, sgid)\n\n\
9424Set the current process's real, effective, and saved group ids.");
9425
9426static PyObject*
9427posix_setresgid (PyObject *self, PyObject *args)
9428{
Victor Stinner8c62be82010-05-06 00:08:46 +00009429 /* We assume uid_t is no larger than a long. */
9430 long rgid, egid, sgid;
9431 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9432 return NULL;
9433 if (setresgid(rgid, egid, sgid) < 0)
9434 return posix_error();
9435 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009436}
9437#endif
9438
9439#ifdef HAVE_GETRESUID
9440PyDoc_STRVAR(posix_getresuid__doc__,
9441"getresuid() -> (ruid, euid, suid)\n\n\
9442Get tuple of the current process's real, effective, and saved user ids.");
9443
9444static PyObject*
9445posix_getresuid (PyObject *self, PyObject *noargs)
9446{
Victor Stinner8c62be82010-05-06 00:08:46 +00009447 uid_t ruid, euid, suid;
9448 long l_ruid, l_euid, l_suid;
9449 if (getresuid(&ruid, &euid, &suid) < 0)
9450 return posix_error();
9451 /* Force the values into long's as we don't know the size of uid_t. */
9452 l_ruid = ruid;
9453 l_euid = euid;
9454 l_suid = suid;
9455 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009456}
9457#endif
9458
9459#ifdef HAVE_GETRESGID
9460PyDoc_STRVAR(posix_getresgid__doc__,
9461"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009462Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009463
9464static PyObject*
9465posix_getresgid (PyObject *self, PyObject *noargs)
9466{
Victor Stinner8c62be82010-05-06 00:08:46 +00009467 uid_t rgid, egid, sgid;
9468 long l_rgid, l_egid, l_sgid;
9469 if (getresgid(&rgid, &egid, &sgid) < 0)
9470 return posix_error();
9471 /* Force the values into long's as we don't know the size of uid_t. */
9472 l_rgid = rgid;
9473 l_egid = egid;
9474 l_sgid = sgid;
9475 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009476}
9477#endif
9478
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009479/* Posix *at family of functions:
9480 faccessat, fchmodat, fchownat, fstatat, futimesat,
9481 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9482 unlinkat, utimensat, mkfifoat */
9483
9484#ifdef HAVE_FACCESSAT
9485PyDoc_STRVAR(posix_faccessat__doc__,
9486"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9487Like access() but if path is relative, it is taken as relative to dirfd.\n\
9488flags is optional and can be constructed by ORing together zero or more\n\
9489of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9490If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9491is interpreted relative to the current working directory.");
9492
9493static PyObject *
9494posix_faccessat(PyObject *self, PyObject *args)
9495{
9496 PyObject *opath;
9497 char *path;
9498 int mode;
9499 int res;
9500 int dirfd, flags = 0;
9501 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9502 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9503 return NULL;
9504 path = PyBytes_AsString(opath);
9505 Py_BEGIN_ALLOW_THREADS
9506 res = faccessat(dirfd, path, mode, flags);
9507 Py_END_ALLOW_THREADS
9508 Py_DECREF(opath);
9509 return PyBool_FromLong(res == 0);
9510}
9511#endif
9512
9513#ifdef HAVE_FCHMODAT
9514PyDoc_STRVAR(posix_fchmodat__doc__,
9515"fchmodat(dirfd, path, mode, flags=0)\n\n\
9516Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9517flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9518If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9519is interpreted relative to the current working directory.");
9520
9521static PyObject *
9522posix_fchmodat(PyObject *self, PyObject *args)
9523{
9524 int dirfd, mode, res;
9525 int flags = 0;
9526 PyObject *opath;
9527 char *path;
9528
9529 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9530 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9531 return NULL;
9532
9533 path = PyBytes_AsString(opath);
9534
9535 Py_BEGIN_ALLOW_THREADS
9536 res = fchmodat(dirfd, path, mode, flags);
9537 Py_END_ALLOW_THREADS
9538 Py_DECREF(opath);
9539 if (res < 0)
9540 return posix_error();
9541 Py_RETURN_NONE;
9542}
9543#endif /* HAVE_FCHMODAT */
9544
9545#ifdef HAVE_FCHOWNAT
9546PyDoc_STRVAR(posix_fchownat__doc__,
9547"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9548Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9549flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9550If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9551is interpreted relative to the current working directory.");
9552
9553static PyObject *
9554posix_fchownat(PyObject *self, PyObject *args)
9555{
9556 PyObject *opath;
9557 int dirfd, res;
9558 long uid, gid;
9559 int flags = 0;
9560 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009561
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009562 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9563 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9564 return NULL;
9565
9566 path = PyBytes_AsString(opath);
9567
9568 Py_BEGIN_ALLOW_THREADS
9569 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9570 Py_END_ALLOW_THREADS
9571 Py_DECREF(opath);
9572 if (res < 0)
9573 return posix_error();
9574 Py_RETURN_NONE;
9575}
9576#endif /* HAVE_FCHOWNAT */
9577
9578#ifdef HAVE_FSTATAT
9579PyDoc_STRVAR(posix_fstatat__doc__,
9580"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9581Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9582flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9583If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9584is interpreted relative to the current working directory.");
9585
9586static PyObject *
9587posix_fstatat(PyObject *self, PyObject *args)
9588{
9589 PyObject *opath;
9590 char *path;
9591 STRUCT_STAT st;
9592 int dirfd, res, flags = 0;
9593
9594 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9595 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9596 return NULL;
9597 path = PyBytes_AsString(opath);
9598
9599 Py_BEGIN_ALLOW_THREADS
9600 res = fstatat(dirfd, path, &st, flags);
9601 Py_END_ALLOW_THREADS
9602 Py_DECREF(opath);
9603 if (res != 0)
9604 return posix_error();
9605
9606 return _pystat_fromstructstat(&st);
9607}
9608#endif
9609
9610#ifdef HAVE_FUTIMESAT
9611PyDoc_STRVAR(posix_futimesat__doc__,
9612"futimesat(dirfd, path, (atime, mtime))\n\
9613futimesat(dirfd, path, None)\n\n\
9614Like utime() 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_futimesat(PyObject *self, PyObject *args)
9620{
9621 PyObject *opath;
9622 char *path;
9623 int res, dirfd;
9624 PyObject* arg;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009625 time_t atime, mtime;
9626 long ausec, musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009627
9628 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9629 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9630 return NULL;
9631 path = PyBytes_AsString(opath);
9632 if (arg == Py_None) {
9633 /* optional time values not given */
9634 Py_BEGIN_ALLOW_THREADS
9635 res = futimesat(dirfd, path, NULL);
9636 Py_END_ALLOW_THREADS
9637 }
9638 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9639 PyErr_SetString(PyExc_TypeError,
9640 "futimesat() arg 3 must be a tuple (atime, mtime)");
9641 Py_DECREF(opath);
9642 return NULL;
9643 }
9644 else {
9645 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009646 &atime, &ausec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009647 Py_DECREF(opath);
9648 return NULL;
9649 }
9650 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009651 &mtime, &musec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009652 Py_DECREF(opath);
9653 return NULL;
9654 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009655
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009656 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009657 {
9658#ifdef HAVE_UTIMENSAT
9659 struct timespec buf[2];
9660 buf[0].tv_sec = atime;
9661 buf[0].tv_nsec = ausec;
9662 buf[1].tv_sec = mtime;
9663 buf[1].tv_nsec = musec;
9664 res = utimensat(dirfd, path, buf, 0);
9665#else
9666 struct timeval buf[2];
9667 buf[0].tv_sec = atime;
9668 buf[0].tv_usec = ausec;
9669 buf[1].tv_sec = mtime;
9670 buf[1].tv_usec = musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009671 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009672#endif
9673 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009674 Py_END_ALLOW_THREADS
9675 }
9676 Py_DECREF(opath);
9677 if (res < 0) {
9678 return posix_error();
9679 }
9680 Py_RETURN_NONE;
9681}
9682#endif
9683
9684#ifdef HAVE_LINKAT
9685PyDoc_STRVAR(posix_linkat__doc__,
9686"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9687Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9688and if dstpath is relative, it is taken as relative to dstfd.\n\
9689flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9690If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9691srcpath is interpreted relative to the current working directory. This\n\
9692also applies for dstpath.");
9693
9694static PyObject *
9695posix_linkat(PyObject *self, PyObject *args)
9696{
9697 PyObject *osrc, *odst;
9698 char *src, *dst;
9699 int res, srcfd, dstfd;
9700 int flags = 0;
9701
9702 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9703 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9704 return NULL;
9705 src = PyBytes_AsString(osrc);
9706 dst = PyBytes_AsString(odst);
9707 Py_BEGIN_ALLOW_THREADS
9708 res = linkat(srcfd, src, dstfd, dst, flags);
9709 Py_END_ALLOW_THREADS
9710 Py_DECREF(osrc);
9711 Py_DECREF(odst);
9712 if (res < 0)
9713 return posix_error();
9714 Py_RETURN_NONE;
9715}
9716#endif /* HAVE_LINKAT */
9717
9718#ifdef HAVE_MKDIRAT
9719PyDoc_STRVAR(posix_mkdirat__doc__,
9720"mkdirat(dirfd, path, mode=0o777)\n\n\
9721Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9722If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9723is interpreted relative to the current working directory.");
9724
9725static PyObject *
9726posix_mkdirat(PyObject *self, PyObject *args)
9727{
9728 int res, dirfd;
9729 PyObject *opath;
9730 char *path;
9731 int mode = 0777;
9732
9733 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9734 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9735 return NULL;
9736 path = PyBytes_AsString(opath);
9737 Py_BEGIN_ALLOW_THREADS
9738 res = mkdirat(dirfd, path, mode);
9739 Py_END_ALLOW_THREADS
9740 Py_DECREF(opath);
9741 if (res < 0)
9742 return posix_error();
9743 Py_RETURN_NONE;
9744}
9745#endif
9746
9747#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9748PyDoc_STRVAR(posix_mknodat__doc__,
9749"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9750Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9751If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9752is interpreted relative to the current working directory.");
9753
9754static PyObject *
9755posix_mknodat(PyObject *self, PyObject *args)
9756{
9757 PyObject *opath;
9758 char *filename;
9759 int mode = 0600;
9760 int device = 0;
9761 int res, dirfd;
9762 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9763 PyUnicode_FSConverter, &opath, &mode, &device))
9764 return NULL;
9765 filename = PyBytes_AS_STRING(opath);
9766 Py_BEGIN_ALLOW_THREADS
9767 res = mknodat(dirfd, filename, mode, device);
9768 Py_END_ALLOW_THREADS
9769 Py_DECREF(opath);
9770 if (res < 0)
9771 return posix_error();
9772 Py_RETURN_NONE;
9773}
9774#endif
9775
9776#ifdef HAVE_OPENAT
9777PyDoc_STRVAR(posix_openat__doc__,
9778"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9779Like open() but if path is relative, it is taken as relative to dirfd.\n\
9780If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9781is interpreted relative to the current working directory.");
9782
9783static PyObject *
9784posix_openat(PyObject *self, PyObject *args)
9785{
9786 PyObject *ofile;
9787 char *file;
9788 int flag, dirfd, fd;
9789 int mode = 0777;
9790
9791 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9792 &dirfd, PyUnicode_FSConverter, &ofile,
9793 &flag, &mode))
9794 return NULL;
9795 file = PyBytes_AsString(ofile);
9796 Py_BEGIN_ALLOW_THREADS
9797 fd = openat(dirfd, file, flag, mode);
9798 Py_END_ALLOW_THREADS
9799 Py_DECREF(ofile);
9800 if (fd < 0)
9801 return posix_error();
9802 return PyLong_FromLong((long)fd);
9803}
9804#endif
9805
9806#ifdef HAVE_READLINKAT
9807PyDoc_STRVAR(posix_readlinkat__doc__,
9808"readlinkat(dirfd, path) -> path\n\n\
9809Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9810If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9811is interpreted relative to the current working directory.");
9812
9813static PyObject *
9814posix_readlinkat(PyObject *self, PyObject *args)
9815{
9816 PyObject *v, *opath;
9817 char buf[MAXPATHLEN];
9818 char *path;
9819 int n, dirfd;
9820 int arg_is_unicode = 0;
9821
9822 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9823 &dirfd, PyUnicode_FSConverter, &opath))
9824 return NULL;
9825 path = PyBytes_AsString(opath);
9826 v = PySequence_GetItem(args, 1);
9827 if (v == NULL) {
9828 Py_DECREF(opath);
9829 return NULL;
9830 }
9831
9832 if (PyUnicode_Check(v)) {
9833 arg_is_unicode = 1;
9834 }
9835 Py_DECREF(v);
9836
9837 Py_BEGIN_ALLOW_THREADS
9838 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9839 Py_END_ALLOW_THREADS
9840 Py_DECREF(opath);
9841 if (n < 0)
9842 return posix_error();
9843
9844 if (arg_is_unicode)
9845 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9846 else
9847 return PyBytes_FromStringAndSize(buf, n);
9848}
9849#endif /* HAVE_READLINKAT */
9850
9851#ifdef HAVE_RENAMEAT
9852PyDoc_STRVAR(posix_renameat__doc__,
9853"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9854Like rename() but if oldpath is relative, it is taken as relative to\n\
9855olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9856If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9857oldpath is interpreted relative to the current working directory. This\n\
9858also applies for newpath.");
9859
9860static PyObject *
9861posix_renameat(PyObject *self, PyObject *args)
9862{
9863 int res;
9864 PyObject *opathold, *opathnew;
9865 char *opath, *npath;
9866 int oldfd, newfd;
9867
9868 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9869 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9870 return NULL;
9871 opath = PyBytes_AsString(opathold);
9872 npath = PyBytes_AsString(opathnew);
9873 Py_BEGIN_ALLOW_THREADS
9874 res = renameat(oldfd, opath, newfd, npath);
9875 Py_END_ALLOW_THREADS
9876 Py_DECREF(opathold);
9877 Py_DECREF(opathnew);
9878 if (res < 0)
9879 return posix_error();
9880 Py_RETURN_NONE;
9881}
9882#endif
9883
9884#if HAVE_SYMLINKAT
9885PyDoc_STRVAR(posix_symlinkat__doc__,
9886"symlinkat(src, dstfd, dst)\n\n\
9887Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9888If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9889is interpreted relative to the current working directory.");
9890
9891static PyObject *
9892posix_symlinkat(PyObject *self, PyObject *args)
9893{
9894 int res, dstfd;
9895 PyObject *osrc, *odst;
9896 char *src, *dst;
9897
9898 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9899 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9900 return NULL;
9901 src = PyBytes_AsString(osrc);
9902 dst = PyBytes_AsString(odst);
9903 Py_BEGIN_ALLOW_THREADS
9904 res = symlinkat(src, dstfd, dst);
9905 Py_END_ALLOW_THREADS
9906 Py_DECREF(osrc);
9907 Py_DECREF(odst);
9908 if (res < 0)
9909 return posix_error();
9910 Py_RETURN_NONE;
9911}
9912#endif /* HAVE_SYMLINKAT */
9913
9914#ifdef HAVE_UNLINKAT
9915PyDoc_STRVAR(posix_unlinkat__doc__,
9916"unlinkat(dirfd, path, flags=0)\n\n\
9917Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9918flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9919specified, unlinkat() behaves like rmdir().\n\
9920If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9921is interpreted relative to the current working directory.");
9922
9923static PyObject *
9924posix_unlinkat(PyObject *self, PyObject *args)
9925{
9926 int dirfd, res, flags = 0;
9927 PyObject *opath;
9928 char *path;
9929
9930 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9931 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9932 return NULL;
9933 path = PyBytes_AsString(opath);
9934 Py_BEGIN_ALLOW_THREADS
9935 res = unlinkat(dirfd, path, flags);
9936 Py_END_ALLOW_THREADS
9937 Py_DECREF(opath);
9938 if (res < 0)
9939 return posix_error();
9940 Py_RETURN_NONE;
9941}
9942#endif
9943
9944#ifdef HAVE_UTIMENSAT
9945PyDoc_STRVAR(posix_utimensat__doc__,
9946"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9947 (mtime_sec, mtime_nsec), flags)\n\
9948utimensat(dirfd, path, None, None, flags)\n\n\
9949Updates the timestamps of a file with nanosecond precision. If path is\n\
9950relative, it is taken as relative to dirfd.\n\
9951The second form sets atime and mtime to the current time.\n\
9952flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9953If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9954is interpreted relative to the current working directory.\n\
9955If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9956current time.\n\
9957If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9958
9959static PyObject *
9960posix_utimensat(PyObject *self, PyObject *args)
9961{
9962 PyObject *opath;
9963 char *path;
9964 int res, dirfd, flags = 0;
9965 PyObject *atime, *mtime;
9966
9967 struct timespec buf[2];
9968
9969 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9970 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9971 return NULL;
9972 path = PyBytes_AsString(opath);
9973 if (atime == Py_None && mtime == Py_None) {
9974 /* optional time values not given */
9975 Py_BEGIN_ALLOW_THREADS
9976 res = utimensat(dirfd, path, NULL, flags);
9977 Py_END_ALLOW_THREADS
9978 }
9979 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9980 PyErr_SetString(PyExc_TypeError,
9981 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9982 Py_DECREF(opath);
9983 return NULL;
9984 }
9985 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9986 PyErr_SetString(PyExc_TypeError,
9987 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9988 Py_DECREF(opath);
9989 return NULL;
9990 }
9991 else {
9992 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9993 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9994 Py_DECREF(opath);
9995 return NULL;
9996 }
9997 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9998 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9999 Py_DECREF(opath);
10000 return NULL;
10001 }
10002 Py_BEGIN_ALLOW_THREADS
10003 res = utimensat(dirfd, path, buf, flags);
10004 Py_END_ALLOW_THREADS
10005 }
10006 Py_DECREF(opath);
10007 if (res < 0) {
10008 return posix_error();
10009 }
10010 Py_RETURN_NONE;
10011}
10012#endif
10013
10014#ifdef HAVE_MKFIFOAT
10015PyDoc_STRVAR(posix_mkfifoat__doc__,
10016"mkfifoat(dirfd, path, mode=0o666)\n\n\
10017Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
10018If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10019is interpreted relative to the current working directory.");
10020
10021static PyObject *
10022posix_mkfifoat(PyObject *self, PyObject *args)
10023{
10024 PyObject *opath;
10025 char *filename;
10026 int mode = 0666;
10027 int res, dirfd;
10028 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
10029 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10030 return NULL;
10031 filename = PyBytes_AS_STRING(opath);
10032 Py_BEGIN_ALLOW_THREADS
10033 res = mkfifoat(dirfd, filename, mode);
10034 Py_END_ALLOW_THREADS
10035 Py_DECREF(opath);
10036 if (res < 0)
10037 return posix_error();
10038 Py_RETURN_NONE;
10039}
10040#endif
10041
Benjamin Peterson9428d532011-09-14 11:45:52 -040010042#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010043
10044static int
10045try_getxattr(const char *path, const char *name,
10046 ssize_t (*get)(const char *, const char *, void *, size_t),
10047 Py_ssize_t buf_size, PyObject **res)
10048{
10049 PyObject *value;
10050 Py_ssize_t len;
10051
10052 assert(buf_size <= XATTR_SIZE_MAX);
10053 value = PyBytes_FromStringAndSize(NULL, buf_size);
10054 if (!value)
10055 return 0;
10056 Py_BEGIN_ALLOW_THREADS;
10057 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10058 Py_END_ALLOW_THREADS;
10059 if (len < 0) {
10060 Py_DECREF(value);
10061 if (errno == ERANGE) {
10062 value = NULL;
10063 }
10064 else {
10065 posix_error();
10066 return 0;
10067 }
10068 }
10069 else if (len != buf_size) {
10070 /* Can only shrink. */
10071 _PyBytes_Resize(&value, len);
10072 }
10073 *res = value;
10074 return 1;
10075}
10076
10077static PyObject *
10078getxattr_common(const char *path, PyObject *name_obj,
10079 ssize_t (*get)(const char *, const char *, void *, size_t))
10080{
10081 PyObject *value;
10082 const char *name = PyBytes_AS_STRING(name_obj);
10083
10084 /* Try a small value first. */
10085 if (!try_getxattr(path, name, get, 128, &value))
10086 return NULL;
10087 if (value)
10088 return value;
10089 /* Now the maximum possible one. */
10090 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10091 return NULL;
10092 assert(value);
10093 return value;
10094}
10095
10096PyDoc_STRVAR(posix_getxattr__doc__,
10097"getxattr(path, attr) -> value\n\n\
10098Return the value of extended attribute *name* on *path*.");
10099
10100static PyObject *
10101posix_getxattr(PyObject *self, PyObject *args)
10102{
10103 PyObject *path, *res, *name;
10104
10105 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10106 PyUnicode_FSConverter, &name))
10107 return NULL;
10108 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10109 Py_DECREF(path);
10110 Py_DECREF(name);
10111 return res;
10112}
10113
10114PyDoc_STRVAR(posix_lgetxattr__doc__,
10115"lgetxattr(path, attr) -> value\n\n\
10116Like getxattr but don't follow symlinks.");
10117
10118static PyObject *
10119posix_lgetxattr(PyObject *self, PyObject *args)
10120{
10121 PyObject *path, *res, *name;
10122
10123 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10124 PyUnicode_FSConverter, &name))
10125 return NULL;
10126 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10127 Py_DECREF(path);
10128 Py_DECREF(name);
10129 return res;
10130}
10131
10132static ssize_t
10133wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10134{
10135 /* Hack to share code. */
10136 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10137}
10138
10139PyDoc_STRVAR(posix_fgetxattr__doc__,
10140"fgetxattr(fd, attr) -> value\n\n\
10141Like getxattr but operate on a fd instead of a path.");
10142
10143static PyObject *
10144posix_fgetxattr(PyObject *self, PyObject *args)
10145{
10146 PyObject *res, *name;
10147 int fd;
10148
10149 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10150 return NULL;
10151 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10152 Py_DECREF(name);
10153 return res;
10154}
10155
10156PyDoc_STRVAR(posix_setxattr__doc__,
10157"setxattr(path, attr, value, flags=0)\n\n\
10158Set extended attribute *attr* on *path* to *value*.");
10159
10160static PyObject *
10161posix_setxattr(PyObject *self, PyObject *args)
10162{
10163 PyObject *path, *name;
10164 Py_buffer data;
10165 int flags = 0, err;
10166
10167 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10168 &path, PyUnicode_FSConverter, &name, &data, &flags))
10169 return NULL;
10170 Py_BEGIN_ALLOW_THREADS;
10171 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10172 data.buf, data.len, flags);
10173 Py_END_ALLOW_THREADS;
10174 Py_DECREF(path);
10175 Py_DECREF(name);
10176 PyBuffer_Release(&data);
10177 if (err)
10178 return posix_error();
10179 Py_RETURN_NONE;
10180}
10181
10182PyDoc_STRVAR(posix_lsetxattr__doc__,
10183"lsetxattr(path, attr, value, flags=0)\n\n\
10184Like setxattr but don't follow symlinks.");
10185
10186static PyObject *
10187posix_lsetxattr(PyObject *self, PyObject *args)
10188{
10189 PyObject *path, *name;
10190 Py_buffer data;
10191 int flags = 0, err;
10192
10193 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10194 &path, PyUnicode_FSConverter, &name, &data, &flags))
10195 return NULL;
10196 Py_BEGIN_ALLOW_THREADS;
10197 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10198 data.buf, data.len, flags);
10199 Py_END_ALLOW_THREADS;
10200 Py_DECREF(path);
10201 Py_DECREF(name);
10202 PyBuffer_Release(&data);
10203 if (err)
10204 return posix_error();
10205 Py_RETURN_NONE;
10206}
10207
10208PyDoc_STRVAR(posix_fsetxattr__doc__,
10209"fsetxattr(fd, attr, value, flags=0)\n\n\
10210Like setxattr but operates on *fd* instead of a path.");
10211
10212static PyObject *
10213posix_fsetxattr(PyObject *self, PyObject *args)
10214{
10215 Py_buffer data;
10216 const char *name;
10217 int fd, flags = 0, err;
10218
10219 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10220 &name, &data, &flags))
10221 return NULL;
10222 Py_BEGIN_ALLOW_THREADS;
10223 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10224 Py_END_ALLOW_THREADS;
10225 Py_DECREF(name);
10226 PyBuffer_Release(&data);
10227 if (err)
10228 return posix_error();
10229 Py_RETURN_NONE;
10230}
10231
10232PyDoc_STRVAR(posix_removexattr__doc__,
10233"removexattr(path, attr)\n\n\
10234Remove extended attribute *attr* on *path*.");
10235
10236static PyObject *
10237posix_removexattr(PyObject *self, PyObject *args)
10238{
10239 PyObject *path, *name;
10240 int err;
10241
10242 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10243 PyUnicode_FSConverter, &name))
10244 return NULL;
10245 Py_BEGIN_ALLOW_THREADS;
10246 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10247 Py_END_ALLOW_THREADS;
10248 Py_DECREF(path);
10249 Py_DECREF(name);
10250 if (err)
10251 return posix_error();
10252 Py_RETURN_NONE;
10253}
10254
10255PyDoc_STRVAR(posix_lremovexattr__doc__,
10256"lremovexattr(path, attr)\n\n\
10257Like removexattr but don't follow symlinks.");
10258
10259static PyObject *
10260posix_lremovexattr(PyObject *self, PyObject *args)
10261{
10262 PyObject *path, *name;
10263 int err;
10264
10265 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10266 PyUnicode_FSConverter, &name))
10267 return NULL;
10268 Py_BEGIN_ALLOW_THREADS;
10269 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10270 Py_END_ALLOW_THREADS;
10271 Py_DECREF(path);
10272 Py_DECREF(name);
10273 if (err)
10274 return posix_error();
10275 Py_RETURN_NONE;
10276}
10277
10278PyDoc_STRVAR(posix_fremovexattr__doc__,
10279"fremovexattr(fd, attr)\n\n\
10280Like removexattr but operates on a file descriptor.");
10281
10282static PyObject *
10283posix_fremovexattr(PyObject *self, PyObject *args)
10284{
10285 PyObject *name;
10286 int fd, err;
10287
10288 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10289 PyUnicode_FSConverter, &name))
10290 return NULL;
10291 Py_BEGIN_ALLOW_THREADS;
10292 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10293 Py_END_ALLOW_THREADS;
10294 Py_DECREF(name);
10295 if (err)
10296 return posix_error();
10297 Py_RETURN_NONE;
10298}
10299
10300static Py_ssize_t
10301try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10302 Py_ssize_t buf_size, char **buf)
10303{
10304 Py_ssize_t len;
10305
10306 *buf = PyMem_MALLOC(buf_size);
10307 if (!*buf) {
10308 PyErr_NoMemory();
10309 return -1;
10310 }
10311 Py_BEGIN_ALLOW_THREADS;
10312 len = list(path, *buf, buf_size);
10313 Py_END_ALLOW_THREADS;
10314 if (len < 0) {
10315 PyMem_FREE(*buf);
10316 if (errno != ERANGE)
10317 posix_error();
10318 return -1;
10319 }
10320 return len;
10321}
10322
10323static PyObject *
10324listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10325{
10326 PyObject *res, *attr;
10327 Py_ssize_t len, err, start, i;
10328 char *buf;
10329
10330 len = try_listxattr(path, list, 256, &buf);
10331 if (len < 0) {
10332 if (PyErr_Occurred())
10333 return NULL;
10334 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10335 if (len < 0)
10336 return NULL;
10337 }
10338 res = PyList_New(0);
10339 if (!res) {
10340 PyMem_FREE(buf);
10341 return NULL;
10342 }
10343 for (start = i = 0; i < len; i++) {
10344 if (!buf[i]) {
10345 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10346 if (!attr) {
10347 Py_DECREF(res);
10348 PyMem_FREE(buf);
10349 return NULL;
10350 }
10351 err = PyList_Append(res, attr);
10352 Py_DECREF(attr);
10353 if (err) {
10354 Py_DECREF(res);
10355 PyMem_FREE(buf);
10356 return NULL;
10357 }
10358 start = i + 1;
10359 }
10360 }
10361 PyMem_FREE(buf);
10362 return res;
10363}
10364
10365PyDoc_STRVAR(posix_listxattr__doc__,
10366"listxattr(path)\n\n\
10367Return a list of extended attributes on *path*.");
10368
10369static PyObject *
10370posix_listxattr(PyObject *self, PyObject *args)
10371{
10372 PyObject *path, *res;
10373
10374 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10375 return NULL;
10376 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10377 Py_DECREF(path);
10378 return res;
10379}
10380
10381PyDoc_STRVAR(posix_llistxattr__doc__,
10382"llistxattr(path)\n\n\
10383Like listxattr but don't follow symlinks..");
10384
10385static PyObject *
10386posix_llistxattr(PyObject *self, PyObject *args)
10387{
10388 PyObject *path, *res;
10389
10390 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10391 return NULL;
10392 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10393 Py_DECREF(path);
10394 return res;
10395}
10396
10397static ssize_t
10398wrap_flistxattr(const char *path, char *buf, size_t len)
10399{
10400 /* Hack to share code. */
10401 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10402}
10403
10404PyDoc_STRVAR(posix_flistxattr__doc__,
10405"flistxattr(path)\n\n\
10406Like flistxattr but operates on a file descriptor.");
10407
10408static PyObject *
10409posix_flistxattr(PyObject *self, PyObject *args)
10410{
10411 long fd;
10412
10413 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10414 return NULL;
10415 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10416}
10417
Benjamin Peterson9428d532011-09-14 11:45:52 -040010418#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010419
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010420static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010421 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010422#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010423 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010424#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010425 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010426#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010427 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010428#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010429 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010430#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010431 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010432#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010433#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010434 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010435#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010436#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010437 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010438#endif /* HAVE_LCHMOD */
10439#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010440 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010441#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010442#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010443 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010444#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010445#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010446 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010447#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010448#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010449 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010450#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010451#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010452 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010453#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +000010454#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010455 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10456 METH_NOARGS, posix_getcwd__doc__},
10457 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10458 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +000010459#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010460#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010461 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010462#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010463 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010464#ifdef HAVE_FDOPENDIR
10465 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10466#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010467 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10468 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010469#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010471#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010472#ifdef HAVE_GETPRIORITY
10473 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10474#endif /* HAVE_GETPRIORITY */
10475#ifdef HAVE_SETPRIORITY
10476 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10477#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010478#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010479 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010480#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010481#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010482 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010483#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010484 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10485 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10486 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010487 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010488#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010489 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010490#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010491#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010492 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010493 win_symlink__doc__},
10494#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010495#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010496 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010497#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010498 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010499#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010501#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010502 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10503 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10504 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010505#ifdef HAVE_FUTIMES
10506 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10507#endif
10508#ifdef HAVE_LUTIMES
10509 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10510#endif
10511#ifdef HAVE_FUTIMENS
10512 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10513#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010514#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010516#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010517 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010518#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10520 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010521#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010522#ifdef HAVE_FEXECVE
10523 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10524#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010525#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010526 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10527 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010528#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010529 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10530 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010531#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010532#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010533#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010534 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010535#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010536#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010537 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010538#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010539#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010540#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010541 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10542 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010543#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010544#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010545 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010546#endif
10547#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010548 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010549#endif
10550#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010551 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010552#endif
10553#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010554 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010555#endif
10556#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010557 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010558#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010559 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010560#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010561 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10562 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10563#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010564#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010565#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010567#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010568#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010570#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010571#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010573#endif /* HAVE_GETEGID */
10574#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010576#endif /* HAVE_GETEUID */
10577#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010579#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010580#ifdef HAVE_GETGROUPLIST
10581 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10582#endif
Fred Drakec9680921999-12-13 16:37:25 +000010583#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010585#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010586 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010587#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010588 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010589#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010590#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010591 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010592#endif /* HAVE_GETPPID */
10593#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010594 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010595#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010596#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010597 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010598#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010599#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010600 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010601#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010602#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010603 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010604#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010605#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010606 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010607#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010608#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10610 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010611 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010612#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010613#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010615#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010616#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010617 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010618#endif /* HAVE_SETEUID */
10619#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010621#endif /* HAVE_SETEGID */
10622#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010623 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010624#endif /* HAVE_SETREUID */
10625#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010626 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010627#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010628#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010630#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010631#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010632 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010633#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010634#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010635 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010636#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010637#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010638 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010639#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010640#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010642#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010643#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010645#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010646#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010647 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010648#endif /* HAVE_WAIT3 */
10649#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010651#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010652#if defined(HAVE_WAITID) && !defined(__APPLE__)
10653 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10654#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010655#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010657#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010658#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010660#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010661#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010663#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010664#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010665 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010666#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010667#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010669#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010670#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010672#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010673 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10674 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10675 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10676 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10677 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10678 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010679#ifdef HAVE_LOCKF
10680 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10681#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010682 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10683 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010684#ifdef HAVE_READV
10685 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10686#endif
10687#ifdef HAVE_PREAD
10688 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10689#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010690 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010691#ifdef HAVE_WRITEV
10692 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10693#endif
10694#ifdef HAVE_PWRITE
10695 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10696#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010697#ifdef HAVE_SENDFILE
10698 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10699 posix_sendfile__doc__},
10700#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10702 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010703#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010705#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010706#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010707 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010708#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010709#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010711#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010712#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010714#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010715#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10717 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10718 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010719#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010720#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010721 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010722#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010723#ifdef HAVE_TRUNCATE
10724 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10725#endif
10726#ifdef HAVE_POSIX_FALLOCATE
10727 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10728#endif
10729#ifdef HAVE_POSIX_FADVISE
10730 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10731#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010732#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010733 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010734#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010735#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010736 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010737#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010738 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010739#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010741#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010742#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010744#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010745#ifdef HAVE_SYNC
10746 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10747#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010748#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010750#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010751#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010752#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010753 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010754#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010755#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010756 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010757#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010758#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010759 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010760#endif /* WIFSTOPPED */
10761#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010762 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010763#endif /* WIFSIGNALED */
10764#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010765 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010766#endif /* WIFEXITED */
10767#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010768 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010769#endif /* WEXITSTATUS */
10770#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010771 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010772#endif /* WTERMSIG */
10773#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010774 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010775#endif /* WSTOPSIG */
10776#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010777#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010778 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010779#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010780#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010781 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010782#endif
Fred Drakec9680921999-12-13 16:37:25 +000010783#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010784 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010785#endif
10786#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010787 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010788#endif
10789#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010790 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010791#endif
10792#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010793 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010794#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010795 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010796#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010798 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010799 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010800 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010801 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010802#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010803#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010804 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010805#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010806 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010807 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010808 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010809 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010810 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010811 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010812#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010813 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010814#endif
10815#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010816 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010817#endif
10818#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010819 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010820#endif
10821#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010823#endif
10824
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010825/* posix *at family of functions */
10826#ifdef HAVE_FACCESSAT
10827 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10828#endif
10829#ifdef HAVE_FCHMODAT
10830 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10831#endif /* HAVE_FCHMODAT */
10832#ifdef HAVE_FCHOWNAT
10833 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10834#endif /* HAVE_FCHOWNAT */
10835#ifdef HAVE_FSTATAT
10836 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10837#endif
10838#ifdef HAVE_FUTIMESAT
10839 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10840#endif
10841#ifdef HAVE_LINKAT
10842 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10843#endif /* HAVE_LINKAT */
10844#ifdef HAVE_MKDIRAT
10845 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10846#endif
10847#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10848 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10849#endif
10850#ifdef HAVE_OPENAT
10851 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10852#endif
10853#ifdef HAVE_READLINKAT
10854 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10855#endif /* HAVE_READLINKAT */
10856#ifdef HAVE_RENAMEAT
10857 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10858#endif
10859#if HAVE_SYMLINKAT
10860 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10861#endif /* HAVE_SYMLINKAT */
10862#ifdef HAVE_UNLINKAT
10863 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10864#endif
10865#ifdef HAVE_UTIMENSAT
10866 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10867#endif
10868#ifdef HAVE_MKFIFOAT
10869 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10870#endif
Benjamin Peterson9428d532011-09-14 11:45:52 -040010871#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010872 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
10873 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
10874 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
10875 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
10876 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
10877 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
10878 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
10879 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
10880 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
10881 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
10882 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
10883 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
10884#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010885 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010886};
10887
10888
Barry Warsaw4a342091996-12-19 23:50:02 +000010889static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010890ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010891{
Victor Stinner8c62be82010-05-06 00:08:46 +000010892 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010893}
10894
Guido van Rossumd48f2521997-12-05 22:19:34 +000010895#if defined(PYOS_OS2)
10896/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010897static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010898{
10899 APIRET rc;
10900 ULONG values[QSV_MAX+1];
10901 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010902 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010903
10904 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010905 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010906 Py_END_ALLOW_THREADS
10907
10908 if (rc != NO_ERROR) {
10909 os2_error(rc);
10910 return -1;
10911 }
10912
Fred Drake4d1e64b2002-04-15 19:40:07 +000010913 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10914 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10915 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10916 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10917 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10918 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10919 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010920
10921 switch (values[QSV_VERSION_MINOR]) {
10922 case 0: ver = "2.00"; break;
10923 case 10: ver = "2.10"; break;
10924 case 11: ver = "2.11"; break;
10925 case 30: ver = "3.00"; break;
10926 case 40: ver = "4.00"; break;
10927 case 50: ver = "5.00"; break;
10928 default:
Tim Peters885d4572001-11-28 20:27:42 +000010929 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010930 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010931 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010932 ver = &tmp[0];
10933 }
10934
10935 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010936 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010937 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010938
10939 /* Add Indicator of Which Drive was Used to Boot the System */
10940 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10941 tmp[1] = ':';
10942 tmp[2] = '\0';
10943
Fred Drake4d1e64b2002-04-15 19:40:07 +000010944 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010945}
10946#endif
10947
Brian Curtin52173d42010-12-02 18:29:18 +000010948#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010949static int
Brian Curtin52173d42010-12-02 18:29:18 +000010950enable_symlink()
10951{
10952 HANDLE tok;
10953 TOKEN_PRIVILEGES tok_priv;
10954 LUID luid;
10955 int meth_idx = 0;
10956
10957 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010958 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010959
10960 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010961 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010962
10963 tok_priv.PrivilegeCount = 1;
10964 tok_priv.Privileges[0].Luid = luid;
10965 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10966
10967 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10968 sizeof(TOKEN_PRIVILEGES),
10969 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010970 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010971
Brian Curtin3b4499c2010-12-28 14:31:47 +000010972 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10973 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010974}
10975#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10976
Barry Warsaw4a342091996-12-19 23:50:02 +000010977static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010978all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010979{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010980#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010981 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010982#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010983#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010984 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010985#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010986#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010987 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010988#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010989#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010990 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010991#endif
Fred Drakec9680921999-12-13 16:37:25 +000010992#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010993 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010994#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010995#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010996 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010997#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010998#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010999 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011000#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011001#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011002 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011003#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011004#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011005 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011006#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011007#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011008 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011009#endif
11010#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011011 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011012#endif
11013#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011014 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011015#endif
11016#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011017 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011018#endif
11019#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011020 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011021#endif
11022#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011023 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011024#endif
11025#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011026 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011027#endif
11028#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011029 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011030#endif
11031#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011032 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011033#endif
11034#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011035 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011036#endif
11037#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011038 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011039#endif
11040#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011041 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011042#endif
11043#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011044 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011045#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011046#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011047 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011048#endif
11049#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011050 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011051#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011052#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011053 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011054#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011055#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011056 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011057#endif
11058#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011059 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011060#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011061#ifdef PRIO_PROCESS
11062 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11063#endif
11064#ifdef PRIO_PGRP
11065 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11066#endif
11067#ifdef PRIO_USER
11068 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11069#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011070#ifdef O_CLOEXEC
11071 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11072#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011073/* posix - constants for *at functions */
11074#ifdef AT_SYMLINK_NOFOLLOW
11075 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11076#endif
11077#ifdef AT_EACCESS
11078 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11079#endif
11080#ifdef AT_FDCWD
11081 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11082#endif
11083#ifdef AT_REMOVEDIR
11084 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11085#endif
11086#ifdef AT_SYMLINK_FOLLOW
11087 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11088#endif
11089#ifdef UTIME_NOW
11090 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11091#endif
11092#ifdef UTIME_OMIT
11093 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11094#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011095
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011096
Tim Peters5aa91602002-01-30 05:46:57 +000011097/* MS Windows */
11098#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 /* Don't inherit in child processes. */
11100 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011101#endif
11102#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 /* Optimize for short life (keep in memory). */
11104 /* MS forgot to define this one with a non-underscore form too. */
11105 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011106#endif
11107#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 /* Automatically delete when last handle is closed. */
11109 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011110#endif
11111#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 /* Optimize for random access. */
11113 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011114#endif
11115#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011116 /* Optimize for sequential access. */
11117 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011118#endif
11119
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011120/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011121#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011122 /* Send a SIGIO signal whenever input or output
11123 becomes available on file descriptor */
11124 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011125#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011126#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011127 /* Direct disk access. */
11128 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011129#endif
11130#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011131 /* Must be a directory. */
11132 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011133#endif
11134#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 /* Do not follow links. */
11136 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011137#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011138#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 /* Do not update the access time. */
11140 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011141#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011142
Victor Stinner8c62be82010-05-06 00:08:46 +000011143 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011144#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011145 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011146#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011147#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011148 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011149#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011150#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011151 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011152#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011153#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011154 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011155#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011156#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011157 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011158#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011159#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011160 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011161#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011162#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011163 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011164#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011165#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011166 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011167#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011168#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011169 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011170#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011171#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011172 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011173#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011174#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011175 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011176#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011177#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011178 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011179#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011180#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011181 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011182#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011183#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011184 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011185#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011186#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011187 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011188#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011189#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011190 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011191#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011192#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011193 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011194#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011195
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011196 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011197#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011198 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011199#endif /* ST_RDONLY */
11200#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011201 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011202#endif /* ST_NOSUID */
11203
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011204 /* FreeBSD sendfile() constants */
11205#ifdef SF_NODISKIO
11206 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11207#endif
11208#ifdef SF_MNOWAIT
11209 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11210#endif
11211#ifdef SF_SYNC
11212 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11213#endif
11214
Ross Lagerwall7807c352011-03-17 20:20:30 +020011215 /* constants for posix_fadvise */
11216#ifdef POSIX_FADV_NORMAL
11217 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11218#endif
11219#ifdef POSIX_FADV_SEQUENTIAL
11220 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11221#endif
11222#ifdef POSIX_FADV_RANDOM
11223 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11224#endif
11225#ifdef POSIX_FADV_NOREUSE
11226 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11227#endif
11228#ifdef POSIX_FADV_WILLNEED
11229 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11230#endif
11231#ifdef POSIX_FADV_DONTNEED
11232 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11233#endif
11234
11235 /* constants for waitid */
11236#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11237 if (ins(d, "P_PID", (long)P_PID)) return -1;
11238 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11239 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11240#endif
11241#ifdef WEXITED
11242 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11243#endif
11244#ifdef WNOWAIT
11245 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11246#endif
11247#ifdef WSTOPPED
11248 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11249#endif
11250#ifdef CLD_EXITED
11251 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11252#endif
11253#ifdef CLD_DUMPED
11254 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11255#endif
11256#ifdef CLD_TRAPPED
11257 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11258#endif
11259#ifdef CLD_CONTINUED
11260 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11261#endif
11262
11263 /* constants for lockf */
11264#ifdef F_LOCK
11265 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11266#endif
11267#ifdef F_TLOCK
11268 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11269#endif
11270#ifdef F_ULOCK
11271 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11272#endif
11273#ifdef F_TEST
11274 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11275#endif
11276
11277 /* constants for futimens */
11278#ifdef UTIME_NOW
11279 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11280#endif
11281#ifdef UTIME_OMIT
11282 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11283#endif
11284
Guido van Rossum246bc171999-02-01 23:54:31 +000011285#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011286#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011287 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11288 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11289 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11290 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11291 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11292 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11293 if (ins(d, "P_PM", (long)P_PM)) return -1;
11294 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11295 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11296 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11297 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11298 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11299 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11300 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11301 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11302 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11303 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11304 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11305 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11306 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011307#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011308 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11309 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11310 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11311 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11312 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011313#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011314#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011315
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011316#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011317 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011318 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11319 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11320#ifdef SCHED_SPORADIC
11321 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11322#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011323#ifdef SCHED_BATCH
11324 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11325#endif
11326#ifdef SCHED_IDLE
11327 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11328#endif
11329#ifdef SCHED_RESET_ON_FORK
11330 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11331#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011332#ifdef SCHED_SYS
11333 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11334#endif
11335#ifdef SCHED_IA
11336 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11337#endif
11338#ifdef SCHED_FSS
11339 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11340#endif
11341#ifdef SCHED_FX
11342 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11343#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011344#endif
11345
Benjamin Peterson9428d532011-09-14 11:45:52 -040011346#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011347 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11348 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11349 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11350#endif
11351
Guido van Rossumd48f2521997-12-05 22:19:34 +000011352#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011353 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011354#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011355 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011356}
11357
11358
Tim Peters5aa91602002-01-30 05:46:57 +000011359#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011360#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011361#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011362
11363#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011364#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011365#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011366
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011367#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011368#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011369#define MODNAME "posix"
11370#endif
11371
Martin v. Löwis1a214512008-06-11 05:26:20 +000011372static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011373 PyModuleDef_HEAD_INIT,
11374 MODNAME,
11375 posix__doc__,
11376 -1,
11377 posix_methods,
11378 NULL,
11379 NULL,
11380 NULL,
11381 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011382};
11383
11384
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011385PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011386INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011387{
Victor Stinner8c62be82010-05-06 00:08:46 +000011388 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011389
Brian Curtin52173d42010-12-02 18:29:18 +000011390#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011391 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011392#endif
11393
Victor Stinner8c62be82010-05-06 00:08:46 +000011394 m = PyModule_Create(&posixmodule);
11395 if (m == NULL)
11396 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011397
Victor Stinner8c62be82010-05-06 00:08:46 +000011398 /* Initialize environ dictionary */
11399 v = convertenviron();
11400 Py_XINCREF(v);
11401 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11402 return NULL;
11403 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011404
Victor Stinner8c62be82010-05-06 00:08:46 +000011405 if (all_ins(m))
11406 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011407
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 if (setup_confname_tables(m))
11409 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011410
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 Py_INCREF(PyExc_OSError);
11412 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011413
Benjamin Peterson2740af82011-08-02 17:41:34 -050011414#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011415 if (PyType_Ready(&cpu_set_type) < 0)
11416 return NULL;
11417 Py_INCREF(&cpu_set_type);
11418 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011419#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011420
Guido van Rossumb3d39562000-01-31 18:41:26 +000011421#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011422 if (posix_putenv_garbage == NULL)
11423 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011424#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011425
Victor Stinner8c62be82010-05-06 00:08:46 +000011426 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011427#if defined(HAVE_WAITID) && !defined(__APPLE__)
11428 waitid_result_desc.name = MODNAME ".waitid_result";
11429 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11430#endif
11431
Victor Stinner8c62be82010-05-06 00:08:46 +000011432 stat_result_desc.name = MODNAME ".stat_result";
11433 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11434 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11435 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11436 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11437 structseq_new = StatResultType.tp_new;
11438 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011439
Victor Stinner8c62be82010-05-06 00:08:46 +000011440 statvfs_result_desc.name = MODNAME ".statvfs_result";
11441 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011442#ifdef NEED_TICKS_PER_SECOND
11443# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011444 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011445# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011446 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011447# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011448 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011449# endif
11450#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011451
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011452#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011453 sched_param_desc.name = MODNAME ".sched_param";
11454 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11455 SchedParamType.tp_new = sched_param_new;
11456#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011457 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011458#if defined(HAVE_WAITID) && !defined(__APPLE__)
11459 Py_INCREF((PyObject*) &WaitidResultType);
11460 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11461#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011462 Py_INCREF((PyObject*) &StatResultType);
11463 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11464 Py_INCREF((PyObject*) &StatVFSResultType);
11465 PyModule_AddObject(m, "statvfs_result",
11466 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011467
11468#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011469 Py_INCREF(&SchedParamType);
11470 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011471#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011472 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011473
11474#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011475 /*
11476 * Step 2 of weak-linking support on Mac OS X.
11477 *
11478 * The code below removes functions that are not available on the
11479 * currently active platform.
11480 *
11481 * This block allow one to use a python binary that was build on
11482 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11483 * OSX 10.4.
11484 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011485#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011486 if (fstatvfs == NULL) {
11487 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11488 return NULL;
11489 }
11490 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011491#endif /* HAVE_FSTATVFS */
11492
11493#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011494 if (statvfs == NULL) {
11495 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11496 return NULL;
11497 }
11498 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011499#endif /* HAVE_STATVFS */
11500
11501# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011502 if (lchown == NULL) {
11503 if (PyObject_DelAttrString(m, "lchown") == -1) {
11504 return NULL;
11505 }
11506 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011507#endif /* HAVE_LCHOWN */
11508
11509
11510#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011511 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011512
Guido van Rossumb6775db1994-08-01 11:34:53 +000011513}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011514
11515#ifdef __cplusplus
11516}
11517#endif