blob: e42db5ba8d56720cbf2d06fc2a255ca7931e4b58 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020031# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#endif /* defined(__VMS) */
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000040"This module provides access to operating system functionality that is\n\
41standardized by the C Standard and the POSIX standard (a thinly\n\
42disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000045
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020047#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Ross Lagerwall4d076da2011-03-18 06:56:53 +020062#ifdef HAVE_SYS_UIO_H
63#include <sys/uio.h>
64#endif
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_TYPES_H */
69
70#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000073
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000075#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000079#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000081
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_FCNTL_H
83#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000084#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000085
Guido van Rossuma6535fd2001-10-18 19:44:10 +000086#ifdef HAVE_GRP_H
87#include <grp.h>
88#endif
89
Barry Warsaw5676bd12003-01-07 20:57:09 +000090#ifdef HAVE_SYSEXITS_H
91#include <sysexits.h>
92#endif /* HAVE_SYSEXITS_H */
93
Anthony Baxter8a560de2004-10-13 15:30:56 +000094#ifdef HAVE_SYS_LOADAVG_H
95#include <sys/loadavg.h>
96#endif
97
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000098#ifdef HAVE_LANGINFO_H
99#include <langinfo.h>
100#endif
101
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000102#ifdef HAVE_SYS_SENDFILE_H
103#include <sys/sendfile.h>
104#endif
105
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500106#ifdef HAVE_SCHED_H
107#include <sched.h>
108#endif
109
Benjamin Peterson9428d532011-09-14 11:45:52 -0400110#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
111#define USE_XATTRS
112#endif
113
114#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400115#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400116#endif
117
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
119#ifdef HAVE_SYS_SOCKET_H
120#include <sys/socket.h>
121#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#endif
123
Victor Stinner8b905bd2011-10-25 13:34:04 +0200124#ifdef HAVE_DLFCN_H
125#include <dlfcn.h>
126#endif
127
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000129/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000130#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000131#include <process.h>
132#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000133#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#define HAVE_GETCWD 1
135#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000136#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#if defined(__OS2__)
138#define HAVE_EXECV 1
139#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#include <process.h>
142#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000143#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000144#define HAVE_EXECV 1
145#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#define HAVE_OPENDIR 1
147#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#define HAVE_WAIT 1
150#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000152#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000153#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000154#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_EXECV 1
157#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000158#define HAVE_SYSTEM 1
159#define HAVE_CWAIT 1
160#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000161#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000162#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000163#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
164/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000165#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166/* Unix functions that the configure script doesn't check for */
167#define HAVE_EXECV 1
168#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000169#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000170#define HAVE_FORK1 1
171#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#define HAVE_GETCWD 1
173#define HAVE_GETEGID 1
174#define HAVE_GETEUID 1
175#define HAVE_GETGID 1
176#define HAVE_GETPPID 1
177#define HAVE_GETUID 1
178#define HAVE_KILL 1
179#define HAVE_OPENDIR 1
180#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000183#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000184#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000185#endif /* _MSC_VER */
186#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000187#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000188#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000189
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000191
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000192#if defined(__sgi)&&_COMPILER_VERSION>=700
193/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
194 (default) */
195extern char *ctermid_r(char *);
196#endif
197
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000198#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000199#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000201#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000202#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000204#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000205extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000206#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000207#endif
208#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000209extern int chdir(char *);
210extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000211#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int chdir(const char *);
213extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000214#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000215#ifdef __BORLANDC__
216extern int chmod(const char *, int);
217#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000219#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000220/*#ifdef HAVE_FCHMOD
221extern int fchmod(int, mode_t);
222#endif*/
223/*#ifdef HAVE_LCHMOD
224extern int lchmod(const char *, mode_t);
225#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000226extern int chown(const char *, uid_t, gid_t);
227extern char *getcwd(char *, int);
228extern char *strerror(int);
229extern int link(const char *, const char *);
230extern int rename(const char *, const char *);
231extern int stat(const char *, struct stat *);
232extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000234extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000235#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000237extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000240
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000241#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#ifdef HAVE_UTIME_H
244#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000245#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000247#ifdef HAVE_SYS_UTIME_H
248#include <sys/utime.h>
249#define HAVE_UTIME_H /* pretend we do for the rest of this file */
250#endif /* HAVE_SYS_UTIME_H */
251
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#ifdef HAVE_SYS_TIMES_H
253#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
256#ifdef HAVE_SYS_PARAM_H
257#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259
260#ifdef HAVE_SYS_UTSNAME_H
261#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000262#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000264#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000266#define NAMLEN(dirent) strlen((dirent)->d_name)
267#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000268#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#include <direct.h>
270#define NAMLEN(dirent) strlen((dirent)->d_name)
271#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000274#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000277#endif
278#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280#endif
281#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#endif
284#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000286#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289#endif
290#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000292#endif
293#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000296#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000297#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000298#endif
299#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000300#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000301#endif
302#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000303#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000304#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000305#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000306#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000308#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000309#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000310#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
311#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000312static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000313#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000314#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000315
Guido van Rossumd48f2521997-12-05 22:19:34 +0000316#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000317#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000318#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321#if defined(PATH_MAX) && PATH_MAX > 1024
322#define MAXPATHLEN PATH_MAX
323#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000326#endif /* MAXPATHLEN */
327
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000328#ifdef UNION_WAIT
329/* Emulate some macros on systems that have a union instead of macros */
330
331#ifndef WIFEXITED
332#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
333#endif
334
335#ifndef WEXITSTATUS
336#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
337#endif
338
339#ifndef WTERMSIG
340#define WTERMSIG(u_wait) ((u_wait).w_termsig)
341#endif
342
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343#define WAIT_TYPE union wait
344#define WAIT_STATUS_INT(s) (s.w_status)
345
346#else /* !UNION_WAIT */
347#define WAIT_TYPE int
348#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000349#endif /* UNION_WAIT */
350
Greg Wardb48bc172000-03-01 21:51:56 +0000351/* Don't use the "_r" form if we don't need it (also, won't have a
352 prototype for it, at least on Solaris -- maybe others as well?). */
353#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
354#define USE_CTERMID_R
355#endif
356
Fred Drake699f3522000-06-29 21:12:41 +0000357/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000358#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000359#undef FSTAT
360#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000361#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000362# define STAT win32_stat
363# define FSTAT win32_fstat
364# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000365#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT stat
367# define FSTAT fstat
368# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000369#endif
370
Tim Peters11b23062003-04-23 02:39:17 +0000371#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000372#include <sys/mkdev.h>
373#else
374#if defined(MAJOR_IN_SYSMACROS)
375#include <sys/sysmacros.h>
376#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000377#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
378#include <sys/mkdev.h>
379#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000380#endif
Fred Drake699f3522000-06-29 21:12:41 +0000381
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200382/* A helper used by a number of POSIX-only functions */
383#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000384static int
385_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000386{
387#if !defined(HAVE_LARGEFILE_SUPPORT)
388 *((off_t*)addr) = PyLong_AsLong(arg);
389#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000390 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000391#endif
392 if (PyErr_Occurred())
393 return 0;
394 return 1;
395}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200396#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000397
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000398#if defined _MSC_VER && _MSC_VER >= 1400
399/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
400 * valid and throw an assertion if it isn't.
401 * Normally, an invalid fd is likely to be a C program error and therefore
402 * an assertion can be useful, but it does contradict the POSIX standard
403 * which for write(2) states:
404 * "Otherwise, -1 shall be returned and errno set to indicate the error."
405 * "[EBADF] The fildes argument is not a valid file descriptor open for
406 * writing."
407 * Furthermore, python allows the user to enter any old integer
408 * as a fd and should merely raise a python exception on error.
409 * The Microsoft CRT doesn't provide an official way to check for the
410 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000411 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000412 * internal structures involved.
413 * The structures below must be updated for each version of visual studio
414 * according to the file internal.h in the CRT source, until MS comes
415 * up with a less hacky way to do this.
416 * (all of this is to avoid globally modifying the CRT behaviour using
417 * _set_invalid_parameter_handler() and _CrtSetReportMode())
418 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000419/* The actual size of the structure is determined at runtime.
420 * Only the first items must be present.
421 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000423 intptr_t osfhnd;
424 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000425} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000426
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000427extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000428#define IOINFO_L2E 5
429#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
430#define IOINFO_ARRAYS 64
431#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
432#define FOPEN 0x01
433#define _NO_CONSOLE_FILENO (intptr_t)-2
434
435/* This function emulates what the windows CRT does to validate file handles */
436int
437_PyVerify_fd(int fd)
438{
Victor Stinner8c62be82010-05-06 00:08:46 +0000439 const int i1 = fd >> IOINFO_L2E;
440 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000441
Antoine Pitrou22e41552010-08-15 18:07:50 +0000442 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000443
Victor Stinner8c62be82010-05-06 00:08:46 +0000444 /* Determine the actual size of the ioinfo structure,
445 * as used by the CRT loaded in memory
446 */
447 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
448 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
449 }
450 if (sizeof_ioinfo == 0) {
451 /* This should not happen... */
452 goto fail;
453 }
454
455 /* See that it isn't a special CLEAR fileno */
456 if (fd != _NO_CONSOLE_FILENO) {
457 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
458 * we check pointer validity and other info
459 */
460 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
461 /* finally, check that the file is open */
462 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
463 if (info->osfile & FOPEN) {
464 return 1;
465 }
466 }
467 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000468 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000469 errno = EBADF;
470 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000471}
472
473/* the special case of checking dup2. The target fd must be in a sensible range */
474static int
475_PyVerify_fd_dup2(int fd1, int fd2)
476{
Victor Stinner8c62be82010-05-06 00:08:46 +0000477 if (!_PyVerify_fd(fd1))
478 return 0;
479 if (fd2 == _NO_CONSOLE_FILENO)
480 return 0;
481 if ((unsigned)fd2 < _NHANDLE_)
482 return 1;
483 else
484 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000485}
486#else
487/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
488#define _PyVerify_fd_dup2(A, B) (1)
489#endif
490
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000491#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000492/* The following structure was copied from
493 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
494 include doesn't seem to be present in the Windows SDK (at least as included
495 with Visual Studio Express). */
496typedef struct _REPARSE_DATA_BUFFER {
497 ULONG ReparseTag;
498 USHORT ReparseDataLength;
499 USHORT Reserved;
500 union {
501 struct {
502 USHORT SubstituteNameOffset;
503 USHORT SubstituteNameLength;
504 USHORT PrintNameOffset;
505 USHORT PrintNameLength;
506 ULONG Flags;
507 WCHAR PathBuffer[1];
508 } SymbolicLinkReparseBuffer;
509
510 struct {
511 USHORT SubstituteNameOffset;
512 USHORT SubstituteNameLength;
513 USHORT PrintNameOffset;
514 USHORT PrintNameLength;
515 WCHAR PathBuffer[1];
516 } MountPointReparseBuffer;
517
518 struct {
519 UCHAR DataBuffer[1];
520 } GenericReparseBuffer;
521 };
522} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
523
524#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
525 GenericReparseBuffer)
526#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
527
528static int
Brian Curtind25aef52011-06-13 15:16:04 -0500529win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000530{
531 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
532 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
533 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000534
535 if (0 == DeviceIoControl(
536 reparse_point_handle,
537 FSCTL_GET_REPARSE_POINT,
538 NULL, 0, /* in buffer */
539 target_buffer, sizeof(target_buffer),
540 &n_bytes_returned,
541 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500542 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000543
544 if (reparse_tag)
545 *reparse_tag = rdb->ReparseTag;
546
Brian Curtind25aef52011-06-13 15:16:04 -0500547 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000548}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100549
550static int
551win32_warn_bytes_api()
552{
553 return PyErr_WarnEx(PyExc_DeprecationWarning,
554 "The Windows bytes API has been deprecated, "
555 "use Unicode filenames instead",
556 1);
557}
558
559static PyObject*
560win32_decode_filename(PyObject *obj)
561{
562 PyObject *unicode;
563 if (PyUnicode_Check(obj)) {
564 if (PyUnicode_READY(obj))
565 return NULL;
566 Py_INCREF(obj);
567 return obj;
568 }
569 if (!PyUnicode_FSDecoder(obj, &unicode))
570 return NULL;
571 if (win32_warn_bytes_api()) {
572 Py_DECREF(unicode);
573 return NULL;
574 }
575 return unicode;
576}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000577#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000578
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000580#ifdef WITH_NEXT_FRAMEWORK
581/* On Darwin/MacOSX a shared library or framework has no access to
582** environ directly, we must obtain it with _NSGetEnviron().
583*/
584#include <crt_externs.h>
585static char **environ;
586#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000588#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589
Barry Warsaw53699e91996-12-10 23:23:01 +0000590static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000591convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592{
Victor Stinner8c62be82010-05-06 00:08:46 +0000593 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000594#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000595 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000596#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000597 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000598#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000599#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000600 APIRET rc;
601 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
602#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000603
Victor Stinner8c62be82010-05-06 00:08:46 +0000604 d = PyDict_New();
605 if (d == NULL)
606 return NULL;
607#ifdef WITH_NEXT_FRAMEWORK
608 if (environ == NULL)
609 environ = *_NSGetEnviron();
610#endif
611#ifdef MS_WINDOWS
612 /* _wenviron must be initialized in this way if the program is started
613 through main() instead of wmain(). */
614 _wgetenv(L"");
615 if (_wenviron == NULL)
616 return d;
617 /* This part ignores errors */
618 for (e = _wenviron; *e != NULL; e++) {
619 PyObject *k;
620 PyObject *v;
621 wchar_t *p = wcschr(*e, L'=');
622 if (p == NULL)
623 continue;
624 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
625 if (k == NULL) {
626 PyErr_Clear();
627 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000628 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000629 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
630 if (v == NULL) {
631 PyErr_Clear();
632 Py_DECREF(k);
633 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000634 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000635 if (PyDict_GetItem(d, k) == NULL) {
636 if (PyDict_SetItem(d, k, v) != 0)
637 PyErr_Clear();
638 }
639 Py_DECREF(k);
640 Py_DECREF(v);
641 }
642#else
643 if (environ == NULL)
644 return d;
645 /* This part ignores errors */
646 for (e = environ; *e != NULL; e++) {
647 PyObject *k;
648 PyObject *v;
649 char *p = strchr(*e, '=');
650 if (p == NULL)
651 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000652 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000653 if (k == NULL) {
654 PyErr_Clear();
655 continue;
656 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000657 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000658 if (v == NULL) {
659 PyErr_Clear();
660 Py_DECREF(k);
661 continue;
662 }
663 if (PyDict_GetItem(d, k) == NULL) {
664 if (PyDict_SetItem(d, k, v) != 0)
665 PyErr_Clear();
666 }
667 Py_DECREF(k);
668 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000669 }
670#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000671#if defined(PYOS_OS2)
672 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
673 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
674 PyObject *v = PyBytes_FromString(buffer);
675 PyDict_SetItemString(d, "BEGINLIBPATH", v);
676 Py_DECREF(v);
677 }
678 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
679 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
680 PyObject *v = PyBytes_FromString(buffer);
681 PyDict_SetItemString(d, "ENDLIBPATH", v);
682 Py_DECREF(v);
683 }
684#endif
685 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686}
687
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688/* Set a POSIX-specific error from errno, and return NULL */
689
Barry Warsawd58d7641998-07-23 16:14:40 +0000690static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000691posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000692{
Victor Stinner8c62be82010-05-06 00:08:46 +0000693 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694}
Barry Warsawd58d7641998-07-23 16:14:40 +0000695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000696posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000697{
Victor Stinner8c62be82010-05-06 00:08:46 +0000698 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000699}
700
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000701
Mark Hammondef8b6542001-05-13 08:04:26 +0000702static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000703posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000704{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000705 PyObject *name_str, *rc;
706 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
707 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000708 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000709 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
710 name_str);
711 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000712 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000713}
714
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000715#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000716static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000717win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000718{
Victor Stinner8c62be82010-05-06 00:08:46 +0000719 /* XXX We should pass the function name along in the future.
720 (winreg.c also wants to pass the function name.)
721 This would however require an additional param to the
722 Windows error object, which is non-trivial.
723 */
724 errno = GetLastError();
725 if (filename)
726 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
727 else
728 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000729}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000730
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000731static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +0200732win32_error_unicode(char* function, wchar_t* filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000733{
Victor Stinner8c62be82010-05-06 00:08:46 +0000734 /* XXX - see win32_error for comments on 'function' */
735 errno = GetLastError();
736 if (filename)
737 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
738 else
739 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000740}
741
Victor Stinnereb5657a2011-09-30 01:44:27 +0200742static PyObject *
743win32_error_object(char* function, PyObject* filename)
744{
745 /* XXX - see win32_error for comments on 'function' */
746 errno = GetLastError();
747 if (filename)
748 return PyErr_SetExcFromWindowsErrWithFilenameObject(
749 PyExc_WindowsError,
750 errno,
751 filename);
752 else
753 return PyErr_SetFromWindowsErr(errno);
754}
755
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000756#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757
Guido van Rossumd48f2521997-12-05 22:19:34 +0000758#if defined(PYOS_OS2)
759/**********************************************************************
760 * Helper Function to Trim and Format OS/2 Messages
761 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000762static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000763os2_formatmsg(char *msgbuf, int msglen, char *reason)
764{
765 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
766
767 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
768 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
769
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000770 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000771 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
772 }
773
774 /* Add Optional Reason Text */
775 if (reason) {
776 strcat(msgbuf, " : ");
777 strcat(msgbuf, reason);
778 }
779}
780
781/**********************************************************************
782 * Decode an OS/2 Operating System Error Code
783 *
784 * A convenience function to lookup an OS/2 error code and return a
785 * text message we can use to raise a Python exception.
786 *
787 * Notes:
788 * The messages for errors returned from the OS/2 kernel reside in
789 * the file OSO001.MSG in the \OS2 directory hierarchy.
790 *
791 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000792static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000793os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
794{
795 APIRET rc;
796 ULONG msglen;
797
798 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
799 Py_BEGIN_ALLOW_THREADS
800 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
801 errorcode, "oso001.msg", &msglen);
802 Py_END_ALLOW_THREADS
803
804 if (rc == NO_ERROR)
805 os2_formatmsg(msgbuf, msglen, reason);
806 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000807 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000808 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000809
810 return msgbuf;
811}
812
813/* Set an OS/2-specific error and return NULL. OS/2 kernel
814 errors are not in a global variable e.g. 'errno' nor are
815 they congruent with posix error numbers. */
816
Victor Stinner8c62be82010-05-06 00:08:46 +0000817static PyObject *
818os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000819{
820 char text[1024];
821 PyObject *v;
822
823 os2_strerror(text, sizeof(text), code, "");
824
825 v = Py_BuildValue("(is)", code, text);
826 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000827 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000828 Py_DECREF(v);
829 }
830 return NULL; /* Signal to Python that an Exception is Pending */
831}
832
833#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834
835/* POSIX generic methods */
836
Barry Warsaw53699e91996-12-10 23:23:01 +0000837static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000838posix_fildes(PyObject *fdobj, int (*func)(int))
839{
Victor Stinner8c62be82010-05-06 00:08:46 +0000840 int fd;
841 int res;
842 fd = PyObject_AsFileDescriptor(fdobj);
843 if (fd < 0)
844 return NULL;
845 if (!_PyVerify_fd(fd))
846 return posix_error();
847 Py_BEGIN_ALLOW_THREADS
848 res = (*func)(fd);
849 Py_END_ALLOW_THREADS
850 if (res < 0)
851 return posix_error();
852 Py_INCREF(Py_None);
853 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000854}
Guido van Rossum21142a01999-01-08 21:05:37 +0000855
856static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000857posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000858{
Victor Stinner8c62be82010-05-06 00:08:46 +0000859 PyObject *opath1 = NULL;
860 char *path1;
861 int res;
862 if (!PyArg_ParseTuple(args, format,
863 PyUnicode_FSConverter, &opath1))
864 return NULL;
865 path1 = PyBytes_AsString(opath1);
866 Py_BEGIN_ALLOW_THREADS
867 res = (*func)(path1);
868 Py_END_ALLOW_THREADS
869 if (res < 0)
870 return posix_error_with_allocated_filename(opath1);
871 Py_DECREF(opath1);
872 Py_INCREF(Py_None);
873 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000874}
875
Barry Warsaw53699e91996-12-10 23:23:01 +0000876static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000877posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000878 char *format,
879 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000880{
Victor Stinner8c62be82010-05-06 00:08:46 +0000881 PyObject *opath1 = NULL, *opath2 = NULL;
882 char *path1, *path2;
883 int res;
884 if (!PyArg_ParseTuple(args, format,
885 PyUnicode_FSConverter, &opath1,
886 PyUnicode_FSConverter, &opath2)) {
887 return NULL;
888 }
889 path1 = PyBytes_AsString(opath1);
890 path2 = PyBytes_AsString(opath2);
891 Py_BEGIN_ALLOW_THREADS
892 res = (*func)(path1, path2);
893 Py_END_ALLOW_THREADS
894 Py_DECREF(opath1);
895 Py_DECREF(opath2);
896 if (res != 0)
897 /* XXX how to report both path1 and path2??? */
898 return posix_error();
899 Py_INCREF(Py_None);
900 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000901}
902
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000903#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000905win32_1str(PyObject* args, char* func,
906 char* format, BOOL (__stdcall *funcA)(LPCSTR),
907 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908{
Victor Stinner8c62be82010-05-06 00:08:46 +0000909 PyObject *uni;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100910 const char *ansi;
Victor Stinner8c62be82010-05-06 00:08:46 +0000911 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000912
Victor Stinnereb5657a2011-09-30 01:44:27 +0200913 if (PyArg_ParseTuple(args, wformat, &uni))
914 {
915 wchar_t *wstr = PyUnicode_AsUnicode(uni);
916 if (wstr == NULL)
917 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000918 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +0200919 result = funcW(wstr);
Victor Stinner8c62be82010-05-06 00:08:46 +0000920 Py_END_ALLOW_THREADS
921 if (!result)
Victor Stinnereb5657a2011-09-30 01:44:27 +0200922 return win32_error_object(func, uni);
Victor Stinner8c62be82010-05-06 00:08:46 +0000923 Py_INCREF(Py_None);
924 return Py_None;
925 }
Victor Stinnereb5657a2011-09-30 01:44:27 +0200926 PyErr_Clear();
927
Victor Stinner8c62be82010-05-06 00:08:46 +0000928 if (!PyArg_ParseTuple(args, format, &ansi))
929 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100930 if (win32_warn_bytes_api())
931 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000932 Py_BEGIN_ALLOW_THREADS
933 result = funcA(ansi);
934 Py_END_ALLOW_THREADS
935 if (!result)
936 return win32_error(func, ansi);
937 Py_INCREF(Py_None);
938 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000939
940}
941
942/* This is a reimplementation of the C library's chdir function,
943 but one that produces Win32 errors instead of DOS error codes.
944 chdir is essentially a wrapper around SetCurrentDirectory; however,
945 it also needs to set "magic" environment variables indicating
946 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000947static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948win32_chdir(LPCSTR path)
949{
Victor Stinner8c62be82010-05-06 00:08:46 +0000950 char new_path[MAX_PATH+1];
951 int result;
952 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953
Victor Stinner8c62be82010-05-06 00:08:46 +0000954 if(!SetCurrentDirectoryA(path))
955 return FALSE;
956 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
957 if (!result)
958 return FALSE;
959 /* In the ANSI API, there should not be any paths longer
960 than MAX_PATH. */
961 assert(result <= MAX_PATH+1);
962 if (strncmp(new_path, "\\\\", 2) == 0 ||
963 strncmp(new_path, "//", 2) == 0)
964 /* UNC path, nothing to do. */
965 return TRUE;
966 env[1] = new_path[0];
967 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968}
969
970/* The Unicode version differs from the ANSI version
971 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000972static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000973win32_wchdir(LPCWSTR path)
974{
Victor Stinner8c62be82010-05-06 00:08:46 +0000975 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
976 int result;
977 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978
Victor Stinner8c62be82010-05-06 00:08:46 +0000979 if(!SetCurrentDirectoryW(path))
980 return FALSE;
981 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
982 if (!result)
983 return FALSE;
984 if (result > MAX_PATH+1) {
985 new_path = malloc(result * sizeof(wchar_t));
986 if (!new_path) {
987 SetLastError(ERROR_OUTOFMEMORY);
988 return FALSE;
989 }
990 result = GetCurrentDirectoryW(result, new_path);
991 if (!result) {
992 free(new_path);
993 return FALSE;
994 }
995 }
996 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
997 wcsncmp(new_path, L"//", 2) == 0)
998 /* UNC path, nothing to do. */
999 return TRUE;
1000 env[1] = new_path[0];
1001 result = SetEnvironmentVariableW(env, new_path);
1002 if (new_path != _new_path)
1003 free(new_path);
1004 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005}
1006#endif
1007
Martin v. Löwis14694662006-02-03 12:54:16 +00001008#ifdef MS_WINDOWS
1009/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1010 - time stamps are restricted to second resolution
1011 - file modification times suffer from forth-and-back conversions between
1012 UTC and local time
1013 Therefore, we implement our own stat, based on the Win32 API directly.
1014*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001015#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001016
1017struct win32_stat{
1018 int st_dev;
1019 __int64 st_ino;
1020 unsigned short st_mode;
1021 int st_nlink;
1022 int st_uid;
1023 int st_gid;
1024 int st_rdev;
1025 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001026 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001027 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001028 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001029 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001030 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001031 int st_ctime_nsec;
1032};
1033
1034static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1035
1036static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001037FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001038{
Victor Stinner8c62be82010-05-06 00:08:46 +00001039 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1040 /* Cannot simply cast and dereference in_ptr,
1041 since it might not be aligned properly */
1042 __int64 in;
1043 memcpy(&in, in_ptr, sizeof(in));
1044 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001045 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001046}
1047
Thomas Wouters477c8d52006-05-27 19:21:47 +00001048static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001049time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001050{
Victor Stinner8c62be82010-05-06 00:08:46 +00001051 /* XXX endianness */
1052 __int64 out;
1053 out = time_in + secs_between_epochs;
1054 out = out * 10000000 + nsec_in / 100;
1055 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056}
1057
Martin v. Löwis14694662006-02-03 12:54:16 +00001058/* Below, we *know* that ugo+r is 0444 */
1059#if _S_IREAD != 0400
1060#error Unsupported C library
1061#endif
1062static int
1063attributes_to_mode(DWORD attr)
1064{
Victor Stinner8c62be82010-05-06 00:08:46 +00001065 int m = 0;
1066 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1067 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1068 else
1069 m |= _S_IFREG;
1070 if (attr & FILE_ATTRIBUTE_READONLY)
1071 m |= 0444;
1072 else
1073 m |= 0666;
1074 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001075}
1076
1077static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001078attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001079{
Victor Stinner8c62be82010-05-06 00:08:46 +00001080 memset(result, 0, sizeof(*result));
1081 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1082 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1083 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1084 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1085 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001086 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001087 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001088 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1089 /* first clear the S_IFMT bits */
1090 result->st_mode ^= (result->st_mode & 0170000);
1091 /* now set the bits that make this a symlink */
1092 result->st_mode |= 0120000;
1093 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001094
Victor Stinner8c62be82010-05-06 00:08:46 +00001095 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001096}
1097
Guido van Rossumd8faa362007-04-27 19:54:29 +00001098static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001099attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001100{
Victor Stinner8c62be82010-05-06 00:08:46 +00001101 HANDLE hFindFile;
1102 WIN32_FIND_DATAA FileData;
1103 hFindFile = FindFirstFileA(pszFile, &FileData);
1104 if (hFindFile == INVALID_HANDLE_VALUE)
1105 return FALSE;
1106 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001107 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001108 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001109 info->dwFileAttributes = FileData.dwFileAttributes;
1110 info->ftCreationTime = FileData.ftCreationTime;
1111 info->ftLastAccessTime = FileData.ftLastAccessTime;
1112 info->ftLastWriteTime = FileData.ftLastWriteTime;
1113 info->nFileSizeHigh = FileData.nFileSizeHigh;
1114 info->nFileSizeLow = FileData.nFileSizeLow;
1115/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001116 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1117 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001118 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001119}
1120
1121static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001122attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001123{
Victor Stinner8c62be82010-05-06 00:08:46 +00001124 HANDLE hFindFile;
1125 WIN32_FIND_DATAW FileData;
1126 hFindFile = FindFirstFileW(pszFile, &FileData);
1127 if (hFindFile == INVALID_HANDLE_VALUE)
1128 return FALSE;
1129 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001130 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001132 info->dwFileAttributes = FileData.dwFileAttributes;
1133 info->ftCreationTime = FileData.ftCreationTime;
1134 info->ftLastAccessTime = FileData.ftLastAccessTime;
1135 info->ftLastWriteTime = FileData.ftLastWriteTime;
1136 info->nFileSizeHigh = FileData.nFileSizeHigh;
1137 info->nFileSizeLow = FileData.nFileSizeLow;
1138/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001139 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1140 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001141 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001142}
1143
Brian Curtind25aef52011-06-13 15:16:04 -05001144/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1145static int has_GetFinalPathNameByHandle = 0;
Brian Curtind25aef52011-06-13 15:16:04 -05001146static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1147 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001148static int
Brian Curtind25aef52011-06-13 15:16:04 -05001149check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001150{
Brian Curtind25aef52011-06-13 15:16:04 -05001151 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001152 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1153 DWORD);
1154
Brian Curtind25aef52011-06-13 15:16:04 -05001155 /* only recheck */
1156 if (!has_GetFinalPathNameByHandle)
1157 {
1158 hKernel32 = GetModuleHandle("KERNEL32");
1159 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1160 "GetFinalPathNameByHandleA");
1161 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1162 "GetFinalPathNameByHandleW");
1163 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1164 Py_GetFinalPathNameByHandleW;
1165 }
1166 return has_GetFinalPathNameByHandle;
1167}
1168
1169static BOOL
1170get_target_path(HANDLE hdl, wchar_t **target_path)
1171{
1172 int buf_size, result_length;
1173 wchar_t *buf;
1174
1175 /* We have a good handle to the target, use it to determine
1176 the target path name (then we'll call lstat on it). */
1177 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1178 VOLUME_NAME_DOS);
1179 if(!buf_size)
1180 return FALSE;
1181
1182 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001183 if (!buf) {
1184 SetLastError(ERROR_OUTOFMEMORY);
1185 return FALSE;
1186 }
1187
Brian Curtind25aef52011-06-13 15:16:04 -05001188 result_length = Py_GetFinalPathNameByHandleW(hdl,
1189 buf, buf_size, VOLUME_NAME_DOS);
1190
1191 if(!result_length) {
1192 free(buf);
1193 return FALSE;
1194 }
1195
1196 if(!CloseHandle(hdl)) {
1197 free(buf);
1198 return FALSE;
1199 }
1200
1201 buf[result_length] = 0;
1202
1203 *target_path = buf;
1204 return TRUE;
1205}
1206
1207static int
1208win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1209 BOOL traverse);
1210static int
1211win32_xstat_impl(const char *path, struct win32_stat *result,
1212 BOOL traverse)
1213{
Victor Stinner26de69d2011-06-17 15:15:38 +02001214 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001215 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001216 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001217 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001218 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001219 const char *dot;
1220
Brian Curtind25aef52011-06-13 15:16:04 -05001221 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001222 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1223 traverse reparse point. */
1224 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001225 }
1226
Brian Curtinf5e76d02010-11-24 13:14:05 +00001227 hFile = CreateFileA(
1228 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001229 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001230 0, /* share mode */
1231 NULL, /* security attributes */
1232 OPEN_EXISTING,
1233 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001234 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1235 Because of this, calls like GetFinalPathNameByHandle will return
1236 the symlink path agin and not the actual final path. */
1237 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1238 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001239 NULL);
1240
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001241 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001242 /* Either the target doesn't exist, or we don't have access to
1243 get a handle to it. If the former, we need to return an error.
1244 If the latter, we can use attributes_from_dir. */
1245 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001246 return -1;
1247 /* Could not get attributes on open file. Fall back to
1248 reading the directory. */
1249 if (!attributes_from_dir(path, &info, &reparse_tag))
1250 /* Very strange. This should not fail now */
1251 return -1;
1252 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1253 if (traverse) {
1254 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001255 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001256 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001257 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001258 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001259 } else {
1260 if (!GetFileInformationByHandle(hFile, &info)) {
1261 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001262 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001263 }
1264 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001265 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1266 return -1;
1267
1268 /* Close the outer open file handle now that we're about to
1269 reopen it with different flags. */
1270 if (!CloseHandle(hFile))
1271 return -1;
1272
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001273 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001274 /* In order to call GetFinalPathNameByHandle we need to open
1275 the file without the reparse handling flag set. */
1276 hFile2 = CreateFileA(
1277 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1278 NULL, OPEN_EXISTING,
1279 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1280 NULL);
1281 if (hFile2 == INVALID_HANDLE_VALUE)
1282 return -1;
1283
1284 if (!get_target_path(hFile2, &target_path))
1285 return -1;
1286
1287 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001288 free(target_path);
1289 return code;
1290 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001291 } else
1292 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001293 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001294 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001295
1296 /* Set S_IEXEC if it is an .exe, .bat, ... */
1297 dot = strrchr(path, '.');
1298 if (dot) {
1299 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1300 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1301 result->st_mode |= 0111;
1302 }
1303 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001304}
1305
1306static int
Brian Curtind25aef52011-06-13 15:16:04 -05001307win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1308 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001309{
1310 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001311 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001312 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001313 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001314 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001315 const wchar_t *dot;
1316
Brian Curtind25aef52011-06-13 15:16:04 -05001317 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001318 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1319 traverse reparse point. */
1320 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001321 }
1322
Brian Curtinf5e76d02010-11-24 13:14:05 +00001323 hFile = CreateFileW(
1324 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001325 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001326 0, /* share mode */
1327 NULL, /* security attributes */
1328 OPEN_EXISTING,
1329 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001330 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1331 Because of this, calls like GetFinalPathNameByHandle will return
1332 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001333 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001334 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001335 NULL);
1336
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001337 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001338 /* Either the target doesn't exist, or we don't have access to
1339 get a handle to it. If the former, we need to return an error.
1340 If the latter, we can use attributes_from_dir. */
1341 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001342 return -1;
1343 /* Could not get attributes on open file. Fall back to
1344 reading the directory. */
1345 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1346 /* Very strange. This should not fail now */
1347 return -1;
1348 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1349 if (traverse) {
1350 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001351 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001352 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001353 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001354 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001355 } else {
1356 if (!GetFileInformationByHandle(hFile, &info)) {
1357 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001358 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001359 }
1360 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001361 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1362 return -1;
1363
1364 /* Close the outer open file handle now that we're about to
1365 reopen it with different flags. */
1366 if (!CloseHandle(hFile))
1367 return -1;
1368
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001369 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001370 /* In order to call GetFinalPathNameByHandle we need to open
1371 the file without the reparse handling flag set. */
1372 hFile2 = CreateFileW(
1373 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1374 NULL, OPEN_EXISTING,
1375 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1376 NULL);
1377 if (hFile2 == INVALID_HANDLE_VALUE)
1378 return -1;
1379
1380 if (!get_target_path(hFile2, &target_path))
1381 return -1;
1382
1383 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001384 free(target_path);
1385 return code;
1386 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001387 } else
1388 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001389 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001390 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001391
1392 /* Set S_IEXEC if it is an .exe, .bat, ... */
1393 dot = wcsrchr(path, '.');
1394 if (dot) {
1395 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1396 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1397 result->st_mode |= 0111;
1398 }
1399 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001400}
1401
1402static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001403win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001405 /* Protocol violation: we explicitly clear errno, instead of
1406 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001407 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001408 errno = 0;
1409 return code;
1410}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001411
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001412static int
1413win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1414{
1415 /* Protocol violation: we explicitly clear errno, instead of
1416 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001417 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001418 errno = 0;
1419 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001420}
Brian Curtind25aef52011-06-13 15:16:04 -05001421/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001422
1423 In Posix, stat automatically traverses symlinks and returns the stat
1424 structure for the target. In Windows, the equivalent GetFileAttributes by
1425 default does not traverse symlinks and instead returns attributes for
1426 the symlink.
1427
1428 Therefore, win32_lstat will get the attributes traditionally, and
1429 win32_stat will first explicitly resolve the symlink target and then will
1430 call win32_lstat on that result.
1431
Ezio Melotti4969f702011-03-15 05:59:46 +02001432 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001433
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001434static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001435win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001436{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001437 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001438}
1439
Victor Stinner8c62be82010-05-06 00:08:46 +00001440static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001441win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001442{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001443 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001444}
1445
1446static int
1447win32_stat(const char* path, struct win32_stat *result)
1448{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001449 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001450}
1451
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001452static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001453win32_stat_w(const wchar_t* path, struct win32_stat *result)
1454{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001455 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001456}
1457
1458static int
1459win32_fstat(int file_number, struct win32_stat *result)
1460{
Victor Stinner8c62be82010-05-06 00:08:46 +00001461 BY_HANDLE_FILE_INFORMATION info;
1462 HANDLE h;
1463 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001464
Victor Stinner8c62be82010-05-06 00:08:46 +00001465 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001466
Victor Stinner8c62be82010-05-06 00:08:46 +00001467 /* Protocol violation: we explicitly clear errno, instead of
1468 setting it to a POSIX error. Callers should use GetLastError. */
1469 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001470
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 if (h == INVALID_HANDLE_VALUE) {
1472 /* This is really a C library error (invalid file handle).
1473 We set the Win32 error to the closes one matching. */
1474 SetLastError(ERROR_INVALID_HANDLE);
1475 return -1;
1476 }
1477 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001478
Victor Stinner8c62be82010-05-06 00:08:46 +00001479 type = GetFileType(h);
1480 if (type == FILE_TYPE_UNKNOWN) {
1481 DWORD error = GetLastError();
1482 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001483 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001484 }
1485 /* else: valid but unknown file */
1486 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001487
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 if (type != FILE_TYPE_DISK) {
1489 if (type == FILE_TYPE_CHAR)
1490 result->st_mode = _S_IFCHR;
1491 else if (type == FILE_TYPE_PIPE)
1492 result->st_mode = _S_IFIFO;
1493 return 0;
1494 }
1495
1496 if (!GetFileInformationByHandle(h, &info)) {
1497 return -1;
1498 }
1499
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001500 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1503 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001504}
1505
1506#endif /* MS_WINDOWS */
1507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001508PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001509"stat_result: Result from stat or lstat.\n\n\
1510This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001511 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001512or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1513\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001514Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1515or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001516\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001517See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001518
1519static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001520 {"st_mode", "protection bits"},
1521 {"st_ino", "inode"},
1522 {"st_dev", "device"},
1523 {"st_nlink", "number of hard links"},
1524 {"st_uid", "user ID of owner"},
1525 {"st_gid", "group ID of owner"},
1526 {"st_size", "total size, in bytes"},
1527 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1528 {NULL, "integer time of last access"},
1529 {NULL, "integer time of last modification"},
1530 {NULL, "integer time of last change"},
1531 {"st_atime", "time of last access"},
1532 {"st_mtime", "time of last modification"},
1533 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001534#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001536#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001537#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001538 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001539#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001540#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001541 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001542#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001543#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001544 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001545#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001546#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001547 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001548#endif
1549#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001550 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001551#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001552 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001553};
1554
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001555#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001556#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001557#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001558#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001559#endif
1560
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001561#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001562#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1563#else
1564#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1565#endif
1566
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001567#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001568#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1569#else
1570#define ST_RDEV_IDX ST_BLOCKS_IDX
1571#endif
1572
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001573#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1574#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1575#else
1576#define ST_FLAGS_IDX ST_RDEV_IDX
1577#endif
1578
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001579#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001580#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001581#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001582#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001583#endif
1584
1585#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1586#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1587#else
1588#define ST_BIRTHTIME_IDX ST_GEN_IDX
1589#endif
1590
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001591static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001592 "stat_result", /* name */
1593 stat_result__doc__, /* doc */
1594 stat_result_fields,
1595 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001596};
1597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001599"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1600This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001601 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001602or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001603\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001605
1606static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001607 {"f_bsize", },
1608 {"f_frsize", },
1609 {"f_blocks", },
1610 {"f_bfree", },
1611 {"f_bavail", },
1612 {"f_files", },
1613 {"f_ffree", },
1614 {"f_favail", },
1615 {"f_flag", },
1616 {"f_namemax",},
1617 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001618};
1619
1620static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 "statvfs_result", /* name */
1622 statvfs_result__doc__, /* doc */
1623 statvfs_result_fields,
1624 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001625};
1626
Ross Lagerwall7807c352011-03-17 20:20:30 +02001627#if defined(HAVE_WAITID) && !defined(__APPLE__)
1628PyDoc_STRVAR(waitid_result__doc__,
1629"waitid_result: Result from waitid.\n\n\
1630This object may be accessed either as a tuple of\n\
1631 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1632or via the attributes si_pid, si_uid, and so on.\n\
1633\n\
1634See os.waitid for more information.");
1635
1636static PyStructSequence_Field waitid_result_fields[] = {
1637 {"si_pid", },
1638 {"si_uid", },
1639 {"si_signo", },
1640 {"si_status", },
1641 {"si_code", },
1642 {0}
1643};
1644
1645static PyStructSequence_Desc waitid_result_desc = {
1646 "waitid_result", /* name */
1647 waitid_result__doc__, /* doc */
1648 waitid_result_fields,
1649 5
1650};
1651static PyTypeObject WaitidResultType;
1652#endif
1653
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001654static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001655static PyTypeObject StatResultType;
1656static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001657#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001658static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001659#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001660static newfunc structseq_new;
1661
1662static PyObject *
1663statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1664{
Victor Stinner8c62be82010-05-06 00:08:46 +00001665 PyStructSequence *result;
1666 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001667
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 result = (PyStructSequence*)structseq_new(type, args, kwds);
1669 if (!result)
1670 return NULL;
1671 /* If we have been initialized from a tuple,
1672 st_?time might be set to None. Initialize it
1673 from the int slots. */
1674 for (i = 7; i <= 9; i++) {
1675 if (result->ob_item[i+3] == Py_None) {
1676 Py_DECREF(Py_None);
1677 Py_INCREF(result->ob_item[i]);
1678 result->ob_item[i+3] = result->ob_item[i];
1679 }
1680 }
1681 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001682}
1683
1684
1685
1686/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001687static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001688
1689PyDoc_STRVAR(stat_float_times__doc__,
1690"stat_float_times([newval]) -> oldval\n\n\
1691Determine whether os.[lf]stat represents time stamps as float objects.\n\
1692If newval is True, future calls to stat() return floats, if it is False,\n\
1693future calls return ints. \n\
1694If newval is omitted, return the current setting.\n");
1695
1696static PyObject*
1697stat_float_times(PyObject* self, PyObject *args)
1698{
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 int newval = -1;
1700 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1701 return NULL;
1702 if (newval == -1)
1703 /* Return old value */
1704 return PyBool_FromLong(_stat_float_times);
1705 _stat_float_times = newval;
1706 Py_INCREF(Py_None);
1707 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001708}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001709
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001710static void
1711fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1712{
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001714#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001715 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001716#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001717 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001718#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001719 if (!ival)
1720 return;
1721 if (_stat_float_times) {
1722 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1723 } else {
1724 fval = ival;
1725 Py_INCREF(fval);
1726 }
1727 PyStructSequence_SET_ITEM(v, index, ival);
1728 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001729}
1730
Tim Peters5aa91602002-01-30 05:46:57 +00001731/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001732 (used by posix_stat() and posix_fstat()) */
1733static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001734_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001735{
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 unsigned long ansec, mnsec, cnsec;
1737 PyObject *v = PyStructSequence_New(&StatResultType);
1738 if (v == NULL)
1739 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001740
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001742#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001743 PyStructSequence_SET_ITEM(v, 1,
1744 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001745#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001746 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001747#endif
1748#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001749 PyStructSequence_SET_ITEM(v, 2,
1750 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001751#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001753#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001754 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1755 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1756 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001757#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 PyStructSequence_SET_ITEM(v, 6,
1759 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001760#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001761 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001762#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001763
Martin v. Löwis14694662006-02-03 12:54:16 +00001764#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001765 ansec = st->st_atim.tv_nsec;
1766 mnsec = st->st_mtim.tv_nsec;
1767 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001768#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 ansec = st->st_atimespec.tv_nsec;
1770 mnsec = st->st_mtimespec.tv_nsec;
1771 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001772#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001773 ansec = st->st_atime_nsec;
1774 mnsec = st->st_mtime_nsec;
1775 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001776#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001777 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001778#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001779 fill_time(v, 7, st->st_atime, ansec);
1780 fill_time(v, 8, st->st_mtime, mnsec);
1781 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001782
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001783#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1785 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001786#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001787#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1789 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001790#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001791#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1793 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001794#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001795#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001796 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1797 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001798#endif
1799#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001800 {
1801 PyObject *val;
1802 unsigned long bsec,bnsec;
1803 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001804#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001805 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001806#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001807 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001808#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 if (_stat_float_times) {
1810 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1811 } else {
1812 val = PyLong_FromLong((long)bsec);
1813 }
1814 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1815 val);
1816 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001817#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001818#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001819 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1820 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001821#endif
Fred Drake699f3522000-06-29 21:12:41 +00001822
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 if (PyErr_Occurred()) {
1824 Py_DECREF(v);
1825 return NULL;
1826 }
Fred Drake699f3522000-06-29 21:12:41 +00001827
Victor Stinner8c62be82010-05-06 00:08:46 +00001828 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001829}
1830
Barry Warsaw53699e91996-12-10 23:23:01 +00001831static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001832posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001834#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001836#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001837 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001838#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 char *wformat,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001840 int (*wstatfunc)(const wchar_t *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841{
Victor Stinner8c62be82010-05-06 00:08:46 +00001842 STRUCT_STAT st;
1843 PyObject *opath;
1844 char *path;
1845 int res;
1846 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001847
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001848#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001849 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 if (PyArg_ParseTuple(args, wformat, &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001851 wchar_t *wpath = PyUnicode_AsUnicode(po);
1852 if (wpath == NULL)
1853 return NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00001854
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00001856 res = wstatfunc(wpath, &st);
1857 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001858
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 if (res != 0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001860 return win32_error_object("stat", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 return _pystat_fromstructstat(&st);
1862 }
1863 /* Drop the argument parsing error as narrow strings
1864 are also valid. */
1865 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001866#endif
1867
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 if (!PyArg_ParseTuple(args, format,
1869 PyUnicode_FSConverter, &opath))
1870 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001871#ifdef MS_WINDOWS
1872 if (win32_warn_bytes_api()) {
1873 Py_DECREF(opath);
1874 return NULL;
1875 }
1876#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001877 path = PyBytes_AsString(opath);
1878 Py_BEGIN_ALLOW_THREADS
1879 res = (*statfunc)(path, &st);
1880 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001881
Victor Stinner8c62be82010-05-06 00:08:46 +00001882 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001883#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001884 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001885#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001886 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001887#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001888 }
1889 else
1890 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001891
Victor Stinner8c62be82010-05-06 00:08:46 +00001892 Py_DECREF(opath);
1893 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001894}
1895
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001896/* POSIX methods */
1897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001899"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001900Use the real uid/gid to test for access to a path. Note that most\n\
1901operations will use the effective uid/gid, therefore this routine can\n\
1902be used in a suid/sgid environment to test if the invoking user has the\n\
1903specified access to the path. The mode argument can be F_OK to test\n\
1904existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001905
1906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001907posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001908{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001909 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00001910 int mode;
1911
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001912#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001913 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02001914 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001915 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001916 wchar_t* wpath = PyUnicode_AsUnicode(po);
1917 if (wpath == NULL)
1918 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001919 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001920 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00001921 Py_END_ALLOW_THREADS
1922 goto finish;
1923 }
1924 /* Drop the argument parsing error as narrow strings
1925 are also valid. */
1926 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001927 if (!PyArg_ParseTuple(args, "yi:access", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00001928 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001929 if (win32_warn_bytes_api())
1930 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 Py_BEGIN_ALLOW_THREADS
1932 attr = GetFileAttributesA(path);
1933 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001934finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001935 if (attr == 0xFFFFFFFF)
1936 /* File does not exist, or cannot read attributes */
1937 return PyBool_FromLong(0);
1938 /* Access is possible if either write access wasn't requested, or
1939 the file isn't read-only, or if it's a directory, as there are
1940 no read-only directories on Windows. */
1941 return PyBool_FromLong(!(mode & 2)
1942 || !(attr & FILE_ATTRIBUTE_READONLY)
1943 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001944#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001945 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 int res;
1947 if (!PyArg_ParseTuple(args, "O&i:access",
1948 PyUnicode_FSConverter, &opath, &mode))
1949 return NULL;
1950 path = PyBytes_AsString(opath);
1951 Py_BEGIN_ALLOW_THREADS
1952 res = access(path, mode);
1953 Py_END_ALLOW_THREADS
1954 Py_DECREF(opath);
1955 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001956#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001957}
1958
Guido van Rossumd371ff11999-01-25 16:12:23 +00001959#ifndef F_OK
1960#define F_OK 0
1961#endif
1962#ifndef R_OK
1963#define R_OK 4
1964#endif
1965#ifndef W_OK
1966#define W_OK 2
1967#endif
1968#ifndef X_OK
1969#define X_OK 1
1970#endif
1971
1972#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001973PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001974"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001975Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001976
1977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001978posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001979{
Victor Stinner8c62be82010-05-06 00:08:46 +00001980 int id;
1981 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001982
Victor Stinner8c62be82010-05-06 00:08:46 +00001983 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1984 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001985
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001986#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001987 /* file descriptor 0 only, the default input device (stdin) */
1988 if (id == 0) {
1989 ret = ttyname();
1990 }
1991 else {
1992 ret = NULL;
1993 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001994#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001995 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001996#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001997 if (ret == NULL)
1998 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001999 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002000}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002001#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002002
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002003#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002005"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002006Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002007
2008static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002009posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002010{
Victor Stinner8c62be82010-05-06 00:08:46 +00002011 char *ret;
2012 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002013
Greg Wardb48bc172000-03-01 21:51:56 +00002014#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002016#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002017 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002018#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002019 if (ret == NULL)
2020 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002021 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002022}
2023#endif
2024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002026"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002027Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002028
Barry Warsaw53699e91996-12-10 23:23:01 +00002029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002030posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002031{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002032#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002033 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002034#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00002036#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002037 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002038#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002039 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002040#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002041}
2042
Fred Drake4d1e64b2002-04-15 19:40:07 +00002043#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002044PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002045"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002046Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002047opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002048
2049static PyObject *
2050posix_fchdir(PyObject *self, PyObject *fdobj)
2051{
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002053}
2054#endif /* HAVE_FCHDIR */
2055
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002058"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060
Barry Warsaw53699e91996-12-10 23:23:01 +00002061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002062posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002063{
Victor Stinner8c62be82010-05-06 00:08:46 +00002064 PyObject *opath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002065 const char *path = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 int i;
2067 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002068#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002069 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002070 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00002071 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002072 wchar_t *wpath = PyUnicode_AsUnicode(po);
2073 if (wpath == NULL)
2074 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002075 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002076 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002077 if (attr != 0xFFFFFFFF) {
2078 if (i & _S_IWRITE)
2079 attr &= ~FILE_ATTRIBUTE_READONLY;
2080 else
2081 attr |= FILE_ATTRIBUTE_READONLY;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002082 res = SetFileAttributesW(wpath, attr);
Victor Stinner8c62be82010-05-06 00:08:46 +00002083 }
2084 else
2085 res = 0;
2086 Py_END_ALLOW_THREADS
2087 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02002088 return win32_error_object("chmod", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002089 Py_INCREF(Py_None);
2090 return Py_None;
2091 }
2092 /* Drop the argument parsing error as narrow strings
2093 are also valid. */
2094 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002095
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002096 if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i))
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002098 if (win32_warn_bytes_api())
2099 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002100 Py_BEGIN_ALLOW_THREADS
2101 attr = GetFileAttributesA(path);
2102 if (attr != 0xFFFFFFFF) {
2103 if (i & _S_IWRITE)
2104 attr &= ~FILE_ATTRIBUTE_READONLY;
2105 else
2106 attr |= FILE_ATTRIBUTE_READONLY;
2107 res = SetFileAttributesA(path, attr);
2108 }
2109 else
2110 res = 0;
2111 Py_END_ALLOW_THREADS
2112 if (!res) {
2113 win32_error("chmod", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002114 return NULL;
2115 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002116 Py_INCREF(Py_None);
2117 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002118#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002119 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2120 &opath, &i))
2121 return NULL;
2122 path = PyBytes_AsString(opath);
2123 Py_BEGIN_ALLOW_THREADS
2124 res = chmod(path, i);
2125 Py_END_ALLOW_THREADS
2126 if (res < 0)
2127 return posix_error_with_allocated_filename(opath);
2128 Py_DECREF(opath);
2129 Py_INCREF(Py_None);
2130 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002131#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002132}
2133
Christian Heimes4e30a842007-11-30 22:12:06 +00002134#ifdef HAVE_FCHMOD
2135PyDoc_STRVAR(posix_fchmod__doc__,
2136"fchmod(fd, mode)\n\n\
2137Change the access permissions of the file given by file\n\
2138descriptor fd.");
2139
2140static PyObject *
2141posix_fchmod(PyObject *self, PyObject *args)
2142{
Victor Stinner8c62be82010-05-06 00:08:46 +00002143 int fd, mode, res;
2144 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2145 return NULL;
2146 Py_BEGIN_ALLOW_THREADS
2147 res = fchmod(fd, mode);
2148 Py_END_ALLOW_THREADS
2149 if (res < 0)
2150 return posix_error();
2151 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002152}
2153#endif /* HAVE_FCHMOD */
2154
2155#ifdef HAVE_LCHMOD
2156PyDoc_STRVAR(posix_lchmod__doc__,
2157"lchmod(path, mode)\n\n\
2158Change the access permissions of a file. If path is a symlink, this\n\
2159affects the link itself rather than the target.");
2160
2161static PyObject *
2162posix_lchmod(PyObject *self, PyObject *args)
2163{
Victor Stinner8c62be82010-05-06 00:08:46 +00002164 PyObject *opath;
2165 char *path;
2166 int i;
2167 int res;
2168 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2169 &opath, &i))
2170 return NULL;
2171 path = PyBytes_AsString(opath);
2172 Py_BEGIN_ALLOW_THREADS
2173 res = lchmod(path, i);
2174 Py_END_ALLOW_THREADS
2175 if (res < 0)
2176 return posix_error_with_allocated_filename(opath);
2177 Py_DECREF(opath);
2178 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002179}
2180#endif /* HAVE_LCHMOD */
2181
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002182
Thomas Wouterscf297e42007-02-23 15:07:44 +00002183#ifdef HAVE_CHFLAGS
2184PyDoc_STRVAR(posix_chflags__doc__,
2185"chflags(path, flags)\n\n\
2186Set file flags.");
2187
2188static PyObject *
2189posix_chflags(PyObject *self, PyObject *args)
2190{
Victor Stinner8c62be82010-05-06 00:08:46 +00002191 PyObject *opath;
2192 char *path;
2193 unsigned long flags;
2194 int res;
2195 if (!PyArg_ParseTuple(args, "O&k:chflags",
2196 PyUnicode_FSConverter, &opath, &flags))
2197 return NULL;
2198 path = PyBytes_AsString(opath);
2199 Py_BEGIN_ALLOW_THREADS
2200 res = chflags(path, flags);
2201 Py_END_ALLOW_THREADS
2202 if (res < 0)
2203 return posix_error_with_allocated_filename(opath);
2204 Py_DECREF(opath);
2205 Py_INCREF(Py_None);
2206 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002207}
2208#endif /* HAVE_CHFLAGS */
2209
2210#ifdef HAVE_LCHFLAGS
2211PyDoc_STRVAR(posix_lchflags__doc__,
2212"lchflags(path, flags)\n\n\
2213Set file flags.\n\
2214This function will not follow symbolic links.");
2215
2216static PyObject *
2217posix_lchflags(PyObject *self, PyObject *args)
2218{
Victor Stinner8c62be82010-05-06 00:08:46 +00002219 PyObject *opath;
2220 char *path;
2221 unsigned long flags;
2222 int res;
2223 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2224 PyUnicode_FSConverter, &opath, &flags))
2225 return NULL;
2226 path = PyBytes_AsString(opath);
2227 Py_BEGIN_ALLOW_THREADS
2228 res = lchflags(path, flags);
2229 Py_END_ALLOW_THREADS
2230 if (res < 0)
2231 return posix_error_with_allocated_filename(opath);
2232 Py_DECREF(opath);
2233 Py_INCREF(Py_None);
2234 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002235}
2236#endif /* HAVE_LCHFLAGS */
2237
Martin v. Löwis244edc82001-10-04 22:44:26 +00002238#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002239PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002240"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002241Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002242
2243static PyObject *
2244posix_chroot(PyObject *self, PyObject *args)
2245{
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002247}
2248#endif
2249
Guido van Rossum21142a01999-01-08 21:05:37 +00002250#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002251PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002252"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002254
2255static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002256posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002257{
Stefan Krah0e803b32010-11-26 16:16:47 +00002258 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002259}
2260#endif /* HAVE_FSYNC */
2261
Ross Lagerwall7807c352011-03-17 20:20:30 +02002262#ifdef HAVE_SYNC
2263PyDoc_STRVAR(posix_sync__doc__,
2264"sync()\n\n\
2265Force write of everything to disk.");
2266
2267static PyObject *
2268posix_sync(PyObject *self, PyObject *noargs)
2269{
2270 Py_BEGIN_ALLOW_THREADS
2271 sync();
2272 Py_END_ALLOW_THREADS
2273 Py_RETURN_NONE;
2274}
2275#endif
2276
Guido van Rossum21142a01999-01-08 21:05:37 +00002277#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002278
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002279#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002280extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2281#endif
2282
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002283PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002284"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002285force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002286 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002287
2288static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002289posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002290{
Stefan Krah0e803b32010-11-26 16:16:47 +00002291 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002292}
2293#endif /* HAVE_FDATASYNC */
2294
2295
Fredrik Lundh10723342000-07-10 16:38:09 +00002296#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002297PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002298"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002299Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002300
Barry Warsaw53699e91996-12-10 23:23:01 +00002301static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002302posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002303{
Victor Stinner8c62be82010-05-06 00:08:46 +00002304 PyObject *opath;
2305 char *path;
2306 long uid, gid;
2307 int res;
2308 if (!PyArg_ParseTuple(args, "O&ll:chown",
2309 PyUnicode_FSConverter, &opath,
2310 &uid, &gid))
2311 return NULL;
2312 path = PyBytes_AsString(opath);
2313 Py_BEGIN_ALLOW_THREADS
2314 res = chown(path, (uid_t) uid, (gid_t) gid);
2315 Py_END_ALLOW_THREADS
2316 if (res < 0)
2317 return posix_error_with_allocated_filename(opath);
2318 Py_DECREF(opath);
2319 Py_INCREF(Py_None);
2320 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002321}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002322#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002323
Christian Heimes4e30a842007-11-30 22:12:06 +00002324#ifdef HAVE_FCHOWN
2325PyDoc_STRVAR(posix_fchown__doc__,
2326"fchown(fd, uid, gid)\n\n\
2327Change the owner and group id of the file given by file descriptor\n\
2328fd to the numeric uid and gid.");
2329
2330static PyObject *
2331posix_fchown(PyObject *self, PyObject *args)
2332{
Victor Stinner8c62be82010-05-06 00:08:46 +00002333 int fd;
2334 long uid, gid;
2335 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002336 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002337 return NULL;
2338 Py_BEGIN_ALLOW_THREADS
2339 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2340 Py_END_ALLOW_THREADS
2341 if (res < 0)
2342 return posix_error();
2343 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002344}
2345#endif /* HAVE_FCHOWN */
2346
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002347#ifdef HAVE_LCHOWN
2348PyDoc_STRVAR(posix_lchown__doc__,
2349"lchown(path, uid, gid)\n\n\
2350Change the owner and group id of path to the numeric uid and gid.\n\
2351This function will not follow symbolic links.");
2352
2353static PyObject *
2354posix_lchown(PyObject *self, PyObject *args)
2355{
Victor Stinner8c62be82010-05-06 00:08:46 +00002356 PyObject *opath;
2357 char *path;
2358 long uid, gid;
2359 int res;
2360 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2361 PyUnicode_FSConverter, &opath,
2362 &uid, &gid))
2363 return NULL;
2364 path = PyBytes_AsString(opath);
2365 Py_BEGIN_ALLOW_THREADS
2366 res = lchown(path, (uid_t) uid, (gid_t) gid);
2367 Py_END_ALLOW_THREADS
2368 if (res < 0)
2369 return posix_error_with_allocated_filename(opath);
2370 Py_DECREF(opath);
2371 Py_INCREF(Py_None);
2372 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002373}
2374#endif /* HAVE_LCHOWN */
2375
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002376
Guido van Rossum36bc6801995-06-14 22:54:23 +00002377#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002378static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002379posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002380{
Victor Stinner8c62be82010-05-06 00:08:46 +00002381 char buf[1026];
2382 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002383
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002384#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002385 if (!use_bytes) {
2386 wchar_t wbuf[1026];
2387 wchar_t *wbuf2 = wbuf;
2388 PyObject *resobj;
2389 DWORD len;
2390 Py_BEGIN_ALLOW_THREADS
2391 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2392 /* If the buffer is large enough, len does not include the
2393 terminating \0. If the buffer is too small, len includes
2394 the space needed for the terminator. */
2395 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2396 wbuf2 = malloc(len * sizeof(wchar_t));
2397 if (wbuf2)
2398 len = GetCurrentDirectoryW(len, wbuf2);
2399 }
2400 Py_END_ALLOW_THREADS
2401 if (!wbuf2) {
2402 PyErr_NoMemory();
2403 return NULL;
2404 }
2405 if (!len) {
2406 if (wbuf2 != wbuf) free(wbuf2);
2407 return win32_error("getcwdu", NULL);
2408 }
2409 resobj = PyUnicode_FromWideChar(wbuf2, len);
2410 if (wbuf2 != wbuf) free(wbuf2);
2411 return resobj;
2412 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01002413
2414 if (win32_warn_bytes_api())
2415 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002416#endif
2417
Victor Stinner8c62be82010-05-06 00:08:46 +00002418 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002419#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002420 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002421#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002422 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002423#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002424 Py_END_ALLOW_THREADS
2425 if (res == NULL)
2426 return posix_error();
2427 if (use_bytes)
2428 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002429 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002430}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002431
2432PyDoc_STRVAR(posix_getcwd__doc__,
2433"getcwd() -> path\n\n\
2434Return a unicode string representing the current working directory.");
2435
2436static PyObject *
2437posix_getcwd_unicode(PyObject *self)
2438{
2439 return posix_getcwd(0);
2440}
2441
2442PyDoc_STRVAR(posix_getcwdb__doc__,
2443"getcwdb() -> path\n\n\
2444Return a bytes string representing the current working directory.");
2445
2446static PyObject *
2447posix_getcwd_bytes(PyObject *self)
2448{
2449 return posix_getcwd(1);
2450}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002451#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002453
Guido van Rossumb6775db1994-08-01 11:34:53 +00002454#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002455PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002456"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002457Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002458
Barry Warsaw53699e91996-12-10 23:23:01 +00002459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002460posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002461{
Victor Stinner8c62be82010-05-06 00:08:46 +00002462 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002463}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002464#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002465
Brian Curtin1b9df392010-11-24 20:24:31 +00002466#ifdef MS_WINDOWS
2467PyDoc_STRVAR(win32_link__doc__,
2468"link(src, dst)\n\n\
2469Create a hard link to a file.");
2470
2471static PyObject *
2472win32_link(PyObject *self, PyObject *args)
2473{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002474 PyObject *src, *dst;
2475 BOOL ok;
Brian Curtin1b9df392010-11-24 20:24:31 +00002476
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002477 if (PyArg_ParseTuple(args, "UU:link", &src, &dst))
Victor Stinnereb5657a2011-09-30 01:44:27 +02002478 {
2479 wchar_t *wsrc, *wdst;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002480
2481 wsrc = PyUnicode_AsUnicode(src);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002482 if (wsrc == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002483 goto error;
2484 wdst = PyUnicode_AsUnicode(dst);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002485 if (wdst == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002486 goto error;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002487
Brian Curtinfc889c42010-11-28 23:59:46 +00002488 Py_BEGIN_ALLOW_THREADS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002489 ok = CreateHardLinkW(wdst, wsrc, NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002490 Py_END_ALLOW_THREADS
2491
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002492 if (!ok)
Brian Curtinfc889c42010-11-28 23:59:46 +00002493 return win32_error("link", NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002494 Py_RETURN_NONE;
2495 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002496 else {
2497 PyErr_Clear();
2498 if (!PyArg_ParseTuple(args, "O&O&:link",
2499 PyUnicode_FSConverter, &src,
2500 PyUnicode_FSConverter, &dst))
2501 return NULL;
Brian Curtinfc889c42010-11-28 23:59:46 +00002502
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002503 if (win32_warn_bytes_api())
2504 goto error;
Brian Curtinfc889c42010-11-28 23:59:46 +00002505
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002506 Py_BEGIN_ALLOW_THREADS
2507 ok = CreateHardLinkA(PyBytes_AS_STRING(dst),
2508 PyBytes_AS_STRING(src),
2509 NULL);
2510 Py_END_ALLOW_THREADS
2511
2512 Py_XDECREF(src);
2513 Py_XDECREF(dst);
2514
2515 if (!ok)
2516 return win32_error("link", NULL);
2517 Py_RETURN_NONE;
2518
2519 error:
2520 Py_XDECREF(src);
2521 Py_XDECREF(dst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002522 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002523 }
Brian Curtin1b9df392010-11-24 20:24:31 +00002524}
2525#endif /* MS_WINDOWS */
2526
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002528PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002529"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002530Return a list containing the names of the entries in the directory.\n\
2531\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002532 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002533\n\
2534The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002535entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002536
Barry Warsaw53699e91996-12-10 23:23:01 +00002537static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002538posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002539{
Victor Stinner8c62be82010-05-06 00:08:46 +00002540 /* XXX Should redo this putting the (now four) versions of opendir
2541 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002542#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002543
Victor Stinner8c62be82010-05-06 00:08:46 +00002544 PyObject *d, *v;
2545 HANDLE hFindFile;
2546 BOOL result;
2547 WIN32_FIND_DATA FileData;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002548 const char *path;
2549 Py_ssize_t pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002550 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2551 char *bufptr = namebuf;
2552 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002553
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002554 PyObject *po = NULL;
2555 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002556 WIN32_FIND_DATAW wFileData;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002557 wchar_t *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002558
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002559 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002560 po_wchars = L".";
2561 len = 1;
2562 } else {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02002563 po_wchars = PyUnicode_AsUnicodeAndSize(po, &len);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002564 if (po_wchars == NULL)
2565 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002566 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002567 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002568 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2569 if (!wnamebuf) {
2570 PyErr_NoMemory();
2571 return NULL;
2572 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002573 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002574 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002575 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00002576 if (wch != L'/' && wch != L'\\' && wch != L':')
2577 wnamebuf[len++] = L'\\';
2578 wcscpy(wnamebuf + len, L"*.*");
2579 }
2580 if ((d = PyList_New(0)) == NULL) {
2581 free(wnamebuf);
2582 return NULL;
2583 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002584 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002585 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002586 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002587 if (hFindFile == INVALID_HANDLE_VALUE) {
2588 int error = GetLastError();
2589 if (error == ERROR_FILE_NOT_FOUND) {
2590 free(wnamebuf);
2591 return d;
2592 }
2593 Py_DECREF(d);
2594 win32_error_unicode("FindFirstFileW", wnamebuf);
2595 free(wnamebuf);
2596 return NULL;
2597 }
2598 do {
2599 /* Skip over . and .. */
2600 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2601 wcscmp(wFileData.cFileName, L"..") != 0) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002602 v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 if (v == NULL) {
2604 Py_DECREF(d);
2605 d = NULL;
2606 break;
2607 }
2608 if (PyList_Append(d, v) != 0) {
2609 Py_DECREF(v);
2610 Py_DECREF(d);
2611 d = NULL;
2612 break;
2613 }
2614 Py_DECREF(v);
2615 }
2616 Py_BEGIN_ALLOW_THREADS
2617 result = FindNextFileW(hFindFile, &wFileData);
2618 Py_END_ALLOW_THREADS
2619 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2620 it got to the end of the directory. */
2621 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2622 Py_DECREF(d);
2623 win32_error_unicode("FindNextFileW", wnamebuf);
2624 FindClose(hFindFile);
2625 free(wnamebuf);
2626 return NULL;
2627 }
2628 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002629
Victor Stinner8c62be82010-05-06 00:08:46 +00002630 if (FindClose(hFindFile) == FALSE) {
2631 Py_DECREF(d);
2632 win32_error_unicode("FindClose", wnamebuf);
2633 free(wnamebuf);
2634 return NULL;
2635 }
2636 free(wnamebuf);
2637 return d;
2638 }
2639 /* Drop the argument parsing error as narrow strings
2640 are also valid. */
2641 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002642
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002643 if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen))
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002645 if (win32_warn_bytes_api())
2646 return NULL;
2647 if (pathlen+1 > MAX_PATH) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002648 PyErr_SetString(PyExc_ValueError, "path too long");
Victor Stinner8c62be82010-05-06 00:08:46 +00002649 return NULL;
2650 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002651 strcpy(namebuf, path);
2652 len = pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002653 if (len > 0) {
2654 char ch = namebuf[len-1];
2655 if (ch != SEP && ch != ALTSEP && ch != ':')
2656 namebuf[len++] = '/';
2657 strcpy(namebuf + len, "*.*");
2658 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002659
Victor Stinner8c62be82010-05-06 00:08:46 +00002660 if ((d = PyList_New(0)) == NULL)
2661 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002662
Antoine Pitroub73caab2010-08-09 23:39:31 +00002663 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002664 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002665 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002666 if (hFindFile == INVALID_HANDLE_VALUE) {
2667 int error = GetLastError();
2668 if (error == ERROR_FILE_NOT_FOUND)
2669 return d;
2670 Py_DECREF(d);
2671 return win32_error("FindFirstFile", namebuf);
2672 }
2673 do {
2674 /* Skip over . and .. */
2675 if (strcmp(FileData.cFileName, ".") != 0 &&
2676 strcmp(FileData.cFileName, "..") != 0) {
2677 v = PyBytes_FromString(FileData.cFileName);
2678 if (v == NULL) {
2679 Py_DECREF(d);
2680 d = NULL;
2681 break;
2682 }
2683 if (PyList_Append(d, v) != 0) {
2684 Py_DECREF(v);
2685 Py_DECREF(d);
2686 d = NULL;
2687 break;
2688 }
2689 Py_DECREF(v);
2690 }
2691 Py_BEGIN_ALLOW_THREADS
2692 result = FindNextFile(hFindFile, &FileData);
2693 Py_END_ALLOW_THREADS
2694 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2695 it got to the end of the directory. */
2696 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2697 Py_DECREF(d);
2698 win32_error("FindNextFile", namebuf);
2699 FindClose(hFindFile);
2700 return NULL;
2701 }
2702 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002703
Victor Stinner8c62be82010-05-06 00:08:46 +00002704 if (FindClose(hFindFile) == FALSE) {
2705 Py_DECREF(d);
2706 return win32_error("FindClose", namebuf);
2707 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002708
Victor Stinner8c62be82010-05-06 00:08:46 +00002709 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002710
Tim Peters0bb44a42000-09-15 07:44:49 +00002711#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002712
2713#ifndef MAX_PATH
2714#define MAX_PATH CCHMAXPATH
2715#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002716 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002717 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002718 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002719 PyObject *d, *v;
2720 char namebuf[MAX_PATH+5];
2721 HDIR hdir = 1;
2722 ULONG srchcnt = 1;
2723 FILEFINDBUF3 ep;
2724 APIRET rc;
2725
Victor Stinner8c62be82010-05-06 00:08:46 +00002726 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002727 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002728 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002729 name = PyBytes_AsString(oname);
2730 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002731 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002732 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002733 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002734 return NULL;
2735 }
2736 strcpy(namebuf, name);
2737 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002738 if (*pt == ALTSEP)
2739 *pt = SEP;
2740 if (namebuf[len-1] != SEP)
2741 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002742 strcpy(namebuf + len, "*.*");
2743
Neal Norwitz6c913782007-10-14 03:23:09 +00002744 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002745 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002746 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002747 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002748
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002749 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2750 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002751 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002752 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2753 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2754 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002755
2756 if (rc != NO_ERROR) {
2757 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002758 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002759 }
2760
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002761 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002762 do {
2763 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002764 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002765 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002766
2767 strcpy(namebuf, ep.achName);
2768
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002769 /* Leave Case of Name Alone -- In Native Form */
2770 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002771
Christian Heimes72b710a2008-05-26 13:28:38 +00002772 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002773 if (v == NULL) {
2774 Py_DECREF(d);
2775 d = NULL;
2776 break;
2777 }
2778 if (PyList_Append(d, v) != 0) {
2779 Py_DECREF(v);
2780 Py_DECREF(d);
2781 d = NULL;
2782 break;
2783 }
2784 Py_DECREF(v);
2785 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2786 }
2787
Victor Stinnerdcb24032010-04-22 12:08:36 +00002788 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002789 return d;
2790#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002791 PyObject *oname;
2792 char *name;
2793 PyObject *d, *v;
2794 DIR *dirp;
2795 struct dirent *ep;
2796 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002797
Victor Stinner8c62be82010-05-06 00:08:46 +00002798 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002799 /* v is never read, so it does not need to be initialized yet. */
2800 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002801 arg_is_unicode = 0;
2802 PyErr_Clear();
2803 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002804 oname = NULL;
2805 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002807 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002808 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002809 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002810 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002811 Py_BEGIN_ALLOW_THREADS
2812 dirp = opendir(name);
2813 Py_END_ALLOW_THREADS
2814 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002815 return posix_error_with_allocated_filename(oname);
2816 }
2817 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002818 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002819 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002820 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002821 Py_DECREF(oname);
2822 return NULL;
2823 }
2824 for (;;) {
2825 errno = 0;
2826 Py_BEGIN_ALLOW_THREADS
2827 ep = readdir(dirp);
2828 Py_END_ALLOW_THREADS
2829 if (ep == NULL) {
2830 if (errno == 0) {
2831 break;
2832 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002833 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002834 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002835 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 Py_DECREF(d);
2837 return posix_error_with_allocated_filename(oname);
2838 }
2839 }
2840 if (ep->d_name[0] == '.' &&
2841 (NAMLEN(ep) == 1 ||
2842 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2843 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002844 if (arg_is_unicode)
2845 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2846 else
2847 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002848 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002849 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002850 break;
2851 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002852 if (PyList_Append(d, v) != 0) {
2853 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002854 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002855 break;
2856 }
2857 Py_DECREF(v);
2858 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002859 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002860 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002861 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002862 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002863
Victor Stinner8c62be82010-05-06 00:08:46 +00002864 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002865
Tim Peters0bb44a42000-09-15 07:44:49 +00002866#endif /* which OS */
2867} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002868
Antoine Pitrou8250e232011-02-25 23:41:16 +00002869#ifdef HAVE_FDOPENDIR
2870PyDoc_STRVAR(posix_fdlistdir__doc__,
2871"fdlistdir(fd) -> list_of_strings\n\n\
2872Like listdir(), but uses a file descriptor instead.\n\
2873After succesful execution of this function, fd will be closed.");
2874
2875static PyObject *
2876posix_fdlistdir(PyObject *self, PyObject *args)
2877{
2878 PyObject *d, *v;
2879 DIR *dirp;
2880 struct dirent *ep;
2881 int fd;
2882
2883 errno = 0;
2884 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2885 return NULL;
2886 Py_BEGIN_ALLOW_THREADS
2887 dirp = fdopendir(fd);
2888 Py_END_ALLOW_THREADS
2889 if (dirp == NULL) {
2890 close(fd);
2891 return posix_error();
2892 }
2893 if ((d = PyList_New(0)) == NULL) {
2894 Py_BEGIN_ALLOW_THREADS
2895 closedir(dirp);
2896 Py_END_ALLOW_THREADS
2897 return NULL;
2898 }
2899 for (;;) {
2900 errno = 0;
2901 Py_BEGIN_ALLOW_THREADS
2902 ep = readdir(dirp);
2903 Py_END_ALLOW_THREADS
2904 if (ep == NULL) {
2905 if (errno == 0) {
2906 break;
2907 } else {
2908 Py_BEGIN_ALLOW_THREADS
2909 closedir(dirp);
2910 Py_END_ALLOW_THREADS
2911 Py_DECREF(d);
2912 return posix_error();
2913 }
2914 }
2915 if (ep->d_name[0] == '.' &&
2916 (NAMLEN(ep) == 1 ||
2917 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2918 continue;
2919 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2920 if (v == NULL) {
2921 Py_CLEAR(d);
2922 break;
2923 }
2924 if (PyList_Append(d, v) != 0) {
2925 Py_DECREF(v);
2926 Py_CLEAR(d);
2927 break;
2928 }
2929 Py_DECREF(v);
2930 }
2931 Py_BEGIN_ALLOW_THREADS
2932 closedir(dirp);
2933 Py_END_ALLOW_THREADS
2934
2935 return d;
2936}
2937#endif
2938
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002939#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002940/* A helper function for abspath on win32 */
2941static PyObject *
2942posix__getfullpathname(PyObject *self, PyObject *args)
2943{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002944 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002945 char outbuf[MAX_PATH*2];
2946 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002947 PyObject *po;
2948
2949 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
2950 {
2951 wchar_t *wpath;
2952 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2953 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00002954 DWORD result;
2955 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002956
2957 wpath = PyUnicode_AsUnicode(po);
2958 if (wpath == NULL)
2959 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002960 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02002961 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002962 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02002963 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002964 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00002965 if (!woutbufp)
2966 return PyErr_NoMemory();
2967 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2968 }
2969 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002970 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00002971 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02002972 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002973 if (woutbufp != woutbuf)
2974 free(woutbufp);
2975 return v;
2976 }
2977 /* Drop the argument parsing error as narrow strings
2978 are also valid. */
2979 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02002980
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002981 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
2982 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00002983 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002984 if (win32_warn_bytes_api())
2985 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02002986 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 outbuf, &temp)) {
2988 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002989 return NULL;
2990 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2992 return PyUnicode_Decode(outbuf, strlen(outbuf),
2993 Py_FileSystemDefaultEncoding, NULL);
2994 }
2995 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002996} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002997
Brian Curtind25aef52011-06-13 15:16:04 -05002998
Brian Curtinf5e76d02010-11-24 13:14:05 +00002999
Brian Curtind40e6f72010-07-08 21:39:08 +00003000/* A helper function for samepath on windows */
3001static PyObject *
3002posix__getfinalpathname(PyObject *self, PyObject *args)
3003{
3004 HANDLE hFile;
3005 int buf_size;
3006 wchar_t *target_path;
3007 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003008 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003009 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003010
Victor Stinnereb5657a2011-09-30 01:44:27 +02003011 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003012 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003013 path = PyUnicode_AsUnicode(po);
3014 if (path == NULL)
3015 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003016
3017 if(!check_GetFinalPathNameByHandle()) {
3018 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3019 NotImplementedError. */
3020 return PyErr_Format(PyExc_NotImplementedError,
3021 "GetFinalPathNameByHandle not available on this platform");
3022 }
3023
3024 hFile = CreateFileW(
3025 path,
3026 0, /* desired access */
3027 0, /* share mode */
3028 NULL, /* security attributes */
3029 OPEN_EXISTING,
3030 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3031 FILE_FLAG_BACKUP_SEMANTICS,
3032 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003033
Victor Stinnereb5657a2011-09-30 01:44:27 +02003034 if(hFile == INVALID_HANDLE_VALUE)
3035 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003036
3037 /* We have a good handle to the target, use it to determine the
3038 target path name. */
3039 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3040
3041 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003042 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003043
3044 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3045 if(!target_path)
3046 return PyErr_NoMemory();
3047
3048 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3049 buf_size, VOLUME_NAME_DOS);
3050 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003051 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003052
3053 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003054 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003055
3056 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003057 result = PyUnicode_FromWideChar(target_path, result_length);
Brian Curtind40e6f72010-07-08 21:39:08 +00003058 free(target_path);
3059 return result;
3060
3061} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003062
3063static PyObject *
3064posix__getfileinformation(PyObject *self, PyObject *args)
3065{
3066 HANDLE hFile;
3067 BY_HANDLE_FILE_INFORMATION info;
3068 int fd;
3069
3070 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3071 return NULL;
3072
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003073 if (!_PyVerify_fd(fd))
3074 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003075
3076 hFile = (HANDLE)_get_osfhandle(fd);
3077 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003078 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003079
3080 if (!GetFileInformationByHandle(hFile, &info))
3081 return win32_error("_getfileinformation", NULL);
3082
3083 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3084 info.nFileIndexHigh,
3085 info.nFileIndexLow);
3086}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003087
Brian Curtin95d028f2011-06-09 09:10:38 -05003088PyDoc_STRVAR(posix__isdir__doc__,
3089"Return true if the pathname refers to an existing directory.");
3090
Brian Curtin9c669cc2011-06-08 18:17:18 -05003091static PyObject *
3092posix__isdir(PyObject *self, PyObject *args)
3093{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003094 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003095 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003096 DWORD attributes;
3097
3098 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003099 wchar_t *wpath = PyUnicode_AsUnicode(po);
3100 if (wpath == NULL)
3101 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003102
3103 attributes = GetFileAttributesW(wpath);
3104 if (attributes == INVALID_FILE_ATTRIBUTES)
3105 Py_RETURN_FALSE;
3106 goto check;
3107 }
3108 /* Drop the argument parsing error as narrow strings
3109 are also valid. */
3110 PyErr_Clear();
3111
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003112 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003113 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003114 if (win32_warn_bytes_api())
3115 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003116 attributes = GetFileAttributesA(path);
3117 if (attributes == INVALID_FILE_ATTRIBUTES)
3118 Py_RETURN_FALSE;
3119
3120check:
3121 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3122 Py_RETURN_TRUE;
3123 else
3124 Py_RETURN_FALSE;
3125}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003126#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003128PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003129"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003130Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003131
Barry Warsaw53699e91996-12-10 23:23:01 +00003132static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003133posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003134{
Victor Stinner8c62be82010-05-06 00:08:46 +00003135 int res;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003136 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003137 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003138
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003139#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003140 PyObject *po;
3141 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode))
3142 {
3143 wchar_t *wpath = PyUnicode_AsUnicode(po);
3144 if (wpath == NULL)
3145 return NULL;
3146
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003148 res = CreateDirectoryW(wpath, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003149 Py_END_ALLOW_THREADS
3150 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003151 return win32_error_object("mkdir", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003152 Py_INCREF(Py_None);
3153 return Py_None;
3154 }
3155 /* Drop the argument parsing error as narrow strings
3156 are also valid. */
3157 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003158 if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003160 if (win32_warn_bytes_api())
3161 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003162 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 res = CreateDirectoryA(path, NULL);
3164 Py_END_ALLOW_THREADS
3165 if (!res) {
3166 win32_error("mkdir", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003167 return NULL;
3168 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003169 Py_INCREF(Py_None);
3170 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003171#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003172 PyObject *opath;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003173
Victor Stinner8c62be82010-05-06 00:08:46 +00003174 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3175 PyUnicode_FSConverter, &opath, &mode))
3176 return NULL;
3177 path = PyBytes_AsString(opath);
3178 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003179#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003180 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003181#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003182 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003183#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003184 Py_END_ALLOW_THREADS
3185 if (res < 0)
3186 return posix_error_with_allocated_filename(opath);
3187 Py_DECREF(opath);
3188 Py_INCREF(Py_None);
3189 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003190#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003191}
3192
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003193
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003194/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3195#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003196#include <sys/resource.h>
3197#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003198
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003199
3200#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003201PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003202"nice(inc) -> new_priority\n\n\
3203Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003204
Barry Warsaw53699e91996-12-10 23:23:01 +00003205static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003206posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003207{
Victor Stinner8c62be82010-05-06 00:08:46 +00003208 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003209
Victor Stinner8c62be82010-05-06 00:08:46 +00003210 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3211 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003212
Victor Stinner8c62be82010-05-06 00:08:46 +00003213 /* There are two flavours of 'nice': one that returns the new
3214 priority (as required by almost all standards out there) and the
3215 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3216 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003217
Victor Stinner8c62be82010-05-06 00:08:46 +00003218 If we are of the nice family that returns the new priority, we
3219 need to clear errno before the call, and check if errno is filled
3220 before calling posix_error() on a returnvalue of -1, because the
3221 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003222
Victor Stinner8c62be82010-05-06 00:08:46 +00003223 errno = 0;
3224 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003225#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003226 if (value == 0)
3227 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003228#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003229 if (value == -1 && errno != 0)
3230 /* either nice() or getpriority() returned an error */
3231 return posix_error();
3232 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003233}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003234#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003235
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003236
3237#ifdef HAVE_GETPRIORITY
3238PyDoc_STRVAR(posix_getpriority__doc__,
3239"getpriority(which, who) -> current_priority\n\n\
3240Get program scheduling priority.");
3241
3242static PyObject *
3243posix_getpriority(PyObject *self, PyObject *args)
3244{
3245 int which, who, retval;
3246
3247 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3248 return NULL;
3249 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003250 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003251 if (errno != 0)
3252 return posix_error();
3253 return PyLong_FromLong((long)retval);
3254}
3255#endif /* HAVE_GETPRIORITY */
3256
3257
3258#ifdef HAVE_SETPRIORITY
3259PyDoc_STRVAR(posix_setpriority__doc__,
3260"setpriority(which, who, prio) -> None\n\n\
3261Set program scheduling priority.");
3262
3263static PyObject *
3264posix_setpriority(PyObject *self, PyObject *args)
3265{
3266 int which, who, prio, retval;
3267
3268 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3269 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003270 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003271 if (retval == -1)
3272 return posix_error();
3273 Py_RETURN_NONE;
3274}
3275#endif /* HAVE_SETPRIORITY */
3276
3277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003278PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003279"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003280Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003281
Barry Warsaw53699e91996-12-10 23:23:01 +00003282static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003283posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003284{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003285#ifdef MS_WINDOWS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003286 PyObject *src, *dst;
Victor Stinner8c62be82010-05-06 00:08:46 +00003287 BOOL result;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003288 if (PyArg_ParseTuple(args, "UU:rename", &src, &dst))
3289 {
3290 wchar_t *wsrc, *wdst;
3291
3292 wsrc = PyUnicode_AsUnicode(src);
3293 if (wsrc == NULL)
3294 return NULL;
3295 wdst = PyUnicode_AsUnicode(dst);
3296 if (wdst == NULL)
3297 return NULL;
3298 Py_BEGIN_ALLOW_THREADS
3299 result = MoveFileW(wsrc, wdst);
3300 Py_END_ALLOW_THREADS
3301 if (!result)
3302 return win32_error("rename", NULL);
3303 Py_INCREF(Py_None);
3304 return Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003305 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003306 else {
3307 PyErr_Clear();
3308 if (!PyArg_ParseTuple(args, "O&O&:rename",
3309 PyUnicode_FSConverter, &src,
3310 PyUnicode_FSConverter, &dst))
3311 return NULL;
3312
3313 if (win32_warn_bytes_api())
3314 goto error;
3315
3316 Py_BEGIN_ALLOW_THREADS
3317 result = MoveFileA(PyBytes_AS_STRING(src),
3318 PyBytes_AS_STRING(dst));
3319 Py_END_ALLOW_THREADS
3320
3321 Py_XDECREF(src);
3322 Py_XDECREF(dst);
3323
3324 if (!result)
3325 return win32_error("rename", NULL);
3326 Py_INCREF(Py_None);
3327 return Py_None;
3328
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003329error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003330 Py_XDECREF(src);
3331 Py_XDECREF(dst);
Victor Stinner8c62be82010-05-06 00:08:46 +00003332 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003333 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003334#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003335 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003336#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003337}
3338
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003340PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003341"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003342Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003343
Barry Warsaw53699e91996-12-10 23:23:01 +00003344static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003345posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003346{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003347#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003348 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003349#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003351#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003352}
3353
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003355PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003356"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003357Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003358
Barry Warsaw53699e91996-12-10 23:23:01 +00003359static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003360posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003361{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003362#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003363 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003364#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003365 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003366#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003367}
3368
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003369
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003370#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003371PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003372"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003373Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Barry Warsaw53699e91996-12-10 23:23:01 +00003375static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003376posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003377{
Victor Stinner8c62be82010-05-06 00:08:46 +00003378 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003379#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003380 wchar_t *command;
3381 if (!PyArg_ParseTuple(args, "u:system", &command))
3382 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003383
Victor Stinner8c62be82010-05-06 00:08:46 +00003384 Py_BEGIN_ALLOW_THREADS
3385 sts = _wsystem(command);
3386 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003387#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003388 PyObject *command_obj;
3389 char *command;
3390 if (!PyArg_ParseTuple(args, "O&:system",
3391 PyUnicode_FSConverter, &command_obj))
3392 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003393
Victor Stinner8c62be82010-05-06 00:08:46 +00003394 command = PyBytes_AsString(command_obj);
3395 Py_BEGIN_ALLOW_THREADS
3396 sts = system(command);
3397 Py_END_ALLOW_THREADS
3398 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003399#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003400 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003401}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003402#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003403
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003404
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003405PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003406"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003407Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003408
Barry Warsaw53699e91996-12-10 23:23:01 +00003409static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003410posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003411{
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 int i;
3413 if (!PyArg_ParseTuple(args, "i:umask", &i))
3414 return NULL;
3415 i = (int)umask(i);
3416 if (i < 0)
3417 return posix_error();
3418 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003419}
3420
Brian Curtind40e6f72010-07-08 21:39:08 +00003421#ifdef MS_WINDOWS
3422
3423/* override the default DeleteFileW behavior so that directory
3424symlinks can be removed with this function, the same as with
3425Unix symlinks */
3426BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3427{
3428 WIN32_FILE_ATTRIBUTE_DATA info;
3429 WIN32_FIND_DATAW find_data;
3430 HANDLE find_data_handle;
3431 int is_directory = 0;
3432 int is_link = 0;
3433
3434 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3435 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003436
Brian Curtind40e6f72010-07-08 21:39:08 +00003437 /* Get WIN32_FIND_DATA structure for the path to determine if
3438 it is a symlink */
3439 if(is_directory &&
3440 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3441 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3442
3443 if(find_data_handle != INVALID_HANDLE_VALUE) {
3444 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3445 FindClose(find_data_handle);
3446 }
3447 }
3448 }
3449
3450 if (is_directory && is_link)
3451 return RemoveDirectoryW(lpFileName);
3452
3453 return DeleteFileW(lpFileName);
3454}
3455#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003457PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003458"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003459Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003461PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003462"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003463Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003464
Barry Warsaw53699e91996-12-10 23:23:01 +00003465static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003466posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003467{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003468#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003469 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3470 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003471#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003472 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003473#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003474}
3475
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003476
Guido van Rossumb6775db1994-08-01 11:34:53 +00003477#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003478PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003479"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003480Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003481
Barry Warsaw53699e91996-12-10 23:23:01 +00003482static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003483posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003484{
Victor Stinner8c62be82010-05-06 00:08:46 +00003485 struct utsname u;
3486 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003487
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 Py_BEGIN_ALLOW_THREADS
3489 res = uname(&u);
3490 Py_END_ALLOW_THREADS
3491 if (res < 0)
3492 return posix_error();
3493 return Py_BuildValue("(sssss)",
3494 u.sysname,
3495 u.nodename,
3496 u.release,
3497 u.version,
3498 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003499}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003500#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003501
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003502
3503/*
3504 * Classic POSIX utime functions supported microseconds (1m/sec).
3505 * Newer POSIX functions support nanoseconds (1 billion per sec).
3506 * posixmodule now uses the new functions where possible.
3507 * This improves accuracy in many situations, for example shutil.copy2().
3508 *
3509 * The implementation isn't currently sophisticated enough to handle
3510 * a platform where HAVE_UTIMENSAT is true but HAVE_FUTIMENS is false.
3511 * Specifically, posix_futimes() would break.
3512 *
3513 * Supporting such a platform wouldn't be impossible; you'd need two
3514 * extract_time() functions, or make its precision a parameter.
3515 * Since such a platform seems unlikely we haven't bothered.
3516 */
3517#if defined(HAVE_UTIMENSAT)
3518#define EXTRACT_TIME_PRECISION (1e9)
3519#if !defined(HAVE_FUTIMENS)
3520#error You HAVE_UTIMENSAT but not HAVE_FUTIMENS... please see accompanying comment.
3521#endif
3522#else
3523#define EXTRACT_TIME_PRECISION (1e6)
3524#endif
3525
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003526static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003527extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003528{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003529 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 if (PyFloat_Check(t)) {
3531 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003532 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003533 if (!intobj)
3534 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003535#if SIZEOF_TIME_T > SIZEOF_LONG
3536 intval = PyLong_AsUnsignedLongLongMask(intobj);
3537#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003538 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003539#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003540 Py_DECREF(intobj);
3541 if (intval == -1 && PyErr_Occurred())
3542 return -1;
3543 *sec = intval;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003544
3545 *usec = (long)((tval - intval) * EXTRACT_TIME_PRECISION);
Victor Stinner8c62be82010-05-06 00:08:46 +00003546 if (*usec < 0)
3547 /* If rounding gave us a negative number,
3548 truncate. */
3549 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003550 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003551 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003552#if SIZEOF_TIME_T > SIZEOF_LONG
3553 intval = PyLong_AsUnsignedLongLongMask(t);
3554#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003556#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 if (intval == -1 && PyErr_Occurred())
3558 return -1;
3559 *sec = intval;
3560 *usec = 0;
3561 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003562}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003564PyDoc_STRVAR(posix_utime__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003565"utime(path[, (atime, mtime)])\n\
3566Set the access and modified time of the file to the given values.\n\
3567If no second argument is used, set the access and modified times to\n\
3568the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003569
Barry Warsaw53699e91996-12-10 23:23:01 +00003570static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003571posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003572{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003573#ifdef MS_WINDOWS
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003574 PyObject *arg = Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003575 PyObject *obwpath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003576 wchar_t *wpath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003577 const char *apath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003578 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003579 time_t atimesec, mtimesec;
3580 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003581 FILETIME atime, mtime;
3582 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003583
Brian Curtin52fbea12011-11-06 13:41:17 -06003584 if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003585 wpath = PyUnicode_AsUnicode(obwpath);
3586 if (wpath == NULL)
3587 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 Py_BEGIN_ALLOW_THREADS
3589 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3590 NULL, OPEN_EXISTING,
3591 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3592 Py_END_ALLOW_THREADS
3593 if (hFile == INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003594 return win32_error_object("utime", obwpath);
3595 }
3596 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00003597 /* Drop the argument parsing error as narrow strings
3598 are also valid. */
3599 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003600
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003601 if (!PyArg_ParseTuple(args, "y|O:utime", &apath, &arg))
3602 return NULL;
3603 if (win32_warn_bytes_api())
Victor Stinner8c62be82010-05-06 00:08:46 +00003604 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003605
Victor Stinner8c62be82010-05-06 00:08:46 +00003606 Py_BEGIN_ALLOW_THREADS
3607 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3608 NULL, OPEN_EXISTING,
3609 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3610 Py_END_ALLOW_THREADS
3611 if (hFile == INVALID_HANDLE_VALUE) {
3612 win32_error("utime", apath);
Victor Stinner8c62be82010-05-06 00:08:46 +00003613 return NULL;
3614 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003615 }
3616
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003617 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003618 SYSTEMTIME now;
3619 GetSystemTime(&now);
3620 if (!SystemTimeToFileTime(&now, &mtime) ||
3621 !SystemTimeToFileTime(&now, &atime)) {
3622 win32_error("utime", NULL);
3623 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003624 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 }
3626 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3627 PyErr_SetString(PyExc_TypeError,
3628 "utime() arg 2 must be a tuple (atime, mtime)");
3629 goto done;
3630 }
3631 else {
3632 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3633 &atimesec, &ausec) == -1)
3634 goto done;
3635 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3636 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3637 &mtimesec, &musec) == -1)
3638 goto done;
3639 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3640 }
3641 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3642 /* Avoid putting the file name into the error here,
3643 as that may confuse the user into believing that
3644 something is wrong with the file, when it also
3645 could be the time stamp that gives a problem. */
3646 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003647 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003648 }
3649 Py_INCREF(Py_None);
3650 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003651done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003652 CloseHandle(hFile);
3653 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003654#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003655
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 PyObject *opath;
3657 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003658 time_t atime, mtime;
3659 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003660 int res;
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003661 PyObject* arg = Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003662
Brian Curtin52fbea12011-11-06 13:41:17 -06003663 if (!PyArg_ParseTuple(args, "O&|O:utime",
Victor Stinner8c62be82010-05-06 00:08:46 +00003664 PyUnicode_FSConverter, &opath, &arg))
3665 return NULL;
3666 path = PyBytes_AsString(opath);
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003667 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003668 /* optional time values not given */
3669 Py_BEGIN_ALLOW_THREADS
3670 res = utime(path, NULL);
3671 Py_END_ALLOW_THREADS
3672 }
3673 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3674 PyErr_SetString(PyExc_TypeError,
3675 "utime() arg 2 must be a tuple (atime, mtime)");
3676 Py_DECREF(opath);
3677 return NULL;
3678 }
3679 else {
3680 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3681 &atime, &ausec) == -1) {
3682 Py_DECREF(opath);
3683 return NULL;
3684 }
3685 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3686 &mtime, &musec) == -1) {
3687 Py_DECREF(opath);
3688 return NULL;
3689 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003690
3691 Py_BEGIN_ALLOW_THREADS
3692 {
3693#ifdef HAVE_UTIMENSAT
3694 struct timespec buf[2];
3695 buf[0].tv_sec = atime;
3696 buf[0].tv_nsec = ausec;
3697 buf[1].tv_sec = mtime;
3698 buf[1].tv_nsec = musec;
3699 res = utimensat(AT_FDCWD, path, buf, 0);
3700#elif defined(HAVE_UTIMES)
3701 struct timeval buf[2];
3702 buf[0].tv_sec = atime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003703 buf[0].tv_usec = ausec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003704 buf[1].tv_sec = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00003705 buf[1].tv_usec = musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003706 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003707#elif defined(HAVE_UTIME_H)
3708 /* XXX should define struct utimbuf instead, above */
3709 struct utimbuf buf;
3710 buf.actime = atime;
3711 buf.modtime = mtime;
3712 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003713#else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003714 time_t buf[2];
3715 buf[0] = atime;
3716 buf[1] = mtime;
3717 res = utime(path, buf);
3718#endif
3719 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003720 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003721 }
3722 if (res < 0) {
3723 return posix_error_with_allocated_filename(opath);
3724 }
3725 Py_DECREF(opath);
3726 Py_INCREF(Py_None);
3727 return Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003728#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003729#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003730}
3731
Ross Lagerwall7807c352011-03-17 20:20:30 +02003732#ifdef HAVE_FUTIMES
3733PyDoc_STRVAR(posix_futimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003734"futimes(fd[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003735Set the access and modified time of the file specified by the file\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003736descriptor fd to the given values. If no second argument is used, set the\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003737access and modified times to the current time.");
3738
3739static PyObject *
3740posix_futimes(PyObject *self, PyObject *args)
3741{
3742 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003743 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003744 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003745 long ausec, musec;
3746
Brian Curtinc1b65d12011-11-07 14:18:54 -06003747 if (!PyArg_ParseTuple(args, "i|O:futimes", &fd, &arg))
Ross Lagerwall7807c352011-03-17 20:20:30 +02003748 return NULL;
3749
3750 if (arg == Py_None) {
3751 /* optional time values not given */
3752 Py_BEGIN_ALLOW_THREADS
3753 res = futimes(fd, NULL);
3754 Py_END_ALLOW_THREADS
3755 }
3756 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3757 PyErr_SetString(PyExc_TypeError,
3758 "futimes() arg 2 must be a tuple (atime, mtime)");
3759 return NULL;
3760 }
3761 else {
3762 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003763 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003764 return NULL;
3765 }
3766 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003767 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003768 return NULL;
3769 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003770 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003771 {
3772#ifdef HAVE_FUTIMENS
3773 struct timespec buf[2];
3774 buf[0].tv_sec = atime;
3775 buf[0].tv_nsec = ausec;
3776 buf[1].tv_sec = mtime;
3777 buf[1].tv_nsec = musec;
3778 res = futimens(fd, buf);
3779#else
3780 struct timeval buf[2];
3781 buf[0].tv_sec = atime;
3782 buf[0].tv_usec = ausec;
3783 buf[1].tv_sec = mtime;
3784 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003785 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003786#endif
3787 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003788 Py_END_ALLOW_THREADS
3789 }
3790 if (res < 0)
3791 return posix_error();
3792 Py_RETURN_NONE;
3793}
3794#endif
3795
3796#ifdef HAVE_LUTIMES
3797PyDoc_STRVAR(posix_lutimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003798"lutimes(path[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003799Like utime(), but if path is a symbolic link, it is not dereferenced.");
3800
3801static PyObject *
3802posix_lutimes(PyObject *self, PyObject *args)
3803{
Brian Curtinc1b65d12011-11-07 14:18:54 -06003804 PyObject *opath;
3805 PyObject *arg = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003806 const char *path;
3807 int res;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003808 time_t atime, mtime;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003809 long ausec, musec;
3810
Brian Curtinc1b65d12011-11-07 14:18:54 -06003811 if (!PyArg_ParseTuple(args, "O&|O:lutimes",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003812 PyUnicode_FSConverter, &opath, &arg))
3813 return NULL;
3814 path = PyBytes_AsString(opath);
3815 if (arg == Py_None) {
3816 /* optional time values not given */
3817 Py_BEGIN_ALLOW_THREADS
3818 res = lutimes(path, NULL);
3819 Py_END_ALLOW_THREADS
3820 }
3821 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3822 PyErr_SetString(PyExc_TypeError,
3823 "lutimes() arg 2 must be a tuple (atime, mtime)");
3824 Py_DECREF(opath);
3825 return NULL;
3826 }
3827 else {
3828 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003829 &atime, &ausec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003830 Py_DECREF(opath);
3831 return NULL;
3832 }
3833 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003834 &mtime, &musec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003835 Py_DECREF(opath);
3836 return NULL;
3837 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003838 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003839 {
3840#ifdef HAVE_UTIMENSAT
3841 struct timespec buf[2];
3842 buf[0].tv_sec = atime;
3843 buf[0].tv_nsec = ausec;
3844 buf[1].tv_sec = mtime;
3845 buf[1].tv_nsec = musec;
3846 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
3847#else
3848 struct timeval buf[2];
3849 buf[0].tv_sec = atime;
3850 buf[0].tv_usec = ausec;
3851 buf[1].tv_sec = mtime;
3852 buf[1].tv_usec = musec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003853 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003854#endif
3855 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003856 Py_END_ALLOW_THREADS
3857 }
3858 Py_DECREF(opath);
3859 if (res < 0)
3860 return posix_error();
3861 Py_RETURN_NONE;
3862}
3863#endif
3864
3865#ifdef HAVE_FUTIMENS
3866PyDoc_STRVAR(posix_futimens__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003867"futimens(fd[, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003868Updates the timestamps of a file specified by the file descriptor fd, with\n\
3869nanosecond precision.\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003870If no second argument is given, set atime and mtime to the current time.\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003871If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3872current time.\n\
3873If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3874
3875static PyObject *
3876posix_futimens(PyObject *self, PyObject *args)
3877{
3878 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003879 PyObject *atime = Py_None;
3880 PyObject *mtime = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003881 struct timespec buf[2];
3882
Brian Curtinc1b65d12011-11-07 14:18:54 -06003883 if (!PyArg_ParseTuple(args, "i|OO:futimens",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003884 &fd, &atime, &mtime))
3885 return NULL;
3886 if (atime == Py_None && mtime == Py_None) {
3887 /* optional time values not given */
3888 Py_BEGIN_ALLOW_THREADS
3889 res = futimens(fd, NULL);
3890 Py_END_ALLOW_THREADS
3891 }
3892 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3893 PyErr_SetString(PyExc_TypeError,
3894 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3895 return NULL;
3896 }
3897 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3898 PyErr_SetString(PyExc_TypeError,
3899 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3900 return NULL;
3901 }
3902 else {
3903 if (!PyArg_ParseTuple(atime, "ll:futimens",
3904 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3905 return NULL;
3906 }
3907 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3908 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3909 return NULL;
3910 }
3911 Py_BEGIN_ALLOW_THREADS
3912 res = futimens(fd, buf);
3913 Py_END_ALLOW_THREADS
3914 }
3915 if (res < 0)
3916 return posix_error();
3917 Py_RETURN_NONE;
3918}
3919#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003920
Guido van Rossum3b066191991-06-04 19:40:25 +00003921/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003922
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003923PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003924"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003925Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003926
Barry Warsaw53699e91996-12-10 23:23:01 +00003927static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003928posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003929{
Victor Stinner8c62be82010-05-06 00:08:46 +00003930 int sts;
3931 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3932 return NULL;
3933 _exit(sts);
3934 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003935}
3936
Martin v. Löwis114619e2002-10-07 06:44:21 +00003937#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3938static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003939free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003940{
Victor Stinner8c62be82010-05-06 00:08:46 +00003941 Py_ssize_t i;
3942 for (i = 0; i < count; i++)
3943 PyMem_Free(array[i]);
3944 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003945}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003946
Antoine Pitrou69f71142009-05-24 21:25:49 +00003947static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003948int fsconvert_strdup(PyObject *o, char**out)
3949{
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 PyObject *bytes;
3951 Py_ssize_t size;
3952 if (!PyUnicode_FSConverter(o, &bytes))
3953 return 0;
3954 size = PyBytes_GET_SIZE(bytes);
3955 *out = PyMem_Malloc(size+1);
3956 if (!*out)
3957 return 0;
3958 memcpy(*out, PyBytes_AsString(bytes), size+1);
3959 Py_DECREF(bytes);
3960 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003961}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003962#endif
3963
Ross Lagerwall7807c352011-03-17 20:20:30 +02003964#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003965static char**
3966parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3967{
Victor Stinner8c62be82010-05-06 00:08:46 +00003968 char **envlist;
3969 Py_ssize_t i, pos, envc;
3970 PyObject *keys=NULL, *vals=NULL;
3971 PyObject *key, *val, *key2, *val2;
3972 char *p, *k, *v;
3973 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003974
Victor Stinner8c62be82010-05-06 00:08:46 +00003975 i = PyMapping_Size(env);
3976 if (i < 0)
3977 return NULL;
3978 envlist = PyMem_NEW(char *, i + 1);
3979 if (envlist == NULL) {
3980 PyErr_NoMemory();
3981 return NULL;
3982 }
3983 envc = 0;
3984 keys = PyMapping_Keys(env);
3985 vals = PyMapping_Values(env);
3986 if (!keys || !vals)
3987 goto error;
3988 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3989 PyErr_Format(PyExc_TypeError,
3990 "env.keys() or env.values() is not a list");
3991 goto error;
3992 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003993
Victor Stinner8c62be82010-05-06 00:08:46 +00003994 for (pos = 0; pos < i; pos++) {
3995 key = PyList_GetItem(keys, pos);
3996 val = PyList_GetItem(vals, pos);
3997 if (!key || !val)
3998 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003999
Victor Stinner8c62be82010-05-06 00:08:46 +00004000 if (PyUnicode_FSConverter(key, &key2) == 0)
4001 goto error;
4002 if (PyUnicode_FSConverter(val, &val2) == 0) {
4003 Py_DECREF(key2);
4004 goto error;
4005 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004006
4007#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004008 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
4009 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00004010#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004011 k = PyBytes_AsString(key2);
4012 v = PyBytes_AsString(val2);
4013 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004014
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 p = PyMem_NEW(char, len);
4016 if (p == NULL) {
4017 PyErr_NoMemory();
4018 Py_DECREF(key2);
4019 Py_DECREF(val2);
4020 goto error;
4021 }
4022 PyOS_snprintf(p, len, "%s=%s", k, v);
4023 envlist[envc++] = p;
4024 Py_DECREF(key2);
4025 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004026#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004027 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004028#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004029 }
4030 Py_DECREF(vals);
4031 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004032
Victor Stinner8c62be82010-05-06 00:08:46 +00004033 envlist[envc] = 0;
4034 *envc_ptr = envc;
4035 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004036
4037error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004038 Py_XDECREF(keys);
4039 Py_XDECREF(vals);
4040 while (--envc >= 0)
4041 PyMem_DEL(envlist[envc]);
4042 PyMem_DEL(envlist);
4043 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004044}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004045
Ross Lagerwall7807c352011-03-17 20:20:30 +02004046static char**
4047parse_arglist(PyObject* argv, Py_ssize_t *argc)
4048{
4049 int i;
4050 char **argvlist = PyMem_NEW(char *, *argc+1);
4051 if (argvlist == NULL) {
4052 PyErr_NoMemory();
4053 return NULL;
4054 }
4055 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004056 PyObject* item = PySequence_ITEM(argv, i);
4057 if (item == NULL)
4058 goto fail;
4059 if (!fsconvert_strdup(item, &argvlist[i])) {
4060 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004061 goto fail;
4062 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004063 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004064 }
4065 argvlist[*argc] = NULL;
4066 return argvlist;
4067fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004068 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004069 free_string_array(argvlist, *argc);
4070 return NULL;
4071}
4072#endif
4073
4074#ifdef HAVE_EXECV
4075PyDoc_STRVAR(posix_execv__doc__,
4076"execv(path, args)\n\n\
4077Execute an executable path with arguments, replacing current process.\n\
4078\n\
4079 path: path of executable file\n\
4080 args: tuple or list of strings");
4081
4082static PyObject *
4083posix_execv(PyObject *self, PyObject *args)
4084{
4085 PyObject *opath;
4086 char *path;
4087 PyObject *argv;
4088 char **argvlist;
4089 Py_ssize_t argc;
4090
4091 /* execv has two arguments: (path, argv), where
4092 argv is a list or tuple of strings. */
4093
4094 if (!PyArg_ParseTuple(args, "O&O:execv",
4095 PyUnicode_FSConverter,
4096 &opath, &argv))
4097 return NULL;
4098 path = PyBytes_AsString(opath);
4099 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4100 PyErr_SetString(PyExc_TypeError,
4101 "execv() arg 2 must be a tuple or list");
4102 Py_DECREF(opath);
4103 return NULL;
4104 }
4105 argc = PySequence_Size(argv);
4106 if (argc < 1) {
4107 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4108 Py_DECREF(opath);
4109 return NULL;
4110 }
4111
4112 argvlist = parse_arglist(argv, &argc);
4113 if (argvlist == NULL) {
4114 Py_DECREF(opath);
4115 return NULL;
4116 }
4117
4118 execv(path, argvlist);
4119
4120 /* If we get here it's definitely an error */
4121
4122 free_string_array(argvlist, argc);
4123 Py_DECREF(opath);
4124 return posix_error();
4125}
4126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004127PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004128"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004129Execute a path with arguments and environment, replacing current process.\n\
4130\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004131 path: path of executable file\n\
4132 args: tuple or list of arguments\n\
4133 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004134
Barry Warsaw53699e91996-12-10 23:23:01 +00004135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004136posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004137{
Victor Stinner8c62be82010-05-06 00:08:46 +00004138 PyObject *opath;
4139 char *path;
4140 PyObject *argv, *env;
4141 char **argvlist;
4142 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004143 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004144
Victor Stinner8c62be82010-05-06 00:08:46 +00004145 /* execve has three arguments: (path, argv, env), where
4146 argv is a list or tuple of strings and env is a dictionary
4147 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004148
Victor Stinner8c62be82010-05-06 00:08:46 +00004149 if (!PyArg_ParseTuple(args, "O&OO:execve",
4150 PyUnicode_FSConverter,
4151 &opath, &argv, &env))
4152 return NULL;
4153 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004154 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004155 PyErr_SetString(PyExc_TypeError,
4156 "execve() arg 2 must be a tuple or list");
4157 goto fail_0;
4158 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004159 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004160 if (!PyMapping_Check(env)) {
4161 PyErr_SetString(PyExc_TypeError,
4162 "execve() arg 3 must be a mapping object");
4163 goto fail_0;
4164 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004165
Ross Lagerwall7807c352011-03-17 20:20:30 +02004166 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004167 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004168 goto fail_0;
4169 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004170
Victor Stinner8c62be82010-05-06 00:08:46 +00004171 envlist = parse_envlist(env, &envc);
4172 if (envlist == NULL)
4173 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004174
Victor Stinner8c62be82010-05-06 00:08:46 +00004175 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004176
Victor Stinner8c62be82010-05-06 00:08:46 +00004177 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004178
Victor Stinner8c62be82010-05-06 00:08:46 +00004179 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004180
Victor Stinner8c62be82010-05-06 00:08:46 +00004181 while (--envc >= 0)
4182 PyMem_DEL(envlist[envc]);
4183 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004184 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004185 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004186 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004187 Py_DECREF(opath);
4188 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004189}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004190#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004191
Ross Lagerwall7807c352011-03-17 20:20:30 +02004192#ifdef HAVE_FEXECVE
4193PyDoc_STRVAR(posix_fexecve__doc__,
4194"fexecve(fd, args, env)\n\n\
4195Execute the program specified by a file descriptor with arguments and\n\
4196environment, replacing the current process.\n\
4197\n\
4198 fd: file descriptor of executable\n\
4199 args: tuple or list of arguments\n\
4200 env: dictionary of strings mapping to strings");
4201
4202static PyObject *
4203posix_fexecve(PyObject *self, PyObject *args)
4204{
4205 int fd;
4206 PyObject *argv, *env;
4207 char **argvlist;
4208 char **envlist;
4209 Py_ssize_t argc, envc;
4210
4211 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4212 &fd, &argv, &env))
4213 return NULL;
4214 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4215 PyErr_SetString(PyExc_TypeError,
4216 "fexecve() arg 2 must be a tuple or list");
4217 return NULL;
4218 }
4219 argc = PySequence_Size(argv);
4220 if (!PyMapping_Check(env)) {
4221 PyErr_SetString(PyExc_TypeError,
4222 "fexecve() arg 3 must be a mapping object");
4223 return NULL;
4224 }
4225
4226 argvlist = parse_arglist(argv, &argc);
4227 if (argvlist == NULL)
4228 return NULL;
4229
4230 envlist = parse_envlist(env, &envc);
4231 if (envlist == NULL)
4232 goto fail;
4233
4234 fexecve(fd, argvlist, envlist);
4235
4236 /* If we get here it's definitely an error */
4237
4238 (void) posix_error();
4239
4240 while (--envc >= 0)
4241 PyMem_DEL(envlist[envc]);
4242 PyMem_DEL(envlist);
4243 fail:
4244 free_string_array(argvlist, argc);
4245 return NULL;
4246}
4247#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004248
Guido van Rossuma1065681999-01-25 23:20:23 +00004249#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004250PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004251"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004252Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004253\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004254 mode: mode of process creation\n\
4255 path: path of executable file\n\
4256 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004257
4258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004259posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004260{
Victor Stinner8c62be82010-05-06 00:08:46 +00004261 PyObject *opath;
4262 char *path;
4263 PyObject *argv;
4264 char **argvlist;
4265 int mode, i;
4266 Py_ssize_t argc;
4267 Py_intptr_t spawnval;
4268 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004269
Victor Stinner8c62be82010-05-06 00:08:46 +00004270 /* spawnv has three arguments: (mode, path, argv), where
4271 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004272
Victor Stinner8c62be82010-05-06 00:08:46 +00004273 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4274 PyUnicode_FSConverter,
4275 &opath, &argv))
4276 return NULL;
4277 path = PyBytes_AsString(opath);
4278 if (PyList_Check(argv)) {
4279 argc = PyList_Size(argv);
4280 getitem = PyList_GetItem;
4281 }
4282 else if (PyTuple_Check(argv)) {
4283 argc = PyTuple_Size(argv);
4284 getitem = PyTuple_GetItem;
4285 }
4286 else {
4287 PyErr_SetString(PyExc_TypeError,
4288 "spawnv() arg 2 must be a tuple or list");
4289 Py_DECREF(opath);
4290 return NULL;
4291 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004292
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 argvlist = PyMem_NEW(char *, argc+1);
4294 if (argvlist == NULL) {
4295 Py_DECREF(opath);
4296 return PyErr_NoMemory();
4297 }
4298 for (i = 0; i < argc; i++) {
4299 if (!fsconvert_strdup((*getitem)(argv, i),
4300 &argvlist[i])) {
4301 free_string_array(argvlist, i);
4302 PyErr_SetString(
4303 PyExc_TypeError,
4304 "spawnv() arg 2 must contain only strings");
4305 Py_DECREF(opath);
4306 return NULL;
4307 }
4308 }
4309 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004310
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004311#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004312 Py_BEGIN_ALLOW_THREADS
4313 spawnval = spawnv(mode, path, argvlist);
4314 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004315#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 if (mode == _OLD_P_OVERLAY)
4317 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004318
Victor Stinner8c62be82010-05-06 00:08:46 +00004319 Py_BEGIN_ALLOW_THREADS
4320 spawnval = _spawnv(mode, path, argvlist);
4321 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004322#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004323
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 free_string_array(argvlist, argc);
4325 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004326
Victor Stinner8c62be82010-05-06 00:08:46 +00004327 if (spawnval == -1)
4328 return posix_error();
4329 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004330#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004331 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004332#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004333 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004334#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004335}
4336
4337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004338PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004339"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004340Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004341\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004342 mode: mode of process creation\n\
4343 path: path of executable file\n\
4344 args: tuple or list of arguments\n\
4345 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004346
4347static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004348posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004349{
Victor Stinner8c62be82010-05-06 00:08:46 +00004350 PyObject *opath;
4351 char *path;
4352 PyObject *argv, *env;
4353 char **argvlist;
4354 char **envlist;
4355 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004356 int mode;
4357 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004358 Py_intptr_t spawnval;
4359 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4360 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004361
Victor Stinner8c62be82010-05-06 00:08:46 +00004362 /* spawnve has four arguments: (mode, path, argv, env), where
4363 argv is a list or tuple of strings and env is a dictionary
4364 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004365
Victor Stinner8c62be82010-05-06 00:08:46 +00004366 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4367 PyUnicode_FSConverter,
4368 &opath, &argv, &env))
4369 return NULL;
4370 path = PyBytes_AsString(opath);
4371 if (PyList_Check(argv)) {
4372 argc = PyList_Size(argv);
4373 getitem = PyList_GetItem;
4374 }
4375 else if (PyTuple_Check(argv)) {
4376 argc = PyTuple_Size(argv);
4377 getitem = PyTuple_GetItem;
4378 }
4379 else {
4380 PyErr_SetString(PyExc_TypeError,
4381 "spawnve() arg 2 must be a tuple or list");
4382 goto fail_0;
4383 }
4384 if (!PyMapping_Check(env)) {
4385 PyErr_SetString(PyExc_TypeError,
4386 "spawnve() arg 3 must be a mapping object");
4387 goto fail_0;
4388 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004389
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 argvlist = PyMem_NEW(char *, argc+1);
4391 if (argvlist == NULL) {
4392 PyErr_NoMemory();
4393 goto fail_0;
4394 }
4395 for (i = 0; i < argc; i++) {
4396 if (!fsconvert_strdup((*getitem)(argv, i),
4397 &argvlist[i]))
4398 {
4399 lastarg = i;
4400 goto fail_1;
4401 }
4402 }
4403 lastarg = argc;
4404 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004405
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 envlist = parse_envlist(env, &envc);
4407 if (envlist == NULL)
4408 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004409
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004410#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004411 Py_BEGIN_ALLOW_THREADS
4412 spawnval = spawnve(mode, path, argvlist, envlist);
4413 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004414#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004415 if (mode == _OLD_P_OVERLAY)
4416 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004417
Victor Stinner8c62be82010-05-06 00:08:46 +00004418 Py_BEGIN_ALLOW_THREADS
4419 spawnval = _spawnve(mode, path, argvlist, envlist);
4420 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004421#endif
Tim Peters25059d32001-12-07 20:35:43 +00004422
Victor Stinner8c62be82010-05-06 00:08:46 +00004423 if (spawnval == -1)
4424 (void) posix_error();
4425 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004426#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004427 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004428#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004429 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004430#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004431
Victor Stinner8c62be82010-05-06 00:08:46 +00004432 while (--envc >= 0)
4433 PyMem_DEL(envlist[envc]);
4434 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004435 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004436 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004437 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004438 Py_DECREF(opath);
4439 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004440}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004441
4442/* OS/2 supports spawnvp & spawnvpe natively */
4443#if defined(PYOS_OS2)
4444PyDoc_STRVAR(posix_spawnvp__doc__,
4445"spawnvp(mode, file, args)\n\n\
4446Execute the program 'file' in a new process, using the environment\n\
4447search path to find the file.\n\
4448\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004449 mode: mode of process creation\n\
4450 file: executable file name\n\
4451 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004452
4453static PyObject *
4454posix_spawnvp(PyObject *self, PyObject *args)
4455{
Victor Stinner8c62be82010-05-06 00:08:46 +00004456 PyObject *opath;
4457 char *path;
4458 PyObject *argv;
4459 char **argvlist;
4460 int mode, i, argc;
4461 Py_intptr_t spawnval;
4462 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004463
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 /* spawnvp has three arguments: (mode, path, argv), where
4465 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004466
Victor Stinner8c62be82010-05-06 00:08:46 +00004467 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4468 PyUnicode_FSConverter,
4469 &opath, &argv))
4470 return NULL;
4471 path = PyBytes_AsString(opath);
4472 if (PyList_Check(argv)) {
4473 argc = PyList_Size(argv);
4474 getitem = PyList_GetItem;
4475 }
4476 else if (PyTuple_Check(argv)) {
4477 argc = PyTuple_Size(argv);
4478 getitem = PyTuple_GetItem;
4479 }
4480 else {
4481 PyErr_SetString(PyExc_TypeError,
4482 "spawnvp() arg 2 must be a tuple or list");
4483 Py_DECREF(opath);
4484 return NULL;
4485 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004486
Victor Stinner8c62be82010-05-06 00:08:46 +00004487 argvlist = PyMem_NEW(char *, argc+1);
4488 if (argvlist == NULL) {
4489 Py_DECREF(opath);
4490 return PyErr_NoMemory();
4491 }
4492 for (i = 0; i < argc; i++) {
4493 if (!fsconvert_strdup((*getitem)(argv, i),
4494 &argvlist[i])) {
4495 free_string_array(argvlist, i);
4496 PyErr_SetString(
4497 PyExc_TypeError,
4498 "spawnvp() arg 2 must contain only strings");
4499 Py_DECREF(opath);
4500 return NULL;
4501 }
4502 }
4503 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004504
Victor Stinner8c62be82010-05-06 00:08:46 +00004505 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004506#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004507 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004508#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004509 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004510#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004511 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004512
Victor Stinner8c62be82010-05-06 00:08:46 +00004513 free_string_array(argvlist, argc);
4514 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004515
Victor Stinner8c62be82010-05-06 00:08:46 +00004516 if (spawnval == -1)
4517 return posix_error();
4518 else
4519 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004520}
4521
4522
4523PyDoc_STRVAR(posix_spawnvpe__doc__,
4524"spawnvpe(mode, file, args, env)\n\n\
4525Execute the program 'file' in a new process, using the environment\n\
4526search path to find the file.\n\
4527\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004528 mode: mode of process creation\n\
4529 file: executable file name\n\
4530 args: tuple or list of arguments\n\
4531 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004532
4533static PyObject *
4534posix_spawnvpe(PyObject *self, PyObject *args)
4535{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02004536 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004537 char *path;
4538 PyObject *argv, *env;
4539 char **argvlist;
4540 char **envlist;
4541 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004542 int mode;
4543 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004544 Py_intptr_t spawnval;
4545 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4546 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004547
Victor Stinner8c62be82010-05-06 00:08:46 +00004548 /* spawnvpe has four arguments: (mode, path, argv, env), where
4549 argv is a list or tuple of strings and env is a dictionary
4550 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004551
Victor Stinner8c62be82010-05-06 00:08:46 +00004552 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4553 PyUnicode_FSConverter,
4554 &opath, &argv, &env))
4555 return NULL;
4556 path = PyBytes_AsString(opath);
4557 if (PyList_Check(argv)) {
4558 argc = PyList_Size(argv);
4559 getitem = PyList_GetItem;
4560 }
4561 else if (PyTuple_Check(argv)) {
4562 argc = PyTuple_Size(argv);
4563 getitem = PyTuple_GetItem;
4564 }
4565 else {
4566 PyErr_SetString(PyExc_TypeError,
4567 "spawnvpe() arg 2 must be a tuple or list");
4568 goto fail_0;
4569 }
4570 if (!PyMapping_Check(env)) {
4571 PyErr_SetString(PyExc_TypeError,
4572 "spawnvpe() arg 3 must be a mapping object");
4573 goto fail_0;
4574 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004575
Victor Stinner8c62be82010-05-06 00:08:46 +00004576 argvlist = PyMem_NEW(char *, argc+1);
4577 if (argvlist == NULL) {
4578 PyErr_NoMemory();
4579 goto fail_0;
4580 }
4581 for (i = 0; i < argc; i++) {
4582 if (!fsconvert_strdup((*getitem)(argv, i),
4583 &argvlist[i]))
4584 {
4585 lastarg = i;
4586 goto fail_1;
4587 }
4588 }
4589 lastarg = argc;
4590 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004591
Victor Stinner8c62be82010-05-06 00:08:46 +00004592 envlist = parse_envlist(env, &envc);
4593 if (envlist == NULL)
4594 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004595
Victor Stinner8c62be82010-05-06 00:08:46 +00004596 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004597#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004598 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004599#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004600 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004601#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004602 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004603
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 if (spawnval == -1)
4605 (void) posix_error();
4606 else
4607 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004608
Victor Stinner8c62be82010-05-06 00:08:46 +00004609 while (--envc >= 0)
4610 PyMem_DEL(envlist[envc]);
4611 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004612 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004613 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004614 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004615 Py_DECREF(opath);
4616 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004617}
4618#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004619#endif /* HAVE_SPAWNV */
4620
4621
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004622#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004623PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004624"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004625Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4626\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004628
4629static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004630posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004631{
Victor Stinner8c62be82010-05-06 00:08:46 +00004632 pid_t pid;
4633 int result = 0;
4634 _PyImport_AcquireLock();
4635 pid = fork1();
4636 if (pid == 0) {
4637 /* child: this clobbers and resets the import lock. */
4638 PyOS_AfterFork();
4639 } else {
4640 /* parent: release the import lock. */
4641 result = _PyImport_ReleaseLock();
4642 }
4643 if (pid == -1)
4644 return posix_error();
4645 if (result < 0) {
4646 /* Don't clobber the OSError if the fork failed. */
4647 PyErr_SetString(PyExc_RuntimeError,
4648 "not holding the import lock");
4649 return NULL;
4650 }
4651 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004652}
4653#endif
4654
4655
Guido van Rossumad0ee831995-03-01 10:34:45 +00004656#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004657PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004658"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004659Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004660Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004661
Barry Warsaw53699e91996-12-10 23:23:01 +00004662static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004663posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004664{
Victor Stinner8c62be82010-05-06 00:08:46 +00004665 pid_t pid;
4666 int result = 0;
4667 _PyImport_AcquireLock();
4668 pid = fork();
4669 if (pid == 0) {
4670 /* child: this clobbers and resets the import lock. */
4671 PyOS_AfterFork();
4672 } else {
4673 /* parent: release the import lock. */
4674 result = _PyImport_ReleaseLock();
4675 }
4676 if (pid == -1)
4677 return posix_error();
4678 if (result < 0) {
4679 /* Don't clobber the OSError if the fork failed. */
4680 PyErr_SetString(PyExc_RuntimeError,
4681 "not holding the import lock");
4682 return NULL;
4683 }
4684 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004685}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004686#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004687
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004688#ifdef HAVE_SCHED_H
4689
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004690#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4691
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004692PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4693"sched_get_priority_max(policy)\n\n\
4694Get the maximum scheduling priority for *policy*.");
4695
4696static PyObject *
4697posix_sched_get_priority_max(PyObject *self, PyObject *args)
4698{
4699 int policy, max;
4700
4701 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4702 return NULL;
4703 max = sched_get_priority_max(policy);
4704 if (max < 0)
4705 return posix_error();
4706 return PyLong_FromLong(max);
4707}
4708
4709PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4710"sched_get_priority_min(policy)\n\n\
4711Get the minimum scheduling priority for *policy*.");
4712
4713static PyObject *
4714posix_sched_get_priority_min(PyObject *self, PyObject *args)
4715{
4716 int policy, min;
4717
4718 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4719 return NULL;
4720 min = sched_get_priority_min(policy);
4721 if (min < 0)
4722 return posix_error();
4723 return PyLong_FromLong(min);
4724}
4725
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004726#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4727
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004728#ifdef HAVE_SCHED_SETSCHEDULER
4729
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004730PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4731"sched_getscheduler(pid)\n\n\
4732Get the scheduling policy for the process with a PID of *pid*.\n\
4733Passing a PID of 0 returns the scheduling policy for the calling process.");
4734
4735static PyObject *
4736posix_sched_getscheduler(PyObject *self, PyObject *args)
4737{
4738 pid_t pid;
4739 int policy;
4740
4741 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4742 return NULL;
4743 policy = sched_getscheduler(pid);
4744 if (policy < 0)
4745 return posix_error();
4746 return PyLong_FromLong(policy);
4747}
4748
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004749#endif
4750
4751#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4752
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004753static PyObject *
4754sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4755{
4756 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004757 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004758
4759 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4760 return NULL;
4761 res = PyStructSequence_New(type);
4762 if (!res)
4763 return NULL;
4764 Py_INCREF(priority);
4765 PyStructSequence_SET_ITEM(res, 0, priority);
4766 return res;
4767}
4768
4769PyDoc_STRVAR(sched_param__doc__,
4770"sched_param(sched_priority): A scheduling parameter.\n\n\
4771Current has only one field: sched_priority");
4772
4773static PyStructSequence_Field sched_param_fields[] = {
4774 {"sched_priority", "the scheduling priority"},
4775 {0}
4776};
4777
4778static PyStructSequence_Desc sched_param_desc = {
4779 "sched_param", /* name */
4780 sched_param__doc__, /* doc */
4781 sched_param_fields,
4782 1
4783};
4784
4785static int
4786convert_sched_param(PyObject *param, struct sched_param *res)
4787{
4788 long priority;
4789
4790 if (Py_TYPE(param) != &SchedParamType) {
4791 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4792 return 0;
4793 }
4794 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4795 if (priority == -1 && PyErr_Occurred())
4796 return 0;
4797 if (priority > INT_MAX || priority < INT_MIN) {
4798 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4799 return 0;
4800 }
4801 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4802 return 1;
4803}
4804
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004805#endif
4806
4807#ifdef HAVE_SCHED_SETSCHEDULER
4808
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004809PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4810"sched_setscheduler(pid, policy, param)\n\n\
4811Set the scheduling policy, *policy*, for *pid*.\n\
4812If *pid* is 0, the calling process is changed.\n\
4813*param* is an instance of sched_param.");
4814
4815static PyObject *
4816posix_sched_setscheduler(PyObject *self, PyObject *args)
4817{
4818 pid_t pid;
4819 int policy;
4820 struct sched_param param;
4821
4822 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4823 &pid, &policy, &convert_sched_param, &param))
4824 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02004825
4826 /*
Jesus Cea54b01492011-09-10 01:53:19 +02004827 ** sched_setscheduler() returns 0 in Linux, but the previous
4828 ** scheduling policy under Solaris/Illumos, and others.
4829 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02004830 */
4831 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004832 return posix_error();
4833 Py_RETURN_NONE;
4834}
4835
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004836#endif
4837
4838#ifdef HAVE_SCHED_SETPARAM
4839
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004840PyDoc_STRVAR(posix_sched_getparam__doc__,
4841"sched_getparam(pid) -> sched_param\n\n\
4842Returns scheduling parameters for the process with *pid* as an instance of the\n\
4843sched_param class. A PID of 0 means the calling process.");
4844
4845static PyObject *
4846posix_sched_getparam(PyObject *self, PyObject *args)
4847{
4848 pid_t pid;
4849 struct sched_param param;
4850 PyObject *res, *priority;
4851
4852 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4853 return NULL;
4854 if (sched_getparam(pid, &param))
4855 return posix_error();
4856 res = PyStructSequence_New(&SchedParamType);
4857 if (!res)
4858 return NULL;
4859 priority = PyLong_FromLong(param.sched_priority);
4860 if (!priority) {
4861 Py_DECREF(res);
4862 return NULL;
4863 }
4864 PyStructSequence_SET_ITEM(res, 0, priority);
4865 return res;
4866}
4867
4868PyDoc_STRVAR(posix_sched_setparam__doc__,
4869"sched_setparam(pid, param)\n\n\
4870Set scheduling parameters for a process with PID *pid*.\n\
4871A PID of 0 means the calling process.");
4872
4873static PyObject *
4874posix_sched_setparam(PyObject *self, PyObject *args)
4875{
4876 pid_t pid;
4877 struct sched_param param;
4878
4879 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4880 &pid, &convert_sched_param, &param))
4881 return NULL;
4882 if (sched_setparam(pid, &param))
4883 return posix_error();
4884 Py_RETURN_NONE;
4885}
4886
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004887#endif
4888
4889#ifdef HAVE_SCHED_RR_GET_INTERVAL
4890
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004891PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4892"sched_rr_get_interval(pid) -> float\n\n\
4893Return the round-robin quantum for the process with PID *pid* in seconds.");
4894
4895static PyObject *
4896posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4897{
4898 pid_t pid;
4899 struct timespec interval;
4900
4901 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4902 return NULL;
4903 if (sched_rr_get_interval(pid, &interval))
4904 return posix_error();
4905 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4906}
4907
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004908#endif
4909
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004910PyDoc_STRVAR(posix_sched_yield__doc__,
4911"sched_yield()\n\n\
4912Voluntarily relinquish the CPU.");
4913
4914static PyObject *
4915posix_sched_yield(PyObject *self, PyObject *noargs)
4916{
4917 if (sched_yield())
4918 return posix_error();
4919 Py_RETURN_NONE;
4920}
4921
Benjamin Peterson2740af82011-08-02 17:41:34 -05004922#ifdef HAVE_SCHED_SETAFFINITY
4923
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004924typedef struct {
4925 PyObject_HEAD;
4926 Py_ssize_t size;
4927 int ncpus;
4928 cpu_set_t *set;
4929} Py_cpu_set;
4930
4931static PyTypeObject cpu_set_type;
4932
4933static void
4934cpu_set_dealloc(Py_cpu_set *set)
4935{
4936 assert(set->set);
4937 CPU_FREE(set->set);
4938 Py_TYPE(set)->tp_free(set);
4939}
4940
4941static Py_cpu_set *
4942make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4943{
4944 Py_cpu_set *set;
4945
4946 if (size < 0) {
4947 PyErr_SetString(PyExc_ValueError, "negative size");
4948 return NULL;
4949 }
4950 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4951 if (!set)
4952 return NULL;
4953 set->ncpus = size;
4954 set->size = CPU_ALLOC_SIZE(size);
4955 set->set = CPU_ALLOC(size);
4956 if (!set->set) {
4957 type->tp_free(set);
4958 PyErr_NoMemory();
4959 return NULL;
4960 }
4961 CPU_ZERO_S(set->size, set->set);
4962 return set;
4963}
4964
4965static PyObject *
4966cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4967{
4968 int size;
4969
4970 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4971 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4972 return NULL;
4973 return (PyObject *)make_new_cpu_set(type, size);
4974}
4975
4976static PyObject *
4977cpu_set_repr(Py_cpu_set *set)
4978{
4979 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
Victor Stinner63941882011-09-29 00:42:28 +02004980}
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004981
4982static Py_ssize_t
4983cpu_set_len(Py_cpu_set *set)
4984{
4985 return set->ncpus;
4986}
4987
4988static int
4989_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4990{
4991 int cpu;
4992 if (!PyArg_ParseTuple(args, requester, &cpu))
4993 return -1;
4994 if (cpu < 0) {
4995 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4996 return -1;
4997 }
4998 if (cpu >= set->ncpus) {
4999 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
5000 return -1;
5001 }
5002 return cpu;
5003}
5004
5005PyDoc_STRVAR(cpu_set_set_doc,
5006"cpu_set.set(i)\n\n\
5007Add CPU *i* to the set.");
5008
5009static PyObject *
5010cpu_set_set(Py_cpu_set *set, PyObject *args)
5011{
5012 int cpu = _get_cpu(set, "i|set", args);
5013 if (cpu == -1)
5014 return NULL;
5015 CPU_SET_S(cpu, set->size, set->set);
5016 Py_RETURN_NONE;
5017}
5018
5019PyDoc_STRVAR(cpu_set_count_doc,
5020"cpu_set.count() -> int\n\n\
5021Return the number of CPUs active in the set.");
5022
5023static PyObject *
5024cpu_set_count(Py_cpu_set *set, PyObject *noargs)
5025{
5026 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
5027}
5028
5029PyDoc_STRVAR(cpu_set_clear_doc,
5030"cpu_set.clear(i)\n\n\
5031Remove CPU *i* from the set.");
5032
5033static PyObject *
5034cpu_set_clear(Py_cpu_set *set, PyObject *args)
5035{
5036 int cpu = _get_cpu(set, "i|clear", args);
5037 if (cpu == -1)
5038 return NULL;
5039 CPU_CLR_S(cpu, set->size, set->set);
5040 Py_RETURN_NONE;
5041}
5042
5043PyDoc_STRVAR(cpu_set_isset_doc,
5044"cpu_set.isset(i) -> bool\n\n\
5045Test if CPU *i* is in the set.");
5046
5047static PyObject *
5048cpu_set_isset(Py_cpu_set *set, PyObject *args)
5049{
5050 int cpu = _get_cpu(set, "i|isset", args);
5051 if (cpu == -1)
5052 return NULL;
5053 if (CPU_ISSET_S(cpu, set->size, set->set))
5054 Py_RETURN_TRUE;
5055 Py_RETURN_FALSE;
5056}
5057
5058PyDoc_STRVAR(cpu_set_zero_doc,
5059"cpu_set.zero()\n\n\
5060Clear the cpu_set.");
5061
5062static PyObject *
5063cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
5064{
5065 CPU_ZERO_S(set->size, set->set);
5066 Py_RETURN_NONE;
5067}
5068
5069static PyObject *
5070cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
5071{
5072 int eq;
5073
Brian Curtindfc80e32011-08-10 20:28:54 -05005074 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
5075 Py_RETURN_NOTIMPLEMENTED;
5076
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005077 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
5078 if ((op == Py_EQ) ? eq : !eq)
5079 Py_RETURN_TRUE;
5080 else
5081 Py_RETURN_FALSE;
5082}
5083
5084#define CPU_SET_BINOP(name, op) \
5085 static PyObject * \
5086 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5087 if (res) { \
5088 Py_INCREF(res); \
5089 } \
5090 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005091 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005092 if (!res) \
5093 return NULL; \
5094 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005095 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005096 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005097 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005098 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005099 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005100 op(res->size, res->set, left->set, right->set); \
5101 return (PyObject *)res; \
5102 } \
5103 static PyObject * \
5104 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5105 return do_cpu_set_##name(left, right, NULL); \
5106 } \
5107 static PyObject * \
5108 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5109 return do_cpu_set_##name(left, right, left); \
5110 } \
5111
5112CPU_SET_BINOP(and, CPU_AND_S)
5113CPU_SET_BINOP(or, CPU_OR_S)
5114CPU_SET_BINOP(xor, CPU_XOR_S)
5115#undef CPU_SET_BINOP
5116
5117PyDoc_STRVAR(cpu_set_doc,
5118"cpu_set(size)\n\n\
5119Create an empty mask of CPUs.");
5120
5121static PyNumberMethods cpu_set_as_number = {
5122 0, /*nb_add*/
5123 0, /*nb_subtract*/
5124 0, /*nb_multiply*/
5125 0, /*nb_remainder*/
5126 0, /*nb_divmod*/
5127 0, /*nb_power*/
5128 0, /*nb_negative*/
5129 0, /*nb_positive*/
5130 0, /*nb_absolute*/
5131 0, /*nb_bool*/
5132 0, /*nb_invert*/
5133 0, /*nb_lshift*/
5134 0, /*nb_rshift*/
5135 (binaryfunc)cpu_set_and, /*nb_and*/
5136 (binaryfunc)cpu_set_xor, /*nb_xor*/
5137 (binaryfunc)cpu_set_or, /*nb_or*/
5138 0, /*nb_int*/
5139 0, /*nb_reserved*/
5140 0, /*nb_float*/
5141 0, /*nb_inplace_add*/
5142 0, /*nb_inplace_subtract*/
5143 0, /*nb_inplace_multiply*/
5144 0, /*nb_inplace_remainder*/
5145 0, /*nb_inplace_power*/
5146 0, /*nb_inplace_lshift*/
5147 0, /*nb_inplace_rshift*/
5148 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5149 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5150 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5151};
5152
5153static PySequenceMethods cpu_set_as_sequence = {
5154 (lenfunc)cpu_set_len, /* sq_length */
5155};
5156
5157static PyMethodDef cpu_set_methods[] = {
5158 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5159 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5160 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5161 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5162 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5163 {NULL, NULL} /* sentinel */
5164};
5165
5166static PyTypeObject cpu_set_type = {
5167 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5168 "posix.cpu_set", /* tp_name */
5169 sizeof(Py_cpu_set), /* tp_basicsize */
5170 0, /* tp_itemsize */
5171 /* methods */
5172 (destructor)cpu_set_dealloc, /* tp_dealloc */
5173 0, /* tp_print */
5174 0, /* tp_getattr */
5175 0, /* tp_setattr */
5176 0, /* tp_reserved */
5177 (reprfunc)cpu_set_repr, /* tp_repr */
5178 &cpu_set_as_number, /* tp_as_number */
5179 &cpu_set_as_sequence, /* tp_as_sequence */
5180 0, /* tp_as_mapping */
5181 PyObject_HashNotImplemented, /* tp_hash */
5182 0, /* tp_call */
5183 0, /* tp_str */
5184 PyObject_GenericGetAttr, /* tp_getattro */
5185 0, /* tp_setattro */
5186 0, /* tp_as_buffer */
5187 Py_TPFLAGS_DEFAULT, /* tp_flags */
5188 cpu_set_doc, /* tp_doc */
5189 0, /* tp_traverse */
5190 0, /* tp_clear */
5191 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5192 0, /* tp_weaklistoffset */
5193 0, /* tp_iter */
5194 0, /* tp_iternext */
5195 cpu_set_methods, /* tp_methods */
5196 0, /* tp_members */
5197 0, /* tp_getset */
5198 0, /* tp_base */
5199 0, /* tp_dict */
5200 0, /* tp_descr_get */
5201 0, /* tp_descr_set */
5202 0, /* tp_dictoffset */
5203 0, /* tp_init */
5204 PyType_GenericAlloc, /* tp_alloc */
5205 cpu_set_new, /* tp_new */
5206 PyObject_Del, /* tp_free */
5207};
5208
5209PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5210"sched_setaffinity(pid, cpu_set)\n\n\
5211Set the affinity of the process with PID *pid* to *cpu_set*.");
5212
5213static PyObject *
5214posix_sched_setaffinity(PyObject *self, PyObject *args)
5215{
5216 pid_t pid;
5217 Py_cpu_set *cpu_set;
5218
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005219 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005220 &pid, &cpu_set_type, &cpu_set))
5221 return NULL;
5222 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5223 return posix_error();
5224 Py_RETURN_NONE;
5225}
5226
5227PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5228"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5229Return the affinity of the process with PID *pid*.\n\
5230The returned cpu_set will be of size *ncpus*.");
5231
5232static PyObject *
5233posix_sched_getaffinity(PyObject *self, PyObject *args)
5234{
5235 pid_t pid;
5236 int ncpus;
5237 Py_cpu_set *res;
5238
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005239 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005240 &pid, &ncpus))
5241 return NULL;
5242 res = make_new_cpu_set(&cpu_set_type, ncpus);
5243 if (!res)
5244 return NULL;
5245 if (sched_getaffinity(pid, res->size, res->set)) {
5246 Py_DECREF(res);
5247 return posix_error();
5248 }
5249 return (PyObject *)res;
5250}
5251
Benjamin Peterson2740af82011-08-02 17:41:34 -05005252#endif /* HAVE_SCHED_SETAFFINITY */
5253
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005254#endif /* HAVE_SCHED_H */
5255
Neal Norwitzb59798b2003-03-21 01:43:31 +00005256/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005257/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5258#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005259#define DEV_PTY_FILE "/dev/ptc"
5260#define HAVE_DEV_PTMX
5261#else
5262#define DEV_PTY_FILE "/dev/ptmx"
5263#endif
5264
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005265#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005266#ifdef HAVE_PTY_H
5267#include <pty.h>
5268#else
5269#ifdef HAVE_LIBUTIL_H
5270#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005271#else
5272#ifdef HAVE_UTIL_H
5273#include <util.h>
5274#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005275#endif /* HAVE_LIBUTIL_H */
5276#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005277#ifdef HAVE_STROPTS_H
5278#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005279#endif
5280#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005281
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005282#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005283PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005284"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005285Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005286
5287static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005288posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005289{
Victor Stinner8c62be82010-05-06 00:08:46 +00005290 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005291#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005292 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005293#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005294#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005295 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005296#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005297 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005298#endif
5299#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005300
Thomas Wouters70c21a12000-07-14 14:28:33 +00005301#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005302 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5303 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005304#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005305 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5306 if (slave_name == NULL)
5307 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005308
Victor Stinner8c62be82010-05-06 00:08:46 +00005309 slave_fd = open(slave_name, O_RDWR);
5310 if (slave_fd < 0)
5311 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005312#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005313 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5314 if (master_fd < 0)
5315 return posix_error();
5316 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5317 /* change permission of slave */
5318 if (grantpt(master_fd) < 0) {
5319 PyOS_setsig(SIGCHLD, sig_saved);
5320 return posix_error();
5321 }
5322 /* unlock slave */
5323 if (unlockpt(master_fd) < 0) {
5324 PyOS_setsig(SIGCHLD, sig_saved);
5325 return posix_error();
5326 }
5327 PyOS_setsig(SIGCHLD, sig_saved);
5328 slave_name = ptsname(master_fd); /* get name of slave */
5329 if (slave_name == NULL)
5330 return posix_error();
5331 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5332 if (slave_fd < 0)
5333 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005334#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005335 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5336 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005337#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005338 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005339#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005340#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005341#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005342
Victor Stinner8c62be82010-05-06 00:08:46 +00005343 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005344
Fred Drake8cef4cf2000-06-28 16:40:38 +00005345}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005346#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005347
5348#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005349PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005350"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005351Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5352Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005353To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005354
5355static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005356posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005357{
Victor Stinner8c62be82010-05-06 00:08:46 +00005358 int master_fd = -1, result = 0;
5359 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005360
Victor Stinner8c62be82010-05-06 00:08:46 +00005361 _PyImport_AcquireLock();
5362 pid = forkpty(&master_fd, NULL, NULL, NULL);
5363 if (pid == 0) {
5364 /* child: this clobbers and resets the import lock. */
5365 PyOS_AfterFork();
5366 } else {
5367 /* parent: release the import lock. */
5368 result = _PyImport_ReleaseLock();
5369 }
5370 if (pid == -1)
5371 return posix_error();
5372 if (result < 0) {
5373 /* Don't clobber the OSError if the fork failed. */
5374 PyErr_SetString(PyExc_RuntimeError,
5375 "not holding the import lock");
5376 return NULL;
5377 }
5378 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005379}
5380#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005381
Ross Lagerwall7807c352011-03-17 20:20:30 +02005382
Guido van Rossumad0ee831995-03-01 10:34:45 +00005383#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005384PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005385"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005386Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005387
Barry Warsaw53699e91996-12-10 23:23:01 +00005388static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005389posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005390{
Victor Stinner8c62be82010-05-06 00:08:46 +00005391 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005392}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005393#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005394
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005395
Guido van Rossumad0ee831995-03-01 10:34:45 +00005396#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005397PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005398"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005399Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005400
Barry Warsaw53699e91996-12-10 23:23:01 +00005401static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005402posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005403{
Victor Stinner8c62be82010-05-06 00:08:46 +00005404 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005405}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005406#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005407
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005408
Guido van Rossumad0ee831995-03-01 10:34:45 +00005409#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005410PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005411"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005412Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005413
Barry Warsaw53699e91996-12-10 23:23:01 +00005414static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005415posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005416{
Victor Stinner8c62be82010-05-06 00:08:46 +00005417 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005418}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005419#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005420
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005422PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005423"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005424Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005425
Barry Warsaw53699e91996-12-10 23:23:01 +00005426static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005427posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005428{
Victor Stinner8c62be82010-05-06 00:08:46 +00005429 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005430}
5431
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005432#ifdef HAVE_GETGROUPLIST
5433PyDoc_STRVAR(posix_getgrouplist__doc__,
5434"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5435Returns a list of groups to which a user belongs.\n\n\
5436 user: username to lookup\n\
5437 group: base group id of the user");
5438
5439static PyObject *
5440posix_getgrouplist(PyObject *self, PyObject *args)
5441{
5442#ifdef NGROUPS_MAX
5443#define MAX_GROUPS NGROUPS_MAX
5444#else
5445 /* defined to be 16 on Solaris7, so this should be a small number */
5446#define MAX_GROUPS 64
5447#endif
5448
5449 const char *user;
5450 int i, ngroups;
5451 PyObject *list;
5452#ifdef __APPLE__
5453 int *groups, basegid;
5454#else
5455 gid_t *groups, basegid;
5456#endif
5457 ngroups = MAX_GROUPS;
5458
5459 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5460 return NULL;
5461
5462#ifdef __APPLE__
5463 groups = PyMem_Malloc(ngroups * sizeof(int));
5464#else
5465 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5466#endif
5467 if (groups == NULL)
5468 return PyErr_NoMemory();
5469
5470 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5471 PyMem_Del(groups);
5472 return posix_error();
5473 }
5474
5475 list = PyList_New(ngroups);
5476 if (list == NULL) {
5477 PyMem_Del(groups);
5478 return NULL;
5479 }
5480
5481 for (i = 0; i < ngroups; i++) {
5482 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5483 if (o == NULL) {
5484 Py_DECREF(list);
5485 PyMem_Del(groups);
5486 return NULL;
5487 }
5488 PyList_SET_ITEM(list, i, o);
5489 }
5490
5491 PyMem_Del(groups);
5492
5493 return list;
5494}
5495#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005496
Fred Drakec9680921999-12-13 16:37:25 +00005497#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005498PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005499"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005501
5502static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005503posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005504{
5505 PyObject *result = NULL;
5506
Fred Drakec9680921999-12-13 16:37:25 +00005507#ifdef NGROUPS_MAX
5508#define MAX_GROUPS NGROUPS_MAX
5509#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005510 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005511#define MAX_GROUPS 64
5512#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005513 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005514
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005515 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005516 * This is a helper variable to store the intermediate result when
5517 * that happens.
5518 *
5519 * To keep the code readable the OSX behaviour is unconditional,
5520 * according to the POSIX spec this should be safe on all unix-y
5521 * systems.
5522 */
5523 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005524 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005525
Victor Stinner8c62be82010-05-06 00:08:46 +00005526 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005527 if (n < 0) {
5528 if (errno == EINVAL) {
5529 n = getgroups(0, NULL);
5530 if (n == -1) {
5531 return posix_error();
5532 }
5533 if (n == 0) {
5534 /* Avoid malloc(0) */
5535 alt_grouplist = grouplist;
5536 } else {
5537 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5538 if (alt_grouplist == NULL) {
5539 errno = EINVAL;
5540 return posix_error();
5541 }
5542 n = getgroups(n, alt_grouplist);
5543 if (n == -1) {
5544 PyMem_Free(alt_grouplist);
5545 return posix_error();
5546 }
5547 }
5548 } else {
5549 return posix_error();
5550 }
5551 }
5552 result = PyList_New(n);
5553 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005554 int i;
5555 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005556 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005557 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005558 Py_DECREF(result);
5559 result = NULL;
5560 break;
Fred Drakec9680921999-12-13 16:37:25 +00005561 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005562 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005563 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005564 }
5565
5566 if (alt_grouplist != grouplist) {
5567 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005568 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005569
Fred Drakec9680921999-12-13 16:37:25 +00005570 return result;
5571}
5572#endif
5573
Antoine Pitroub7572f02009-12-02 20:46:48 +00005574#ifdef HAVE_INITGROUPS
5575PyDoc_STRVAR(posix_initgroups__doc__,
5576"initgroups(username, gid) -> None\n\n\
5577Call the system initgroups() to initialize the group access list with all of\n\
5578the groups of which the specified username is a member, plus the specified\n\
5579group id.");
5580
5581static PyObject *
5582posix_initgroups(PyObject *self, PyObject *args)
5583{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005584 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005585 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005586 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005587 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005588
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005589 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5590 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005591 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005592 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005593
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005594 res = initgroups(username, (gid_t) gid);
5595 Py_DECREF(oname);
5596 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005597 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005598
Victor Stinner8c62be82010-05-06 00:08:46 +00005599 Py_INCREF(Py_None);
5600 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005601}
5602#endif
5603
Martin v. Löwis606edc12002-06-13 21:09:11 +00005604#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005605PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005606"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005607Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005608
5609static PyObject *
5610posix_getpgid(PyObject *self, PyObject *args)
5611{
Victor Stinner8c62be82010-05-06 00:08:46 +00005612 pid_t pid, pgid;
5613 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5614 return NULL;
5615 pgid = getpgid(pid);
5616 if (pgid < 0)
5617 return posix_error();
5618 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005619}
5620#endif /* HAVE_GETPGID */
5621
5622
Guido van Rossumb6775db1994-08-01 11:34:53 +00005623#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005624PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005625"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005627
Barry Warsaw53699e91996-12-10 23:23:01 +00005628static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005629posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005630{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005631#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005633#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005634 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005635#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005636}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005637#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005638
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005639
Guido van Rossumb6775db1994-08-01 11:34:53 +00005640#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005641PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005642"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005643Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005644
Barry Warsaw53699e91996-12-10 23:23:01 +00005645static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005646posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005647{
Guido van Rossum64933891994-10-20 21:56:42 +00005648#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005650#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005651 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005652#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005653 return posix_error();
5654 Py_INCREF(Py_None);
5655 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005656}
5657
Guido van Rossumb6775db1994-08-01 11:34:53 +00005658#endif /* HAVE_SETPGRP */
5659
Guido van Rossumad0ee831995-03-01 10:34:45 +00005660#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005661
5662#ifdef MS_WINDOWS
5663#include <tlhelp32.h>
5664
5665static PyObject*
5666win32_getppid()
5667{
5668 HANDLE snapshot;
5669 pid_t mypid;
5670 PyObject* result = NULL;
5671 BOOL have_record;
5672 PROCESSENTRY32 pe;
5673
5674 mypid = getpid(); /* This function never fails */
5675
5676 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5677 if (snapshot == INVALID_HANDLE_VALUE)
5678 return PyErr_SetFromWindowsErr(GetLastError());
5679
5680 pe.dwSize = sizeof(pe);
5681 have_record = Process32First(snapshot, &pe);
5682 while (have_record) {
5683 if (mypid == (pid_t)pe.th32ProcessID) {
5684 /* We could cache the ulong value in a static variable. */
5685 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5686 break;
5687 }
5688
5689 have_record = Process32Next(snapshot, &pe);
5690 }
5691
5692 /* If our loop exits and our pid was not found (result will be NULL)
5693 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5694 * error anyway, so let's raise it. */
5695 if (!result)
5696 result = PyErr_SetFromWindowsErr(GetLastError());
5697
5698 CloseHandle(snapshot);
5699
5700 return result;
5701}
5702#endif /*MS_WINDOWS*/
5703
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005704PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005705"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005706Return the parent's process id. If the parent process has already exited,\n\
5707Windows machines will still return its id; others systems will return the id\n\
5708of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005709
Barry Warsaw53699e91996-12-10 23:23:01 +00005710static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005711posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005712{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005713#ifdef MS_WINDOWS
5714 return win32_getppid();
5715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005716 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005717#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005718}
5719#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005721
Fred Drake12c6e2d1999-12-14 21:25:03 +00005722#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005723PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005724"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005725Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005726
5727static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005728posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005729{
Victor Stinner8c62be82010-05-06 00:08:46 +00005730 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005731#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005732 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02005733 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005734
5735 if (GetUserNameW(user_name, &num_chars)) {
5736 /* num_chars is the number of unicode chars plus null terminator */
5737 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005738 }
5739 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005740 result = PyErr_SetFromWindowsErr(GetLastError());
5741#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005742 char *name;
5743 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005744
Victor Stinner8c62be82010-05-06 00:08:46 +00005745 errno = 0;
5746 name = getlogin();
5747 if (name == NULL) {
5748 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005749 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005750 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005751 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005752 }
5753 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005754 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005755 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005756#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005757 return result;
5758}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005759#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005760
Guido van Rossumad0ee831995-03-01 10:34:45 +00005761#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005762PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005763"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005764Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005765
Barry Warsaw53699e91996-12-10 23:23:01 +00005766static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005767posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005768{
Victor Stinner8c62be82010-05-06 00:08:46 +00005769 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005770}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005771#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005772
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005773
Guido van Rossumad0ee831995-03-01 10:34:45 +00005774#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005775PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005776"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005777Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005778
Barry Warsaw53699e91996-12-10 23:23:01 +00005779static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005780posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005781{
Victor Stinner8c62be82010-05-06 00:08:46 +00005782 pid_t pid;
5783 int sig;
5784 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5785 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005786#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005787 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5788 APIRET rc;
5789 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005790 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005791
5792 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5793 APIRET rc;
5794 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005795 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005796
5797 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005798 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005799#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005800 if (kill(pid, sig) == -1)
5801 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005802#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005803 Py_INCREF(Py_None);
5804 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005805}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005806#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005807
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005808#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005809PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005810"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005811Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005812
5813static PyObject *
5814posix_killpg(PyObject *self, PyObject *args)
5815{
Victor Stinner8c62be82010-05-06 00:08:46 +00005816 int sig;
5817 pid_t pgid;
5818 /* XXX some man pages make the `pgid` parameter an int, others
5819 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5820 take the same type. Moreover, pid_t is always at least as wide as
5821 int (else compilation of this module fails), which is safe. */
5822 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5823 return NULL;
5824 if (killpg(pgid, sig) == -1)
5825 return posix_error();
5826 Py_INCREF(Py_None);
5827 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005828}
5829#endif
5830
Brian Curtineb24d742010-04-12 17:16:38 +00005831#ifdef MS_WINDOWS
5832PyDoc_STRVAR(win32_kill__doc__,
5833"kill(pid, sig)\n\n\
5834Kill a process with a signal.");
5835
5836static PyObject *
5837win32_kill(PyObject *self, PyObject *args)
5838{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005839 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005840 DWORD pid, sig, err;
5841 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005842
Victor Stinner8c62be82010-05-06 00:08:46 +00005843 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5844 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005845
Victor Stinner8c62be82010-05-06 00:08:46 +00005846 /* Console processes which share a common console can be sent CTRL+C or
5847 CTRL+BREAK events, provided they handle said events. */
5848 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5849 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5850 err = GetLastError();
5851 PyErr_SetFromWindowsErr(err);
5852 }
5853 else
5854 Py_RETURN_NONE;
5855 }
Brian Curtineb24d742010-04-12 17:16:38 +00005856
Victor Stinner8c62be82010-05-06 00:08:46 +00005857 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5858 attempt to open and terminate the process. */
5859 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5860 if (handle == NULL) {
5861 err = GetLastError();
5862 return PyErr_SetFromWindowsErr(err);
5863 }
Brian Curtineb24d742010-04-12 17:16:38 +00005864
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 if (TerminateProcess(handle, sig) == 0) {
5866 err = GetLastError();
5867 result = PyErr_SetFromWindowsErr(err);
5868 } else {
5869 Py_INCREF(Py_None);
5870 result = Py_None;
5871 }
Brian Curtineb24d742010-04-12 17:16:38 +00005872
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 CloseHandle(handle);
5874 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005875}
5876#endif /* MS_WINDOWS */
5877
Guido van Rossumc0125471996-06-28 18:55:32 +00005878#ifdef HAVE_PLOCK
5879
5880#ifdef HAVE_SYS_LOCK_H
5881#include <sys/lock.h>
5882#endif
5883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005884PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005885"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005886Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005887
Barry Warsaw53699e91996-12-10 23:23:01 +00005888static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005889posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005890{
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 int op;
5892 if (!PyArg_ParseTuple(args, "i:plock", &op))
5893 return NULL;
5894 if (plock(op) == -1)
5895 return posix_error();
5896 Py_INCREF(Py_None);
5897 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005898}
5899#endif
5900
Guido van Rossumb6775db1994-08-01 11:34:53 +00005901#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005902PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005903"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005904Set the current process's user id.");
5905
Barry Warsaw53699e91996-12-10 23:23:01 +00005906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005907posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005908{
Victor Stinner8c62be82010-05-06 00:08:46 +00005909 long uid_arg;
5910 uid_t uid;
5911 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5912 return NULL;
5913 uid = uid_arg;
5914 if (uid != uid_arg) {
5915 PyErr_SetString(PyExc_OverflowError, "user id too big");
5916 return NULL;
5917 }
5918 if (setuid(uid) < 0)
5919 return posix_error();
5920 Py_INCREF(Py_None);
5921 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005922}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005923#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005924
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005925
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005926#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005928"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005929Set the current process's effective user id.");
5930
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005931static PyObject *
5932posix_seteuid (PyObject *self, PyObject *args)
5933{
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 long euid_arg;
5935 uid_t euid;
5936 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5937 return NULL;
5938 euid = euid_arg;
5939 if (euid != euid_arg) {
5940 PyErr_SetString(PyExc_OverflowError, "user id too big");
5941 return NULL;
5942 }
5943 if (seteuid(euid) < 0) {
5944 return posix_error();
5945 } else {
5946 Py_INCREF(Py_None);
5947 return Py_None;
5948 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005949}
5950#endif /* HAVE_SETEUID */
5951
5952#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005953PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005954"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005955Set the current process's effective group id.");
5956
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005957static PyObject *
5958posix_setegid (PyObject *self, PyObject *args)
5959{
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 long egid_arg;
5961 gid_t egid;
5962 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5963 return NULL;
5964 egid = egid_arg;
5965 if (egid != egid_arg) {
5966 PyErr_SetString(PyExc_OverflowError, "group id too big");
5967 return NULL;
5968 }
5969 if (setegid(egid) < 0) {
5970 return posix_error();
5971 } else {
5972 Py_INCREF(Py_None);
5973 return Py_None;
5974 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005975}
5976#endif /* HAVE_SETEGID */
5977
5978#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005979PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005980"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005981Set the current process's real and effective user ids.");
5982
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005983static PyObject *
5984posix_setreuid (PyObject *self, PyObject *args)
5985{
Victor Stinner8c62be82010-05-06 00:08:46 +00005986 long ruid_arg, euid_arg;
5987 uid_t ruid, euid;
5988 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5989 return NULL;
5990 if (ruid_arg == -1)
5991 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5992 else
5993 ruid = ruid_arg; /* otherwise, assign from our long */
5994 if (euid_arg == -1)
5995 euid = (uid_t)-1;
5996 else
5997 euid = euid_arg;
5998 if ((euid_arg != -1 && euid != euid_arg) ||
5999 (ruid_arg != -1 && ruid != ruid_arg)) {
6000 PyErr_SetString(PyExc_OverflowError, "user id too big");
6001 return NULL;
6002 }
6003 if (setreuid(ruid, euid) < 0) {
6004 return posix_error();
6005 } else {
6006 Py_INCREF(Py_None);
6007 return Py_None;
6008 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006009}
6010#endif /* HAVE_SETREUID */
6011
6012#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006013PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006014"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015Set the current process's real and effective group ids.");
6016
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006017static PyObject *
6018posix_setregid (PyObject *self, PyObject *args)
6019{
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 long rgid_arg, egid_arg;
6021 gid_t rgid, egid;
6022 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
6023 return NULL;
6024 if (rgid_arg == -1)
6025 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
6026 else
6027 rgid = rgid_arg; /* otherwise, assign from our long */
6028 if (egid_arg == -1)
6029 egid = (gid_t)-1;
6030 else
6031 egid = egid_arg;
6032 if ((egid_arg != -1 && egid != egid_arg) ||
6033 (rgid_arg != -1 && rgid != rgid_arg)) {
6034 PyErr_SetString(PyExc_OverflowError, "group id too big");
6035 return NULL;
6036 }
6037 if (setregid(rgid, egid) < 0) {
6038 return posix_error();
6039 } else {
6040 Py_INCREF(Py_None);
6041 return Py_None;
6042 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006043}
6044#endif /* HAVE_SETREGID */
6045
Guido van Rossumb6775db1994-08-01 11:34:53 +00006046#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006047PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006048"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006049Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006050
Barry Warsaw53699e91996-12-10 23:23:01 +00006051static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006052posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006053{
Victor Stinner8c62be82010-05-06 00:08:46 +00006054 long gid_arg;
6055 gid_t gid;
6056 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
6057 return NULL;
6058 gid = gid_arg;
6059 if (gid != gid_arg) {
6060 PyErr_SetString(PyExc_OverflowError, "group id too big");
6061 return NULL;
6062 }
6063 if (setgid(gid) < 0)
6064 return posix_error();
6065 Py_INCREF(Py_None);
6066 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006067}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006068#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006069
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006070#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006071PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006072"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006073Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006074
6075static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006076posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006077{
Victor Stinner8c62be82010-05-06 00:08:46 +00006078 int i, len;
6079 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006080
Victor Stinner8c62be82010-05-06 00:08:46 +00006081 if (!PySequence_Check(groups)) {
6082 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6083 return NULL;
6084 }
6085 len = PySequence_Size(groups);
6086 if (len > MAX_GROUPS) {
6087 PyErr_SetString(PyExc_ValueError, "too many groups");
6088 return NULL;
6089 }
6090 for(i = 0; i < len; i++) {
6091 PyObject *elem;
6092 elem = PySequence_GetItem(groups, i);
6093 if (!elem)
6094 return NULL;
6095 if (!PyLong_Check(elem)) {
6096 PyErr_SetString(PyExc_TypeError,
6097 "groups must be integers");
6098 Py_DECREF(elem);
6099 return NULL;
6100 } else {
6101 unsigned long x = PyLong_AsUnsignedLong(elem);
6102 if (PyErr_Occurred()) {
6103 PyErr_SetString(PyExc_TypeError,
6104 "group id too big");
6105 Py_DECREF(elem);
6106 return NULL;
6107 }
6108 grouplist[i] = x;
6109 /* read back the value to see if it fitted in gid_t */
6110 if (grouplist[i] != x) {
6111 PyErr_SetString(PyExc_TypeError,
6112 "group id too big");
6113 Py_DECREF(elem);
6114 return NULL;
6115 }
6116 }
6117 Py_DECREF(elem);
6118 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006119
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 if (setgroups(len, grouplist) < 0)
6121 return posix_error();
6122 Py_INCREF(Py_None);
6123 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006124}
6125#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006126
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006127#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6128static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00006129wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006130{
Victor Stinner8c62be82010-05-06 00:08:46 +00006131 PyObject *result;
6132 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006133 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006134
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 if (pid == -1)
6136 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006137
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 if (struct_rusage == NULL) {
6139 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6140 if (m == NULL)
6141 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006142 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006143 Py_DECREF(m);
6144 if (struct_rusage == NULL)
6145 return NULL;
6146 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006147
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6149 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6150 if (!result)
6151 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006152
6153#ifndef doubletime
6154#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6155#endif
6156
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 PyStructSequence_SET_ITEM(result, 0,
6158 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6159 PyStructSequence_SET_ITEM(result, 1,
6160 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006161#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006162 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6163 SET_INT(result, 2, ru->ru_maxrss);
6164 SET_INT(result, 3, ru->ru_ixrss);
6165 SET_INT(result, 4, ru->ru_idrss);
6166 SET_INT(result, 5, ru->ru_isrss);
6167 SET_INT(result, 6, ru->ru_minflt);
6168 SET_INT(result, 7, ru->ru_majflt);
6169 SET_INT(result, 8, ru->ru_nswap);
6170 SET_INT(result, 9, ru->ru_inblock);
6171 SET_INT(result, 10, ru->ru_oublock);
6172 SET_INT(result, 11, ru->ru_msgsnd);
6173 SET_INT(result, 12, ru->ru_msgrcv);
6174 SET_INT(result, 13, ru->ru_nsignals);
6175 SET_INT(result, 14, ru->ru_nvcsw);
6176 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006177#undef SET_INT
6178
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 if (PyErr_Occurred()) {
6180 Py_DECREF(result);
6181 return NULL;
6182 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006183
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006185}
6186#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6187
6188#ifdef HAVE_WAIT3
6189PyDoc_STRVAR(posix_wait3__doc__,
6190"wait3(options) -> (pid, status, rusage)\n\n\
6191Wait for completion of a child process.");
6192
6193static PyObject *
6194posix_wait3(PyObject *self, PyObject *args)
6195{
Victor Stinner8c62be82010-05-06 00:08:46 +00006196 pid_t pid;
6197 int options;
6198 struct rusage ru;
6199 WAIT_TYPE status;
6200 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006201
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6203 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006204
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 Py_BEGIN_ALLOW_THREADS
6206 pid = wait3(&status, options, &ru);
6207 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006208
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006210}
6211#endif /* HAVE_WAIT3 */
6212
6213#ifdef HAVE_WAIT4
6214PyDoc_STRVAR(posix_wait4__doc__,
6215"wait4(pid, options) -> (pid, status, rusage)\n\n\
6216Wait for completion of a given child process.");
6217
6218static PyObject *
6219posix_wait4(PyObject *self, PyObject *args)
6220{
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 pid_t pid;
6222 int options;
6223 struct rusage ru;
6224 WAIT_TYPE status;
6225 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006226
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6228 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006229
Victor Stinner8c62be82010-05-06 00:08:46 +00006230 Py_BEGIN_ALLOW_THREADS
6231 pid = wait4(pid, &status, options, &ru);
6232 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006233
Victor Stinner8c62be82010-05-06 00:08:46 +00006234 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006235}
6236#endif /* HAVE_WAIT4 */
6237
Ross Lagerwall7807c352011-03-17 20:20:30 +02006238#if defined(HAVE_WAITID) && !defined(__APPLE__)
6239PyDoc_STRVAR(posix_waitid__doc__,
6240"waitid(idtype, id, options) -> waitid_result\n\n\
6241Wait for the completion of one or more child processes.\n\n\
6242idtype can be P_PID, P_PGID or P_ALL.\n\
6243id specifies the pid to wait on.\n\
6244options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6245or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6246Returns either waitid_result or None if WNOHANG is specified and there are\n\
6247no children in a waitable state.");
6248
6249static PyObject *
6250posix_waitid(PyObject *self, PyObject *args)
6251{
6252 PyObject *result;
6253 idtype_t idtype;
6254 id_t id;
6255 int options, res;
6256 siginfo_t si;
6257 si.si_pid = 0;
6258 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6259 return NULL;
6260 Py_BEGIN_ALLOW_THREADS
6261 res = waitid(idtype, id, &si, options);
6262 Py_END_ALLOW_THREADS
6263 if (res == -1)
6264 return posix_error();
6265
6266 if (si.si_pid == 0)
6267 Py_RETURN_NONE;
6268
6269 result = PyStructSequence_New(&WaitidResultType);
6270 if (!result)
6271 return NULL;
6272
6273 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6274 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6275 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6276 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6277 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6278 if (PyErr_Occurred()) {
6279 Py_DECREF(result);
6280 return NULL;
6281 }
6282
6283 return result;
6284}
6285#endif
6286
Guido van Rossumb6775db1994-08-01 11:34:53 +00006287#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006288PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006289"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006290Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006291
Barry Warsaw53699e91996-12-10 23:23:01 +00006292static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006293posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006294{
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 pid_t pid;
6296 int options;
6297 WAIT_TYPE status;
6298 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006299
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6301 return NULL;
6302 Py_BEGIN_ALLOW_THREADS
6303 pid = waitpid(pid, &status, options);
6304 Py_END_ALLOW_THREADS
6305 if (pid == -1)
6306 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006307
Victor Stinner8c62be82010-05-06 00:08:46 +00006308 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006309}
6310
Tim Petersab034fa2002-02-01 11:27:43 +00006311#elif defined(HAVE_CWAIT)
6312
6313/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006314PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006315"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006316"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006317
6318static PyObject *
6319posix_waitpid(PyObject *self, PyObject *args)
6320{
Victor Stinner8c62be82010-05-06 00:08:46 +00006321 Py_intptr_t pid;
6322 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006323
Victor Stinner8c62be82010-05-06 00:08:46 +00006324 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6325 return NULL;
6326 Py_BEGIN_ALLOW_THREADS
6327 pid = _cwait(&status, pid, options);
6328 Py_END_ALLOW_THREADS
6329 if (pid == -1)
6330 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006331
Victor Stinner8c62be82010-05-06 00:08:46 +00006332 /* shift the status left a byte so this is more like the POSIX waitpid */
6333 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006334}
6335#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006336
Guido van Rossumad0ee831995-03-01 10:34:45 +00006337#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006338PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006339"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006340Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006341
Barry Warsaw53699e91996-12-10 23:23:01 +00006342static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006343posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006344{
Victor Stinner8c62be82010-05-06 00:08:46 +00006345 pid_t pid;
6346 WAIT_TYPE status;
6347 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006348
Victor Stinner8c62be82010-05-06 00:08:46 +00006349 Py_BEGIN_ALLOW_THREADS
6350 pid = wait(&status);
6351 Py_END_ALLOW_THREADS
6352 if (pid == -1)
6353 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006354
Victor Stinner8c62be82010-05-06 00:08:46 +00006355 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006356}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006357#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006358
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006360PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006361"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006362Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006363
Barry Warsaw53699e91996-12-10 23:23:01 +00006364static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006365posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006366{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006367#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006368 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006369#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006370#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006371 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006372 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006373#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006375#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006376#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006377}
6378
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006379
Guido van Rossumb6775db1994-08-01 11:34:53 +00006380#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006381PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006382"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006383Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006384
Barry Warsaw53699e91996-12-10 23:23:01 +00006385static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006386posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006387{
Victor Stinner8c62be82010-05-06 00:08:46 +00006388 PyObject* v;
6389 char buf[MAXPATHLEN];
6390 PyObject *opath;
6391 char *path;
6392 int n;
6393 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006394
Victor Stinner8c62be82010-05-06 00:08:46 +00006395 if (!PyArg_ParseTuple(args, "O&:readlink",
6396 PyUnicode_FSConverter, &opath))
6397 return NULL;
6398 path = PyBytes_AsString(opath);
6399 v = PySequence_GetItem(args, 0);
6400 if (v == NULL) {
6401 Py_DECREF(opath);
6402 return NULL;
6403 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006404
Victor Stinner8c62be82010-05-06 00:08:46 +00006405 if (PyUnicode_Check(v)) {
6406 arg_is_unicode = 1;
6407 }
6408 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006409
Victor Stinner8c62be82010-05-06 00:08:46 +00006410 Py_BEGIN_ALLOW_THREADS
6411 n = readlink(path, buf, (int) sizeof buf);
6412 Py_END_ALLOW_THREADS
6413 if (n < 0)
6414 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006415
Victor Stinner8c62be82010-05-06 00:08:46 +00006416 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006417 if (arg_is_unicode)
6418 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6419 else
6420 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006421}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006422#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006423
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006424
Brian Curtin52173d42010-12-02 18:29:18 +00006425#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006426PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006427"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006428Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006429
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006430static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006431posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006432{
Victor Stinner8c62be82010-05-06 00:08:46 +00006433 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006434}
6435#endif /* HAVE_SYMLINK */
6436
Brian Curtind40e6f72010-07-08 21:39:08 +00006437#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6438
6439PyDoc_STRVAR(win_readlink__doc__,
6440"readlink(path) -> path\n\n\
6441Return a string representing the path to which the symbolic link points.");
6442
Brian Curtind40e6f72010-07-08 21:39:08 +00006443/* Windows readlink implementation */
6444static PyObject *
6445win_readlink(PyObject *self, PyObject *args)
6446{
6447 wchar_t *path;
6448 DWORD n_bytes_returned;
6449 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006450 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00006451 HANDLE reparse_point_handle;
6452
6453 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6454 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6455 wchar_t *print_name;
6456
6457 if (!PyArg_ParseTuple(args,
Victor Stinnereb5657a2011-09-30 01:44:27 +02006458 "U:readlink",
6459 &po))
6460 return NULL;
6461 path = PyUnicode_AsUnicode(po);
6462 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006463 return NULL;
6464
6465 /* First get a handle to the reparse point */
6466 Py_BEGIN_ALLOW_THREADS
6467 reparse_point_handle = CreateFileW(
6468 path,
6469 0,
6470 0,
6471 0,
6472 OPEN_EXISTING,
6473 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6474 0);
6475 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006476
Brian Curtind40e6f72010-07-08 21:39:08 +00006477 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006478 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006479
Brian Curtind40e6f72010-07-08 21:39:08 +00006480 Py_BEGIN_ALLOW_THREADS
6481 /* New call DeviceIoControl to read the reparse point */
6482 io_result = DeviceIoControl(
6483 reparse_point_handle,
6484 FSCTL_GET_REPARSE_POINT,
6485 0, 0, /* in buffer */
6486 target_buffer, sizeof(target_buffer),
6487 &n_bytes_returned,
6488 0 /* we're not using OVERLAPPED_IO */
6489 );
6490 CloseHandle(reparse_point_handle);
6491 Py_END_ALLOW_THREADS
6492
6493 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006494 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00006495
6496 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6497 {
6498 PyErr_SetString(PyExc_ValueError,
6499 "not a symbolic link");
6500 return NULL;
6501 }
Brian Curtin74e45612010-07-09 15:58:59 +00006502 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6503 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6504
6505 result = PyUnicode_FromWideChar(print_name,
6506 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006507 return result;
6508}
6509
6510#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6511
Brian Curtin52173d42010-12-02 18:29:18 +00006512#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006513
6514/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6515static int has_CreateSymbolicLinkW = 0;
6516static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6517static int
6518check_CreateSymbolicLinkW()
6519{
6520 HINSTANCE hKernel32;
6521 /* only recheck */
6522 if (has_CreateSymbolicLinkW)
6523 return has_CreateSymbolicLinkW;
6524 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006525 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6526 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006527 if (Py_CreateSymbolicLinkW)
6528 has_CreateSymbolicLinkW = 1;
6529 return has_CreateSymbolicLinkW;
6530}
6531
6532PyDoc_STRVAR(win_symlink__doc__,
6533"symlink(src, dst, target_is_directory=False)\n\n\
6534Create a symbolic link pointing to src named dst.\n\
6535target_is_directory is required if the target is to be interpreted as\n\
6536a directory.\n\
6537This function requires Windows 6.0 or greater, and raises a\n\
6538NotImplementedError otherwise.");
6539
6540static PyObject *
6541win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6542{
6543 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006544 PyObject *osrc, *odest;
6545 PyObject *usrc = NULL, *udest = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006546 wchar_t *wsrc, *wdest;
Brian Curtind40e6f72010-07-08 21:39:08 +00006547 int target_is_directory = 0;
6548 DWORD res;
6549 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006550
Brian Curtind40e6f72010-07-08 21:39:08 +00006551 if (!check_CreateSymbolicLinkW())
6552 {
6553 /* raise NotImplementedError */
6554 return PyErr_Format(PyExc_NotImplementedError,
6555 "CreateSymbolicLinkW not found");
6556 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006557 if (!PyArg_ParseTupleAndKeywords(
6558 args, kwargs, "OO|i:symlink", kwlist,
6559 &osrc, &odest, &target_is_directory))
Brian Curtind40e6f72010-07-08 21:39:08 +00006560 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006561
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006562 usrc = win32_decode_filename(osrc);
6563 if (!usrc)
6564 return NULL;
6565 udest = win32_decode_filename(odest);
6566 if (!udest)
6567 goto error;
6568
Brian Curtin3b4499c2010-12-28 14:31:47 +00006569 if (win32_can_symlink == 0)
6570 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6571
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006572 wsrc = PyUnicode_AsUnicode(usrc);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006573 if (wsrc == NULL)
6574 goto error;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006575 wdest = PyUnicode_AsUnicode(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006576 if (wsrc == NULL)
6577 goto error;
6578
Brian Curtind40e6f72010-07-08 21:39:08 +00006579 /* if src is a directory, ensure target_is_directory==1 */
6580 if(
6581 GetFileAttributesExW(
Victor Stinnereb5657a2011-09-30 01:44:27 +02006582 wsrc, GetFileExInfoStandard, &src_info
Brian Curtind40e6f72010-07-08 21:39:08 +00006583 ))
6584 {
6585 target_is_directory = target_is_directory ||
6586 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6587 }
6588
6589 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006590 res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
Brian Curtind40e6f72010-07-08 21:39:08 +00006591 Py_END_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006592
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006593 Py_DECREF(usrc);
6594 Py_DECREF(udest);
Brian Curtind40e6f72010-07-08 21:39:08 +00006595 if (!res)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006596 return win32_error_object("symlink", osrc);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006597
Brian Curtind40e6f72010-07-08 21:39:08 +00006598 Py_INCREF(Py_None);
6599 return Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006600
6601error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006602 Py_XDECREF(usrc);
6603 Py_XDECREF(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006604 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00006605}
Brian Curtin52173d42010-12-02 18:29:18 +00006606#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006607
6608#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006609#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6610static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006611system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006612{
6613 ULONG value = 0;
6614
6615 Py_BEGIN_ALLOW_THREADS
6616 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6617 Py_END_ALLOW_THREADS
6618
6619 return value;
6620}
6621
6622static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006623posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006624{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006625 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006626 return Py_BuildValue("ddddd",
6627 (double)0 /* t.tms_utime / HZ */,
6628 (double)0 /* t.tms_stime / HZ */,
6629 (double)0 /* t.tms_cutime / HZ */,
6630 (double)0 /* t.tms_cstime / HZ */,
6631 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006632}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006633#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006634#define NEED_TICKS_PER_SECOND
6635static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006636static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006637posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006638{
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 struct tms t;
6640 clock_t c;
6641 errno = 0;
6642 c = times(&t);
6643 if (c == (clock_t) -1)
6644 return posix_error();
6645 return Py_BuildValue("ddddd",
6646 (double)t.tms_utime / ticks_per_second,
6647 (double)t.tms_stime / ticks_per_second,
6648 (double)t.tms_cutime / ticks_per_second,
6649 (double)t.tms_cstime / ticks_per_second,
6650 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006651}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006652#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006653#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006654
6655
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006656#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006657#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006658static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006659posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006660{
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 FILETIME create, exit, kernel, user;
6662 HANDLE hProc;
6663 hProc = GetCurrentProcess();
6664 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6665 /* The fields of a FILETIME structure are the hi and lo part
6666 of a 64-bit value expressed in 100 nanosecond units.
6667 1e7 is one second in such units; 1e-7 the inverse.
6668 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6669 */
6670 return Py_BuildValue(
6671 "ddddd",
6672 (double)(user.dwHighDateTime*429.4967296 +
6673 user.dwLowDateTime*1e-7),
6674 (double)(kernel.dwHighDateTime*429.4967296 +
6675 kernel.dwLowDateTime*1e-7),
6676 (double)0,
6677 (double)0,
6678 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006679}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006680#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006681
6682#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006683PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006684"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006685Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006686#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006687
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006688
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006689#ifdef HAVE_GETSID
6690PyDoc_STRVAR(posix_getsid__doc__,
6691"getsid(pid) -> sid\n\n\
6692Call the system call getsid().");
6693
6694static PyObject *
6695posix_getsid(PyObject *self, PyObject *args)
6696{
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 pid_t pid;
6698 int sid;
6699 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6700 return NULL;
6701 sid = getsid(pid);
6702 if (sid < 0)
6703 return posix_error();
6704 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006705}
6706#endif /* HAVE_GETSID */
6707
6708
Guido van Rossumb6775db1994-08-01 11:34:53 +00006709#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006710PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006711"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006712Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006713
Barry Warsaw53699e91996-12-10 23:23:01 +00006714static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006715posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006716{
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 if (setsid() < 0)
6718 return posix_error();
6719 Py_INCREF(Py_None);
6720 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006721}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006722#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006723
Guido van Rossumb6775db1994-08-01 11:34:53 +00006724#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006725PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006726"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006727Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006728
Barry Warsaw53699e91996-12-10 23:23:01 +00006729static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006730posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006731{
Victor Stinner8c62be82010-05-06 00:08:46 +00006732 pid_t pid;
6733 int pgrp;
6734 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6735 return NULL;
6736 if (setpgid(pid, pgrp) < 0)
6737 return posix_error();
6738 Py_INCREF(Py_None);
6739 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006740}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006741#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006743
Guido van Rossumb6775db1994-08-01 11:34:53 +00006744#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006745PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006746"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006747Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006748
Barry Warsaw53699e91996-12-10 23:23:01 +00006749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006750posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006751{
Victor Stinner8c62be82010-05-06 00:08:46 +00006752 int fd;
6753 pid_t pgid;
6754 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6755 return NULL;
6756 pgid = tcgetpgrp(fd);
6757 if (pgid < 0)
6758 return posix_error();
6759 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006760}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006761#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006762
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006763
Guido van Rossumb6775db1994-08-01 11:34:53 +00006764#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006765PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006766"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006767Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006768
Barry Warsaw53699e91996-12-10 23:23:01 +00006769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006770posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006771{
Victor Stinner8c62be82010-05-06 00:08:46 +00006772 int fd;
6773 pid_t pgid;
6774 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6775 return NULL;
6776 if (tcsetpgrp(fd, pgid) < 0)
6777 return posix_error();
6778 Py_INCREF(Py_None);
6779 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006780}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006781#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006782
Guido van Rossum687dd131993-05-17 08:34:16 +00006783/* Functions acting on file descriptors */
6784
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006785PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006786"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006787Open a file (for low level IO).");
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_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006791{
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 PyObject *ofile;
6793 char *file;
6794 int flag;
6795 int mode = 0777;
6796 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006797
6798#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006799 PyObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006800 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02006801 wchar_t *wpath = PyUnicode_AsUnicode(po);
6802 if (wpath == NULL)
6803 return NULL;
6804
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006806 fd = _wopen(wpath, flag, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 Py_END_ALLOW_THREADS
6808 if (fd < 0)
6809 return posix_error();
6810 return PyLong_FromLong((long)fd);
6811 }
6812 /* Drop the argument parsing error as narrow strings
6813 are also valid. */
6814 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006815#endif
6816
Victor Stinner26de69d2011-06-17 15:15:38 +02006817 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006818 PyUnicode_FSConverter, &ofile,
6819 &flag, &mode))
6820 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006821#ifdef MS_WINDOWS
6822 if (win32_warn_bytes_api()) {
6823 Py_DECREF(ofile);
6824 return NULL;
6825 }
6826#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006827 file = PyBytes_AsString(ofile);
6828 Py_BEGIN_ALLOW_THREADS
6829 fd = open(file, flag, mode);
6830 Py_END_ALLOW_THREADS
6831 if (fd < 0)
6832 return posix_error_with_allocated_filename(ofile);
6833 Py_DECREF(ofile);
6834 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006835}
6836
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006838PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006839"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006840Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006841
Barry Warsaw53699e91996-12-10 23:23:01 +00006842static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006843posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006844{
Victor Stinner8c62be82010-05-06 00:08:46 +00006845 int fd, res;
6846 if (!PyArg_ParseTuple(args, "i:close", &fd))
6847 return NULL;
6848 if (!_PyVerify_fd(fd))
6849 return posix_error();
6850 Py_BEGIN_ALLOW_THREADS
6851 res = close(fd);
6852 Py_END_ALLOW_THREADS
6853 if (res < 0)
6854 return posix_error();
6855 Py_INCREF(Py_None);
6856 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006857}
6858
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006859
Victor Stinner8c62be82010-05-06 00:08:46 +00006860PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006861"closerange(fd_low, fd_high)\n\n\
6862Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6863
6864static PyObject *
6865posix_closerange(PyObject *self, PyObject *args)
6866{
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 int fd_from, fd_to, i;
6868 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6869 return NULL;
6870 Py_BEGIN_ALLOW_THREADS
6871 for (i = fd_from; i < fd_to; i++)
6872 if (_PyVerify_fd(i))
6873 close(i);
6874 Py_END_ALLOW_THREADS
6875 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006876}
6877
6878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006879PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006880"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006881Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006882
Barry Warsaw53699e91996-12-10 23:23:01 +00006883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006884posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006885{
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 int fd;
6887 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6888 return NULL;
6889 if (!_PyVerify_fd(fd))
6890 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006891 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 if (fd < 0)
6893 return posix_error();
6894 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006895}
6896
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006898PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006899"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006900Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006901
Barry Warsaw53699e91996-12-10 23:23:01 +00006902static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006903posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006904{
Victor Stinner8c62be82010-05-06 00:08:46 +00006905 int fd, fd2, res;
6906 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6907 return NULL;
6908 if (!_PyVerify_fd_dup2(fd, fd2))
6909 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 if (res < 0)
6912 return posix_error();
6913 Py_INCREF(Py_None);
6914 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006915}
6916
Ross Lagerwall7807c352011-03-17 20:20:30 +02006917#ifdef HAVE_LOCKF
6918PyDoc_STRVAR(posix_lockf__doc__,
6919"lockf(fd, cmd, len)\n\n\
6920Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6921fd is an open file descriptor.\n\
6922cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6923F_TEST.\n\
6924len specifies the section of the file to lock.");
6925
6926static PyObject *
6927posix_lockf(PyObject *self, PyObject *args)
6928{
6929 int fd, cmd, res;
6930 off_t len;
6931 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6932 &fd, &cmd, _parse_off_t, &len))
6933 return NULL;
6934
6935 Py_BEGIN_ALLOW_THREADS
6936 res = lockf(fd, cmd, len);
6937 Py_END_ALLOW_THREADS
6938
6939 if (res < 0)
6940 return posix_error();
6941
6942 Py_RETURN_NONE;
6943}
6944#endif
6945
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006947PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006948"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006949Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006950
Barry Warsaw53699e91996-12-10 23:23:01 +00006951static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006952posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006953{
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006955#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006956 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006957#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006959#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006960 PyObject *posobj;
6961 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006962 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006963#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6965 switch (how) {
6966 case 0: how = SEEK_SET; break;
6967 case 1: how = SEEK_CUR; break;
6968 case 2: how = SEEK_END; break;
6969 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006970#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006971
Ross Lagerwall8e749672011-03-17 21:54:07 +02006972#if !defined(HAVE_LARGEFILE_SUPPORT)
6973 pos = PyLong_AsLong(posobj);
6974#else
6975 pos = PyLong_AsLongLong(posobj);
6976#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006977 if (PyErr_Occurred())
6978 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006979
Victor Stinner8c62be82010-05-06 00:08:46 +00006980 if (!_PyVerify_fd(fd))
6981 return posix_error();
6982 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006983#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006985#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006986 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006987#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 Py_END_ALLOW_THREADS
6989 if (res < 0)
6990 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006991
6992#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006994#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006996#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006997}
6998
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006999
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007000PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007001"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007002Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007003
Barry Warsaw53699e91996-12-10 23:23:01 +00007004static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007005posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007006{
Victor Stinner8c62be82010-05-06 00:08:46 +00007007 int fd, size;
7008 Py_ssize_t n;
7009 PyObject *buffer;
7010 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7011 return NULL;
7012 if (size < 0) {
7013 errno = EINVAL;
7014 return posix_error();
7015 }
7016 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7017 if (buffer == NULL)
7018 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007019 if (!_PyVerify_fd(fd)) {
7020 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007022 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 Py_BEGIN_ALLOW_THREADS
7024 n = read(fd, PyBytes_AS_STRING(buffer), size);
7025 Py_END_ALLOW_THREADS
7026 if (n < 0) {
7027 Py_DECREF(buffer);
7028 return posix_error();
7029 }
7030 if (n != size)
7031 _PyBytes_Resize(&buffer, n);
7032 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007033}
7034
Ross Lagerwall7807c352011-03-17 20:20:30 +02007035#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7036 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007037static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007038iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7039{
7040 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007041 Py_ssize_t blen, total = 0;
7042
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007043 *iov = PyMem_New(struct iovec, cnt);
7044 if (*iov == NULL) {
7045 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007046 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007047 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007048
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007049 *buf = PyMem_New(Py_buffer, cnt);
7050 if (*buf == NULL) {
7051 PyMem_Del(*iov);
7052 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007053 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007054 }
7055
7056 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007057 PyObject *item = PySequence_GetItem(seq, i);
7058 if (item == NULL)
7059 goto fail;
7060 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7061 Py_DECREF(item);
7062 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007063 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007064 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007065 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007066 blen = (*buf)[i].len;
7067 (*iov)[i].iov_len = blen;
7068 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007069 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007070 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007071
7072fail:
7073 PyMem_Del(*iov);
7074 for (j = 0; j < i; j++) {
7075 PyBuffer_Release(&(*buf)[j]);
7076 }
7077 PyMem_Del(*buf);
7078 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007079}
7080
7081static void
7082iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7083{
7084 int i;
7085 PyMem_Del(iov);
7086 for (i = 0; i < cnt; i++) {
7087 PyBuffer_Release(&buf[i]);
7088 }
7089 PyMem_Del(buf);
7090}
7091#endif
7092
Ross Lagerwall7807c352011-03-17 20:20:30 +02007093#ifdef HAVE_READV
7094PyDoc_STRVAR(posix_readv__doc__,
7095"readv(fd, buffers) -> bytesread\n\n\
7096Read from a file descriptor into a number of writable buffers. buffers\n\
7097is an arbitrary sequence of writable buffers.\n\
7098Returns the total number of bytes read.");
7099
7100static PyObject *
7101posix_readv(PyObject *self, PyObject *args)
7102{
7103 int fd, cnt;
7104 Py_ssize_t n;
7105 PyObject *seq;
7106 struct iovec *iov;
7107 Py_buffer *buf;
7108
7109 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7110 return NULL;
7111 if (!PySequence_Check(seq)) {
7112 PyErr_SetString(PyExc_TypeError,
7113 "readv() arg 2 must be a sequence");
7114 return NULL;
7115 }
7116 cnt = PySequence_Size(seq);
7117
7118 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7119 return NULL;
7120
7121 Py_BEGIN_ALLOW_THREADS
7122 n = readv(fd, iov, cnt);
7123 Py_END_ALLOW_THREADS
7124
7125 iov_cleanup(iov, buf, cnt);
7126 return PyLong_FromSsize_t(n);
7127}
7128#endif
7129
7130#ifdef HAVE_PREAD
7131PyDoc_STRVAR(posix_pread__doc__,
7132"pread(fd, buffersize, offset) -> string\n\n\
7133Read from a file descriptor, fd, at a position of offset. It will read up\n\
7134to buffersize number of bytes. The file offset remains unchanged.");
7135
7136static PyObject *
7137posix_pread(PyObject *self, PyObject *args)
7138{
7139 int fd, size;
7140 off_t offset;
7141 Py_ssize_t n;
7142 PyObject *buffer;
7143 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7144 return NULL;
7145
7146 if (size < 0) {
7147 errno = EINVAL;
7148 return posix_error();
7149 }
7150 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7151 if (buffer == NULL)
7152 return NULL;
7153 if (!_PyVerify_fd(fd)) {
7154 Py_DECREF(buffer);
7155 return posix_error();
7156 }
7157 Py_BEGIN_ALLOW_THREADS
7158 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7159 Py_END_ALLOW_THREADS
7160 if (n < 0) {
7161 Py_DECREF(buffer);
7162 return posix_error();
7163 }
7164 if (n != size)
7165 _PyBytes_Resize(&buffer, n);
7166 return buffer;
7167}
7168#endif
7169
7170PyDoc_STRVAR(posix_write__doc__,
7171"write(fd, string) -> byteswritten\n\n\
7172Write a string to a file descriptor.");
7173
7174static PyObject *
7175posix_write(PyObject *self, PyObject *args)
7176{
7177 Py_buffer pbuf;
7178 int fd;
7179 Py_ssize_t size, len;
7180
7181 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7182 return NULL;
7183 if (!_PyVerify_fd(fd)) {
7184 PyBuffer_Release(&pbuf);
7185 return posix_error();
7186 }
7187 len = pbuf.len;
7188 Py_BEGIN_ALLOW_THREADS
7189#if defined(MS_WIN64) || defined(MS_WINDOWS)
7190 if (len > INT_MAX)
7191 len = INT_MAX;
7192 size = write(fd, pbuf.buf, (int)len);
7193#else
7194 size = write(fd, pbuf.buf, len);
7195#endif
7196 Py_END_ALLOW_THREADS
7197 PyBuffer_Release(&pbuf);
7198 if (size < 0)
7199 return posix_error();
7200 return PyLong_FromSsize_t(size);
7201}
7202
7203#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007204PyDoc_STRVAR(posix_sendfile__doc__,
7205"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7206sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7207 -> byteswritten\n\
7208Copy nbytes bytes from file descriptor in to file descriptor out.");
7209
7210static PyObject *
7211posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7212{
7213 int in, out;
7214 Py_ssize_t ret;
7215 off_t offset;
7216
7217#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7218#ifndef __APPLE__
7219 Py_ssize_t len;
7220#endif
7221 PyObject *headers = NULL, *trailers = NULL;
7222 Py_buffer *hbuf, *tbuf;
7223 off_t sbytes;
7224 struct sf_hdtr sf;
7225 int flags = 0;
7226 sf.headers = NULL;
7227 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007228 static char *keywords[] = {"out", "in",
7229 "offset", "count",
7230 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007231
7232#ifdef __APPLE__
7233 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007234 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007235#else
7236 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007237 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007238#endif
7239 &headers, &trailers, &flags))
7240 return NULL;
7241 if (headers != NULL) {
7242 if (!PySequence_Check(headers)) {
7243 PyErr_SetString(PyExc_TypeError,
7244 "sendfile() headers must be a sequence or None");
7245 return NULL;
7246 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007247 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007248 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007249 if (sf.hdr_cnt > 0 &&
7250 !(i = iov_setup(&(sf.headers), &hbuf,
7251 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007252 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007253#ifdef __APPLE__
7254 sbytes += i;
7255#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007256 }
7257 }
7258 if (trailers != NULL) {
7259 if (!PySequence_Check(trailers)) {
7260 PyErr_SetString(PyExc_TypeError,
7261 "sendfile() trailers must be a sequence or None");
7262 return NULL;
7263 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007264 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007265 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007266 if (sf.trl_cnt > 0 &&
7267 !(i = iov_setup(&(sf.trailers), &tbuf,
7268 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007269 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007270#ifdef __APPLE__
7271 sbytes += i;
7272#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007273 }
7274 }
7275
7276 Py_BEGIN_ALLOW_THREADS
7277#ifdef __APPLE__
7278 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7279#else
7280 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7281#endif
7282 Py_END_ALLOW_THREADS
7283
7284 if (sf.headers != NULL)
7285 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7286 if (sf.trailers != NULL)
7287 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7288
7289 if (ret < 0) {
7290 if ((errno == EAGAIN) || (errno == EBUSY)) {
7291 if (sbytes != 0) {
7292 // some data has been sent
7293 goto done;
7294 }
7295 else {
7296 // no data has been sent; upper application is supposed
7297 // to retry on EAGAIN or EBUSY
7298 return posix_error();
7299 }
7300 }
7301 return posix_error();
7302 }
7303 goto done;
7304
7305done:
7306 #if !defined(HAVE_LARGEFILE_SUPPORT)
7307 return Py_BuildValue("l", sbytes);
7308 #else
7309 return Py_BuildValue("L", sbytes);
7310 #endif
7311
7312#else
7313 Py_ssize_t count;
7314 PyObject *offobj;
7315 static char *keywords[] = {"out", "in",
7316 "offset", "count", NULL};
7317 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7318 keywords, &out, &in, &offobj, &count))
7319 return NULL;
7320#ifdef linux
7321 if (offobj == Py_None) {
7322 Py_BEGIN_ALLOW_THREADS
7323 ret = sendfile(out, in, NULL, count);
7324 Py_END_ALLOW_THREADS
7325 if (ret < 0)
7326 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007327 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007328 }
7329#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007330 if (!_parse_off_t(offobj, &offset))
7331 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007332 Py_BEGIN_ALLOW_THREADS
7333 ret = sendfile(out, in, &offset, count);
7334 Py_END_ALLOW_THREADS
7335 if (ret < 0)
7336 return posix_error();
7337 return Py_BuildValue("n", ret);
7338#endif
7339}
7340#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007342PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007343"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007344Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007345
Barry Warsaw53699e91996-12-10 23:23:01 +00007346static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007347posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007348{
Victor Stinner8c62be82010-05-06 00:08:46 +00007349 int fd;
7350 STRUCT_STAT st;
7351 int res;
7352 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7353 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007354#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 /* on OpenVMS we must ensure that all bytes are written to the file */
7356 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007357#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007358 if (!_PyVerify_fd(fd))
7359 return posix_error();
7360 Py_BEGIN_ALLOW_THREADS
7361 res = FSTAT(fd, &st);
7362 Py_END_ALLOW_THREADS
7363 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007364#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007366#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007368#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 }
Tim Peters5aa91602002-01-30 05:46:57 +00007370
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007372}
7373
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007374PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007375"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007376Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007377connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007378
7379static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007380posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007381{
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 int fd;
7383 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7384 return NULL;
7385 if (!_PyVerify_fd(fd))
7386 return PyBool_FromLong(0);
7387 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007388}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007389
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007390#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007391PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007392"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007393Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007394
Barry Warsaw53699e91996-12-10 23:23:01 +00007395static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007396posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007397{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007398#if defined(PYOS_OS2)
7399 HFILE read, write;
7400 APIRET rc;
7401
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007402 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007403 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007404 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007405
7406 return Py_BuildValue("(ii)", read, write);
7407#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007408#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007409 int fds[2];
7410 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007412 if (res != 0)
7413 return posix_error();
7414 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007415#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007416 HANDLE read, write;
7417 int read_fd, write_fd;
7418 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007420 if (!ok)
7421 return win32_error("CreatePipe", NULL);
7422 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7423 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7424 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007425#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007426#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007427}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007428#endif /* HAVE_PIPE */
7429
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007430#ifdef HAVE_PIPE2
7431PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007432"pipe2(flags) -> (read_end, write_end)\n\n\
7433Create a pipe with flags set atomically.\n\
7434flags can be constructed by ORing together one or more of these values:\n\
7435O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007436");
7437
7438static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007439posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007440{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007441 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007442 int fds[2];
7443 int res;
7444
Charles-François Natali368f34b2011-06-06 19:49:47 +02007445 flags = PyLong_AsLong(arg);
7446 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007447 return NULL;
7448
7449 res = pipe2(fds, flags);
7450 if (res != 0)
7451 return posix_error();
7452 return Py_BuildValue("(ii)", fds[0], fds[1]);
7453}
7454#endif /* HAVE_PIPE2 */
7455
Ross Lagerwall7807c352011-03-17 20:20:30 +02007456#ifdef HAVE_WRITEV
7457PyDoc_STRVAR(posix_writev__doc__,
7458"writev(fd, buffers) -> byteswritten\n\n\
7459Write the contents of buffers to a file descriptor, where buffers is an\n\
7460arbitrary sequence of buffers.\n\
7461Returns the total bytes written.");
7462
7463static PyObject *
7464posix_writev(PyObject *self, PyObject *args)
7465{
7466 int fd, cnt;
7467 Py_ssize_t res;
7468 PyObject *seq;
7469 struct iovec *iov;
7470 Py_buffer *buf;
7471 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7472 return NULL;
7473 if (!PySequence_Check(seq)) {
7474 PyErr_SetString(PyExc_TypeError,
7475 "writev() arg 2 must be a sequence");
7476 return NULL;
7477 }
7478 cnt = PySequence_Size(seq);
7479
7480 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7481 return NULL;
7482 }
7483
7484 Py_BEGIN_ALLOW_THREADS
7485 res = writev(fd, iov, cnt);
7486 Py_END_ALLOW_THREADS
7487
7488 iov_cleanup(iov, buf, cnt);
7489 return PyLong_FromSsize_t(res);
7490}
7491#endif
7492
7493#ifdef HAVE_PWRITE
7494PyDoc_STRVAR(posix_pwrite__doc__,
7495"pwrite(fd, string, offset) -> byteswritten\n\n\
7496Write string to a file descriptor, fd, from offset, leaving the file\n\
7497offset unchanged.");
7498
7499static PyObject *
7500posix_pwrite(PyObject *self, PyObject *args)
7501{
7502 Py_buffer pbuf;
7503 int fd;
7504 off_t offset;
7505 Py_ssize_t size;
7506
7507 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7508 return NULL;
7509
7510 if (!_PyVerify_fd(fd)) {
7511 PyBuffer_Release(&pbuf);
7512 return posix_error();
7513 }
7514 Py_BEGIN_ALLOW_THREADS
7515 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7516 Py_END_ALLOW_THREADS
7517 PyBuffer_Release(&pbuf);
7518 if (size < 0)
7519 return posix_error();
7520 return PyLong_FromSsize_t(size);
7521}
7522#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007523
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007524#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007525PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007526"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007527Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007528
Barry Warsaw53699e91996-12-10 23:23:01 +00007529static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007530posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007531{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007532 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007533 char *filename;
7534 int mode = 0666;
7535 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007536 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7537 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007539 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 Py_BEGIN_ALLOW_THREADS
7541 res = mkfifo(filename, mode);
7542 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007543 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 if (res < 0)
7545 return posix_error();
7546 Py_INCREF(Py_None);
7547 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007548}
7549#endif
7550
7551
Neal Norwitz11690112002-07-30 01:08:28 +00007552#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007553PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007554"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007555Create a filesystem node (file, device special file or named pipe)\n\
7556named filename. mode specifies both the permissions to use and the\n\
7557type of node to be created, being combined (bitwise OR) with one of\n\
7558S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007559device defines the newly created device special file (probably using\n\
7560os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007561
7562
7563static PyObject *
7564posix_mknod(PyObject *self, PyObject *args)
7565{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007566 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 char *filename;
7568 int mode = 0600;
7569 int device = 0;
7570 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007571 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7572 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007574 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 Py_BEGIN_ALLOW_THREADS
7576 res = mknod(filename, mode, device);
7577 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007578 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007579 if (res < 0)
7580 return posix_error();
7581 Py_INCREF(Py_None);
7582 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007583}
7584#endif
7585
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007586#ifdef HAVE_DEVICE_MACROS
7587PyDoc_STRVAR(posix_major__doc__,
7588"major(device) -> major number\n\
7589Extracts a device major number from a raw device number.");
7590
7591static PyObject *
7592posix_major(PyObject *self, PyObject *args)
7593{
Victor Stinner8c62be82010-05-06 00:08:46 +00007594 int device;
7595 if (!PyArg_ParseTuple(args, "i:major", &device))
7596 return NULL;
7597 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007598}
7599
7600PyDoc_STRVAR(posix_minor__doc__,
7601"minor(device) -> minor number\n\
7602Extracts a device minor number from a raw device number.");
7603
7604static PyObject *
7605posix_minor(PyObject *self, PyObject *args)
7606{
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 int device;
7608 if (!PyArg_ParseTuple(args, "i:minor", &device))
7609 return NULL;
7610 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007611}
7612
7613PyDoc_STRVAR(posix_makedev__doc__,
7614"makedev(major, minor) -> device number\n\
7615Composes a raw device number from the major and minor device numbers.");
7616
7617static PyObject *
7618posix_makedev(PyObject *self, PyObject *args)
7619{
Victor Stinner8c62be82010-05-06 00:08:46 +00007620 int major, minor;
7621 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7622 return NULL;
7623 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007624}
7625#endif /* device macros */
7626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007627
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007628#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007629PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007630"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007631Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007632
Barry Warsaw53699e91996-12-10 23:23:01 +00007633static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007634posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007635{
Victor Stinner8c62be82010-05-06 00:08:46 +00007636 int fd;
7637 off_t length;
7638 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007639
Ross Lagerwall7807c352011-03-17 20:20:30 +02007640 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007641 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007642
Victor Stinner8c62be82010-05-06 00:08:46 +00007643 Py_BEGIN_ALLOW_THREADS
7644 res = ftruncate(fd, length);
7645 Py_END_ALLOW_THREADS
7646 if (res < 0)
7647 return posix_error();
7648 Py_INCREF(Py_None);
7649 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007650}
7651#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007652
Ross Lagerwall7807c352011-03-17 20:20:30 +02007653#ifdef HAVE_TRUNCATE
7654PyDoc_STRVAR(posix_truncate__doc__,
7655"truncate(path, length)\n\n\
7656Truncate the file given by path to length bytes.");
7657
7658static PyObject *
7659posix_truncate(PyObject *self, PyObject *args)
7660{
7661 PyObject *opath;
7662 const char *path;
7663 off_t length;
7664 int res;
7665
7666 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7667 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7668 return NULL;
7669 path = PyBytes_AsString(opath);
7670
7671 Py_BEGIN_ALLOW_THREADS
7672 res = truncate(path, length);
7673 Py_END_ALLOW_THREADS
7674 Py_DECREF(opath);
7675 if (res < 0)
7676 return posix_error();
7677 Py_RETURN_NONE;
7678}
7679#endif
7680
7681#ifdef HAVE_POSIX_FALLOCATE
7682PyDoc_STRVAR(posix_posix_fallocate__doc__,
7683"posix_fallocate(fd, offset, len)\n\n\
7684Ensures that enough disk space is allocated for the file specified by fd\n\
7685starting from offset and continuing for len bytes.");
7686
7687static PyObject *
7688posix_posix_fallocate(PyObject *self, PyObject *args)
7689{
7690 off_t len, offset;
7691 int res, fd;
7692
7693 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7694 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7695 return NULL;
7696
7697 Py_BEGIN_ALLOW_THREADS
7698 res = posix_fallocate(fd, offset, len);
7699 Py_END_ALLOW_THREADS
7700 if (res != 0) {
7701 errno = res;
7702 return posix_error();
7703 }
7704 Py_RETURN_NONE;
7705}
7706#endif
7707
7708#ifdef HAVE_POSIX_FADVISE
7709PyDoc_STRVAR(posix_posix_fadvise__doc__,
7710"posix_fadvise(fd, offset, len, advice)\n\n\
7711Announces an intention to access data in a specific pattern thus allowing\n\
7712the kernel to make optimizations.\n\
7713The advice applies to the region of the file specified by fd starting at\n\
7714offset and continuing for len bytes.\n\
7715advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7716POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7717POSIX_FADV_DONTNEED.");
7718
7719static PyObject *
7720posix_posix_fadvise(PyObject *self, PyObject *args)
7721{
7722 off_t len, offset;
7723 int res, fd, advice;
7724
7725 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7726 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7727 return NULL;
7728
7729 Py_BEGIN_ALLOW_THREADS
7730 res = posix_fadvise(fd, offset, len, advice);
7731 Py_END_ALLOW_THREADS
7732 if (res != 0) {
7733 errno = res;
7734 return posix_error();
7735 }
7736 Py_RETURN_NONE;
7737}
7738#endif
7739
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007740#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007741PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007742"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007743Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007744
Fred Drake762e2061999-08-26 17:23:54 +00007745/* Save putenv() parameters as values here, so we can collect them when they
7746 * get re-set with another call for the same key. */
7747static PyObject *posix_putenv_garbage;
7748
Tim Peters5aa91602002-01-30 05:46:57 +00007749static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007750posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007751{
Victor Stinner84ae1182010-05-06 22:05:07 +00007752 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007753#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01007754 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007755 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01007756
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007758 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01007759 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007761
Victor Stinner65170952011-11-22 22:16:17 +01007762 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00007763 if (newstr == NULL) {
7764 PyErr_NoMemory();
7765 goto error;
7766 }
Victor Stinner65170952011-11-22 22:16:17 +01007767 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
7768 PyErr_Format(PyExc_ValueError,
7769 "the environment variable is longer than %u characters",
7770 _MAX_ENV);
7771 goto error;
7772 }
7773
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02007775 if (newenv == NULL)
7776 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007777 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007779 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007781#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007782 PyObject *os1, *os2;
7783 char *s1, *s2;
7784 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007785
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007786 if (!PyArg_ParseTuple(args,
7787 "O&O&:putenv",
7788 PyUnicode_FSConverter, &os1,
7789 PyUnicode_FSConverter, &os2))
7790 return NULL;
7791 s1 = PyBytes_AsString(os1);
7792 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00007793
Victor Stinner65170952011-11-22 22:16:17 +01007794 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007795 if (newstr == NULL) {
7796 PyErr_NoMemory();
7797 goto error;
7798 }
7799
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007803 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007805#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007806
Victor Stinner8c62be82010-05-06 00:08:46 +00007807 /* Install the first arg and newstr in posix_putenv_garbage;
7808 * this will cause previous value to be collected. This has to
7809 * happen after the real putenv() call because the old value
7810 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01007811 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 /* really not much we can do; just leak */
7813 PyErr_Clear();
7814 }
7815 else {
7816 Py_DECREF(newstr);
7817 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007818
Martin v. Löwis011e8422009-05-05 04:43:17 +00007819#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 Py_DECREF(os1);
7821 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007822#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007823 Py_RETURN_NONE;
7824
7825error:
7826#ifndef MS_WINDOWS
7827 Py_DECREF(os1);
7828 Py_DECREF(os2);
7829#endif
7830 Py_XDECREF(newstr);
7831 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00007832}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007833#endif /* putenv */
7834
Guido van Rossumc524d952001-10-19 01:31:59 +00007835#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007836PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007837"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007838Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007839
7840static PyObject *
7841posix_unsetenv(PyObject *self, PyObject *args)
7842{
Victor Stinner65170952011-11-22 22:16:17 +01007843 PyObject *name;
Victor Stinner60b385e2011-11-22 22:01:28 +01007844 int err;
Victor Stinner84ae1182010-05-06 22:05:07 +00007845
7846 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00007847
Victor Stinner65170952011-11-22 22:16:17 +01007848 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00007849 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00007850
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06007851
Victor Stinner65170952011-11-22 22:16:17 +01007852 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007853 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06007854 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01007855 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007856 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007857
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 /* Remove the key from posix_putenv_garbage;
7859 * this will cause it to be collected. This has to
7860 * happen after the real unsetenv() call because the
7861 * old value was still accessible until then.
7862 */
Victor Stinner65170952011-11-22 22:16:17 +01007863 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 /* really not much we can do; just leak */
7865 PyErr_Clear();
7866 }
Victor Stinner65170952011-11-22 22:16:17 +01007867 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00007868 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007869}
7870#endif /* unsetenv */
7871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007872PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007873"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007874Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007875
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007877posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007878{
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 int code;
7880 char *message;
7881 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7882 return NULL;
7883 message = strerror(code);
7884 if (message == NULL) {
7885 PyErr_SetString(PyExc_ValueError,
7886 "strerror() argument out of range");
7887 return NULL;
7888 }
7889 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007890}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007891
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007892
Guido van Rossumc9641791998-08-04 15:26:23 +00007893#ifdef HAVE_SYS_WAIT_H
7894
Fred Drake106c1a02002-04-23 15:58:02 +00007895#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007896PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007897"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007898Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007899
7900static PyObject *
7901posix_WCOREDUMP(PyObject *self, PyObject *args)
7902{
Victor Stinner8c62be82010-05-06 00:08:46 +00007903 WAIT_TYPE status;
7904 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007905
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7907 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007908
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007910}
7911#endif /* WCOREDUMP */
7912
7913#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007914PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007915"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007916Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007917job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007918
7919static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007920posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007921{
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 WAIT_TYPE status;
7923 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007924
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7926 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007927
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007929}
7930#endif /* WIFCONTINUED */
7931
Guido van Rossumc9641791998-08-04 15:26:23 +00007932#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007933PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007934"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007935Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007936
7937static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007938posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007939{
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 WAIT_TYPE status;
7941 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007942
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7944 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007945
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007947}
7948#endif /* WIFSTOPPED */
7949
7950#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007951PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007952"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007953Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007954
7955static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007956posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007957{
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 WAIT_TYPE status;
7959 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007960
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7962 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007963
Victor Stinner8c62be82010-05-06 00:08:46 +00007964 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007965}
7966#endif /* WIFSIGNALED */
7967
7968#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007969PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007970"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007971Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007972system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007973
7974static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007975posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007976{
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 WAIT_TYPE status;
7978 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007979
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7981 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007982
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007984}
7985#endif /* WIFEXITED */
7986
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007987#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007988PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007989"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007990Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007991
7992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007993posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007994{
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 WAIT_TYPE status;
7996 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007997
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7999 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008000
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008002}
8003#endif /* WEXITSTATUS */
8004
8005#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008006PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008007"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008008Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008009value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008010
8011static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008012posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008013{
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 WAIT_TYPE status;
8015 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008016
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8018 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008019
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008021}
8022#endif /* WTERMSIG */
8023
8024#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008025PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008026"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008027Return the signal that stopped the process that provided\n\
8028the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008029
8030static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008031posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008032{
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 WAIT_TYPE status;
8034 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008035
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8037 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008038
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008040}
8041#endif /* WSTOPSIG */
8042
8043#endif /* HAVE_SYS_WAIT_H */
8044
8045
Thomas Wouters477c8d52006-05-27 19:21:47 +00008046#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008047#ifdef _SCO_DS
8048/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8049 needed definitions in sys/statvfs.h */
8050#define _SVID3
8051#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008052#include <sys/statvfs.h>
8053
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008054static PyObject*
8055_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8057 if (v == NULL)
8058 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008059
8060#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8062 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8063 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8064 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8065 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8066 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8067 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8068 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8069 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8070 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008071#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8073 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8074 PyStructSequence_SET_ITEM(v, 2,
8075 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8076 PyStructSequence_SET_ITEM(v, 3,
8077 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8078 PyStructSequence_SET_ITEM(v, 4,
8079 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8080 PyStructSequence_SET_ITEM(v, 5,
8081 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8082 PyStructSequence_SET_ITEM(v, 6,
8083 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8084 PyStructSequence_SET_ITEM(v, 7,
8085 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8086 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8087 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008088#endif
8089
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008091}
8092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008093PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008094"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008095Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008096
8097static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008098posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008099{
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 int fd, res;
8101 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008102
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8104 return NULL;
8105 Py_BEGIN_ALLOW_THREADS
8106 res = fstatvfs(fd, &st);
8107 Py_END_ALLOW_THREADS
8108 if (res != 0)
8109 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008110
Victor Stinner8c62be82010-05-06 00:08:46 +00008111 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008112}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008113#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008114
8115
Thomas Wouters477c8d52006-05-27 19:21:47 +00008116#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008117#include <sys/statvfs.h>
8118
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008119PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008120"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008121Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008122
8123static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008124posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008125{
Victor Stinner6fa67772011-09-20 04:04:33 +02008126 PyObject *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00008127 int res;
8128 struct statvfs st;
Victor Stinner6fa67772011-09-20 04:04:33 +02008129 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 return NULL;
8131 Py_BEGIN_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008132 res = statvfs(PyBytes_AS_STRING(path), &st);
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 Py_END_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008134 if (res != 0) {
8135 posix_error_with_filename(PyBytes_AS_STRING(path));
8136 Py_DECREF(path);
8137 return NULL;
8138 }
8139 Py_DECREF(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008140
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008142}
8143#endif /* HAVE_STATVFS */
8144
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008145#ifdef MS_WINDOWS
8146PyDoc_STRVAR(win32__getdiskusage__doc__,
8147"_getdiskusage(path) -> (total, free)\n\n\
8148Return disk usage statistics about the given path as (total, free) tuple.");
8149
8150static PyObject *
8151win32__getdiskusage(PyObject *self, PyObject *args)
8152{
8153 BOOL retval;
8154 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008155 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008156
Victor Stinner6139c1b2011-11-09 22:14:14 +01008157 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008158 return NULL;
8159
8160 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008161 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008162 Py_END_ALLOW_THREADS
8163 if (retval == 0)
8164 return PyErr_SetFromWindowsErr(0);
8165
8166 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8167}
8168#endif
8169
8170
Fred Drakec9680921999-12-13 16:37:25 +00008171/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8172 * It maps strings representing configuration variable names to
8173 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008174 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008175 * rarely-used constants. There are three separate tables that use
8176 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008177 *
8178 * This code is always included, even if none of the interfaces that
8179 * need it are included. The #if hackery needed to avoid it would be
8180 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008181 */
8182struct constdef {
8183 char *name;
8184 long value;
8185};
8186
Fred Drake12c6e2d1999-12-14 21:25:03 +00008187static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008188conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008189 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008190{
Christian Heimes217cfd12007-12-02 14:31:20 +00008191 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008192 *valuep = PyLong_AS_LONG(arg);
8193 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008194 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008195 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008196 /* look up the value in the table using a binary search */
8197 size_t lo = 0;
8198 size_t mid;
8199 size_t hi = tablesize;
8200 int cmp;
8201 const char *confname;
8202 if (!PyUnicode_Check(arg)) {
8203 PyErr_SetString(PyExc_TypeError,
8204 "configuration names must be strings or integers");
8205 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008206 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008207 confname = _PyUnicode_AsString(arg);
8208 if (confname == NULL)
8209 return 0;
8210 while (lo < hi) {
8211 mid = (lo + hi) / 2;
8212 cmp = strcmp(confname, table[mid].name);
8213 if (cmp < 0)
8214 hi = mid;
8215 else if (cmp > 0)
8216 lo = mid + 1;
8217 else {
8218 *valuep = table[mid].value;
8219 return 1;
8220 }
8221 }
8222 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8223 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008225}
8226
8227
8228#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8229static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008230#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008231 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008232#endif
8233#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008234 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008235#endif
Fred Drakec9680921999-12-13 16:37:25 +00008236#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008237 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008238#endif
8239#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008240 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008241#endif
8242#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008244#endif
8245#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008247#endif
8248#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008250#endif
8251#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008253#endif
8254#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008255 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008256#endif
8257#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008259#endif
8260#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008262#endif
8263#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008265#endif
8266#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008267 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008268#endif
8269#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008271#endif
8272#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008274#endif
8275#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008277#endif
8278#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008280#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008281#ifdef _PC_ACL_ENABLED
8282 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8283#endif
8284#ifdef _PC_MIN_HOLE_SIZE
8285 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8286#endif
8287#ifdef _PC_ALLOC_SIZE_MIN
8288 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8289#endif
8290#ifdef _PC_REC_INCR_XFER_SIZE
8291 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8292#endif
8293#ifdef _PC_REC_MAX_XFER_SIZE
8294 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8295#endif
8296#ifdef _PC_REC_MIN_XFER_SIZE
8297 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8298#endif
8299#ifdef _PC_REC_XFER_ALIGN
8300 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8301#endif
8302#ifdef _PC_SYMLINK_MAX
8303 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8304#endif
8305#ifdef _PC_XATTR_ENABLED
8306 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8307#endif
8308#ifdef _PC_XATTR_EXISTS
8309 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8310#endif
8311#ifdef _PC_TIMESTAMP_RESOLUTION
8312 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8313#endif
Fred Drakec9680921999-12-13 16:37:25 +00008314};
8315
Fred Drakec9680921999-12-13 16:37:25 +00008316static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008317conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008318{
8319 return conv_confname(arg, valuep, posix_constants_pathconf,
8320 sizeof(posix_constants_pathconf)
8321 / sizeof(struct constdef));
8322}
8323#endif
8324
8325#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008326PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008327"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008328Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008329If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008330
8331static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008332posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008333{
8334 PyObject *result = NULL;
8335 int name, fd;
8336
Fred Drake12c6e2d1999-12-14 21:25:03 +00008337 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8338 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008339 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008340
Stefan Krah0e803b32010-11-26 16:16:47 +00008341 errno = 0;
8342 limit = fpathconf(fd, name);
8343 if (limit == -1 && errno != 0)
8344 posix_error();
8345 else
8346 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008347 }
8348 return result;
8349}
8350#endif
8351
8352
8353#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008354PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008355"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008356Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008357If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008358
8359static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008360posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008361{
8362 PyObject *result = NULL;
8363 int name;
8364 char *path;
8365
8366 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8367 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008369
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 errno = 0;
8371 limit = pathconf(path, name);
8372 if (limit == -1 && errno != 0) {
8373 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008374 /* could be a path or name problem */
8375 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008376 else
Stefan Krah99439262010-11-26 12:58:05 +00008377 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 }
8379 else
8380 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008381 }
8382 return result;
8383}
8384#endif
8385
8386#ifdef HAVE_CONFSTR
8387static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008388#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008389 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008390#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008391#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008392 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008393#endif
8394#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008395 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008396#endif
Fred Draked86ed291999-12-15 15:34:33 +00008397#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008398 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008399#endif
8400#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008401 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008402#endif
8403#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008405#endif
8406#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008407 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008408#endif
Fred Drakec9680921999-12-13 16:37:25 +00008409#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008410 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008411#endif
8412#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008413 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008414#endif
8415#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008416 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008417#endif
8418#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008419 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008420#endif
8421#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008422 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008423#endif
8424#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008425 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008426#endif
8427#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008428 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008429#endif
8430#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008431 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008432#endif
Fred Draked86ed291999-12-15 15:34:33 +00008433#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008434 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008435#endif
Fred Drakec9680921999-12-13 16:37:25 +00008436#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008437 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008438#endif
Fred Draked86ed291999-12-15 15:34:33 +00008439#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008440 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008441#endif
8442#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008443 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008444#endif
8445#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008447#endif
8448#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008450#endif
Fred Drakec9680921999-12-13 16:37:25 +00008451#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008453#endif
8454#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008455 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008456#endif
8457#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008458 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008459#endif
8460#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008461 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008462#endif
8463#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008465#endif
8466#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008468#endif
8469#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008471#endif
8472#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008474#endif
8475#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008477#endif
8478#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008479 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008480#endif
8481#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008483#endif
8484#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008486#endif
8487#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008488 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008489#endif
8490#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008491 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008492#endif
8493#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008494 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008495#endif
8496#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008497 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008498#endif
Fred Draked86ed291999-12-15 15:34:33 +00008499#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008501#endif
8502#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008504#endif
8505#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008507#endif
8508#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008510#endif
8511#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008512 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008513#endif
8514#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008516#endif
8517#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008519#endif
8520#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008522#endif
8523#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008524 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008525#endif
8526#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008527 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008528#endif
8529#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008530 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008531#endif
8532#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008534#endif
8535#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008537#endif
Fred Drakec9680921999-12-13 16:37:25 +00008538};
8539
8540static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008541conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008542{
8543 return conv_confname(arg, valuep, posix_constants_confstr,
8544 sizeof(posix_constants_confstr)
8545 / sizeof(struct constdef));
8546}
8547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008548PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008549"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008550Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008551
8552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008553posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008554{
8555 PyObject *result = NULL;
8556 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008557 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008558 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008559
Victor Stinnercb043522010-09-10 23:49:04 +00008560 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8561 return NULL;
8562
8563 errno = 0;
8564 len = confstr(name, buffer, sizeof(buffer));
8565 if (len == 0) {
8566 if (errno) {
8567 posix_error();
8568 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008569 }
8570 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008571 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008572 }
8573 }
Victor Stinnercb043522010-09-10 23:49:04 +00008574
8575 if ((unsigned int)len >= sizeof(buffer)) {
8576 char *buf = PyMem_Malloc(len);
8577 if (buf == NULL)
8578 return PyErr_NoMemory();
8579 confstr(name, buf, len);
8580 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8581 PyMem_Free(buf);
8582 }
8583 else
8584 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008585 return result;
8586}
8587#endif
8588
8589
8590#ifdef HAVE_SYSCONF
8591static struct constdef posix_constants_sysconf[] = {
8592#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008593 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008594#endif
8595#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008596 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008597#endif
8598#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008600#endif
8601#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008602 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008603#endif
8604#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008605 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008606#endif
8607#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008608 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008609#endif
8610#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008611 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008612#endif
8613#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008614 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008615#endif
8616#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008617 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008618#endif
8619#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008620 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008621#endif
Fred Draked86ed291999-12-15 15:34:33 +00008622#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008623 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008624#endif
8625#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008626 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008627#endif
Fred Drakec9680921999-12-13 16:37:25 +00008628#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008630#endif
Fred Drakec9680921999-12-13 16:37:25 +00008631#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008632 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008633#endif
8634#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008636#endif
8637#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008638 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008639#endif
8640#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008641 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008642#endif
8643#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008644 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008645#endif
Fred Draked86ed291999-12-15 15:34:33 +00008646#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008647 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008648#endif
Fred Drakec9680921999-12-13 16:37:25 +00008649#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008651#endif
8652#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008653 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008654#endif
8655#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008657#endif
8658#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008660#endif
8661#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008662 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008663#endif
Fred Draked86ed291999-12-15 15:34:33 +00008664#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008665 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008666#endif
Fred Drakec9680921999-12-13 16:37:25 +00008667#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008668 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008669#endif
8670#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008672#endif
8673#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008675#endif
8676#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008677 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008678#endif
8679#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008680 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008681#endif
8682#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008683 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008684#endif
8685#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008686 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008687#endif
8688#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008689 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008690#endif
8691#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008692 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008693#endif
8694#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008696#endif
8697#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008698 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008699#endif
8700#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008702#endif
8703#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008705#endif
8706#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008707 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008708#endif
8709#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008710 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008711#endif
8712#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008713 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008714#endif
8715#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008717#endif
8718#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008720#endif
8721#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008723#endif
8724#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008726#endif
8727#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008729#endif
8730#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008731 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008732#endif
8733#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008735#endif
Fred Draked86ed291999-12-15 15:34:33 +00008736#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008738#endif
Fred Drakec9680921999-12-13 16:37:25 +00008739#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008741#endif
8742#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008744#endif
8745#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008746 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008747#endif
Fred Draked86ed291999-12-15 15:34:33 +00008748#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008749 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008750#endif
Fred Drakec9680921999-12-13 16:37:25 +00008751#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008752 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008753#endif
Fred Draked86ed291999-12-15 15:34:33 +00008754#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008756#endif
8757#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008759#endif
Fred Drakec9680921999-12-13 16:37:25 +00008760#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008762#endif
8763#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008764 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008765#endif
8766#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008768#endif
8769#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008770 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008771#endif
Fred Draked86ed291999-12-15 15:34:33 +00008772#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008774#endif
Fred Drakec9680921999-12-13 16:37:25 +00008775#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008777#endif
8778#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008779 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008780#endif
8781#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008782 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008783#endif
8784#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008785 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008786#endif
8787#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008789#endif
8790#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008791 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008792#endif
8793#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008795#endif
Fred Draked86ed291999-12-15 15:34:33 +00008796#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008797 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008798#endif
Fred Drakec9680921999-12-13 16:37:25 +00008799#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008801#endif
8802#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008804#endif
Fred Draked86ed291999-12-15 15:34:33 +00008805#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008807#endif
Fred Drakec9680921999-12-13 16:37:25 +00008808#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008810#endif
8811#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008813#endif
8814#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008816#endif
8817#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008818 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008819#endif
8820#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008821 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008822#endif
8823#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008825#endif
8826#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008827 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008828#endif
8829#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008831#endif
8832#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008834#endif
Fred Draked86ed291999-12-15 15:34:33 +00008835#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008836 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008837#endif
8838#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008839 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008840#endif
Fred Drakec9680921999-12-13 16:37:25 +00008841#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008842 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008843#endif
8844#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008845 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008846#endif
8847#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008848 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008849#endif
8850#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008851 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008852#endif
8853#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008854 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008855#endif
8856#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008857 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008858#endif
8859#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008860 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008861#endif
8862#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008863 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008864#endif
8865#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008866 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008867#endif
8868#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008869 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008870#endif
8871#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008872 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008873#endif
8874#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008876#endif
8877#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008878 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008879#endif
8880#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008882#endif
8883#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008884 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008885#endif
8886#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008888#endif
8889#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008890 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008891#endif
8892#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008894#endif
8895#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008896 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008897#endif
8898#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008900#endif
8901#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008903#endif
8904#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008905 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008906#endif
8907#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008908 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008909#endif
8910#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008911 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008912#endif
8913#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008914 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008915#endif
8916#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008917 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008918#endif
8919#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008920 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008921#endif
8922#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008923 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008924#endif
8925#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008926 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008927#endif
8928#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008930#endif
8931#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008932 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008933#endif
8934#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008935 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008936#endif
8937#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008938 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008939#endif
8940#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008941 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008942#endif
8943#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008944 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008945#endif
Fred Draked86ed291999-12-15 15:34:33 +00008946#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008948#endif
Fred Drakec9680921999-12-13 16:37:25 +00008949#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008950 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008951#endif
8952#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008953 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008954#endif
8955#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008956 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008957#endif
8958#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008959 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008960#endif
8961#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008962 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008963#endif
8964#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008965 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008966#endif
8967#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008968 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008969#endif
8970#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008971 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008972#endif
8973#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008974 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008975#endif
8976#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008977 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008978#endif
8979#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008980 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008981#endif
8982#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008983 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008984#endif
8985#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008986 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008987#endif
8988#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008989 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008990#endif
8991#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008992 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008993#endif
8994#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008995 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008996#endif
8997#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008998 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008999#endif
9000#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009001 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009002#endif
9003#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009004 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009005#endif
9006#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009008#endif
9009#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009010 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009011#endif
9012#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009013 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009014#endif
9015#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009016 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009017#endif
9018#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009019 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009020#endif
9021#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009022 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009023#endif
9024#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009025 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009026#endif
9027#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009028 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009029#endif
9030#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009031 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009032#endif
9033#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009034 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009035#endif
9036#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009037 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009038#endif
9039#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009040 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009041#endif
9042#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009043 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009044#endif
9045#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009046 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009047#endif
9048#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009049 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009050#endif
9051#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009052 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009053#endif
9054#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009055 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009056#endif
9057#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009058 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009059#endif
9060#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009061 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009062#endif
9063#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009064 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009065#endif
9066#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009067 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009068#endif
9069#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009070 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009071#endif
9072#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009073 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009074#endif
9075#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009076 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009077#endif
9078#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009079 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009080#endif
9081#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009082 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009083#endif
9084};
9085
9086static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009087conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009088{
9089 return conv_confname(arg, valuep, posix_constants_sysconf,
9090 sizeof(posix_constants_sysconf)
9091 / sizeof(struct constdef));
9092}
9093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009094PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009095"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009096Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009097
9098static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009099posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009100{
9101 PyObject *result = NULL;
9102 int name;
9103
9104 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9105 int value;
9106
9107 errno = 0;
9108 value = sysconf(name);
9109 if (value == -1 && errno != 0)
9110 posix_error();
9111 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009112 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009113 }
9114 return result;
9115}
9116#endif
9117
9118
Fred Drakebec628d1999-12-15 18:31:10 +00009119/* This code is used to ensure that the tables of configuration value names
9120 * are in sorted order as required by conv_confname(), and also to build the
9121 * the exported dictionaries that are used to publish information about the
9122 * names available on the host platform.
9123 *
9124 * Sorting the table at runtime ensures that the table is properly ordered
9125 * when used, even for platforms we're not able to test on. It also makes
9126 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009127 */
Fred Drakebec628d1999-12-15 18:31:10 +00009128
9129static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009130cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009131{
9132 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009133 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009134 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009135 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009136
9137 return strcmp(c1->name, c2->name);
9138}
9139
9140static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009141setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009142 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009143{
Fred Drakebec628d1999-12-15 18:31:10 +00009144 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009145 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009146
9147 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9148 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009149 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009150 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009151
Barry Warsaw3155db32000-04-13 15:20:40 +00009152 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009153 PyObject *o = PyLong_FromLong(table[i].value);
9154 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9155 Py_XDECREF(o);
9156 Py_DECREF(d);
9157 return -1;
9158 }
9159 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009160 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009161 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009162}
9163
Fred Drakebec628d1999-12-15 18:31:10 +00009164/* Return -1 on failure, 0 on success. */
9165static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009166setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009167{
9168#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009169 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009170 sizeof(posix_constants_pathconf)
9171 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009172 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009173 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009174#endif
9175#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009176 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009177 sizeof(posix_constants_confstr)
9178 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009179 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009180 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009181#endif
9182#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009183 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009184 sizeof(posix_constants_sysconf)
9185 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009186 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009187 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009188#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009189 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009190}
Fred Draked86ed291999-12-15 15:34:33 +00009191
9192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009193PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009194"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009195Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009196in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009197
9198static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009199posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009200{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009201 abort();
9202 /*NOTREACHED*/
9203 Py_FatalError("abort() called from Python code didn't abort!");
9204 return NULL;
9205}
Fred Drakebec628d1999-12-15 18:31:10 +00009206
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009207#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009208PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009209"startfile(filepath [, operation]) - Start a file with its associated\n\
9210application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009211\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009212When \"operation\" is not specified or \"open\", this acts like\n\
9213double-clicking the file in Explorer, or giving the file name as an\n\
9214argument to the DOS \"start\" command: the file is opened with whatever\n\
9215application (if any) its extension is associated.\n\
9216When another \"operation\" is given, it specifies what should be done with\n\
9217the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009218\n\
9219startfile returns as soon as the associated application is launched.\n\
9220There is no option to wait for the application to close, and no way\n\
9221to retrieve the application's exit status.\n\
9222\n\
9223The filepath is relative to the current directory. If you want to use\n\
9224an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009225the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009226
9227static PyObject *
9228win32_startfile(PyObject *self, PyObject *args)
9229{
Victor Stinner8c62be82010-05-06 00:08:46 +00009230 PyObject *ofilepath;
9231 char *filepath;
9232 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009233 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009234 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009235
Victor Stinnereb5657a2011-09-30 01:44:27 +02009236 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009237 if (!PyArg_ParseTuple(args, "U|s:startfile",
9238 &unipath, &operation)) {
9239 PyErr_Clear();
9240 goto normal;
9241 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009242
Victor Stinner8c62be82010-05-06 00:08:46 +00009243 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009244 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009245 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009246 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009247 PyErr_Clear();
9248 operation = NULL;
9249 goto normal;
9250 }
9251 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009252
Victor Stinnereb5657a2011-09-30 01:44:27 +02009253 wpath = PyUnicode_AsUnicode(unipath);
9254 if (wpath == NULL)
9255 goto normal;
9256 if (uoperation) {
9257 woperation = PyUnicode_AsUnicode(uoperation);
9258 if (woperation == NULL)
9259 goto normal;
9260 }
9261 else
9262 woperation = NULL;
9263
Victor Stinner8c62be82010-05-06 00:08:46 +00009264 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009265 rc = ShellExecuteW((HWND)0, woperation, wpath,
9266 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009267 Py_END_ALLOW_THREADS
9268
Victor Stinnereb5657a2011-09-30 01:44:27 +02009269 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009270 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009271 win32_error_object("startfile", unipath);
9272 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009273 }
9274 Py_INCREF(Py_None);
9275 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009276
9277normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009278 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9279 PyUnicode_FSConverter, &ofilepath,
9280 &operation))
9281 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009282 if (win32_warn_bytes_api()) {
9283 Py_DECREF(ofilepath);
9284 return NULL;
9285 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009286 filepath = PyBytes_AsString(ofilepath);
9287 Py_BEGIN_ALLOW_THREADS
9288 rc = ShellExecute((HWND)0, operation, filepath,
9289 NULL, NULL, SW_SHOWNORMAL);
9290 Py_END_ALLOW_THREADS
9291 if (rc <= (HINSTANCE)32) {
9292 PyObject *errval = win32_error("startfile", filepath);
9293 Py_DECREF(ofilepath);
9294 return errval;
9295 }
9296 Py_DECREF(ofilepath);
9297 Py_INCREF(Py_None);
9298 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009299}
9300#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009301
Martin v. Löwis438b5342002-12-27 10:16:42 +00009302#ifdef HAVE_GETLOADAVG
9303PyDoc_STRVAR(posix_getloadavg__doc__,
9304"getloadavg() -> (float, float, float)\n\n\
9305Return the number of processes in the system run queue averaged over\n\
9306the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9307was unobtainable");
9308
9309static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009310posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009311{
9312 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009313 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009314 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9315 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009316 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009317 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009318}
9319#endif
9320
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009321#ifdef MS_WINDOWS
9322
9323PyDoc_STRVAR(win32_urandom__doc__,
9324"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009325Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009326
9327typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9328 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9329 DWORD dwFlags );
9330typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9331 BYTE *pbBuffer );
9332
9333static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009334/* This handle is never explicitly released. Instead, the operating
9335 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009336static HCRYPTPROV hCryptProv = 0;
9337
Tim Peters4ad82172004-08-30 17:02:04 +00009338static PyObject*
9339win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009340{
Victor Stinner8c62be82010-05-06 00:08:46 +00009341 int howMany;
9342 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009343
Victor Stinner8c62be82010-05-06 00:08:46 +00009344 /* Read arguments */
9345 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9346 return NULL;
9347 if (howMany < 0)
9348 return PyErr_Format(PyExc_ValueError,
9349 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009350
Victor Stinner8c62be82010-05-06 00:08:46 +00009351 if (hCryptProv == 0) {
9352 HINSTANCE hAdvAPI32 = NULL;
9353 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009354
Victor Stinner8c62be82010-05-06 00:08:46 +00009355 /* Obtain handle to the DLL containing CryptoAPI
9356 This should not fail */
9357 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9358 if(hAdvAPI32 == NULL)
9359 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009360
Victor Stinner8c62be82010-05-06 00:08:46 +00009361 /* Obtain pointers to the CryptoAPI functions
9362 This will fail on some early versions of Win95 */
9363 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9364 hAdvAPI32,
9365 "CryptAcquireContextA");
9366 if (pCryptAcquireContext == NULL)
9367 return PyErr_Format(PyExc_NotImplementedError,
9368 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009369
Victor Stinner8c62be82010-05-06 00:08:46 +00009370 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9371 hAdvAPI32, "CryptGenRandom");
9372 if (pCryptGenRandom == NULL)
9373 return PyErr_Format(PyExc_NotImplementedError,
9374 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009375
Victor Stinner8c62be82010-05-06 00:08:46 +00009376 /* Acquire context */
9377 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9378 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9379 return win32_error("CryptAcquireContext", NULL);
9380 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009381
Victor Stinner8c62be82010-05-06 00:08:46 +00009382 /* Allocate bytes */
9383 result = PyBytes_FromStringAndSize(NULL, howMany);
9384 if (result != NULL) {
9385 /* Get random data */
9386 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9387 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9388 PyBytes_AS_STRING(result))) {
9389 Py_DECREF(result);
9390 return win32_error("CryptGenRandom", NULL);
9391 }
9392 }
9393 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009394}
9395#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009396
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009397PyDoc_STRVAR(device_encoding__doc__,
9398"device_encoding(fd) -> str\n\n\
9399Return a string describing the encoding of the device\n\
9400if the output is a terminal; else return None.");
9401
9402static PyObject *
9403device_encoding(PyObject *self, PyObject *args)
9404{
Victor Stinner8c62be82010-05-06 00:08:46 +00009405 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009406#if defined(MS_WINDOWS) || defined(MS_WIN64)
9407 UINT cp;
9408#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009409 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9410 return NULL;
9411 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9412 Py_INCREF(Py_None);
9413 return Py_None;
9414 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009415#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009416 if (fd == 0)
9417 cp = GetConsoleCP();
9418 else if (fd == 1 || fd == 2)
9419 cp = GetConsoleOutputCP();
9420 else
9421 cp = 0;
9422 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9423 has no console */
9424 if (cp != 0)
9425 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009426#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009427 {
9428 char *codeset = nl_langinfo(CODESET);
9429 if (codeset != NULL && codeset[0] != 0)
9430 return PyUnicode_FromString(codeset);
9431 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009432#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009433 Py_INCREF(Py_None);
9434 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009435}
9436
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009437#ifdef __VMS
9438/* Use openssl random routine */
9439#include <openssl/rand.h>
9440PyDoc_STRVAR(vms_urandom__doc__,
9441"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009442Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009443
9444static PyObject*
9445vms_urandom(PyObject *self, PyObject *args)
9446{
Victor Stinner8c62be82010-05-06 00:08:46 +00009447 int howMany;
9448 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009449
Victor Stinner8c62be82010-05-06 00:08:46 +00009450 /* Read arguments */
9451 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9452 return NULL;
9453 if (howMany < 0)
9454 return PyErr_Format(PyExc_ValueError,
9455 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009456
Victor Stinner8c62be82010-05-06 00:08:46 +00009457 /* Allocate bytes */
9458 result = PyBytes_FromStringAndSize(NULL, howMany);
9459 if (result != NULL) {
9460 /* Get random data */
9461 if (RAND_pseudo_bytes((unsigned char*)
9462 PyBytes_AS_STRING(result),
9463 howMany) < 0) {
9464 Py_DECREF(result);
9465 return PyErr_Format(PyExc_ValueError,
9466 "RAND_pseudo_bytes");
9467 }
9468 }
9469 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009470}
9471#endif
9472
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009473#ifdef HAVE_SETRESUID
9474PyDoc_STRVAR(posix_setresuid__doc__,
9475"setresuid(ruid, euid, suid)\n\n\
9476Set the current process's real, effective, and saved user ids.");
9477
9478static PyObject*
9479posix_setresuid (PyObject *self, PyObject *args)
9480{
Victor Stinner8c62be82010-05-06 00:08:46 +00009481 /* We assume uid_t is no larger than a long. */
9482 long ruid, euid, suid;
9483 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9484 return NULL;
9485 if (setresuid(ruid, euid, suid) < 0)
9486 return posix_error();
9487 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009488}
9489#endif
9490
9491#ifdef HAVE_SETRESGID
9492PyDoc_STRVAR(posix_setresgid__doc__,
9493"setresgid(rgid, egid, sgid)\n\n\
9494Set the current process's real, effective, and saved group ids.");
9495
9496static PyObject*
9497posix_setresgid (PyObject *self, PyObject *args)
9498{
Victor Stinner8c62be82010-05-06 00:08:46 +00009499 /* We assume uid_t is no larger than a long. */
9500 long rgid, egid, sgid;
9501 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9502 return NULL;
9503 if (setresgid(rgid, egid, sgid) < 0)
9504 return posix_error();
9505 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009506}
9507#endif
9508
9509#ifdef HAVE_GETRESUID
9510PyDoc_STRVAR(posix_getresuid__doc__,
9511"getresuid() -> (ruid, euid, suid)\n\n\
9512Get tuple of the current process's real, effective, and saved user ids.");
9513
9514static PyObject*
9515posix_getresuid (PyObject *self, PyObject *noargs)
9516{
Victor Stinner8c62be82010-05-06 00:08:46 +00009517 uid_t ruid, euid, suid;
9518 long l_ruid, l_euid, l_suid;
9519 if (getresuid(&ruid, &euid, &suid) < 0)
9520 return posix_error();
9521 /* Force the values into long's as we don't know the size of uid_t. */
9522 l_ruid = ruid;
9523 l_euid = euid;
9524 l_suid = suid;
9525 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009526}
9527#endif
9528
9529#ifdef HAVE_GETRESGID
9530PyDoc_STRVAR(posix_getresgid__doc__,
9531"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009532Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009533
9534static PyObject*
9535posix_getresgid (PyObject *self, PyObject *noargs)
9536{
Victor Stinner8c62be82010-05-06 00:08:46 +00009537 uid_t rgid, egid, sgid;
9538 long l_rgid, l_egid, l_sgid;
9539 if (getresgid(&rgid, &egid, &sgid) < 0)
9540 return posix_error();
9541 /* Force the values into long's as we don't know the size of uid_t. */
9542 l_rgid = rgid;
9543 l_egid = egid;
9544 l_sgid = sgid;
9545 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009546}
9547#endif
9548
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009549/* Posix *at family of functions:
9550 faccessat, fchmodat, fchownat, fstatat, futimesat,
9551 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9552 unlinkat, utimensat, mkfifoat */
9553
9554#ifdef HAVE_FACCESSAT
9555PyDoc_STRVAR(posix_faccessat__doc__,
9556"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9557Like access() but if path is relative, it is taken as relative to dirfd.\n\
9558flags is optional and can be constructed by ORing together zero or more\n\
9559of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9560If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9561is interpreted relative to the current working directory.");
9562
9563static PyObject *
9564posix_faccessat(PyObject *self, PyObject *args)
9565{
9566 PyObject *opath;
9567 char *path;
9568 int mode;
9569 int res;
9570 int dirfd, flags = 0;
9571 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9572 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9573 return NULL;
9574 path = PyBytes_AsString(opath);
9575 Py_BEGIN_ALLOW_THREADS
9576 res = faccessat(dirfd, path, mode, flags);
9577 Py_END_ALLOW_THREADS
9578 Py_DECREF(opath);
9579 return PyBool_FromLong(res == 0);
9580}
9581#endif
9582
9583#ifdef HAVE_FCHMODAT
9584PyDoc_STRVAR(posix_fchmodat__doc__,
9585"fchmodat(dirfd, path, mode, flags=0)\n\n\
9586Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9587flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9588If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9589is interpreted relative to the current working directory.");
9590
9591static PyObject *
9592posix_fchmodat(PyObject *self, PyObject *args)
9593{
9594 int dirfd, mode, res;
9595 int flags = 0;
9596 PyObject *opath;
9597 char *path;
9598
9599 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9600 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9601 return NULL;
9602
9603 path = PyBytes_AsString(opath);
9604
9605 Py_BEGIN_ALLOW_THREADS
9606 res = fchmodat(dirfd, path, mode, flags);
9607 Py_END_ALLOW_THREADS
9608 Py_DECREF(opath);
9609 if (res < 0)
9610 return posix_error();
9611 Py_RETURN_NONE;
9612}
9613#endif /* HAVE_FCHMODAT */
9614
9615#ifdef HAVE_FCHOWNAT
9616PyDoc_STRVAR(posix_fchownat__doc__,
9617"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9618Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9619flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9620If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9621is interpreted relative to the current working directory.");
9622
9623static PyObject *
9624posix_fchownat(PyObject *self, PyObject *args)
9625{
9626 PyObject *opath;
9627 int dirfd, res;
9628 long uid, gid;
9629 int flags = 0;
9630 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009631
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009632 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9633 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9634 return NULL;
9635
9636 path = PyBytes_AsString(opath);
9637
9638 Py_BEGIN_ALLOW_THREADS
9639 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9640 Py_END_ALLOW_THREADS
9641 Py_DECREF(opath);
9642 if (res < 0)
9643 return posix_error();
9644 Py_RETURN_NONE;
9645}
9646#endif /* HAVE_FCHOWNAT */
9647
9648#ifdef HAVE_FSTATAT
9649PyDoc_STRVAR(posix_fstatat__doc__,
9650"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9651Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9652flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9653If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9654is interpreted relative to the current working directory.");
9655
9656static PyObject *
9657posix_fstatat(PyObject *self, PyObject *args)
9658{
9659 PyObject *opath;
9660 char *path;
9661 STRUCT_STAT st;
9662 int dirfd, res, flags = 0;
9663
9664 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9665 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9666 return NULL;
9667 path = PyBytes_AsString(opath);
9668
9669 Py_BEGIN_ALLOW_THREADS
9670 res = fstatat(dirfd, path, &st, flags);
9671 Py_END_ALLOW_THREADS
9672 Py_DECREF(opath);
9673 if (res != 0)
9674 return posix_error();
9675
9676 return _pystat_fromstructstat(&st);
9677}
9678#endif
9679
9680#ifdef HAVE_FUTIMESAT
9681PyDoc_STRVAR(posix_futimesat__doc__,
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009682"futimesat(dirfd, path[, (atime, mtime)])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009683Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9684If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9685is interpreted relative to the current working directory.");
9686
9687static PyObject *
9688posix_futimesat(PyObject *self, PyObject *args)
9689{
9690 PyObject *opath;
9691 char *path;
9692 int res, dirfd;
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009693 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009694 time_t atime, mtime;
9695 long ausec, musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009696
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009697 if (!PyArg_ParseTuple(args, "iO&|O:futimesat",
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009698 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9699 return NULL;
9700 path = PyBytes_AsString(opath);
9701 if (arg == Py_None) {
9702 /* optional time values not given */
9703 Py_BEGIN_ALLOW_THREADS
9704 res = futimesat(dirfd, path, NULL);
9705 Py_END_ALLOW_THREADS
9706 }
9707 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9708 PyErr_SetString(PyExc_TypeError,
9709 "futimesat() arg 3 must be a tuple (atime, mtime)");
9710 Py_DECREF(opath);
9711 return NULL;
9712 }
9713 else {
9714 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009715 &atime, &ausec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009716 Py_DECREF(opath);
9717 return NULL;
9718 }
9719 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009720 &mtime, &musec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009721 Py_DECREF(opath);
9722 return NULL;
9723 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009724
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009725 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009726 {
9727#ifdef HAVE_UTIMENSAT
9728 struct timespec buf[2];
9729 buf[0].tv_sec = atime;
9730 buf[0].tv_nsec = ausec;
9731 buf[1].tv_sec = mtime;
9732 buf[1].tv_nsec = musec;
9733 res = utimensat(dirfd, path, buf, 0);
9734#else
9735 struct timeval buf[2];
9736 buf[0].tv_sec = atime;
9737 buf[0].tv_usec = ausec;
9738 buf[1].tv_sec = mtime;
9739 buf[1].tv_usec = musec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009740 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009741#endif
9742 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009743 Py_END_ALLOW_THREADS
9744 }
9745 Py_DECREF(opath);
9746 if (res < 0) {
9747 return posix_error();
9748 }
9749 Py_RETURN_NONE;
9750}
9751#endif
9752
9753#ifdef HAVE_LINKAT
9754PyDoc_STRVAR(posix_linkat__doc__,
9755"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9756Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9757and if dstpath is relative, it is taken as relative to dstfd.\n\
9758flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9759If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9760srcpath is interpreted relative to the current working directory. This\n\
9761also applies for dstpath.");
9762
9763static PyObject *
9764posix_linkat(PyObject *self, PyObject *args)
9765{
9766 PyObject *osrc, *odst;
9767 char *src, *dst;
9768 int res, srcfd, dstfd;
9769 int flags = 0;
9770
9771 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9772 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9773 return NULL;
9774 src = PyBytes_AsString(osrc);
9775 dst = PyBytes_AsString(odst);
9776 Py_BEGIN_ALLOW_THREADS
9777 res = linkat(srcfd, src, dstfd, dst, flags);
9778 Py_END_ALLOW_THREADS
9779 Py_DECREF(osrc);
9780 Py_DECREF(odst);
9781 if (res < 0)
9782 return posix_error();
9783 Py_RETURN_NONE;
9784}
9785#endif /* HAVE_LINKAT */
9786
9787#ifdef HAVE_MKDIRAT
9788PyDoc_STRVAR(posix_mkdirat__doc__,
9789"mkdirat(dirfd, path, mode=0o777)\n\n\
9790Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9791If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9792is interpreted relative to the current working directory.");
9793
9794static PyObject *
9795posix_mkdirat(PyObject *self, PyObject *args)
9796{
9797 int res, dirfd;
9798 PyObject *opath;
9799 char *path;
9800 int mode = 0777;
9801
9802 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9803 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9804 return NULL;
9805 path = PyBytes_AsString(opath);
9806 Py_BEGIN_ALLOW_THREADS
9807 res = mkdirat(dirfd, path, mode);
9808 Py_END_ALLOW_THREADS
9809 Py_DECREF(opath);
9810 if (res < 0)
9811 return posix_error();
9812 Py_RETURN_NONE;
9813}
9814#endif
9815
9816#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9817PyDoc_STRVAR(posix_mknodat__doc__,
9818"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9819Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9820If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9821is interpreted relative to the current working directory.");
9822
9823static PyObject *
9824posix_mknodat(PyObject *self, PyObject *args)
9825{
9826 PyObject *opath;
9827 char *filename;
9828 int mode = 0600;
9829 int device = 0;
9830 int res, dirfd;
9831 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9832 PyUnicode_FSConverter, &opath, &mode, &device))
9833 return NULL;
9834 filename = PyBytes_AS_STRING(opath);
9835 Py_BEGIN_ALLOW_THREADS
9836 res = mknodat(dirfd, filename, mode, device);
9837 Py_END_ALLOW_THREADS
9838 Py_DECREF(opath);
9839 if (res < 0)
9840 return posix_error();
9841 Py_RETURN_NONE;
9842}
9843#endif
9844
9845#ifdef HAVE_OPENAT
9846PyDoc_STRVAR(posix_openat__doc__,
9847"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9848Like open() but if path is relative, it is taken as relative to dirfd.\n\
9849If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9850is interpreted relative to the current working directory.");
9851
9852static PyObject *
9853posix_openat(PyObject *self, PyObject *args)
9854{
9855 PyObject *ofile;
9856 char *file;
9857 int flag, dirfd, fd;
9858 int mode = 0777;
9859
9860 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9861 &dirfd, PyUnicode_FSConverter, &ofile,
9862 &flag, &mode))
9863 return NULL;
9864 file = PyBytes_AsString(ofile);
9865 Py_BEGIN_ALLOW_THREADS
9866 fd = openat(dirfd, file, flag, mode);
9867 Py_END_ALLOW_THREADS
9868 Py_DECREF(ofile);
9869 if (fd < 0)
9870 return posix_error();
9871 return PyLong_FromLong((long)fd);
9872}
9873#endif
9874
9875#ifdef HAVE_READLINKAT
9876PyDoc_STRVAR(posix_readlinkat__doc__,
9877"readlinkat(dirfd, path) -> path\n\n\
9878Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9879If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9880is interpreted relative to the current working directory.");
9881
9882static PyObject *
9883posix_readlinkat(PyObject *self, PyObject *args)
9884{
9885 PyObject *v, *opath;
9886 char buf[MAXPATHLEN];
9887 char *path;
9888 int n, dirfd;
9889 int arg_is_unicode = 0;
9890
9891 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9892 &dirfd, PyUnicode_FSConverter, &opath))
9893 return NULL;
9894 path = PyBytes_AsString(opath);
9895 v = PySequence_GetItem(args, 1);
9896 if (v == NULL) {
9897 Py_DECREF(opath);
9898 return NULL;
9899 }
9900
9901 if (PyUnicode_Check(v)) {
9902 arg_is_unicode = 1;
9903 }
9904 Py_DECREF(v);
9905
9906 Py_BEGIN_ALLOW_THREADS
9907 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9908 Py_END_ALLOW_THREADS
9909 Py_DECREF(opath);
9910 if (n < 0)
9911 return posix_error();
9912
9913 if (arg_is_unicode)
9914 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9915 else
9916 return PyBytes_FromStringAndSize(buf, n);
9917}
9918#endif /* HAVE_READLINKAT */
9919
9920#ifdef HAVE_RENAMEAT
9921PyDoc_STRVAR(posix_renameat__doc__,
9922"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9923Like rename() but if oldpath is relative, it is taken as relative to\n\
9924olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9925If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9926oldpath is interpreted relative to the current working directory. This\n\
9927also applies for newpath.");
9928
9929static PyObject *
9930posix_renameat(PyObject *self, PyObject *args)
9931{
9932 int res;
9933 PyObject *opathold, *opathnew;
9934 char *opath, *npath;
9935 int oldfd, newfd;
9936
9937 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9938 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9939 return NULL;
9940 opath = PyBytes_AsString(opathold);
9941 npath = PyBytes_AsString(opathnew);
9942 Py_BEGIN_ALLOW_THREADS
9943 res = renameat(oldfd, opath, newfd, npath);
9944 Py_END_ALLOW_THREADS
9945 Py_DECREF(opathold);
9946 Py_DECREF(opathnew);
9947 if (res < 0)
9948 return posix_error();
9949 Py_RETURN_NONE;
9950}
9951#endif
9952
9953#if HAVE_SYMLINKAT
9954PyDoc_STRVAR(posix_symlinkat__doc__,
9955"symlinkat(src, dstfd, dst)\n\n\
9956Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9957If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9958is interpreted relative to the current working directory.");
9959
9960static PyObject *
9961posix_symlinkat(PyObject *self, PyObject *args)
9962{
9963 int res, dstfd;
9964 PyObject *osrc, *odst;
9965 char *src, *dst;
9966
9967 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9968 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9969 return NULL;
9970 src = PyBytes_AsString(osrc);
9971 dst = PyBytes_AsString(odst);
9972 Py_BEGIN_ALLOW_THREADS
9973 res = symlinkat(src, dstfd, dst);
9974 Py_END_ALLOW_THREADS
9975 Py_DECREF(osrc);
9976 Py_DECREF(odst);
9977 if (res < 0)
9978 return posix_error();
9979 Py_RETURN_NONE;
9980}
9981#endif /* HAVE_SYMLINKAT */
9982
9983#ifdef HAVE_UNLINKAT
9984PyDoc_STRVAR(posix_unlinkat__doc__,
9985"unlinkat(dirfd, path, flags=0)\n\n\
9986Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9987flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9988specified, unlinkat() behaves like rmdir().\n\
9989If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9990is interpreted relative to the current working directory.");
9991
9992static PyObject *
9993posix_unlinkat(PyObject *self, PyObject *args)
9994{
9995 int dirfd, res, flags = 0;
9996 PyObject *opath;
9997 char *path;
9998
9999 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
10000 &dirfd, PyUnicode_FSConverter, &opath, &flags))
10001 return NULL;
10002 path = PyBytes_AsString(opath);
10003 Py_BEGIN_ALLOW_THREADS
10004 res = unlinkat(dirfd, path, flags);
10005 Py_END_ALLOW_THREADS
10006 Py_DECREF(opath);
10007 if (res < 0)
10008 return posix_error();
10009 Py_RETURN_NONE;
10010}
10011#endif
10012
10013#ifdef HAVE_UTIMENSAT
10014PyDoc_STRVAR(posix_utimensat__doc__,
Brian Curtin569b4942011-11-07 16:09:20 -060010015"utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\
10016 mtime=(mtime_sec, mtime_nsec), flags=0])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010017utimensat(dirfd, path, None, None, flags)\n\n\
10018Updates the timestamps of a file with nanosecond precision. If path is\n\
10019relative, it is taken as relative to dirfd.\n\
Brian Curtin569b4942011-11-07 16:09:20 -060010020If atime and mtime are both None, which is the default, set atime and\n\
10021mtime to the current time.\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010022flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
10023If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10024is interpreted relative to the current working directory.\n\
10025If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
10026current time.\n\
10027If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
10028
10029static PyObject *
Brian Curtin569b4942011-11-07 16:09:20 -060010030posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010031{
10032 PyObject *opath;
10033 char *path;
10034 int res, dirfd, flags = 0;
Brian Curtin569b4942011-11-07 16:09:20 -060010035 PyObject *atime = Py_None;
10036 PyObject *mtime = Py_None;
10037
10038 static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL};
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010039
10040 struct timespec buf[2];
10041
Brian Curtin569b4942011-11-07 16:09:20 -060010042 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist,
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010043 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
10044 return NULL;
10045 path = PyBytes_AsString(opath);
10046 if (atime == Py_None && mtime == Py_None) {
10047 /* optional time values not given */
10048 Py_BEGIN_ALLOW_THREADS
10049 res = utimensat(dirfd, path, NULL, flags);
10050 Py_END_ALLOW_THREADS
10051 }
10052 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
10053 PyErr_SetString(PyExc_TypeError,
10054 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
10055 Py_DECREF(opath);
10056 return NULL;
10057 }
10058 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
10059 PyErr_SetString(PyExc_TypeError,
10060 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
10061 Py_DECREF(opath);
10062 return NULL;
10063 }
10064 else {
10065 if (!PyArg_ParseTuple(atime, "ll:utimensat",
10066 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
10067 Py_DECREF(opath);
10068 return NULL;
10069 }
10070 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
10071 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
10072 Py_DECREF(opath);
10073 return NULL;
10074 }
10075 Py_BEGIN_ALLOW_THREADS
10076 res = utimensat(dirfd, path, buf, flags);
10077 Py_END_ALLOW_THREADS
10078 }
10079 Py_DECREF(opath);
10080 if (res < 0) {
10081 return posix_error();
10082 }
10083 Py_RETURN_NONE;
10084}
10085#endif
10086
10087#ifdef HAVE_MKFIFOAT
10088PyDoc_STRVAR(posix_mkfifoat__doc__,
10089"mkfifoat(dirfd, path, mode=0o666)\n\n\
10090Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
10091If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10092is interpreted relative to the current working directory.");
10093
10094static PyObject *
10095posix_mkfifoat(PyObject *self, PyObject *args)
10096{
10097 PyObject *opath;
10098 char *filename;
10099 int mode = 0666;
10100 int res, dirfd;
10101 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
10102 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10103 return NULL;
10104 filename = PyBytes_AS_STRING(opath);
10105 Py_BEGIN_ALLOW_THREADS
10106 res = mkfifoat(dirfd, filename, mode);
10107 Py_END_ALLOW_THREADS
10108 Py_DECREF(opath);
10109 if (res < 0)
10110 return posix_error();
10111 Py_RETURN_NONE;
10112}
10113#endif
10114
Benjamin Peterson9428d532011-09-14 11:45:52 -040010115#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010116
10117static int
10118try_getxattr(const char *path, const char *name,
10119 ssize_t (*get)(const char *, const char *, void *, size_t),
10120 Py_ssize_t buf_size, PyObject **res)
10121{
10122 PyObject *value;
10123 Py_ssize_t len;
10124
10125 assert(buf_size <= XATTR_SIZE_MAX);
10126 value = PyBytes_FromStringAndSize(NULL, buf_size);
10127 if (!value)
10128 return 0;
10129 Py_BEGIN_ALLOW_THREADS;
10130 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10131 Py_END_ALLOW_THREADS;
10132 if (len < 0) {
10133 Py_DECREF(value);
10134 if (errno == ERANGE) {
10135 value = NULL;
10136 }
10137 else {
10138 posix_error();
10139 return 0;
10140 }
10141 }
10142 else if (len != buf_size) {
10143 /* Can only shrink. */
10144 _PyBytes_Resize(&value, len);
10145 }
10146 *res = value;
10147 return 1;
10148}
10149
10150static PyObject *
10151getxattr_common(const char *path, PyObject *name_obj,
10152 ssize_t (*get)(const char *, const char *, void *, size_t))
10153{
10154 PyObject *value;
10155 const char *name = PyBytes_AS_STRING(name_obj);
10156
10157 /* Try a small value first. */
10158 if (!try_getxattr(path, name, get, 128, &value))
10159 return NULL;
10160 if (value)
10161 return value;
10162 /* Now the maximum possible one. */
10163 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10164 return NULL;
10165 assert(value);
10166 return value;
10167}
10168
10169PyDoc_STRVAR(posix_getxattr__doc__,
10170"getxattr(path, attr) -> value\n\n\
10171Return the value of extended attribute *name* on *path*.");
10172
10173static PyObject *
10174posix_getxattr(PyObject *self, PyObject *args)
10175{
10176 PyObject *path, *res, *name;
10177
10178 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10179 PyUnicode_FSConverter, &name))
10180 return NULL;
10181 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10182 Py_DECREF(path);
10183 Py_DECREF(name);
10184 return res;
10185}
10186
10187PyDoc_STRVAR(posix_lgetxattr__doc__,
10188"lgetxattr(path, attr) -> value\n\n\
10189Like getxattr but don't follow symlinks.");
10190
10191static PyObject *
10192posix_lgetxattr(PyObject *self, PyObject *args)
10193{
10194 PyObject *path, *res, *name;
10195
10196 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10197 PyUnicode_FSConverter, &name))
10198 return NULL;
10199 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10200 Py_DECREF(path);
10201 Py_DECREF(name);
10202 return res;
10203}
10204
10205static ssize_t
10206wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10207{
10208 /* Hack to share code. */
10209 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10210}
10211
10212PyDoc_STRVAR(posix_fgetxattr__doc__,
10213"fgetxattr(fd, attr) -> value\n\n\
10214Like getxattr but operate on a fd instead of a path.");
10215
10216static PyObject *
10217posix_fgetxattr(PyObject *self, PyObject *args)
10218{
10219 PyObject *res, *name;
10220 int fd;
10221
10222 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10223 return NULL;
10224 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10225 Py_DECREF(name);
10226 return res;
10227}
10228
10229PyDoc_STRVAR(posix_setxattr__doc__,
10230"setxattr(path, attr, value, flags=0)\n\n\
10231Set extended attribute *attr* on *path* to *value*.");
10232
10233static PyObject *
10234posix_setxattr(PyObject *self, PyObject *args)
10235{
10236 PyObject *path, *name;
10237 Py_buffer data;
10238 int flags = 0, err;
10239
10240 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10241 &path, PyUnicode_FSConverter, &name, &data, &flags))
10242 return NULL;
10243 Py_BEGIN_ALLOW_THREADS;
10244 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10245 data.buf, data.len, flags);
10246 Py_END_ALLOW_THREADS;
10247 Py_DECREF(path);
10248 Py_DECREF(name);
10249 PyBuffer_Release(&data);
10250 if (err)
10251 return posix_error();
10252 Py_RETURN_NONE;
10253}
10254
10255PyDoc_STRVAR(posix_lsetxattr__doc__,
10256"lsetxattr(path, attr, value, flags=0)\n\n\
10257Like setxattr but don't follow symlinks.");
10258
10259static PyObject *
10260posix_lsetxattr(PyObject *self, PyObject *args)
10261{
10262 PyObject *path, *name;
10263 Py_buffer data;
10264 int flags = 0, err;
10265
10266 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10267 &path, PyUnicode_FSConverter, &name, &data, &flags))
10268 return NULL;
10269 Py_BEGIN_ALLOW_THREADS;
10270 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10271 data.buf, data.len, flags);
10272 Py_END_ALLOW_THREADS;
10273 Py_DECREF(path);
10274 Py_DECREF(name);
10275 PyBuffer_Release(&data);
10276 if (err)
10277 return posix_error();
10278 Py_RETURN_NONE;
10279}
10280
10281PyDoc_STRVAR(posix_fsetxattr__doc__,
10282"fsetxattr(fd, attr, value, flags=0)\n\n\
10283Like setxattr but operates on *fd* instead of a path.");
10284
10285static PyObject *
10286posix_fsetxattr(PyObject *self, PyObject *args)
10287{
10288 Py_buffer data;
10289 const char *name;
10290 int fd, flags = 0, err;
10291
10292 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10293 &name, &data, &flags))
10294 return NULL;
10295 Py_BEGIN_ALLOW_THREADS;
10296 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10297 Py_END_ALLOW_THREADS;
10298 Py_DECREF(name);
10299 PyBuffer_Release(&data);
10300 if (err)
10301 return posix_error();
10302 Py_RETURN_NONE;
10303}
10304
10305PyDoc_STRVAR(posix_removexattr__doc__,
10306"removexattr(path, attr)\n\n\
10307Remove extended attribute *attr* on *path*.");
10308
10309static PyObject *
10310posix_removexattr(PyObject *self, PyObject *args)
10311{
10312 PyObject *path, *name;
10313 int err;
10314
10315 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10316 PyUnicode_FSConverter, &name))
10317 return NULL;
10318 Py_BEGIN_ALLOW_THREADS;
10319 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10320 Py_END_ALLOW_THREADS;
10321 Py_DECREF(path);
10322 Py_DECREF(name);
10323 if (err)
10324 return posix_error();
10325 Py_RETURN_NONE;
10326}
10327
10328PyDoc_STRVAR(posix_lremovexattr__doc__,
10329"lremovexattr(path, attr)\n\n\
10330Like removexattr but don't follow symlinks.");
10331
10332static PyObject *
10333posix_lremovexattr(PyObject *self, PyObject *args)
10334{
10335 PyObject *path, *name;
10336 int err;
10337
10338 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10339 PyUnicode_FSConverter, &name))
10340 return NULL;
10341 Py_BEGIN_ALLOW_THREADS;
10342 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10343 Py_END_ALLOW_THREADS;
10344 Py_DECREF(path);
10345 Py_DECREF(name);
10346 if (err)
10347 return posix_error();
10348 Py_RETURN_NONE;
10349}
10350
10351PyDoc_STRVAR(posix_fremovexattr__doc__,
10352"fremovexattr(fd, attr)\n\n\
10353Like removexattr but operates on a file descriptor.");
10354
10355static PyObject *
10356posix_fremovexattr(PyObject *self, PyObject *args)
10357{
10358 PyObject *name;
10359 int fd, err;
10360
10361 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10362 PyUnicode_FSConverter, &name))
10363 return NULL;
10364 Py_BEGIN_ALLOW_THREADS;
10365 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10366 Py_END_ALLOW_THREADS;
10367 Py_DECREF(name);
10368 if (err)
10369 return posix_error();
10370 Py_RETURN_NONE;
10371}
10372
10373static Py_ssize_t
10374try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10375 Py_ssize_t buf_size, char **buf)
10376{
10377 Py_ssize_t len;
10378
10379 *buf = PyMem_MALLOC(buf_size);
10380 if (!*buf) {
10381 PyErr_NoMemory();
10382 return -1;
10383 }
10384 Py_BEGIN_ALLOW_THREADS;
10385 len = list(path, *buf, buf_size);
10386 Py_END_ALLOW_THREADS;
10387 if (len < 0) {
10388 PyMem_FREE(*buf);
10389 if (errno != ERANGE)
10390 posix_error();
10391 return -1;
10392 }
10393 return len;
10394}
10395
10396static PyObject *
10397listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10398{
10399 PyObject *res, *attr;
10400 Py_ssize_t len, err, start, i;
10401 char *buf;
10402
10403 len = try_listxattr(path, list, 256, &buf);
10404 if (len < 0) {
10405 if (PyErr_Occurred())
10406 return NULL;
10407 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10408 if (len < 0)
10409 return NULL;
10410 }
10411 res = PyList_New(0);
10412 if (!res) {
10413 PyMem_FREE(buf);
10414 return NULL;
10415 }
10416 for (start = i = 0; i < len; i++) {
10417 if (!buf[i]) {
10418 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10419 if (!attr) {
10420 Py_DECREF(res);
10421 PyMem_FREE(buf);
10422 return NULL;
10423 }
10424 err = PyList_Append(res, attr);
10425 Py_DECREF(attr);
10426 if (err) {
10427 Py_DECREF(res);
10428 PyMem_FREE(buf);
10429 return NULL;
10430 }
10431 start = i + 1;
10432 }
10433 }
10434 PyMem_FREE(buf);
10435 return res;
10436}
10437
10438PyDoc_STRVAR(posix_listxattr__doc__,
10439"listxattr(path)\n\n\
10440Return a list of extended attributes on *path*.");
10441
10442static PyObject *
10443posix_listxattr(PyObject *self, PyObject *args)
10444{
10445 PyObject *path, *res;
10446
10447 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10448 return NULL;
10449 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10450 Py_DECREF(path);
10451 return res;
10452}
10453
10454PyDoc_STRVAR(posix_llistxattr__doc__,
10455"llistxattr(path)\n\n\
10456Like listxattr but don't follow symlinks..");
10457
10458static PyObject *
10459posix_llistxattr(PyObject *self, PyObject *args)
10460{
10461 PyObject *path, *res;
10462
10463 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10464 return NULL;
10465 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10466 Py_DECREF(path);
10467 return res;
10468}
10469
10470static ssize_t
10471wrap_flistxattr(const char *path, char *buf, size_t len)
10472{
10473 /* Hack to share code. */
10474 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10475}
10476
10477PyDoc_STRVAR(posix_flistxattr__doc__,
10478"flistxattr(path)\n\n\
10479Like flistxattr but operates on a file descriptor.");
10480
10481static PyObject *
10482posix_flistxattr(PyObject *self, PyObject *args)
10483{
10484 long fd;
10485
10486 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10487 return NULL;
10488 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10489}
10490
Benjamin Peterson9428d532011-09-14 11:45:52 -040010491#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010492
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010493static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010495#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010496 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010497#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010498 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010499#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010501#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010502 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010503#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010504 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010505#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010506#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010507 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010508#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010509#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010510 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010511#endif /* HAVE_LCHMOD */
10512#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010513 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010514#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010515#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010516 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010517#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010518#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010520#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010521#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010523#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010524#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010525 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010526#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010527#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010528 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10529 METH_NOARGS, posix_getcwd__doc__},
10530 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10531 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010532#endif
10533#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010534 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010535#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010537#ifdef HAVE_FDOPENDIR
10538 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10539#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010540 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10541 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010542#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010543 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010544#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010545#ifdef HAVE_GETPRIORITY
10546 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10547#endif /* HAVE_GETPRIORITY */
10548#ifdef HAVE_SETPRIORITY
10549 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10550#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010551#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010552 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010553#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010554#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010555 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010556#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010557 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10558 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10559 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010560 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010561#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010562 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010563#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010564#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010565 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010566 win_symlink__doc__},
10567#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010568#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010570#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010571 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010572#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010574#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10576 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10577 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010578#ifdef HAVE_FUTIMES
10579 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10580#endif
10581#ifdef HAVE_LUTIMES
10582 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10583#endif
10584#ifdef HAVE_FUTIMENS
10585 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10586#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010587#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010588 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010589#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010591#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010592 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10593 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010594#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010595#ifdef HAVE_FEXECVE
10596 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10597#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010598#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10600 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010601#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010602 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10603 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010604#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010605#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010606#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010607 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010608#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010609#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010610 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010611#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010612#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010613#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010614 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10615 {"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 +020010616#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010617#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010618 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010619#endif
10620#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010621 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010622#endif
10623#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010624 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010625#endif
10626#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010627 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010628#endif
10629#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010630 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010631#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010632 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010633#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010634 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10635 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10636#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010637#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010638#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010639 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010640#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010641#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010642 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010643#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010644#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010645 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010646#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010647#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010648 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010649#endif /* HAVE_GETEUID */
10650#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010651 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010652#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010653#ifdef HAVE_GETGROUPLIST
10654 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10655#endif
Fred Drakec9680921999-12-13 16:37:25 +000010656#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010657 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010658#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010660#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010662#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010663#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010665#endif /* HAVE_GETPPID */
10666#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010667 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010668#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010669#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010671#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010672#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010673 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010674#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010675#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010676 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010677#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010678#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010679 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010680#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010681#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010682 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10683 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010684 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010685#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010686#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010687 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010688#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010689#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010690 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010691#endif /* HAVE_SETEUID */
10692#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010693 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010694#endif /* HAVE_SETEGID */
10695#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010696 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010697#endif /* HAVE_SETREUID */
10698#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010699 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010700#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010701#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010702 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010703#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010704#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010705 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010706#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010707#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010708 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010709#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010710#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010711 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010712#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010713#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010715#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010716#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010717 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010718#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010719#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010720 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010721#endif /* HAVE_WAIT3 */
10722#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010723 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010724#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010725#if defined(HAVE_WAITID) && !defined(__APPLE__)
10726 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10727#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010728#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010729 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010730#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010731#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010732 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010733#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010734#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010735 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010736#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010737#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010738 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010739#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010740#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010741 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010742#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010743#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010744 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010745#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10747 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10748 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10749 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10750 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10751 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010752#ifdef HAVE_LOCKF
10753 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10754#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10756 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010757#ifdef HAVE_READV
10758 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10759#endif
10760#ifdef HAVE_PREAD
10761 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10762#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010763 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010764#ifdef HAVE_WRITEV
10765 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10766#endif
10767#ifdef HAVE_PWRITE
10768 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10769#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010770#ifdef HAVE_SENDFILE
10771 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10772 posix_sendfile__doc__},
10773#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010774 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10775 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010776#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010777 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010778#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010779#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010780 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010781#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010782#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010783 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010784#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010785#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010786 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010787#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010788#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010789 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10790 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10791 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010792#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010793#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010795#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010796#ifdef HAVE_TRUNCATE
10797 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10798#endif
10799#ifdef HAVE_POSIX_FALLOCATE
10800 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10801#endif
10802#ifdef HAVE_POSIX_FADVISE
10803 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10804#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010805#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010807#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010808#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010810#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010811 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010812#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010813 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010814#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010815#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010816 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010817#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010818#ifdef HAVE_SYNC
10819 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10820#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010821#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010823#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010824#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010825#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010826 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010827#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010828#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010829 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010830#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010831#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010832 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010833#endif /* WIFSTOPPED */
10834#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010835 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010836#endif /* WIFSIGNALED */
10837#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010838 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010839#endif /* WIFEXITED */
10840#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010841 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010842#endif /* WEXITSTATUS */
10843#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010844 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010845#endif /* WTERMSIG */
10846#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010847 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010848#endif /* WSTOPSIG */
10849#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010850#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010852#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010853#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010855#endif
Fred Drakec9680921999-12-13 16:37:25 +000010856#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010858#endif
10859#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010861#endif
10862#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010864#endif
10865#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010866 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010867#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010869#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010870 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010871 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010872 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010873 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010874 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010875#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010876#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010877 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010878#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010879 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010880 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010881 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010882 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010884 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010885#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010886 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010887#endif
10888#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010890#endif
10891#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010892 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010893#endif
10894#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010895 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010896#endif
10897
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010898/* posix *at family of functions */
10899#ifdef HAVE_FACCESSAT
10900 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10901#endif
10902#ifdef HAVE_FCHMODAT
10903 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10904#endif /* HAVE_FCHMODAT */
10905#ifdef HAVE_FCHOWNAT
10906 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10907#endif /* HAVE_FCHOWNAT */
10908#ifdef HAVE_FSTATAT
10909 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10910#endif
10911#ifdef HAVE_FUTIMESAT
10912 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10913#endif
10914#ifdef HAVE_LINKAT
10915 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10916#endif /* HAVE_LINKAT */
10917#ifdef HAVE_MKDIRAT
10918 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10919#endif
10920#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10921 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10922#endif
10923#ifdef HAVE_OPENAT
10924 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10925#endif
10926#ifdef HAVE_READLINKAT
10927 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10928#endif /* HAVE_READLINKAT */
10929#ifdef HAVE_RENAMEAT
10930 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10931#endif
10932#if HAVE_SYMLINKAT
10933 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10934#endif /* HAVE_SYMLINKAT */
10935#ifdef HAVE_UNLINKAT
10936 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10937#endif
10938#ifdef HAVE_UTIMENSAT
Jesus Cead03a4912011-11-08 17:28:04 +010010939 {"utimensat", (PyCFunction)posix_utimensat,
10940 METH_VARARGS | METH_KEYWORDS,
Brian Curtin569b4942011-11-07 16:09:20 -060010941 posix_utimensat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010942#endif
10943#ifdef HAVE_MKFIFOAT
10944 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10945#endif
Benjamin Peterson9428d532011-09-14 11:45:52 -040010946#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010947 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
10948 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
10949 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
10950 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
10951 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
10952 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
10953 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
10954 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
10955 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
10956 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
10957 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
10958 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
10959#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010960 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010961};
10962
10963
Barry Warsaw4a342091996-12-19 23:50:02 +000010964static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010965ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010966{
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010968}
10969
Guido van Rossumd48f2521997-12-05 22:19:34 +000010970#if defined(PYOS_OS2)
10971/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010972static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010973{
10974 APIRET rc;
10975 ULONG values[QSV_MAX+1];
10976 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010977 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010978
10979 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010980 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010981 Py_END_ALLOW_THREADS
10982
10983 if (rc != NO_ERROR) {
10984 os2_error(rc);
10985 return -1;
10986 }
10987
Fred Drake4d1e64b2002-04-15 19:40:07 +000010988 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10989 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10990 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10991 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10992 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10993 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10994 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010995
10996 switch (values[QSV_VERSION_MINOR]) {
10997 case 0: ver = "2.00"; break;
10998 case 10: ver = "2.10"; break;
10999 case 11: ver = "2.11"; break;
11000 case 30: ver = "3.00"; break;
11001 case 40: ver = "4.00"; break;
11002 case 50: ver = "5.00"; break;
11003 default:
Tim Peters885d4572001-11-28 20:27:42 +000011004 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000011005 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000011006 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011007 ver = &tmp[0];
11008 }
11009
11010 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011011 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011012 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011013
11014 /* Add Indicator of Which Drive was Used to Boot the System */
11015 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
11016 tmp[1] = ':';
11017 tmp[2] = '\0';
11018
Fred Drake4d1e64b2002-04-15 19:40:07 +000011019 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011020}
11021#endif
11022
Brian Curtin52173d42010-12-02 18:29:18 +000011023#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011024static int
Brian Curtin52173d42010-12-02 18:29:18 +000011025enable_symlink()
11026{
11027 HANDLE tok;
11028 TOKEN_PRIVILEGES tok_priv;
11029 LUID luid;
11030 int meth_idx = 0;
11031
11032 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011033 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011034
11035 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011036 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011037
11038 tok_priv.PrivilegeCount = 1;
11039 tok_priv.Privileges[0].Luid = luid;
11040 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
11041
11042 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
11043 sizeof(TOKEN_PRIVILEGES),
11044 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011045 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011046
Brian Curtin3b4499c2010-12-28 14:31:47 +000011047 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
11048 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000011049}
11050#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
11051
Barry Warsaw4a342091996-12-19 23:50:02 +000011052static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011053all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000011054{
Guido van Rossum94f6f721999-01-06 18:42:14 +000011055#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011056 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011057#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011058#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011059 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011060#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011061#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011062 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011063#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011064#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011065 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011066#endif
Fred Drakec9680921999-12-13 16:37:25 +000011067#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011068 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000011069#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011070#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011071 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011072#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011073#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011074 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011075#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011076#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011077 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011078#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011079#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011080 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011081#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011082#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011083 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011084#endif
11085#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011086 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011087#endif
11088#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011089 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011090#endif
11091#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011092 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011093#endif
11094#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011095 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011096#endif
11097#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011098 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011099#endif
11100#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011101 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011102#endif
11103#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011104 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011105#endif
11106#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011107 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011108#endif
11109#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011110 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011111#endif
11112#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011113 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011114#endif
11115#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011116 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011117#endif
11118#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011119 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011120#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011121#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011122 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011123#endif
11124#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011125 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011126#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011127#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011128 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011129#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011130#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011131 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011132#endif
11133#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011134 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011135#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011136#ifdef PRIO_PROCESS
11137 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11138#endif
11139#ifdef PRIO_PGRP
11140 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11141#endif
11142#ifdef PRIO_USER
11143 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11144#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011145#ifdef O_CLOEXEC
11146 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11147#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011148/* posix - constants for *at functions */
11149#ifdef AT_SYMLINK_NOFOLLOW
11150 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11151#endif
11152#ifdef AT_EACCESS
11153 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11154#endif
11155#ifdef AT_FDCWD
11156 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11157#endif
11158#ifdef AT_REMOVEDIR
11159 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11160#endif
11161#ifdef AT_SYMLINK_FOLLOW
11162 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11163#endif
11164#ifdef UTIME_NOW
11165 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11166#endif
11167#ifdef UTIME_OMIT
11168 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11169#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011170
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011171
Tim Peters5aa91602002-01-30 05:46:57 +000011172/* MS Windows */
11173#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011174 /* Don't inherit in child processes. */
11175 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011176#endif
11177#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011178 /* Optimize for short life (keep in memory). */
11179 /* MS forgot to define this one with a non-underscore form too. */
11180 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011181#endif
11182#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 /* Automatically delete when last handle is closed. */
11184 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011185#endif
11186#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011187 /* Optimize for random access. */
11188 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011189#endif
11190#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011191 /* Optimize for sequential access. */
11192 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011193#endif
11194
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011195/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011196#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011197 /* Send a SIGIO signal whenever input or output
11198 becomes available on file descriptor */
11199 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011200#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011201#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 /* Direct disk access. */
11203 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011204#endif
11205#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011206 /* Must be a directory. */
11207 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011208#endif
11209#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011210 /* Do not follow links. */
11211 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011212#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011213#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011214 /* Do not update the access time. */
11215 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011216#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011217
Victor Stinner8c62be82010-05-06 00:08:46 +000011218 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011219#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011220 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011221#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011222#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011223 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011224#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011225#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011226 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011227#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011228#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011229 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011230#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011231#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011232 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011233#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011234#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011235 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011236#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011237#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011238 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011239#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011240#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011241 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011242#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011243#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011244 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011245#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011246#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011247 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011248#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011249#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011250 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011251#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011252#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011253 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011254#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011255#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011256 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011257#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011258#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011259 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011260#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011261#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011263#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011264#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011265 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011266#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011267#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011268 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011269#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011270
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011271 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011272#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011273 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011274#endif /* ST_RDONLY */
11275#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011276 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011277#endif /* ST_NOSUID */
11278
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011279 /* FreeBSD sendfile() constants */
11280#ifdef SF_NODISKIO
11281 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11282#endif
11283#ifdef SF_MNOWAIT
11284 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11285#endif
11286#ifdef SF_SYNC
11287 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11288#endif
11289
Ross Lagerwall7807c352011-03-17 20:20:30 +020011290 /* constants for posix_fadvise */
11291#ifdef POSIX_FADV_NORMAL
11292 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11293#endif
11294#ifdef POSIX_FADV_SEQUENTIAL
11295 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11296#endif
11297#ifdef POSIX_FADV_RANDOM
11298 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11299#endif
11300#ifdef POSIX_FADV_NOREUSE
11301 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11302#endif
11303#ifdef POSIX_FADV_WILLNEED
11304 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11305#endif
11306#ifdef POSIX_FADV_DONTNEED
11307 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11308#endif
11309
11310 /* constants for waitid */
11311#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11312 if (ins(d, "P_PID", (long)P_PID)) return -1;
11313 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11314 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11315#endif
11316#ifdef WEXITED
11317 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11318#endif
11319#ifdef WNOWAIT
11320 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11321#endif
11322#ifdef WSTOPPED
11323 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11324#endif
11325#ifdef CLD_EXITED
11326 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11327#endif
11328#ifdef CLD_DUMPED
11329 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11330#endif
11331#ifdef CLD_TRAPPED
11332 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11333#endif
11334#ifdef CLD_CONTINUED
11335 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11336#endif
11337
11338 /* constants for lockf */
11339#ifdef F_LOCK
11340 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11341#endif
11342#ifdef F_TLOCK
11343 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11344#endif
11345#ifdef F_ULOCK
11346 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11347#endif
11348#ifdef F_TEST
11349 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11350#endif
11351
11352 /* constants for futimens */
11353#ifdef UTIME_NOW
11354 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11355#endif
11356#ifdef UTIME_OMIT
11357 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11358#endif
11359
Guido van Rossum246bc171999-02-01 23:54:31 +000011360#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011361#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011362 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11363 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11364 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11365 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11366 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11367 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11368 if (ins(d, "P_PM", (long)P_PM)) return -1;
11369 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11370 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11371 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11372 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11373 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11374 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11375 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11376 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11377 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11378 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11379 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11380 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11381 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011382#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011383 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11384 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11385 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11386 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11387 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011388#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011389#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011390
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011391#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011392 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011393 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11394 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11395#ifdef SCHED_SPORADIC
11396 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11397#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011398#ifdef SCHED_BATCH
11399 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11400#endif
11401#ifdef SCHED_IDLE
11402 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11403#endif
11404#ifdef SCHED_RESET_ON_FORK
11405 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11406#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011407#ifdef SCHED_SYS
11408 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11409#endif
11410#ifdef SCHED_IA
11411 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11412#endif
11413#ifdef SCHED_FSS
11414 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11415#endif
11416#ifdef SCHED_FX
11417 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11418#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011419#endif
11420
Benjamin Peterson9428d532011-09-14 11:45:52 -040011421#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011422 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11423 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11424 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11425#endif
11426
Victor Stinner8b905bd2011-10-25 13:34:04 +020011427#ifdef RTLD_LAZY
11428 if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1;
11429#endif
11430#ifdef RTLD_NOW
11431 if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1;
11432#endif
11433#ifdef RTLD_GLOBAL
11434 if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1;
11435#endif
11436#ifdef RTLD_LOCAL
11437 if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1;
11438#endif
11439#ifdef RTLD_NODELETE
11440 if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1;
11441#endif
11442#ifdef RTLD_NOLOAD
11443 if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1;
11444#endif
11445#ifdef RTLD_DEEPBIND
11446 if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1;
11447#endif
11448
Guido van Rossumd48f2521997-12-05 22:19:34 +000011449#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011450 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011451#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011452 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011453}
11454
11455
Tim Peters5aa91602002-01-30 05:46:57 +000011456#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011457#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011458#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011459
11460#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011461#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011462#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011463
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011464#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011465#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011466#define MODNAME "posix"
11467#endif
11468
Martin v. Löwis1a214512008-06-11 05:26:20 +000011469static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011470 PyModuleDef_HEAD_INIT,
11471 MODNAME,
11472 posix__doc__,
11473 -1,
11474 posix_methods,
11475 NULL,
11476 NULL,
11477 NULL,
11478 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011479};
11480
11481
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011482PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011483INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011484{
Victor Stinner8c62be82010-05-06 00:08:46 +000011485 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011486
Brian Curtin52173d42010-12-02 18:29:18 +000011487#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011488 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011489#endif
11490
Victor Stinner8c62be82010-05-06 00:08:46 +000011491 m = PyModule_Create(&posixmodule);
11492 if (m == NULL)
11493 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011494
Victor Stinner8c62be82010-05-06 00:08:46 +000011495 /* Initialize environ dictionary */
11496 v = convertenviron();
11497 Py_XINCREF(v);
11498 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11499 return NULL;
11500 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011501
Victor Stinner8c62be82010-05-06 00:08:46 +000011502 if (all_ins(m))
11503 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011504
Victor Stinner8c62be82010-05-06 00:08:46 +000011505 if (setup_confname_tables(m))
11506 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011507
Victor Stinner8c62be82010-05-06 00:08:46 +000011508 Py_INCREF(PyExc_OSError);
11509 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011510
Benjamin Peterson2740af82011-08-02 17:41:34 -050011511#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011512 if (PyType_Ready(&cpu_set_type) < 0)
11513 return NULL;
11514 Py_INCREF(&cpu_set_type);
11515 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011516#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011517
Guido van Rossumb3d39562000-01-31 18:41:26 +000011518#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011519 if (posix_putenv_garbage == NULL)
11520 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011521#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011522
Victor Stinner8c62be82010-05-06 00:08:46 +000011523 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011524#if defined(HAVE_WAITID) && !defined(__APPLE__)
11525 waitid_result_desc.name = MODNAME ".waitid_result";
11526 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11527#endif
11528
Victor Stinner8c62be82010-05-06 00:08:46 +000011529 stat_result_desc.name = MODNAME ".stat_result";
11530 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11531 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11532 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11533 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11534 structseq_new = StatResultType.tp_new;
11535 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011536
Victor Stinner8c62be82010-05-06 00:08:46 +000011537 statvfs_result_desc.name = MODNAME ".statvfs_result";
11538 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011539#ifdef NEED_TICKS_PER_SECOND
11540# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011541 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011542# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011543 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011544# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011545 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011546# endif
11547#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011548
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011549#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011550 sched_param_desc.name = MODNAME ".sched_param";
11551 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11552 SchedParamType.tp_new = sched_param_new;
11553#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011554 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011555#if defined(HAVE_WAITID) && !defined(__APPLE__)
11556 Py_INCREF((PyObject*) &WaitidResultType);
11557 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11558#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011559 Py_INCREF((PyObject*) &StatResultType);
11560 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11561 Py_INCREF((PyObject*) &StatVFSResultType);
11562 PyModule_AddObject(m, "statvfs_result",
11563 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011564
11565#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011566 Py_INCREF(&SchedParamType);
11567 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011568#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011569 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011570
11571#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011572 /*
11573 * Step 2 of weak-linking support on Mac OS X.
11574 *
11575 * The code below removes functions that are not available on the
11576 * currently active platform.
11577 *
11578 * This block allow one to use a python binary that was build on
11579 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11580 * OSX 10.4.
11581 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011582#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011583 if (fstatvfs == NULL) {
11584 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11585 return NULL;
11586 }
11587 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011588#endif /* HAVE_FSTATVFS */
11589
11590#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011591 if (statvfs == NULL) {
11592 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11593 return NULL;
11594 }
11595 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011596#endif /* HAVE_STATVFS */
11597
11598# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011599 if (lchown == NULL) {
11600 if (PyObject_DelAttrString(m, "lchown") == -1) {
11601 return NULL;
11602 }
11603 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011604#endif /* HAVE_LCHOWN */
11605
11606
11607#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011608 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011609
Guido van Rossumb6775db1994-08-01 11:34:53 +000011610}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011611
11612#ifdef __cplusplus
11613}
11614#endif