blob: 90194159df5bc61091efed97cbabdbcda0ebe851 [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 Peterson799bd802011-08-31 22:15:17 -0400110#ifdef HAVE_ATTR_XATTR_H
111#include <attr/xattr.h>
112#endif
113
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000114#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
115#ifdef HAVE_SYS_SOCKET_H
116#include <sys/socket.h>
117#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#endif
119
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000121/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000122#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000123#include <process.h>
124#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_GETCWD 1
127#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000129#if defined(__OS2__)
130#define HAVE_EXECV 1
131#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000132#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#include <process.h>
134#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#define HAVE_EXECV 1
137#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000138#define HAVE_OPENDIR 1
139#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000140#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_WAIT 1
142#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000143#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000144#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000145#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000146#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000147#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000148#define HAVE_EXECV 1
149#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000150#define HAVE_SYSTEM 1
151#define HAVE_CWAIT 1
152#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000153#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000154#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000155#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
156/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000157#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000158/* Unix functions that the configure script doesn't check for */
159#define HAVE_EXECV 1
160#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000161#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000162#define HAVE_FORK1 1
163#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#define HAVE_GETCWD 1
165#define HAVE_GETEGID 1
166#define HAVE_GETEUID 1
167#define HAVE_GETGID 1
168#define HAVE_GETPPID 1
169#define HAVE_GETUID 1
170#define HAVE_KILL 1
171#define HAVE_OPENDIR 1
172#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000173#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000175#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000176#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177#endif /* _MSC_VER */
178#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000179#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000181
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000183
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000184#if defined(__sgi)&&_COMPILER_VERSION>=700
185/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
186 (default) */
187extern char *ctermid_r(char *);
188#endif
189
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000190#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000192extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000193#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000194#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000196#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000198#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000199#endif
200#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000201extern int chdir(char *);
202extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000203#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int chdir(const char *);
205extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000206#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000207#ifdef __BORLANDC__
208extern int chmod(const char *, int);
209#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000211#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000212/*#ifdef HAVE_FCHMOD
213extern int fchmod(int, mode_t);
214#endif*/
215/*#ifdef HAVE_LCHMOD
216extern int lchmod(const char *, mode_t);
217#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int chown(const char *, uid_t, gid_t);
219extern char *getcwd(char *, int);
220extern char *strerror(int);
221extern int link(const char *, const char *);
222extern int rename(const char *, const char *);
223extern int stat(const char *, struct stat *);
224extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000226extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000227#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000229extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000230#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000232
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000233#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#ifdef HAVE_UTIME_H
236#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000239#ifdef HAVE_SYS_UTIME_H
240#include <sys/utime.h>
241#define HAVE_UTIME_H /* pretend we do for the rest of this file */
242#endif /* HAVE_SYS_UTIME_H */
243
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244#ifdef HAVE_SYS_TIMES_H
245#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000246#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
248#ifdef HAVE_SYS_PARAM_H
249#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000250#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
252#ifdef HAVE_SYS_UTSNAME_H
253#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000254#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000256#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000258#define NAMLEN(dirent) strlen((dirent)->d_name)
259#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000260#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000261#include <direct.h>
262#define NAMLEN(dirent) strlen((dirent)->d_name)
263#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000265#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000266#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000267#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000269#endif
270#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000272#endif
273#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#endif
276#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281#endif
282#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284#endif
285#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000288#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000289#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000290#endif
291#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000292#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000293#endif
294#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000295#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000296#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000297#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000298#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000300#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000301#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000302#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
303#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000304static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000305#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000306#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307
Guido van Rossumd48f2521997-12-05 22:19:34 +0000308#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000309#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000310#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313#if defined(PATH_MAX) && PATH_MAX > 1024
314#define MAXPATHLEN PATH_MAX
315#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000318#endif /* MAXPATHLEN */
319
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000320#ifdef UNION_WAIT
321/* Emulate some macros on systems that have a union instead of macros */
322
323#ifndef WIFEXITED
324#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
325#endif
326
327#ifndef WEXITSTATUS
328#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
329#endif
330
331#ifndef WTERMSIG
332#define WTERMSIG(u_wait) ((u_wait).w_termsig)
333#endif
334
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335#define WAIT_TYPE union wait
336#define WAIT_STATUS_INT(s) (s.w_status)
337
338#else /* !UNION_WAIT */
339#define WAIT_TYPE int
340#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000341#endif /* UNION_WAIT */
342
Greg Wardb48bc172000-03-01 21:51:56 +0000343/* Don't use the "_r" form if we don't need it (also, won't have a
344 prototype for it, at least on Solaris -- maybe others as well?). */
345#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
346#define USE_CTERMID_R
347#endif
348
Fred Drake699f3522000-06-29 21:12:41 +0000349/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000350#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000351#undef FSTAT
352#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000353#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000354# define STAT win32_stat
355# define FSTAT win32_fstat
356# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000357#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000358# define STAT stat
359# define FSTAT fstat
360# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000361#endif
362
Tim Peters11b23062003-04-23 02:39:17 +0000363#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000364#include <sys/mkdev.h>
365#else
366#if defined(MAJOR_IN_SYSMACROS)
367#include <sys/sysmacros.h>
368#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000369#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
370#include <sys/mkdev.h>
371#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000372#endif
Fred Drake699f3522000-06-29 21:12:41 +0000373
Georg Brandla391b112011-02-25 15:23:18 +0000374static int
375_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000376{
377#if !defined(HAVE_LARGEFILE_SUPPORT)
378 *((off_t*)addr) = PyLong_AsLong(arg);
379#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000380 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000381#endif
382 if (PyErr_Occurred())
383 return 0;
384 return 1;
385}
386
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000387#if defined _MSC_VER && _MSC_VER >= 1400
388/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
389 * valid and throw an assertion if it isn't.
390 * Normally, an invalid fd is likely to be a C program error and therefore
391 * an assertion can be useful, but it does contradict the POSIX standard
392 * which for write(2) states:
393 * "Otherwise, -1 shall be returned and errno set to indicate the error."
394 * "[EBADF] The fildes argument is not a valid file descriptor open for
395 * writing."
396 * Furthermore, python allows the user to enter any old integer
397 * as a fd and should merely raise a python exception on error.
398 * The Microsoft CRT doesn't provide an official way to check for the
399 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000400 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000401 * internal structures involved.
402 * The structures below must be updated for each version of visual studio
403 * according to the file internal.h in the CRT source, until MS comes
404 * up with a less hacky way to do this.
405 * (all of this is to avoid globally modifying the CRT behaviour using
406 * _set_invalid_parameter_handler() and _CrtSetReportMode())
407 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000408/* The actual size of the structure is determined at runtime.
409 * Only the first items must be present.
410 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000411typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000412 intptr_t osfhnd;
413 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000414} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000415
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000416extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000417#define IOINFO_L2E 5
418#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
419#define IOINFO_ARRAYS 64
420#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
421#define FOPEN 0x01
422#define _NO_CONSOLE_FILENO (intptr_t)-2
423
424/* This function emulates what the windows CRT does to validate file handles */
425int
426_PyVerify_fd(int fd)
427{
Victor Stinner8c62be82010-05-06 00:08:46 +0000428 const int i1 = fd >> IOINFO_L2E;
429 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000430
Antoine Pitrou22e41552010-08-15 18:07:50 +0000431 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000432
Victor Stinner8c62be82010-05-06 00:08:46 +0000433 /* Determine the actual size of the ioinfo structure,
434 * as used by the CRT loaded in memory
435 */
436 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
437 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
438 }
439 if (sizeof_ioinfo == 0) {
440 /* This should not happen... */
441 goto fail;
442 }
443
444 /* See that it isn't a special CLEAR fileno */
445 if (fd != _NO_CONSOLE_FILENO) {
446 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
447 * we check pointer validity and other info
448 */
449 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
450 /* finally, check that the file is open */
451 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
452 if (info->osfile & FOPEN) {
453 return 1;
454 }
455 }
456 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000457 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000458 errno = EBADF;
459 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000460}
461
462/* the special case of checking dup2. The target fd must be in a sensible range */
463static int
464_PyVerify_fd_dup2(int fd1, int fd2)
465{
Victor Stinner8c62be82010-05-06 00:08:46 +0000466 if (!_PyVerify_fd(fd1))
467 return 0;
468 if (fd2 == _NO_CONSOLE_FILENO)
469 return 0;
470 if ((unsigned)fd2 < _NHANDLE_)
471 return 1;
472 else
473 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000474}
475#else
476/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
477#define _PyVerify_fd_dup2(A, B) (1)
478#endif
479
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000480#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000481/* The following structure was copied from
482 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
483 include doesn't seem to be present in the Windows SDK (at least as included
484 with Visual Studio Express). */
485typedef struct _REPARSE_DATA_BUFFER {
486 ULONG ReparseTag;
487 USHORT ReparseDataLength;
488 USHORT Reserved;
489 union {
490 struct {
491 USHORT SubstituteNameOffset;
492 USHORT SubstituteNameLength;
493 USHORT PrintNameOffset;
494 USHORT PrintNameLength;
495 ULONG Flags;
496 WCHAR PathBuffer[1];
497 } SymbolicLinkReparseBuffer;
498
499 struct {
500 USHORT SubstituteNameOffset;
501 USHORT SubstituteNameLength;
502 USHORT PrintNameOffset;
503 USHORT PrintNameLength;
504 WCHAR PathBuffer[1];
505 } MountPointReparseBuffer;
506
507 struct {
508 UCHAR DataBuffer[1];
509 } GenericReparseBuffer;
510 };
511} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
512
513#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
514 GenericReparseBuffer)
515#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
516
517static int
Brian Curtind25aef52011-06-13 15:16:04 -0500518win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000519{
520 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
521 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
522 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000523
524 if (0 == DeviceIoControl(
525 reparse_point_handle,
526 FSCTL_GET_REPARSE_POINT,
527 NULL, 0, /* in buffer */
528 target_buffer, sizeof(target_buffer),
529 &n_bytes_returned,
530 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500531 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000532
533 if (reparse_tag)
534 *reparse_tag = rdb->ReparseTag;
535
Brian Curtind25aef52011-06-13 15:16:04 -0500536 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000537}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000538#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000539
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000541#ifdef WITH_NEXT_FRAMEWORK
542/* On Darwin/MacOSX a shared library or framework has no access to
543** environ directly, we must obtain it with _NSGetEnviron().
544*/
545#include <crt_externs.h>
546static char **environ;
547#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000549#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550
Barry Warsaw53699e91996-12-10 23:23:01 +0000551static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000552convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553{
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000555#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000557#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000558 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000559#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000560#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000561 APIRET rc;
562 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
563#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000564
Victor Stinner8c62be82010-05-06 00:08:46 +0000565 d = PyDict_New();
566 if (d == NULL)
567 return NULL;
568#ifdef WITH_NEXT_FRAMEWORK
569 if (environ == NULL)
570 environ = *_NSGetEnviron();
571#endif
572#ifdef MS_WINDOWS
573 /* _wenviron must be initialized in this way if the program is started
574 through main() instead of wmain(). */
575 _wgetenv(L"");
576 if (_wenviron == NULL)
577 return d;
578 /* This part ignores errors */
579 for (e = _wenviron; *e != NULL; e++) {
580 PyObject *k;
581 PyObject *v;
582 wchar_t *p = wcschr(*e, L'=');
583 if (p == NULL)
584 continue;
585 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
586 if (k == NULL) {
587 PyErr_Clear();
588 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000589 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000590 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
591 if (v == NULL) {
592 PyErr_Clear();
593 Py_DECREF(k);
594 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000595 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000596 if (PyDict_GetItem(d, k) == NULL) {
597 if (PyDict_SetItem(d, k, v) != 0)
598 PyErr_Clear();
599 }
600 Py_DECREF(k);
601 Py_DECREF(v);
602 }
603#else
604 if (environ == NULL)
605 return d;
606 /* This part ignores errors */
607 for (e = environ; *e != NULL; e++) {
608 PyObject *k;
609 PyObject *v;
610 char *p = strchr(*e, '=');
611 if (p == NULL)
612 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000613 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000614 if (k == NULL) {
615 PyErr_Clear();
616 continue;
617 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000618 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000619 if (v == NULL) {
620 PyErr_Clear();
621 Py_DECREF(k);
622 continue;
623 }
624 if (PyDict_GetItem(d, k) == NULL) {
625 if (PyDict_SetItem(d, k, v) != 0)
626 PyErr_Clear();
627 }
628 Py_DECREF(k);
629 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000630 }
631#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000632#if defined(PYOS_OS2)
633 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
634 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
635 PyObject *v = PyBytes_FromString(buffer);
636 PyDict_SetItemString(d, "BEGINLIBPATH", v);
637 Py_DECREF(v);
638 }
639 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
640 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
641 PyObject *v = PyBytes_FromString(buffer);
642 PyDict_SetItemString(d, "ENDLIBPATH", v);
643 Py_DECREF(v);
644 }
645#endif
646 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647}
648
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649/* Set a POSIX-specific error from errno, and return NULL */
650
Barry Warsawd58d7641998-07-23 16:14:40 +0000651static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000652posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000653{
Victor Stinner8c62be82010-05-06 00:08:46 +0000654 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000655}
Barry Warsawd58d7641998-07-23 16:14:40 +0000656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000657posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000658{
Victor Stinner8c62be82010-05-06 00:08:46 +0000659 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000660}
661
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000662
Mark Hammondef8b6542001-05-13 08:04:26 +0000663static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000664posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000665{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000666 PyObject *name_str, *rc;
667 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
668 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000669 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000670 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
671 name_str);
672 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000673 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000674}
675
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000676#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000677static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000678win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000679{
Victor Stinner8c62be82010-05-06 00:08:46 +0000680 /* XXX We should pass the function name along in the future.
681 (winreg.c also wants to pass the function name.)
682 This would however require an additional param to the
683 Windows error object, which is non-trivial.
684 */
685 errno = GetLastError();
686 if (filename)
687 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
688 else
689 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000690}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000691
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000692static PyObject *
693win32_error_unicode(char* function, Py_UNICODE* filename)
694{
Victor Stinner8c62be82010-05-06 00:08:46 +0000695 /* XXX - see win32_error for comments on 'function' */
696 errno = GetLastError();
697 if (filename)
698 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
699 else
700 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000701}
702
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000704convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705{
Victor Stinner8c62be82010-05-06 00:08:46 +0000706 if (PyUnicode_CheckExact(*param))
707 Py_INCREF(*param);
708 else if (PyUnicode_Check(*param))
709 /* For a Unicode subtype that's not a Unicode object,
710 return a true Unicode object with the same data. */
711 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
712 PyUnicode_GET_SIZE(*param));
713 else
714 *param = PyUnicode_FromEncodedObject(*param,
715 Py_FileSystemDefaultEncoding,
716 "strict");
717 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000718}
719
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000720#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721
Guido van Rossumd48f2521997-12-05 22:19:34 +0000722#if defined(PYOS_OS2)
723/**********************************************************************
724 * Helper Function to Trim and Format OS/2 Messages
725 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000726static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000727os2_formatmsg(char *msgbuf, int msglen, char *reason)
728{
729 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
730
731 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
732 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
733
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000734 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000735 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
736 }
737
738 /* Add Optional Reason Text */
739 if (reason) {
740 strcat(msgbuf, " : ");
741 strcat(msgbuf, reason);
742 }
743}
744
745/**********************************************************************
746 * Decode an OS/2 Operating System Error Code
747 *
748 * A convenience function to lookup an OS/2 error code and return a
749 * text message we can use to raise a Python exception.
750 *
751 * Notes:
752 * The messages for errors returned from the OS/2 kernel reside in
753 * the file OSO001.MSG in the \OS2 directory hierarchy.
754 *
755 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000756static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000757os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
758{
759 APIRET rc;
760 ULONG msglen;
761
762 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
763 Py_BEGIN_ALLOW_THREADS
764 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
765 errorcode, "oso001.msg", &msglen);
766 Py_END_ALLOW_THREADS
767
768 if (rc == NO_ERROR)
769 os2_formatmsg(msgbuf, msglen, reason);
770 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000771 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000772 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000773
774 return msgbuf;
775}
776
777/* Set an OS/2-specific error and return NULL. OS/2 kernel
778 errors are not in a global variable e.g. 'errno' nor are
779 they congruent with posix error numbers. */
780
Victor Stinner8c62be82010-05-06 00:08:46 +0000781static PyObject *
782os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000783{
784 char text[1024];
785 PyObject *v;
786
787 os2_strerror(text, sizeof(text), code, "");
788
789 v = Py_BuildValue("(is)", code, text);
790 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000791 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000792 Py_DECREF(v);
793 }
794 return NULL; /* Signal to Python that an Exception is Pending */
795}
796
797#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798
799/* POSIX generic methods */
800
Barry Warsaw53699e91996-12-10 23:23:01 +0000801static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000802posix_fildes(PyObject *fdobj, int (*func)(int))
803{
Victor Stinner8c62be82010-05-06 00:08:46 +0000804 int fd;
805 int res;
806 fd = PyObject_AsFileDescriptor(fdobj);
807 if (fd < 0)
808 return NULL;
809 if (!_PyVerify_fd(fd))
810 return posix_error();
811 Py_BEGIN_ALLOW_THREADS
812 res = (*func)(fd);
813 Py_END_ALLOW_THREADS
814 if (res < 0)
815 return posix_error();
816 Py_INCREF(Py_None);
817 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000818}
Guido van Rossum21142a01999-01-08 21:05:37 +0000819
820static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000821posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822{
Victor Stinner8c62be82010-05-06 00:08:46 +0000823 PyObject *opath1 = NULL;
824 char *path1;
825 int res;
826 if (!PyArg_ParseTuple(args, format,
827 PyUnicode_FSConverter, &opath1))
828 return NULL;
829 path1 = PyBytes_AsString(opath1);
830 Py_BEGIN_ALLOW_THREADS
831 res = (*func)(path1);
832 Py_END_ALLOW_THREADS
833 if (res < 0)
834 return posix_error_with_allocated_filename(opath1);
835 Py_DECREF(opath1);
836 Py_INCREF(Py_None);
837 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838}
839
Barry Warsaw53699e91996-12-10 23:23:01 +0000840static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000841posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000842 char *format,
843 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844{
Victor Stinner8c62be82010-05-06 00:08:46 +0000845 PyObject *opath1 = NULL, *opath2 = NULL;
846 char *path1, *path2;
847 int res;
848 if (!PyArg_ParseTuple(args, format,
849 PyUnicode_FSConverter, &opath1,
850 PyUnicode_FSConverter, &opath2)) {
851 return NULL;
852 }
853 path1 = PyBytes_AsString(opath1);
854 path2 = PyBytes_AsString(opath2);
855 Py_BEGIN_ALLOW_THREADS
856 res = (*func)(path1, path2);
857 Py_END_ALLOW_THREADS
858 Py_DECREF(opath1);
859 Py_DECREF(opath2);
860 if (res != 0)
861 /* XXX how to report both path1 and path2??? */
862 return posix_error();
863 Py_INCREF(Py_None);
864 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000867#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000869win32_1str(PyObject* args, char* func,
870 char* format, BOOL (__stdcall *funcA)(LPCSTR),
871 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872{
Victor Stinner8c62be82010-05-06 00:08:46 +0000873 PyObject *uni;
874 char *ansi;
875 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000876
Victor Stinner8c62be82010-05-06 00:08:46 +0000877 if (!PyArg_ParseTuple(args, wformat, &uni))
878 PyErr_Clear();
879 else {
880 Py_BEGIN_ALLOW_THREADS
881 result = funcW(PyUnicode_AsUnicode(uni));
882 Py_END_ALLOW_THREADS
883 if (!result)
884 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
885 Py_INCREF(Py_None);
886 return Py_None;
887 }
888 if (!PyArg_ParseTuple(args, format, &ansi))
889 return NULL;
890 Py_BEGIN_ALLOW_THREADS
891 result = funcA(ansi);
892 Py_END_ALLOW_THREADS
893 if (!result)
894 return win32_error(func, ansi);
895 Py_INCREF(Py_None);
896 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000897
898}
899
900/* This is a reimplementation of the C library's chdir function,
901 but one that produces Win32 errors instead of DOS error codes.
902 chdir is essentially a wrapper around SetCurrentDirectory; however,
903 it also needs to set "magic" environment variables indicating
904 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000905static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906win32_chdir(LPCSTR path)
907{
Victor Stinner8c62be82010-05-06 00:08:46 +0000908 char new_path[MAX_PATH+1];
909 int result;
910 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911
Victor Stinner8c62be82010-05-06 00:08:46 +0000912 if(!SetCurrentDirectoryA(path))
913 return FALSE;
914 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
915 if (!result)
916 return FALSE;
917 /* In the ANSI API, there should not be any paths longer
918 than MAX_PATH. */
919 assert(result <= MAX_PATH+1);
920 if (strncmp(new_path, "\\\\", 2) == 0 ||
921 strncmp(new_path, "//", 2) == 0)
922 /* UNC path, nothing to do. */
923 return TRUE;
924 env[1] = new_path[0];
925 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926}
927
928/* The Unicode version differs from the ANSI version
929 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000930static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931win32_wchdir(LPCWSTR path)
932{
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
934 int result;
935 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936
Victor Stinner8c62be82010-05-06 00:08:46 +0000937 if(!SetCurrentDirectoryW(path))
938 return FALSE;
939 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
940 if (!result)
941 return FALSE;
942 if (result > MAX_PATH+1) {
943 new_path = malloc(result * sizeof(wchar_t));
944 if (!new_path) {
945 SetLastError(ERROR_OUTOFMEMORY);
946 return FALSE;
947 }
948 result = GetCurrentDirectoryW(result, new_path);
949 if (!result) {
950 free(new_path);
951 return FALSE;
952 }
953 }
954 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
955 wcsncmp(new_path, L"//", 2) == 0)
956 /* UNC path, nothing to do. */
957 return TRUE;
958 env[1] = new_path[0];
959 result = SetEnvironmentVariableW(env, new_path);
960 if (new_path != _new_path)
961 free(new_path);
962 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963}
964#endif
965
Martin v. Löwis14694662006-02-03 12:54:16 +0000966#ifdef MS_WINDOWS
967/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
968 - time stamps are restricted to second resolution
969 - file modification times suffer from forth-and-back conversions between
970 UTC and local time
971 Therefore, we implement our own stat, based on the Win32 API directly.
972*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000973#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000974
975struct win32_stat{
976 int st_dev;
977 __int64 st_ino;
978 unsigned short st_mode;
979 int st_nlink;
980 int st_uid;
981 int st_gid;
982 int st_rdev;
983 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000984 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000985 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000986 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000987 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000988 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000989 int st_ctime_nsec;
990};
991
992static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
993
994static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000995FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000996{
Victor Stinner8c62be82010-05-06 00:08:46 +0000997 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
998 /* Cannot simply cast and dereference in_ptr,
999 since it might not be aligned properly */
1000 __int64 in;
1001 memcpy(&in, in_ptr, sizeof(in));
1002 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001003 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001004}
1005
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001007time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008{
Victor Stinner8c62be82010-05-06 00:08:46 +00001009 /* XXX endianness */
1010 __int64 out;
1011 out = time_in + secs_between_epochs;
1012 out = out * 10000000 + nsec_in / 100;
1013 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001014}
1015
Martin v. Löwis14694662006-02-03 12:54:16 +00001016/* Below, we *know* that ugo+r is 0444 */
1017#if _S_IREAD != 0400
1018#error Unsupported C library
1019#endif
1020static int
1021attributes_to_mode(DWORD attr)
1022{
Victor Stinner8c62be82010-05-06 00:08:46 +00001023 int m = 0;
1024 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1025 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1026 else
1027 m |= _S_IFREG;
1028 if (attr & FILE_ATTRIBUTE_READONLY)
1029 m |= 0444;
1030 else
1031 m |= 0666;
1032 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001033}
1034
1035static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001036attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001037{
Victor Stinner8c62be82010-05-06 00:08:46 +00001038 memset(result, 0, sizeof(*result));
1039 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1040 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1041 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1042 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1043 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001044 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001045 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001046 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1047 /* first clear the S_IFMT bits */
1048 result->st_mode ^= (result->st_mode & 0170000);
1049 /* now set the bits that make this a symlink */
1050 result->st_mode |= 0120000;
1051 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001052
Victor Stinner8c62be82010-05-06 00:08:46 +00001053 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001054}
1055
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001057attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001058{
Victor Stinner8c62be82010-05-06 00:08:46 +00001059 HANDLE hFindFile;
1060 WIN32_FIND_DATAA FileData;
1061 hFindFile = FindFirstFileA(pszFile, &FileData);
1062 if (hFindFile == INVALID_HANDLE_VALUE)
1063 return FALSE;
1064 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001065 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001066 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001067 info->dwFileAttributes = FileData.dwFileAttributes;
1068 info->ftCreationTime = FileData.ftCreationTime;
1069 info->ftLastAccessTime = FileData.ftLastAccessTime;
1070 info->ftLastWriteTime = FileData.ftLastWriteTime;
1071 info->nFileSizeHigh = FileData.nFileSizeHigh;
1072 info->nFileSizeLow = FileData.nFileSizeLow;
1073/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001074 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1075 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001076 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001077}
1078
1079static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001080attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001081{
Victor Stinner8c62be82010-05-06 00:08:46 +00001082 HANDLE hFindFile;
1083 WIN32_FIND_DATAW FileData;
1084 hFindFile = FindFirstFileW(pszFile, &FileData);
1085 if (hFindFile == INVALID_HANDLE_VALUE)
1086 return FALSE;
1087 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001088 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001089 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001090 info->dwFileAttributes = FileData.dwFileAttributes;
1091 info->ftCreationTime = FileData.ftCreationTime;
1092 info->ftLastAccessTime = FileData.ftLastAccessTime;
1093 info->ftLastWriteTime = FileData.ftLastWriteTime;
1094 info->nFileSizeHigh = FileData.nFileSizeHigh;
1095 info->nFileSizeLow = FileData.nFileSizeLow;
1096/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001097 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1098 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001099 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001100}
1101
Brian Curtind25aef52011-06-13 15:16:04 -05001102/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1103static int has_GetFinalPathNameByHandle = 0;
1104static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1105 DWORD);
1106static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1107 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001108static int
Brian Curtind25aef52011-06-13 15:16:04 -05001109check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001110{
Brian Curtind25aef52011-06-13 15:16:04 -05001111 HINSTANCE hKernel32;
1112 /* only recheck */
1113 if (!has_GetFinalPathNameByHandle)
1114 {
1115 hKernel32 = GetModuleHandle("KERNEL32");
1116 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1117 "GetFinalPathNameByHandleA");
1118 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1119 "GetFinalPathNameByHandleW");
1120 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1121 Py_GetFinalPathNameByHandleW;
1122 }
1123 return has_GetFinalPathNameByHandle;
1124}
1125
1126static BOOL
1127get_target_path(HANDLE hdl, wchar_t **target_path)
1128{
1129 int buf_size, result_length;
1130 wchar_t *buf;
1131
1132 /* We have a good handle to the target, use it to determine
1133 the target path name (then we'll call lstat on it). */
1134 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1135 VOLUME_NAME_DOS);
1136 if(!buf_size)
1137 return FALSE;
1138
1139 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001140 if (!buf) {
1141 SetLastError(ERROR_OUTOFMEMORY);
1142 return FALSE;
1143 }
1144
Brian Curtind25aef52011-06-13 15:16:04 -05001145 result_length = Py_GetFinalPathNameByHandleW(hdl,
1146 buf, buf_size, VOLUME_NAME_DOS);
1147
1148 if(!result_length) {
1149 free(buf);
1150 return FALSE;
1151 }
1152
1153 if(!CloseHandle(hdl)) {
1154 free(buf);
1155 return FALSE;
1156 }
1157
1158 buf[result_length] = 0;
1159
1160 *target_path = buf;
1161 return TRUE;
1162}
1163
1164static int
1165win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1166 BOOL traverse);
1167static int
1168win32_xstat_impl(const char *path, struct win32_stat *result,
1169 BOOL traverse)
1170{
Victor Stinner26de69d2011-06-17 15:15:38 +02001171 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001172 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001173 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001174 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001175 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001176 const char *dot;
1177
Brian Curtind25aef52011-06-13 15:16:04 -05001178 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001179 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1180 traverse reparse point. */
1181 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001182 }
1183
Brian Curtinf5e76d02010-11-24 13:14:05 +00001184 hFile = CreateFileA(
1185 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001186 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001187 0, /* share mode */
1188 NULL, /* security attributes */
1189 OPEN_EXISTING,
1190 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001191 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1192 Because of this, calls like GetFinalPathNameByHandle will return
1193 the symlink path agin and not the actual final path. */
1194 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1195 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001196 NULL);
1197
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001198 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001199 /* Either the target doesn't exist, or we don't have access to
1200 get a handle to it. If the former, we need to return an error.
1201 If the latter, we can use attributes_from_dir. */
1202 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001203 return -1;
1204 /* Could not get attributes on open file. Fall back to
1205 reading the directory. */
1206 if (!attributes_from_dir(path, &info, &reparse_tag))
1207 /* Very strange. This should not fail now */
1208 return -1;
1209 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1210 if (traverse) {
1211 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001212 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001213 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001214 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001215 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001216 } else {
1217 if (!GetFileInformationByHandle(hFile, &info)) {
1218 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001219 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001220 }
1221 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001222 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1223 return -1;
1224
1225 /* Close the outer open file handle now that we're about to
1226 reopen it with different flags. */
1227 if (!CloseHandle(hFile))
1228 return -1;
1229
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001230 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001231 /* In order to call GetFinalPathNameByHandle we need to open
1232 the file without the reparse handling flag set. */
1233 hFile2 = CreateFileA(
1234 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1235 NULL, OPEN_EXISTING,
1236 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1237 NULL);
1238 if (hFile2 == INVALID_HANDLE_VALUE)
1239 return -1;
1240
1241 if (!get_target_path(hFile2, &target_path))
1242 return -1;
1243
1244 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001245 free(target_path);
1246 return code;
1247 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001248 } else
1249 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001250 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001251 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001252
1253 /* Set S_IEXEC if it is an .exe, .bat, ... */
1254 dot = strrchr(path, '.');
1255 if (dot) {
1256 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1257 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1258 result->st_mode |= 0111;
1259 }
1260 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001261}
1262
1263static int
Brian Curtind25aef52011-06-13 15:16:04 -05001264win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1265 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001266{
1267 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001268 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001269 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001270 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001271 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001272 const wchar_t *dot;
1273
Brian Curtind25aef52011-06-13 15:16:04 -05001274 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001275 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1276 traverse reparse point. */
1277 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001278 }
1279
Brian Curtinf5e76d02010-11-24 13:14:05 +00001280 hFile = CreateFileW(
1281 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001282 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001283 0, /* share mode */
1284 NULL, /* security attributes */
1285 OPEN_EXISTING,
1286 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001287 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1288 Because of this, calls like GetFinalPathNameByHandle will return
1289 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001290 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001291 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001292 NULL);
1293
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001294 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001295 /* Either the target doesn't exist, or we don't have access to
1296 get a handle to it. If the former, we need to return an error.
1297 If the latter, we can use attributes_from_dir. */
1298 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001299 return -1;
1300 /* Could not get attributes on open file. Fall back to
1301 reading the directory. */
1302 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1303 /* Very strange. This should not fail now */
1304 return -1;
1305 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1306 if (traverse) {
1307 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001309 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001310 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001311 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001312 } else {
1313 if (!GetFileInformationByHandle(hFile, &info)) {
1314 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001315 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001316 }
1317 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001318 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1319 return -1;
1320
1321 /* Close the outer open file handle now that we're about to
1322 reopen it with different flags. */
1323 if (!CloseHandle(hFile))
1324 return -1;
1325
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001326 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001327 /* In order to call GetFinalPathNameByHandle we need to open
1328 the file without the reparse handling flag set. */
1329 hFile2 = CreateFileW(
1330 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1331 NULL, OPEN_EXISTING,
1332 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1333 NULL);
1334 if (hFile2 == INVALID_HANDLE_VALUE)
1335 return -1;
1336
1337 if (!get_target_path(hFile2, &target_path))
1338 return -1;
1339
1340 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001341 free(target_path);
1342 return code;
1343 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001344 } else
1345 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001346 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001347 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001348
1349 /* Set S_IEXEC if it is an .exe, .bat, ... */
1350 dot = wcsrchr(path, '.');
1351 if (dot) {
1352 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1353 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1354 result->st_mode |= 0111;
1355 }
1356 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001357}
1358
1359static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001360win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001361{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001362 /* Protocol violation: we explicitly clear errno, instead of
1363 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001364 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001365 errno = 0;
1366 return code;
1367}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001368
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001369static int
1370win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1371{
1372 /* Protocol violation: we explicitly clear errno, instead of
1373 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001374 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001375 errno = 0;
1376 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001377}
Brian Curtind25aef52011-06-13 15:16:04 -05001378/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001379
1380 In Posix, stat automatically traverses symlinks and returns the stat
1381 structure for the target. In Windows, the equivalent GetFileAttributes by
1382 default does not traverse symlinks and instead returns attributes for
1383 the symlink.
1384
1385 Therefore, win32_lstat will get the attributes traditionally, and
1386 win32_stat will first explicitly resolve the symlink target and then will
1387 call win32_lstat on that result.
1388
Ezio Melotti4969f702011-03-15 05:59:46 +02001389 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001390
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001391static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001392win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001393{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001395}
1396
Victor Stinner8c62be82010-05-06 00:08:46 +00001397static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001398win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001399{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001400 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001401}
1402
1403static int
1404win32_stat(const char* path, struct win32_stat *result)
1405{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001406 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001407}
1408
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001409static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001410win32_stat_w(const wchar_t* path, struct win32_stat *result)
1411{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001412 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001413}
1414
1415static int
1416win32_fstat(int file_number, struct win32_stat *result)
1417{
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 BY_HANDLE_FILE_INFORMATION info;
1419 HANDLE h;
1420 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001423
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 /* Protocol violation: we explicitly clear errno, instead of
1425 setting it to a POSIX error. Callers should use GetLastError. */
1426 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001427
Victor Stinner8c62be82010-05-06 00:08:46 +00001428 if (h == INVALID_HANDLE_VALUE) {
1429 /* This is really a C library error (invalid file handle).
1430 We set the Win32 error to the closes one matching. */
1431 SetLastError(ERROR_INVALID_HANDLE);
1432 return -1;
1433 }
1434 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001435
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 type = GetFileType(h);
1437 if (type == FILE_TYPE_UNKNOWN) {
1438 DWORD error = GetLastError();
1439 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001440 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001441 }
1442 /* else: valid but unknown file */
1443 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001444
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 if (type != FILE_TYPE_DISK) {
1446 if (type == FILE_TYPE_CHAR)
1447 result->st_mode = _S_IFCHR;
1448 else if (type == FILE_TYPE_PIPE)
1449 result->st_mode = _S_IFIFO;
1450 return 0;
1451 }
1452
1453 if (!GetFileInformationByHandle(h, &info)) {
1454 return -1;
1455 }
1456
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001457 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001458 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1460 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001461}
1462
1463#endif /* MS_WINDOWS */
1464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001465PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001466"stat_result: Result from stat or lstat.\n\n\
1467This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001468 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001469or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1470\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001471Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1472or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001473\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001474See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001475
1476static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001477 {"st_mode", "protection bits"},
1478 {"st_ino", "inode"},
1479 {"st_dev", "device"},
1480 {"st_nlink", "number of hard links"},
1481 {"st_uid", "user ID of owner"},
1482 {"st_gid", "group ID of owner"},
1483 {"st_size", "total size, in bytes"},
1484 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1485 {NULL, "integer time of last access"},
1486 {NULL, "integer time of last modification"},
1487 {NULL, "integer time of last change"},
1488 {"st_atime", "time of last access"},
1489 {"st_mtime", "time of last modification"},
1490 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001491#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001493#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001494#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001495 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001496#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001497#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001498 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001499#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001500#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001502#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001503#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001504 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001505#endif
1506#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001508#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001509 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001510};
1511
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001512#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001513#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001514#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001515#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001516#endif
1517
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001518#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001519#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1520#else
1521#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1522#endif
1523
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001524#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001525#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1526#else
1527#define ST_RDEV_IDX ST_BLOCKS_IDX
1528#endif
1529
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001530#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1531#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1532#else
1533#define ST_FLAGS_IDX ST_RDEV_IDX
1534#endif
1535
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001536#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001537#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001538#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001539#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001540#endif
1541
1542#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1543#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1544#else
1545#define ST_BIRTHTIME_IDX ST_GEN_IDX
1546#endif
1547
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001548static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001549 "stat_result", /* name */
1550 stat_result__doc__, /* doc */
1551 stat_result_fields,
1552 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001553};
1554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001555PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001556"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1557This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001558 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001559or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001560\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001562
1563static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001564 {"f_bsize", },
1565 {"f_frsize", },
1566 {"f_blocks", },
1567 {"f_bfree", },
1568 {"f_bavail", },
1569 {"f_files", },
1570 {"f_ffree", },
1571 {"f_favail", },
1572 {"f_flag", },
1573 {"f_namemax",},
1574 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001575};
1576
1577static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 "statvfs_result", /* name */
1579 statvfs_result__doc__, /* doc */
1580 statvfs_result_fields,
1581 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001582};
1583
Ross Lagerwall7807c352011-03-17 20:20:30 +02001584#if defined(HAVE_WAITID) && !defined(__APPLE__)
1585PyDoc_STRVAR(waitid_result__doc__,
1586"waitid_result: Result from waitid.\n\n\
1587This object may be accessed either as a tuple of\n\
1588 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1589or via the attributes si_pid, si_uid, and so on.\n\
1590\n\
1591See os.waitid for more information.");
1592
1593static PyStructSequence_Field waitid_result_fields[] = {
1594 {"si_pid", },
1595 {"si_uid", },
1596 {"si_signo", },
1597 {"si_status", },
1598 {"si_code", },
1599 {0}
1600};
1601
1602static PyStructSequence_Desc waitid_result_desc = {
1603 "waitid_result", /* name */
1604 waitid_result__doc__, /* doc */
1605 waitid_result_fields,
1606 5
1607};
1608static PyTypeObject WaitidResultType;
1609#endif
1610
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001611static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001612static PyTypeObject StatResultType;
1613static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001614#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001615static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001616#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001617static newfunc structseq_new;
1618
1619static PyObject *
1620statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1621{
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 PyStructSequence *result;
1623 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001624
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 result = (PyStructSequence*)structseq_new(type, args, kwds);
1626 if (!result)
1627 return NULL;
1628 /* If we have been initialized from a tuple,
1629 st_?time might be set to None. Initialize it
1630 from the int slots. */
1631 for (i = 7; i <= 9; i++) {
1632 if (result->ob_item[i+3] == Py_None) {
1633 Py_DECREF(Py_None);
1634 Py_INCREF(result->ob_item[i]);
1635 result->ob_item[i+3] = result->ob_item[i];
1636 }
1637 }
1638 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001639}
1640
1641
1642
1643/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001644static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001645
1646PyDoc_STRVAR(stat_float_times__doc__,
1647"stat_float_times([newval]) -> oldval\n\n\
1648Determine whether os.[lf]stat represents time stamps as float objects.\n\
1649If newval is True, future calls to stat() return floats, if it is False,\n\
1650future calls return ints. \n\
1651If newval is omitted, return the current setting.\n");
1652
1653static PyObject*
1654stat_float_times(PyObject* self, PyObject *args)
1655{
Victor Stinner8c62be82010-05-06 00:08:46 +00001656 int newval = -1;
1657 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1658 return NULL;
1659 if (newval == -1)
1660 /* Return old value */
1661 return PyBool_FromLong(_stat_float_times);
1662 _stat_float_times = newval;
1663 Py_INCREF(Py_None);
1664 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001665}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001666
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001667static void
1668fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1669{
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001671#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001673#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001674 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001675#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001676 if (!ival)
1677 return;
1678 if (_stat_float_times) {
1679 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1680 } else {
1681 fval = ival;
1682 Py_INCREF(fval);
1683 }
1684 PyStructSequence_SET_ITEM(v, index, ival);
1685 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001686}
1687
Tim Peters5aa91602002-01-30 05:46:57 +00001688/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001689 (used by posix_stat() and posix_fstat()) */
1690static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001691_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001692{
Victor Stinner8c62be82010-05-06 00:08:46 +00001693 unsigned long ansec, mnsec, cnsec;
1694 PyObject *v = PyStructSequence_New(&StatResultType);
1695 if (v == NULL)
1696 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001697
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001699#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 PyStructSequence_SET_ITEM(v, 1,
1701 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001702#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001703 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001704#endif
1705#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 PyStructSequence_SET_ITEM(v, 2,
1707 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001708#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001710#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1712 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1713 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001714#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001715 PyStructSequence_SET_ITEM(v, 6,
1716 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001717#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001719#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001720
Martin v. Löwis14694662006-02-03 12:54:16 +00001721#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001722 ansec = st->st_atim.tv_nsec;
1723 mnsec = st->st_mtim.tv_nsec;
1724 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001725#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 ansec = st->st_atimespec.tv_nsec;
1727 mnsec = st->st_mtimespec.tv_nsec;
1728 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001729#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 ansec = st->st_atime_nsec;
1731 mnsec = st->st_mtime_nsec;
1732 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001733#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001734 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001735#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 fill_time(v, 7, st->st_atime, ansec);
1737 fill_time(v, 8, st->st_mtime, mnsec);
1738 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001739
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001740#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1742 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001743#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001744#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001745 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1746 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001747#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001748#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001749 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1750 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001751#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001752#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1754 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001755#endif
1756#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001757 {
1758 PyObject *val;
1759 unsigned long bsec,bnsec;
1760 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001761#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001763#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001764 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001765#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001766 if (_stat_float_times) {
1767 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1768 } else {
1769 val = PyLong_FromLong((long)bsec);
1770 }
1771 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1772 val);
1773 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001774#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001775#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1777 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001778#endif
Fred Drake699f3522000-06-29 21:12:41 +00001779
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 if (PyErr_Occurred()) {
1781 Py_DECREF(v);
1782 return NULL;
1783 }
Fred Drake699f3522000-06-29 21:12:41 +00001784
Victor Stinner8c62be82010-05-06 00:08:46 +00001785 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001786}
1787
Barry Warsaw53699e91996-12-10 23:23:01 +00001788static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001789posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001793#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001795#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001796 char *wformat,
1797 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001798{
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 STRUCT_STAT st;
1800 PyObject *opath;
1801 char *path;
1802 int res;
1803 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001804
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001805#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 PyUnicodeObject *po;
1807 if (PyArg_ParseTuple(args, wformat, &po)) {
1808 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001809
Victor Stinner8c62be82010-05-06 00:08:46 +00001810 Py_BEGIN_ALLOW_THREADS
1811 /* PyUnicode_AS_UNICODE result OK without
1812 thread lock as it is a simple dereference. */
1813 res = wstatfunc(wpath, &st);
1814 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001815
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 if (res != 0)
1817 return win32_error_unicode("stat", wpath);
1818 return _pystat_fromstructstat(&st);
1819 }
1820 /* Drop the argument parsing error as narrow strings
1821 are also valid. */
1822 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001823#endif
1824
Victor Stinner8c62be82010-05-06 00:08:46 +00001825 if (!PyArg_ParseTuple(args, format,
1826 PyUnicode_FSConverter, &opath))
1827 return NULL;
1828 path = PyBytes_AsString(opath);
1829 Py_BEGIN_ALLOW_THREADS
1830 res = (*statfunc)(path, &st);
1831 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001832
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001834#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001836#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001837 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001838#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 }
1840 else
1841 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001842
Victor Stinner8c62be82010-05-06 00:08:46 +00001843 Py_DECREF(opath);
1844 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845}
1846
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001847/* POSIX methods */
1848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001850"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001851Use the real uid/gid to test for access to a path. Note that most\n\
1852operations will use the effective uid/gid, therefore this routine can\n\
1853be used in a suid/sgid environment to test if the invoking user has the\n\
1854specified access to the path. The mode argument can be F_OK to test\n\
1855existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001856
1857static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001858posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001859{
Victor Stinner8c62be82010-05-06 00:08:46 +00001860 PyObject *opath;
1861 char *path;
1862 int mode;
1863
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001864#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 DWORD attr;
1866 PyUnicodeObject *po;
1867 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1868 Py_BEGIN_ALLOW_THREADS
1869 /* PyUnicode_AS_UNICODE OK without thread lock as
1870 it is a simple dereference. */
1871 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1872 Py_END_ALLOW_THREADS
1873 goto finish;
1874 }
1875 /* Drop the argument parsing error as narrow strings
1876 are also valid. */
1877 PyErr_Clear();
1878 if (!PyArg_ParseTuple(args, "O&i:access",
1879 PyUnicode_FSConverter, &opath, &mode))
1880 return NULL;
1881 path = PyBytes_AsString(opath);
1882 Py_BEGIN_ALLOW_THREADS
1883 attr = GetFileAttributesA(path);
1884 Py_END_ALLOW_THREADS
1885 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001886finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001887 if (attr == 0xFFFFFFFF)
1888 /* File does not exist, or cannot read attributes */
1889 return PyBool_FromLong(0);
1890 /* Access is possible if either write access wasn't requested, or
1891 the file isn't read-only, or if it's a directory, as there are
1892 no read-only directories on Windows. */
1893 return PyBool_FromLong(!(mode & 2)
1894 || !(attr & FILE_ATTRIBUTE_READONLY)
1895 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001896#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001897 int res;
1898 if (!PyArg_ParseTuple(args, "O&i:access",
1899 PyUnicode_FSConverter, &opath, &mode))
1900 return NULL;
1901 path = PyBytes_AsString(opath);
1902 Py_BEGIN_ALLOW_THREADS
1903 res = access(path, mode);
1904 Py_END_ALLOW_THREADS
1905 Py_DECREF(opath);
1906 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001907#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001908}
1909
Guido van Rossumd371ff11999-01-25 16:12:23 +00001910#ifndef F_OK
1911#define F_OK 0
1912#endif
1913#ifndef R_OK
1914#define R_OK 4
1915#endif
1916#ifndef W_OK
1917#define W_OK 2
1918#endif
1919#ifndef X_OK
1920#define X_OK 1
1921#endif
1922
1923#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001924PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001926Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001927
1928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001929posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001930{
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 int id;
1932 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001933
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1935 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001936
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001937#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 /* file descriptor 0 only, the default input device (stdin) */
1939 if (id == 0) {
1940 ret = ttyname();
1941 }
1942 else {
1943 ret = NULL;
1944 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001945#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001947#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001948 if (ret == NULL)
1949 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001950 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001951}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001952#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001953
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001954#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001956"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001958
1959static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001960posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001961{
Victor Stinner8c62be82010-05-06 00:08:46 +00001962 char *ret;
1963 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001964
Greg Wardb48bc172000-03-01 21:51:56 +00001965#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001967#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001968 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001969#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001970 if (ret == NULL)
1971 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001972 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001973}
1974#endif
1975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001976PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001977"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001978Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979
Barry Warsaw53699e91996-12-10 23:23:01 +00001980static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001981posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001982{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001983#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001984 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001985#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001987#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001988 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001989#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001990 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001991#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001992}
1993
Fred Drake4d1e64b2002-04-15 19:40:07 +00001994#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001996"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001997Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001998opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001999
2000static PyObject *
2001posix_fchdir(PyObject *self, PyObject *fdobj)
2002{
Victor Stinner8c62be82010-05-06 00:08:46 +00002003 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002004}
2005#endif /* HAVE_FCHDIR */
2006
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002008PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002009"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002010Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002011
Barry Warsaw53699e91996-12-10 23:23:01 +00002012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002013posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002014{
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 PyObject *opath = NULL;
2016 char *path = NULL;
2017 int i;
2018 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002019#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002020 DWORD attr;
2021 PyUnicodeObject *po;
2022 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2023 Py_BEGIN_ALLOW_THREADS
2024 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2025 if (attr != 0xFFFFFFFF) {
2026 if (i & _S_IWRITE)
2027 attr &= ~FILE_ATTRIBUTE_READONLY;
2028 else
2029 attr |= FILE_ATTRIBUTE_READONLY;
2030 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2031 }
2032 else
2033 res = 0;
2034 Py_END_ALLOW_THREADS
2035 if (!res)
2036 return win32_error_unicode("chmod",
2037 PyUnicode_AS_UNICODE(po));
2038 Py_INCREF(Py_None);
2039 return Py_None;
2040 }
2041 /* Drop the argument parsing error as narrow strings
2042 are also valid. */
2043 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002044
Victor Stinner8c62be82010-05-06 00:08:46 +00002045 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2046 &opath, &i))
2047 return NULL;
2048 path = PyBytes_AsString(opath);
2049 Py_BEGIN_ALLOW_THREADS
2050 attr = GetFileAttributesA(path);
2051 if (attr != 0xFFFFFFFF) {
2052 if (i & _S_IWRITE)
2053 attr &= ~FILE_ATTRIBUTE_READONLY;
2054 else
2055 attr |= FILE_ATTRIBUTE_READONLY;
2056 res = SetFileAttributesA(path, attr);
2057 }
2058 else
2059 res = 0;
2060 Py_END_ALLOW_THREADS
2061 if (!res) {
2062 win32_error("chmod", path);
2063 Py_DECREF(opath);
2064 return NULL;
2065 }
2066 Py_DECREF(opath);
2067 Py_INCREF(Py_None);
2068 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002069#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002070 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2071 &opath, &i))
2072 return NULL;
2073 path = PyBytes_AsString(opath);
2074 Py_BEGIN_ALLOW_THREADS
2075 res = chmod(path, i);
2076 Py_END_ALLOW_THREADS
2077 if (res < 0)
2078 return posix_error_with_allocated_filename(opath);
2079 Py_DECREF(opath);
2080 Py_INCREF(Py_None);
2081 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002082#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002083}
2084
Christian Heimes4e30a842007-11-30 22:12:06 +00002085#ifdef HAVE_FCHMOD
2086PyDoc_STRVAR(posix_fchmod__doc__,
2087"fchmod(fd, mode)\n\n\
2088Change the access permissions of the file given by file\n\
2089descriptor fd.");
2090
2091static PyObject *
2092posix_fchmod(PyObject *self, PyObject *args)
2093{
Victor Stinner8c62be82010-05-06 00:08:46 +00002094 int fd, mode, res;
2095 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2096 return NULL;
2097 Py_BEGIN_ALLOW_THREADS
2098 res = fchmod(fd, mode);
2099 Py_END_ALLOW_THREADS
2100 if (res < 0)
2101 return posix_error();
2102 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002103}
2104#endif /* HAVE_FCHMOD */
2105
2106#ifdef HAVE_LCHMOD
2107PyDoc_STRVAR(posix_lchmod__doc__,
2108"lchmod(path, mode)\n\n\
2109Change the access permissions of a file. If path is a symlink, this\n\
2110affects the link itself rather than the target.");
2111
2112static PyObject *
2113posix_lchmod(PyObject *self, PyObject *args)
2114{
Victor Stinner8c62be82010-05-06 00:08:46 +00002115 PyObject *opath;
2116 char *path;
2117 int i;
2118 int res;
2119 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2120 &opath, &i))
2121 return NULL;
2122 path = PyBytes_AsString(opath);
2123 Py_BEGIN_ALLOW_THREADS
2124 res = lchmod(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_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002130}
2131#endif /* HAVE_LCHMOD */
2132
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002133
Thomas Wouterscf297e42007-02-23 15:07:44 +00002134#ifdef HAVE_CHFLAGS
2135PyDoc_STRVAR(posix_chflags__doc__,
2136"chflags(path, flags)\n\n\
2137Set file flags.");
2138
2139static PyObject *
2140posix_chflags(PyObject *self, PyObject *args)
2141{
Victor Stinner8c62be82010-05-06 00:08:46 +00002142 PyObject *opath;
2143 char *path;
2144 unsigned long flags;
2145 int res;
2146 if (!PyArg_ParseTuple(args, "O&k:chflags",
2147 PyUnicode_FSConverter, &opath, &flags))
2148 return NULL;
2149 path = PyBytes_AsString(opath);
2150 Py_BEGIN_ALLOW_THREADS
2151 res = chflags(path, flags);
2152 Py_END_ALLOW_THREADS
2153 if (res < 0)
2154 return posix_error_with_allocated_filename(opath);
2155 Py_DECREF(opath);
2156 Py_INCREF(Py_None);
2157 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002158}
2159#endif /* HAVE_CHFLAGS */
2160
2161#ifdef HAVE_LCHFLAGS
2162PyDoc_STRVAR(posix_lchflags__doc__,
2163"lchflags(path, flags)\n\n\
2164Set file flags.\n\
2165This function will not follow symbolic links.");
2166
2167static PyObject *
2168posix_lchflags(PyObject *self, PyObject *args)
2169{
Victor Stinner8c62be82010-05-06 00:08:46 +00002170 PyObject *opath;
2171 char *path;
2172 unsigned long flags;
2173 int res;
2174 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2175 PyUnicode_FSConverter, &opath, &flags))
2176 return NULL;
2177 path = PyBytes_AsString(opath);
2178 Py_BEGIN_ALLOW_THREADS
2179 res = lchflags(path, flags);
2180 Py_END_ALLOW_THREADS
2181 if (res < 0)
2182 return posix_error_with_allocated_filename(opath);
2183 Py_DECREF(opath);
2184 Py_INCREF(Py_None);
2185 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002186}
2187#endif /* HAVE_LCHFLAGS */
2188
Martin v. Löwis244edc82001-10-04 22:44:26 +00002189#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002190PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002191"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002192Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002193
2194static PyObject *
2195posix_chroot(PyObject *self, PyObject *args)
2196{
Victor Stinner8c62be82010-05-06 00:08:46 +00002197 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002198}
2199#endif
2200
Guido van Rossum21142a01999-01-08 21:05:37 +00002201#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002202PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002203"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002204force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002205
2206static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002207posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002208{
Stefan Krah0e803b32010-11-26 16:16:47 +00002209 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002210}
2211#endif /* HAVE_FSYNC */
2212
Ross Lagerwall7807c352011-03-17 20:20:30 +02002213#ifdef HAVE_SYNC
2214PyDoc_STRVAR(posix_sync__doc__,
2215"sync()\n\n\
2216Force write of everything to disk.");
2217
2218static PyObject *
2219posix_sync(PyObject *self, PyObject *noargs)
2220{
2221 Py_BEGIN_ALLOW_THREADS
2222 sync();
2223 Py_END_ALLOW_THREADS
2224 Py_RETURN_NONE;
2225}
2226#endif
2227
Guido van Rossum21142a01999-01-08 21:05:37 +00002228#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002229
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002230#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002231extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2232#endif
2233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002234PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002235"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002236force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002237 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002238
2239static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002240posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002241{
Stefan Krah0e803b32010-11-26 16:16:47 +00002242 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002243}
2244#endif /* HAVE_FDATASYNC */
2245
2246
Fredrik Lundh10723342000-07-10 16:38:09 +00002247#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002248PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002249"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002250Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002251
Barry Warsaw53699e91996-12-10 23:23:01 +00002252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002253posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002254{
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 PyObject *opath;
2256 char *path;
2257 long uid, gid;
2258 int res;
2259 if (!PyArg_ParseTuple(args, "O&ll:chown",
2260 PyUnicode_FSConverter, &opath,
2261 &uid, &gid))
2262 return NULL;
2263 path = PyBytes_AsString(opath);
2264 Py_BEGIN_ALLOW_THREADS
2265 res = chown(path, (uid_t) uid, (gid_t) gid);
2266 Py_END_ALLOW_THREADS
2267 if (res < 0)
2268 return posix_error_with_allocated_filename(opath);
2269 Py_DECREF(opath);
2270 Py_INCREF(Py_None);
2271 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002273#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002274
Christian Heimes4e30a842007-11-30 22:12:06 +00002275#ifdef HAVE_FCHOWN
2276PyDoc_STRVAR(posix_fchown__doc__,
2277"fchown(fd, uid, gid)\n\n\
2278Change the owner and group id of the file given by file descriptor\n\
2279fd to the numeric uid and gid.");
2280
2281static PyObject *
2282posix_fchown(PyObject *self, PyObject *args)
2283{
Victor Stinner8c62be82010-05-06 00:08:46 +00002284 int fd;
2285 long uid, gid;
2286 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002287 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002288 return NULL;
2289 Py_BEGIN_ALLOW_THREADS
2290 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2291 Py_END_ALLOW_THREADS
2292 if (res < 0)
2293 return posix_error();
2294 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002295}
2296#endif /* HAVE_FCHOWN */
2297
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002298#ifdef HAVE_LCHOWN
2299PyDoc_STRVAR(posix_lchown__doc__,
2300"lchown(path, uid, gid)\n\n\
2301Change the owner and group id of path to the numeric uid and gid.\n\
2302This function will not follow symbolic links.");
2303
2304static PyObject *
2305posix_lchown(PyObject *self, PyObject *args)
2306{
Victor Stinner8c62be82010-05-06 00:08:46 +00002307 PyObject *opath;
2308 char *path;
2309 long uid, gid;
2310 int res;
2311 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2312 PyUnicode_FSConverter, &opath,
2313 &uid, &gid))
2314 return NULL;
2315 path = PyBytes_AsString(opath);
2316 Py_BEGIN_ALLOW_THREADS
2317 res = lchown(path, (uid_t) uid, (gid_t) gid);
2318 Py_END_ALLOW_THREADS
2319 if (res < 0)
2320 return posix_error_with_allocated_filename(opath);
2321 Py_DECREF(opath);
2322 Py_INCREF(Py_None);
2323 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002324}
2325#endif /* HAVE_LCHOWN */
2326
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002327
Guido van Rossum36bc6801995-06-14 22:54:23 +00002328#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002329static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002330posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002331{
Victor Stinner8c62be82010-05-06 00:08:46 +00002332 char buf[1026];
2333 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002334
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002335#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002336 if (!use_bytes) {
2337 wchar_t wbuf[1026];
2338 wchar_t *wbuf2 = wbuf;
2339 PyObject *resobj;
2340 DWORD len;
2341 Py_BEGIN_ALLOW_THREADS
2342 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2343 /* If the buffer is large enough, len does not include the
2344 terminating \0. If the buffer is too small, len includes
2345 the space needed for the terminator. */
2346 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2347 wbuf2 = malloc(len * sizeof(wchar_t));
2348 if (wbuf2)
2349 len = GetCurrentDirectoryW(len, wbuf2);
2350 }
2351 Py_END_ALLOW_THREADS
2352 if (!wbuf2) {
2353 PyErr_NoMemory();
2354 return NULL;
2355 }
2356 if (!len) {
2357 if (wbuf2 != wbuf) free(wbuf2);
2358 return win32_error("getcwdu", NULL);
2359 }
2360 resobj = PyUnicode_FromWideChar(wbuf2, len);
2361 if (wbuf2 != wbuf) free(wbuf2);
2362 return resobj;
2363 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002364#endif
2365
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002367#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002369#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002370 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002371#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002372 Py_END_ALLOW_THREADS
2373 if (res == NULL)
2374 return posix_error();
2375 if (use_bytes)
2376 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002377 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002378}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002379
2380PyDoc_STRVAR(posix_getcwd__doc__,
2381"getcwd() -> path\n\n\
2382Return a unicode string representing the current working directory.");
2383
2384static PyObject *
2385posix_getcwd_unicode(PyObject *self)
2386{
2387 return posix_getcwd(0);
2388}
2389
2390PyDoc_STRVAR(posix_getcwdb__doc__,
2391"getcwdb() -> path\n\n\
2392Return a bytes string representing the current working directory.");
2393
2394static PyObject *
2395posix_getcwd_bytes(PyObject *self)
2396{
2397 return posix_getcwd(1);
2398}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002399#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002400
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002401
Guido van Rossumb6775db1994-08-01 11:34:53 +00002402#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002403PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002404"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002405Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002406
Barry Warsaw53699e91996-12-10 23:23:01 +00002407static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002408posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002409{
Victor Stinner8c62be82010-05-06 00:08:46 +00002410 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002411}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002412#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002413
Brian Curtin1b9df392010-11-24 20:24:31 +00002414#ifdef MS_WINDOWS
2415PyDoc_STRVAR(win32_link__doc__,
2416"link(src, dst)\n\n\
2417Create a hard link to a file.");
2418
2419static PyObject *
2420win32_link(PyObject *self, PyObject *args)
2421{
2422 PyObject *osrc, *odst;
2423 char *src, *dst;
2424 BOOL rslt;
2425
Brian Curtinfc889c42010-11-28 23:59:46 +00002426 PyUnicodeObject *usrc, *udst;
2427 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2428 Py_BEGIN_ALLOW_THREADS
2429 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2430 PyUnicode_AS_UNICODE(usrc), NULL);
2431 Py_END_ALLOW_THREADS
2432
2433 if (rslt == 0)
2434 return win32_error("link", NULL);
2435
2436 Py_RETURN_NONE;
2437 }
2438
2439 /* Narrow strings also valid. */
2440 PyErr_Clear();
2441
Brian Curtin1b9df392010-11-24 20:24:31 +00002442 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2443 PyUnicode_FSConverter, &odst))
2444 return NULL;
2445
2446 src = PyBytes_AsString(osrc);
2447 dst = PyBytes_AsString(odst);
2448
2449 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002450 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002451 Py_END_ALLOW_THREADS
2452
Stefan Krah30b341f2010-11-27 11:44:18 +00002453 Py_DECREF(osrc);
2454 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002455 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002456 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002457
2458 Py_RETURN_NONE;
2459}
2460#endif /* MS_WINDOWS */
2461
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002463PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002464"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002465Return a list containing the names of the entries in the directory.\n\
2466\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002467 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002468\n\
2469The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002471
Barry Warsaw53699e91996-12-10 23:23:01 +00002472static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002473posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002474{
Victor Stinner8c62be82010-05-06 00:08:46 +00002475 /* XXX Should redo this putting the (now four) versions of opendir
2476 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002477#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002478
Victor Stinner8c62be82010-05-06 00:08:46 +00002479 PyObject *d, *v;
2480 HANDLE hFindFile;
2481 BOOL result;
2482 WIN32_FIND_DATA FileData;
2483 PyObject *opath;
2484 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2485 char *bufptr = namebuf;
2486 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002487
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002488 PyObject *po = NULL;
2489 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002490 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002491 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002492
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002493 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002494 po_wchars = L".";
2495 len = 1;
2496 } else {
2497 po_wchars = PyUnicode_AS_UNICODE(po);
2498 len = PyUnicode_GET_SIZE(po);
2499 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002500 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002501 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2502 if (!wnamebuf) {
2503 PyErr_NoMemory();
2504 return NULL;
2505 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002506 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002507 if (len > 0) {
2508 Py_UNICODE wch = wnamebuf[len-1];
2509 if (wch != L'/' && wch != L'\\' && wch != L':')
2510 wnamebuf[len++] = L'\\';
2511 wcscpy(wnamebuf + len, L"*.*");
2512 }
2513 if ((d = PyList_New(0)) == NULL) {
2514 free(wnamebuf);
2515 return NULL;
2516 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002517 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002518 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002519 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002520 if (hFindFile == INVALID_HANDLE_VALUE) {
2521 int error = GetLastError();
2522 if (error == ERROR_FILE_NOT_FOUND) {
2523 free(wnamebuf);
2524 return d;
2525 }
2526 Py_DECREF(d);
2527 win32_error_unicode("FindFirstFileW", wnamebuf);
2528 free(wnamebuf);
2529 return NULL;
2530 }
2531 do {
2532 /* Skip over . and .. */
2533 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2534 wcscmp(wFileData.cFileName, L"..") != 0) {
2535 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2536 if (v == NULL) {
2537 Py_DECREF(d);
2538 d = NULL;
2539 break;
2540 }
2541 if (PyList_Append(d, v) != 0) {
2542 Py_DECREF(v);
2543 Py_DECREF(d);
2544 d = NULL;
2545 break;
2546 }
2547 Py_DECREF(v);
2548 }
2549 Py_BEGIN_ALLOW_THREADS
2550 result = FindNextFileW(hFindFile, &wFileData);
2551 Py_END_ALLOW_THREADS
2552 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2553 it got to the end of the directory. */
2554 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2555 Py_DECREF(d);
2556 win32_error_unicode("FindNextFileW", wnamebuf);
2557 FindClose(hFindFile);
2558 free(wnamebuf);
2559 return NULL;
2560 }
2561 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002562
Victor Stinner8c62be82010-05-06 00:08:46 +00002563 if (FindClose(hFindFile) == FALSE) {
2564 Py_DECREF(d);
2565 win32_error_unicode("FindClose", wnamebuf);
2566 free(wnamebuf);
2567 return NULL;
2568 }
2569 free(wnamebuf);
2570 return d;
2571 }
2572 /* Drop the argument parsing error as narrow strings
2573 are also valid. */
2574 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002575
Victor Stinner8c62be82010-05-06 00:08:46 +00002576 if (!PyArg_ParseTuple(args, "O&:listdir",
2577 PyUnicode_FSConverter, &opath))
2578 return NULL;
2579 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2580 PyErr_SetString(PyExc_ValueError, "path too long");
2581 Py_DECREF(opath);
2582 return NULL;
2583 }
2584 strcpy(namebuf, PyBytes_AsString(opath));
2585 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002586 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002587 if (len > 0) {
2588 char ch = namebuf[len-1];
2589 if (ch != SEP && ch != ALTSEP && ch != ':')
2590 namebuf[len++] = '/';
2591 strcpy(namebuf + len, "*.*");
2592 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002593
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 if ((d = PyList_New(0)) == NULL)
2595 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002596
Antoine Pitroub73caab2010-08-09 23:39:31 +00002597 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002598 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002599 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002600 if (hFindFile == INVALID_HANDLE_VALUE) {
2601 int error = GetLastError();
2602 if (error == ERROR_FILE_NOT_FOUND)
2603 return d;
2604 Py_DECREF(d);
2605 return win32_error("FindFirstFile", namebuf);
2606 }
2607 do {
2608 /* Skip over . and .. */
2609 if (strcmp(FileData.cFileName, ".") != 0 &&
2610 strcmp(FileData.cFileName, "..") != 0) {
2611 v = PyBytes_FromString(FileData.cFileName);
2612 if (v == NULL) {
2613 Py_DECREF(d);
2614 d = NULL;
2615 break;
2616 }
2617 if (PyList_Append(d, v) != 0) {
2618 Py_DECREF(v);
2619 Py_DECREF(d);
2620 d = NULL;
2621 break;
2622 }
2623 Py_DECREF(v);
2624 }
2625 Py_BEGIN_ALLOW_THREADS
2626 result = FindNextFile(hFindFile, &FileData);
2627 Py_END_ALLOW_THREADS
2628 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2629 it got to the end of the directory. */
2630 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2631 Py_DECREF(d);
2632 win32_error("FindNextFile", namebuf);
2633 FindClose(hFindFile);
2634 return NULL;
2635 }
2636 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002637
Victor Stinner8c62be82010-05-06 00:08:46 +00002638 if (FindClose(hFindFile) == FALSE) {
2639 Py_DECREF(d);
2640 return win32_error("FindClose", namebuf);
2641 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002642
Victor Stinner8c62be82010-05-06 00:08:46 +00002643 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002644
Tim Peters0bb44a42000-09-15 07:44:49 +00002645#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002646
2647#ifndef MAX_PATH
2648#define MAX_PATH CCHMAXPATH
2649#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002650 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002651 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002652 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002653 PyObject *d, *v;
2654 char namebuf[MAX_PATH+5];
2655 HDIR hdir = 1;
2656 ULONG srchcnt = 1;
2657 FILEFINDBUF3 ep;
2658 APIRET rc;
2659
Victor Stinner8c62be82010-05-06 00:08:46 +00002660 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002661 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002662 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002663 name = PyBytes_AsString(oname);
2664 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002665 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002666 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002667 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002668 return NULL;
2669 }
2670 strcpy(namebuf, name);
2671 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002672 if (*pt == ALTSEP)
2673 *pt = SEP;
2674 if (namebuf[len-1] != SEP)
2675 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002676 strcpy(namebuf + len, "*.*");
2677
Neal Norwitz6c913782007-10-14 03:23:09 +00002678 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002679 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002680 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002681 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002682
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002683 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2684 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002685 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002686 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2687 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2688 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002689
2690 if (rc != NO_ERROR) {
2691 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002692 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002693 }
2694
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002695 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002696 do {
2697 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002698 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002699 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002700
2701 strcpy(namebuf, ep.achName);
2702
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002703 /* Leave Case of Name Alone -- In Native Form */
2704 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002705
Christian Heimes72b710a2008-05-26 13:28:38 +00002706 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002707 if (v == NULL) {
2708 Py_DECREF(d);
2709 d = NULL;
2710 break;
2711 }
2712 if (PyList_Append(d, v) != 0) {
2713 Py_DECREF(v);
2714 Py_DECREF(d);
2715 d = NULL;
2716 break;
2717 }
2718 Py_DECREF(v);
2719 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2720 }
2721
Victor Stinnerdcb24032010-04-22 12:08:36 +00002722 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002723 return d;
2724#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002725 PyObject *oname;
2726 char *name;
2727 PyObject *d, *v;
2728 DIR *dirp;
2729 struct dirent *ep;
2730 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002731
Victor Stinner8c62be82010-05-06 00:08:46 +00002732 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002733 /* v is never read, so it does not need to be initialized yet. */
2734 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002735 arg_is_unicode = 0;
2736 PyErr_Clear();
2737 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002738 oname = NULL;
2739 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002740 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002741 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002742 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002743 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002744 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002745 Py_BEGIN_ALLOW_THREADS
2746 dirp = opendir(name);
2747 Py_END_ALLOW_THREADS
2748 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 return posix_error_with_allocated_filename(oname);
2750 }
2751 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002752 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002753 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002754 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002755 Py_DECREF(oname);
2756 return NULL;
2757 }
2758 for (;;) {
2759 errno = 0;
2760 Py_BEGIN_ALLOW_THREADS
2761 ep = readdir(dirp);
2762 Py_END_ALLOW_THREADS
2763 if (ep == NULL) {
2764 if (errno == 0) {
2765 break;
2766 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002767 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002768 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002769 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002770 Py_DECREF(d);
2771 return posix_error_with_allocated_filename(oname);
2772 }
2773 }
2774 if (ep->d_name[0] == '.' &&
2775 (NAMLEN(ep) == 1 ||
2776 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2777 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002778 if (arg_is_unicode)
2779 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2780 else
2781 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002783 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002784 break;
2785 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002786 if (PyList_Append(d, v) != 0) {
2787 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002788 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002789 break;
2790 }
2791 Py_DECREF(v);
2792 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002793 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002795 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002796 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002797
Victor Stinner8c62be82010-05-06 00:08:46 +00002798 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002799
Tim Peters0bb44a42000-09-15 07:44:49 +00002800#endif /* which OS */
2801} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002802
Antoine Pitrou8250e232011-02-25 23:41:16 +00002803#ifdef HAVE_FDOPENDIR
2804PyDoc_STRVAR(posix_fdlistdir__doc__,
2805"fdlistdir(fd) -> list_of_strings\n\n\
2806Like listdir(), but uses a file descriptor instead.\n\
2807After succesful execution of this function, fd will be closed.");
2808
2809static PyObject *
2810posix_fdlistdir(PyObject *self, PyObject *args)
2811{
2812 PyObject *d, *v;
2813 DIR *dirp;
2814 struct dirent *ep;
2815 int fd;
2816
2817 errno = 0;
2818 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2819 return NULL;
2820 Py_BEGIN_ALLOW_THREADS
2821 dirp = fdopendir(fd);
2822 Py_END_ALLOW_THREADS
2823 if (dirp == NULL) {
2824 close(fd);
2825 return posix_error();
2826 }
2827 if ((d = PyList_New(0)) == NULL) {
2828 Py_BEGIN_ALLOW_THREADS
2829 closedir(dirp);
2830 Py_END_ALLOW_THREADS
2831 return NULL;
2832 }
2833 for (;;) {
2834 errno = 0;
2835 Py_BEGIN_ALLOW_THREADS
2836 ep = readdir(dirp);
2837 Py_END_ALLOW_THREADS
2838 if (ep == NULL) {
2839 if (errno == 0) {
2840 break;
2841 } else {
2842 Py_BEGIN_ALLOW_THREADS
2843 closedir(dirp);
2844 Py_END_ALLOW_THREADS
2845 Py_DECREF(d);
2846 return posix_error();
2847 }
2848 }
2849 if (ep->d_name[0] == '.' &&
2850 (NAMLEN(ep) == 1 ||
2851 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2852 continue;
2853 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2854 if (v == NULL) {
2855 Py_CLEAR(d);
2856 break;
2857 }
2858 if (PyList_Append(d, v) != 0) {
2859 Py_DECREF(v);
2860 Py_CLEAR(d);
2861 break;
2862 }
2863 Py_DECREF(v);
2864 }
2865 Py_BEGIN_ALLOW_THREADS
2866 closedir(dirp);
2867 Py_END_ALLOW_THREADS
2868
2869 return d;
2870}
2871#endif
2872
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002873#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002874/* A helper function for abspath on win32 */
2875static PyObject *
2876posix__getfullpathname(PyObject *self, PyObject *args)
2877{
Victor Stinner8c62be82010-05-06 00:08:46 +00002878 PyObject *opath;
2879 char *path;
2880 char outbuf[MAX_PATH*2];
2881 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002882#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002883 PyUnicodeObject *po;
2884 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2885 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2886 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2887 Py_UNICODE *wtemp;
2888 DWORD result;
2889 PyObject *v;
2890 result = GetFullPathNameW(wpath,
2891 sizeof(woutbuf)/sizeof(woutbuf[0]),
2892 woutbuf, &wtemp);
2893 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2894 woutbufp = malloc(result * sizeof(Py_UNICODE));
2895 if (!woutbufp)
2896 return PyErr_NoMemory();
2897 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2898 }
2899 if (result)
2900 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2901 else
2902 v = win32_error_unicode("GetFullPathNameW", wpath);
2903 if (woutbufp != woutbuf)
2904 free(woutbufp);
2905 return v;
2906 }
2907 /* Drop the argument parsing error as narrow strings
2908 are also valid. */
2909 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002910
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002911#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002912 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2913 PyUnicode_FSConverter, &opath))
2914 return NULL;
2915 path = PyBytes_AsString(opath);
2916 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2917 outbuf, &temp)) {
2918 win32_error("GetFullPathName", path);
2919 Py_DECREF(opath);
2920 return NULL;
2921 }
2922 Py_DECREF(opath);
2923 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2924 return PyUnicode_Decode(outbuf, strlen(outbuf),
2925 Py_FileSystemDefaultEncoding, NULL);
2926 }
2927 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002928} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002929
Brian Curtind25aef52011-06-13 15:16:04 -05002930
Brian Curtinf5e76d02010-11-24 13:14:05 +00002931
Brian Curtind40e6f72010-07-08 21:39:08 +00002932/* A helper function for samepath on windows */
2933static PyObject *
2934posix__getfinalpathname(PyObject *self, PyObject *args)
2935{
2936 HANDLE hFile;
2937 int buf_size;
2938 wchar_t *target_path;
2939 int result_length;
2940 PyObject *result;
2941 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002942
Brian Curtin94622b02010-09-24 00:03:39 +00002943 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002944 return NULL;
2945 }
2946
2947 if(!check_GetFinalPathNameByHandle()) {
2948 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2949 NotImplementedError. */
2950 return PyErr_Format(PyExc_NotImplementedError,
2951 "GetFinalPathNameByHandle not available on this platform");
2952 }
2953
2954 hFile = CreateFileW(
2955 path,
2956 0, /* desired access */
2957 0, /* share mode */
2958 NULL, /* security attributes */
2959 OPEN_EXISTING,
2960 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2961 FILE_FLAG_BACKUP_SEMANTICS,
2962 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002963
Brian Curtind40e6f72010-07-08 21:39:08 +00002964 if(hFile == INVALID_HANDLE_VALUE) {
2965 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002966 return PyErr_Format(PyExc_RuntimeError,
2967 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002968 }
2969
2970 /* We have a good handle to the target, use it to determine the
2971 target path name. */
2972 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2973
2974 if(!buf_size)
2975 return win32_error_unicode("GetFinalPathNameByHandle", path);
2976
2977 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2978 if(!target_path)
2979 return PyErr_NoMemory();
2980
2981 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2982 buf_size, VOLUME_NAME_DOS);
2983 if(!result_length)
2984 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2985
2986 if(!CloseHandle(hFile))
2987 return win32_error_unicode("GetFinalPathNameByHandle", path);
2988
2989 target_path[result_length] = 0;
2990 result = PyUnicode_FromUnicode(target_path, result_length);
2991 free(target_path);
2992 return result;
2993
2994} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002995
2996static PyObject *
2997posix__getfileinformation(PyObject *self, PyObject *args)
2998{
2999 HANDLE hFile;
3000 BY_HANDLE_FILE_INFORMATION info;
3001 int fd;
3002
3003 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3004 return NULL;
3005
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003006 if (!_PyVerify_fd(fd))
3007 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003008
3009 hFile = (HANDLE)_get_osfhandle(fd);
3010 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003011 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003012
3013 if (!GetFileInformationByHandle(hFile, &info))
3014 return win32_error("_getfileinformation", NULL);
3015
3016 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3017 info.nFileIndexHigh,
3018 info.nFileIndexLow);
3019}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003020
Brian Curtin95d028f2011-06-09 09:10:38 -05003021PyDoc_STRVAR(posix__isdir__doc__,
3022"Return true if the pathname refers to an existing directory.");
3023
Brian Curtin9c669cc2011-06-08 18:17:18 -05003024static PyObject *
3025posix__isdir(PyObject *self, PyObject *args)
3026{
3027 PyObject *opath;
3028 char *path;
3029 PyUnicodeObject *po;
3030 DWORD attributes;
3031
3032 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3033 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3034
3035 attributes = GetFileAttributesW(wpath);
3036 if (attributes == INVALID_FILE_ATTRIBUTES)
3037 Py_RETURN_FALSE;
3038 goto check;
3039 }
3040 /* Drop the argument parsing error as narrow strings
3041 are also valid. */
3042 PyErr_Clear();
3043
3044 if (!PyArg_ParseTuple(args, "O&:_isdir",
3045 PyUnicode_FSConverter, &opath))
3046 return NULL;
3047
3048 path = PyBytes_AsString(opath);
3049 attributes = GetFileAttributesA(path);
3050 if (attributes == INVALID_FILE_ATTRIBUTES)
3051 Py_RETURN_FALSE;
3052
3053check:
3054 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3055 Py_RETURN_TRUE;
3056 else
3057 Py_RETURN_FALSE;
3058}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003059#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003061PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003062"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003063Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003064
Barry Warsaw53699e91996-12-10 23:23:01 +00003065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003066posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003067{
Victor Stinner8c62be82010-05-06 00:08:46 +00003068 int res;
3069 PyObject *opath;
3070 char *path;
3071 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003072
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003073#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003074 PyUnicodeObject *po;
3075 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3076 Py_BEGIN_ALLOW_THREADS
3077 /* PyUnicode_AS_UNICODE OK without thread lock as
3078 it is a simple dereference. */
3079 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3080 Py_END_ALLOW_THREADS
3081 if (!res)
3082 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3083 Py_INCREF(Py_None);
3084 return Py_None;
3085 }
3086 /* Drop the argument parsing error as narrow strings
3087 are also valid. */
3088 PyErr_Clear();
3089 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3090 PyUnicode_FSConverter, &opath, &mode))
3091 return NULL;
3092 path = PyBytes_AsString(opath);
3093 Py_BEGIN_ALLOW_THREADS
3094 /* PyUnicode_AS_UNICODE OK without thread lock as
3095 it is a simple dereference. */
3096 res = CreateDirectoryA(path, NULL);
3097 Py_END_ALLOW_THREADS
3098 if (!res) {
3099 win32_error("mkdir", path);
3100 Py_DECREF(opath);
3101 return NULL;
3102 }
3103 Py_DECREF(opath);
3104 Py_INCREF(Py_None);
3105 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003106#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003107
Victor Stinner8c62be82010-05-06 00:08:46 +00003108 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3109 PyUnicode_FSConverter, &opath, &mode))
3110 return NULL;
3111 path = PyBytes_AsString(opath);
3112 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003113#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003115#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003116 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003117#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003118 Py_END_ALLOW_THREADS
3119 if (res < 0)
3120 return posix_error_with_allocated_filename(opath);
3121 Py_DECREF(opath);
3122 Py_INCREF(Py_None);
3123 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003124#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003125}
3126
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003127
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003128/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3129#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003130#include <sys/resource.h>
3131#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003132
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003133
3134#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003135PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003136"nice(inc) -> new_priority\n\n\
3137Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003138
Barry Warsaw53699e91996-12-10 23:23:01 +00003139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003140posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003141{
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003143
Victor Stinner8c62be82010-05-06 00:08:46 +00003144 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3145 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003146
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 /* There are two flavours of 'nice': one that returns the new
3148 priority (as required by almost all standards out there) and the
3149 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3150 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003151
Victor Stinner8c62be82010-05-06 00:08:46 +00003152 If we are of the nice family that returns the new priority, we
3153 need to clear errno before the call, and check if errno is filled
3154 before calling posix_error() on a returnvalue of -1, because the
3155 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003156
Victor Stinner8c62be82010-05-06 00:08:46 +00003157 errno = 0;
3158 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003159#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 if (value == 0)
3161 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003162#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 if (value == -1 && errno != 0)
3164 /* either nice() or getpriority() returned an error */
3165 return posix_error();
3166 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003167}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003168#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003169
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003170
3171#ifdef HAVE_GETPRIORITY
3172PyDoc_STRVAR(posix_getpriority__doc__,
3173"getpriority(which, who) -> current_priority\n\n\
3174Get program scheduling priority.");
3175
3176static PyObject *
3177posix_getpriority(PyObject *self, PyObject *args)
3178{
3179 int which, who, retval;
3180
3181 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3182 return NULL;
3183 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003184 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003185 if (errno != 0)
3186 return posix_error();
3187 return PyLong_FromLong((long)retval);
3188}
3189#endif /* HAVE_GETPRIORITY */
3190
3191
3192#ifdef HAVE_SETPRIORITY
3193PyDoc_STRVAR(posix_setpriority__doc__,
3194"setpriority(which, who, prio) -> None\n\n\
3195Set program scheduling priority.");
3196
3197static PyObject *
3198posix_setpriority(PyObject *self, PyObject *args)
3199{
3200 int which, who, prio, retval;
3201
3202 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3203 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003204 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003205 if (retval == -1)
3206 return posix_error();
3207 Py_RETURN_NONE;
3208}
3209#endif /* HAVE_SETPRIORITY */
3210
3211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003212PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003213"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003214Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003215
Barry Warsaw53699e91996-12-10 23:23:01 +00003216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003217posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003218{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003219#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003220 PyObject *o1, *o2;
3221 char *p1, *p2;
3222 BOOL result;
3223 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3224 goto error;
3225 if (!convert_to_unicode(&o1))
3226 goto error;
3227 if (!convert_to_unicode(&o2)) {
3228 Py_DECREF(o1);
3229 goto error;
3230 }
3231 Py_BEGIN_ALLOW_THREADS
3232 result = MoveFileW(PyUnicode_AsUnicode(o1),
3233 PyUnicode_AsUnicode(o2));
3234 Py_END_ALLOW_THREADS
3235 Py_DECREF(o1);
3236 Py_DECREF(o2);
3237 if (!result)
3238 return win32_error("rename", NULL);
3239 Py_INCREF(Py_None);
3240 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003241error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003242 PyErr_Clear();
3243 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3244 return NULL;
3245 Py_BEGIN_ALLOW_THREADS
3246 result = MoveFileA(p1, p2);
3247 Py_END_ALLOW_THREADS
3248 if (!result)
3249 return win32_error("rename", NULL);
3250 Py_INCREF(Py_None);
3251 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003252#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003253 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003254#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003255}
3256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003258PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003259"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003260Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003261
Barry Warsaw53699e91996-12-10 23:23:01 +00003262static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003263posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003264{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003265#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003266 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003267#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003268 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003269#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003270}
3271
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003272
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003273PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003274"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003275Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003276
Barry Warsaw53699e91996-12-10 23:23:01 +00003277static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003278posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003279{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003280#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003281 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003282#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003283 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003284#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003285}
3286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003287
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003288#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003289PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003290"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003291Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003292
Barry Warsaw53699e91996-12-10 23:23:01 +00003293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003294posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003295{
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003297#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 wchar_t *command;
3299 if (!PyArg_ParseTuple(args, "u:system", &command))
3300 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003301
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 Py_BEGIN_ALLOW_THREADS
3303 sts = _wsystem(command);
3304 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003305#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 PyObject *command_obj;
3307 char *command;
3308 if (!PyArg_ParseTuple(args, "O&:system",
3309 PyUnicode_FSConverter, &command_obj))
3310 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003311
Victor Stinner8c62be82010-05-06 00:08:46 +00003312 command = PyBytes_AsString(command_obj);
3313 Py_BEGIN_ALLOW_THREADS
3314 sts = system(command);
3315 Py_END_ALLOW_THREADS
3316 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003317#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003318 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003319}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003320#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003321
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003323PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003324"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003325Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003326
Barry Warsaw53699e91996-12-10 23:23:01 +00003327static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003328posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003329{
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 int i;
3331 if (!PyArg_ParseTuple(args, "i:umask", &i))
3332 return NULL;
3333 i = (int)umask(i);
3334 if (i < 0)
3335 return posix_error();
3336 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003337}
3338
Brian Curtind40e6f72010-07-08 21:39:08 +00003339#ifdef MS_WINDOWS
3340
3341/* override the default DeleteFileW behavior so that directory
3342symlinks can be removed with this function, the same as with
3343Unix symlinks */
3344BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3345{
3346 WIN32_FILE_ATTRIBUTE_DATA info;
3347 WIN32_FIND_DATAW find_data;
3348 HANDLE find_data_handle;
3349 int is_directory = 0;
3350 int is_link = 0;
3351
3352 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3353 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003354
Brian Curtind40e6f72010-07-08 21:39:08 +00003355 /* Get WIN32_FIND_DATA structure for the path to determine if
3356 it is a symlink */
3357 if(is_directory &&
3358 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3359 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3360
3361 if(find_data_handle != INVALID_HANDLE_VALUE) {
3362 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3363 FindClose(find_data_handle);
3364 }
3365 }
3366 }
3367
3368 if (is_directory && is_link)
3369 return RemoveDirectoryW(lpFileName);
3370
3371 return DeleteFileW(lpFileName);
3372}
3373#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003376"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003377Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003379PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003380"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003381Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003382
Barry Warsaw53699e91996-12-10 23:23:01 +00003383static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003384posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003385{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003386#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003387 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3388 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003389#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003390 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003391#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003392}
3393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003394
Guido van Rossumb6775db1994-08-01 11:34:53 +00003395#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003396PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003397"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003398Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003399
Barry Warsaw53699e91996-12-10 23:23:01 +00003400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003401posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003402{
Victor Stinner8c62be82010-05-06 00:08:46 +00003403 struct utsname u;
3404 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003405
Victor Stinner8c62be82010-05-06 00:08:46 +00003406 Py_BEGIN_ALLOW_THREADS
3407 res = uname(&u);
3408 Py_END_ALLOW_THREADS
3409 if (res < 0)
3410 return posix_error();
3411 return Py_BuildValue("(sssss)",
3412 u.sysname,
3413 u.nodename,
3414 u.release,
3415 u.version,
3416 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003417}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003418#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003419
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003420static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003421extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003422{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003423 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 if (PyFloat_Check(t)) {
3425 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003426 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003427 if (!intobj)
3428 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003429#if SIZEOF_TIME_T > SIZEOF_LONG
3430 intval = PyLong_AsUnsignedLongLongMask(intobj);
3431#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003432 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003433#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003434 Py_DECREF(intobj);
3435 if (intval == -1 && PyErr_Occurred())
3436 return -1;
3437 *sec = intval;
3438 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3439 if (*usec < 0)
3440 /* If rounding gave us a negative number,
3441 truncate. */
3442 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003443 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003445#if SIZEOF_TIME_T > SIZEOF_LONG
3446 intval = PyLong_AsUnsignedLongLongMask(t);
3447#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003448 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003449#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003450 if (intval == -1 && PyErr_Occurred())
3451 return -1;
3452 *sec = intval;
3453 *usec = 0;
3454 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003455}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003457PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003458"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003459utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003460Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003461second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003462
Barry Warsaw53699e91996-12-10 23:23:01 +00003463static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003464posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003465{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003466#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003467 PyObject *arg;
3468 PyUnicodeObject *obwpath;
3469 wchar_t *wpath = NULL;
3470 PyObject *oapath;
3471 char *apath;
3472 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003473 time_t atimesec, mtimesec;
3474 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003475 FILETIME atime, mtime;
3476 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003477
Victor Stinner8c62be82010-05-06 00:08:46 +00003478 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3479 wpath = PyUnicode_AS_UNICODE(obwpath);
3480 Py_BEGIN_ALLOW_THREADS
3481 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3482 NULL, OPEN_EXISTING,
3483 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3484 Py_END_ALLOW_THREADS
3485 if (hFile == INVALID_HANDLE_VALUE)
3486 return win32_error_unicode("utime", wpath);
3487 } else
3488 /* Drop the argument parsing error as narrow strings
3489 are also valid. */
3490 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003491
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 if (!wpath) {
3493 if (!PyArg_ParseTuple(args, "O&O:utime",
3494 PyUnicode_FSConverter, &oapath, &arg))
3495 return NULL;
3496 apath = PyBytes_AsString(oapath);
3497 Py_BEGIN_ALLOW_THREADS
3498 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3499 NULL, OPEN_EXISTING,
3500 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3501 Py_END_ALLOW_THREADS
3502 if (hFile == INVALID_HANDLE_VALUE) {
3503 win32_error("utime", apath);
3504 Py_DECREF(oapath);
3505 return NULL;
3506 }
3507 Py_DECREF(oapath);
3508 }
3509
3510 if (arg == Py_None) {
3511 SYSTEMTIME now;
3512 GetSystemTime(&now);
3513 if (!SystemTimeToFileTime(&now, &mtime) ||
3514 !SystemTimeToFileTime(&now, &atime)) {
3515 win32_error("utime", NULL);
3516 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003517 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003518 }
3519 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3520 PyErr_SetString(PyExc_TypeError,
3521 "utime() arg 2 must be a tuple (atime, mtime)");
3522 goto done;
3523 }
3524 else {
3525 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3526 &atimesec, &ausec) == -1)
3527 goto done;
3528 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3529 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3530 &mtimesec, &musec) == -1)
3531 goto done;
3532 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3533 }
3534 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3535 /* Avoid putting the file name into the error here,
3536 as that may confuse the user into believing that
3537 something is wrong with the file, when it also
3538 could be the time stamp that gives a problem. */
3539 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003540 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003541 }
3542 Py_INCREF(Py_None);
3543 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003544done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 CloseHandle(hFile);
3546 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003547#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003548
Victor Stinner8c62be82010-05-06 00:08:46 +00003549 PyObject *opath;
3550 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003551 time_t atime, mtime;
3552 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 int res;
3554 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003555
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003556#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003558#define ATIME buf[0].tv_sec
3559#define MTIME buf[1].tv_sec
3560#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003561/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003562 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003563#define ATIME buf.actime
3564#define MTIME buf.modtime
3565#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003566#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003567 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003568#define ATIME buf[0]
3569#define MTIME buf[1]
3570#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003571#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003572
Mark Hammond817c9292003-12-03 01:22:38 +00003573
Victor Stinner8c62be82010-05-06 00:08:46 +00003574 if (!PyArg_ParseTuple(args, "O&O:utime",
3575 PyUnicode_FSConverter, &opath, &arg))
3576 return NULL;
3577 path = PyBytes_AsString(opath);
3578 if (arg == Py_None) {
3579 /* optional time values not given */
3580 Py_BEGIN_ALLOW_THREADS
3581 res = utime(path, NULL);
3582 Py_END_ALLOW_THREADS
3583 }
3584 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3585 PyErr_SetString(PyExc_TypeError,
3586 "utime() arg 2 must be a tuple (atime, mtime)");
3587 Py_DECREF(opath);
3588 return NULL;
3589 }
3590 else {
3591 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3592 &atime, &ausec) == -1) {
3593 Py_DECREF(opath);
3594 return NULL;
3595 }
3596 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3597 &mtime, &musec) == -1) {
3598 Py_DECREF(opath);
3599 return NULL;
3600 }
3601 ATIME = atime;
3602 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003603#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003604 buf[0].tv_usec = ausec;
3605 buf[1].tv_usec = musec;
3606 Py_BEGIN_ALLOW_THREADS
3607 res = utimes(path, buf);
3608 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003609#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 Py_BEGIN_ALLOW_THREADS
3611 res = utime(path, UTIME_ARG);
3612 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003613#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 }
3615 if (res < 0) {
3616 return posix_error_with_allocated_filename(opath);
3617 }
3618 Py_DECREF(opath);
3619 Py_INCREF(Py_None);
3620 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003621#undef UTIME_ARG
3622#undef ATIME
3623#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003624#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003625}
3626
Ross Lagerwall7807c352011-03-17 20:20:30 +02003627#ifdef HAVE_FUTIMES
3628PyDoc_STRVAR(posix_futimes__doc__,
3629"futimes(fd, (atime, mtime))\n\
3630futimes(fd, None)\n\n\
3631Set the access and modified time of the file specified by the file\n\
3632descriptor fd to the given values. If the second form is used, set the\n\
3633access and modified times to the current time.");
3634
3635static PyObject *
3636posix_futimes(PyObject *self, PyObject *args)
3637{
3638 int res, fd;
3639 PyObject* arg;
3640 struct timeval buf[2];
3641 long ausec, musec;
3642
3643 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3644 return NULL;
3645
3646 if (arg == Py_None) {
3647 /* optional time values not given */
3648 Py_BEGIN_ALLOW_THREADS
3649 res = futimes(fd, NULL);
3650 Py_END_ALLOW_THREADS
3651 }
3652 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3653 PyErr_SetString(PyExc_TypeError,
3654 "futimes() arg 2 must be a tuple (atime, mtime)");
3655 return NULL;
3656 }
3657 else {
3658 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3659 &(buf[0].tv_sec), &ausec) == -1) {
3660 return NULL;
3661 }
3662 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3663 &(buf[1].tv_sec), &musec) == -1) {
3664 return NULL;
3665 }
3666 buf[0].tv_usec = ausec;
3667 buf[1].tv_usec = musec;
3668 Py_BEGIN_ALLOW_THREADS
3669 res = futimes(fd, buf);
3670 Py_END_ALLOW_THREADS
3671 }
3672 if (res < 0)
3673 return posix_error();
3674 Py_RETURN_NONE;
3675}
3676#endif
3677
3678#ifdef HAVE_LUTIMES
3679PyDoc_STRVAR(posix_lutimes__doc__,
3680"lutimes(path, (atime, mtime))\n\
3681lutimes(path, None)\n\n\
3682Like utime(), but if path is a symbolic link, it is not dereferenced.");
3683
3684static PyObject *
3685posix_lutimes(PyObject *self, PyObject *args)
3686{
3687 PyObject *opath, *arg;
3688 const char *path;
3689 int res;
3690 struct timeval buf[2];
3691 long ausec, musec;
3692
3693 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3694 PyUnicode_FSConverter, &opath, &arg))
3695 return NULL;
3696 path = PyBytes_AsString(opath);
3697 if (arg == Py_None) {
3698 /* optional time values not given */
3699 Py_BEGIN_ALLOW_THREADS
3700 res = lutimes(path, NULL);
3701 Py_END_ALLOW_THREADS
3702 }
3703 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3704 PyErr_SetString(PyExc_TypeError,
3705 "lutimes() arg 2 must be a tuple (atime, mtime)");
3706 Py_DECREF(opath);
3707 return NULL;
3708 }
3709 else {
3710 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3711 &(buf[0].tv_sec), &ausec) == -1) {
3712 Py_DECREF(opath);
3713 return NULL;
3714 }
3715 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3716 &(buf[1].tv_sec), &musec) == -1) {
3717 Py_DECREF(opath);
3718 return NULL;
3719 }
3720 buf[0].tv_usec = ausec;
3721 buf[1].tv_usec = musec;
3722 Py_BEGIN_ALLOW_THREADS
3723 res = lutimes(path, buf);
3724 Py_END_ALLOW_THREADS
3725 }
3726 Py_DECREF(opath);
3727 if (res < 0)
3728 return posix_error();
3729 Py_RETURN_NONE;
3730}
3731#endif
3732
3733#ifdef HAVE_FUTIMENS
3734PyDoc_STRVAR(posix_futimens__doc__,
3735"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3736futimens(fd, None, None)\n\n\
3737Updates the timestamps of a file specified by the file descriptor fd, with\n\
3738nanosecond precision.\n\
3739The second form sets atime and mtime to the current time.\n\
3740If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3741current time.\n\
3742If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3743
3744static PyObject *
3745posix_futimens(PyObject *self, PyObject *args)
3746{
3747 int res, fd;
3748 PyObject *atime, *mtime;
3749 struct timespec buf[2];
3750
3751 if (!PyArg_ParseTuple(args, "iOO:futimens",
3752 &fd, &atime, &mtime))
3753 return NULL;
3754 if (atime == Py_None && mtime == Py_None) {
3755 /* optional time values not given */
3756 Py_BEGIN_ALLOW_THREADS
3757 res = futimens(fd, NULL);
3758 Py_END_ALLOW_THREADS
3759 }
3760 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3761 PyErr_SetString(PyExc_TypeError,
3762 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3763 return NULL;
3764 }
3765 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3766 PyErr_SetString(PyExc_TypeError,
3767 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3768 return NULL;
3769 }
3770 else {
3771 if (!PyArg_ParseTuple(atime, "ll:futimens",
3772 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3773 return NULL;
3774 }
3775 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3776 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3777 return NULL;
3778 }
3779 Py_BEGIN_ALLOW_THREADS
3780 res = futimens(fd, buf);
3781 Py_END_ALLOW_THREADS
3782 }
3783 if (res < 0)
3784 return posix_error();
3785 Py_RETURN_NONE;
3786}
3787#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003788
Guido van Rossum3b066191991-06-04 19:40:25 +00003789/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003790
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003791PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003792"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003793Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003794
Barry Warsaw53699e91996-12-10 23:23:01 +00003795static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003796posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003797{
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 int sts;
3799 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3800 return NULL;
3801 _exit(sts);
3802 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003803}
3804
Martin v. Löwis114619e2002-10-07 06:44:21 +00003805#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3806static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003807free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003808{
Victor Stinner8c62be82010-05-06 00:08:46 +00003809 Py_ssize_t i;
3810 for (i = 0; i < count; i++)
3811 PyMem_Free(array[i]);
3812 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003813}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003814
Antoine Pitrou69f71142009-05-24 21:25:49 +00003815static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003816int fsconvert_strdup(PyObject *o, char**out)
3817{
Victor Stinner8c62be82010-05-06 00:08:46 +00003818 PyObject *bytes;
3819 Py_ssize_t size;
3820 if (!PyUnicode_FSConverter(o, &bytes))
3821 return 0;
3822 size = PyBytes_GET_SIZE(bytes);
3823 *out = PyMem_Malloc(size+1);
3824 if (!*out)
3825 return 0;
3826 memcpy(*out, PyBytes_AsString(bytes), size+1);
3827 Py_DECREF(bytes);
3828 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003829}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003830#endif
3831
Ross Lagerwall7807c352011-03-17 20:20:30 +02003832#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003833static char**
3834parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3835{
Victor Stinner8c62be82010-05-06 00:08:46 +00003836 char **envlist;
3837 Py_ssize_t i, pos, envc;
3838 PyObject *keys=NULL, *vals=NULL;
3839 PyObject *key, *val, *key2, *val2;
3840 char *p, *k, *v;
3841 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003842
Victor Stinner8c62be82010-05-06 00:08:46 +00003843 i = PyMapping_Size(env);
3844 if (i < 0)
3845 return NULL;
3846 envlist = PyMem_NEW(char *, i + 1);
3847 if (envlist == NULL) {
3848 PyErr_NoMemory();
3849 return NULL;
3850 }
3851 envc = 0;
3852 keys = PyMapping_Keys(env);
3853 vals = PyMapping_Values(env);
3854 if (!keys || !vals)
3855 goto error;
3856 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3857 PyErr_Format(PyExc_TypeError,
3858 "env.keys() or env.values() is not a list");
3859 goto error;
3860 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003861
Victor Stinner8c62be82010-05-06 00:08:46 +00003862 for (pos = 0; pos < i; pos++) {
3863 key = PyList_GetItem(keys, pos);
3864 val = PyList_GetItem(vals, pos);
3865 if (!key || !val)
3866 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003867
Victor Stinner8c62be82010-05-06 00:08:46 +00003868 if (PyUnicode_FSConverter(key, &key2) == 0)
3869 goto error;
3870 if (PyUnicode_FSConverter(val, &val2) == 0) {
3871 Py_DECREF(key2);
3872 goto error;
3873 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003874
3875#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003876 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3877 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003878#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003879 k = PyBytes_AsString(key2);
3880 v = PyBytes_AsString(val2);
3881 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003882
Victor Stinner8c62be82010-05-06 00:08:46 +00003883 p = PyMem_NEW(char, len);
3884 if (p == NULL) {
3885 PyErr_NoMemory();
3886 Py_DECREF(key2);
3887 Py_DECREF(val2);
3888 goto error;
3889 }
3890 PyOS_snprintf(p, len, "%s=%s", k, v);
3891 envlist[envc++] = p;
3892 Py_DECREF(key2);
3893 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003894#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003895 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003896#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 }
3898 Py_DECREF(vals);
3899 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003900
Victor Stinner8c62be82010-05-06 00:08:46 +00003901 envlist[envc] = 0;
3902 *envc_ptr = envc;
3903 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003904
3905error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003906 Py_XDECREF(keys);
3907 Py_XDECREF(vals);
3908 while (--envc >= 0)
3909 PyMem_DEL(envlist[envc]);
3910 PyMem_DEL(envlist);
3911 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003912}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003913
Ross Lagerwall7807c352011-03-17 20:20:30 +02003914static char**
3915parse_arglist(PyObject* argv, Py_ssize_t *argc)
3916{
3917 int i;
3918 char **argvlist = PyMem_NEW(char *, *argc+1);
3919 if (argvlist == NULL) {
3920 PyErr_NoMemory();
3921 return NULL;
3922 }
3923 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003924 PyObject* item = PySequence_ITEM(argv, i);
3925 if (item == NULL)
3926 goto fail;
3927 if (!fsconvert_strdup(item, &argvlist[i])) {
3928 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003929 goto fail;
3930 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003931 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003932 }
3933 argvlist[*argc] = NULL;
3934 return argvlist;
3935fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003936 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003937 free_string_array(argvlist, *argc);
3938 return NULL;
3939}
3940#endif
3941
3942#ifdef HAVE_EXECV
3943PyDoc_STRVAR(posix_execv__doc__,
3944"execv(path, args)\n\n\
3945Execute an executable path with arguments, replacing current process.\n\
3946\n\
3947 path: path of executable file\n\
3948 args: tuple or list of strings");
3949
3950static PyObject *
3951posix_execv(PyObject *self, PyObject *args)
3952{
3953 PyObject *opath;
3954 char *path;
3955 PyObject *argv;
3956 char **argvlist;
3957 Py_ssize_t argc;
3958
3959 /* execv has two arguments: (path, argv), where
3960 argv is a list or tuple of strings. */
3961
3962 if (!PyArg_ParseTuple(args, "O&O:execv",
3963 PyUnicode_FSConverter,
3964 &opath, &argv))
3965 return NULL;
3966 path = PyBytes_AsString(opath);
3967 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3968 PyErr_SetString(PyExc_TypeError,
3969 "execv() arg 2 must be a tuple or list");
3970 Py_DECREF(opath);
3971 return NULL;
3972 }
3973 argc = PySequence_Size(argv);
3974 if (argc < 1) {
3975 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3976 Py_DECREF(opath);
3977 return NULL;
3978 }
3979
3980 argvlist = parse_arglist(argv, &argc);
3981 if (argvlist == NULL) {
3982 Py_DECREF(opath);
3983 return NULL;
3984 }
3985
3986 execv(path, argvlist);
3987
3988 /* If we get here it's definitely an error */
3989
3990 free_string_array(argvlist, argc);
3991 Py_DECREF(opath);
3992 return posix_error();
3993}
3994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003995PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003996"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003997Execute a path with arguments and environment, replacing current process.\n\
3998\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003999 path: path of executable file\n\
4000 args: tuple or list of arguments\n\
4001 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004002
Barry Warsaw53699e91996-12-10 23:23:01 +00004003static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004004posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004005{
Victor Stinner8c62be82010-05-06 00:08:46 +00004006 PyObject *opath;
4007 char *path;
4008 PyObject *argv, *env;
4009 char **argvlist;
4010 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004011 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004012
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 /* execve has three arguments: (path, argv, env), where
4014 argv is a list or tuple of strings and env is a dictionary
4015 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004016
Victor Stinner8c62be82010-05-06 00:08:46 +00004017 if (!PyArg_ParseTuple(args, "O&OO:execve",
4018 PyUnicode_FSConverter,
4019 &opath, &argv, &env))
4020 return NULL;
4021 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004022 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004023 PyErr_SetString(PyExc_TypeError,
4024 "execve() arg 2 must be a tuple or list");
4025 goto fail_0;
4026 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004027 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004028 if (!PyMapping_Check(env)) {
4029 PyErr_SetString(PyExc_TypeError,
4030 "execve() arg 3 must be a mapping object");
4031 goto fail_0;
4032 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004033
Ross Lagerwall7807c352011-03-17 20:20:30 +02004034 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004036 goto fail_0;
4037 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004038
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 envlist = parse_envlist(env, &envc);
4040 if (envlist == NULL)
4041 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004042
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004044
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004046
Victor Stinner8c62be82010-05-06 00:08:46 +00004047 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004048
Victor Stinner8c62be82010-05-06 00:08:46 +00004049 while (--envc >= 0)
4050 PyMem_DEL(envlist[envc]);
4051 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004052 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004053 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004054 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004055 Py_DECREF(opath);
4056 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004057}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004058#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004059
Ross Lagerwall7807c352011-03-17 20:20:30 +02004060#ifdef HAVE_FEXECVE
4061PyDoc_STRVAR(posix_fexecve__doc__,
4062"fexecve(fd, args, env)\n\n\
4063Execute the program specified by a file descriptor with arguments and\n\
4064environment, replacing the current process.\n\
4065\n\
4066 fd: file descriptor of executable\n\
4067 args: tuple or list of arguments\n\
4068 env: dictionary of strings mapping to strings");
4069
4070static PyObject *
4071posix_fexecve(PyObject *self, PyObject *args)
4072{
4073 int fd;
4074 PyObject *argv, *env;
4075 char **argvlist;
4076 char **envlist;
4077 Py_ssize_t argc, envc;
4078
4079 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4080 &fd, &argv, &env))
4081 return NULL;
4082 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4083 PyErr_SetString(PyExc_TypeError,
4084 "fexecve() arg 2 must be a tuple or list");
4085 return NULL;
4086 }
4087 argc = PySequence_Size(argv);
4088 if (!PyMapping_Check(env)) {
4089 PyErr_SetString(PyExc_TypeError,
4090 "fexecve() arg 3 must be a mapping object");
4091 return NULL;
4092 }
4093
4094 argvlist = parse_arglist(argv, &argc);
4095 if (argvlist == NULL)
4096 return NULL;
4097
4098 envlist = parse_envlist(env, &envc);
4099 if (envlist == NULL)
4100 goto fail;
4101
4102 fexecve(fd, argvlist, envlist);
4103
4104 /* If we get here it's definitely an error */
4105
4106 (void) posix_error();
4107
4108 while (--envc >= 0)
4109 PyMem_DEL(envlist[envc]);
4110 PyMem_DEL(envlist);
4111 fail:
4112 free_string_array(argvlist, argc);
4113 return NULL;
4114}
4115#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004116
Guido van Rossuma1065681999-01-25 23:20:23 +00004117#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004118PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004119"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004120Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004121\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004122 mode: mode of process creation\n\
4123 path: path of executable file\n\
4124 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004125
4126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004127posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004128{
Victor Stinner8c62be82010-05-06 00:08:46 +00004129 PyObject *opath;
4130 char *path;
4131 PyObject *argv;
4132 char **argvlist;
4133 int mode, i;
4134 Py_ssize_t argc;
4135 Py_intptr_t spawnval;
4136 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004137
Victor Stinner8c62be82010-05-06 00:08:46 +00004138 /* spawnv has three arguments: (mode, path, argv), where
4139 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004140
Victor Stinner8c62be82010-05-06 00:08:46 +00004141 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4142 PyUnicode_FSConverter,
4143 &opath, &argv))
4144 return NULL;
4145 path = PyBytes_AsString(opath);
4146 if (PyList_Check(argv)) {
4147 argc = PyList_Size(argv);
4148 getitem = PyList_GetItem;
4149 }
4150 else if (PyTuple_Check(argv)) {
4151 argc = PyTuple_Size(argv);
4152 getitem = PyTuple_GetItem;
4153 }
4154 else {
4155 PyErr_SetString(PyExc_TypeError,
4156 "spawnv() arg 2 must be a tuple or list");
4157 Py_DECREF(opath);
4158 return NULL;
4159 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004160
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 argvlist = PyMem_NEW(char *, argc+1);
4162 if (argvlist == NULL) {
4163 Py_DECREF(opath);
4164 return PyErr_NoMemory();
4165 }
4166 for (i = 0; i < argc; i++) {
4167 if (!fsconvert_strdup((*getitem)(argv, i),
4168 &argvlist[i])) {
4169 free_string_array(argvlist, i);
4170 PyErr_SetString(
4171 PyExc_TypeError,
4172 "spawnv() arg 2 must contain only strings");
4173 Py_DECREF(opath);
4174 return NULL;
4175 }
4176 }
4177 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004178
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004179#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 Py_BEGIN_ALLOW_THREADS
4181 spawnval = spawnv(mode, path, argvlist);
4182 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004183#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004184 if (mode == _OLD_P_OVERLAY)
4185 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004186
Victor Stinner8c62be82010-05-06 00:08:46 +00004187 Py_BEGIN_ALLOW_THREADS
4188 spawnval = _spawnv(mode, path, argvlist);
4189 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004190#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004191
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 free_string_array(argvlist, argc);
4193 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004194
Victor Stinner8c62be82010-05-06 00:08:46 +00004195 if (spawnval == -1)
4196 return posix_error();
4197 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004198#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004199 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004200#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004201 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004202#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004203}
4204
4205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004206PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004207"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004208Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004209\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004210 mode: mode of process creation\n\
4211 path: path of executable file\n\
4212 args: tuple or list of arguments\n\
4213 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004214
4215static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004216posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004217{
Victor Stinner8c62be82010-05-06 00:08:46 +00004218 PyObject *opath;
4219 char *path;
4220 PyObject *argv, *env;
4221 char **argvlist;
4222 char **envlist;
4223 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004224 int mode;
4225 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 Py_intptr_t spawnval;
4227 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4228 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004229
Victor Stinner8c62be82010-05-06 00:08:46 +00004230 /* spawnve has four arguments: (mode, path, argv, env), where
4231 argv is a list or tuple of strings and env is a dictionary
4232 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004233
Victor Stinner8c62be82010-05-06 00:08:46 +00004234 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4235 PyUnicode_FSConverter,
4236 &opath, &argv, &env))
4237 return NULL;
4238 path = PyBytes_AsString(opath);
4239 if (PyList_Check(argv)) {
4240 argc = PyList_Size(argv);
4241 getitem = PyList_GetItem;
4242 }
4243 else if (PyTuple_Check(argv)) {
4244 argc = PyTuple_Size(argv);
4245 getitem = PyTuple_GetItem;
4246 }
4247 else {
4248 PyErr_SetString(PyExc_TypeError,
4249 "spawnve() arg 2 must be a tuple or list");
4250 goto fail_0;
4251 }
4252 if (!PyMapping_Check(env)) {
4253 PyErr_SetString(PyExc_TypeError,
4254 "spawnve() arg 3 must be a mapping object");
4255 goto fail_0;
4256 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004257
Victor Stinner8c62be82010-05-06 00:08:46 +00004258 argvlist = PyMem_NEW(char *, argc+1);
4259 if (argvlist == NULL) {
4260 PyErr_NoMemory();
4261 goto fail_0;
4262 }
4263 for (i = 0; i < argc; i++) {
4264 if (!fsconvert_strdup((*getitem)(argv, i),
4265 &argvlist[i]))
4266 {
4267 lastarg = i;
4268 goto fail_1;
4269 }
4270 }
4271 lastarg = argc;
4272 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004273
Victor Stinner8c62be82010-05-06 00:08:46 +00004274 envlist = parse_envlist(env, &envc);
4275 if (envlist == NULL)
4276 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004277
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004278#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004279 Py_BEGIN_ALLOW_THREADS
4280 spawnval = spawnve(mode, path, argvlist, envlist);
4281 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004282#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004283 if (mode == _OLD_P_OVERLAY)
4284 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004285
Victor Stinner8c62be82010-05-06 00:08:46 +00004286 Py_BEGIN_ALLOW_THREADS
4287 spawnval = _spawnve(mode, path, argvlist, envlist);
4288 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004289#endif
Tim Peters25059d32001-12-07 20:35:43 +00004290
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 if (spawnval == -1)
4292 (void) posix_error();
4293 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004294#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004295 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004296#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004297 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004298#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004299
Victor Stinner8c62be82010-05-06 00:08:46 +00004300 while (--envc >= 0)
4301 PyMem_DEL(envlist[envc]);
4302 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004303 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004304 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004305 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004306 Py_DECREF(opath);
4307 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004308}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004309
4310/* OS/2 supports spawnvp & spawnvpe natively */
4311#if defined(PYOS_OS2)
4312PyDoc_STRVAR(posix_spawnvp__doc__,
4313"spawnvp(mode, file, args)\n\n\
4314Execute the program 'file' in a new process, using the environment\n\
4315search path to find the file.\n\
4316\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004317 mode: mode of process creation\n\
4318 file: executable file name\n\
4319 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004320
4321static PyObject *
4322posix_spawnvp(PyObject *self, PyObject *args)
4323{
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 PyObject *opath;
4325 char *path;
4326 PyObject *argv;
4327 char **argvlist;
4328 int mode, i, argc;
4329 Py_intptr_t spawnval;
4330 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004331
Victor Stinner8c62be82010-05-06 00:08:46 +00004332 /* spawnvp has three arguments: (mode, path, argv), where
4333 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004334
Victor Stinner8c62be82010-05-06 00:08:46 +00004335 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4336 PyUnicode_FSConverter,
4337 &opath, &argv))
4338 return NULL;
4339 path = PyBytes_AsString(opath);
4340 if (PyList_Check(argv)) {
4341 argc = PyList_Size(argv);
4342 getitem = PyList_GetItem;
4343 }
4344 else if (PyTuple_Check(argv)) {
4345 argc = PyTuple_Size(argv);
4346 getitem = PyTuple_GetItem;
4347 }
4348 else {
4349 PyErr_SetString(PyExc_TypeError,
4350 "spawnvp() arg 2 must be a tuple or list");
4351 Py_DECREF(opath);
4352 return NULL;
4353 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004354
Victor Stinner8c62be82010-05-06 00:08:46 +00004355 argvlist = PyMem_NEW(char *, argc+1);
4356 if (argvlist == NULL) {
4357 Py_DECREF(opath);
4358 return PyErr_NoMemory();
4359 }
4360 for (i = 0; i < argc; i++) {
4361 if (!fsconvert_strdup((*getitem)(argv, i),
4362 &argvlist[i])) {
4363 free_string_array(argvlist, i);
4364 PyErr_SetString(
4365 PyExc_TypeError,
4366 "spawnvp() arg 2 must contain only strings");
4367 Py_DECREF(opath);
4368 return NULL;
4369 }
4370 }
4371 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004372
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004376#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004378#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004379 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004380
Victor Stinner8c62be82010-05-06 00:08:46 +00004381 free_string_array(argvlist, argc);
4382 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004383
Victor Stinner8c62be82010-05-06 00:08:46 +00004384 if (spawnval == -1)
4385 return posix_error();
4386 else
4387 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004388}
4389
4390
4391PyDoc_STRVAR(posix_spawnvpe__doc__,
4392"spawnvpe(mode, file, args, env)\n\n\
4393Execute the program 'file' in a new process, using the environment\n\
4394search path to find the file.\n\
4395\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004396 mode: mode of process creation\n\
4397 file: executable file name\n\
4398 args: tuple or list of arguments\n\
4399 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004400
4401static PyObject *
4402posix_spawnvpe(PyObject *self, PyObject *args)
4403{
Victor Stinner8c62be82010-05-06 00:08:46 +00004404 PyObject *opath
4405 char *path;
4406 PyObject *argv, *env;
4407 char **argvlist;
4408 char **envlist;
4409 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004410 int mode;
4411 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004412 Py_intptr_t spawnval;
4413 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4414 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004415
Victor Stinner8c62be82010-05-06 00:08:46 +00004416 /* spawnvpe has four arguments: (mode, path, argv, env), where
4417 argv is a list or tuple of strings and env is a dictionary
4418 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004419
Victor Stinner8c62be82010-05-06 00:08:46 +00004420 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4421 PyUnicode_FSConverter,
4422 &opath, &argv, &env))
4423 return NULL;
4424 path = PyBytes_AsString(opath);
4425 if (PyList_Check(argv)) {
4426 argc = PyList_Size(argv);
4427 getitem = PyList_GetItem;
4428 }
4429 else if (PyTuple_Check(argv)) {
4430 argc = PyTuple_Size(argv);
4431 getitem = PyTuple_GetItem;
4432 }
4433 else {
4434 PyErr_SetString(PyExc_TypeError,
4435 "spawnvpe() arg 2 must be a tuple or list");
4436 goto fail_0;
4437 }
4438 if (!PyMapping_Check(env)) {
4439 PyErr_SetString(PyExc_TypeError,
4440 "spawnvpe() arg 3 must be a mapping object");
4441 goto fail_0;
4442 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004443
Victor Stinner8c62be82010-05-06 00:08:46 +00004444 argvlist = PyMem_NEW(char *, argc+1);
4445 if (argvlist == NULL) {
4446 PyErr_NoMemory();
4447 goto fail_0;
4448 }
4449 for (i = 0; i < argc; i++) {
4450 if (!fsconvert_strdup((*getitem)(argv, i),
4451 &argvlist[i]))
4452 {
4453 lastarg = i;
4454 goto fail_1;
4455 }
4456 }
4457 lastarg = argc;
4458 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004459
Victor Stinner8c62be82010-05-06 00:08:46 +00004460 envlist = parse_envlist(env, &envc);
4461 if (envlist == NULL)
4462 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004463
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004465#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004469#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004470 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004471
Victor Stinner8c62be82010-05-06 00:08:46 +00004472 if (spawnval == -1)
4473 (void) posix_error();
4474 else
4475 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004476
Victor Stinner8c62be82010-05-06 00:08:46 +00004477 while (--envc >= 0)
4478 PyMem_DEL(envlist[envc]);
4479 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004480 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004482 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004483 Py_DECREF(opath);
4484 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004485}
4486#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004487#endif /* HAVE_SPAWNV */
4488
4489
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004490#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004491PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004492"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004493Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4494\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004495Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004496
4497static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004498posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004499{
Victor Stinner8c62be82010-05-06 00:08:46 +00004500 pid_t pid;
4501 int result = 0;
4502 _PyImport_AcquireLock();
4503 pid = fork1();
4504 if (pid == 0) {
4505 /* child: this clobbers and resets the import lock. */
4506 PyOS_AfterFork();
4507 } else {
4508 /* parent: release the import lock. */
4509 result = _PyImport_ReleaseLock();
4510 }
4511 if (pid == -1)
4512 return posix_error();
4513 if (result < 0) {
4514 /* Don't clobber the OSError if the fork failed. */
4515 PyErr_SetString(PyExc_RuntimeError,
4516 "not holding the import lock");
4517 return NULL;
4518 }
4519 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004520}
4521#endif
4522
4523
Guido van Rossumad0ee831995-03-01 10:34:45 +00004524#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004525PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004526"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004527Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004528Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004529
Barry Warsaw53699e91996-12-10 23:23:01 +00004530static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004531posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004532{
Victor Stinner8c62be82010-05-06 00:08:46 +00004533 pid_t pid;
4534 int result = 0;
4535 _PyImport_AcquireLock();
4536 pid = fork();
4537 if (pid == 0) {
4538 /* child: this clobbers and resets the import lock. */
4539 PyOS_AfterFork();
4540 } else {
4541 /* parent: release the import lock. */
4542 result = _PyImport_ReleaseLock();
4543 }
4544 if (pid == -1)
4545 return posix_error();
4546 if (result < 0) {
4547 /* Don't clobber the OSError if the fork failed. */
4548 PyErr_SetString(PyExc_RuntimeError,
4549 "not holding the import lock");
4550 return NULL;
4551 }
4552 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004553}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004554#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004555
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004556#ifdef HAVE_SCHED_H
4557
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004558#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4559
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004560PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4561"sched_get_priority_max(policy)\n\n\
4562Get the maximum scheduling priority for *policy*.");
4563
4564static PyObject *
4565posix_sched_get_priority_max(PyObject *self, PyObject *args)
4566{
4567 int policy, max;
4568
4569 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4570 return NULL;
4571 max = sched_get_priority_max(policy);
4572 if (max < 0)
4573 return posix_error();
4574 return PyLong_FromLong(max);
4575}
4576
4577PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4578"sched_get_priority_min(policy)\n\n\
4579Get the minimum scheduling priority for *policy*.");
4580
4581static PyObject *
4582posix_sched_get_priority_min(PyObject *self, PyObject *args)
4583{
4584 int policy, min;
4585
4586 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4587 return NULL;
4588 min = sched_get_priority_min(policy);
4589 if (min < 0)
4590 return posix_error();
4591 return PyLong_FromLong(min);
4592}
4593
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004594#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4595
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004596#ifdef HAVE_SCHED_SETSCHEDULER
4597
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004598PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4599"sched_getscheduler(pid)\n\n\
4600Get the scheduling policy for the process with a PID of *pid*.\n\
4601Passing a PID of 0 returns the scheduling policy for the calling process.");
4602
4603static PyObject *
4604posix_sched_getscheduler(PyObject *self, PyObject *args)
4605{
4606 pid_t pid;
4607 int policy;
4608
4609 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4610 return NULL;
4611 policy = sched_getscheduler(pid);
4612 if (policy < 0)
4613 return posix_error();
4614 return PyLong_FromLong(policy);
4615}
4616
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004617#endif
4618
4619#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4620
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004621static PyObject *
4622sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4623{
4624 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004625 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004626
4627 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4628 return NULL;
4629 res = PyStructSequence_New(type);
4630 if (!res)
4631 return NULL;
4632 Py_INCREF(priority);
4633 PyStructSequence_SET_ITEM(res, 0, priority);
4634 return res;
4635}
4636
4637PyDoc_STRVAR(sched_param__doc__,
4638"sched_param(sched_priority): A scheduling parameter.\n\n\
4639Current has only one field: sched_priority");
4640
4641static PyStructSequence_Field sched_param_fields[] = {
4642 {"sched_priority", "the scheduling priority"},
4643 {0}
4644};
4645
4646static PyStructSequence_Desc sched_param_desc = {
4647 "sched_param", /* name */
4648 sched_param__doc__, /* doc */
4649 sched_param_fields,
4650 1
4651};
4652
4653static int
4654convert_sched_param(PyObject *param, struct sched_param *res)
4655{
4656 long priority;
4657
4658 if (Py_TYPE(param) != &SchedParamType) {
4659 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4660 return 0;
4661 }
4662 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4663 if (priority == -1 && PyErr_Occurred())
4664 return 0;
4665 if (priority > INT_MAX || priority < INT_MIN) {
4666 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4667 return 0;
4668 }
4669 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4670 return 1;
4671}
4672
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004673#endif
4674
4675#ifdef HAVE_SCHED_SETSCHEDULER
4676
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004677PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4678"sched_setscheduler(pid, policy, param)\n\n\
4679Set the scheduling policy, *policy*, for *pid*.\n\
4680If *pid* is 0, the calling process is changed.\n\
4681*param* is an instance of sched_param.");
4682
4683static PyObject *
4684posix_sched_setscheduler(PyObject *self, PyObject *args)
4685{
4686 pid_t pid;
4687 int policy;
4688 struct sched_param param;
4689
4690 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4691 &pid, &policy, &convert_sched_param, &param))
4692 return NULL;
4693 if (sched_setscheduler(pid, policy, &param))
4694 return posix_error();
4695 Py_RETURN_NONE;
4696}
4697
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004698#endif
4699
4700#ifdef HAVE_SCHED_SETPARAM
4701
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004702PyDoc_STRVAR(posix_sched_getparam__doc__,
4703"sched_getparam(pid) -> sched_param\n\n\
4704Returns scheduling parameters for the process with *pid* as an instance of the\n\
4705sched_param class. A PID of 0 means the calling process.");
4706
4707static PyObject *
4708posix_sched_getparam(PyObject *self, PyObject *args)
4709{
4710 pid_t pid;
4711 struct sched_param param;
4712 PyObject *res, *priority;
4713
4714 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4715 return NULL;
4716 if (sched_getparam(pid, &param))
4717 return posix_error();
4718 res = PyStructSequence_New(&SchedParamType);
4719 if (!res)
4720 return NULL;
4721 priority = PyLong_FromLong(param.sched_priority);
4722 if (!priority) {
4723 Py_DECREF(res);
4724 return NULL;
4725 }
4726 PyStructSequence_SET_ITEM(res, 0, priority);
4727 return res;
4728}
4729
4730PyDoc_STRVAR(posix_sched_setparam__doc__,
4731"sched_setparam(pid, param)\n\n\
4732Set scheduling parameters for a process with PID *pid*.\n\
4733A PID of 0 means the calling process.");
4734
4735static PyObject *
4736posix_sched_setparam(PyObject *self, PyObject *args)
4737{
4738 pid_t pid;
4739 struct sched_param param;
4740
4741 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4742 &pid, &convert_sched_param, &param))
4743 return NULL;
4744 if (sched_setparam(pid, &param))
4745 return posix_error();
4746 Py_RETURN_NONE;
4747}
4748
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004749#endif
4750
4751#ifdef HAVE_SCHED_RR_GET_INTERVAL
4752
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004753PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4754"sched_rr_get_interval(pid) -> float\n\n\
4755Return the round-robin quantum for the process with PID *pid* in seconds.");
4756
4757static PyObject *
4758posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4759{
4760 pid_t pid;
4761 struct timespec interval;
4762
4763 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4764 return NULL;
4765 if (sched_rr_get_interval(pid, &interval))
4766 return posix_error();
4767 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4768}
4769
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004770#endif
4771
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004772PyDoc_STRVAR(posix_sched_yield__doc__,
4773"sched_yield()\n\n\
4774Voluntarily relinquish the CPU.");
4775
4776static PyObject *
4777posix_sched_yield(PyObject *self, PyObject *noargs)
4778{
4779 if (sched_yield())
4780 return posix_error();
4781 Py_RETURN_NONE;
4782}
4783
Benjamin Peterson2740af82011-08-02 17:41:34 -05004784#ifdef HAVE_SCHED_SETAFFINITY
4785
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004786typedef struct {
4787 PyObject_HEAD;
4788 Py_ssize_t size;
4789 int ncpus;
4790 cpu_set_t *set;
4791} Py_cpu_set;
4792
4793static PyTypeObject cpu_set_type;
4794
4795static void
4796cpu_set_dealloc(Py_cpu_set *set)
4797{
4798 assert(set->set);
4799 CPU_FREE(set->set);
4800 Py_TYPE(set)->tp_free(set);
4801}
4802
4803static Py_cpu_set *
4804make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4805{
4806 Py_cpu_set *set;
4807
4808 if (size < 0) {
4809 PyErr_SetString(PyExc_ValueError, "negative size");
4810 return NULL;
4811 }
4812 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4813 if (!set)
4814 return NULL;
4815 set->ncpus = size;
4816 set->size = CPU_ALLOC_SIZE(size);
4817 set->set = CPU_ALLOC(size);
4818 if (!set->set) {
4819 type->tp_free(set);
4820 PyErr_NoMemory();
4821 return NULL;
4822 }
4823 CPU_ZERO_S(set->size, set->set);
4824 return set;
4825}
4826
4827static PyObject *
4828cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4829{
4830 int size;
4831
4832 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4833 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4834 return NULL;
4835 return (PyObject *)make_new_cpu_set(type, size);
4836}
4837
4838static PyObject *
4839cpu_set_repr(Py_cpu_set *set)
4840{
4841 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4842}
4843
4844static Py_ssize_t
4845cpu_set_len(Py_cpu_set *set)
4846{
4847 return set->ncpus;
4848}
4849
4850static int
4851_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4852{
4853 int cpu;
4854 if (!PyArg_ParseTuple(args, requester, &cpu))
4855 return -1;
4856 if (cpu < 0) {
4857 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4858 return -1;
4859 }
4860 if (cpu >= set->ncpus) {
4861 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4862 return -1;
4863 }
4864 return cpu;
4865}
4866
4867PyDoc_STRVAR(cpu_set_set_doc,
4868"cpu_set.set(i)\n\n\
4869Add CPU *i* to the set.");
4870
4871static PyObject *
4872cpu_set_set(Py_cpu_set *set, PyObject *args)
4873{
4874 int cpu = _get_cpu(set, "i|set", args);
4875 if (cpu == -1)
4876 return NULL;
4877 CPU_SET_S(cpu, set->size, set->set);
4878 Py_RETURN_NONE;
4879}
4880
4881PyDoc_STRVAR(cpu_set_count_doc,
4882"cpu_set.count() -> int\n\n\
4883Return the number of CPUs active in the set.");
4884
4885static PyObject *
4886cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4887{
4888 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
4889}
4890
4891PyDoc_STRVAR(cpu_set_clear_doc,
4892"cpu_set.clear(i)\n\n\
4893Remove CPU *i* from the set.");
4894
4895static PyObject *
4896cpu_set_clear(Py_cpu_set *set, PyObject *args)
4897{
4898 int cpu = _get_cpu(set, "i|clear", args);
4899 if (cpu == -1)
4900 return NULL;
4901 CPU_CLR_S(cpu, set->size, set->set);
4902 Py_RETURN_NONE;
4903}
4904
4905PyDoc_STRVAR(cpu_set_isset_doc,
4906"cpu_set.isset(i) -> bool\n\n\
4907Test if CPU *i* is in the set.");
4908
4909static PyObject *
4910cpu_set_isset(Py_cpu_set *set, PyObject *args)
4911{
4912 int cpu = _get_cpu(set, "i|isset", args);
4913 if (cpu == -1)
4914 return NULL;
4915 if (CPU_ISSET_S(cpu, set->size, set->set))
4916 Py_RETURN_TRUE;
4917 Py_RETURN_FALSE;
4918}
4919
4920PyDoc_STRVAR(cpu_set_zero_doc,
4921"cpu_set.zero()\n\n\
4922Clear the cpu_set.");
4923
4924static PyObject *
4925cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
4926{
4927 CPU_ZERO_S(set->size, set->set);
4928 Py_RETURN_NONE;
4929}
4930
4931static PyObject *
4932cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
4933{
4934 int eq;
4935
Brian Curtindfc80e32011-08-10 20:28:54 -05004936 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
4937 Py_RETURN_NOTIMPLEMENTED;
4938
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004939 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
4940 if ((op == Py_EQ) ? eq : !eq)
4941 Py_RETURN_TRUE;
4942 else
4943 Py_RETURN_FALSE;
4944}
4945
4946#define CPU_SET_BINOP(name, op) \
4947 static PyObject * \
4948 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
4949 if (res) { \
4950 Py_INCREF(res); \
4951 } \
4952 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05004953 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004954 if (!res) \
4955 return NULL; \
4956 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05004957 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004958 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05004959 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004960 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05004961 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004962 op(res->size, res->set, left->set, right->set); \
4963 return (PyObject *)res; \
4964 } \
4965 static PyObject * \
4966 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
4967 return do_cpu_set_##name(left, right, NULL); \
4968 } \
4969 static PyObject * \
4970 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
4971 return do_cpu_set_##name(left, right, left); \
4972 } \
4973
4974CPU_SET_BINOP(and, CPU_AND_S)
4975CPU_SET_BINOP(or, CPU_OR_S)
4976CPU_SET_BINOP(xor, CPU_XOR_S)
4977#undef CPU_SET_BINOP
4978
4979PyDoc_STRVAR(cpu_set_doc,
4980"cpu_set(size)\n\n\
4981Create an empty mask of CPUs.");
4982
4983static PyNumberMethods cpu_set_as_number = {
4984 0, /*nb_add*/
4985 0, /*nb_subtract*/
4986 0, /*nb_multiply*/
4987 0, /*nb_remainder*/
4988 0, /*nb_divmod*/
4989 0, /*nb_power*/
4990 0, /*nb_negative*/
4991 0, /*nb_positive*/
4992 0, /*nb_absolute*/
4993 0, /*nb_bool*/
4994 0, /*nb_invert*/
4995 0, /*nb_lshift*/
4996 0, /*nb_rshift*/
4997 (binaryfunc)cpu_set_and, /*nb_and*/
4998 (binaryfunc)cpu_set_xor, /*nb_xor*/
4999 (binaryfunc)cpu_set_or, /*nb_or*/
5000 0, /*nb_int*/
5001 0, /*nb_reserved*/
5002 0, /*nb_float*/
5003 0, /*nb_inplace_add*/
5004 0, /*nb_inplace_subtract*/
5005 0, /*nb_inplace_multiply*/
5006 0, /*nb_inplace_remainder*/
5007 0, /*nb_inplace_power*/
5008 0, /*nb_inplace_lshift*/
5009 0, /*nb_inplace_rshift*/
5010 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5011 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5012 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5013};
5014
5015static PySequenceMethods cpu_set_as_sequence = {
5016 (lenfunc)cpu_set_len, /* sq_length */
5017};
5018
5019static PyMethodDef cpu_set_methods[] = {
5020 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5021 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5022 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5023 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5024 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5025 {NULL, NULL} /* sentinel */
5026};
5027
5028static PyTypeObject cpu_set_type = {
5029 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5030 "posix.cpu_set", /* tp_name */
5031 sizeof(Py_cpu_set), /* tp_basicsize */
5032 0, /* tp_itemsize */
5033 /* methods */
5034 (destructor)cpu_set_dealloc, /* tp_dealloc */
5035 0, /* tp_print */
5036 0, /* tp_getattr */
5037 0, /* tp_setattr */
5038 0, /* tp_reserved */
5039 (reprfunc)cpu_set_repr, /* tp_repr */
5040 &cpu_set_as_number, /* tp_as_number */
5041 &cpu_set_as_sequence, /* tp_as_sequence */
5042 0, /* tp_as_mapping */
5043 PyObject_HashNotImplemented, /* tp_hash */
5044 0, /* tp_call */
5045 0, /* tp_str */
5046 PyObject_GenericGetAttr, /* tp_getattro */
5047 0, /* tp_setattro */
5048 0, /* tp_as_buffer */
5049 Py_TPFLAGS_DEFAULT, /* tp_flags */
5050 cpu_set_doc, /* tp_doc */
5051 0, /* tp_traverse */
5052 0, /* tp_clear */
5053 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5054 0, /* tp_weaklistoffset */
5055 0, /* tp_iter */
5056 0, /* tp_iternext */
5057 cpu_set_methods, /* tp_methods */
5058 0, /* tp_members */
5059 0, /* tp_getset */
5060 0, /* tp_base */
5061 0, /* tp_dict */
5062 0, /* tp_descr_get */
5063 0, /* tp_descr_set */
5064 0, /* tp_dictoffset */
5065 0, /* tp_init */
5066 PyType_GenericAlloc, /* tp_alloc */
5067 cpu_set_new, /* tp_new */
5068 PyObject_Del, /* tp_free */
5069};
5070
5071PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5072"sched_setaffinity(pid, cpu_set)\n\n\
5073Set the affinity of the process with PID *pid* to *cpu_set*.");
5074
5075static PyObject *
5076posix_sched_setaffinity(PyObject *self, PyObject *args)
5077{
5078 pid_t pid;
5079 Py_cpu_set *cpu_set;
5080
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005081 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005082 &pid, &cpu_set_type, &cpu_set))
5083 return NULL;
5084 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5085 return posix_error();
5086 Py_RETURN_NONE;
5087}
5088
5089PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5090"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5091Return the affinity of the process with PID *pid*.\n\
5092The returned cpu_set will be of size *ncpus*.");
5093
5094static PyObject *
5095posix_sched_getaffinity(PyObject *self, PyObject *args)
5096{
5097 pid_t pid;
5098 int ncpus;
5099 Py_cpu_set *res;
5100
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005101 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005102 &pid, &ncpus))
5103 return NULL;
5104 res = make_new_cpu_set(&cpu_set_type, ncpus);
5105 if (!res)
5106 return NULL;
5107 if (sched_getaffinity(pid, res->size, res->set)) {
5108 Py_DECREF(res);
5109 return posix_error();
5110 }
5111 return (PyObject *)res;
5112}
5113
Benjamin Peterson2740af82011-08-02 17:41:34 -05005114#endif /* HAVE_SCHED_SETAFFINITY */
5115
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005116#endif /* HAVE_SCHED_H */
5117
Neal Norwitzb59798b2003-03-21 01:43:31 +00005118/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005119/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5120#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005121#define DEV_PTY_FILE "/dev/ptc"
5122#define HAVE_DEV_PTMX
5123#else
5124#define DEV_PTY_FILE "/dev/ptmx"
5125#endif
5126
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005127#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005128#ifdef HAVE_PTY_H
5129#include <pty.h>
5130#else
5131#ifdef HAVE_LIBUTIL_H
5132#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005133#else
5134#ifdef HAVE_UTIL_H
5135#include <util.h>
5136#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005137#endif /* HAVE_LIBUTIL_H */
5138#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005139#ifdef HAVE_STROPTS_H
5140#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005141#endif
5142#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005143
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005144#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005145PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005146"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005147Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005148
5149static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005150posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005151{
Victor Stinner8c62be82010-05-06 00:08:46 +00005152 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005153#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005154 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005155#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005156#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005157 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005158#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005159 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005160#endif
5161#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005162
Thomas Wouters70c21a12000-07-14 14:28:33 +00005163#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005164 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5165 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005166#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005167 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5168 if (slave_name == NULL)
5169 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005170
Victor Stinner8c62be82010-05-06 00:08:46 +00005171 slave_fd = open(slave_name, O_RDWR);
5172 if (slave_fd < 0)
5173 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005174#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005175 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5176 if (master_fd < 0)
5177 return posix_error();
5178 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5179 /* change permission of slave */
5180 if (grantpt(master_fd) < 0) {
5181 PyOS_setsig(SIGCHLD, sig_saved);
5182 return posix_error();
5183 }
5184 /* unlock slave */
5185 if (unlockpt(master_fd) < 0) {
5186 PyOS_setsig(SIGCHLD, sig_saved);
5187 return posix_error();
5188 }
5189 PyOS_setsig(SIGCHLD, sig_saved);
5190 slave_name = ptsname(master_fd); /* get name of slave */
5191 if (slave_name == NULL)
5192 return posix_error();
5193 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5194 if (slave_fd < 0)
5195 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005196#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005197 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5198 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005199#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005200 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005201#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005202#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005203#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005204
Victor Stinner8c62be82010-05-06 00:08:46 +00005205 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005206
Fred Drake8cef4cf2000-06-28 16:40:38 +00005207}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005208#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005209
5210#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005211PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005212"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005213Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5214Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005215To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005216
5217static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005218posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005219{
Victor Stinner8c62be82010-05-06 00:08:46 +00005220 int master_fd = -1, result = 0;
5221 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005222
Victor Stinner8c62be82010-05-06 00:08:46 +00005223 _PyImport_AcquireLock();
5224 pid = forkpty(&master_fd, NULL, NULL, NULL);
5225 if (pid == 0) {
5226 /* child: this clobbers and resets the import lock. */
5227 PyOS_AfterFork();
5228 } else {
5229 /* parent: release the import lock. */
5230 result = _PyImport_ReleaseLock();
5231 }
5232 if (pid == -1)
5233 return posix_error();
5234 if (result < 0) {
5235 /* Don't clobber the OSError if the fork failed. */
5236 PyErr_SetString(PyExc_RuntimeError,
5237 "not holding the import lock");
5238 return NULL;
5239 }
5240 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005241}
5242#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005243
Ross Lagerwall7807c352011-03-17 20:20:30 +02005244
Guido van Rossumad0ee831995-03-01 10:34:45 +00005245#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005246PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005247"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005248Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005249
Barry Warsaw53699e91996-12-10 23:23:01 +00005250static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005251posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005252{
Victor Stinner8c62be82010-05-06 00:08:46 +00005253 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005254}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005255#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005257
Guido van Rossumad0ee831995-03-01 10:34:45 +00005258#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005259PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005260"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005261Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005262
Barry Warsaw53699e91996-12-10 23:23:01 +00005263static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005264posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005265{
Victor Stinner8c62be82010-05-06 00:08:46 +00005266 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005267}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005268#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005269
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005270
Guido van Rossumad0ee831995-03-01 10:34:45 +00005271#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005273"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005274Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005275
Barry Warsaw53699e91996-12-10 23:23:01 +00005276static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005277posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005278{
Victor Stinner8c62be82010-05-06 00:08:46 +00005279 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005280}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005281#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005284PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005285"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005286Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005287
Barry Warsaw53699e91996-12-10 23:23:01 +00005288static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005289posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005290{
Victor Stinner8c62be82010-05-06 00:08:46 +00005291 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005292}
5293
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005294#ifdef HAVE_GETGROUPLIST
5295PyDoc_STRVAR(posix_getgrouplist__doc__,
5296"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5297Returns a list of groups to which a user belongs.\n\n\
5298 user: username to lookup\n\
5299 group: base group id of the user");
5300
5301static PyObject *
5302posix_getgrouplist(PyObject *self, PyObject *args)
5303{
5304#ifdef NGROUPS_MAX
5305#define MAX_GROUPS NGROUPS_MAX
5306#else
5307 /* defined to be 16 on Solaris7, so this should be a small number */
5308#define MAX_GROUPS 64
5309#endif
5310
5311 const char *user;
5312 int i, ngroups;
5313 PyObject *list;
5314#ifdef __APPLE__
5315 int *groups, basegid;
5316#else
5317 gid_t *groups, basegid;
5318#endif
5319 ngroups = MAX_GROUPS;
5320
5321 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5322 return NULL;
5323
5324#ifdef __APPLE__
5325 groups = PyMem_Malloc(ngroups * sizeof(int));
5326#else
5327 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5328#endif
5329 if (groups == NULL)
5330 return PyErr_NoMemory();
5331
5332 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5333 PyMem_Del(groups);
5334 return posix_error();
5335 }
5336
5337 list = PyList_New(ngroups);
5338 if (list == NULL) {
5339 PyMem_Del(groups);
5340 return NULL;
5341 }
5342
5343 for (i = 0; i < ngroups; i++) {
5344 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5345 if (o == NULL) {
5346 Py_DECREF(list);
5347 PyMem_Del(groups);
5348 return NULL;
5349 }
5350 PyList_SET_ITEM(list, i, o);
5351 }
5352
5353 PyMem_Del(groups);
5354
5355 return list;
5356}
5357#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005358
Fred Drakec9680921999-12-13 16:37:25 +00005359#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005360PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005361"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005362Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005363
5364static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005365posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005366{
5367 PyObject *result = NULL;
5368
Fred Drakec9680921999-12-13 16:37:25 +00005369#ifdef NGROUPS_MAX
5370#define MAX_GROUPS NGROUPS_MAX
5371#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005372 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005373#define MAX_GROUPS 64
5374#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005375 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005376
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005377 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005378 * This is a helper variable to store the intermediate result when
5379 * that happens.
5380 *
5381 * To keep the code readable the OSX behaviour is unconditional,
5382 * according to the POSIX spec this should be safe on all unix-y
5383 * systems.
5384 */
5385 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005387
Victor Stinner8c62be82010-05-06 00:08:46 +00005388 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005389 if (n < 0) {
5390 if (errno == EINVAL) {
5391 n = getgroups(0, NULL);
5392 if (n == -1) {
5393 return posix_error();
5394 }
5395 if (n == 0) {
5396 /* Avoid malloc(0) */
5397 alt_grouplist = grouplist;
5398 } else {
5399 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5400 if (alt_grouplist == NULL) {
5401 errno = EINVAL;
5402 return posix_error();
5403 }
5404 n = getgroups(n, alt_grouplist);
5405 if (n == -1) {
5406 PyMem_Free(alt_grouplist);
5407 return posix_error();
5408 }
5409 }
5410 } else {
5411 return posix_error();
5412 }
5413 }
5414 result = PyList_New(n);
5415 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005416 int i;
5417 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005418 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005419 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005420 Py_DECREF(result);
5421 result = NULL;
5422 break;
Fred Drakec9680921999-12-13 16:37:25 +00005423 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005424 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005425 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005426 }
5427
5428 if (alt_grouplist != grouplist) {
5429 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005430 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005431
Fred Drakec9680921999-12-13 16:37:25 +00005432 return result;
5433}
5434#endif
5435
Antoine Pitroub7572f02009-12-02 20:46:48 +00005436#ifdef HAVE_INITGROUPS
5437PyDoc_STRVAR(posix_initgroups__doc__,
5438"initgroups(username, gid) -> None\n\n\
5439Call the system initgroups() to initialize the group access list with all of\n\
5440the groups of which the specified username is a member, plus the specified\n\
5441group id.");
5442
5443static PyObject *
5444posix_initgroups(PyObject *self, PyObject *args)
5445{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005446 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005447 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005448 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005449 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005450
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005451 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5452 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005453 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005454 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005455
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005456 res = initgroups(username, (gid_t) gid);
5457 Py_DECREF(oname);
5458 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005459 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005460
Victor Stinner8c62be82010-05-06 00:08:46 +00005461 Py_INCREF(Py_None);
5462 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005463}
5464#endif
5465
Martin v. Löwis606edc12002-06-13 21:09:11 +00005466#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005467PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005468"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005469Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005470
5471static PyObject *
5472posix_getpgid(PyObject *self, PyObject *args)
5473{
Victor Stinner8c62be82010-05-06 00:08:46 +00005474 pid_t pid, pgid;
5475 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5476 return NULL;
5477 pgid = getpgid(pid);
5478 if (pgid < 0)
5479 return posix_error();
5480 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005481}
5482#endif /* HAVE_GETPGID */
5483
5484
Guido van Rossumb6775db1994-08-01 11:34:53 +00005485#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005486PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005487"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005488Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005489
Barry Warsaw53699e91996-12-10 23:23:01 +00005490static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005491posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005492{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005493#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005494 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005495#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005496 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005497#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005498}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005499#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005500
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005501
Guido van Rossumb6775db1994-08-01 11:34:53 +00005502#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005503PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005504"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005505Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005506
Barry Warsaw53699e91996-12-10 23:23:01 +00005507static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005508posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005509{
Guido van Rossum64933891994-10-20 21:56:42 +00005510#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005512#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005513 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005514#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005515 return posix_error();
5516 Py_INCREF(Py_None);
5517 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005518}
5519
Guido van Rossumb6775db1994-08-01 11:34:53 +00005520#endif /* HAVE_SETPGRP */
5521
Guido van Rossumad0ee831995-03-01 10:34:45 +00005522#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005523
5524#ifdef MS_WINDOWS
5525#include <tlhelp32.h>
5526
5527static PyObject*
5528win32_getppid()
5529{
5530 HANDLE snapshot;
5531 pid_t mypid;
5532 PyObject* result = NULL;
5533 BOOL have_record;
5534 PROCESSENTRY32 pe;
5535
5536 mypid = getpid(); /* This function never fails */
5537
5538 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5539 if (snapshot == INVALID_HANDLE_VALUE)
5540 return PyErr_SetFromWindowsErr(GetLastError());
5541
5542 pe.dwSize = sizeof(pe);
5543 have_record = Process32First(snapshot, &pe);
5544 while (have_record) {
5545 if (mypid == (pid_t)pe.th32ProcessID) {
5546 /* We could cache the ulong value in a static variable. */
5547 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5548 break;
5549 }
5550
5551 have_record = Process32Next(snapshot, &pe);
5552 }
5553
5554 /* If our loop exits and our pid was not found (result will be NULL)
5555 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5556 * error anyway, so let's raise it. */
5557 if (!result)
5558 result = PyErr_SetFromWindowsErr(GetLastError());
5559
5560 CloseHandle(snapshot);
5561
5562 return result;
5563}
5564#endif /*MS_WINDOWS*/
5565
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005567"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005568Return the parent's process id. If the parent process has already exited,\n\
5569Windows machines will still return its id; others systems will return the id\n\
5570of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005571
Barry Warsaw53699e91996-12-10 23:23:01 +00005572static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005573posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005574{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005575#ifdef MS_WINDOWS
5576 return win32_getppid();
5577#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005578 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005579#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005580}
5581#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005582
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005583
Fred Drake12c6e2d1999-12-14 21:25:03 +00005584#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005585PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005586"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005587Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005588
5589static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005590posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005591{
Victor Stinner8c62be82010-05-06 00:08:46 +00005592 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005593#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005594 wchar_t user_name[UNLEN + 1];
5595 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5596
5597 if (GetUserNameW(user_name, &num_chars)) {
5598 /* num_chars is the number of unicode chars plus null terminator */
5599 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005600 }
5601 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005602 result = PyErr_SetFromWindowsErr(GetLastError());
5603#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005604 char *name;
5605 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005606
Victor Stinner8c62be82010-05-06 00:08:46 +00005607 errno = 0;
5608 name = getlogin();
5609 if (name == NULL) {
5610 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005611 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005612 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005613 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 }
5615 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005616 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005617 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005618#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005619 return result;
5620}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005621#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005622
Guido van Rossumad0ee831995-03-01 10:34:45 +00005623#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005624PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005625"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005626Return the current process's user 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_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005630{
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005632}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005633#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005634
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005635
Guido van Rossumad0ee831995-03-01 10:34:45 +00005636#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005637PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005638"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005639Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005640
Barry Warsaw53699e91996-12-10 23:23:01 +00005641static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005642posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005643{
Victor Stinner8c62be82010-05-06 00:08:46 +00005644 pid_t pid;
5645 int sig;
5646 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5647 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005648#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005649 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5650 APIRET rc;
5651 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005652 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005653
5654 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5655 APIRET rc;
5656 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005657 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005658
5659 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005660 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005661#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005662 if (kill(pid, sig) == -1)
5663 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005664#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005665 Py_INCREF(Py_None);
5666 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005667}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005668#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005669
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005670#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005671PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005672"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005673Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005674
5675static PyObject *
5676posix_killpg(PyObject *self, PyObject *args)
5677{
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 int sig;
5679 pid_t pgid;
5680 /* XXX some man pages make the `pgid` parameter an int, others
5681 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5682 take the same type. Moreover, pid_t is always at least as wide as
5683 int (else compilation of this module fails), which is safe. */
5684 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5685 return NULL;
5686 if (killpg(pgid, sig) == -1)
5687 return posix_error();
5688 Py_INCREF(Py_None);
5689 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005690}
5691#endif
5692
Brian Curtineb24d742010-04-12 17:16:38 +00005693#ifdef MS_WINDOWS
5694PyDoc_STRVAR(win32_kill__doc__,
5695"kill(pid, sig)\n\n\
5696Kill a process with a signal.");
5697
5698static PyObject *
5699win32_kill(PyObject *self, PyObject *args)
5700{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005701 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005702 DWORD pid, sig, err;
5703 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005704
Victor Stinner8c62be82010-05-06 00:08:46 +00005705 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5706 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005707
Victor Stinner8c62be82010-05-06 00:08:46 +00005708 /* Console processes which share a common console can be sent CTRL+C or
5709 CTRL+BREAK events, provided they handle said events. */
5710 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5711 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5712 err = GetLastError();
5713 PyErr_SetFromWindowsErr(err);
5714 }
5715 else
5716 Py_RETURN_NONE;
5717 }
Brian Curtineb24d742010-04-12 17:16:38 +00005718
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5720 attempt to open and terminate the process. */
5721 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5722 if (handle == NULL) {
5723 err = GetLastError();
5724 return PyErr_SetFromWindowsErr(err);
5725 }
Brian Curtineb24d742010-04-12 17:16:38 +00005726
Victor Stinner8c62be82010-05-06 00:08:46 +00005727 if (TerminateProcess(handle, sig) == 0) {
5728 err = GetLastError();
5729 result = PyErr_SetFromWindowsErr(err);
5730 } else {
5731 Py_INCREF(Py_None);
5732 result = Py_None;
5733 }
Brian Curtineb24d742010-04-12 17:16:38 +00005734
Victor Stinner8c62be82010-05-06 00:08:46 +00005735 CloseHandle(handle);
5736 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005737}
5738#endif /* MS_WINDOWS */
5739
Guido van Rossumc0125471996-06-28 18:55:32 +00005740#ifdef HAVE_PLOCK
5741
5742#ifdef HAVE_SYS_LOCK_H
5743#include <sys/lock.h>
5744#endif
5745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005746PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005747"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005748Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005749
Barry Warsaw53699e91996-12-10 23:23:01 +00005750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005751posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005752{
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 int op;
5754 if (!PyArg_ParseTuple(args, "i:plock", &op))
5755 return NULL;
5756 if (plock(op) == -1)
5757 return posix_error();
5758 Py_INCREF(Py_None);
5759 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005760}
5761#endif
5762
Guido van Rossumb6775db1994-08-01 11:34:53 +00005763#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005764PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005765"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005766Set the current process's user id.");
5767
Barry Warsaw53699e91996-12-10 23:23:01 +00005768static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005769posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005770{
Victor Stinner8c62be82010-05-06 00:08:46 +00005771 long uid_arg;
5772 uid_t uid;
5773 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5774 return NULL;
5775 uid = uid_arg;
5776 if (uid != uid_arg) {
5777 PyErr_SetString(PyExc_OverflowError, "user id too big");
5778 return NULL;
5779 }
5780 if (setuid(uid) < 0)
5781 return posix_error();
5782 Py_INCREF(Py_None);
5783 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005784}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005785#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005786
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005787
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005788#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005789PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005790"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005791Set the current process's effective user id.");
5792
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005793static PyObject *
5794posix_seteuid (PyObject *self, PyObject *args)
5795{
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 long euid_arg;
5797 uid_t euid;
5798 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5799 return NULL;
5800 euid = euid_arg;
5801 if (euid != euid_arg) {
5802 PyErr_SetString(PyExc_OverflowError, "user id too big");
5803 return NULL;
5804 }
5805 if (seteuid(euid) < 0) {
5806 return posix_error();
5807 } else {
5808 Py_INCREF(Py_None);
5809 return Py_None;
5810 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005811}
5812#endif /* HAVE_SETEUID */
5813
5814#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005816"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005817Set the current process's effective group id.");
5818
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005819static PyObject *
5820posix_setegid (PyObject *self, PyObject *args)
5821{
Victor Stinner8c62be82010-05-06 00:08:46 +00005822 long egid_arg;
5823 gid_t egid;
5824 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5825 return NULL;
5826 egid = egid_arg;
5827 if (egid != egid_arg) {
5828 PyErr_SetString(PyExc_OverflowError, "group id too big");
5829 return NULL;
5830 }
5831 if (setegid(egid) < 0) {
5832 return posix_error();
5833 } else {
5834 Py_INCREF(Py_None);
5835 return Py_None;
5836 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005837}
5838#endif /* HAVE_SETEGID */
5839
5840#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005841PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005842"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005843Set the current process's real and effective user ids.");
5844
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005845static PyObject *
5846posix_setreuid (PyObject *self, PyObject *args)
5847{
Victor Stinner8c62be82010-05-06 00:08:46 +00005848 long ruid_arg, euid_arg;
5849 uid_t ruid, euid;
5850 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5851 return NULL;
5852 if (ruid_arg == -1)
5853 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5854 else
5855 ruid = ruid_arg; /* otherwise, assign from our long */
5856 if (euid_arg == -1)
5857 euid = (uid_t)-1;
5858 else
5859 euid = euid_arg;
5860 if ((euid_arg != -1 && euid != euid_arg) ||
5861 (ruid_arg != -1 && ruid != ruid_arg)) {
5862 PyErr_SetString(PyExc_OverflowError, "user id too big");
5863 return NULL;
5864 }
5865 if (setreuid(ruid, euid) < 0) {
5866 return posix_error();
5867 } else {
5868 Py_INCREF(Py_None);
5869 return Py_None;
5870 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005871}
5872#endif /* HAVE_SETREUID */
5873
5874#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005875PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005876"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005877Set the current process's real and effective group ids.");
5878
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005879static PyObject *
5880posix_setregid (PyObject *self, PyObject *args)
5881{
Victor Stinner8c62be82010-05-06 00:08:46 +00005882 long rgid_arg, egid_arg;
5883 gid_t rgid, egid;
5884 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5885 return NULL;
5886 if (rgid_arg == -1)
5887 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5888 else
5889 rgid = rgid_arg; /* otherwise, assign from our long */
5890 if (egid_arg == -1)
5891 egid = (gid_t)-1;
5892 else
5893 egid = egid_arg;
5894 if ((egid_arg != -1 && egid != egid_arg) ||
5895 (rgid_arg != -1 && rgid != rgid_arg)) {
5896 PyErr_SetString(PyExc_OverflowError, "group id too big");
5897 return NULL;
5898 }
5899 if (setregid(rgid, egid) < 0) {
5900 return posix_error();
5901 } else {
5902 Py_INCREF(Py_None);
5903 return Py_None;
5904 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005905}
5906#endif /* HAVE_SETREGID */
5907
Guido van Rossumb6775db1994-08-01 11:34:53 +00005908#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005909PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005910"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005911Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005912
Barry Warsaw53699e91996-12-10 23:23:01 +00005913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005914posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005915{
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 long gid_arg;
5917 gid_t gid;
5918 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5919 return NULL;
5920 gid = gid_arg;
5921 if (gid != gid_arg) {
5922 PyErr_SetString(PyExc_OverflowError, "group id too big");
5923 return NULL;
5924 }
5925 if (setgid(gid) < 0)
5926 return posix_error();
5927 Py_INCREF(Py_None);
5928 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005929}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005930#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005931
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005932#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005933PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005934"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005935Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005936
5937static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005938posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005939{
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 int i, len;
5941 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005942
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 if (!PySequence_Check(groups)) {
5944 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5945 return NULL;
5946 }
5947 len = PySequence_Size(groups);
5948 if (len > MAX_GROUPS) {
5949 PyErr_SetString(PyExc_ValueError, "too many groups");
5950 return NULL;
5951 }
5952 for(i = 0; i < len; i++) {
5953 PyObject *elem;
5954 elem = PySequence_GetItem(groups, i);
5955 if (!elem)
5956 return NULL;
5957 if (!PyLong_Check(elem)) {
5958 PyErr_SetString(PyExc_TypeError,
5959 "groups must be integers");
5960 Py_DECREF(elem);
5961 return NULL;
5962 } else {
5963 unsigned long x = PyLong_AsUnsignedLong(elem);
5964 if (PyErr_Occurred()) {
5965 PyErr_SetString(PyExc_TypeError,
5966 "group id too big");
5967 Py_DECREF(elem);
5968 return NULL;
5969 }
5970 grouplist[i] = x;
5971 /* read back the value to see if it fitted in gid_t */
5972 if (grouplist[i] != x) {
5973 PyErr_SetString(PyExc_TypeError,
5974 "group id too big");
5975 Py_DECREF(elem);
5976 return NULL;
5977 }
5978 }
5979 Py_DECREF(elem);
5980 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005981
Victor Stinner8c62be82010-05-06 00:08:46 +00005982 if (setgroups(len, grouplist) < 0)
5983 return posix_error();
5984 Py_INCREF(Py_None);
5985 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005986}
5987#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005988
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005989#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5990static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005991wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005992{
Victor Stinner8c62be82010-05-06 00:08:46 +00005993 PyObject *result;
5994 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005995
Victor Stinner8c62be82010-05-06 00:08:46 +00005996 if (pid == -1)
5997 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005998
Victor Stinner8c62be82010-05-06 00:08:46 +00005999 if (struct_rusage == NULL) {
6000 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6001 if (m == NULL)
6002 return NULL;
6003 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6004 Py_DECREF(m);
6005 if (struct_rusage == NULL)
6006 return NULL;
6007 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006008
Victor Stinner8c62be82010-05-06 00:08:46 +00006009 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6010 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6011 if (!result)
6012 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006013
6014#ifndef doubletime
6015#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6016#endif
6017
Victor Stinner8c62be82010-05-06 00:08:46 +00006018 PyStructSequence_SET_ITEM(result, 0,
6019 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6020 PyStructSequence_SET_ITEM(result, 1,
6021 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006022#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006023 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6024 SET_INT(result, 2, ru->ru_maxrss);
6025 SET_INT(result, 3, ru->ru_ixrss);
6026 SET_INT(result, 4, ru->ru_idrss);
6027 SET_INT(result, 5, ru->ru_isrss);
6028 SET_INT(result, 6, ru->ru_minflt);
6029 SET_INT(result, 7, ru->ru_majflt);
6030 SET_INT(result, 8, ru->ru_nswap);
6031 SET_INT(result, 9, ru->ru_inblock);
6032 SET_INT(result, 10, ru->ru_oublock);
6033 SET_INT(result, 11, ru->ru_msgsnd);
6034 SET_INT(result, 12, ru->ru_msgrcv);
6035 SET_INT(result, 13, ru->ru_nsignals);
6036 SET_INT(result, 14, ru->ru_nvcsw);
6037 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006038#undef SET_INT
6039
Victor Stinner8c62be82010-05-06 00:08:46 +00006040 if (PyErr_Occurred()) {
6041 Py_DECREF(result);
6042 return NULL;
6043 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006044
Victor Stinner8c62be82010-05-06 00:08:46 +00006045 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006046}
6047#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6048
6049#ifdef HAVE_WAIT3
6050PyDoc_STRVAR(posix_wait3__doc__,
6051"wait3(options) -> (pid, status, rusage)\n\n\
6052Wait for completion of a child process.");
6053
6054static PyObject *
6055posix_wait3(PyObject *self, PyObject *args)
6056{
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 pid_t pid;
6058 int options;
6059 struct rusage ru;
6060 WAIT_TYPE status;
6061 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006062
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6064 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006065
Victor Stinner8c62be82010-05-06 00:08:46 +00006066 Py_BEGIN_ALLOW_THREADS
6067 pid = wait3(&status, options, &ru);
6068 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006069
Victor Stinner8c62be82010-05-06 00:08:46 +00006070 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006071}
6072#endif /* HAVE_WAIT3 */
6073
6074#ifdef HAVE_WAIT4
6075PyDoc_STRVAR(posix_wait4__doc__,
6076"wait4(pid, options) -> (pid, status, rusage)\n\n\
6077Wait for completion of a given child process.");
6078
6079static PyObject *
6080posix_wait4(PyObject *self, PyObject *args)
6081{
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 pid_t pid;
6083 int options;
6084 struct rusage ru;
6085 WAIT_TYPE status;
6086 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006087
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6089 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006090
Victor Stinner8c62be82010-05-06 00:08:46 +00006091 Py_BEGIN_ALLOW_THREADS
6092 pid = wait4(pid, &status, options, &ru);
6093 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006094
Victor Stinner8c62be82010-05-06 00:08:46 +00006095 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006096}
6097#endif /* HAVE_WAIT4 */
6098
Ross Lagerwall7807c352011-03-17 20:20:30 +02006099#if defined(HAVE_WAITID) && !defined(__APPLE__)
6100PyDoc_STRVAR(posix_waitid__doc__,
6101"waitid(idtype, id, options) -> waitid_result\n\n\
6102Wait for the completion of one or more child processes.\n\n\
6103idtype can be P_PID, P_PGID or P_ALL.\n\
6104id specifies the pid to wait on.\n\
6105options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6106or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6107Returns either waitid_result or None if WNOHANG is specified and there are\n\
6108no children in a waitable state.");
6109
6110static PyObject *
6111posix_waitid(PyObject *self, PyObject *args)
6112{
6113 PyObject *result;
6114 idtype_t idtype;
6115 id_t id;
6116 int options, res;
6117 siginfo_t si;
6118 si.si_pid = 0;
6119 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6120 return NULL;
6121 Py_BEGIN_ALLOW_THREADS
6122 res = waitid(idtype, id, &si, options);
6123 Py_END_ALLOW_THREADS
6124 if (res == -1)
6125 return posix_error();
6126
6127 if (si.si_pid == 0)
6128 Py_RETURN_NONE;
6129
6130 result = PyStructSequence_New(&WaitidResultType);
6131 if (!result)
6132 return NULL;
6133
6134 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6135 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6136 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6137 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6138 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6139 if (PyErr_Occurred()) {
6140 Py_DECREF(result);
6141 return NULL;
6142 }
6143
6144 return result;
6145}
6146#endif
6147
Guido van Rossumb6775db1994-08-01 11:34:53 +00006148#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006149PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006150"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006151Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006152
Barry Warsaw53699e91996-12-10 23:23:01 +00006153static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006154posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006155{
Victor Stinner8c62be82010-05-06 00:08:46 +00006156 pid_t pid;
6157 int options;
6158 WAIT_TYPE status;
6159 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006160
Victor Stinner8c62be82010-05-06 00:08:46 +00006161 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6162 return NULL;
6163 Py_BEGIN_ALLOW_THREADS
6164 pid = waitpid(pid, &status, options);
6165 Py_END_ALLOW_THREADS
6166 if (pid == -1)
6167 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006168
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006170}
6171
Tim Petersab034fa2002-02-01 11:27:43 +00006172#elif defined(HAVE_CWAIT)
6173
6174/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006175PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006176"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006178
6179static PyObject *
6180posix_waitpid(PyObject *self, PyObject *args)
6181{
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 Py_intptr_t pid;
6183 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006184
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6186 return NULL;
6187 Py_BEGIN_ALLOW_THREADS
6188 pid = _cwait(&status, pid, options);
6189 Py_END_ALLOW_THREADS
6190 if (pid == -1)
6191 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006192
Victor Stinner8c62be82010-05-06 00:08:46 +00006193 /* shift the status left a byte so this is more like the POSIX waitpid */
6194 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006195}
6196#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006197
Guido van Rossumad0ee831995-03-01 10:34:45 +00006198#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006199PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006200"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006201Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006202
Barry Warsaw53699e91996-12-10 23:23:01 +00006203static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006204posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006205{
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 pid_t pid;
6207 WAIT_TYPE status;
6208 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006209
Victor Stinner8c62be82010-05-06 00:08:46 +00006210 Py_BEGIN_ALLOW_THREADS
6211 pid = wait(&status);
6212 Py_END_ALLOW_THREADS
6213 if (pid == -1)
6214 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006215
Victor Stinner8c62be82010-05-06 00:08:46 +00006216 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006217}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006218#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006219
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006221PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006222"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006223Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006224
Barry Warsaw53699e91996-12-10 23:23:01 +00006225static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006226posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006227{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006228#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006229 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006230#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006231#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006232 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006233 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006234#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006235 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006237#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006238}
6239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006240
Guido van Rossumb6775db1994-08-01 11:34:53 +00006241#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006242PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006243"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006244Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006245
Barry Warsaw53699e91996-12-10 23:23:01 +00006246static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006247posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006248{
Victor Stinner8c62be82010-05-06 00:08:46 +00006249 PyObject* v;
6250 char buf[MAXPATHLEN];
6251 PyObject *opath;
6252 char *path;
6253 int n;
6254 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006255
Victor Stinner8c62be82010-05-06 00:08:46 +00006256 if (!PyArg_ParseTuple(args, "O&:readlink",
6257 PyUnicode_FSConverter, &opath))
6258 return NULL;
6259 path = PyBytes_AsString(opath);
6260 v = PySequence_GetItem(args, 0);
6261 if (v == NULL) {
6262 Py_DECREF(opath);
6263 return NULL;
6264 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006265
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 if (PyUnicode_Check(v)) {
6267 arg_is_unicode = 1;
6268 }
6269 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006270
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 Py_BEGIN_ALLOW_THREADS
6272 n = readlink(path, buf, (int) sizeof buf);
6273 Py_END_ALLOW_THREADS
6274 if (n < 0)
6275 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006276
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006278 if (arg_is_unicode)
6279 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6280 else
6281 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006282}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006283#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006285
Brian Curtin52173d42010-12-02 18:29:18 +00006286#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006287PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006288"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006289Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006290
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006292posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006293{
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006295}
6296#endif /* HAVE_SYMLINK */
6297
Brian Curtind40e6f72010-07-08 21:39:08 +00006298#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6299
6300PyDoc_STRVAR(win_readlink__doc__,
6301"readlink(path) -> path\n\n\
6302Return a string representing the path to which the symbolic link points.");
6303
Brian Curtind40e6f72010-07-08 21:39:08 +00006304/* Windows readlink implementation */
6305static PyObject *
6306win_readlink(PyObject *self, PyObject *args)
6307{
6308 wchar_t *path;
6309 DWORD n_bytes_returned;
6310 DWORD io_result;
6311 PyObject *result;
6312 HANDLE reparse_point_handle;
6313
6314 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6315 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6316 wchar_t *print_name;
6317
6318 if (!PyArg_ParseTuple(args,
6319 "u:readlink",
6320 &path))
6321 return NULL;
6322
6323 /* First get a handle to the reparse point */
6324 Py_BEGIN_ALLOW_THREADS
6325 reparse_point_handle = CreateFileW(
6326 path,
6327 0,
6328 0,
6329 0,
6330 OPEN_EXISTING,
6331 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6332 0);
6333 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006334
Brian Curtind40e6f72010-07-08 21:39:08 +00006335 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6336 {
6337 return win32_error_unicode("readlink", path);
6338 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006339
Brian Curtind40e6f72010-07-08 21:39:08 +00006340 Py_BEGIN_ALLOW_THREADS
6341 /* New call DeviceIoControl to read the reparse point */
6342 io_result = DeviceIoControl(
6343 reparse_point_handle,
6344 FSCTL_GET_REPARSE_POINT,
6345 0, 0, /* in buffer */
6346 target_buffer, sizeof(target_buffer),
6347 &n_bytes_returned,
6348 0 /* we're not using OVERLAPPED_IO */
6349 );
6350 CloseHandle(reparse_point_handle);
6351 Py_END_ALLOW_THREADS
6352
6353 if (io_result==0)
6354 {
6355 return win32_error_unicode("readlink", path);
6356 }
6357
6358 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6359 {
6360 PyErr_SetString(PyExc_ValueError,
6361 "not a symbolic link");
6362 return NULL;
6363 }
Brian Curtin74e45612010-07-09 15:58:59 +00006364 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6365 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6366
6367 result = PyUnicode_FromWideChar(print_name,
6368 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006369 return result;
6370}
6371
6372#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6373
Brian Curtin52173d42010-12-02 18:29:18 +00006374#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006375
6376/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6377static int has_CreateSymbolicLinkW = 0;
6378static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6379static int
6380check_CreateSymbolicLinkW()
6381{
6382 HINSTANCE hKernel32;
6383 /* only recheck */
6384 if (has_CreateSymbolicLinkW)
6385 return has_CreateSymbolicLinkW;
6386 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006387 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6388 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006389 if (Py_CreateSymbolicLinkW)
6390 has_CreateSymbolicLinkW = 1;
6391 return has_CreateSymbolicLinkW;
6392}
6393
6394PyDoc_STRVAR(win_symlink__doc__,
6395"symlink(src, dst, target_is_directory=False)\n\n\
6396Create a symbolic link pointing to src named dst.\n\
6397target_is_directory is required if the target is to be interpreted as\n\
6398a directory.\n\
6399This function requires Windows 6.0 or greater, and raises a\n\
6400NotImplementedError otherwise.");
6401
6402static PyObject *
6403win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6404{
6405 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6406 PyObject *src, *dest;
6407 int target_is_directory = 0;
6408 DWORD res;
6409 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006410
Brian Curtind40e6f72010-07-08 21:39:08 +00006411 if (!check_CreateSymbolicLinkW())
6412 {
6413 /* raise NotImplementedError */
6414 return PyErr_Format(PyExc_NotImplementedError,
6415 "CreateSymbolicLinkW not found");
6416 }
6417 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6418 kwlist, &src, &dest, &target_is_directory))
6419 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006420
6421 if (win32_can_symlink == 0)
6422 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6423
Brian Curtind40e6f72010-07-08 21:39:08 +00006424 if (!convert_to_unicode(&src)) { return NULL; }
6425 if (!convert_to_unicode(&dest)) {
6426 Py_DECREF(src);
6427 return NULL;
6428 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006429
Brian Curtind40e6f72010-07-08 21:39:08 +00006430 /* if src is a directory, ensure target_is_directory==1 */
6431 if(
6432 GetFileAttributesExW(
6433 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6434 ))
6435 {
6436 target_is_directory = target_is_directory ||
6437 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6438 }
6439
6440 Py_BEGIN_ALLOW_THREADS
6441 res = Py_CreateSymbolicLinkW(
6442 PyUnicode_AsUnicode(dest),
6443 PyUnicode_AsUnicode(src),
6444 target_is_directory);
6445 Py_END_ALLOW_THREADS
6446 Py_DECREF(src);
6447 Py_DECREF(dest);
6448 if (!res)
6449 {
6450 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6451 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006452
Brian Curtind40e6f72010-07-08 21:39:08 +00006453 Py_INCREF(Py_None);
6454 return Py_None;
6455}
Brian Curtin52173d42010-12-02 18:29:18 +00006456#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006457
6458#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006459#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6460static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006461system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006462{
6463 ULONG value = 0;
6464
6465 Py_BEGIN_ALLOW_THREADS
6466 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6467 Py_END_ALLOW_THREADS
6468
6469 return value;
6470}
6471
6472static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006473posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006474{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006475 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 return Py_BuildValue("ddddd",
6477 (double)0 /* t.tms_utime / HZ */,
6478 (double)0 /* t.tms_stime / HZ */,
6479 (double)0 /* t.tms_cutime / HZ */,
6480 (double)0 /* t.tms_cstime / HZ */,
6481 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006482}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006483#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006484#define NEED_TICKS_PER_SECOND
6485static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006486static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006487posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006488{
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 struct tms t;
6490 clock_t c;
6491 errno = 0;
6492 c = times(&t);
6493 if (c == (clock_t) -1)
6494 return posix_error();
6495 return Py_BuildValue("ddddd",
6496 (double)t.tms_utime / ticks_per_second,
6497 (double)t.tms_stime / ticks_per_second,
6498 (double)t.tms_cutime / ticks_per_second,
6499 (double)t.tms_cstime / ticks_per_second,
6500 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006501}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006502#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006503#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006504
6505
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006506#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006507#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006508static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006509posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006510{
Victor Stinner8c62be82010-05-06 00:08:46 +00006511 FILETIME create, exit, kernel, user;
6512 HANDLE hProc;
6513 hProc = GetCurrentProcess();
6514 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6515 /* The fields of a FILETIME structure are the hi and lo part
6516 of a 64-bit value expressed in 100 nanosecond units.
6517 1e7 is one second in such units; 1e-7 the inverse.
6518 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6519 */
6520 return Py_BuildValue(
6521 "ddddd",
6522 (double)(user.dwHighDateTime*429.4967296 +
6523 user.dwLowDateTime*1e-7),
6524 (double)(kernel.dwHighDateTime*429.4967296 +
6525 kernel.dwLowDateTime*1e-7),
6526 (double)0,
6527 (double)0,
6528 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006529}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006530#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006531
6532#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006533PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006534"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006535Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006536#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006537
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006538
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006539#ifdef HAVE_GETSID
6540PyDoc_STRVAR(posix_getsid__doc__,
6541"getsid(pid) -> sid\n\n\
6542Call the system call getsid().");
6543
6544static PyObject *
6545posix_getsid(PyObject *self, PyObject *args)
6546{
Victor Stinner8c62be82010-05-06 00:08:46 +00006547 pid_t pid;
6548 int sid;
6549 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6550 return NULL;
6551 sid = getsid(pid);
6552 if (sid < 0)
6553 return posix_error();
6554 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006555}
6556#endif /* HAVE_GETSID */
6557
6558
Guido van Rossumb6775db1994-08-01 11:34:53 +00006559#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006560PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006561"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006562Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006563
Barry Warsaw53699e91996-12-10 23:23:01 +00006564static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006565posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006566{
Victor Stinner8c62be82010-05-06 00:08:46 +00006567 if (setsid() < 0)
6568 return posix_error();
6569 Py_INCREF(Py_None);
6570 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006571}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006572#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006573
Guido van Rossumb6775db1994-08-01 11:34:53 +00006574#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006575PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006576"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006577Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006578
Barry Warsaw53699e91996-12-10 23:23:01 +00006579static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006580posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006581{
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 pid_t pid;
6583 int pgrp;
6584 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6585 return NULL;
6586 if (setpgid(pid, pgrp) < 0)
6587 return posix_error();
6588 Py_INCREF(Py_None);
6589 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006590}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006591#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006592
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006593
Guido van Rossumb6775db1994-08-01 11:34:53 +00006594#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006595PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006596"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006597Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006598
Barry Warsaw53699e91996-12-10 23:23:01 +00006599static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006600posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006601{
Victor Stinner8c62be82010-05-06 00:08:46 +00006602 int fd;
6603 pid_t pgid;
6604 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6605 return NULL;
6606 pgid = tcgetpgrp(fd);
6607 if (pgid < 0)
6608 return posix_error();
6609 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006610}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006611#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006612
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006613
Guido van Rossumb6775db1994-08-01 11:34:53 +00006614#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006615PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006616"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006617Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006618
Barry Warsaw53699e91996-12-10 23:23:01 +00006619static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006620posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006621{
Victor Stinner8c62be82010-05-06 00:08:46 +00006622 int fd;
6623 pid_t pgid;
6624 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6625 return NULL;
6626 if (tcsetpgrp(fd, pgid) < 0)
6627 return posix_error();
6628 Py_INCREF(Py_None);
6629 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006630}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006631#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006632
Guido van Rossum687dd131993-05-17 08:34:16 +00006633/* Functions acting on file descriptors */
6634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006635PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006636"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006637Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006638
Barry Warsaw53699e91996-12-10 23:23:01 +00006639static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006640posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006641{
Victor Stinner8c62be82010-05-06 00:08:46 +00006642 PyObject *ofile;
6643 char *file;
6644 int flag;
6645 int mode = 0777;
6646 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006647
6648#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006650 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006651 Py_BEGIN_ALLOW_THREADS
6652 /* PyUnicode_AS_UNICODE OK without thread
6653 lock as it is a simple dereference. */
6654 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6655 Py_END_ALLOW_THREADS
6656 if (fd < 0)
6657 return posix_error();
6658 return PyLong_FromLong((long)fd);
6659 }
6660 /* Drop the argument parsing error as narrow strings
6661 are also valid. */
6662 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006663#endif
6664
Victor Stinner26de69d2011-06-17 15:15:38 +02006665 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 PyUnicode_FSConverter, &ofile,
6667 &flag, &mode))
6668 return NULL;
6669 file = PyBytes_AsString(ofile);
6670 Py_BEGIN_ALLOW_THREADS
6671 fd = open(file, flag, mode);
6672 Py_END_ALLOW_THREADS
6673 if (fd < 0)
6674 return posix_error_with_allocated_filename(ofile);
6675 Py_DECREF(ofile);
6676 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006677}
6678
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006680PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006681"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006682Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006683
Barry Warsaw53699e91996-12-10 23:23:01 +00006684static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006685posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006686{
Victor Stinner8c62be82010-05-06 00:08:46 +00006687 int fd, res;
6688 if (!PyArg_ParseTuple(args, "i:close", &fd))
6689 return NULL;
6690 if (!_PyVerify_fd(fd))
6691 return posix_error();
6692 Py_BEGIN_ALLOW_THREADS
6693 res = close(fd);
6694 Py_END_ALLOW_THREADS
6695 if (res < 0)
6696 return posix_error();
6697 Py_INCREF(Py_None);
6698 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006699}
6700
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006701
Victor Stinner8c62be82010-05-06 00:08:46 +00006702PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006703"closerange(fd_low, fd_high)\n\n\
6704Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6705
6706static PyObject *
6707posix_closerange(PyObject *self, PyObject *args)
6708{
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 int fd_from, fd_to, i;
6710 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6711 return NULL;
6712 Py_BEGIN_ALLOW_THREADS
6713 for (i = fd_from; i < fd_to; i++)
6714 if (_PyVerify_fd(i))
6715 close(i);
6716 Py_END_ALLOW_THREADS
6717 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006718}
6719
6720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006721PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006722"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006723Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006724
Barry Warsaw53699e91996-12-10 23:23:01 +00006725static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006726posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006727{
Victor Stinner8c62be82010-05-06 00:08:46 +00006728 int fd;
6729 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6730 return NULL;
6731 if (!_PyVerify_fd(fd))
6732 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006734 if (fd < 0)
6735 return posix_error();
6736 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006737}
6738
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006740PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006741"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006742Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006743
Barry Warsaw53699e91996-12-10 23:23:01 +00006744static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006745posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006746{
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 int fd, fd2, res;
6748 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6749 return NULL;
6750 if (!_PyVerify_fd_dup2(fd, fd2))
6751 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006752 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006753 if (res < 0)
6754 return posix_error();
6755 Py_INCREF(Py_None);
6756 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006757}
6758
Ross Lagerwall7807c352011-03-17 20:20:30 +02006759#ifdef HAVE_LOCKF
6760PyDoc_STRVAR(posix_lockf__doc__,
6761"lockf(fd, cmd, len)\n\n\
6762Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6763fd is an open file descriptor.\n\
6764cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6765F_TEST.\n\
6766len specifies the section of the file to lock.");
6767
6768static PyObject *
6769posix_lockf(PyObject *self, PyObject *args)
6770{
6771 int fd, cmd, res;
6772 off_t len;
6773 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6774 &fd, &cmd, _parse_off_t, &len))
6775 return NULL;
6776
6777 Py_BEGIN_ALLOW_THREADS
6778 res = lockf(fd, cmd, len);
6779 Py_END_ALLOW_THREADS
6780
6781 if (res < 0)
6782 return posix_error();
6783
6784 Py_RETURN_NONE;
6785}
6786#endif
6787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006789PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006790"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006791Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006792
Barry Warsaw53699e91996-12-10 23:23:01 +00006793static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006794posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006795{
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006797#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006799#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006801#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006802 PyObject *posobj;
6803 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006805#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6807 switch (how) {
6808 case 0: how = SEEK_SET; break;
6809 case 1: how = SEEK_CUR; break;
6810 case 2: how = SEEK_END; break;
6811 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006812#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006813
Ross Lagerwall8e749672011-03-17 21:54:07 +02006814#if !defined(HAVE_LARGEFILE_SUPPORT)
6815 pos = PyLong_AsLong(posobj);
6816#else
6817 pos = PyLong_AsLongLong(posobj);
6818#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 if (PyErr_Occurred())
6820 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006821
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 if (!_PyVerify_fd(fd))
6823 return posix_error();
6824 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006825#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006827#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006829#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006830 Py_END_ALLOW_THREADS
6831 if (res < 0)
6832 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006833
6834#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006836#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006838#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006839}
6840
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006842PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006843"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006844Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006845
Barry Warsaw53699e91996-12-10 23:23:01 +00006846static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006847posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006848{
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 int fd, size;
6850 Py_ssize_t n;
6851 PyObject *buffer;
6852 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6853 return NULL;
6854 if (size < 0) {
6855 errno = EINVAL;
6856 return posix_error();
6857 }
6858 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6859 if (buffer == NULL)
6860 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006861 if (!_PyVerify_fd(fd)) {
6862 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006863 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006864 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 Py_BEGIN_ALLOW_THREADS
6866 n = read(fd, PyBytes_AS_STRING(buffer), size);
6867 Py_END_ALLOW_THREADS
6868 if (n < 0) {
6869 Py_DECREF(buffer);
6870 return posix_error();
6871 }
6872 if (n != size)
6873 _PyBytes_Resize(&buffer, n);
6874 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006875}
6876
Ross Lagerwall7807c352011-03-17 20:20:30 +02006877#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6878 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006879static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006880iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6881{
6882 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006883 Py_ssize_t blen, total = 0;
6884
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006885 *iov = PyMem_New(struct iovec, cnt);
6886 if (*iov == NULL) {
6887 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006888 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006889 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006890
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006891 *buf = PyMem_New(Py_buffer, cnt);
6892 if (*buf == NULL) {
6893 PyMem_Del(*iov);
6894 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006895 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006896 }
6897
6898 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006899 PyObject *item = PySequence_GetItem(seq, i);
6900 if (item == NULL)
6901 goto fail;
6902 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6903 Py_DECREF(item);
6904 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006905 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006906 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006907 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006908 blen = (*buf)[i].len;
6909 (*iov)[i].iov_len = blen;
6910 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006911 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006912 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006913
6914fail:
6915 PyMem_Del(*iov);
6916 for (j = 0; j < i; j++) {
6917 PyBuffer_Release(&(*buf)[j]);
6918 }
6919 PyMem_Del(*buf);
6920 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006921}
6922
6923static void
6924iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6925{
6926 int i;
6927 PyMem_Del(iov);
6928 for (i = 0; i < cnt; i++) {
6929 PyBuffer_Release(&buf[i]);
6930 }
6931 PyMem_Del(buf);
6932}
6933#endif
6934
Ross Lagerwall7807c352011-03-17 20:20:30 +02006935#ifdef HAVE_READV
6936PyDoc_STRVAR(posix_readv__doc__,
6937"readv(fd, buffers) -> bytesread\n\n\
6938Read from a file descriptor into a number of writable buffers. buffers\n\
6939is an arbitrary sequence of writable buffers.\n\
6940Returns the total number of bytes read.");
6941
6942static PyObject *
6943posix_readv(PyObject *self, PyObject *args)
6944{
6945 int fd, cnt;
6946 Py_ssize_t n;
6947 PyObject *seq;
6948 struct iovec *iov;
6949 Py_buffer *buf;
6950
6951 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6952 return NULL;
6953 if (!PySequence_Check(seq)) {
6954 PyErr_SetString(PyExc_TypeError,
6955 "readv() arg 2 must be a sequence");
6956 return NULL;
6957 }
6958 cnt = PySequence_Size(seq);
6959
6960 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6961 return NULL;
6962
6963 Py_BEGIN_ALLOW_THREADS
6964 n = readv(fd, iov, cnt);
6965 Py_END_ALLOW_THREADS
6966
6967 iov_cleanup(iov, buf, cnt);
6968 return PyLong_FromSsize_t(n);
6969}
6970#endif
6971
6972#ifdef HAVE_PREAD
6973PyDoc_STRVAR(posix_pread__doc__,
6974"pread(fd, buffersize, offset) -> string\n\n\
6975Read from a file descriptor, fd, at a position of offset. It will read up\n\
6976to buffersize number of bytes. The file offset remains unchanged.");
6977
6978static PyObject *
6979posix_pread(PyObject *self, PyObject *args)
6980{
6981 int fd, size;
6982 off_t offset;
6983 Py_ssize_t n;
6984 PyObject *buffer;
6985 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6986 return NULL;
6987
6988 if (size < 0) {
6989 errno = EINVAL;
6990 return posix_error();
6991 }
6992 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6993 if (buffer == NULL)
6994 return NULL;
6995 if (!_PyVerify_fd(fd)) {
6996 Py_DECREF(buffer);
6997 return posix_error();
6998 }
6999 Py_BEGIN_ALLOW_THREADS
7000 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7001 Py_END_ALLOW_THREADS
7002 if (n < 0) {
7003 Py_DECREF(buffer);
7004 return posix_error();
7005 }
7006 if (n != size)
7007 _PyBytes_Resize(&buffer, n);
7008 return buffer;
7009}
7010#endif
7011
7012PyDoc_STRVAR(posix_write__doc__,
7013"write(fd, string) -> byteswritten\n\n\
7014Write a string to a file descriptor.");
7015
7016static PyObject *
7017posix_write(PyObject *self, PyObject *args)
7018{
7019 Py_buffer pbuf;
7020 int fd;
7021 Py_ssize_t size, len;
7022
7023 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7024 return NULL;
7025 if (!_PyVerify_fd(fd)) {
7026 PyBuffer_Release(&pbuf);
7027 return posix_error();
7028 }
7029 len = pbuf.len;
7030 Py_BEGIN_ALLOW_THREADS
7031#if defined(MS_WIN64) || defined(MS_WINDOWS)
7032 if (len > INT_MAX)
7033 len = INT_MAX;
7034 size = write(fd, pbuf.buf, (int)len);
7035#else
7036 size = write(fd, pbuf.buf, len);
7037#endif
7038 Py_END_ALLOW_THREADS
7039 PyBuffer_Release(&pbuf);
7040 if (size < 0)
7041 return posix_error();
7042 return PyLong_FromSsize_t(size);
7043}
7044
7045#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007046PyDoc_STRVAR(posix_sendfile__doc__,
7047"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7048sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7049 -> byteswritten\n\
7050Copy nbytes bytes from file descriptor in to file descriptor out.");
7051
7052static PyObject *
7053posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7054{
7055 int in, out;
7056 Py_ssize_t ret;
7057 off_t offset;
7058
7059#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7060#ifndef __APPLE__
7061 Py_ssize_t len;
7062#endif
7063 PyObject *headers = NULL, *trailers = NULL;
7064 Py_buffer *hbuf, *tbuf;
7065 off_t sbytes;
7066 struct sf_hdtr sf;
7067 int flags = 0;
7068 sf.headers = NULL;
7069 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007070 static char *keywords[] = {"out", "in",
7071 "offset", "count",
7072 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007073
7074#ifdef __APPLE__
7075 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007076 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007077#else
7078 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007079 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007080#endif
7081 &headers, &trailers, &flags))
7082 return NULL;
7083 if (headers != NULL) {
7084 if (!PySequence_Check(headers)) {
7085 PyErr_SetString(PyExc_TypeError,
7086 "sendfile() headers must be a sequence or None");
7087 return NULL;
7088 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007089 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007090 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007091 if (sf.hdr_cnt > 0 &&
7092 !(i = iov_setup(&(sf.headers), &hbuf,
7093 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007094 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007095#ifdef __APPLE__
7096 sbytes += i;
7097#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007098 }
7099 }
7100 if (trailers != NULL) {
7101 if (!PySequence_Check(trailers)) {
7102 PyErr_SetString(PyExc_TypeError,
7103 "sendfile() trailers must be a sequence or None");
7104 return NULL;
7105 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007106 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007107 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007108 if (sf.trl_cnt > 0 &&
7109 !(i = iov_setup(&(sf.trailers), &tbuf,
7110 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007111 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007112#ifdef __APPLE__
7113 sbytes += i;
7114#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007115 }
7116 }
7117
7118 Py_BEGIN_ALLOW_THREADS
7119#ifdef __APPLE__
7120 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7121#else
7122 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7123#endif
7124 Py_END_ALLOW_THREADS
7125
7126 if (sf.headers != NULL)
7127 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7128 if (sf.trailers != NULL)
7129 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7130
7131 if (ret < 0) {
7132 if ((errno == EAGAIN) || (errno == EBUSY)) {
7133 if (sbytes != 0) {
7134 // some data has been sent
7135 goto done;
7136 }
7137 else {
7138 // no data has been sent; upper application is supposed
7139 // to retry on EAGAIN or EBUSY
7140 return posix_error();
7141 }
7142 }
7143 return posix_error();
7144 }
7145 goto done;
7146
7147done:
7148 #if !defined(HAVE_LARGEFILE_SUPPORT)
7149 return Py_BuildValue("l", sbytes);
7150 #else
7151 return Py_BuildValue("L", sbytes);
7152 #endif
7153
7154#else
7155 Py_ssize_t count;
7156 PyObject *offobj;
7157 static char *keywords[] = {"out", "in",
7158 "offset", "count", NULL};
7159 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7160 keywords, &out, &in, &offobj, &count))
7161 return NULL;
7162#ifdef linux
7163 if (offobj == Py_None) {
7164 Py_BEGIN_ALLOW_THREADS
7165 ret = sendfile(out, in, NULL, count);
7166 Py_END_ALLOW_THREADS
7167 if (ret < 0)
7168 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007169 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007170 }
7171#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007172 if (!_parse_off_t(offobj, &offset))
7173 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007174 Py_BEGIN_ALLOW_THREADS
7175 ret = sendfile(out, in, &offset, count);
7176 Py_END_ALLOW_THREADS
7177 if (ret < 0)
7178 return posix_error();
7179 return Py_BuildValue("n", ret);
7180#endif
7181}
7182#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007184PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007185"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007186Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007187
Barry Warsaw53699e91996-12-10 23:23:01 +00007188static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007189posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007190{
Victor Stinner8c62be82010-05-06 00:08:46 +00007191 int fd;
7192 STRUCT_STAT st;
7193 int res;
7194 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7195 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007196#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 /* on OpenVMS we must ensure that all bytes are written to the file */
7198 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007199#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 if (!_PyVerify_fd(fd))
7201 return posix_error();
7202 Py_BEGIN_ALLOW_THREADS
7203 res = FSTAT(fd, &st);
7204 Py_END_ALLOW_THREADS
7205 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007206#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007208#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007209 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007210#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 }
Tim Peters5aa91602002-01-30 05:46:57 +00007212
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007214}
7215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007216PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007217"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007218Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007219connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007220
7221static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007222posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007223{
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 int fd;
7225 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7226 return NULL;
7227 if (!_PyVerify_fd(fd))
7228 return PyBool_FromLong(0);
7229 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007230}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007231
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007232#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007233PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007234"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007235Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007236
Barry Warsaw53699e91996-12-10 23:23:01 +00007237static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007238posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007239{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007240#if defined(PYOS_OS2)
7241 HFILE read, write;
7242 APIRET rc;
7243
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007244 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007245 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007246 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007247
7248 return Py_BuildValue("(ii)", read, write);
7249#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007250#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 int fds[2];
7252 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 if (res != 0)
7255 return posix_error();
7256 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007257#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 HANDLE read, write;
7259 int read_fd, write_fd;
7260 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007262 if (!ok)
7263 return win32_error("CreatePipe", NULL);
7264 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7265 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7266 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007267#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007268#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007269}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007270#endif /* HAVE_PIPE */
7271
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007272#ifdef HAVE_PIPE2
7273PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007274"pipe2(flags) -> (read_end, write_end)\n\n\
7275Create a pipe with flags set atomically.\n\
7276flags can be constructed by ORing together one or more of these values:\n\
7277O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007278");
7279
7280static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007281posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007282{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007283 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007284 int fds[2];
7285 int res;
7286
Charles-François Natali368f34b2011-06-06 19:49:47 +02007287 flags = PyLong_AsLong(arg);
7288 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007289 return NULL;
7290
7291 res = pipe2(fds, flags);
7292 if (res != 0)
7293 return posix_error();
7294 return Py_BuildValue("(ii)", fds[0], fds[1]);
7295}
7296#endif /* HAVE_PIPE2 */
7297
Ross Lagerwall7807c352011-03-17 20:20:30 +02007298#ifdef HAVE_WRITEV
7299PyDoc_STRVAR(posix_writev__doc__,
7300"writev(fd, buffers) -> byteswritten\n\n\
7301Write the contents of buffers to a file descriptor, where buffers is an\n\
7302arbitrary sequence of buffers.\n\
7303Returns the total bytes written.");
7304
7305static PyObject *
7306posix_writev(PyObject *self, PyObject *args)
7307{
7308 int fd, cnt;
7309 Py_ssize_t res;
7310 PyObject *seq;
7311 struct iovec *iov;
7312 Py_buffer *buf;
7313 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7314 return NULL;
7315 if (!PySequence_Check(seq)) {
7316 PyErr_SetString(PyExc_TypeError,
7317 "writev() arg 2 must be a sequence");
7318 return NULL;
7319 }
7320 cnt = PySequence_Size(seq);
7321
7322 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7323 return NULL;
7324 }
7325
7326 Py_BEGIN_ALLOW_THREADS
7327 res = writev(fd, iov, cnt);
7328 Py_END_ALLOW_THREADS
7329
7330 iov_cleanup(iov, buf, cnt);
7331 return PyLong_FromSsize_t(res);
7332}
7333#endif
7334
7335#ifdef HAVE_PWRITE
7336PyDoc_STRVAR(posix_pwrite__doc__,
7337"pwrite(fd, string, offset) -> byteswritten\n\n\
7338Write string to a file descriptor, fd, from offset, leaving the file\n\
7339offset unchanged.");
7340
7341static PyObject *
7342posix_pwrite(PyObject *self, PyObject *args)
7343{
7344 Py_buffer pbuf;
7345 int fd;
7346 off_t offset;
7347 Py_ssize_t size;
7348
7349 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7350 return NULL;
7351
7352 if (!_PyVerify_fd(fd)) {
7353 PyBuffer_Release(&pbuf);
7354 return posix_error();
7355 }
7356 Py_BEGIN_ALLOW_THREADS
7357 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7358 Py_END_ALLOW_THREADS
7359 PyBuffer_Release(&pbuf);
7360 if (size < 0)
7361 return posix_error();
7362 return PyLong_FromSsize_t(size);
7363}
7364#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007365
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007366#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007367PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007368"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007369Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007370
Barry Warsaw53699e91996-12-10 23:23:01 +00007371static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007372posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007373{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007374 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007375 char *filename;
7376 int mode = 0666;
7377 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007378 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7379 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007381 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 Py_BEGIN_ALLOW_THREADS
7383 res = mkfifo(filename, mode);
7384 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007385 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007386 if (res < 0)
7387 return posix_error();
7388 Py_INCREF(Py_None);
7389 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007390}
7391#endif
7392
7393
Neal Norwitz11690112002-07-30 01:08:28 +00007394#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007395PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007396"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007397Create a filesystem node (file, device special file or named pipe)\n\
7398named filename. mode specifies both the permissions to use and the\n\
7399type of node to be created, being combined (bitwise OR) with one of\n\
7400S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007401device defines the newly created device special file (probably using\n\
7402os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007403
7404
7405static PyObject *
7406posix_mknod(PyObject *self, PyObject *args)
7407{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007408 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007409 char *filename;
7410 int mode = 0600;
7411 int device = 0;
7412 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007413 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7414 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007415 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007416 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007417 Py_BEGIN_ALLOW_THREADS
7418 res = mknod(filename, mode, device);
7419 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007420 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007421 if (res < 0)
7422 return posix_error();
7423 Py_INCREF(Py_None);
7424 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007425}
7426#endif
7427
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007428#ifdef HAVE_DEVICE_MACROS
7429PyDoc_STRVAR(posix_major__doc__,
7430"major(device) -> major number\n\
7431Extracts a device major number from a raw device number.");
7432
7433static PyObject *
7434posix_major(PyObject *self, PyObject *args)
7435{
Victor Stinner8c62be82010-05-06 00:08:46 +00007436 int device;
7437 if (!PyArg_ParseTuple(args, "i:major", &device))
7438 return NULL;
7439 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007440}
7441
7442PyDoc_STRVAR(posix_minor__doc__,
7443"minor(device) -> minor number\n\
7444Extracts a device minor number from a raw device number.");
7445
7446static PyObject *
7447posix_minor(PyObject *self, PyObject *args)
7448{
Victor Stinner8c62be82010-05-06 00:08:46 +00007449 int device;
7450 if (!PyArg_ParseTuple(args, "i:minor", &device))
7451 return NULL;
7452 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007453}
7454
7455PyDoc_STRVAR(posix_makedev__doc__,
7456"makedev(major, minor) -> device number\n\
7457Composes a raw device number from the major and minor device numbers.");
7458
7459static PyObject *
7460posix_makedev(PyObject *self, PyObject *args)
7461{
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 int major, minor;
7463 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7464 return NULL;
7465 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007466}
7467#endif /* device macros */
7468
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007469
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007470#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007471PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007472"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007473Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007474
Barry Warsaw53699e91996-12-10 23:23:01 +00007475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007476posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007477{
Victor Stinner8c62be82010-05-06 00:08:46 +00007478 int fd;
7479 off_t length;
7480 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007481
Ross Lagerwall7807c352011-03-17 20:20:30 +02007482 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007483 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007484
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 Py_BEGIN_ALLOW_THREADS
7486 res = ftruncate(fd, length);
7487 Py_END_ALLOW_THREADS
7488 if (res < 0)
7489 return posix_error();
7490 Py_INCREF(Py_None);
7491 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007492}
7493#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007494
Ross Lagerwall7807c352011-03-17 20:20:30 +02007495#ifdef HAVE_TRUNCATE
7496PyDoc_STRVAR(posix_truncate__doc__,
7497"truncate(path, length)\n\n\
7498Truncate the file given by path to length bytes.");
7499
7500static PyObject *
7501posix_truncate(PyObject *self, PyObject *args)
7502{
7503 PyObject *opath;
7504 const char *path;
7505 off_t length;
7506 int res;
7507
7508 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7509 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7510 return NULL;
7511 path = PyBytes_AsString(opath);
7512
7513 Py_BEGIN_ALLOW_THREADS
7514 res = truncate(path, length);
7515 Py_END_ALLOW_THREADS
7516 Py_DECREF(opath);
7517 if (res < 0)
7518 return posix_error();
7519 Py_RETURN_NONE;
7520}
7521#endif
7522
7523#ifdef HAVE_POSIX_FALLOCATE
7524PyDoc_STRVAR(posix_posix_fallocate__doc__,
7525"posix_fallocate(fd, offset, len)\n\n\
7526Ensures that enough disk space is allocated for the file specified by fd\n\
7527starting from offset and continuing for len bytes.");
7528
7529static PyObject *
7530posix_posix_fallocate(PyObject *self, PyObject *args)
7531{
7532 off_t len, offset;
7533 int res, fd;
7534
7535 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7536 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7537 return NULL;
7538
7539 Py_BEGIN_ALLOW_THREADS
7540 res = posix_fallocate(fd, offset, len);
7541 Py_END_ALLOW_THREADS
7542 if (res != 0) {
7543 errno = res;
7544 return posix_error();
7545 }
7546 Py_RETURN_NONE;
7547}
7548#endif
7549
7550#ifdef HAVE_POSIX_FADVISE
7551PyDoc_STRVAR(posix_posix_fadvise__doc__,
7552"posix_fadvise(fd, offset, len, advice)\n\n\
7553Announces an intention to access data in a specific pattern thus allowing\n\
7554the kernel to make optimizations.\n\
7555The advice applies to the region of the file specified by fd starting at\n\
7556offset and continuing for len bytes.\n\
7557advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7558POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7559POSIX_FADV_DONTNEED.");
7560
7561static PyObject *
7562posix_posix_fadvise(PyObject *self, PyObject *args)
7563{
7564 off_t len, offset;
7565 int res, fd, advice;
7566
7567 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7568 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7569 return NULL;
7570
7571 Py_BEGIN_ALLOW_THREADS
7572 res = posix_fadvise(fd, offset, len, advice);
7573 Py_END_ALLOW_THREADS
7574 if (res != 0) {
7575 errno = res;
7576 return posix_error();
7577 }
7578 Py_RETURN_NONE;
7579}
7580#endif
7581
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007582#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007583PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007584"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007585Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007586
Fred Drake762e2061999-08-26 17:23:54 +00007587/* Save putenv() parameters as values here, so we can collect them when they
7588 * get re-set with another call for the same key. */
7589static PyObject *posix_putenv_garbage;
7590
Tim Peters5aa91602002-01-30 05:46:57 +00007591static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007592posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007593{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007594#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 wchar_t *s1, *s2;
7596 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007597#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 PyObject *os1, *os2;
7599 char *s1, *s2;
7600 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007601#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007602 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007603 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007604
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007605#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007606 if (!PyArg_ParseTuple(args,
7607 "uu:putenv",
7608 &s1, &s2))
7609 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007610#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007611 if (!PyArg_ParseTuple(args,
7612 "O&O&:putenv",
7613 PyUnicode_FSConverter, &os1,
7614 PyUnicode_FSConverter, &os2))
7615 return NULL;
7616 s1 = PyBytes_AsString(os1);
7617 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007618#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007619
7620#if defined(PYOS_OS2)
7621 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7622 APIRET rc;
7623
Guido van Rossumd48f2521997-12-05 22:19:34 +00007624 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007625 if (rc != NO_ERROR) {
7626 os2_error(rc);
7627 goto error;
7628 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007629
7630 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7631 APIRET rc;
7632
Guido van Rossumd48f2521997-12-05 22:19:34 +00007633 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007634 if (rc != NO_ERROR) {
7635 os2_error(rc);
7636 goto error;
7637 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007638 } else {
7639#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 /* XXX This can leak memory -- not easy to fix :-( */
7641 /* len includes space for a trailing \0; the size arg to
7642 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007643#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007644 len = wcslen(s1) + wcslen(s2) + 2;
7645 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007646#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007647 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007648 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007649#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007650 if (newstr == NULL) {
7651 PyErr_NoMemory();
7652 goto error;
7653 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007654#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007655 newenv = PyUnicode_AsUnicode(newstr);
7656 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7657 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007658 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007659 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007661#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007662 newenv = PyBytes_AS_STRING(newstr);
7663 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7664 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007665 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007666 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007668#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007669
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 /* Install the first arg and newstr in posix_putenv_garbage;
7671 * this will cause previous value to be collected. This has to
7672 * happen after the real putenv() call because the old value
7673 * was still accessible until then. */
7674 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007675#ifdef MS_WINDOWS
7676 PyTuple_GET_ITEM(args, 0),
7677#else
7678 os1,
7679#endif
7680 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 /* really not much we can do; just leak */
7682 PyErr_Clear();
7683 }
7684 else {
7685 Py_DECREF(newstr);
7686 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007687
7688#if defined(PYOS_OS2)
7689 }
7690#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007691
Martin v. Löwis011e8422009-05-05 04:43:17 +00007692#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007693 Py_DECREF(os1);
7694 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007695#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007696 Py_RETURN_NONE;
7697
7698error:
7699#ifndef MS_WINDOWS
7700 Py_DECREF(os1);
7701 Py_DECREF(os2);
7702#endif
7703 Py_XDECREF(newstr);
7704 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007705}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007706#endif /* putenv */
7707
Guido van Rossumc524d952001-10-19 01:31:59 +00007708#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007709PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007710"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007711Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007712
7713static PyObject *
7714posix_unsetenv(PyObject *self, PyObject *args)
7715{
Victor Stinner84ae1182010-05-06 22:05:07 +00007716#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007718
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7720 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007721#else
7722 PyObject *os1;
7723 char *s1;
7724
7725 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7726 PyUnicode_FSConverter, &os1))
7727 return NULL;
7728 s1 = PyBytes_AsString(os1);
7729#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007730
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007732
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 /* Remove the key from posix_putenv_garbage;
7734 * this will cause it to be collected. This has to
7735 * happen after the real unsetenv() call because the
7736 * old value was still accessible until then.
7737 */
7738 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007739#ifdef MS_WINDOWS
7740 PyTuple_GET_ITEM(args, 0)
7741#else
7742 os1
7743#endif
7744 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007745 /* really not much we can do; just leak */
7746 PyErr_Clear();
7747 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007748
Victor Stinner84ae1182010-05-06 22:05:07 +00007749#ifndef MS_WINDOWS
7750 Py_DECREF(os1);
7751#endif
7752 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007753}
7754#endif /* unsetenv */
7755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007756PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007757"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007758Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007759
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007761posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007762{
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 int code;
7764 char *message;
7765 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7766 return NULL;
7767 message = strerror(code);
7768 if (message == NULL) {
7769 PyErr_SetString(PyExc_ValueError,
7770 "strerror() argument out of range");
7771 return NULL;
7772 }
7773 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007774}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007775
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007776
Guido van Rossumc9641791998-08-04 15:26:23 +00007777#ifdef HAVE_SYS_WAIT_H
7778
Fred Drake106c1a02002-04-23 15:58:02 +00007779#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007780PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007781"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007782Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007783
7784static PyObject *
7785posix_WCOREDUMP(PyObject *self, PyObject *args)
7786{
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 WAIT_TYPE status;
7788 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007789
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7791 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007792
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007794}
7795#endif /* WCOREDUMP */
7796
7797#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007798PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007799"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007800Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007801job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007802
7803static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007804posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007805{
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 WAIT_TYPE status;
7807 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007808
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7810 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007811
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007813}
7814#endif /* WIFCONTINUED */
7815
Guido van Rossumc9641791998-08-04 15:26:23 +00007816#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007817PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007818"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007819Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007820
7821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007822posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007823{
Victor Stinner8c62be82010-05-06 00:08:46 +00007824 WAIT_TYPE status;
7825 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007826
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7828 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007829
Victor Stinner8c62be82010-05-06 00:08:46 +00007830 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007831}
7832#endif /* WIFSTOPPED */
7833
7834#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007835PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007836"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007837Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007838
7839static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007840posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007841{
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 WAIT_TYPE status;
7843 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007844
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7846 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007847
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007849}
7850#endif /* WIFSIGNALED */
7851
7852#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007853PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007854"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007855Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007856system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007857
7858static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007859posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007860{
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 WAIT_TYPE status;
7862 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007863
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7865 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007866
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007868}
7869#endif /* WIFEXITED */
7870
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007871#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007872PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007873"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007874Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007875
7876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007877posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007878{
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 WAIT_TYPE status;
7880 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007881
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7883 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007884
Victor Stinner8c62be82010-05-06 00:08:46 +00007885 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007886}
7887#endif /* WEXITSTATUS */
7888
7889#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007890PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007891"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007892Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007893value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007894
7895static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007896posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007897{
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 WAIT_TYPE status;
7899 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007900
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7902 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007903
Victor Stinner8c62be82010-05-06 00:08:46 +00007904 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007905}
7906#endif /* WTERMSIG */
7907
7908#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007909PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007910"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007911Return the signal that stopped the process that provided\n\
7912the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007913
7914static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007915posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007916{
Victor Stinner8c62be82010-05-06 00:08:46 +00007917 WAIT_TYPE status;
7918 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007919
Victor Stinner8c62be82010-05-06 00:08:46 +00007920 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7921 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007922
Victor Stinner8c62be82010-05-06 00:08:46 +00007923 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007924}
7925#endif /* WSTOPSIG */
7926
7927#endif /* HAVE_SYS_WAIT_H */
7928
7929
Thomas Wouters477c8d52006-05-27 19:21:47 +00007930#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007931#ifdef _SCO_DS
7932/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7933 needed definitions in sys/statvfs.h */
7934#define _SVID3
7935#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007936#include <sys/statvfs.h>
7937
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007938static PyObject*
7939_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7941 if (v == NULL)
7942 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007943
7944#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7946 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7947 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7948 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7949 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7950 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7951 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7952 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7953 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7954 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007955#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007956 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7957 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7958 PyStructSequence_SET_ITEM(v, 2,
7959 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7960 PyStructSequence_SET_ITEM(v, 3,
7961 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7962 PyStructSequence_SET_ITEM(v, 4,
7963 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7964 PyStructSequence_SET_ITEM(v, 5,
7965 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7966 PyStructSequence_SET_ITEM(v, 6,
7967 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7968 PyStructSequence_SET_ITEM(v, 7,
7969 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7970 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7971 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007972#endif
7973
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007975}
7976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007977PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007978"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007979Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007980
7981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007982posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007983{
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 int fd, res;
7985 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007986
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7988 return NULL;
7989 Py_BEGIN_ALLOW_THREADS
7990 res = fstatvfs(fd, &st);
7991 Py_END_ALLOW_THREADS
7992 if (res != 0)
7993 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007994
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007996}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007997#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007998
7999
Thomas Wouters477c8d52006-05-27 19:21:47 +00008000#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008001#include <sys/statvfs.h>
8002
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008003PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008004"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008005Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008006
8007static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008008posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008009{
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 char *path;
8011 int res;
8012 struct statvfs st;
8013 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
8014 return NULL;
8015 Py_BEGIN_ALLOW_THREADS
8016 res = statvfs(path, &st);
8017 Py_END_ALLOW_THREADS
8018 if (res != 0)
8019 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008020
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008022}
8023#endif /* HAVE_STATVFS */
8024
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008025#ifdef MS_WINDOWS
8026PyDoc_STRVAR(win32__getdiskusage__doc__,
8027"_getdiskusage(path) -> (total, free)\n\n\
8028Return disk usage statistics about the given path as (total, free) tuple.");
8029
8030static PyObject *
8031win32__getdiskusage(PyObject *self, PyObject *args)
8032{
8033 BOOL retval;
8034 ULARGE_INTEGER _, total, free;
8035 LPCTSTR path;
8036
8037 if (! PyArg_ParseTuple(args, "s", &path))
8038 return NULL;
8039
8040 Py_BEGIN_ALLOW_THREADS
8041 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8042 Py_END_ALLOW_THREADS
8043 if (retval == 0)
8044 return PyErr_SetFromWindowsErr(0);
8045
8046 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8047}
8048#endif
8049
8050
Fred Drakec9680921999-12-13 16:37:25 +00008051/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8052 * It maps strings representing configuration variable names to
8053 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008054 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008055 * rarely-used constants. There are three separate tables that use
8056 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008057 *
8058 * This code is always included, even if none of the interfaces that
8059 * need it are included. The #if hackery needed to avoid it would be
8060 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008061 */
8062struct constdef {
8063 char *name;
8064 long value;
8065};
8066
Fred Drake12c6e2d1999-12-14 21:25:03 +00008067static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008068conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008069 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008070{
Christian Heimes217cfd12007-12-02 14:31:20 +00008071 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008072 *valuep = PyLong_AS_LONG(arg);
8073 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008074 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008075 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008076 /* look up the value in the table using a binary search */
8077 size_t lo = 0;
8078 size_t mid;
8079 size_t hi = tablesize;
8080 int cmp;
8081 const char *confname;
8082 if (!PyUnicode_Check(arg)) {
8083 PyErr_SetString(PyExc_TypeError,
8084 "configuration names must be strings or integers");
8085 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008086 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008087 confname = _PyUnicode_AsString(arg);
8088 if (confname == NULL)
8089 return 0;
8090 while (lo < hi) {
8091 mid = (lo + hi) / 2;
8092 cmp = strcmp(confname, table[mid].name);
8093 if (cmp < 0)
8094 hi = mid;
8095 else if (cmp > 0)
8096 lo = mid + 1;
8097 else {
8098 *valuep = table[mid].value;
8099 return 1;
8100 }
8101 }
8102 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8103 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008104 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008105}
8106
8107
8108#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8109static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008110#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008111 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008112#endif
8113#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008114 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008115#endif
Fred Drakec9680921999-12-13 16:37:25 +00008116#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008117 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
8131#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008151#endif
8152#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008153 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008154#endif
8155#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008156 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008157#endif
8158#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008160#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008161#ifdef _PC_ACL_ENABLED
8162 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8163#endif
8164#ifdef _PC_MIN_HOLE_SIZE
8165 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8166#endif
8167#ifdef _PC_ALLOC_SIZE_MIN
8168 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8169#endif
8170#ifdef _PC_REC_INCR_XFER_SIZE
8171 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8172#endif
8173#ifdef _PC_REC_MAX_XFER_SIZE
8174 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8175#endif
8176#ifdef _PC_REC_MIN_XFER_SIZE
8177 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8178#endif
8179#ifdef _PC_REC_XFER_ALIGN
8180 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8181#endif
8182#ifdef _PC_SYMLINK_MAX
8183 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8184#endif
8185#ifdef _PC_XATTR_ENABLED
8186 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8187#endif
8188#ifdef _PC_XATTR_EXISTS
8189 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8190#endif
8191#ifdef _PC_TIMESTAMP_RESOLUTION
8192 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8193#endif
Fred Drakec9680921999-12-13 16:37:25 +00008194};
8195
Fred Drakec9680921999-12-13 16:37:25 +00008196static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008197conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008198{
8199 return conv_confname(arg, valuep, posix_constants_pathconf,
8200 sizeof(posix_constants_pathconf)
8201 / sizeof(struct constdef));
8202}
8203#endif
8204
8205#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008206PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008207"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008208Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008209If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008210
8211static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008212posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008213{
8214 PyObject *result = NULL;
8215 int name, fd;
8216
Fred Drake12c6e2d1999-12-14 21:25:03 +00008217 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8218 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008219 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008220
Stefan Krah0e803b32010-11-26 16:16:47 +00008221 errno = 0;
8222 limit = fpathconf(fd, name);
8223 if (limit == -1 && errno != 0)
8224 posix_error();
8225 else
8226 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008227 }
8228 return result;
8229}
8230#endif
8231
8232
8233#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008234PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008235"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008236Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008237If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008238
8239static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008240posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008241{
8242 PyObject *result = NULL;
8243 int name;
8244 char *path;
8245
8246 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8247 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008249
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 errno = 0;
8251 limit = pathconf(path, name);
8252 if (limit == -1 && errno != 0) {
8253 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008254 /* could be a path or name problem */
8255 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008256 else
Stefan Krah99439262010-11-26 12:58:05 +00008257 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008258 }
8259 else
8260 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008261 }
8262 return result;
8263}
8264#endif
8265
8266#ifdef HAVE_CONFSTR
8267static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008268#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008270#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008271#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008273#endif
8274#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008276#endif
Fred Draked86ed291999-12-15 15:34:33 +00008277#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008279#endif
8280#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008282#endif
8283#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008284 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008285#endif
8286#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008288#endif
Fred Drakec9680921999-12-13 16:37:25 +00008289#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008291#endif
8292#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008294#endif
8295#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008297#endif
8298#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008300#endif
8301#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008303#endif
8304#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008306#endif
8307#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008308 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008309#endif
8310#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008312#endif
Fred Draked86ed291999-12-15 15:34:33 +00008313#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008315#endif
Fred Drakec9680921999-12-13 16:37:25 +00008316#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008318#endif
Fred Draked86ed291999-12-15 15:34:33 +00008319#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008321#endif
8322#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008324#endif
8325#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008327#endif
8328#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008329 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008330#endif
Fred Drakec9680921999-12-13 16:37:25 +00008331#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008332 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008333#endif
8334#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008336#endif
8337#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008338 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008339#endif
8340#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008341 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008342#endif
8343#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008344 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008345#endif
8346#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008347 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008348#endif
8349#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008350 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008351#endif
8352#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008353 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008354#endif
8355#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008357#endif
8358#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008359 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008360#endif
8361#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008362 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008363#endif
8364#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008365 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008366#endif
8367#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008369#endif
8370#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008372#endif
8373#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008374 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008375#endif
8376#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008378#endif
Fred Draked86ed291999-12-15 15:34:33 +00008379#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008380 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008381#endif
8382#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008383 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008384#endif
8385#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008386 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008387#endif
8388#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008389 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008390#endif
8391#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008392 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008393#endif
8394#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008395 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008396#endif
8397#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008398 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008399#endif
8400#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008401 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008402#endif
8403#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008405#endif
8406#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008407 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008408#endif
8409#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008410 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008411#endif
8412#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008413 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008414#endif
8415#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008416 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008417#endif
Fred Drakec9680921999-12-13 16:37:25 +00008418};
8419
8420static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008421conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008422{
8423 return conv_confname(arg, valuep, posix_constants_confstr,
8424 sizeof(posix_constants_confstr)
8425 / sizeof(struct constdef));
8426}
8427
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008428PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008429"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008430Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008431
8432static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008433posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008434{
8435 PyObject *result = NULL;
8436 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008437 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008438 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008439
Victor Stinnercb043522010-09-10 23:49:04 +00008440 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8441 return NULL;
8442
8443 errno = 0;
8444 len = confstr(name, buffer, sizeof(buffer));
8445 if (len == 0) {
8446 if (errno) {
8447 posix_error();
8448 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008449 }
8450 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008451 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008452 }
8453 }
Victor Stinnercb043522010-09-10 23:49:04 +00008454
8455 if ((unsigned int)len >= sizeof(buffer)) {
8456 char *buf = PyMem_Malloc(len);
8457 if (buf == NULL)
8458 return PyErr_NoMemory();
8459 confstr(name, buf, len);
8460 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8461 PyMem_Free(buf);
8462 }
8463 else
8464 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008465 return result;
8466}
8467#endif
8468
8469
8470#ifdef HAVE_SYSCONF
8471static struct constdef posix_constants_sysconf[] = {
8472#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008474#endif
8475#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008477#endif
8478#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008479 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008480#endif
8481#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008483#endif
8484#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008486#endif
8487#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008488 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008489#endif
8490#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008491 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008492#endif
8493#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008494 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008495#endif
8496#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008497 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008498#endif
8499#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008501#endif
Fred Draked86ed291999-12-15 15:34:33 +00008502#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008504#endif
8505#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008507#endif
Fred Drakec9680921999-12-13 16:37:25 +00008508#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008510#endif
Fred Drakec9680921999-12-13 16:37:25 +00008511#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008512 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008513#endif
8514#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008516#endif
8517#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008519#endif
8520#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008522#endif
8523#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008524 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008525#endif
Fred Draked86ed291999-12-15 15:34:33 +00008526#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008527 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008528#endif
Fred Drakec9680921999-12-13 16:37:25 +00008529#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008530 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008531#endif
8532#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008534#endif
8535#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008537#endif
8538#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008540#endif
8541#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008542 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008543#endif
Fred Draked86ed291999-12-15 15:34:33 +00008544#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008545 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008546#endif
Fred Drakec9680921999-12-13 16:37:25 +00008547#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008548 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008549#endif
8550#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008552#endif
8553#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008554 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008555#endif
8556#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008557 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008558#endif
8559#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008560 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008561#endif
8562#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008563 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008564#endif
8565#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008566 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008567#endif
8568#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008569 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008570#endif
8571#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008572 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008573#endif
8574#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008575 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008576#endif
8577#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008578 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008579#endif
8580#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008581 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008582#endif
8583#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008584 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008585#endif
8586#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008587 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008588#endif
8589#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008590 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008591#endif
8592#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008593 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008594#endif
8595#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008596 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008597#endif
8598#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008600#endif
8601#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008602 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008603#endif
8604#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008605 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008606#endif
8607#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008608 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008609#endif
8610#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008611 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008612#endif
8613#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008614 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008615#endif
Fred Draked86ed291999-12-15 15:34:33 +00008616#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008617 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008618#endif
Fred Drakec9680921999-12-13 16:37:25 +00008619#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008620 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008621#endif
8622#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008623 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008624#endif
8625#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008626 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008627#endif
Fred Draked86ed291999-12-15 15:34:33 +00008628#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008630#endif
Fred Drakec9680921999-12-13 16:37:25 +00008631#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008632 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008633#endif
Fred Draked86ed291999-12-15 15:34:33 +00008634#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008636#endif
8637#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008638 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008639#endif
Fred Drakec9680921999-12-13 16:37:25 +00008640#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008641 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008642#endif
8643#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008644 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008645#endif
8646#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008647 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008648#endif
8649#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008651#endif
Fred Draked86ed291999-12-15 15:34:33 +00008652#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008653 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008654#endif
Fred Drakec9680921999-12-13 16:37:25 +00008655#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008657#endif
8658#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008660#endif
8661#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008662 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008663#endif
8664#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008665 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008666#endif
8667#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008668 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008669#endif
8670#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008672#endif
8673#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008675#endif
Fred Draked86ed291999-12-15 15:34:33 +00008676#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008677 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008678#endif
Fred Drakec9680921999-12-13 16:37:25 +00008679#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008680 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008681#endif
8682#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008683 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008684#endif
Fred Draked86ed291999-12-15 15:34:33 +00008685#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008686 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008687#endif
Fred Drakec9680921999-12-13 16:37:25 +00008688#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008689 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008690#endif
8691#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008692 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008693#endif
8694#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008696#endif
8697#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008698 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008699#endif
8700#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008702#endif
8703#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008705#endif
8706#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008707 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008708#endif
8709#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008710 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008711#endif
8712#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008713 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008714#endif
Fred Draked86ed291999-12-15 15:34:33 +00008715#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008717#endif
8718#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008720#endif
Fred Drakec9680921999-12-13 16:37:25 +00008721#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008723#endif
8724#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008726#endif
8727#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008729#endif
8730#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008731 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008732#endif
8733#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008735#endif
8736#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008738#endif
8739#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008741#endif
8742#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008744#endif
8745#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008746 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008747#endif
8748#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008749 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008750#endif
8751#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008752 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008753#endif
8754#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008756#endif
8757#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008759#endif
8760#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008762#endif
8763#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008764 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008765#endif
8766#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008768#endif
8769#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008770 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008771#endif
8772#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008774#endif
8775#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008777#endif
8778#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008779 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008780#endif
8781#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008782 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008783#endif
8784#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008785 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008786#endif
8787#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008789#endif
8790#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008791 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008792#endif
8793#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008795#endif
8796#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008797 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008798#endif
8799#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008801#endif
8802#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008804#endif
8805#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008807#endif
8808#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008810#endif
8811#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008813#endif
8814#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008816#endif
8817#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008818 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008819#endif
8820#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008821 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008822#endif
8823#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008825#endif
Fred Draked86ed291999-12-15 15:34:33 +00008826#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008827 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008828#endif
Fred Drakec9680921999-12-13 16:37:25 +00008829#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008831#endif
8832#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008834#endif
8835#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008836 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008837#endif
8838#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008839 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008840#endif
8841#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008842 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008843#endif
8844#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008845 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008846#endif
8847#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008848 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008849#endif
8850#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008851 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008852#endif
8853#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008854 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008855#endif
8856#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008857 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008858#endif
8859#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008860 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008861#endif
8862#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008863 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008864#endif
8865#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008866 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008867#endif
8868#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008869 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008870#endif
8871#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008872 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008873#endif
8874#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008876#endif
8877#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008878 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008879#endif
8880#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008882#endif
8883#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008884 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008885#endif
8886#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008888#endif
8889#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008890 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008891#endif
8892#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008894#endif
8895#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008896 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008897#endif
8898#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008900#endif
8901#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008903#endif
8904#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008905 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008906#endif
8907#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008908 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008909#endif
8910#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008911 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008912#endif
8913#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008914 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008915#endif
8916#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008917 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008918#endif
8919#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008920 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008921#endif
8922#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008923 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008924#endif
8925#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008926 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008927#endif
8928#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008930#endif
8931#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008932 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008933#endif
8934#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008935 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008936#endif
8937#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008938 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008939#endif
8940#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008941 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008942#endif
8943#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008944 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008945#endif
8946#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008948#endif
8949#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008950 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008951#endif
8952#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008953 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008954#endif
8955#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008956 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008957#endif
8958#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008959 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008960#endif
8961#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008962 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008963#endif
8964};
8965
8966static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008967conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008968{
8969 return conv_confname(arg, valuep, posix_constants_sysconf,
8970 sizeof(posix_constants_sysconf)
8971 / sizeof(struct constdef));
8972}
8973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008974PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008975"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008976Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008977
8978static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008979posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008980{
8981 PyObject *result = NULL;
8982 int name;
8983
8984 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8985 int value;
8986
8987 errno = 0;
8988 value = sysconf(name);
8989 if (value == -1 && errno != 0)
8990 posix_error();
8991 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008992 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008993 }
8994 return result;
8995}
8996#endif
8997
8998
Fred Drakebec628d1999-12-15 18:31:10 +00008999/* This code is used to ensure that the tables of configuration value names
9000 * are in sorted order as required by conv_confname(), and also to build the
9001 * the exported dictionaries that are used to publish information about the
9002 * names available on the host platform.
9003 *
9004 * Sorting the table at runtime ensures that the table is properly ordered
9005 * when used, even for platforms we're not able to test on. It also makes
9006 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009007 */
Fred Drakebec628d1999-12-15 18:31:10 +00009008
9009static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009010cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009011{
9012 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009013 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009014 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009015 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009016
9017 return strcmp(c1->name, c2->name);
9018}
9019
9020static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009021setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009022 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009023{
Fred Drakebec628d1999-12-15 18:31:10 +00009024 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009025 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009026
9027 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9028 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009029 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009030 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009031
Barry Warsaw3155db32000-04-13 15:20:40 +00009032 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009033 PyObject *o = PyLong_FromLong(table[i].value);
9034 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9035 Py_XDECREF(o);
9036 Py_DECREF(d);
9037 return -1;
9038 }
9039 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009040 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009041 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009042}
9043
Fred Drakebec628d1999-12-15 18:31:10 +00009044/* Return -1 on failure, 0 on success. */
9045static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009046setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009047{
9048#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009049 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009050 sizeof(posix_constants_pathconf)
9051 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009052 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009053 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009054#endif
9055#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009056 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009057 sizeof(posix_constants_confstr)
9058 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009059 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009060 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009061#endif
9062#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009063 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009064 sizeof(posix_constants_sysconf)
9065 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009066 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009067 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009068#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009069 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009070}
Fred Draked86ed291999-12-15 15:34:33 +00009071
9072
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009073PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009074"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009075Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009076in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009077
9078static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009079posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009080{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009081 abort();
9082 /*NOTREACHED*/
9083 Py_FatalError("abort() called from Python code didn't abort!");
9084 return NULL;
9085}
Fred Drakebec628d1999-12-15 18:31:10 +00009086
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009087#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009088PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009089"startfile(filepath [, operation]) - Start a file with its associated\n\
9090application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009091\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009092When \"operation\" is not specified or \"open\", this acts like\n\
9093double-clicking the file in Explorer, or giving the file name as an\n\
9094argument to the DOS \"start\" command: the file is opened with whatever\n\
9095application (if any) its extension is associated.\n\
9096When another \"operation\" is given, it specifies what should be done with\n\
9097the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009098\n\
9099startfile returns as soon as the associated application is launched.\n\
9100There is no option to wait for the application to close, and no way\n\
9101to retrieve the application's exit status.\n\
9102\n\
9103The filepath is relative to the current directory. If you want to use\n\
9104an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009105the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009106
9107static PyObject *
9108win32_startfile(PyObject *self, PyObject *args)
9109{
Victor Stinner8c62be82010-05-06 00:08:46 +00009110 PyObject *ofilepath;
9111 char *filepath;
9112 char *operation = NULL;
9113 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009114
Victor Stinner8c62be82010-05-06 00:08:46 +00009115 PyObject *unipath, *woperation = NULL;
9116 if (!PyArg_ParseTuple(args, "U|s:startfile",
9117 &unipath, &operation)) {
9118 PyErr_Clear();
9119 goto normal;
9120 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009121
Victor Stinner8c62be82010-05-06 00:08:46 +00009122 if (operation) {
9123 woperation = PyUnicode_DecodeASCII(operation,
9124 strlen(operation), NULL);
9125 if (!woperation) {
9126 PyErr_Clear();
9127 operation = NULL;
9128 goto normal;
9129 }
9130 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009131
Victor Stinner8c62be82010-05-06 00:08:46 +00009132 Py_BEGIN_ALLOW_THREADS
9133 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9134 PyUnicode_AS_UNICODE(unipath),
9135 NULL, NULL, SW_SHOWNORMAL);
9136 Py_END_ALLOW_THREADS
9137
9138 Py_XDECREF(woperation);
9139 if (rc <= (HINSTANCE)32) {
9140 PyObject *errval = win32_error_unicode("startfile",
9141 PyUnicode_AS_UNICODE(unipath));
9142 return errval;
9143 }
9144 Py_INCREF(Py_None);
9145 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009146
9147normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009148 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9149 PyUnicode_FSConverter, &ofilepath,
9150 &operation))
9151 return NULL;
9152 filepath = PyBytes_AsString(ofilepath);
9153 Py_BEGIN_ALLOW_THREADS
9154 rc = ShellExecute((HWND)0, operation, filepath,
9155 NULL, NULL, SW_SHOWNORMAL);
9156 Py_END_ALLOW_THREADS
9157 if (rc <= (HINSTANCE)32) {
9158 PyObject *errval = win32_error("startfile", filepath);
9159 Py_DECREF(ofilepath);
9160 return errval;
9161 }
9162 Py_DECREF(ofilepath);
9163 Py_INCREF(Py_None);
9164 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009165}
9166#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009167
Martin v. Löwis438b5342002-12-27 10:16:42 +00009168#ifdef HAVE_GETLOADAVG
9169PyDoc_STRVAR(posix_getloadavg__doc__,
9170"getloadavg() -> (float, float, float)\n\n\
9171Return the number of processes in the system run queue averaged over\n\
9172the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9173was unobtainable");
9174
9175static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009176posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009177{
9178 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009179 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009180 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9181 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009182 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009183 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009184}
9185#endif
9186
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009187#ifdef MS_WINDOWS
9188
9189PyDoc_STRVAR(win32_urandom__doc__,
9190"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009191Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009192
9193typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9194 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9195 DWORD dwFlags );
9196typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9197 BYTE *pbBuffer );
9198
9199static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009200/* This handle is never explicitly released. Instead, the operating
9201 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009202static HCRYPTPROV hCryptProv = 0;
9203
Tim Peters4ad82172004-08-30 17:02:04 +00009204static PyObject*
9205win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009206{
Victor Stinner8c62be82010-05-06 00:08:46 +00009207 int howMany;
9208 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009209
Victor Stinner8c62be82010-05-06 00:08:46 +00009210 /* Read arguments */
9211 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9212 return NULL;
9213 if (howMany < 0)
9214 return PyErr_Format(PyExc_ValueError,
9215 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009216
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 if (hCryptProv == 0) {
9218 HINSTANCE hAdvAPI32 = NULL;
9219 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009220
Victor Stinner8c62be82010-05-06 00:08:46 +00009221 /* Obtain handle to the DLL containing CryptoAPI
9222 This should not fail */
9223 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9224 if(hAdvAPI32 == NULL)
9225 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009226
Victor Stinner8c62be82010-05-06 00:08:46 +00009227 /* Obtain pointers to the CryptoAPI functions
9228 This will fail on some early versions of Win95 */
9229 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9230 hAdvAPI32,
9231 "CryptAcquireContextA");
9232 if (pCryptAcquireContext == NULL)
9233 return PyErr_Format(PyExc_NotImplementedError,
9234 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009235
Victor Stinner8c62be82010-05-06 00:08:46 +00009236 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9237 hAdvAPI32, "CryptGenRandom");
9238 if (pCryptGenRandom == NULL)
9239 return PyErr_Format(PyExc_NotImplementedError,
9240 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009241
Victor Stinner8c62be82010-05-06 00:08:46 +00009242 /* Acquire context */
9243 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9244 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9245 return win32_error("CryptAcquireContext", NULL);
9246 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009247
Victor Stinner8c62be82010-05-06 00:08:46 +00009248 /* Allocate bytes */
9249 result = PyBytes_FromStringAndSize(NULL, howMany);
9250 if (result != NULL) {
9251 /* Get random data */
9252 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9253 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9254 PyBytes_AS_STRING(result))) {
9255 Py_DECREF(result);
9256 return win32_error("CryptGenRandom", NULL);
9257 }
9258 }
9259 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009260}
9261#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009262
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009263PyDoc_STRVAR(device_encoding__doc__,
9264"device_encoding(fd) -> str\n\n\
9265Return a string describing the encoding of the device\n\
9266if the output is a terminal; else return None.");
9267
9268static PyObject *
9269device_encoding(PyObject *self, PyObject *args)
9270{
Victor Stinner8c62be82010-05-06 00:08:46 +00009271 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009272#if defined(MS_WINDOWS) || defined(MS_WIN64)
9273 UINT cp;
9274#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009275 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9276 return NULL;
9277 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9278 Py_INCREF(Py_None);
9279 return Py_None;
9280 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009281#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009282 if (fd == 0)
9283 cp = GetConsoleCP();
9284 else if (fd == 1 || fd == 2)
9285 cp = GetConsoleOutputCP();
9286 else
9287 cp = 0;
9288 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9289 has no console */
9290 if (cp != 0)
9291 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009292#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 {
9294 char *codeset = nl_langinfo(CODESET);
9295 if (codeset != NULL && codeset[0] != 0)
9296 return PyUnicode_FromString(codeset);
9297 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009298#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009299 Py_INCREF(Py_None);
9300 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009301}
9302
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009303#ifdef __VMS
9304/* Use openssl random routine */
9305#include <openssl/rand.h>
9306PyDoc_STRVAR(vms_urandom__doc__,
9307"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009308Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009309
9310static PyObject*
9311vms_urandom(PyObject *self, PyObject *args)
9312{
Victor Stinner8c62be82010-05-06 00:08:46 +00009313 int howMany;
9314 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009315
Victor Stinner8c62be82010-05-06 00:08:46 +00009316 /* Read arguments */
9317 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9318 return NULL;
9319 if (howMany < 0)
9320 return PyErr_Format(PyExc_ValueError,
9321 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009322
Victor Stinner8c62be82010-05-06 00:08:46 +00009323 /* Allocate bytes */
9324 result = PyBytes_FromStringAndSize(NULL, howMany);
9325 if (result != NULL) {
9326 /* Get random data */
9327 if (RAND_pseudo_bytes((unsigned char*)
9328 PyBytes_AS_STRING(result),
9329 howMany) < 0) {
9330 Py_DECREF(result);
9331 return PyErr_Format(PyExc_ValueError,
9332 "RAND_pseudo_bytes");
9333 }
9334 }
9335 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009336}
9337#endif
9338
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009339#ifdef HAVE_SETRESUID
9340PyDoc_STRVAR(posix_setresuid__doc__,
9341"setresuid(ruid, euid, suid)\n\n\
9342Set the current process's real, effective, and saved user ids.");
9343
9344static PyObject*
9345posix_setresuid (PyObject *self, PyObject *args)
9346{
Victor Stinner8c62be82010-05-06 00:08:46 +00009347 /* We assume uid_t is no larger than a long. */
9348 long ruid, euid, suid;
9349 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9350 return NULL;
9351 if (setresuid(ruid, euid, suid) < 0)
9352 return posix_error();
9353 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009354}
9355#endif
9356
9357#ifdef HAVE_SETRESGID
9358PyDoc_STRVAR(posix_setresgid__doc__,
9359"setresgid(rgid, egid, sgid)\n\n\
9360Set the current process's real, effective, and saved group ids.");
9361
9362static PyObject*
9363posix_setresgid (PyObject *self, PyObject *args)
9364{
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 /* We assume uid_t is no larger than a long. */
9366 long rgid, egid, sgid;
9367 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9368 return NULL;
9369 if (setresgid(rgid, egid, sgid) < 0)
9370 return posix_error();
9371 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009372}
9373#endif
9374
9375#ifdef HAVE_GETRESUID
9376PyDoc_STRVAR(posix_getresuid__doc__,
9377"getresuid() -> (ruid, euid, suid)\n\n\
9378Get tuple of the current process's real, effective, and saved user ids.");
9379
9380static PyObject*
9381posix_getresuid (PyObject *self, PyObject *noargs)
9382{
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 uid_t ruid, euid, suid;
9384 long l_ruid, l_euid, l_suid;
9385 if (getresuid(&ruid, &euid, &suid) < 0)
9386 return posix_error();
9387 /* Force the values into long's as we don't know the size of uid_t. */
9388 l_ruid = ruid;
9389 l_euid = euid;
9390 l_suid = suid;
9391 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009392}
9393#endif
9394
9395#ifdef HAVE_GETRESGID
9396PyDoc_STRVAR(posix_getresgid__doc__,
9397"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009398Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009399
9400static PyObject*
9401posix_getresgid (PyObject *self, PyObject *noargs)
9402{
Victor Stinner8c62be82010-05-06 00:08:46 +00009403 uid_t rgid, egid, sgid;
9404 long l_rgid, l_egid, l_sgid;
9405 if (getresgid(&rgid, &egid, &sgid) < 0)
9406 return posix_error();
9407 /* Force the values into long's as we don't know the size of uid_t. */
9408 l_rgid = rgid;
9409 l_egid = egid;
9410 l_sgid = sgid;
9411 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009412}
9413#endif
9414
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009415/* Posix *at family of functions:
9416 faccessat, fchmodat, fchownat, fstatat, futimesat,
9417 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9418 unlinkat, utimensat, mkfifoat */
9419
9420#ifdef HAVE_FACCESSAT
9421PyDoc_STRVAR(posix_faccessat__doc__,
9422"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9423Like access() but if path is relative, it is taken as relative to dirfd.\n\
9424flags is optional and can be constructed by ORing together zero or more\n\
9425of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9426If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9427is interpreted relative to the current working directory.");
9428
9429static PyObject *
9430posix_faccessat(PyObject *self, PyObject *args)
9431{
9432 PyObject *opath;
9433 char *path;
9434 int mode;
9435 int res;
9436 int dirfd, flags = 0;
9437 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9438 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9439 return NULL;
9440 path = PyBytes_AsString(opath);
9441 Py_BEGIN_ALLOW_THREADS
9442 res = faccessat(dirfd, path, mode, flags);
9443 Py_END_ALLOW_THREADS
9444 Py_DECREF(opath);
9445 return PyBool_FromLong(res == 0);
9446}
9447#endif
9448
9449#ifdef HAVE_FCHMODAT
9450PyDoc_STRVAR(posix_fchmodat__doc__,
9451"fchmodat(dirfd, path, mode, flags=0)\n\n\
9452Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9453flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9454If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9455is interpreted relative to the current working directory.");
9456
9457static PyObject *
9458posix_fchmodat(PyObject *self, PyObject *args)
9459{
9460 int dirfd, mode, res;
9461 int flags = 0;
9462 PyObject *opath;
9463 char *path;
9464
9465 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9466 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9467 return NULL;
9468
9469 path = PyBytes_AsString(opath);
9470
9471 Py_BEGIN_ALLOW_THREADS
9472 res = fchmodat(dirfd, path, mode, flags);
9473 Py_END_ALLOW_THREADS
9474 Py_DECREF(opath);
9475 if (res < 0)
9476 return posix_error();
9477 Py_RETURN_NONE;
9478}
9479#endif /* HAVE_FCHMODAT */
9480
9481#ifdef HAVE_FCHOWNAT
9482PyDoc_STRVAR(posix_fchownat__doc__,
9483"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9484Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9485flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9486If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9487is interpreted relative to the current working directory.");
9488
9489static PyObject *
9490posix_fchownat(PyObject *self, PyObject *args)
9491{
9492 PyObject *opath;
9493 int dirfd, res;
9494 long uid, gid;
9495 int flags = 0;
9496 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009497
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009498 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9499 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9500 return NULL;
9501
9502 path = PyBytes_AsString(opath);
9503
9504 Py_BEGIN_ALLOW_THREADS
9505 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9506 Py_END_ALLOW_THREADS
9507 Py_DECREF(opath);
9508 if (res < 0)
9509 return posix_error();
9510 Py_RETURN_NONE;
9511}
9512#endif /* HAVE_FCHOWNAT */
9513
9514#ifdef HAVE_FSTATAT
9515PyDoc_STRVAR(posix_fstatat__doc__,
9516"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9517Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9518flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9519If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9520is interpreted relative to the current working directory.");
9521
9522static PyObject *
9523posix_fstatat(PyObject *self, PyObject *args)
9524{
9525 PyObject *opath;
9526 char *path;
9527 STRUCT_STAT st;
9528 int dirfd, res, flags = 0;
9529
9530 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9531 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9532 return NULL;
9533 path = PyBytes_AsString(opath);
9534
9535 Py_BEGIN_ALLOW_THREADS
9536 res = fstatat(dirfd, path, &st, flags);
9537 Py_END_ALLOW_THREADS
9538 Py_DECREF(opath);
9539 if (res != 0)
9540 return posix_error();
9541
9542 return _pystat_fromstructstat(&st);
9543}
9544#endif
9545
9546#ifdef HAVE_FUTIMESAT
9547PyDoc_STRVAR(posix_futimesat__doc__,
9548"futimesat(dirfd, path, (atime, mtime))\n\
9549futimesat(dirfd, path, None)\n\n\
9550Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9551If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9552is interpreted relative to the current working directory.");
9553
9554static PyObject *
9555posix_futimesat(PyObject *self, PyObject *args)
9556{
9557 PyObject *opath;
9558 char *path;
9559 int res, dirfd;
9560 PyObject* arg;
9561
9562 struct timeval buf[2];
9563
9564 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9565 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9566 return NULL;
9567 path = PyBytes_AsString(opath);
9568 if (arg == Py_None) {
9569 /* optional time values not given */
9570 Py_BEGIN_ALLOW_THREADS
9571 res = futimesat(dirfd, path, NULL);
9572 Py_END_ALLOW_THREADS
9573 }
9574 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9575 PyErr_SetString(PyExc_TypeError,
9576 "futimesat() arg 3 must be a tuple (atime, mtime)");
9577 Py_DECREF(opath);
9578 return NULL;
9579 }
9580 else {
9581 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009582 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009583 Py_DECREF(opath);
9584 return NULL;
9585 }
9586 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009587 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009588 Py_DECREF(opath);
9589 return NULL;
9590 }
9591 Py_BEGIN_ALLOW_THREADS
9592 res = futimesat(dirfd, path, buf);
9593 Py_END_ALLOW_THREADS
9594 }
9595 Py_DECREF(opath);
9596 if (res < 0) {
9597 return posix_error();
9598 }
9599 Py_RETURN_NONE;
9600}
9601#endif
9602
9603#ifdef HAVE_LINKAT
9604PyDoc_STRVAR(posix_linkat__doc__,
9605"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9606Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9607and if dstpath is relative, it is taken as relative to dstfd.\n\
9608flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9609If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9610srcpath is interpreted relative to the current working directory. This\n\
9611also applies for dstpath.");
9612
9613static PyObject *
9614posix_linkat(PyObject *self, PyObject *args)
9615{
9616 PyObject *osrc, *odst;
9617 char *src, *dst;
9618 int res, srcfd, dstfd;
9619 int flags = 0;
9620
9621 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9622 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9623 return NULL;
9624 src = PyBytes_AsString(osrc);
9625 dst = PyBytes_AsString(odst);
9626 Py_BEGIN_ALLOW_THREADS
9627 res = linkat(srcfd, src, dstfd, dst, flags);
9628 Py_END_ALLOW_THREADS
9629 Py_DECREF(osrc);
9630 Py_DECREF(odst);
9631 if (res < 0)
9632 return posix_error();
9633 Py_RETURN_NONE;
9634}
9635#endif /* HAVE_LINKAT */
9636
9637#ifdef HAVE_MKDIRAT
9638PyDoc_STRVAR(posix_mkdirat__doc__,
9639"mkdirat(dirfd, path, mode=0o777)\n\n\
9640Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9641If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9642is interpreted relative to the current working directory.");
9643
9644static PyObject *
9645posix_mkdirat(PyObject *self, PyObject *args)
9646{
9647 int res, dirfd;
9648 PyObject *opath;
9649 char *path;
9650 int mode = 0777;
9651
9652 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9653 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9654 return NULL;
9655 path = PyBytes_AsString(opath);
9656 Py_BEGIN_ALLOW_THREADS
9657 res = mkdirat(dirfd, path, mode);
9658 Py_END_ALLOW_THREADS
9659 Py_DECREF(opath);
9660 if (res < 0)
9661 return posix_error();
9662 Py_RETURN_NONE;
9663}
9664#endif
9665
9666#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9667PyDoc_STRVAR(posix_mknodat__doc__,
9668"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9669Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9670If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9671is interpreted relative to the current working directory.");
9672
9673static PyObject *
9674posix_mknodat(PyObject *self, PyObject *args)
9675{
9676 PyObject *opath;
9677 char *filename;
9678 int mode = 0600;
9679 int device = 0;
9680 int res, dirfd;
9681 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9682 PyUnicode_FSConverter, &opath, &mode, &device))
9683 return NULL;
9684 filename = PyBytes_AS_STRING(opath);
9685 Py_BEGIN_ALLOW_THREADS
9686 res = mknodat(dirfd, filename, mode, device);
9687 Py_END_ALLOW_THREADS
9688 Py_DECREF(opath);
9689 if (res < 0)
9690 return posix_error();
9691 Py_RETURN_NONE;
9692}
9693#endif
9694
9695#ifdef HAVE_OPENAT
9696PyDoc_STRVAR(posix_openat__doc__,
9697"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9698Like open() but if path is relative, it is taken as relative to dirfd.\n\
9699If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9700is interpreted relative to the current working directory.");
9701
9702static PyObject *
9703posix_openat(PyObject *self, PyObject *args)
9704{
9705 PyObject *ofile;
9706 char *file;
9707 int flag, dirfd, fd;
9708 int mode = 0777;
9709
9710 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9711 &dirfd, PyUnicode_FSConverter, &ofile,
9712 &flag, &mode))
9713 return NULL;
9714 file = PyBytes_AsString(ofile);
9715 Py_BEGIN_ALLOW_THREADS
9716 fd = openat(dirfd, file, flag, mode);
9717 Py_END_ALLOW_THREADS
9718 Py_DECREF(ofile);
9719 if (fd < 0)
9720 return posix_error();
9721 return PyLong_FromLong((long)fd);
9722}
9723#endif
9724
9725#ifdef HAVE_READLINKAT
9726PyDoc_STRVAR(posix_readlinkat__doc__,
9727"readlinkat(dirfd, path) -> path\n\n\
9728Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9729If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9730is interpreted relative to the current working directory.");
9731
9732static PyObject *
9733posix_readlinkat(PyObject *self, PyObject *args)
9734{
9735 PyObject *v, *opath;
9736 char buf[MAXPATHLEN];
9737 char *path;
9738 int n, dirfd;
9739 int arg_is_unicode = 0;
9740
9741 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9742 &dirfd, PyUnicode_FSConverter, &opath))
9743 return NULL;
9744 path = PyBytes_AsString(opath);
9745 v = PySequence_GetItem(args, 1);
9746 if (v == NULL) {
9747 Py_DECREF(opath);
9748 return NULL;
9749 }
9750
9751 if (PyUnicode_Check(v)) {
9752 arg_is_unicode = 1;
9753 }
9754 Py_DECREF(v);
9755
9756 Py_BEGIN_ALLOW_THREADS
9757 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9758 Py_END_ALLOW_THREADS
9759 Py_DECREF(opath);
9760 if (n < 0)
9761 return posix_error();
9762
9763 if (arg_is_unicode)
9764 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9765 else
9766 return PyBytes_FromStringAndSize(buf, n);
9767}
9768#endif /* HAVE_READLINKAT */
9769
9770#ifdef HAVE_RENAMEAT
9771PyDoc_STRVAR(posix_renameat__doc__,
9772"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9773Like rename() but if oldpath is relative, it is taken as relative to\n\
9774olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9775If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9776oldpath is interpreted relative to the current working directory. This\n\
9777also applies for newpath.");
9778
9779static PyObject *
9780posix_renameat(PyObject *self, PyObject *args)
9781{
9782 int res;
9783 PyObject *opathold, *opathnew;
9784 char *opath, *npath;
9785 int oldfd, newfd;
9786
9787 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9788 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9789 return NULL;
9790 opath = PyBytes_AsString(opathold);
9791 npath = PyBytes_AsString(opathnew);
9792 Py_BEGIN_ALLOW_THREADS
9793 res = renameat(oldfd, opath, newfd, npath);
9794 Py_END_ALLOW_THREADS
9795 Py_DECREF(opathold);
9796 Py_DECREF(opathnew);
9797 if (res < 0)
9798 return posix_error();
9799 Py_RETURN_NONE;
9800}
9801#endif
9802
9803#if HAVE_SYMLINKAT
9804PyDoc_STRVAR(posix_symlinkat__doc__,
9805"symlinkat(src, dstfd, dst)\n\n\
9806Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9807If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9808is interpreted relative to the current working directory.");
9809
9810static PyObject *
9811posix_symlinkat(PyObject *self, PyObject *args)
9812{
9813 int res, dstfd;
9814 PyObject *osrc, *odst;
9815 char *src, *dst;
9816
9817 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9818 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9819 return NULL;
9820 src = PyBytes_AsString(osrc);
9821 dst = PyBytes_AsString(odst);
9822 Py_BEGIN_ALLOW_THREADS
9823 res = symlinkat(src, dstfd, dst);
9824 Py_END_ALLOW_THREADS
9825 Py_DECREF(osrc);
9826 Py_DECREF(odst);
9827 if (res < 0)
9828 return posix_error();
9829 Py_RETURN_NONE;
9830}
9831#endif /* HAVE_SYMLINKAT */
9832
9833#ifdef HAVE_UNLINKAT
9834PyDoc_STRVAR(posix_unlinkat__doc__,
9835"unlinkat(dirfd, path, flags=0)\n\n\
9836Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9837flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9838specified, unlinkat() behaves like rmdir().\n\
9839If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9840is interpreted relative to the current working directory.");
9841
9842static PyObject *
9843posix_unlinkat(PyObject *self, PyObject *args)
9844{
9845 int dirfd, res, flags = 0;
9846 PyObject *opath;
9847 char *path;
9848
9849 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9850 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9851 return NULL;
9852 path = PyBytes_AsString(opath);
9853 Py_BEGIN_ALLOW_THREADS
9854 res = unlinkat(dirfd, path, flags);
9855 Py_END_ALLOW_THREADS
9856 Py_DECREF(opath);
9857 if (res < 0)
9858 return posix_error();
9859 Py_RETURN_NONE;
9860}
9861#endif
9862
9863#ifdef HAVE_UTIMENSAT
9864PyDoc_STRVAR(posix_utimensat__doc__,
9865"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9866 (mtime_sec, mtime_nsec), flags)\n\
9867utimensat(dirfd, path, None, None, flags)\n\n\
9868Updates the timestamps of a file with nanosecond precision. If path is\n\
9869relative, it is taken as relative to dirfd.\n\
9870The second form sets atime and mtime to the current time.\n\
9871flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9872If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9873is interpreted relative to the current working directory.\n\
9874If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9875current time.\n\
9876If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9877
9878static PyObject *
9879posix_utimensat(PyObject *self, PyObject *args)
9880{
9881 PyObject *opath;
9882 char *path;
9883 int res, dirfd, flags = 0;
9884 PyObject *atime, *mtime;
9885
9886 struct timespec buf[2];
9887
9888 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9889 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9890 return NULL;
9891 path = PyBytes_AsString(opath);
9892 if (atime == Py_None && mtime == Py_None) {
9893 /* optional time values not given */
9894 Py_BEGIN_ALLOW_THREADS
9895 res = utimensat(dirfd, path, NULL, flags);
9896 Py_END_ALLOW_THREADS
9897 }
9898 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9899 PyErr_SetString(PyExc_TypeError,
9900 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9901 Py_DECREF(opath);
9902 return NULL;
9903 }
9904 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9905 PyErr_SetString(PyExc_TypeError,
9906 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9907 Py_DECREF(opath);
9908 return NULL;
9909 }
9910 else {
9911 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9912 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9913 Py_DECREF(opath);
9914 return NULL;
9915 }
9916 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9917 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9918 Py_DECREF(opath);
9919 return NULL;
9920 }
9921 Py_BEGIN_ALLOW_THREADS
9922 res = utimensat(dirfd, path, buf, flags);
9923 Py_END_ALLOW_THREADS
9924 }
9925 Py_DECREF(opath);
9926 if (res < 0) {
9927 return posix_error();
9928 }
9929 Py_RETURN_NONE;
9930}
9931#endif
9932
9933#ifdef HAVE_MKFIFOAT
9934PyDoc_STRVAR(posix_mkfifoat__doc__,
9935"mkfifoat(dirfd, path, mode=0o666)\n\n\
9936Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9937If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9938is interpreted relative to the current working directory.");
9939
9940static PyObject *
9941posix_mkfifoat(PyObject *self, PyObject *args)
9942{
9943 PyObject *opath;
9944 char *filename;
9945 int mode = 0666;
9946 int res, dirfd;
9947 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9948 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9949 return NULL;
9950 filename = PyBytes_AS_STRING(opath);
9951 Py_BEGIN_ALLOW_THREADS
9952 res = mkfifoat(dirfd, filename, mode);
9953 Py_END_ALLOW_THREADS
9954 Py_DECREF(opath);
9955 if (res < 0)
9956 return posix_error();
9957 Py_RETURN_NONE;
9958}
9959#endif
9960
Benjamin Peterson799bd802011-08-31 22:15:17 -04009961#ifdef HAVE_ATTR_XATTR_H
9962
9963static int
9964try_getxattr(const char *path, const char *name,
9965 ssize_t (*get)(const char *, const char *, void *, size_t),
9966 Py_ssize_t buf_size, PyObject **res)
9967{
9968 PyObject *value;
9969 Py_ssize_t len;
9970
9971 assert(buf_size <= XATTR_SIZE_MAX);
9972 value = PyBytes_FromStringAndSize(NULL, buf_size);
9973 if (!value)
9974 return 0;
9975 Py_BEGIN_ALLOW_THREADS;
9976 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
9977 Py_END_ALLOW_THREADS;
9978 if (len < 0) {
9979 Py_DECREF(value);
9980 if (errno == ERANGE) {
9981 value = NULL;
9982 }
9983 else {
9984 posix_error();
9985 return 0;
9986 }
9987 }
9988 else if (len != buf_size) {
9989 /* Can only shrink. */
9990 _PyBytes_Resize(&value, len);
9991 }
9992 *res = value;
9993 return 1;
9994}
9995
9996static PyObject *
9997getxattr_common(const char *path, PyObject *name_obj,
9998 ssize_t (*get)(const char *, const char *, void *, size_t))
9999{
10000 PyObject *value;
10001 const char *name = PyBytes_AS_STRING(name_obj);
10002
10003 /* Try a small value first. */
10004 if (!try_getxattr(path, name, get, 128, &value))
10005 return NULL;
10006 if (value)
10007 return value;
10008 /* Now the maximum possible one. */
10009 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10010 return NULL;
10011 assert(value);
10012 return value;
10013}
10014
10015PyDoc_STRVAR(posix_getxattr__doc__,
10016"getxattr(path, attr) -> value\n\n\
10017Return the value of extended attribute *name* on *path*.");
10018
10019static PyObject *
10020posix_getxattr(PyObject *self, PyObject *args)
10021{
10022 PyObject *path, *res, *name;
10023
10024 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10025 PyUnicode_FSConverter, &name))
10026 return NULL;
10027 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10028 Py_DECREF(path);
10029 Py_DECREF(name);
10030 return res;
10031}
10032
10033PyDoc_STRVAR(posix_lgetxattr__doc__,
10034"lgetxattr(path, attr) -> value\n\n\
10035Like getxattr but don't follow symlinks.");
10036
10037static PyObject *
10038posix_lgetxattr(PyObject *self, PyObject *args)
10039{
10040 PyObject *path, *res, *name;
10041
10042 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10043 PyUnicode_FSConverter, &name))
10044 return NULL;
10045 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10046 Py_DECREF(path);
10047 Py_DECREF(name);
10048 return res;
10049}
10050
10051static ssize_t
10052wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10053{
10054 /* Hack to share code. */
10055 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10056}
10057
10058PyDoc_STRVAR(posix_fgetxattr__doc__,
10059"fgetxattr(fd, attr) -> value\n\n\
10060Like getxattr but operate on a fd instead of a path.");
10061
10062static PyObject *
10063posix_fgetxattr(PyObject *self, PyObject *args)
10064{
10065 PyObject *res, *name;
10066 int fd;
10067
10068 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10069 return NULL;
10070 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10071 Py_DECREF(name);
10072 return res;
10073}
10074
10075PyDoc_STRVAR(posix_setxattr__doc__,
10076"setxattr(path, attr, value, flags=0)\n\n\
10077Set extended attribute *attr* on *path* to *value*.");
10078
10079static PyObject *
10080posix_setxattr(PyObject *self, PyObject *args)
10081{
10082 PyObject *path, *name;
10083 Py_buffer data;
10084 int flags = 0, err;
10085
10086 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10087 &path, PyUnicode_FSConverter, &name, &data, &flags))
10088 return NULL;
10089 Py_BEGIN_ALLOW_THREADS;
10090 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10091 data.buf, data.len, flags);
10092 Py_END_ALLOW_THREADS;
10093 Py_DECREF(path);
10094 Py_DECREF(name);
10095 PyBuffer_Release(&data);
10096 if (err)
10097 return posix_error();
10098 Py_RETURN_NONE;
10099}
10100
10101PyDoc_STRVAR(posix_lsetxattr__doc__,
10102"lsetxattr(path, attr, value, flags=0)\n\n\
10103Like setxattr but don't follow symlinks.");
10104
10105static PyObject *
10106posix_lsetxattr(PyObject *self, PyObject *args)
10107{
10108 PyObject *path, *name;
10109 Py_buffer data;
10110 int flags = 0, err;
10111
10112 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10113 &path, PyUnicode_FSConverter, &name, &data, &flags))
10114 return NULL;
10115 Py_BEGIN_ALLOW_THREADS;
10116 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10117 data.buf, data.len, flags);
10118 Py_END_ALLOW_THREADS;
10119 Py_DECREF(path);
10120 Py_DECREF(name);
10121 PyBuffer_Release(&data);
10122 if (err)
10123 return posix_error();
10124 Py_RETURN_NONE;
10125}
10126
10127PyDoc_STRVAR(posix_fsetxattr__doc__,
10128"fsetxattr(fd, attr, value, flags=0)\n\n\
10129Like setxattr but operates on *fd* instead of a path.");
10130
10131static PyObject *
10132posix_fsetxattr(PyObject *self, PyObject *args)
10133{
10134 Py_buffer data;
10135 const char *name;
10136 int fd, flags = 0, err;
10137
10138 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10139 &name, &data, &flags))
10140 return NULL;
10141 Py_BEGIN_ALLOW_THREADS;
10142 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10143 Py_END_ALLOW_THREADS;
10144 Py_DECREF(name);
10145 PyBuffer_Release(&data);
10146 if (err)
10147 return posix_error();
10148 Py_RETURN_NONE;
10149}
10150
10151PyDoc_STRVAR(posix_removexattr__doc__,
10152"removexattr(path, attr)\n\n\
10153Remove extended attribute *attr* on *path*.");
10154
10155static PyObject *
10156posix_removexattr(PyObject *self, PyObject *args)
10157{
10158 PyObject *path, *name;
10159 int err;
10160
10161 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10162 PyUnicode_FSConverter, &name))
10163 return NULL;
10164 Py_BEGIN_ALLOW_THREADS;
10165 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10166 Py_END_ALLOW_THREADS;
10167 Py_DECREF(path);
10168 Py_DECREF(name);
10169 if (err)
10170 return posix_error();
10171 Py_RETURN_NONE;
10172}
10173
10174PyDoc_STRVAR(posix_lremovexattr__doc__,
10175"lremovexattr(path, attr)\n\n\
10176Like removexattr but don't follow symlinks.");
10177
10178static PyObject *
10179posix_lremovexattr(PyObject *self, PyObject *args)
10180{
10181 PyObject *path, *name;
10182 int err;
10183
10184 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10185 PyUnicode_FSConverter, &name))
10186 return NULL;
10187 Py_BEGIN_ALLOW_THREADS;
10188 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10189 Py_END_ALLOW_THREADS;
10190 Py_DECREF(path);
10191 Py_DECREF(name);
10192 if (err)
10193 return posix_error();
10194 Py_RETURN_NONE;
10195}
10196
10197PyDoc_STRVAR(posix_fremovexattr__doc__,
10198"fremovexattr(fd, attr)\n\n\
10199Like removexattr but operates on a file descriptor.");
10200
10201static PyObject *
10202posix_fremovexattr(PyObject *self, PyObject *args)
10203{
10204 PyObject *name;
10205 int fd, err;
10206
10207 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10208 PyUnicode_FSConverter, &name))
10209 return NULL;
10210 Py_BEGIN_ALLOW_THREADS;
10211 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10212 Py_END_ALLOW_THREADS;
10213 Py_DECREF(name);
10214 if (err)
10215 return posix_error();
10216 Py_RETURN_NONE;
10217}
10218
10219static Py_ssize_t
10220try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10221 Py_ssize_t buf_size, char **buf)
10222{
10223 Py_ssize_t len;
10224
10225 *buf = PyMem_MALLOC(buf_size);
10226 if (!*buf) {
10227 PyErr_NoMemory();
10228 return -1;
10229 }
10230 Py_BEGIN_ALLOW_THREADS;
10231 len = list(path, *buf, buf_size);
10232 Py_END_ALLOW_THREADS;
10233 if (len < 0) {
10234 PyMem_FREE(*buf);
10235 if (errno != ERANGE)
10236 posix_error();
10237 return -1;
10238 }
10239 return len;
10240}
10241
10242static PyObject *
10243listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10244{
10245 PyObject *res, *attr;
10246 Py_ssize_t len, err, start, i;
10247 char *buf;
10248
10249 len = try_listxattr(path, list, 256, &buf);
10250 if (len < 0) {
10251 if (PyErr_Occurred())
10252 return NULL;
10253 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10254 if (len < 0)
10255 return NULL;
10256 }
10257 res = PyList_New(0);
10258 if (!res) {
10259 PyMem_FREE(buf);
10260 return NULL;
10261 }
10262 for (start = i = 0; i < len; i++) {
10263 if (!buf[i]) {
10264 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10265 if (!attr) {
10266 Py_DECREF(res);
10267 PyMem_FREE(buf);
10268 return NULL;
10269 }
10270 err = PyList_Append(res, attr);
10271 Py_DECREF(attr);
10272 if (err) {
10273 Py_DECREF(res);
10274 PyMem_FREE(buf);
10275 return NULL;
10276 }
10277 start = i + 1;
10278 }
10279 }
10280 PyMem_FREE(buf);
10281 return res;
10282}
10283
10284PyDoc_STRVAR(posix_listxattr__doc__,
10285"listxattr(path)\n\n\
10286Return a list of extended attributes on *path*.");
10287
10288static PyObject *
10289posix_listxattr(PyObject *self, PyObject *args)
10290{
10291 PyObject *path, *res;
10292
10293 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10294 return NULL;
10295 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10296 Py_DECREF(path);
10297 return res;
10298}
10299
10300PyDoc_STRVAR(posix_llistxattr__doc__,
10301"llistxattr(path)\n\n\
10302Like listxattr but don't follow symlinks..");
10303
10304static PyObject *
10305posix_llistxattr(PyObject *self, PyObject *args)
10306{
10307 PyObject *path, *res;
10308
10309 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10310 return NULL;
10311 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10312 Py_DECREF(path);
10313 return res;
10314}
10315
10316static ssize_t
10317wrap_flistxattr(const char *path, char *buf, size_t len)
10318{
10319 /* Hack to share code. */
10320 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10321}
10322
10323PyDoc_STRVAR(posix_flistxattr__doc__,
10324"flistxattr(path)\n\n\
10325Like flistxattr but operates on a file descriptor.");
10326
10327static PyObject *
10328posix_flistxattr(PyObject *self, PyObject *args)
10329{
10330 long fd;
10331
10332 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10333 return NULL;
10334 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10335}
10336
10337#endif /* HAVE_ATTR_XATTR_H */
10338
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010339static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010340 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010341#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010342 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010343#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010344 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010345#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010346 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010347#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010348 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010349#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010350 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010351#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010352#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010353 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010354#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010355#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010356 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010357#endif /* HAVE_LCHMOD */
10358#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010359 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010360#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010361#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010362 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010363#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010364#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010365 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010366#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010367#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010368 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010369#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010370#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010371 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010372#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +000010373#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010374 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10375 METH_NOARGS, posix_getcwd__doc__},
10376 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10377 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +000010378#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010379#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010380 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010381#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010382 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010383#ifdef HAVE_FDOPENDIR
10384 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10385#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010386 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10387 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010388#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010389 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010390#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010391#ifdef HAVE_GETPRIORITY
10392 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10393#endif /* HAVE_GETPRIORITY */
10394#ifdef HAVE_SETPRIORITY
10395 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10396#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010397#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010398 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010399#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010400#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010401 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010402#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010403 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10404 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10405 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010406 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010407#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010408 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010409#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010410#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010411 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010412 win_symlink__doc__},
10413#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010414#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010415 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010416#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010417 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010418#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010419 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010420#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010421 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10422 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10423 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010424#ifdef HAVE_FUTIMES
10425 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10426#endif
10427#ifdef HAVE_LUTIMES
10428 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10429#endif
10430#ifdef HAVE_FUTIMENS
10431 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10432#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010433#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010434 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010435#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010436 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010437#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010438 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10439 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010440#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010441#ifdef HAVE_FEXECVE
10442 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10443#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010444#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010445 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10446 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010447#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010448 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10449 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010450#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010451#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010452#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010453 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010454#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010455#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010456 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010457#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010458#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010459#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010460 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10461 {"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 +020010462#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010463#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010464 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010465#endif
10466#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010467 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010468#endif
10469#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010470 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010471#endif
10472#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010473 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010474#endif
10475#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010476 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010477#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010478 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010479#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010480 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10481 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10482#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010483#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010484#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010485 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010486#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010487#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010489#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010490#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010492#endif /* HAVE_GETEGID */
10493#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010495#endif /* HAVE_GETEUID */
10496#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010498#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010499#ifdef HAVE_GETGROUPLIST
10500 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10501#endif
Fred Drakec9680921999-12-13 16:37:25 +000010502#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010504#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010505 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010506#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010507 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010508#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010509#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010510 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010511#endif /* HAVE_GETPPID */
10512#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010513 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010514#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010515#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010516 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010517#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010518#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010520#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010521#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010523#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010524#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010525 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010526#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010527#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010528 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10529 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010530 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010531#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010532#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010534#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010535#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010537#endif /* HAVE_SETEUID */
10538#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010539 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010540#endif /* HAVE_SETEGID */
10541#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010543#endif /* HAVE_SETREUID */
10544#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010546#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010547#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010549#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010550#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010552#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010553#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010554 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010555#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010556#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010557 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010558#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010559#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010560 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010561#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010562#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010563 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010564#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010565#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010567#endif /* HAVE_WAIT3 */
10568#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010570#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010571#if defined(HAVE_WAITID) && !defined(__APPLE__)
10572 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10573#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010574#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010576#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010577#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010579#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010580#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010582#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010583#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010585#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010586#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010588#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010589#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010591#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010592 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10593 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10594 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10595 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10596 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10597 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010598#ifdef HAVE_LOCKF
10599 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10600#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010601 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10602 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010603#ifdef HAVE_READV
10604 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10605#endif
10606#ifdef HAVE_PREAD
10607 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10608#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010610#ifdef HAVE_WRITEV
10611 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10612#endif
10613#ifdef HAVE_PWRITE
10614 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10615#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010616#ifdef HAVE_SENDFILE
10617 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10618 posix_sendfile__doc__},
10619#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10621 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010622#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010623 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010624#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010625#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010626 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010627#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010628#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010630#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010631#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010632 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010633#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010634#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010635 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10636 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10637 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010638#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010639#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010640 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010641#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010642#ifdef HAVE_TRUNCATE
10643 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10644#endif
10645#ifdef HAVE_POSIX_FALLOCATE
10646 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10647#endif
10648#ifdef HAVE_POSIX_FADVISE
10649 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10650#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010651#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010653#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010654#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010655 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010656#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010657 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010658#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010660#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010661#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010663#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010664#ifdef HAVE_SYNC
10665 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10666#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010667#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010669#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010670#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010671#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010672 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010673#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010674#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010675 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010676#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010677#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010679#endif /* WIFSTOPPED */
10680#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010682#endif /* WIFSIGNALED */
10683#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010684 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010685#endif /* WIFEXITED */
10686#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010687 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010688#endif /* WEXITSTATUS */
10689#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010690 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010691#endif /* WTERMSIG */
10692#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010693 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010694#endif /* WSTOPSIG */
10695#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010696#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010698#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010699#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010701#endif
Fred Drakec9680921999-12-13 16:37:25 +000010702#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010704#endif
10705#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010707#endif
10708#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010710#endif
10711#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010712 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010713#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010715#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010717 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010718 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010719 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010720 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010721#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010722#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010723 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010724#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010725 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010726 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010727 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010728 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010729 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010730 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010731#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010732 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010733#endif
10734#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010735 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010736#endif
10737#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010738 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010739#endif
10740#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010741 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010742#endif
10743
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010744/* posix *at family of functions */
10745#ifdef HAVE_FACCESSAT
10746 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10747#endif
10748#ifdef HAVE_FCHMODAT
10749 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10750#endif /* HAVE_FCHMODAT */
10751#ifdef HAVE_FCHOWNAT
10752 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10753#endif /* HAVE_FCHOWNAT */
10754#ifdef HAVE_FSTATAT
10755 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10756#endif
10757#ifdef HAVE_FUTIMESAT
10758 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10759#endif
10760#ifdef HAVE_LINKAT
10761 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10762#endif /* HAVE_LINKAT */
10763#ifdef HAVE_MKDIRAT
10764 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10765#endif
10766#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10767 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10768#endif
10769#ifdef HAVE_OPENAT
10770 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10771#endif
10772#ifdef HAVE_READLINKAT
10773 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10774#endif /* HAVE_READLINKAT */
10775#ifdef HAVE_RENAMEAT
10776 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10777#endif
10778#if HAVE_SYMLINKAT
10779 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10780#endif /* HAVE_SYMLINKAT */
10781#ifdef HAVE_UNLINKAT
10782 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10783#endif
10784#ifdef HAVE_UTIMENSAT
10785 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10786#endif
10787#ifdef HAVE_MKFIFOAT
10788 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10789#endif
Benjamin Peterson799bd802011-08-31 22:15:17 -040010790#ifdef HAVE_ATTR_XATTR_H
10791 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
10792 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
10793 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
10794 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
10795 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
10796 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
10797 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
10798 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
10799 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
10800 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
10801 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
10802 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
10803#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010804 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010805};
10806
10807
Barry Warsaw4a342091996-12-19 23:50:02 +000010808static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010809ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010810{
Victor Stinner8c62be82010-05-06 00:08:46 +000010811 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010812}
10813
Guido van Rossumd48f2521997-12-05 22:19:34 +000010814#if defined(PYOS_OS2)
10815/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010816static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010817{
10818 APIRET rc;
10819 ULONG values[QSV_MAX+1];
10820 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010821 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010822
10823 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010824 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010825 Py_END_ALLOW_THREADS
10826
10827 if (rc != NO_ERROR) {
10828 os2_error(rc);
10829 return -1;
10830 }
10831
Fred Drake4d1e64b2002-04-15 19:40:07 +000010832 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10833 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10834 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10835 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10836 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10837 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10838 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010839
10840 switch (values[QSV_VERSION_MINOR]) {
10841 case 0: ver = "2.00"; break;
10842 case 10: ver = "2.10"; break;
10843 case 11: ver = "2.11"; break;
10844 case 30: ver = "3.00"; break;
10845 case 40: ver = "4.00"; break;
10846 case 50: ver = "5.00"; break;
10847 default:
Tim Peters885d4572001-11-28 20:27:42 +000010848 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010849 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010850 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010851 ver = &tmp[0];
10852 }
10853
10854 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010855 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010856 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010857
10858 /* Add Indicator of Which Drive was Used to Boot the System */
10859 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10860 tmp[1] = ':';
10861 tmp[2] = '\0';
10862
Fred Drake4d1e64b2002-04-15 19:40:07 +000010863 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010864}
10865#endif
10866
Brian Curtin52173d42010-12-02 18:29:18 +000010867#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010868static int
Brian Curtin52173d42010-12-02 18:29:18 +000010869enable_symlink()
10870{
10871 HANDLE tok;
10872 TOKEN_PRIVILEGES tok_priv;
10873 LUID luid;
10874 int meth_idx = 0;
10875
10876 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010877 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010878
10879 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010880 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010881
10882 tok_priv.PrivilegeCount = 1;
10883 tok_priv.Privileges[0].Luid = luid;
10884 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10885
10886 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10887 sizeof(TOKEN_PRIVILEGES),
10888 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010889 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010890
Brian Curtin3b4499c2010-12-28 14:31:47 +000010891 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10892 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010893}
10894#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10895
Barry Warsaw4a342091996-12-19 23:50:02 +000010896static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010897all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010898{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010899#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010900 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010901#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010902#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010903 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010904#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010905#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010906 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010907#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010908#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010909 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010910#endif
Fred Drakec9680921999-12-13 16:37:25 +000010911#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010912 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010913#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010914#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010915 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010916#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010917#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010918 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010919#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010920#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000010921 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010922#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010923#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000010924 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010925#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010926#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010927 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010928#endif
10929#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010930 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010931#endif
10932#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000010933 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010934#endif
10935#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000010936 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010937#endif
10938#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010939 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010940#endif
10941#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000010942 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010943#endif
10944#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010945 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010946#endif
10947#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010948 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010949#endif
10950#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010951 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010952#endif
10953#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010954 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010955#endif
10956#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010957 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010958#endif
10959#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000010960 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010961#endif
10962#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010963 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010964#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000010965#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010966 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010967#endif
10968#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000010969 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010970#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010971#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010972 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010973#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000010974#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010975 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010976#endif
10977#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010978 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010979#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010980#ifdef PRIO_PROCESS
10981 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
10982#endif
10983#ifdef PRIO_PGRP
10984 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
10985#endif
10986#ifdef PRIO_USER
10987 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
10988#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010989#ifdef O_CLOEXEC
10990 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
10991#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010992/* posix - constants for *at functions */
10993#ifdef AT_SYMLINK_NOFOLLOW
10994 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
10995#endif
10996#ifdef AT_EACCESS
10997 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
10998#endif
10999#ifdef AT_FDCWD
11000 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11001#endif
11002#ifdef AT_REMOVEDIR
11003 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11004#endif
11005#ifdef AT_SYMLINK_FOLLOW
11006 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11007#endif
11008#ifdef UTIME_NOW
11009 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11010#endif
11011#ifdef UTIME_OMIT
11012 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11013#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011014
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011015
Tim Peters5aa91602002-01-30 05:46:57 +000011016/* MS Windows */
11017#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 /* Don't inherit in child processes. */
11019 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011020#endif
11021#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011022 /* Optimize for short life (keep in memory). */
11023 /* MS forgot to define this one with a non-underscore form too. */
11024 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011025#endif
11026#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 /* Automatically delete when last handle is closed. */
11028 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011029#endif
11030#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011031 /* Optimize for random access. */
11032 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011033#endif
11034#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011035 /* Optimize for sequential access. */
11036 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011037#endif
11038
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011039/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011040#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011041 /* Send a SIGIO signal whenever input or output
11042 becomes available on file descriptor */
11043 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011044#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011045#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011046 /* Direct disk access. */
11047 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011048#endif
11049#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011050 /* Must be a directory. */
11051 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011052#endif
11053#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 /* Do not follow links. */
11055 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011056#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011057#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011058 /* Do not update the access time. */
11059 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011060#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011061
Victor Stinner8c62be82010-05-06 00:08:46 +000011062 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011063#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011064 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011065#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011066#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011068#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011069#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011071#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011072#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011074#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011075#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011077#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011078#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011080#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011081#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011083#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011084#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011086#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011087#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011089#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011090#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011092#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011093#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011095#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011096#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011098#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011099#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011101#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011102#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011104#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011105#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011107#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011108#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011110#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011111#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011113#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011114
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011115 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011116#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011117 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011118#endif /* ST_RDONLY */
11119#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011120 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011121#endif /* ST_NOSUID */
11122
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011123 /* FreeBSD sendfile() constants */
11124#ifdef SF_NODISKIO
11125 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11126#endif
11127#ifdef SF_MNOWAIT
11128 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11129#endif
11130#ifdef SF_SYNC
11131 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11132#endif
11133
Ross Lagerwall7807c352011-03-17 20:20:30 +020011134 /* constants for posix_fadvise */
11135#ifdef POSIX_FADV_NORMAL
11136 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11137#endif
11138#ifdef POSIX_FADV_SEQUENTIAL
11139 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11140#endif
11141#ifdef POSIX_FADV_RANDOM
11142 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11143#endif
11144#ifdef POSIX_FADV_NOREUSE
11145 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11146#endif
11147#ifdef POSIX_FADV_WILLNEED
11148 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11149#endif
11150#ifdef POSIX_FADV_DONTNEED
11151 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11152#endif
11153
11154 /* constants for waitid */
11155#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11156 if (ins(d, "P_PID", (long)P_PID)) return -1;
11157 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11158 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11159#endif
11160#ifdef WEXITED
11161 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11162#endif
11163#ifdef WNOWAIT
11164 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11165#endif
11166#ifdef WSTOPPED
11167 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11168#endif
11169#ifdef CLD_EXITED
11170 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11171#endif
11172#ifdef CLD_DUMPED
11173 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11174#endif
11175#ifdef CLD_TRAPPED
11176 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11177#endif
11178#ifdef CLD_CONTINUED
11179 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11180#endif
11181
11182 /* constants for lockf */
11183#ifdef F_LOCK
11184 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11185#endif
11186#ifdef F_TLOCK
11187 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11188#endif
11189#ifdef F_ULOCK
11190 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11191#endif
11192#ifdef F_TEST
11193 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11194#endif
11195
11196 /* constants for futimens */
11197#ifdef UTIME_NOW
11198 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11199#endif
11200#ifdef UTIME_OMIT
11201 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11202#endif
11203
Guido van Rossum246bc171999-02-01 23:54:31 +000011204#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011205#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011206 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11207 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11208 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11209 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11210 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11211 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11212 if (ins(d, "P_PM", (long)P_PM)) return -1;
11213 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11214 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11215 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11216 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11217 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11218 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11219 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11220 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11221 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11222 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11223 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11224 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11225 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011226#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011227 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11228 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11229 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11230 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11231 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011232#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011233#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011234
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011235#ifdef HAVE_SCHED_H
11236 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11237 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11238#ifdef SCHED_SPORADIC
11239 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11240#endif
11241 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
11242#ifdef SCHED_BATCH
11243 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11244#endif
11245#ifdef SCHED_IDLE
11246 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11247#endif
11248#ifdef SCHED_RESET_ON_FORK
11249 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11250#endif
11251#endif
11252
Benjamin Peterson799bd802011-08-31 22:15:17 -040011253#ifdef HAVE_ATTR_XATTR_H
11254 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11255 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11256 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11257#endif
11258
Guido van Rossumd48f2521997-12-05 22:19:34 +000011259#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011260 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011261#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011263}
11264
11265
Tim Peters5aa91602002-01-30 05:46:57 +000011266#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011267#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011268#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011269
11270#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011271#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011272#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011273
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011274#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011275#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011276#define MODNAME "posix"
11277#endif
11278
Martin v. Löwis1a214512008-06-11 05:26:20 +000011279static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011280 PyModuleDef_HEAD_INIT,
11281 MODNAME,
11282 posix__doc__,
11283 -1,
11284 posix_methods,
11285 NULL,
11286 NULL,
11287 NULL,
11288 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011289};
11290
11291
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011292PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011293INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011294{
Victor Stinner8c62be82010-05-06 00:08:46 +000011295 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011296
Brian Curtin52173d42010-12-02 18:29:18 +000011297#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011298 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011299#endif
11300
Victor Stinner8c62be82010-05-06 00:08:46 +000011301 m = PyModule_Create(&posixmodule);
11302 if (m == NULL)
11303 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011304
Victor Stinner8c62be82010-05-06 00:08:46 +000011305 /* Initialize environ dictionary */
11306 v = convertenviron();
11307 Py_XINCREF(v);
11308 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11309 return NULL;
11310 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011311
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 if (all_ins(m))
11313 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011314
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 if (setup_confname_tables(m))
11316 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011317
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 Py_INCREF(PyExc_OSError);
11319 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011320
Benjamin Peterson2740af82011-08-02 17:41:34 -050011321#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011322 if (PyType_Ready(&cpu_set_type) < 0)
11323 return NULL;
11324 Py_INCREF(&cpu_set_type);
11325 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011326#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011327
Guido van Rossumb3d39562000-01-31 18:41:26 +000011328#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011329 if (posix_putenv_garbage == NULL)
11330 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011331#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011332
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011334#if defined(HAVE_WAITID) && !defined(__APPLE__)
11335 waitid_result_desc.name = MODNAME ".waitid_result";
11336 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11337#endif
11338
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 stat_result_desc.name = MODNAME ".stat_result";
11340 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11341 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11342 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11343 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11344 structseq_new = StatResultType.tp_new;
11345 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011346
Victor Stinner8c62be82010-05-06 00:08:46 +000011347 statvfs_result_desc.name = MODNAME ".statvfs_result";
11348 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011349#ifdef NEED_TICKS_PER_SECOND
11350# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011352# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011353 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011354# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011355 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011356# endif
11357#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011358
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011359#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011360 sched_param_desc.name = MODNAME ".sched_param";
11361 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11362 SchedParamType.tp_new = sched_param_new;
11363#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011364 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011365#if defined(HAVE_WAITID) && !defined(__APPLE__)
11366 Py_INCREF((PyObject*) &WaitidResultType);
11367 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11368#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011369 Py_INCREF((PyObject*) &StatResultType);
11370 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11371 Py_INCREF((PyObject*) &StatVFSResultType);
11372 PyModule_AddObject(m, "statvfs_result",
11373 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011374
11375#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011376 Py_INCREF(&SchedParamType);
11377 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011378#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011379 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011380
11381#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011382 /*
11383 * Step 2 of weak-linking support on Mac OS X.
11384 *
11385 * The code below removes functions that are not available on the
11386 * currently active platform.
11387 *
11388 * This block allow one to use a python binary that was build on
11389 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11390 * OSX 10.4.
11391 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011392#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011393 if (fstatvfs == NULL) {
11394 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11395 return NULL;
11396 }
11397 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011398#endif /* HAVE_FSTATVFS */
11399
11400#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011401 if (statvfs == NULL) {
11402 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11403 return NULL;
11404 }
11405 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011406#endif /* HAVE_STATVFS */
11407
11408# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011409 if (lchown == NULL) {
11410 if (PyObject_DelAttrString(m, "lchown") == -1) {
11411 return NULL;
11412 }
11413 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011414#endif /* HAVE_LCHOWN */
11415
11416
11417#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011418 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011419
Guido van Rossumb6775db1994-08-01 11:34:53 +000011420}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011421
11422#ifdef __cplusplus
11423}
11424#endif