blob: ebddbaa531288db0aafd26d6be7978cb3ba0e2f7 [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
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000110#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
111#ifdef HAVE_SYS_SOCKET_H
112#include <sys/socket.h>
113#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000114#endif
115
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000117/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000118#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000119#include <process.h>
120#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000121#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000122#define HAVE_GETCWD 1
123#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000124#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#if defined(__OS2__)
126#define HAVE_EXECV 1
127#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000128#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000129#include <process.h>
130#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000131#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#define HAVE_EXECV 1
133#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#define HAVE_OPENDIR 1
135#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000136#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#define HAVE_WAIT 1
138#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000140#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000141#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000142#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000143#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000144#define HAVE_EXECV 1
145#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000146#define HAVE_SYSTEM 1
147#define HAVE_CWAIT 1
148#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000149#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000150#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000151#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
152/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154/* Unix functions that the configure script doesn't check for */
155#define HAVE_EXECV 1
156#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000157#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000158#define HAVE_FORK1 1
159#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#define HAVE_GETCWD 1
161#define HAVE_GETEGID 1
162#define HAVE_GETEUID 1
163#define HAVE_GETGID 1
164#define HAVE_GETPPID 1
165#define HAVE_GETUID 1
166#define HAVE_KILL 1
167#define HAVE_OPENDIR 1
168#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000169#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000170#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000171#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000172#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#endif /* _MSC_VER */
174#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000175#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000177
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000179
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000180#if defined(__sgi)&&_COMPILER_VERSION>=700
181/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
182 (default) */
183extern char *ctermid_r(char *);
184#endif
185
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000186#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000187#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000189#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000190#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000191extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000193extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000194#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000195#endif
196#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000197extern int chdir(char *);
198extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000199#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000200extern int chdir(const char *);
201extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000202#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000203#ifdef __BORLANDC__
204extern int chmod(const char *, int);
205#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000207#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000208/*#ifdef HAVE_FCHMOD
209extern int fchmod(int, mode_t);
210#endif*/
211/*#ifdef HAVE_LCHMOD
212extern int lchmod(const char *, mode_t);
213#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000214extern int chown(const char *, uid_t, gid_t);
215extern char *getcwd(char *, int);
216extern char *strerror(int);
217extern int link(const char *, const char *);
218extern int rename(const char *, const char *);
219extern int stat(const char *, struct stat *);
220extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000222extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000223#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000225extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000226#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000228
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000229#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#ifdef HAVE_UTIME_H
232#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000233#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000235#ifdef HAVE_SYS_UTIME_H
236#include <sys/utime.h>
237#define HAVE_UTIME_H /* pretend we do for the rest of this file */
238#endif /* HAVE_SYS_UTIME_H */
239
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#ifdef HAVE_SYS_TIMES_H
241#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000242#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243
244#ifdef HAVE_SYS_PARAM_H
245#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000246#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
248#ifdef HAVE_SYS_UTSNAME_H
249#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000250#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000254#define NAMLEN(dirent) strlen((dirent)->d_name)
255#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000256#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000257#include <direct.h>
258#define NAMLEN(dirent) strlen((dirent)->d_name)
259#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000261#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000262#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000263#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000265#endif
266#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000268#endif
269#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000271#endif
272#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000274#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#endif
278#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280#endif
281#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000284#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000285#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000286#endif
287#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000288#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000289#endif
290#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000291#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000292#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000293#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000294#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000296#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000297#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000298#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
299#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000300static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000301#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000302#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303
Guido van Rossumd48f2521997-12-05 22:19:34 +0000304#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000306#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307
Tim Petersbc2e10e2002-03-03 23:17:02 +0000308#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309#if defined(PATH_MAX) && PATH_MAX > 1024
310#define MAXPATHLEN PATH_MAX
311#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000313#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000314#endif /* MAXPATHLEN */
315
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000316#ifdef UNION_WAIT
317/* Emulate some macros on systems that have a union instead of macros */
318
319#ifndef WIFEXITED
320#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
321#endif
322
323#ifndef WEXITSTATUS
324#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
325#endif
326
327#ifndef WTERMSIG
328#define WTERMSIG(u_wait) ((u_wait).w_termsig)
329#endif
330
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331#define WAIT_TYPE union wait
332#define WAIT_STATUS_INT(s) (s.w_status)
333
334#else /* !UNION_WAIT */
335#define WAIT_TYPE int
336#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000337#endif /* UNION_WAIT */
338
Greg Wardb48bc172000-03-01 21:51:56 +0000339/* Don't use the "_r" form if we don't need it (also, won't have a
340 prototype for it, at least on Solaris -- maybe others as well?). */
341#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
342#define USE_CTERMID_R
343#endif
344
Fred Drake699f3522000-06-29 21:12:41 +0000345/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000346#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000347#undef FSTAT
348#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000349#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000350# define STAT win32_stat
351# define FSTAT win32_fstat
352# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000353#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000354# define STAT stat
355# define FSTAT fstat
356# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000357#endif
358
Tim Peters11b23062003-04-23 02:39:17 +0000359#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000360#include <sys/mkdev.h>
361#else
362#if defined(MAJOR_IN_SYSMACROS)
363#include <sys/sysmacros.h>
364#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000365#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
366#include <sys/mkdev.h>
367#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000368#endif
Fred Drake699f3522000-06-29 21:12:41 +0000369
Georg Brandla391b112011-02-25 15:23:18 +0000370static int
371_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000372{
373#if !defined(HAVE_LARGEFILE_SUPPORT)
374 *((off_t*)addr) = PyLong_AsLong(arg);
375#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000376 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000377#endif
378 if (PyErr_Occurred())
379 return 0;
380 return 1;
381}
382
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000383#if defined _MSC_VER && _MSC_VER >= 1400
384/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
385 * valid and throw an assertion if it isn't.
386 * Normally, an invalid fd is likely to be a C program error and therefore
387 * an assertion can be useful, but it does contradict the POSIX standard
388 * which for write(2) states:
389 * "Otherwise, -1 shall be returned and errno set to indicate the error."
390 * "[EBADF] The fildes argument is not a valid file descriptor open for
391 * writing."
392 * Furthermore, python allows the user to enter any old integer
393 * as a fd and should merely raise a python exception on error.
394 * The Microsoft CRT doesn't provide an official way to check for the
395 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000396 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000397 * internal structures involved.
398 * The structures below must be updated for each version of visual studio
399 * according to the file internal.h in the CRT source, until MS comes
400 * up with a less hacky way to do this.
401 * (all of this is to avoid globally modifying the CRT behaviour using
402 * _set_invalid_parameter_handler() and _CrtSetReportMode())
403 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000404/* The actual size of the structure is determined at runtime.
405 * Only the first items must be present.
406 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000407typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000408 intptr_t osfhnd;
409 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000410} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000411
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000412extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000413#define IOINFO_L2E 5
414#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
415#define IOINFO_ARRAYS 64
416#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
417#define FOPEN 0x01
418#define _NO_CONSOLE_FILENO (intptr_t)-2
419
420/* This function emulates what the windows CRT does to validate file handles */
421int
422_PyVerify_fd(int fd)
423{
Victor Stinner8c62be82010-05-06 00:08:46 +0000424 const int i1 = fd >> IOINFO_L2E;
425 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000426
Antoine Pitrou22e41552010-08-15 18:07:50 +0000427 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000428
Victor Stinner8c62be82010-05-06 00:08:46 +0000429 /* Determine the actual size of the ioinfo structure,
430 * as used by the CRT loaded in memory
431 */
432 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
433 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
434 }
435 if (sizeof_ioinfo == 0) {
436 /* This should not happen... */
437 goto fail;
438 }
439
440 /* See that it isn't a special CLEAR fileno */
441 if (fd != _NO_CONSOLE_FILENO) {
442 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
443 * we check pointer validity and other info
444 */
445 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
446 /* finally, check that the file is open */
447 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
448 if (info->osfile & FOPEN) {
449 return 1;
450 }
451 }
452 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000453 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000454 errno = EBADF;
455 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000456}
457
458/* the special case of checking dup2. The target fd must be in a sensible range */
459static int
460_PyVerify_fd_dup2(int fd1, int fd2)
461{
Victor Stinner8c62be82010-05-06 00:08:46 +0000462 if (!_PyVerify_fd(fd1))
463 return 0;
464 if (fd2 == _NO_CONSOLE_FILENO)
465 return 0;
466 if ((unsigned)fd2 < _NHANDLE_)
467 return 1;
468 else
469 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000470}
471#else
472/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
473#define _PyVerify_fd_dup2(A, B) (1)
474#endif
475
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000476#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000477/* The following structure was copied from
478 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
479 include doesn't seem to be present in the Windows SDK (at least as included
480 with Visual Studio Express). */
481typedef struct _REPARSE_DATA_BUFFER {
482 ULONG ReparseTag;
483 USHORT ReparseDataLength;
484 USHORT Reserved;
485 union {
486 struct {
487 USHORT SubstituteNameOffset;
488 USHORT SubstituteNameLength;
489 USHORT PrintNameOffset;
490 USHORT PrintNameLength;
491 ULONG Flags;
492 WCHAR PathBuffer[1];
493 } SymbolicLinkReparseBuffer;
494
495 struct {
496 USHORT SubstituteNameOffset;
497 USHORT SubstituteNameLength;
498 USHORT PrintNameOffset;
499 USHORT PrintNameLength;
500 WCHAR PathBuffer[1];
501 } MountPointReparseBuffer;
502
503 struct {
504 UCHAR DataBuffer[1];
505 } GenericReparseBuffer;
506 };
507} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
508
509#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
510 GenericReparseBuffer)
511#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
512
513static int
Brian Curtind25aef52011-06-13 15:16:04 -0500514win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000515{
516 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
517 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
518 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000519
520 if (0 == DeviceIoControl(
521 reparse_point_handle,
522 FSCTL_GET_REPARSE_POINT,
523 NULL, 0, /* in buffer */
524 target_buffer, sizeof(target_buffer),
525 &n_bytes_returned,
526 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500527 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000528
529 if (reparse_tag)
530 *reparse_tag = rdb->ReparseTag;
531
Brian Curtind25aef52011-06-13 15:16:04 -0500532 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000533}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000534#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000535
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000536/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000537#ifdef WITH_NEXT_FRAMEWORK
538/* On Darwin/MacOSX a shared library or framework has no access to
539** environ directly, we must obtain it with _NSGetEnviron().
540*/
541#include <crt_externs.h>
542static char **environ;
543#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000545#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546
Barry Warsaw53699e91996-12-10 23:23:01 +0000547static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000548convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549{
Victor Stinner8c62be82010-05-06 00:08:46 +0000550 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000551#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000553#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000555#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000556#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000557 APIRET rc;
558 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
559#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000560
Victor Stinner8c62be82010-05-06 00:08:46 +0000561 d = PyDict_New();
562 if (d == NULL)
563 return NULL;
564#ifdef WITH_NEXT_FRAMEWORK
565 if (environ == NULL)
566 environ = *_NSGetEnviron();
567#endif
568#ifdef MS_WINDOWS
569 /* _wenviron must be initialized in this way if the program is started
570 through main() instead of wmain(). */
571 _wgetenv(L"");
572 if (_wenviron == NULL)
573 return d;
574 /* This part ignores errors */
575 for (e = _wenviron; *e != NULL; e++) {
576 PyObject *k;
577 PyObject *v;
578 wchar_t *p = wcschr(*e, L'=');
579 if (p == NULL)
580 continue;
581 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
582 if (k == NULL) {
583 PyErr_Clear();
584 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000585 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000586 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
587 if (v == NULL) {
588 PyErr_Clear();
589 Py_DECREF(k);
590 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000591 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000592 if (PyDict_GetItem(d, k) == NULL) {
593 if (PyDict_SetItem(d, k, v) != 0)
594 PyErr_Clear();
595 }
596 Py_DECREF(k);
597 Py_DECREF(v);
598 }
599#else
600 if (environ == NULL)
601 return d;
602 /* This part ignores errors */
603 for (e = environ; *e != NULL; e++) {
604 PyObject *k;
605 PyObject *v;
606 char *p = strchr(*e, '=');
607 if (p == NULL)
608 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000609 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000610 if (k == NULL) {
611 PyErr_Clear();
612 continue;
613 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000614 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000615 if (v == NULL) {
616 PyErr_Clear();
617 Py_DECREF(k);
618 continue;
619 }
620 if (PyDict_GetItem(d, k) == NULL) {
621 if (PyDict_SetItem(d, k, v) != 0)
622 PyErr_Clear();
623 }
624 Py_DECREF(k);
625 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000626 }
627#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000628#if defined(PYOS_OS2)
629 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
630 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
631 PyObject *v = PyBytes_FromString(buffer);
632 PyDict_SetItemString(d, "BEGINLIBPATH", v);
633 Py_DECREF(v);
634 }
635 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
636 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
637 PyObject *v = PyBytes_FromString(buffer);
638 PyDict_SetItemString(d, "ENDLIBPATH", v);
639 Py_DECREF(v);
640 }
641#endif
642 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643}
644
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645/* Set a POSIX-specific error from errno, and return NULL */
646
Barry Warsawd58d7641998-07-23 16:14:40 +0000647static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000648posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000649{
Victor Stinner8c62be82010-05-06 00:08:46 +0000650 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651}
Barry Warsawd58d7641998-07-23 16:14:40 +0000652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000653posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000654{
Victor Stinner8c62be82010-05-06 00:08:46 +0000655 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000656}
657
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000658
Mark Hammondef8b6542001-05-13 08:04:26 +0000659static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000660posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000661{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000662 PyObject *name_str, *rc;
663 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
664 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000665 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000666 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
667 name_str);
668 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000669 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000670}
671
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000672#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000673static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000674win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000675{
Victor Stinner8c62be82010-05-06 00:08:46 +0000676 /* XXX We should pass the function name along in the future.
677 (winreg.c also wants to pass the function name.)
678 This would however require an additional param to the
679 Windows error object, which is non-trivial.
680 */
681 errno = GetLastError();
682 if (filename)
683 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
684 else
685 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000686}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000688static PyObject *
689win32_error_unicode(char* function, Py_UNICODE* filename)
690{
Victor Stinner8c62be82010-05-06 00:08:46 +0000691 /* XXX - see win32_error for comments on 'function' */
692 errno = GetLastError();
693 if (filename)
694 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
695 else
696 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000697}
698
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000700convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701{
Victor Stinner8c62be82010-05-06 00:08:46 +0000702 if (PyUnicode_CheckExact(*param))
703 Py_INCREF(*param);
704 else if (PyUnicode_Check(*param))
705 /* For a Unicode subtype that's not a Unicode object,
706 return a true Unicode object with the same data. */
707 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
708 PyUnicode_GET_SIZE(*param));
709 else
710 *param = PyUnicode_FromEncodedObject(*param,
711 Py_FileSystemDefaultEncoding,
712 "strict");
713 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000714}
715
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000716#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717
Guido van Rossumd48f2521997-12-05 22:19:34 +0000718#if defined(PYOS_OS2)
719/**********************************************************************
720 * Helper Function to Trim and Format OS/2 Messages
721 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000722static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000723os2_formatmsg(char *msgbuf, int msglen, char *reason)
724{
725 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
726
727 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
728 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
729
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000730 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000731 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
732 }
733
734 /* Add Optional Reason Text */
735 if (reason) {
736 strcat(msgbuf, " : ");
737 strcat(msgbuf, reason);
738 }
739}
740
741/**********************************************************************
742 * Decode an OS/2 Operating System Error Code
743 *
744 * A convenience function to lookup an OS/2 error code and return a
745 * text message we can use to raise a Python exception.
746 *
747 * Notes:
748 * The messages for errors returned from the OS/2 kernel reside in
749 * the file OSO001.MSG in the \OS2 directory hierarchy.
750 *
751 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000752static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000753os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
754{
755 APIRET rc;
756 ULONG msglen;
757
758 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
759 Py_BEGIN_ALLOW_THREADS
760 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
761 errorcode, "oso001.msg", &msglen);
762 Py_END_ALLOW_THREADS
763
764 if (rc == NO_ERROR)
765 os2_formatmsg(msgbuf, msglen, reason);
766 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000767 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000768 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000769
770 return msgbuf;
771}
772
773/* Set an OS/2-specific error and return NULL. OS/2 kernel
774 errors are not in a global variable e.g. 'errno' nor are
775 they congruent with posix error numbers. */
776
Victor Stinner8c62be82010-05-06 00:08:46 +0000777static PyObject *
778os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000779{
780 char text[1024];
781 PyObject *v;
782
783 os2_strerror(text, sizeof(text), code, "");
784
785 v = Py_BuildValue("(is)", code, text);
786 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000787 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000788 Py_DECREF(v);
789 }
790 return NULL; /* Signal to Python that an Exception is Pending */
791}
792
793#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794
795/* POSIX generic methods */
796
Barry Warsaw53699e91996-12-10 23:23:01 +0000797static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000798posix_fildes(PyObject *fdobj, int (*func)(int))
799{
Victor Stinner8c62be82010-05-06 00:08:46 +0000800 int fd;
801 int res;
802 fd = PyObject_AsFileDescriptor(fdobj);
803 if (fd < 0)
804 return NULL;
805 if (!_PyVerify_fd(fd))
806 return posix_error();
807 Py_BEGIN_ALLOW_THREADS
808 res = (*func)(fd);
809 Py_END_ALLOW_THREADS
810 if (res < 0)
811 return posix_error();
812 Py_INCREF(Py_None);
813 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000814}
Guido van Rossum21142a01999-01-08 21:05:37 +0000815
816static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000817posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818{
Victor Stinner8c62be82010-05-06 00:08:46 +0000819 PyObject *opath1 = NULL;
820 char *path1;
821 int res;
822 if (!PyArg_ParseTuple(args, format,
823 PyUnicode_FSConverter, &opath1))
824 return NULL;
825 path1 = PyBytes_AsString(opath1);
826 Py_BEGIN_ALLOW_THREADS
827 res = (*func)(path1);
828 Py_END_ALLOW_THREADS
829 if (res < 0)
830 return posix_error_with_allocated_filename(opath1);
831 Py_DECREF(opath1);
832 Py_INCREF(Py_None);
833 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834}
835
Barry Warsaw53699e91996-12-10 23:23:01 +0000836static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000837posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000838 char *format,
839 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000840{
Victor Stinner8c62be82010-05-06 00:08:46 +0000841 PyObject *opath1 = NULL, *opath2 = NULL;
842 char *path1, *path2;
843 int res;
844 if (!PyArg_ParseTuple(args, format,
845 PyUnicode_FSConverter, &opath1,
846 PyUnicode_FSConverter, &opath2)) {
847 return NULL;
848 }
849 path1 = PyBytes_AsString(opath1);
850 path2 = PyBytes_AsString(opath2);
851 Py_BEGIN_ALLOW_THREADS
852 res = (*func)(path1, path2);
853 Py_END_ALLOW_THREADS
854 Py_DECREF(opath1);
855 Py_DECREF(opath2);
856 if (res != 0)
857 /* XXX how to report both path1 and path2??? */
858 return posix_error();
859 Py_INCREF(Py_None);
860 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861}
862
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000863#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000864static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000865win32_1str(PyObject* args, char* func,
866 char* format, BOOL (__stdcall *funcA)(LPCSTR),
867 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868{
Victor Stinner8c62be82010-05-06 00:08:46 +0000869 PyObject *uni;
870 char *ansi;
871 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000872
Victor Stinner8c62be82010-05-06 00:08:46 +0000873 if (!PyArg_ParseTuple(args, wformat, &uni))
874 PyErr_Clear();
875 else {
876 Py_BEGIN_ALLOW_THREADS
877 result = funcW(PyUnicode_AsUnicode(uni));
878 Py_END_ALLOW_THREADS
879 if (!result)
880 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
881 Py_INCREF(Py_None);
882 return Py_None;
883 }
884 if (!PyArg_ParseTuple(args, format, &ansi))
885 return NULL;
886 Py_BEGIN_ALLOW_THREADS
887 result = funcA(ansi);
888 Py_END_ALLOW_THREADS
889 if (!result)
890 return win32_error(func, ansi);
891 Py_INCREF(Py_None);
892 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000893
894}
895
896/* This is a reimplementation of the C library's chdir function,
897 but one that produces Win32 errors instead of DOS error codes.
898 chdir is essentially a wrapper around SetCurrentDirectory; however,
899 it also needs to set "magic" environment variables indicating
900 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000901static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000902win32_chdir(LPCSTR path)
903{
Victor Stinner8c62be82010-05-06 00:08:46 +0000904 char new_path[MAX_PATH+1];
905 int result;
906 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907
Victor Stinner8c62be82010-05-06 00:08:46 +0000908 if(!SetCurrentDirectoryA(path))
909 return FALSE;
910 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
911 if (!result)
912 return FALSE;
913 /* In the ANSI API, there should not be any paths longer
914 than MAX_PATH. */
915 assert(result <= MAX_PATH+1);
916 if (strncmp(new_path, "\\\\", 2) == 0 ||
917 strncmp(new_path, "//", 2) == 0)
918 /* UNC path, nothing to do. */
919 return TRUE;
920 env[1] = new_path[0];
921 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922}
923
924/* The Unicode version differs from the ANSI version
925 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000926static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000927win32_wchdir(LPCWSTR path)
928{
Victor Stinner8c62be82010-05-06 00:08:46 +0000929 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
930 int result;
931 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 if(!SetCurrentDirectoryW(path))
934 return FALSE;
935 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
936 if (!result)
937 return FALSE;
938 if (result > MAX_PATH+1) {
939 new_path = malloc(result * sizeof(wchar_t));
940 if (!new_path) {
941 SetLastError(ERROR_OUTOFMEMORY);
942 return FALSE;
943 }
944 result = GetCurrentDirectoryW(result, new_path);
945 if (!result) {
946 free(new_path);
947 return FALSE;
948 }
949 }
950 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
951 wcsncmp(new_path, L"//", 2) == 0)
952 /* UNC path, nothing to do. */
953 return TRUE;
954 env[1] = new_path[0];
955 result = SetEnvironmentVariableW(env, new_path);
956 if (new_path != _new_path)
957 free(new_path);
958 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000959}
960#endif
961
Martin v. Löwis14694662006-02-03 12:54:16 +0000962#ifdef MS_WINDOWS
963/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
964 - time stamps are restricted to second resolution
965 - file modification times suffer from forth-and-back conversions between
966 UTC and local time
967 Therefore, we implement our own stat, based on the Win32 API directly.
968*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000969#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000970
971struct win32_stat{
972 int st_dev;
973 __int64 st_ino;
974 unsigned short st_mode;
975 int st_nlink;
976 int st_uid;
977 int st_gid;
978 int st_rdev;
979 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000980 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000981 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000982 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000983 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000984 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000985 int st_ctime_nsec;
986};
987
988static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
989
990static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000991FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000992{
Victor Stinner8c62be82010-05-06 00:08:46 +0000993 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
994 /* Cannot simply cast and dereference in_ptr,
995 since it might not be aligned properly */
996 __int64 in;
997 memcpy(&in, in_ptr, sizeof(in));
998 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000999 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001000}
1001
Thomas Wouters477c8d52006-05-27 19:21:47 +00001002static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001003time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001004{
Victor Stinner8c62be82010-05-06 00:08:46 +00001005 /* XXX endianness */
1006 __int64 out;
1007 out = time_in + secs_between_epochs;
1008 out = out * 10000000 + nsec_in / 100;
1009 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010}
1011
Martin v. Löwis14694662006-02-03 12:54:16 +00001012/* Below, we *know* that ugo+r is 0444 */
1013#if _S_IREAD != 0400
1014#error Unsupported C library
1015#endif
1016static int
1017attributes_to_mode(DWORD attr)
1018{
Victor Stinner8c62be82010-05-06 00:08:46 +00001019 int m = 0;
1020 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1021 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1022 else
1023 m |= _S_IFREG;
1024 if (attr & FILE_ATTRIBUTE_READONLY)
1025 m |= 0444;
1026 else
1027 m |= 0666;
1028 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001029}
1030
1031static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001032attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001033{
Victor Stinner8c62be82010-05-06 00:08:46 +00001034 memset(result, 0, sizeof(*result));
1035 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1036 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1037 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1038 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1039 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001040 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001041 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001042 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1043 /* first clear the S_IFMT bits */
1044 result->st_mode ^= (result->st_mode & 0170000);
1045 /* now set the bits that make this a symlink */
1046 result->st_mode |= 0120000;
1047 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001048
Victor Stinner8c62be82010-05-06 00:08:46 +00001049 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001050}
1051
Guido van Rossumd8faa362007-04-27 19:54:29 +00001052static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001053attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001054{
Victor Stinner8c62be82010-05-06 00:08:46 +00001055 HANDLE hFindFile;
1056 WIN32_FIND_DATAA FileData;
1057 hFindFile = FindFirstFileA(pszFile, &FileData);
1058 if (hFindFile == INVALID_HANDLE_VALUE)
1059 return FALSE;
1060 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001061 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001062 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001063 info->dwFileAttributes = FileData.dwFileAttributes;
1064 info->ftCreationTime = FileData.ftCreationTime;
1065 info->ftLastAccessTime = FileData.ftLastAccessTime;
1066 info->ftLastWriteTime = FileData.ftLastWriteTime;
1067 info->nFileSizeHigh = FileData.nFileSizeHigh;
1068 info->nFileSizeLow = FileData.nFileSizeLow;
1069/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001070 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1071 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001072 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001073}
1074
1075static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001076attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001077{
Victor Stinner8c62be82010-05-06 00:08:46 +00001078 HANDLE hFindFile;
1079 WIN32_FIND_DATAW FileData;
1080 hFindFile = FindFirstFileW(pszFile, &FileData);
1081 if (hFindFile == INVALID_HANDLE_VALUE)
1082 return FALSE;
1083 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001084 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001085 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001086 info->dwFileAttributes = FileData.dwFileAttributes;
1087 info->ftCreationTime = FileData.ftCreationTime;
1088 info->ftLastAccessTime = FileData.ftLastAccessTime;
1089 info->ftLastWriteTime = FileData.ftLastWriteTime;
1090 info->nFileSizeHigh = FileData.nFileSizeHigh;
1091 info->nFileSizeLow = FileData.nFileSizeLow;
1092/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001093 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1094 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001095 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001096}
1097
Brian Curtind25aef52011-06-13 15:16:04 -05001098/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1099static int has_GetFinalPathNameByHandle = 0;
1100static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1101 DWORD);
1102static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1103 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001104static int
Brian Curtind25aef52011-06-13 15:16:04 -05001105check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001106{
Brian Curtind25aef52011-06-13 15:16:04 -05001107 HINSTANCE hKernel32;
1108 /* only recheck */
1109 if (!has_GetFinalPathNameByHandle)
1110 {
1111 hKernel32 = GetModuleHandle("KERNEL32");
1112 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1113 "GetFinalPathNameByHandleA");
1114 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1115 "GetFinalPathNameByHandleW");
1116 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1117 Py_GetFinalPathNameByHandleW;
1118 }
1119 return has_GetFinalPathNameByHandle;
1120}
1121
1122static BOOL
1123get_target_path(HANDLE hdl, wchar_t **target_path)
1124{
1125 int buf_size, result_length;
1126 wchar_t *buf;
1127
1128 /* We have a good handle to the target, use it to determine
1129 the target path name (then we'll call lstat on it). */
1130 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1131 VOLUME_NAME_DOS);
1132 if(!buf_size)
1133 return FALSE;
1134
1135 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001136 if (!buf) {
1137 SetLastError(ERROR_OUTOFMEMORY);
1138 return FALSE;
1139 }
1140
Brian Curtind25aef52011-06-13 15:16:04 -05001141 result_length = Py_GetFinalPathNameByHandleW(hdl,
1142 buf, buf_size, VOLUME_NAME_DOS);
1143
1144 if(!result_length) {
1145 free(buf);
1146 return FALSE;
1147 }
1148
1149 if(!CloseHandle(hdl)) {
1150 free(buf);
1151 return FALSE;
1152 }
1153
1154 buf[result_length] = 0;
1155
1156 *target_path = buf;
1157 return TRUE;
1158}
1159
1160static int
1161win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1162 BOOL traverse);
1163static int
1164win32_xstat_impl(const char *path, struct win32_stat *result,
1165 BOOL traverse)
1166{
Victor Stinner26de69d2011-06-17 15:15:38 +02001167 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001168 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001169 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001170 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001171 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001172 const char *dot;
1173
Brian Curtind25aef52011-06-13 15:16:04 -05001174 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001175 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1176 traverse reparse point. */
1177 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001178 }
1179
Brian Curtinf5e76d02010-11-24 13:14:05 +00001180 hFile = CreateFileA(
1181 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001182 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001183 0, /* share mode */
1184 NULL, /* security attributes */
1185 OPEN_EXISTING,
1186 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001187 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1188 Because of this, calls like GetFinalPathNameByHandle will return
1189 the symlink path agin and not the actual final path. */
1190 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1191 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001192 NULL);
1193
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001194 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001195 /* Either the target doesn't exist, or we don't have access to
1196 get a handle to it. If the former, we need to return an error.
1197 If the latter, we can use attributes_from_dir. */
1198 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001199 return -1;
1200 /* Could not get attributes on open file. Fall back to
1201 reading the directory. */
1202 if (!attributes_from_dir(path, &info, &reparse_tag))
1203 /* Very strange. This should not fail now */
1204 return -1;
1205 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1206 if (traverse) {
1207 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001208 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001209 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001210 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001211 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001212 } else {
1213 if (!GetFileInformationByHandle(hFile, &info)) {
1214 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001215 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001216 }
1217 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001218 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1219 return -1;
1220
1221 /* Close the outer open file handle now that we're about to
1222 reopen it with different flags. */
1223 if (!CloseHandle(hFile))
1224 return -1;
1225
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001226 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001227 /* In order to call GetFinalPathNameByHandle we need to open
1228 the file without the reparse handling flag set. */
1229 hFile2 = CreateFileA(
1230 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1231 NULL, OPEN_EXISTING,
1232 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1233 NULL);
1234 if (hFile2 == INVALID_HANDLE_VALUE)
1235 return -1;
1236
1237 if (!get_target_path(hFile2, &target_path))
1238 return -1;
1239
1240 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001241 free(target_path);
1242 return code;
1243 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001244 } else
1245 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001246 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001247 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248
1249 /* Set S_IEXEC if it is an .exe, .bat, ... */
1250 dot = strrchr(path, '.');
1251 if (dot) {
1252 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1253 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1254 result->st_mode |= 0111;
1255 }
1256 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001257}
1258
1259static int
Brian Curtind25aef52011-06-13 15:16:04 -05001260win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1261 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001262{
1263 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001264 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001265 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001266 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001267 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268 const wchar_t *dot;
1269
Brian Curtind25aef52011-06-13 15:16:04 -05001270 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001271 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1272 traverse reparse point. */
1273 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001274 }
1275
Brian Curtinf5e76d02010-11-24 13:14:05 +00001276 hFile = CreateFileW(
1277 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001278 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001279 0, /* share mode */
1280 NULL, /* security attributes */
1281 OPEN_EXISTING,
1282 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001283 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1284 Because of this, calls like GetFinalPathNameByHandle will return
1285 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001286 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001287 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001288 NULL);
1289
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001290 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001291 /* Either the target doesn't exist, or we don't have access to
1292 get a handle to it. If the former, we need to return an error.
1293 If the latter, we can use attributes_from_dir. */
1294 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001295 return -1;
1296 /* Could not get attributes on open file. Fall back to
1297 reading the directory. */
1298 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1299 /* Very strange. This should not fail now */
1300 return -1;
1301 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1302 if (traverse) {
1303 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001304 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001305 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001306 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001307 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001308 } else {
1309 if (!GetFileInformationByHandle(hFile, &info)) {
1310 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001311 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001312 }
1313 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001314 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1315 return -1;
1316
1317 /* Close the outer open file handle now that we're about to
1318 reopen it with different flags. */
1319 if (!CloseHandle(hFile))
1320 return -1;
1321
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001322 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001323 /* In order to call GetFinalPathNameByHandle we need to open
1324 the file without the reparse handling flag set. */
1325 hFile2 = CreateFileW(
1326 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1327 NULL, OPEN_EXISTING,
1328 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1329 NULL);
1330 if (hFile2 == INVALID_HANDLE_VALUE)
1331 return -1;
1332
1333 if (!get_target_path(hFile2, &target_path))
1334 return -1;
1335
1336 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001337 free(target_path);
1338 return code;
1339 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001340 } else
1341 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001342 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001343 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001344
1345 /* Set S_IEXEC if it is an .exe, .bat, ... */
1346 dot = wcsrchr(path, '.');
1347 if (dot) {
1348 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1349 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1350 result->st_mode |= 0111;
1351 }
1352 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001353}
1354
1355static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001356win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001357{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001358 /* Protocol violation: we explicitly clear errno, instead of
1359 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001360 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001361 errno = 0;
1362 return code;
1363}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001364
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001365static int
1366win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1367{
1368 /* Protocol violation: we explicitly clear errno, instead of
1369 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001370 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001371 errno = 0;
1372 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001373}
Brian Curtind25aef52011-06-13 15:16:04 -05001374/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001375
1376 In Posix, stat automatically traverses symlinks and returns the stat
1377 structure for the target. In Windows, the equivalent GetFileAttributes by
1378 default does not traverse symlinks and instead returns attributes for
1379 the symlink.
1380
1381 Therefore, win32_lstat will get the attributes traditionally, and
1382 win32_stat will first explicitly resolve the symlink target and then will
1383 call win32_lstat on that result.
1384
Ezio Melotti4969f702011-03-15 05:59:46 +02001385 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001386
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001387static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001388win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001389{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001390 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001391}
1392
Victor Stinner8c62be82010-05-06 00:08:46 +00001393static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001394win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001395{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001396 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001397}
1398
1399static int
1400win32_stat(const char* path, struct win32_stat *result)
1401{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001402 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001403}
1404
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001405static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001406win32_stat_w(const wchar_t* path, struct win32_stat *result)
1407{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001408 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001409}
1410
1411static int
1412win32_fstat(int file_number, struct win32_stat *result)
1413{
Victor Stinner8c62be82010-05-06 00:08:46 +00001414 BY_HANDLE_FILE_INFORMATION info;
1415 HANDLE h;
1416 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001417
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001419
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 /* Protocol violation: we explicitly clear errno, instead of
1421 setting it to a POSIX error. Callers should use GetLastError. */
1422 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001423
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 if (h == INVALID_HANDLE_VALUE) {
1425 /* This is really a C library error (invalid file handle).
1426 We set the Win32 error to the closes one matching. */
1427 SetLastError(ERROR_INVALID_HANDLE);
1428 return -1;
1429 }
1430 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001431
Victor Stinner8c62be82010-05-06 00:08:46 +00001432 type = GetFileType(h);
1433 if (type == FILE_TYPE_UNKNOWN) {
1434 DWORD error = GetLastError();
1435 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001436 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001437 }
1438 /* else: valid but unknown file */
1439 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001440
Victor Stinner8c62be82010-05-06 00:08:46 +00001441 if (type != FILE_TYPE_DISK) {
1442 if (type == FILE_TYPE_CHAR)
1443 result->st_mode = _S_IFCHR;
1444 else if (type == FILE_TYPE_PIPE)
1445 result->st_mode = _S_IFIFO;
1446 return 0;
1447 }
1448
1449 if (!GetFileInformationByHandle(h, &info)) {
1450 return -1;
1451 }
1452
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001453 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001454 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001455 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1456 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001457}
1458
1459#endif /* MS_WINDOWS */
1460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001461PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001462"stat_result: Result from stat or lstat.\n\n\
1463This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001464 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001465or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1466\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001467Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1468or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001469\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001470See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001471
1472static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001473 {"st_mode", "protection bits"},
1474 {"st_ino", "inode"},
1475 {"st_dev", "device"},
1476 {"st_nlink", "number of hard links"},
1477 {"st_uid", "user ID of owner"},
1478 {"st_gid", "group ID of owner"},
1479 {"st_size", "total size, in bytes"},
1480 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1481 {NULL, "integer time of last access"},
1482 {NULL, "integer time of last modification"},
1483 {NULL, "integer time of last change"},
1484 {"st_atime", "time of last access"},
1485 {"st_mtime", "time of last modification"},
1486 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001487#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001489#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001490#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001491 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001492#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001493#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001494 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001495#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001496#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001497 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001498#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001499#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001500 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001501#endif
1502#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001503 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001504#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001505 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001506};
1507
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001508#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001509#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001510#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001511#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001512#endif
1513
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001514#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001515#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1516#else
1517#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1518#endif
1519
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001520#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001521#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1522#else
1523#define ST_RDEV_IDX ST_BLOCKS_IDX
1524#endif
1525
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001526#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1527#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1528#else
1529#define ST_FLAGS_IDX ST_RDEV_IDX
1530#endif
1531
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001532#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001533#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001534#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001535#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001536#endif
1537
1538#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1539#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1540#else
1541#define ST_BIRTHTIME_IDX ST_GEN_IDX
1542#endif
1543
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001544static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001545 "stat_result", /* name */
1546 stat_result__doc__, /* doc */
1547 stat_result_fields,
1548 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001549};
1550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001551PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001552"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1553This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001554 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001555or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001556\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001557See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001558
1559static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001560 {"f_bsize", },
1561 {"f_frsize", },
1562 {"f_blocks", },
1563 {"f_bfree", },
1564 {"f_bavail", },
1565 {"f_files", },
1566 {"f_ffree", },
1567 {"f_favail", },
1568 {"f_flag", },
1569 {"f_namemax",},
1570 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001571};
1572
1573static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 "statvfs_result", /* name */
1575 statvfs_result__doc__, /* doc */
1576 statvfs_result_fields,
1577 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001578};
1579
Ross Lagerwall7807c352011-03-17 20:20:30 +02001580#if defined(HAVE_WAITID) && !defined(__APPLE__)
1581PyDoc_STRVAR(waitid_result__doc__,
1582"waitid_result: Result from waitid.\n\n\
1583This object may be accessed either as a tuple of\n\
1584 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1585or via the attributes si_pid, si_uid, and so on.\n\
1586\n\
1587See os.waitid for more information.");
1588
1589static PyStructSequence_Field waitid_result_fields[] = {
1590 {"si_pid", },
1591 {"si_uid", },
1592 {"si_signo", },
1593 {"si_status", },
1594 {"si_code", },
1595 {0}
1596};
1597
1598static PyStructSequence_Desc waitid_result_desc = {
1599 "waitid_result", /* name */
1600 waitid_result__doc__, /* doc */
1601 waitid_result_fields,
1602 5
1603};
1604static PyTypeObject WaitidResultType;
1605#endif
1606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001607static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001608static PyTypeObject StatResultType;
1609static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001610#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001611static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001612#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001613static newfunc structseq_new;
1614
1615static PyObject *
1616statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1617{
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 PyStructSequence *result;
1619 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001620
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 result = (PyStructSequence*)structseq_new(type, args, kwds);
1622 if (!result)
1623 return NULL;
1624 /* If we have been initialized from a tuple,
1625 st_?time might be set to None. Initialize it
1626 from the int slots. */
1627 for (i = 7; i <= 9; i++) {
1628 if (result->ob_item[i+3] == Py_None) {
1629 Py_DECREF(Py_None);
1630 Py_INCREF(result->ob_item[i]);
1631 result->ob_item[i+3] = result->ob_item[i];
1632 }
1633 }
1634 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001635}
1636
1637
1638
1639/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001640static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001641
1642PyDoc_STRVAR(stat_float_times__doc__,
1643"stat_float_times([newval]) -> oldval\n\n\
1644Determine whether os.[lf]stat represents time stamps as float objects.\n\
1645If newval is True, future calls to stat() return floats, if it is False,\n\
1646future calls return ints. \n\
1647If newval is omitted, return the current setting.\n");
1648
1649static PyObject*
1650stat_float_times(PyObject* self, PyObject *args)
1651{
Victor Stinner8c62be82010-05-06 00:08:46 +00001652 int newval = -1;
1653 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1654 return NULL;
1655 if (newval == -1)
1656 /* Return old value */
1657 return PyBool_FromLong(_stat_float_times);
1658 _stat_float_times = newval;
1659 Py_INCREF(Py_None);
1660 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001661}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001662
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001663static void
1664fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1665{
Victor Stinner8c62be82010-05-06 00:08:46 +00001666 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001667#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001669#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001671#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 if (!ival)
1673 return;
1674 if (_stat_float_times) {
1675 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1676 } else {
1677 fval = ival;
1678 Py_INCREF(fval);
1679 }
1680 PyStructSequence_SET_ITEM(v, index, ival);
1681 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001682}
1683
Tim Peters5aa91602002-01-30 05:46:57 +00001684/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001685 (used by posix_stat() and posix_fstat()) */
1686static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001687_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001688{
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 unsigned long ansec, mnsec, cnsec;
1690 PyObject *v = PyStructSequence_New(&StatResultType);
1691 if (v == NULL)
1692 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001693
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001695#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 PyStructSequence_SET_ITEM(v, 1,
1697 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001698#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001700#endif
1701#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 PyStructSequence_SET_ITEM(v, 2,
1703 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001704#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001705 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001706#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001707 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1708 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1709 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001710#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 PyStructSequence_SET_ITEM(v, 6,
1712 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001713#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001714 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001715#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001716
Martin v. Löwis14694662006-02-03 12:54:16 +00001717#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 ansec = st->st_atim.tv_nsec;
1719 mnsec = st->st_mtim.tv_nsec;
1720 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001721#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001722 ansec = st->st_atimespec.tv_nsec;
1723 mnsec = st->st_mtimespec.tv_nsec;
1724 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001725#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 ansec = st->st_atime_nsec;
1727 mnsec = st->st_mtime_nsec;
1728 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001729#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001731#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 fill_time(v, 7, st->st_atime, ansec);
1733 fill_time(v, 8, st->st_mtime, mnsec);
1734 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001735
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001736#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1738 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001739#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001740#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1742 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001743#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001744#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001745 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1746 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001747#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001748#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001749 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1750 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001751#endif
1752#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 {
1754 PyObject *val;
1755 unsigned long bsec,bnsec;
1756 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001757#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001759#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001761#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 if (_stat_float_times) {
1763 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1764 } else {
1765 val = PyLong_FromLong((long)bsec);
1766 }
1767 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1768 val);
1769 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001770#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001771#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001772 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1773 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001774#endif
Fred Drake699f3522000-06-29 21:12:41 +00001775
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 if (PyErr_Occurred()) {
1777 Py_DECREF(v);
1778 return NULL;
1779 }
Fred Drake699f3522000-06-29 21:12:41 +00001780
Victor Stinner8c62be82010-05-06 00:08:46 +00001781 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001782}
1783
Barry Warsaw53699e91996-12-10 23:23:01 +00001784static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001785posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001786 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001787#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001789#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 char *wformat,
1793 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001794{
Victor Stinner8c62be82010-05-06 00:08:46 +00001795 STRUCT_STAT st;
1796 PyObject *opath;
1797 char *path;
1798 int res;
1799 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001800
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001801#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001802 PyUnicodeObject *po;
1803 if (PyArg_ParseTuple(args, wformat, &po)) {
1804 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001805
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 Py_BEGIN_ALLOW_THREADS
1807 /* PyUnicode_AS_UNICODE result OK without
1808 thread lock as it is a simple dereference. */
1809 res = wstatfunc(wpath, &st);
1810 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001811
Victor Stinner8c62be82010-05-06 00:08:46 +00001812 if (res != 0)
1813 return win32_error_unicode("stat", wpath);
1814 return _pystat_fromstructstat(&st);
1815 }
1816 /* Drop the argument parsing error as narrow strings
1817 are also valid. */
1818 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001819#endif
1820
Victor Stinner8c62be82010-05-06 00:08:46 +00001821 if (!PyArg_ParseTuple(args, format,
1822 PyUnicode_FSConverter, &opath))
1823 return NULL;
1824 path = PyBytes_AsString(opath);
1825 Py_BEGIN_ALLOW_THREADS
1826 res = (*statfunc)(path, &st);
1827 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001828
Victor Stinner8c62be82010-05-06 00:08:46 +00001829 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001830#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001832#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001834#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 }
1836 else
1837 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001838
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 Py_DECREF(opath);
1840 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841}
1842
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001843/* POSIX methods */
1844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001846"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001847Use the real uid/gid to test for access to a path. Note that most\n\
1848operations will use the effective uid/gid, therefore this routine can\n\
1849be used in a suid/sgid environment to test if the invoking user has the\n\
1850specified access to the path. The mode argument can be F_OK to test\n\
1851existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001852
1853static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001854posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001855{
Victor Stinner8c62be82010-05-06 00:08:46 +00001856 PyObject *opath;
1857 char *path;
1858 int mode;
1859
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001860#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 DWORD attr;
1862 PyUnicodeObject *po;
1863 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1864 Py_BEGIN_ALLOW_THREADS
1865 /* PyUnicode_AS_UNICODE OK without thread lock as
1866 it is a simple dereference. */
1867 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1868 Py_END_ALLOW_THREADS
1869 goto finish;
1870 }
1871 /* Drop the argument parsing error as narrow strings
1872 are also valid. */
1873 PyErr_Clear();
1874 if (!PyArg_ParseTuple(args, "O&i:access",
1875 PyUnicode_FSConverter, &opath, &mode))
1876 return NULL;
1877 path = PyBytes_AsString(opath);
1878 Py_BEGIN_ALLOW_THREADS
1879 attr = GetFileAttributesA(path);
1880 Py_END_ALLOW_THREADS
1881 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001882finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 if (attr == 0xFFFFFFFF)
1884 /* File does not exist, or cannot read attributes */
1885 return PyBool_FromLong(0);
1886 /* Access is possible if either write access wasn't requested, or
1887 the file isn't read-only, or if it's a directory, as there are
1888 no read-only directories on Windows. */
1889 return PyBool_FromLong(!(mode & 2)
1890 || !(attr & FILE_ATTRIBUTE_READONLY)
1891 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001892#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001893 int res;
1894 if (!PyArg_ParseTuple(args, "O&i:access",
1895 PyUnicode_FSConverter, &opath, &mode))
1896 return NULL;
1897 path = PyBytes_AsString(opath);
1898 Py_BEGIN_ALLOW_THREADS
1899 res = access(path, mode);
1900 Py_END_ALLOW_THREADS
1901 Py_DECREF(opath);
1902 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001904}
1905
Guido van Rossumd371ff11999-01-25 16:12:23 +00001906#ifndef F_OK
1907#define F_OK 0
1908#endif
1909#ifndef R_OK
1910#define R_OK 4
1911#endif
1912#ifndef W_OK
1913#define W_OK 2
1914#endif
1915#ifndef X_OK
1916#define X_OK 1
1917#endif
1918
1919#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001921"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001923
1924static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001925posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001926{
Victor Stinner8c62be82010-05-06 00:08:46 +00001927 int id;
1928 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001929
Victor Stinner8c62be82010-05-06 00:08:46 +00001930 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1931 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001932
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001933#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 /* file descriptor 0 only, the default input device (stdin) */
1935 if (id == 0) {
1936 ret = ttyname();
1937 }
1938 else {
1939 ret = NULL;
1940 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001941#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001942 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001943#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 if (ret == NULL)
1945 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001946 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001947}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001948#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001949
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001950#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001951PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001952"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001954
1955static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001956posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001957{
Victor Stinner8c62be82010-05-06 00:08:46 +00001958 char *ret;
1959 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001960
Greg Wardb48bc172000-03-01 21:51:56 +00001961#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001962 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001963#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001965#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 if (ret == NULL)
1967 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001968 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001969}
1970#endif
1971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001973"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001974Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Barry Warsaw53699e91996-12-10 23:23:01 +00001976static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001977posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001979#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001980 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001982 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001983#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001984 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001985#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001987#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001988}
1989
Fred Drake4d1e64b2002-04-15 19:40:07 +00001990#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001992"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001993Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001995
1996static PyObject *
1997posix_fchdir(PyObject *self, PyObject *fdobj)
1998{
Victor Stinner8c62be82010-05-06 00:08:46 +00001999 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002000}
2001#endif /* HAVE_FCHDIR */
2002
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002005"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002006Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002007
Barry Warsaw53699e91996-12-10 23:23:01 +00002008static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002009posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002010{
Victor Stinner8c62be82010-05-06 00:08:46 +00002011 PyObject *opath = NULL;
2012 char *path = NULL;
2013 int i;
2014 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002015#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 DWORD attr;
2017 PyUnicodeObject *po;
2018 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2019 Py_BEGIN_ALLOW_THREADS
2020 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2021 if (attr != 0xFFFFFFFF) {
2022 if (i & _S_IWRITE)
2023 attr &= ~FILE_ATTRIBUTE_READONLY;
2024 else
2025 attr |= FILE_ATTRIBUTE_READONLY;
2026 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2027 }
2028 else
2029 res = 0;
2030 Py_END_ALLOW_THREADS
2031 if (!res)
2032 return win32_error_unicode("chmod",
2033 PyUnicode_AS_UNICODE(po));
2034 Py_INCREF(Py_None);
2035 return Py_None;
2036 }
2037 /* Drop the argument parsing error as narrow strings
2038 are also valid. */
2039 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002040
Victor Stinner8c62be82010-05-06 00:08:46 +00002041 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2042 &opath, &i))
2043 return NULL;
2044 path = PyBytes_AsString(opath);
2045 Py_BEGIN_ALLOW_THREADS
2046 attr = GetFileAttributesA(path);
2047 if (attr != 0xFFFFFFFF) {
2048 if (i & _S_IWRITE)
2049 attr &= ~FILE_ATTRIBUTE_READONLY;
2050 else
2051 attr |= FILE_ATTRIBUTE_READONLY;
2052 res = SetFileAttributesA(path, attr);
2053 }
2054 else
2055 res = 0;
2056 Py_END_ALLOW_THREADS
2057 if (!res) {
2058 win32_error("chmod", path);
2059 Py_DECREF(opath);
2060 return NULL;
2061 }
2062 Py_DECREF(opath);
2063 Py_INCREF(Py_None);
2064 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002065#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2067 &opath, &i))
2068 return NULL;
2069 path = PyBytes_AsString(opath);
2070 Py_BEGIN_ALLOW_THREADS
2071 res = chmod(path, i);
2072 Py_END_ALLOW_THREADS
2073 if (res < 0)
2074 return posix_error_with_allocated_filename(opath);
2075 Py_DECREF(opath);
2076 Py_INCREF(Py_None);
2077 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002078#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002079}
2080
Christian Heimes4e30a842007-11-30 22:12:06 +00002081#ifdef HAVE_FCHMOD
2082PyDoc_STRVAR(posix_fchmod__doc__,
2083"fchmod(fd, mode)\n\n\
2084Change the access permissions of the file given by file\n\
2085descriptor fd.");
2086
2087static PyObject *
2088posix_fchmod(PyObject *self, PyObject *args)
2089{
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 int fd, mode, res;
2091 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2092 return NULL;
2093 Py_BEGIN_ALLOW_THREADS
2094 res = fchmod(fd, mode);
2095 Py_END_ALLOW_THREADS
2096 if (res < 0)
2097 return posix_error();
2098 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002099}
2100#endif /* HAVE_FCHMOD */
2101
2102#ifdef HAVE_LCHMOD
2103PyDoc_STRVAR(posix_lchmod__doc__,
2104"lchmod(path, mode)\n\n\
2105Change the access permissions of a file. If path is a symlink, this\n\
2106affects the link itself rather than the target.");
2107
2108static PyObject *
2109posix_lchmod(PyObject *self, PyObject *args)
2110{
Victor Stinner8c62be82010-05-06 00:08:46 +00002111 PyObject *opath;
2112 char *path;
2113 int i;
2114 int res;
2115 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2116 &opath, &i))
2117 return NULL;
2118 path = PyBytes_AsString(opath);
2119 Py_BEGIN_ALLOW_THREADS
2120 res = lchmod(path, i);
2121 Py_END_ALLOW_THREADS
2122 if (res < 0)
2123 return posix_error_with_allocated_filename(opath);
2124 Py_DECREF(opath);
2125 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002126}
2127#endif /* HAVE_LCHMOD */
2128
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002129
Thomas Wouterscf297e42007-02-23 15:07:44 +00002130#ifdef HAVE_CHFLAGS
2131PyDoc_STRVAR(posix_chflags__doc__,
2132"chflags(path, flags)\n\n\
2133Set file flags.");
2134
2135static PyObject *
2136posix_chflags(PyObject *self, PyObject *args)
2137{
Victor Stinner8c62be82010-05-06 00:08:46 +00002138 PyObject *opath;
2139 char *path;
2140 unsigned long flags;
2141 int res;
2142 if (!PyArg_ParseTuple(args, "O&k:chflags",
2143 PyUnicode_FSConverter, &opath, &flags))
2144 return NULL;
2145 path = PyBytes_AsString(opath);
2146 Py_BEGIN_ALLOW_THREADS
2147 res = chflags(path, flags);
2148 Py_END_ALLOW_THREADS
2149 if (res < 0)
2150 return posix_error_with_allocated_filename(opath);
2151 Py_DECREF(opath);
2152 Py_INCREF(Py_None);
2153 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002154}
2155#endif /* HAVE_CHFLAGS */
2156
2157#ifdef HAVE_LCHFLAGS
2158PyDoc_STRVAR(posix_lchflags__doc__,
2159"lchflags(path, flags)\n\n\
2160Set file flags.\n\
2161This function will not follow symbolic links.");
2162
2163static PyObject *
2164posix_lchflags(PyObject *self, PyObject *args)
2165{
Victor Stinner8c62be82010-05-06 00:08:46 +00002166 PyObject *opath;
2167 char *path;
2168 unsigned long flags;
2169 int res;
2170 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2171 PyUnicode_FSConverter, &opath, &flags))
2172 return NULL;
2173 path = PyBytes_AsString(opath);
2174 Py_BEGIN_ALLOW_THREADS
2175 res = lchflags(path, flags);
2176 Py_END_ALLOW_THREADS
2177 if (res < 0)
2178 return posix_error_with_allocated_filename(opath);
2179 Py_DECREF(opath);
2180 Py_INCREF(Py_None);
2181 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002182}
2183#endif /* HAVE_LCHFLAGS */
2184
Martin v. Löwis244edc82001-10-04 22:44:26 +00002185#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002186PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002187"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002188Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002189
2190static PyObject *
2191posix_chroot(PyObject *self, PyObject *args)
2192{
Victor Stinner8c62be82010-05-06 00:08:46 +00002193 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002194}
2195#endif
2196
Guido van Rossum21142a01999-01-08 21:05:37 +00002197#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002198PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002199"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002200force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002201
2202static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002203posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002204{
Stefan Krah0e803b32010-11-26 16:16:47 +00002205 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002206}
2207#endif /* HAVE_FSYNC */
2208
Ross Lagerwall7807c352011-03-17 20:20:30 +02002209#ifdef HAVE_SYNC
2210PyDoc_STRVAR(posix_sync__doc__,
2211"sync()\n\n\
2212Force write of everything to disk.");
2213
2214static PyObject *
2215posix_sync(PyObject *self, PyObject *noargs)
2216{
2217 Py_BEGIN_ALLOW_THREADS
2218 sync();
2219 Py_END_ALLOW_THREADS
2220 Py_RETURN_NONE;
2221}
2222#endif
2223
Guido van Rossum21142a01999-01-08 21:05:37 +00002224#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002225
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002226#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002227extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2228#endif
2229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002230PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002231"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002232force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002233 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002234
2235static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002236posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002237{
Stefan Krah0e803b32010-11-26 16:16:47 +00002238 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002239}
2240#endif /* HAVE_FDATASYNC */
2241
2242
Fredrik Lundh10723342000-07-10 16:38:09 +00002243#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002244PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002245"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002246Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002247
Barry Warsaw53699e91996-12-10 23:23:01 +00002248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002249posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250{
Victor Stinner8c62be82010-05-06 00:08:46 +00002251 PyObject *opath;
2252 char *path;
2253 long uid, gid;
2254 int res;
2255 if (!PyArg_ParseTuple(args, "O&ll:chown",
2256 PyUnicode_FSConverter, &opath,
2257 &uid, &gid))
2258 return NULL;
2259 path = PyBytes_AsString(opath);
2260 Py_BEGIN_ALLOW_THREADS
2261 res = chown(path, (uid_t) uid, (gid_t) gid);
2262 Py_END_ALLOW_THREADS
2263 if (res < 0)
2264 return posix_error_with_allocated_filename(opath);
2265 Py_DECREF(opath);
2266 Py_INCREF(Py_None);
2267 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002268}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002269#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002270
Christian Heimes4e30a842007-11-30 22:12:06 +00002271#ifdef HAVE_FCHOWN
2272PyDoc_STRVAR(posix_fchown__doc__,
2273"fchown(fd, uid, gid)\n\n\
2274Change the owner and group id of the file given by file descriptor\n\
2275fd to the numeric uid and gid.");
2276
2277static PyObject *
2278posix_fchown(PyObject *self, PyObject *args)
2279{
Victor Stinner8c62be82010-05-06 00:08:46 +00002280 int fd;
2281 long uid, gid;
2282 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002283 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002284 return NULL;
2285 Py_BEGIN_ALLOW_THREADS
2286 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2287 Py_END_ALLOW_THREADS
2288 if (res < 0)
2289 return posix_error();
2290 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002291}
2292#endif /* HAVE_FCHOWN */
2293
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002294#ifdef HAVE_LCHOWN
2295PyDoc_STRVAR(posix_lchown__doc__,
2296"lchown(path, uid, gid)\n\n\
2297Change the owner and group id of path to the numeric uid and gid.\n\
2298This function will not follow symbolic links.");
2299
2300static PyObject *
2301posix_lchown(PyObject *self, PyObject *args)
2302{
Victor Stinner8c62be82010-05-06 00:08:46 +00002303 PyObject *opath;
2304 char *path;
2305 long uid, gid;
2306 int res;
2307 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2308 PyUnicode_FSConverter, &opath,
2309 &uid, &gid))
2310 return NULL;
2311 path = PyBytes_AsString(opath);
2312 Py_BEGIN_ALLOW_THREADS
2313 res = lchown(path, (uid_t) uid, (gid_t) gid);
2314 Py_END_ALLOW_THREADS
2315 if (res < 0)
2316 return posix_error_with_allocated_filename(opath);
2317 Py_DECREF(opath);
2318 Py_INCREF(Py_None);
2319 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002320}
2321#endif /* HAVE_LCHOWN */
2322
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002323
Guido van Rossum36bc6801995-06-14 22:54:23 +00002324#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002325static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002326posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002327{
Victor Stinner8c62be82010-05-06 00:08:46 +00002328 char buf[1026];
2329 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002331#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002332 if (!use_bytes) {
2333 wchar_t wbuf[1026];
2334 wchar_t *wbuf2 = wbuf;
2335 PyObject *resobj;
2336 DWORD len;
2337 Py_BEGIN_ALLOW_THREADS
2338 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2339 /* If the buffer is large enough, len does not include the
2340 terminating \0. If the buffer is too small, len includes
2341 the space needed for the terminator. */
2342 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2343 wbuf2 = malloc(len * sizeof(wchar_t));
2344 if (wbuf2)
2345 len = GetCurrentDirectoryW(len, wbuf2);
2346 }
2347 Py_END_ALLOW_THREADS
2348 if (!wbuf2) {
2349 PyErr_NoMemory();
2350 return NULL;
2351 }
2352 if (!len) {
2353 if (wbuf2 != wbuf) free(wbuf2);
2354 return win32_error("getcwdu", NULL);
2355 }
2356 resobj = PyUnicode_FromWideChar(wbuf2, len);
2357 if (wbuf2 != wbuf) free(wbuf2);
2358 return resobj;
2359 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002360#endif
2361
Victor Stinner8c62be82010-05-06 00:08:46 +00002362 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002363#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002364 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002365#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002367#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 Py_END_ALLOW_THREADS
2369 if (res == NULL)
2370 return posix_error();
2371 if (use_bytes)
2372 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002373 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002374}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002375
2376PyDoc_STRVAR(posix_getcwd__doc__,
2377"getcwd() -> path\n\n\
2378Return a unicode string representing the current working directory.");
2379
2380static PyObject *
2381posix_getcwd_unicode(PyObject *self)
2382{
2383 return posix_getcwd(0);
2384}
2385
2386PyDoc_STRVAR(posix_getcwdb__doc__,
2387"getcwdb() -> path\n\n\
2388Return a bytes string representing the current working directory.");
2389
2390static PyObject *
2391posix_getcwd_bytes(PyObject *self)
2392{
2393 return posix_getcwd(1);
2394}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002395#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002396
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002397
Guido van Rossumb6775db1994-08-01 11:34:53 +00002398#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002399PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002400"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002401Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002402
Barry Warsaw53699e91996-12-10 23:23:01 +00002403static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002404posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002405{
Victor Stinner8c62be82010-05-06 00:08:46 +00002406 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002407}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002408#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002409
Brian Curtin1b9df392010-11-24 20:24:31 +00002410#ifdef MS_WINDOWS
2411PyDoc_STRVAR(win32_link__doc__,
2412"link(src, dst)\n\n\
2413Create a hard link to a file.");
2414
2415static PyObject *
2416win32_link(PyObject *self, PyObject *args)
2417{
2418 PyObject *osrc, *odst;
2419 char *src, *dst;
2420 BOOL rslt;
2421
Brian Curtinfc889c42010-11-28 23:59:46 +00002422 PyUnicodeObject *usrc, *udst;
2423 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2424 Py_BEGIN_ALLOW_THREADS
2425 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2426 PyUnicode_AS_UNICODE(usrc), NULL);
2427 Py_END_ALLOW_THREADS
2428
2429 if (rslt == 0)
2430 return win32_error("link", NULL);
2431
2432 Py_RETURN_NONE;
2433 }
2434
2435 /* Narrow strings also valid. */
2436 PyErr_Clear();
2437
Brian Curtin1b9df392010-11-24 20:24:31 +00002438 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2439 PyUnicode_FSConverter, &odst))
2440 return NULL;
2441
2442 src = PyBytes_AsString(osrc);
2443 dst = PyBytes_AsString(odst);
2444
2445 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002446 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002447 Py_END_ALLOW_THREADS
2448
Stefan Krah30b341f2010-11-27 11:44:18 +00002449 Py_DECREF(osrc);
2450 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002451 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002452 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002453
2454 Py_RETURN_NONE;
2455}
2456#endif /* MS_WINDOWS */
2457
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002459PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002460"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002461Return a list containing the names of the entries in the directory.\n\
2462\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002463 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002464\n\
2465The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002466entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002467
Barry Warsaw53699e91996-12-10 23:23:01 +00002468static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002469posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002470{
Victor Stinner8c62be82010-05-06 00:08:46 +00002471 /* XXX Should redo this putting the (now four) versions of opendir
2472 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002473#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002474
Victor Stinner8c62be82010-05-06 00:08:46 +00002475 PyObject *d, *v;
2476 HANDLE hFindFile;
2477 BOOL result;
2478 WIN32_FIND_DATA FileData;
2479 PyObject *opath;
2480 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2481 char *bufptr = namebuf;
2482 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002483
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002484 PyObject *po = NULL;
2485 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002487 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002488
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002489 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002490 po_wchars = L".";
2491 len = 1;
2492 } else {
2493 po_wchars = PyUnicode_AS_UNICODE(po);
2494 len = PyUnicode_GET_SIZE(po);
2495 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002496 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002497 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2498 if (!wnamebuf) {
2499 PyErr_NoMemory();
2500 return NULL;
2501 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002502 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002503 if (len > 0) {
2504 Py_UNICODE wch = wnamebuf[len-1];
2505 if (wch != L'/' && wch != L'\\' && wch != L':')
2506 wnamebuf[len++] = L'\\';
2507 wcscpy(wnamebuf + len, L"*.*");
2508 }
2509 if ((d = PyList_New(0)) == NULL) {
2510 free(wnamebuf);
2511 return NULL;
2512 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002513 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002514 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002515 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002516 if (hFindFile == INVALID_HANDLE_VALUE) {
2517 int error = GetLastError();
2518 if (error == ERROR_FILE_NOT_FOUND) {
2519 free(wnamebuf);
2520 return d;
2521 }
2522 Py_DECREF(d);
2523 win32_error_unicode("FindFirstFileW", wnamebuf);
2524 free(wnamebuf);
2525 return NULL;
2526 }
2527 do {
2528 /* Skip over . and .. */
2529 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2530 wcscmp(wFileData.cFileName, L"..") != 0) {
2531 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2532 if (v == NULL) {
2533 Py_DECREF(d);
2534 d = NULL;
2535 break;
2536 }
2537 if (PyList_Append(d, v) != 0) {
2538 Py_DECREF(v);
2539 Py_DECREF(d);
2540 d = NULL;
2541 break;
2542 }
2543 Py_DECREF(v);
2544 }
2545 Py_BEGIN_ALLOW_THREADS
2546 result = FindNextFileW(hFindFile, &wFileData);
2547 Py_END_ALLOW_THREADS
2548 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2549 it got to the end of the directory. */
2550 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2551 Py_DECREF(d);
2552 win32_error_unicode("FindNextFileW", wnamebuf);
2553 FindClose(hFindFile);
2554 free(wnamebuf);
2555 return NULL;
2556 }
2557 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002558
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 if (FindClose(hFindFile) == FALSE) {
2560 Py_DECREF(d);
2561 win32_error_unicode("FindClose", wnamebuf);
2562 free(wnamebuf);
2563 return NULL;
2564 }
2565 free(wnamebuf);
2566 return d;
2567 }
2568 /* Drop the argument parsing error as narrow strings
2569 are also valid. */
2570 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002571
Victor Stinner8c62be82010-05-06 00:08:46 +00002572 if (!PyArg_ParseTuple(args, "O&:listdir",
2573 PyUnicode_FSConverter, &opath))
2574 return NULL;
2575 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2576 PyErr_SetString(PyExc_ValueError, "path too long");
2577 Py_DECREF(opath);
2578 return NULL;
2579 }
2580 strcpy(namebuf, PyBytes_AsString(opath));
2581 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002582 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 if (len > 0) {
2584 char ch = namebuf[len-1];
2585 if (ch != SEP && ch != ALTSEP && ch != ':')
2586 namebuf[len++] = '/';
2587 strcpy(namebuf + len, "*.*");
2588 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002589
Victor Stinner8c62be82010-05-06 00:08:46 +00002590 if ((d = PyList_New(0)) == NULL)
2591 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002592
Antoine Pitroub73caab2010-08-09 23:39:31 +00002593 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002595 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002596 if (hFindFile == INVALID_HANDLE_VALUE) {
2597 int error = GetLastError();
2598 if (error == ERROR_FILE_NOT_FOUND)
2599 return d;
2600 Py_DECREF(d);
2601 return win32_error("FindFirstFile", namebuf);
2602 }
2603 do {
2604 /* Skip over . and .. */
2605 if (strcmp(FileData.cFileName, ".") != 0 &&
2606 strcmp(FileData.cFileName, "..") != 0) {
2607 v = PyBytes_FromString(FileData.cFileName);
2608 if (v == NULL) {
2609 Py_DECREF(d);
2610 d = NULL;
2611 break;
2612 }
2613 if (PyList_Append(d, v) != 0) {
2614 Py_DECREF(v);
2615 Py_DECREF(d);
2616 d = NULL;
2617 break;
2618 }
2619 Py_DECREF(v);
2620 }
2621 Py_BEGIN_ALLOW_THREADS
2622 result = FindNextFile(hFindFile, &FileData);
2623 Py_END_ALLOW_THREADS
2624 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2625 it got to the end of the directory. */
2626 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2627 Py_DECREF(d);
2628 win32_error("FindNextFile", namebuf);
2629 FindClose(hFindFile);
2630 return NULL;
2631 }
2632 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002633
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 if (FindClose(hFindFile) == FALSE) {
2635 Py_DECREF(d);
2636 return win32_error("FindClose", namebuf);
2637 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002638
Victor Stinner8c62be82010-05-06 00:08:46 +00002639 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002640
Tim Peters0bb44a42000-09-15 07:44:49 +00002641#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002642
2643#ifndef MAX_PATH
2644#define MAX_PATH CCHMAXPATH
2645#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002646 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002647 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002648 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002649 PyObject *d, *v;
2650 char namebuf[MAX_PATH+5];
2651 HDIR hdir = 1;
2652 ULONG srchcnt = 1;
2653 FILEFINDBUF3 ep;
2654 APIRET rc;
2655
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002657 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002658 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002659 name = PyBytes_AsString(oname);
2660 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002661 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002662 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002663 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002664 return NULL;
2665 }
2666 strcpy(namebuf, name);
2667 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002668 if (*pt == ALTSEP)
2669 *pt = SEP;
2670 if (namebuf[len-1] != SEP)
2671 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002672 strcpy(namebuf + len, "*.*");
2673
Neal Norwitz6c913782007-10-14 03:23:09 +00002674 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002675 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002676 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002677 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002678
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002679 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2680 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002681 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002682 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2683 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2684 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002685
2686 if (rc != NO_ERROR) {
2687 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002688 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002689 }
2690
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002691 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002692 do {
2693 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002694 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002695 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002696
2697 strcpy(namebuf, ep.achName);
2698
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002699 /* Leave Case of Name Alone -- In Native Form */
2700 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002701
Christian Heimes72b710a2008-05-26 13:28:38 +00002702 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002703 if (v == NULL) {
2704 Py_DECREF(d);
2705 d = NULL;
2706 break;
2707 }
2708 if (PyList_Append(d, v) != 0) {
2709 Py_DECREF(v);
2710 Py_DECREF(d);
2711 d = NULL;
2712 break;
2713 }
2714 Py_DECREF(v);
2715 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2716 }
2717
Victor Stinnerdcb24032010-04-22 12:08:36 +00002718 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002719 return d;
2720#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002721 PyObject *oname;
2722 char *name;
2723 PyObject *d, *v;
2724 DIR *dirp;
2725 struct dirent *ep;
2726 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002727
Victor Stinner8c62be82010-05-06 00:08:46 +00002728 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002729 /* v is never read, so it does not need to be initialized yet. */
2730 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002731 arg_is_unicode = 0;
2732 PyErr_Clear();
2733 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002734 oname = NULL;
2735 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002736 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002737 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002738 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002739 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002740 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002741 Py_BEGIN_ALLOW_THREADS
2742 dirp = opendir(name);
2743 Py_END_ALLOW_THREADS
2744 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002745 return posix_error_with_allocated_filename(oname);
2746 }
2747 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002748 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002750 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 Py_DECREF(oname);
2752 return NULL;
2753 }
2754 for (;;) {
2755 errno = 0;
2756 Py_BEGIN_ALLOW_THREADS
2757 ep = readdir(dirp);
2758 Py_END_ALLOW_THREADS
2759 if (ep == NULL) {
2760 if (errno == 0) {
2761 break;
2762 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002763 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002764 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002765 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002766 Py_DECREF(d);
2767 return posix_error_with_allocated_filename(oname);
2768 }
2769 }
2770 if (ep->d_name[0] == '.' &&
2771 (NAMLEN(ep) == 1 ||
2772 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2773 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002774 if (arg_is_unicode)
2775 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2776 else
2777 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002778 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002779 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002780 break;
2781 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 if (PyList_Append(d, v) != 0) {
2783 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002784 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002785 break;
2786 }
2787 Py_DECREF(v);
2788 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002789 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002790 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002791 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002792 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002793
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002795
Tim Peters0bb44a42000-09-15 07:44:49 +00002796#endif /* which OS */
2797} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002798
Antoine Pitrou8250e232011-02-25 23:41:16 +00002799#ifdef HAVE_FDOPENDIR
2800PyDoc_STRVAR(posix_fdlistdir__doc__,
2801"fdlistdir(fd) -> list_of_strings\n\n\
2802Like listdir(), but uses a file descriptor instead.\n\
2803After succesful execution of this function, fd will be closed.");
2804
2805static PyObject *
2806posix_fdlistdir(PyObject *self, PyObject *args)
2807{
2808 PyObject *d, *v;
2809 DIR *dirp;
2810 struct dirent *ep;
2811 int fd;
2812
2813 errno = 0;
2814 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2815 return NULL;
2816 Py_BEGIN_ALLOW_THREADS
2817 dirp = fdopendir(fd);
2818 Py_END_ALLOW_THREADS
2819 if (dirp == NULL) {
2820 close(fd);
2821 return posix_error();
2822 }
2823 if ((d = PyList_New(0)) == NULL) {
2824 Py_BEGIN_ALLOW_THREADS
2825 closedir(dirp);
2826 Py_END_ALLOW_THREADS
2827 return NULL;
2828 }
2829 for (;;) {
2830 errno = 0;
2831 Py_BEGIN_ALLOW_THREADS
2832 ep = readdir(dirp);
2833 Py_END_ALLOW_THREADS
2834 if (ep == NULL) {
2835 if (errno == 0) {
2836 break;
2837 } else {
2838 Py_BEGIN_ALLOW_THREADS
2839 closedir(dirp);
2840 Py_END_ALLOW_THREADS
2841 Py_DECREF(d);
2842 return posix_error();
2843 }
2844 }
2845 if (ep->d_name[0] == '.' &&
2846 (NAMLEN(ep) == 1 ||
2847 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2848 continue;
2849 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2850 if (v == NULL) {
2851 Py_CLEAR(d);
2852 break;
2853 }
2854 if (PyList_Append(d, v) != 0) {
2855 Py_DECREF(v);
2856 Py_CLEAR(d);
2857 break;
2858 }
2859 Py_DECREF(v);
2860 }
2861 Py_BEGIN_ALLOW_THREADS
2862 closedir(dirp);
2863 Py_END_ALLOW_THREADS
2864
2865 return d;
2866}
2867#endif
2868
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002869#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002870/* A helper function for abspath on win32 */
2871static PyObject *
2872posix__getfullpathname(PyObject *self, PyObject *args)
2873{
Victor Stinner8c62be82010-05-06 00:08:46 +00002874 PyObject *opath;
2875 char *path;
2876 char outbuf[MAX_PATH*2];
2877 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002878#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 PyUnicodeObject *po;
2880 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2881 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2882 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2883 Py_UNICODE *wtemp;
2884 DWORD result;
2885 PyObject *v;
2886 result = GetFullPathNameW(wpath,
2887 sizeof(woutbuf)/sizeof(woutbuf[0]),
2888 woutbuf, &wtemp);
2889 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2890 woutbufp = malloc(result * sizeof(Py_UNICODE));
2891 if (!woutbufp)
2892 return PyErr_NoMemory();
2893 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2894 }
2895 if (result)
2896 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2897 else
2898 v = win32_error_unicode("GetFullPathNameW", wpath);
2899 if (woutbufp != woutbuf)
2900 free(woutbufp);
2901 return v;
2902 }
2903 /* Drop the argument parsing error as narrow strings
2904 are also valid. */
2905 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002906
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002907#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002908 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2909 PyUnicode_FSConverter, &opath))
2910 return NULL;
2911 path = PyBytes_AsString(opath);
2912 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2913 outbuf, &temp)) {
2914 win32_error("GetFullPathName", path);
2915 Py_DECREF(opath);
2916 return NULL;
2917 }
2918 Py_DECREF(opath);
2919 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2920 return PyUnicode_Decode(outbuf, strlen(outbuf),
2921 Py_FileSystemDefaultEncoding, NULL);
2922 }
2923 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002924} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002925
Brian Curtind25aef52011-06-13 15:16:04 -05002926
Brian Curtinf5e76d02010-11-24 13:14:05 +00002927
Brian Curtind40e6f72010-07-08 21:39:08 +00002928/* A helper function for samepath on windows */
2929static PyObject *
2930posix__getfinalpathname(PyObject *self, PyObject *args)
2931{
2932 HANDLE hFile;
2933 int buf_size;
2934 wchar_t *target_path;
2935 int result_length;
2936 PyObject *result;
2937 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002938
Brian Curtin94622b02010-09-24 00:03:39 +00002939 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002940 return NULL;
2941 }
2942
2943 if(!check_GetFinalPathNameByHandle()) {
2944 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2945 NotImplementedError. */
2946 return PyErr_Format(PyExc_NotImplementedError,
2947 "GetFinalPathNameByHandle not available on this platform");
2948 }
2949
2950 hFile = CreateFileW(
2951 path,
2952 0, /* desired access */
2953 0, /* share mode */
2954 NULL, /* security attributes */
2955 OPEN_EXISTING,
2956 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2957 FILE_FLAG_BACKUP_SEMANTICS,
2958 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002959
Brian Curtind40e6f72010-07-08 21:39:08 +00002960 if(hFile == INVALID_HANDLE_VALUE) {
2961 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002962 return PyErr_Format(PyExc_RuntimeError,
2963 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002964 }
2965
2966 /* We have a good handle to the target, use it to determine the
2967 target path name. */
2968 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2969
2970 if(!buf_size)
2971 return win32_error_unicode("GetFinalPathNameByHandle", path);
2972
2973 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2974 if(!target_path)
2975 return PyErr_NoMemory();
2976
2977 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2978 buf_size, VOLUME_NAME_DOS);
2979 if(!result_length)
2980 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2981
2982 if(!CloseHandle(hFile))
2983 return win32_error_unicode("GetFinalPathNameByHandle", path);
2984
2985 target_path[result_length] = 0;
2986 result = PyUnicode_FromUnicode(target_path, result_length);
2987 free(target_path);
2988 return result;
2989
2990} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002991
2992static PyObject *
2993posix__getfileinformation(PyObject *self, PyObject *args)
2994{
2995 HANDLE hFile;
2996 BY_HANDLE_FILE_INFORMATION info;
2997 int fd;
2998
2999 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3000 return NULL;
3001
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003002 if (!_PyVerify_fd(fd))
3003 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003004
3005 hFile = (HANDLE)_get_osfhandle(fd);
3006 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003007 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003008
3009 if (!GetFileInformationByHandle(hFile, &info))
3010 return win32_error("_getfileinformation", NULL);
3011
3012 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3013 info.nFileIndexHigh,
3014 info.nFileIndexLow);
3015}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003016
Brian Curtin95d028f2011-06-09 09:10:38 -05003017PyDoc_STRVAR(posix__isdir__doc__,
3018"Return true if the pathname refers to an existing directory.");
3019
Brian Curtin9c669cc2011-06-08 18:17:18 -05003020static PyObject *
3021posix__isdir(PyObject *self, PyObject *args)
3022{
3023 PyObject *opath;
3024 char *path;
3025 PyUnicodeObject *po;
3026 DWORD attributes;
3027
3028 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3029 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3030
3031 attributes = GetFileAttributesW(wpath);
3032 if (attributes == INVALID_FILE_ATTRIBUTES)
3033 Py_RETURN_FALSE;
3034 goto check;
3035 }
3036 /* Drop the argument parsing error as narrow strings
3037 are also valid. */
3038 PyErr_Clear();
3039
3040 if (!PyArg_ParseTuple(args, "O&:_isdir",
3041 PyUnicode_FSConverter, &opath))
3042 return NULL;
3043
3044 path = PyBytes_AsString(opath);
3045 attributes = GetFileAttributesA(path);
3046 if (attributes == INVALID_FILE_ATTRIBUTES)
3047 Py_RETURN_FALSE;
3048
3049check:
3050 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3051 Py_RETURN_TRUE;
3052 else
3053 Py_RETURN_FALSE;
3054}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003055#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003057PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003058"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003059Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003060
Barry Warsaw53699e91996-12-10 23:23:01 +00003061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003062posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003063{
Victor Stinner8c62be82010-05-06 00:08:46 +00003064 int res;
3065 PyObject *opath;
3066 char *path;
3067 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003068
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003069#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003070 PyUnicodeObject *po;
3071 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3072 Py_BEGIN_ALLOW_THREADS
3073 /* PyUnicode_AS_UNICODE OK without thread lock as
3074 it is a simple dereference. */
3075 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3076 Py_END_ALLOW_THREADS
3077 if (!res)
3078 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3079 Py_INCREF(Py_None);
3080 return Py_None;
3081 }
3082 /* Drop the argument parsing error as narrow strings
3083 are also valid. */
3084 PyErr_Clear();
3085 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3086 PyUnicode_FSConverter, &opath, &mode))
3087 return NULL;
3088 path = PyBytes_AsString(opath);
3089 Py_BEGIN_ALLOW_THREADS
3090 /* PyUnicode_AS_UNICODE OK without thread lock as
3091 it is a simple dereference. */
3092 res = CreateDirectoryA(path, NULL);
3093 Py_END_ALLOW_THREADS
3094 if (!res) {
3095 win32_error("mkdir", path);
3096 Py_DECREF(opath);
3097 return NULL;
3098 }
3099 Py_DECREF(opath);
3100 Py_INCREF(Py_None);
3101 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003102#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003103
Victor Stinner8c62be82010-05-06 00:08:46 +00003104 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3105 PyUnicode_FSConverter, &opath, &mode))
3106 return NULL;
3107 path = PyBytes_AsString(opath);
3108 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003109#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003111#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003112 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003113#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 Py_END_ALLOW_THREADS
3115 if (res < 0)
3116 return posix_error_with_allocated_filename(opath);
3117 Py_DECREF(opath);
3118 Py_INCREF(Py_None);
3119 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003121}
3122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003124/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3125#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003126#include <sys/resource.h>
3127#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003128
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003129
3130#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003131PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003132"nice(inc) -> new_priority\n\n\
3133Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003134
Barry Warsaw53699e91996-12-10 23:23:01 +00003135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003136posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003137{
Victor Stinner8c62be82010-05-06 00:08:46 +00003138 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003139
Victor Stinner8c62be82010-05-06 00:08:46 +00003140 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3141 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003142
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 /* There are two flavours of 'nice': one that returns the new
3144 priority (as required by almost all standards out there) and the
3145 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3146 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003147
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 If we are of the nice family that returns the new priority, we
3149 need to clear errno before the call, and check if errno is filled
3150 before calling posix_error() on a returnvalue of -1, because the
3151 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003152
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 errno = 0;
3154 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003155#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003156 if (value == 0)
3157 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003158#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 if (value == -1 && errno != 0)
3160 /* either nice() or getpriority() returned an error */
3161 return posix_error();
3162 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003163}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003164#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003165
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003166
3167#ifdef HAVE_GETPRIORITY
3168PyDoc_STRVAR(posix_getpriority__doc__,
3169"getpriority(which, who) -> current_priority\n\n\
3170Get program scheduling priority.");
3171
3172static PyObject *
3173posix_getpriority(PyObject *self, PyObject *args)
3174{
3175 int which, who, retval;
3176
3177 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3178 return NULL;
3179 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003180 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003181 if (errno != 0)
3182 return posix_error();
3183 return PyLong_FromLong((long)retval);
3184}
3185#endif /* HAVE_GETPRIORITY */
3186
3187
3188#ifdef HAVE_SETPRIORITY
3189PyDoc_STRVAR(posix_setpriority__doc__,
3190"setpriority(which, who, prio) -> None\n\n\
3191Set program scheduling priority.");
3192
3193static PyObject *
3194posix_setpriority(PyObject *self, PyObject *args)
3195{
3196 int which, who, prio, retval;
3197
3198 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3199 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003200 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003201 if (retval == -1)
3202 return posix_error();
3203 Py_RETURN_NONE;
3204}
3205#endif /* HAVE_SETPRIORITY */
3206
3207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003208PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003209"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211
Barry Warsaw53699e91996-12-10 23:23:01 +00003212static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003213posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003214{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003215#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 PyObject *o1, *o2;
3217 char *p1, *p2;
3218 BOOL result;
3219 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3220 goto error;
3221 if (!convert_to_unicode(&o1))
3222 goto error;
3223 if (!convert_to_unicode(&o2)) {
3224 Py_DECREF(o1);
3225 goto error;
3226 }
3227 Py_BEGIN_ALLOW_THREADS
3228 result = MoveFileW(PyUnicode_AsUnicode(o1),
3229 PyUnicode_AsUnicode(o2));
3230 Py_END_ALLOW_THREADS
3231 Py_DECREF(o1);
3232 Py_DECREF(o2);
3233 if (!result)
3234 return win32_error("rename", NULL);
3235 Py_INCREF(Py_None);
3236 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003237error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003238 PyErr_Clear();
3239 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3240 return NULL;
3241 Py_BEGIN_ALLOW_THREADS
3242 result = MoveFileA(p1, p2);
3243 Py_END_ALLOW_THREADS
3244 if (!result)
3245 return win32_error("rename", NULL);
3246 Py_INCREF(Py_None);
3247 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003248#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003250#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003251}
3252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003254PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003255"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003256Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003257
Barry Warsaw53699e91996-12-10 23:23:01 +00003258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003259posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003260{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003261#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003262 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003263#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003265#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003266}
3267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003269PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003270"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003271Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003272
Barry Warsaw53699e91996-12-10 23:23:01 +00003273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003274posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003275{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003276#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003277 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003278#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003279 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003280#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003281}
3282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003283
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003284#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003285PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003286"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003287Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003288
Barry Warsaw53699e91996-12-10 23:23:01 +00003289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003290posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003291{
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003293#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003294 wchar_t *command;
3295 if (!PyArg_ParseTuple(args, "u:system", &command))
3296 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003297
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 Py_BEGIN_ALLOW_THREADS
3299 sts = _wsystem(command);
3300 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003301#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 PyObject *command_obj;
3303 char *command;
3304 if (!PyArg_ParseTuple(args, "O&:system",
3305 PyUnicode_FSConverter, &command_obj))
3306 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003307
Victor Stinner8c62be82010-05-06 00:08:46 +00003308 command = PyBytes_AsString(command_obj);
3309 Py_BEGIN_ALLOW_THREADS
3310 sts = system(command);
3311 Py_END_ALLOW_THREADS
3312 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003313#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003315}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003316#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003317
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003319PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003320"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003321Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003322
Barry Warsaw53699e91996-12-10 23:23:01 +00003323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003324posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003325{
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 int i;
3327 if (!PyArg_ParseTuple(args, "i:umask", &i))
3328 return NULL;
3329 i = (int)umask(i);
3330 if (i < 0)
3331 return posix_error();
3332 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003333}
3334
Brian Curtind40e6f72010-07-08 21:39:08 +00003335#ifdef MS_WINDOWS
3336
3337/* override the default DeleteFileW behavior so that directory
3338symlinks can be removed with this function, the same as with
3339Unix symlinks */
3340BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3341{
3342 WIN32_FILE_ATTRIBUTE_DATA info;
3343 WIN32_FIND_DATAW find_data;
3344 HANDLE find_data_handle;
3345 int is_directory = 0;
3346 int is_link = 0;
3347
3348 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3349 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003350
Brian Curtind40e6f72010-07-08 21:39:08 +00003351 /* Get WIN32_FIND_DATA structure for the path to determine if
3352 it is a symlink */
3353 if(is_directory &&
3354 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3355 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3356
3357 if(find_data_handle != INVALID_HANDLE_VALUE) {
3358 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3359 FindClose(find_data_handle);
3360 }
3361 }
3362 }
3363
3364 if (is_directory && is_link)
3365 return RemoveDirectoryW(lpFileName);
3366
3367 return DeleteFileW(lpFileName);
3368}
3369#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003371PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003372"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003373Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003376"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003377Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003378
Barry Warsaw53699e91996-12-10 23:23:01 +00003379static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003380posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003381{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003382#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003383 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3384 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003385#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003386 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003387#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003388}
3389
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003390
Guido van Rossumb6775db1994-08-01 11:34:53 +00003391#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003392PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003393"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003394Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003395
Barry Warsaw53699e91996-12-10 23:23:01 +00003396static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003397posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003398{
Victor Stinner8c62be82010-05-06 00:08:46 +00003399 struct utsname u;
3400 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003401
Victor Stinner8c62be82010-05-06 00:08:46 +00003402 Py_BEGIN_ALLOW_THREADS
3403 res = uname(&u);
3404 Py_END_ALLOW_THREADS
3405 if (res < 0)
3406 return posix_error();
3407 return Py_BuildValue("(sssss)",
3408 u.sysname,
3409 u.nodename,
3410 u.release,
3411 u.version,
3412 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003413}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003414#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003415
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003416static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003417extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003418{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003419 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003420 if (PyFloat_Check(t)) {
3421 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003422 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003423 if (!intobj)
3424 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003425#if SIZEOF_TIME_T > SIZEOF_LONG
3426 intval = PyLong_AsUnsignedLongLongMask(intobj);
3427#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003429#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 Py_DECREF(intobj);
3431 if (intval == -1 && PyErr_Occurred())
3432 return -1;
3433 *sec = intval;
3434 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3435 if (*usec < 0)
3436 /* If rounding gave us a negative number,
3437 truncate. */
3438 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003439 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003441#if SIZEOF_TIME_T > SIZEOF_LONG
3442 intval = PyLong_AsUnsignedLongLongMask(t);
3443#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003445#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 if (intval == -1 && PyErr_Occurred())
3447 return -1;
3448 *sec = intval;
3449 *usec = 0;
3450 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003451}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003453PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003454"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003455utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003456Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003457second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003458
Barry Warsaw53699e91996-12-10 23:23:01 +00003459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003460posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003461{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003462#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 PyObject *arg;
3464 PyUnicodeObject *obwpath;
3465 wchar_t *wpath = NULL;
3466 PyObject *oapath;
3467 char *apath;
3468 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003469 time_t atimesec, mtimesec;
3470 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003471 FILETIME atime, mtime;
3472 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003473
Victor Stinner8c62be82010-05-06 00:08:46 +00003474 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3475 wpath = PyUnicode_AS_UNICODE(obwpath);
3476 Py_BEGIN_ALLOW_THREADS
3477 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3478 NULL, OPEN_EXISTING,
3479 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3480 Py_END_ALLOW_THREADS
3481 if (hFile == INVALID_HANDLE_VALUE)
3482 return win32_error_unicode("utime", wpath);
3483 } else
3484 /* Drop the argument parsing error as narrow strings
3485 are also valid. */
3486 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003487
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 if (!wpath) {
3489 if (!PyArg_ParseTuple(args, "O&O:utime",
3490 PyUnicode_FSConverter, &oapath, &arg))
3491 return NULL;
3492 apath = PyBytes_AsString(oapath);
3493 Py_BEGIN_ALLOW_THREADS
3494 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3495 NULL, OPEN_EXISTING,
3496 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3497 Py_END_ALLOW_THREADS
3498 if (hFile == INVALID_HANDLE_VALUE) {
3499 win32_error("utime", apath);
3500 Py_DECREF(oapath);
3501 return NULL;
3502 }
3503 Py_DECREF(oapath);
3504 }
3505
3506 if (arg == Py_None) {
3507 SYSTEMTIME now;
3508 GetSystemTime(&now);
3509 if (!SystemTimeToFileTime(&now, &mtime) ||
3510 !SystemTimeToFileTime(&now, &atime)) {
3511 win32_error("utime", NULL);
3512 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003513 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 }
3515 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3516 PyErr_SetString(PyExc_TypeError,
3517 "utime() arg 2 must be a tuple (atime, mtime)");
3518 goto done;
3519 }
3520 else {
3521 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3522 &atimesec, &ausec) == -1)
3523 goto done;
3524 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3525 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3526 &mtimesec, &musec) == -1)
3527 goto done;
3528 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3529 }
3530 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3531 /* Avoid putting the file name into the error here,
3532 as that may confuse the user into believing that
3533 something is wrong with the file, when it also
3534 could be the time stamp that gives a problem. */
3535 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003536 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003537 }
3538 Py_INCREF(Py_None);
3539 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003540done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003541 CloseHandle(hFile);
3542 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003543#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003544
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 PyObject *opath;
3546 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003547 time_t atime, mtime;
3548 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003549 int res;
3550 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003551
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003552#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003554#define ATIME buf[0].tv_sec
3555#define MTIME buf[1].tv_sec
3556#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003557/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003559#define ATIME buf.actime
3560#define MTIME buf.modtime
3561#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003562#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003564#define ATIME buf[0]
3565#define MTIME buf[1]
3566#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003567#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003568
Mark Hammond817c9292003-12-03 01:22:38 +00003569
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 if (!PyArg_ParseTuple(args, "O&O:utime",
3571 PyUnicode_FSConverter, &opath, &arg))
3572 return NULL;
3573 path = PyBytes_AsString(opath);
3574 if (arg == Py_None) {
3575 /* optional time values not given */
3576 Py_BEGIN_ALLOW_THREADS
3577 res = utime(path, NULL);
3578 Py_END_ALLOW_THREADS
3579 }
3580 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3581 PyErr_SetString(PyExc_TypeError,
3582 "utime() arg 2 must be a tuple (atime, mtime)");
3583 Py_DECREF(opath);
3584 return NULL;
3585 }
3586 else {
3587 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3588 &atime, &ausec) == -1) {
3589 Py_DECREF(opath);
3590 return NULL;
3591 }
3592 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3593 &mtime, &musec) == -1) {
3594 Py_DECREF(opath);
3595 return NULL;
3596 }
3597 ATIME = atime;
3598 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003599#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003600 buf[0].tv_usec = ausec;
3601 buf[1].tv_usec = musec;
3602 Py_BEGIN_ALLOW_THREADS
3603 res = utimes(path, buf);
3604 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003605#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003606 Py_BEGIN_ALLOW_THREADS
3607 res = utime(path, UTIME_ARG);
3608 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003609#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 }
3611 if (res < 0) {
3612 return posix_error_with_allocated_filename(opath);
3613 }
3614 Py_DECREF(opath);
3615 Py_INCREF(Py_None);
3616 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003617#undef UTIME_ARG
3618#undef ATIME
3619#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003620#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003621}
3622
Ross Lagerwall7807c352011-03-17 20:20:30 +02003623#ifdef HAVE_FUTIMES
3624PyDoc_STRVAR(posix_futimes__doc__,
3625"futimes(fd, (atime, mtime))\n\
3626futimes(fd, None)\n\n\
3627Set the access and modified time of the file specified by the file\n\
3628descriptor fd to the given values. If the second form is used, set the\n\
3629access and modified times to the current time.");
3630
3631static PyObject *
3632posix_futimes(PyObject *self, PyObject *args)
3633{
3634 int res, fd;
3635 PyObject* arg;
3636 struct timeval buf[2];
3637 long ausec, musec;
3638
3639 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3640 return NULL;
3641
3642 if (arg == Py_None) {
3643 /* optional time values not given */
3644 Py_BEGIN_ALLOW_THREADS
3645 res = futimes(fd, NULL);
3646 Py_END_ALLOW_THREADS
3647 }
3648 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3649 PyErr_SetString(PyExc_TypeError,
3650 "futimes() arg 2 must be a tuple (atime, mtime)");
3651 return NULL;
3652 }
3653 else {
3654 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3655 &(buf[0].tv_sec), &ausec) == -1) {
3656 return NULL;
3657 }
3658 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3659 &(buf[1].tv_sec), &musec) == -1) {
3660 return NULL;
3661 }
3662 buf[0].tv_usec = ausec;
3663 buf[1].tv_usec = musec;
3664 Py_BEGIN_ALLOW_THREADS
3665 res = futimes(fd, buf);
3666 Py_END_ALLOW_THREADS
3667 }
3668 if (res < 0)
3669 return posix_error();
3670 Py_RETURN_NONE;
3671}
3672#endif
3673
3674#ifdef HAVE_LUTIMES
3675PyDoc_STRVAR(posix_lutimes__doc__,
3676"lutimes(path, (atime, mtime))\n\
3677lutimes(path, None)\n\n\
3678Like utime(), but if path is a symbolic link, it is not dereferenced.");
3679
3680static PyObject *
3681posix_lutimes(PyObject *self, PyObject *args)
3682{
3683 PyObject *opath, *arg;
3684 const char *path;
3685 int res;
3686 struct timeval buf[2];
3687 long ausec, musec;
3688
3689 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3690 PyUnicode_FSConverter, &opath, &arg))
3691 return NULL;
3692 path = PyBytes_AsString(opath);
3693 if (arg == Py_None) {
3694 /* optional time values not given */
3695 Py_BEGIN_ALLOW_THREADS
3696 res = lutimes(path, NULL);
3697 Py_END_ALLOW_THREADS
3698 }
3699 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3700 PyErr_SetString(PyExc_TypeError,
3701 "lutimes() arg 2 must be a tuple (atime, mtime)");
3702 Py_DECREF(opath);
3703 return NULL;
3704 }
3705 else {
3706 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3707 &(buf[0].tv_sec), &ausec) == -1) {
3708 Py_DECREF(opath);
3709 return NULL;
3710 }
3711 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3712 &(buf[1].tv_sec), &musec) == -1) {
3713 Py_DECREF(opath);
3714 return NULL;
3715 }
3716 buf[0].tv_usec = ausec;
3717 buf[1].tv_usec = musec;
3718 Py_BEGIN_ALLOW_THREADS
3719 res = lutimes(path, buf);
3720 Py_END_ALLOW_THREADS
3721 }
3722 Py_DECREF(opath);
3723 if (res < 0)
3724 return posix_error();
3725 Py_RETURN_NONE;
3726}
3727#endif
3728
3729#ifdef HAVE_FUTIMENS
3730PyDoc_STRVAR(posix_futimens__doc__,
3731"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3732futimens(fd, None, None)\n\n\
3733Updates the timestamps of a file specified by the file descriptor fd, with\n\
3734nanosecond precision.\n\
3735The second form sets atime and mtime to the current time.\n\
3736If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3737current time.\n\
3738If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3739
3740static PyObject *
3741posix_futimens(PyObject *self, PyObject *args)
3742{
3743 int res, fd;
3744 PyObject *atime, *mtime;
3745 struct timespec buf[2];
3746
3747 if (!PyArg_ParseTuple(args, "iOO:futimens",
3748 &fd, &atime, &mtime))
3749 return NULL;
3750 if (atime == Py_None && mtime == Py_None) {
3751 /* optional time values not given */
3752 Py_BEGIN_ALLOW_THREADS
3753 res = futimens(fd, NULL);
3754 Py_END_ALLOW_THREADS
3755 }
3756 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3757 PyErr_SetString(PyExc_TypeError,
3758 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3759 return NULL;
3760 }
3761 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3762 PyErr_SetString(PyExc_TypeError,
3763 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3764 return NULL;
3765 }
3766 else {
3767 if (!PyArg_ParseTuple(atime, "ll:futimens",
3768 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3769 return NULL;
3770 }
3771 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3772 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3773 return NULL;
3774 }
3775 Py_BEGIN_ALLOW_THREADS
3776 res = futimens(fd, buf);
3777 Py_END_ALLOW_THREADS
3778 }
3779 if (res < 0)
3780 return posix_error();
3781 Py_RETURN_NONE;
3782}
3783#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003784
Guido van Rossum3b066191991-06-04 19:40:25 +00003785/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003786
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Barry Warsaw53699e91996-12-10 23:23:01 +00003791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003792posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003793{
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 int sts;
3795 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3796 return NULL;
3797 _exit(sts);
3798 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003799}
3800
Martin v. Löwis114619e2002-10-07 06:44:21 +00003801#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3802static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003803free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003804{
Victor Stinner8c62be82010-05-06 00:08:46 +00003805 Py_ssize_t i;
3806 for (i = 0; i < count; i++)
3807 PyMem_Free(array[i]);
3808 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003809}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003810
Antoine Pitrou69f71142009-05-24 21:25:49 +00003811static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003812int fsconvert_strdup(PyObject *o, char**out)
3813{
Victor Stinner8c62be82010-05-06 00:08:46 +00003814 PyObject *bytes;
3815 Py_ssize_t size;
3816 if (!PyUnicode_FSConverter(o, &bytes))
3817 return 0;
3818 size = PyBytes_GET_SIZE(bytes);
3819 *out = PyMem_Malloc(size+1);
3820 if (!*out)
3821 return 0;
3822 memcpy(*out, PyBytes_AsString(bytes), size+1);
3823 Py_DECREF(bytes);
3824 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003825}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003826#endif
3827
Ross Lagerwall7807c352011-03-17 20:20:30 +02003828#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003829static char**
3830parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3831{
Victor Stinner8c62be82010-05-06 00:08:46 +00003832 char **envlist;
3833 Py_ssize_t i, pos, envc;
3834 PyObject *keys=NULL, *vals=NULL;
3835 PyObject *key, *val, *key2, *val2;
3836 char *p, *k, *v;
3837 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003838
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 i = PyMapping_Size(env);
3840 if (i < 0)
3841 return NULL;
3842 envlist = PyMem_NEW(char *, i + 1);
3843 if (envlist == NULL) {
3844 PyErr_NoMemory();
3845 return NULL;
3846 }
3847 envc = 0;
3848 keys = PyMapping_Keys(env);
3849 vals = PyMapping_Values(env);
3850 if (!keys || !vals)
3851 goto error;
3852 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3853 PyErr_Format(PyExc_TypeError,
3854 "env.keys() or env.values() is not a list");
3855 goto error;
3856 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003857
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 for (pos = 0; pos < i; pos++) {
3859 key = PyList_GetItem(keys, pos);
3860 val = PyList_GetItem(vals, pos);
3861 if (!key || !val)
3862 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003863
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 if (PyUnicode_FSConverter(key, &key2) == 0)
3865 goto error;
3866 if (PyUnicode_FSConverter(val, &val2) == 0) {
3867 Py_DECREF(key2);
3868 goto error;
3869 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003870
3871#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003872 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3873 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003874#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003875 k = PyBytes_AsString(key2);
3876 v = PyBytes_AsString(val2);
3877 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003878
Victor Stinner8c62be82010-05-06 00:08:46 +00003879 p = PyMem_NEW(char, len);
3880 if (p == NULL) {
3881 PyErr_NoMemory();
3882 Py_DECREF(key2);
3883 Py_DECREF(val2);
3884 goto error;
3885 }
3886 PyOS_snprintf(p, len, "%s=%s", k, v);
3887 envlist[envc++] = p;
3888 Py_DECREF(key2);
3889 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003890#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003891 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003892#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 }
3894 Py_DECREF(vals);
3895 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003896
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 envlist[envc] = 0;
3898 *envc_ptr = envc;
3899 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003900
3901error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003902 Py_XDECREF(keys);
3903 Py_XDECREF(vals);
3904 while (--envc >= 0)
3905 PyMem_DEL(envlist[envc]);
3906 PyMem_DEL(envlist);
3907 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003908}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003909
Ross Lagerwall7807c352011-03-17 20:20:30 +02003910static char**
3911parse_arglist(PyObject* argv, Py_ssize_t *argc)
3912{
3913 int i;
3914 char **argvlist = PyMem_NEW(char *, *argc+1);
3915 if (argvlist == NULL) {
3916 PyErr_NoMemory();
3917 return NULL;
3918 }
3919 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003920 PyObject* item = PySequence_ITEM(argv, i);
3921 if (item == NULL)
3922 goto fail;
3923 if (!fsconvert_strdup(item, &argvlist[i])) {
3924 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003925 goto fail;
3926 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003927 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003928 }
3929 argvlist[*argc] = NULL;
3930 return argvlist;
3931fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003932 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003933 free_string_array(argvlist, *argc);
3934 return NULL;
3935}
3936#endif
3937
3938#ifdef HAVE_EXECV
3939PyDoc_STRVAR(posix_execv__doc__,
3940"execv(path, args)\n\n\
3941Execute an executable path with arguments, replacing current process.\n\
3942\n\
3943 path: path of executable file\n\
3944 args: tuple or list of strings");
3945
3946static PyObject *
3947posix_execv(PyObject *self, PyObject *args)
3948{
3949 PyObject *opath;
3950 char *path;
3951 PyObject *argv;
3952 char **argvlist;
3953 Py_ssize_t argc;
3954
3955 /* execv has two arguments: (path, argv), where
3956 argv is a list or tuple of strings. */
3957
3958 if (!PyArg_ParseTuple(args, "O&O:execv",
3959 PyUnicode_FSConverter,
3960 &opath, &argv))
3961 return NULL;
3962 path = PyBytes_AsString(opath);
3963 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3964 PyErr_SetString(PyExc_TypeError,
3965 "execv() arg 2 must be a tuple or list");
3966 Py_DECREF(opath);
3967 return NULL;
3968 }
3969 argc = PySequence_Size(argv);
3970 if (argc < 1) {
3971 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3972 Py_DECREF(opath);
3973 return NULL;
3974 }
3975
3976 argvlist = parse_arglist(argv, &argc);
3977 if (argvlist == NULL) {
3978 Py_DECREF(opath);
3979 return NULL;
3980 }
3981
3982 execv(path, argvlist);
3983
3984 /* If we get here it's definitely an error */
3985
3986 free_string_array(argvlist, argc);
3987 Py_DECREF(opath);
3988 return posix_error();
3989}
3990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003991PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003992"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003993Execute a path with arguments and environment, replacing current process.\n\
3994\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 path: path of executable file\n\
3996 args: tuple or list of arguments\n\
3997 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003998
Barry Warsaw53699e91996-12-10 23:23:01 +00003999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004000posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004001{
Victor Stinner8c62be82010-05-06 00:08:46 +00004002 PyObject *opath;
4003 char *path;
4004 PyObject *argv, *env;
4005 char **argvlist;
4006 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004007 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004008
Victor Stinner8c62be82010-05-06 00:08:46 +00004009 /* execve has three arguments: (path, argv, env), where
4010 argv is a list or tuple of strings and env is a dictionary
4011 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004012
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 if (!PyArg_ParseTuple(args, "O&OO:execve",
4014 PyUnicode_FSConverter,
4015 &opath, &argv, &env))
4016 return NULL;
4017 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004018 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004019 PyErr_SetString(PyExc_TypeError,
4020 "execve() arg 2 must be a tuple or list");
4021 goto fail_0;
4022 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004023 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004024 if (!PyMapping_Check(env)) {
4025 PyErr_SetString(PyExc_TypeError,
4026 "execve() arg 3 must be a mapping object");
4027 goto fail_0;
4028 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004029
Ross Lagerwall7807c352011-03-17 20:20:30 +02004030 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 goto fail_0;
4033 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004034
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 envlist = parse_envlist(env, &envc);
4036 if (envlist == NULL)
4037 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004038
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004040
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004042
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004044
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 while (--envc >= 0)
4046 PyMem_DEL(envlist[envc]);
4047 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004048 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004049 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004050 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004051 Py_DECREF(opath);
4052 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004053}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004054#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004055
Ross Lagerwall7807c352011-03-17 20:20:30 +02004056#ifdef HAVE_FEXECVE
4057PyDoc_STRVAR(posix_fexecve__doc__,
4058"fexecve(fd, args, env)\n\n\
4059Execute the program specified by a file descriptor with arguments and\n\
4060environment, replacing the current process.\n\
4061\n\
4062 fd: file descriptor of executable\n\
4063 args: tuple or list of arguments\n\
4064 env: dictionary of strings mapping to strings");
4065
4066static PyObject *
4067posix_fexecve(PyObject *self, PyObject *args)
4068{
4069 int fd;
4070 PyObject *argv, *env;
4071 char **argvlist;
4072 char **envlist;
4073 Py_ssize_t argc, envc;
4074
4075 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4076 &fd, &argv, &env))
4077 return NULL;
4078 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4079 PyErr_SetString(PyExc_TypeError,
4080 "fexecve() arg 2 must be a tuple or list");
4081 return NULL;
4082 }
4083 argc = PySequence_Size(argv);
4084 if (!PyMapping_Check(env)) {
4085 PyErr_SetString(PyExc_TypeError,
4086 "fexecve() arg 3 must be a mapping object");
4087 return NULL;
4088 }
4089
4090 argvlist = parse_arglist(argv, &argc);
4091 if (argvlist == NULL)
4092 return NULL;
4093
4094 envlist = parse_envlist(env, &envc);
4095 if (envlist == NULL)
4096 goto fail;
4097
4098 fexecve(fd, argvlist, envlist);
4099
4100 /* If we get here it's definitely an error */
4101
4102 (void) posix_error();
4103
4104 while (--envc >= 0)
4105 PyMem_DEL(envlist[envc]);
4106 PyMem_DEL(envlist);
4107 fail:
4108 free_string_array(argvlist, argc);
4109 return NULL;
4110}
4111#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004112
Guido van Rossuma1065681999-01-25 23:20:23 +00004113#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004114PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004115"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004116Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004117\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004118 mode: mode of process creation\n\
4119 path: path of executable file\n\
4120 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004121
4122static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004123posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004124{
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 PyObject *opath;
4126 char *path;
4127 PyObject *argv;
4128 char **argvlist;
4129 int mode, i;
4130 Py_ssize_t argc;
4131 Py_intptr_t spawnval;
4132 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004133
Victor Stinner8c62be82010-05-06 00:08:46 +00004134 /* spawnv has three arguments: (mode, path, argv), where
4135 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004136
Victor Stinner8c62be82010-05-06 00:08:46 +00004137 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4138 PyUnicode_FSConverter,
4139 &opath, &argv))
4140 return NULL;
4141 path = PyBytes_AsString(opath);
4142 if (PyList_Check(argv)) {
4143 argc = PyList_Size(argv);
4144 getitem = PyList_GetItem;
4145 }
4146 else if (PyTuple_Check(argv)) {
4147 argc = PyTuple_Size(argv);
4148 getitem = PyTuple_GetItem;
4149 }
4150 else {
4151 PyErr_SetString(PyExc_TypeError,
4152 "spawnv() arg 2 must be a tuple or list");
4153 Py_DECREF(opath);
4154 return NULL;
4155 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004156
Victor Stinner8c62be82010-05-06 00:08:46 +00004157 argvlist = PyMem_NEW(char *, argc+1);
4158 if (argvlist == NULL) {
4159 Py_DECREF(opath);
4160 return PyErr_NoMemory();
4161 }
4162 for (i = 0; i < argc; i++) {
4163 if (!fsconvert_strdup((*getitem)(argv, i),
4164 &argvlist[i])) {
4165 free_string_array(argvlist, i);
4166 PyErr_SetString(
4167 PyExc_TypeError,
4168 "spawnv() arg 2 must contain only strings");
4169 Py_DECREF(opath);
4170 return NULL;
4171 }
4172 }
4173 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004174
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004175#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 Py_BEGIN_ALLOW_THREADS
4177 spawnval = spawnv(mode, path, argvlist);
4178 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004179#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 if (mode == _OLD_P_OVERLAY)
4181 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004182
Victor Stinner8c62be82010-05-06 00:08:46 +00004183 Py_BEGIN_ALLOW_THREADS
4184 spawnval = _spawnv(mode, path, argvlist);
4185 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004186#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004187
Victor Stinner8c62be82010-05-06 00:08:46 +00004188 free_string_array(argvlist, argc);
4189 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004190
Victor Stinner8c62be82010-05-06 00:08:46 +00004191 if (spawnval == -1)
4192 return posix_error();
4193 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004194#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004195 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004196#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004197 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004198#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004199}
4200
4201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004202PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004203"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004204Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004205\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004206 mode: mode of process creation\n\
4207 path: path of executable file\n\
4208 args: tuple or list of arguments\n\
4209 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004210
4211static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004212posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004213{
Victor Stinner8c62be82010-05-06 00:08:46 +00004214 PyObject *opath;
4215 char *path;
4216 PyObject *argv, *env;
4217 char **argvlist;
4218 char **envlist;
4219 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004220 int mode;
4221 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004222 Py_intptr_t spawnval;
4223 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4224 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004225
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 /* spawnve has four arguments: (mode, path, argv, env), where
4227 argv is a list or tuple of strings and env is a dictionary
4228 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004229
Victor Stinner8c62be82010-05-06 00:08:46 +00004230 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4231 PyUnicode_FSConverter,
4232 &opath, &argv, &env))
4233 return NULL;
4234 path = PyBytes_AsString(opath);
4235 if (PyList_Check(argv)) {
4236 argc = PyList_Size(argv);
4237 getitem = PyList_GetItem;
4238 }
4239 else if (PyTuple_Check(argv)) {
4240 argc = PyTuple_Size(argv);
4241 getitem = PyTuple_GetItem;
4242 }
4243 else {
4244 PyErr_SetString(PyExc_TypeError,
4245 "spawnve() arg 2 must be a tuple or list");
4246 goto fail_0;
4247 }
4248 if (!PyMapping_Check(env)) {
4249 PyErr_SetString(PyExc_TypeError,
4250 "spawnve() arg 3 must be a mapping object");
4251 goto fail_0;
4252 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004253
Victor Stinner8c62be82010-05-06 00:08:46 +00004254 argvlist = PyMem_NEW(char *, argc+1);
4255 if (argvlist == NULL) {
4256 PyErr_NoMemory();
4257 goto fail_0;
4258 }
4259 for (i = 0; i < argc; i++) {
4260 if (!fsconvert_strdup((*getitem)(argv, i),
4261 &argvlist[i]))
4262 {
4263 lastarg = i;
4264 goto fail_1;
4265 }
4266 }
4267 lastarg = argc;
4268 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004269
Victor Stinner8c62be82010-05-06 00:08:46 +00004270 envlist = parse_envlist(env, &envc);
4271 if (envlist == NULL)
4272 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004273
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004274#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004275 Py_BEGIN_ALLOW_THREADS
4276 spawnval = spawnve(mode, path, argvlist, envlist);
4277 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004278#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004279 if (mode == _OLD_P_OVERLAY)
4280 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004281
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 Py_BEGIN_ALLOW_THREADS
4283 spawnval = _spawnve(mode, path, argvlist, envlist);
4284 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004285#endif
Tim Peters25059d32001-12-07 20:35:43 +00004286
Victor Stinner8c62be82010-05-06 00:08:46 +00004287 if (spawnval == -1)
4288 (void) posix_error();
4289 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004290#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004292#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004294#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004295
Victor Stinner8c62be82010-05-06 00:08:46 +00004296 while (--envc >= 0)
4297 PyMem_DEL(envlist[envc]);
4298 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004299 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004300 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004301 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 Py_DECREF(opath);
4303 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004304}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004305
4306/* OS/2 supports spawnvp & spawnvpe natively */
4307#if defined(PYOS_OS2)
4308PyDoc_STRVAR(posix_spawnvp__doc__,
4309"spawnvp(mode, file, args)\n\n\
4310Execute the program 'file' in a new process, using the environment\n\
4311search path to find the file.\n\
4312\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 mode: mode of process creation\n\
4314 file: executable file name\n\
4315 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004316
4317static PyObject *
4318posix_spawnvp(PyObject *self, PyObject *args)
4319{
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 PyObject *opath;
4321 char *path;
4322 PyObject *argv;
4323 char **argvlist;
4324 int mode, i, argc;
4325 Py_intptr_t spawnval;
4326 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004327
Victor Stinner8c62be82010-05-06 00:08:46 +00004328 /* spawnvp has three arguments: (mode, path, argv), where
4329 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004330
Victor Stinner8c62be82010-05-06 00:08:46 +00004331 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4332 PyUnicode_FSConverter,
4333 &opath, &argv))
4334 return NULL;
4335 path = PyBytes_AsString(opath);
4336 if (PyList_Check(argv)) {
4337 argc = PyList_Size(argv);
4338 getitem = PyList_GetItem;
4339 }
4340 else if (PyTuple_Check(argv)) {
4341 argc = PyTuple_Size(argv);
4342 getitem = PyTuple_GetItem;
4343 }
4344 else {
4345 PyErr_SetString(PyExc_TypeError,
4346 "spawnvp() arg 2 must be a tuple or list");
4347 Py_DECREF(opath);
4348 return NULL;
4349 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004350
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 argvlist = PyMem_NEW(char *, argc+1);
4352 if (argvlist == NULL) {
4353 Py_DECREF(opath);
4354 return PyErr_NoMemory();
4355 }
4356 for (i = 0; i < argc; i++) {
4357 if (!fsconvert_strdup((*getitem)(argv, i),
4358 &argvlist[i])) {
4359 free_string_array(argvlist, i);
4360 PyErr_SetString(
4361 PyExc_TypeError,
4362 "spawnvp() arg 2 must contain only strings");
4363 Py_DECREF(opath);
4364 return NULL;
4365 }
4366 }
4367 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004368
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004370#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004372#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004376
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 free_string_array(argvlist, argc);
4378 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004379
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 if (spawnval == -1)
4381 return posix_error();
4382 else
4383 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004384}
4385
4386
4387PyDoc_STRVAR(posix_spawnvpe__doc__,
4388"spawnvpe(mode, file, args, env)\n\n\
4389Execute the program 'file' in a new process, using the environment\n\
4390search path to find the file.\n\
4391\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004392 mode: mode of process creation\n\
4393 file: executable file name\n\
4394 args: tuple or list of arguments\n\
4395 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004396
4397static PyObject *
4398posix_spawnvpe(PyObject *self, PyObject *args)
4399{
Victor Stinner8c62be82010-05-06 00:08:46 +00004400 PyObject *opath
4401 char *path;
4402 PyObject *argv, *env;
4403 char **argvlist;
4404 char **envlist;
4405 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004406 int mode;
4407 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004408 Py_intptr_t spawnval;
4409 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4410 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004411
Victor Stinner8c62be82010-05-06 00:08:46 +00004412 /* spawnvpe has four arguments: (mode, path, argv, env), where
4413 argv is a list or tuple of strings and env is a dictionary
4414 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004415
Victor Stinner8c62be82010-05-06 00:08:46 +00004416 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4417 PyUnicode_FSConverter,
4418 &opath, &argv, &env))
4419 return NULL;
4420 path = PyBytes_AsString(opath);
4421 if (PyList_Check(argv)) {
4422 argc = PyList_Size(argv);
4423 getitem = PyList_GetItem;
4424 }
4425 else if (PyTuple_Check(argv)) {
4426 argc = PyTuple_Size(argv);
4427 getitem = PyTuple_GetItem;
4428 }
4429 else {
4430 PyErr_SetString(PyExc_TypeError,
4431 "spawnvpe() arg 2 must be a tuple or list");
4432 goto fail_0;
4433 }
4434 if (!PyMapping_Check(env)) {
4435 PyErr_SetString(PyExc_TypeError,
4436 "spawnvpe() arg 3 must be a mapping object");
4437 goto fail_0;
4438 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004439
Victor Stinner8c62be82010-05-06 00:08:46 +00004440 argvlist = PyMem_NEW(char *, argc+1);
4441 if (argvlist == NULL) {
4442 PyErr_NoMemory();
4443 goto fail_0;
4444 }
4445 for (i = 0; i < argc; i++) {
4446 if (!fsconvert_strdup((*getitem)(argv, i),
4447 &argvlist[i]))
4448 {
4449 lastarg = i;
4450 goto fail_1;
4451 }
4452 }
4453 lastarg = argc;
4454 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004455
Victor Stinner8c62be82010-05-06 00:08:46 +00004456 envlist = parse_envlist(env, &envc);
4457 if (envlist == NULL)
4458 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004459
Victor Stinner8c62be82010-05-06 00:08:46 +00004460 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004461#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004462 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004463#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004465#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 if (spawnval == -1)
4469 (void) posix_error();
4470 else
4471 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004472
Victor Stinner8c62be82010-05-06 00:08:46 +00004473 while (--envc >= 0)
4474 PyMem_DEL(envlist[envc]);
4475 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004476 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004477 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004478 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004479 Py_DECREF(opath);
4480 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004481}
4482#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004483#endif /* HAVE_SPAWNV */
4484
4485
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004486#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004487PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004488"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004489Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4490\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004491Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004492
4493static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004494posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004495{
Victor Stinner8c62be82010-05-06 00:08:46 +00004496 pid_t pid;
4497 int result = 0;
4498 _PyImport_AcquireLock();
4499 pid = fork1();
4500 if (pid == 0) {
4501 /* child: this clobbers and resets the import lock. */
4502 PyOS_AfterFork();
4503 } else {
4504 /* parent: release the import lock. */
4505 result = _PyImport_ReleaseLock();
4506 }
4507 if (pid == -1)
4508 return posix_error();
4509 if (result < 0) {
4510 /* Don't clobber the OSError if the fork failed. */
4511 PyErr_SetString(PyExc_RuntimeError,
4512 "not holding the import lock");
4513 return NULL;
4514 }
4515 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004516}
4517#endif
4518
4519
Guido van Rossumad0ee831995-03-01 10:34:45 +00004520#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004521PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004522"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004523Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004524Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004525
Barry Warsaw53699e91996-12-10 23:23:01 +00004526static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004527posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004528{
Victor Stinner8c62be82010-05-06 00:08:46 +00004529 pid_t pid;
4530 int result = 0;
4531 _PyImport_AcquireLock();
4532 pid = fork();
4533 if (pid == 0) {
4534 /* child: this clobbers and resets the import lock. */
4535 PyOS_AfterFork();
4536 } else {
4537 /* parent: release the import lock. */
4538 result = _PyImport_ReleaseLock();
4539 }
4540 if (pid == -1)
4541 return posix_error();
4542 if (result < 0) {
4543 /* Don't clobber the OSError if the fork failed. */
4544 PyErr_SetString(PyExc_RuntimeError,
4545 "not holding the import lock");
4546 return NULL;
4547 }
4548 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004549}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004550#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004551
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004552#ifdef HAVE_SCHED_H
4553
4554PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4555"sched_get_priority_max(policy)\n\n\
4556Get the maximum scheduling priority for *policy*.");
4557
4558static PyObject *
4559posix_sched_get_priority_max(PyObject *self, PyObject *args)
4560{
4561 int policy, max;
4562
4563 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4564 return NULL;
4565 max = sched_get_priority_max(policy);
4566 if (max < 0)
4567 return posix_error();
4568 return PyLong_FromLong(max);
4569}
4570
4571PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4572"sched_get_priority_min(policy)\n\n\
4573Get the minimum scheduling priority for *policy*.");
4574
4575static PyObject *
4576posix_sched_get_priority_min(PyObject *self, PyObject *args)
4577{
4578 int policy, min;
4579
4580 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4581 return NULL;
4582 min = sched_get_priority_min(policy);
4583 if (min < 0)
4584 return posix_error();
4585 return PyLong_FromLong(min);
4586}
4587
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004588#ifdef HAVE_SCHED_SETSCHEDULER
4589
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004590PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4591"sched_getscheduler(pid)\n\n\
4592Get the scheduling policy for the process with a PID of *pid*.\n\
4593Passing a PID of 0 returns the scheduling policy for the calling process.");
4594
4595static PyObject *
4596posix_sched_getscheduler(PyObject *self, PyObject *args)
4597{
4598 pid_t pid;
4599 int policy;
4600
4601 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4602 return NULL;
4603 policy = sched_getscheduler(pid);
4604 if (policy < 0)
4605 return posix_error();
4606 return PyLong_FromLong(policy);
4607}
4608
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004609#endif
4610
4611#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4612
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004613static PyObject *
4614sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4615{
4616 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004617 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004618
4619 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4620 return NULL;
4621 res = PyStructSequence_New(type);
4622 if (!res)
4623 return NULL;
4624 Py_INCREF(priority);
4625 PyStructSequence_SET_ITEM(res, 0, priority);
4626 return res;
4627}
4628
4629PyDoc_STRVAR(sched_param__doc__,
4630"sched_param(sched_priority): A scheduling parameter.\n\n\
4631Current has only one field: sched_priority");
4632
4633static PyStructSequence_Field sched_param_fields[] = {
4634 {"sched_priority", "the scheduling priority"},
4635 {0}
4636};
4637
4638static PyStructSequence_Desc sched_param_desc = {
4639 "sched_param", /* name */
4640 sched_param__doc__, /* doc */
4641 sched_param_fields,
4642 1
4643};
4644
4645static int
4646convert_sched_param(PyObject *param, struct sched_param *res)
4647{
4648 long priority;
4649
4650 if (Py_TYPE(param) != &SchedParamType) {
4651 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4652 return 0;
4653 }
4654 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4655 if (priority == -1 && PyErr_Occurred())
4656 return 0;
4657 if (priority > INT_MAX || priority < INT_MIN) {
4658 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4659 return 0;
4660 }
4661 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4662 return 1;
4663}
4664
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004665#endif
4666
4667#ifdef HAVE_SCHED_SETSCHEDULER
4668
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004669PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4670"sched_setscheduler(pid, policy, param)\n\n\
4671Set the scheduling policy, *policy*, for *pid*.\n\
4672If *pid* is 0, the calling process is changed.\n\
4673*param* is an instance of sched_param.");
4674
4675static PyObject *
4676posix_sched_setscheduler(PyObject *self, PyObject *args)
4677{
4678 pid_t pid;
4679 int policy;
4680 struct sched_param param;
4681
4682 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4683 &pid, &policy, &convert_sched_param, &param))
4684 return NULL;
4685 if (sched_setscheduler(pid, policy, &param))
4686 return posix_error();
4687 Py_RETURN_NONE;
4688}
4689
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004690#endif
4691
4692#ifdef HAVE_SCHED_SETPARAM
4693
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004694PyDoc_STRVAR(posix_sched_getparam__doc__,
4695"sched_getparam(pid) -> sched_param\n\n\
4696Returns scheduling parameters for the process with *pid* as an instance of the\n\
4697sched_param class. A PID of 0 means the calling process.");
4698
4699static PyObject *
4700posix_sched_getparam(PyObject *self, PyObject *args)
4701{
4702 pid_t pid;
4703 struct sched_param param;
4704 PyObject *res, *priority;
4705
4706 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4707 return NULL;
4708 if (sched_getparam(pid, &param))
4709 return posix_error();
4710 res = PyStructSequence_New(&SchedParamType);
4711 if (!res)
4712 return NULL;
4713 priority = PyLong_FromLong(param.sched_priority);
4714 if (!priority) {
4715 Py_DECREF(res);
4716 return NULL;
4717 }
4718 PyStructSequence_SET_ITEM(res, 0, priority);
4719 return res;
4720}
4721
4722PyDoc_STRVAR(posix_sched_setparam__doc__,
4723"sched_setparam(pid, param)\n\n\
4724Set scheduling parameters for a process with PID *pid*.\n\
4725A PID of 0 means the calling process.");
4726
4727static PyObject *
4728posix_sched_setparam(PyObject *self, PyObject *args)
4729{
4730 pid_t pid;
4731 struct sched_param param;
4732
4733 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4734 &pid, &convert_sched_param, &param))
4735 return NULL;
4736 if (sched_setparam(pid, &param))
4737 return posix_error();
4738 Py_RETURN_NONE;
4739}
4740
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004741#endif
4742
4743#ifdef HAVE_SCHED_RR_GET_INTERVAL
4744
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004745PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4746"sched_rr_get_interval(pid) -> float\n\n\
4747Return the round-robin quantum for the process with PID *pid* in seconds.");
4748
4749static PyObject *
4750posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4751{
4752 pid_t pid;
4753 struct timespec interval;
4754
4755 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4756 return NULL;
4757 if (sched_rr_get_interval(pid, &interval))
4758 return posix_error();
4759 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4760}
4761
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004762#endif
4763
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004764PyDoc_STRVAR(posix_sched_yield__doc__,
4765"sched_yield()\n\n\
4766Voluntarily relinquish the CPU.");
4767
4768static PyObject *
4769posix_sched_yield(PyObject *self, PyObject *noargs)
4770{
4771 if (sched_yield())
4772 return posix_error();
4773 Py_RETURN_NONE;
4774}
4775
Benjamin Peterson2740af82011-08-02 17:41:34 -05004776#ifdef HAVE_SCHED_SETAFFINITY
4777
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004778typedef struct {
4779 PyObject_HEAD;
4780 Py_ssize_t size;
4781 int ncpus;
4782 cpu_set_t *set;
4783} Py_cpu_set;
4784
4785static PyTypeObject cpu_set_type;
4786
4787static void
4788cpu_set_dealloc(Py_cpu_set *set)
4789{
4790 assert(set->set);
4791 CPU_FREE(set->set);
4792 Py_TYPE(set)->tp_free(set);
4793}
4794
4795static Py_cpu_set *
4796make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4797{
4798 Py_cpu_set *set;
4799
4800 if (size < 0) {
4801 PyErr_SetString(PyExc_ValueError, "negative size");
4802 return NULL;
4803 }
4804 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4805 if (!set)
4806 return NULL;
4807 set->ncpus = size;
4808 set->size = CPU_ALLOC_SIZE(size);
4809 set->set = CPU_ALLOC(size);
4810 if (!set->set) {
4811 type->tp_free(set);
4812 PyErr_NoMemory();
4813 return NULL;
4814 }
4815 CPU_ZERO_S(set->size, set->set);
4816 return set;
4817}
4818
4819static PyObject *
4820cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4821{
4822 int size;
4823
4824 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4825 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4826 return NULL;
4827 return (PyObject *)make_new_cpu_set(type, size);
4828}
4829
4830static PyObject *
4831cpu_set_repr(Py_cpu_set *set)
4832{
4833 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4834}
4835
4836static Py_ssize_t
4837cpu_set_len(Py_cpu_set *set)
4838{
4839 return set->ncpus;
4840}
4841
4842static int
4843_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4844{
4845 int cpu;
4846 if (!PyArg_ParseTuple(args, requester, &cpu))
4847 return -1;
4848 if (cpu < 0) {
4849 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4850 return -1;
4851 }
4852 if (cpu >= set->ncpus) {
4853 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4854 return -1;
4855 }
4856 return cpu;
4857}
4858
4859PyDoc_STRVAR(cpu_set_set_doc,
4860"cpu_set.set(i)\n\n\
4861Add CPU *i* to the set.");
4862
4863static PyObject *
4864cpu_set_set(Py_cpu_set *set, PyObject *args)
4865{
4866 int cpu = _get_cpu(set, "i|set", args);
4867 if (cpu == -1)
4868 return NULL;
4869 CPU_SET_S(cpu, set->size, set->set);
4870 Py_RETURN_NONE;
4871}
4872
4873PyDoc_STRVAR(cpu_set_count_doc,
4874"cpu_set.count() -> int\n\n\
4875Return the number of CPUs active in the set.");
4876
4877static PyObject *
4878cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4879{
4880 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
4881}
4882
4883PyDoc_STRVAR(cpu_set_clear_doc,
4884"cpu_set.clear(i)\n\n\
4885Remove CPU *i* from the set.");
4886
4887static PyObject *
4888cpu_set_clear(Py_cpu_set *set, PyObject *args)
4889{
4890 int cpu = _get_cpu(set, "i|clear", args);
4891 if (cpu == -1)
4892 return NULL;
4893 CPU_CLR_S(cpu, set->size, set->set);
4894 Py_RETURN_NONE;
4895}
4896
4897PyDoc_STRVAR(cpu_set_isset_doc,
4898"cpu_set.isset(i) -> bool\n\n\
4899Test if CPU *i* is in the set.");
4900
4901static PyObject *
4902cpu_set_isset(Py_cpu_set *set, PyObject *args)
4903{
4904 int cpu = _get_cpu(set, "i|isset", args);
4905 if (cpu == -1)
4906 return NULL;
4907 if (CPU_ISSET_S(cpu, set->size, set->set))
4908 Py_RETURN_TRUE;
4909 Py_RETURN_FALSE;
4910}
4911
4912PyDoc_STRVAR(cpu_set_zero_doc,
4913"cpu_set.zero()\n\n\
4914Clear the cpu_set.");
4915
4916static PyObject *
4917cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
4918{
4919 CPU_ZERO_S(set->size, set->set);
4920 Py_RETURN_NONE;
4921}
4922
4923static PyObject *
4924cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
4925{
4926 int eq;
4927
4928 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) {
4929 Py_INCREF(Py_NotImplemented);
4930 return Py_NotImplemented;
4931 }
4932 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
4933 if ((op == Py_EQ) ? eq : !eq)
4934 Py_RETURN_TRUE;
4935 else
4936 Py_RETURN_FALSE;
4937}
4938
4939#define CPU_SET_BINOP(name, op) \
4940 static PyObject * \
4941 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
4942 if (res) { \
4943 Py_INCREF(res); \
4944 } \
4945 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05004946 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004947 if (!res) \
4948 return NULL; \
4949 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05004950 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004951 Py_DECREF(res); \
4952 Py_INCREF(Py_NotImplemented); \
4953 return Py_NotImplemented; \
4954 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05004955 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004956 op(res->size, res->set, left->set, right->set); \
4957 return (PyObject *)res; \
4958 } \
4959 static PyObject * \
4960 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
4961 return do_cpu_set_##name(left, right, NULL); \
4962 } \
4963 static PyObject * \
4964 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
4965 return do_cpu_set_##name(left, right, left); \
4966 } \
4967
4968CPU_SET_BINOP(and, CPU_AND_S)
4969CPU_SET_BINOP(or, CPU_OR_S)
4970CPU_SET_BINOP(xor, CPU_XOR_S)
4971#undef CPU_SET_BINOP
4972
4973PyDoc_STRVAR(cpu_set_doc,
4974"cpu_set(size)\n\n\
4975Create an empty mask of CPUs.");
4976
4977static PyNumberMethods cpu_set_as_number = {
4978 0, /*nb_add*/
4979 0, /*nb_subtract*/
4980 0, /*nb_multiply*/
4981 0, /*nb_remainder*/
4982 0, /*nb_divmod*/
4983 0, /*nb_power*/
4984 0, /*nb_negative*/
4985 0, /*nb_positive*/
4986 0, /*nb_absolute*/
4987 0, /*nb_bool*/
4988 0, /*nb_invert*/
4989 0, /*nb_lshift*/
4990 0, /*nb_rshift*/
4991 (binaryfunc)cpu_set_and, /*nb_and*/
4992 (binaryfunc)cpu_set_xor, /*nb_xor*/
4993 (binaryfunc)cpu_set_or, /*nb_or*/
4994 0, /*nb_int*/
4995 0, /*nb_reserved*/
4996 0, /*nb_float*/
4997 0, /*nb_inplace_add*/
4998 0, /*nb_inplace_subtract*/
4999 0, /*nb_inplace_multiply*/
5000 0, /*nb_inplace_remainder*/
5001 0, /*nb_inplace_power*/
5002 0, /*nb_inplace_lshift*/
5003 0, /*nb_inplace_rshift*/
5004 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5005 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5006 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5007};
5008
5009static PySequenceMethods cpu_set_as_sequence = {
5010 (lenfunc)cpu_set_len, /* sq_length */
5011};
5012
5013static PyMethodDef cpu_set_methods[] = {
5014 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5015 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5016 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5017 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5018 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5019 {NULL, NULL} /* sentinel */
5020};
5021
5022static PyTypeObject cpu_set_type = {
5023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5024 "posix.cpu_set", /* tp_name */
5025 sizeof(Py_cpu_set), /* tp_basicsize */
5026 0, /* tp_itemsize */
5027 /* methods */
5028 (destructor)cpu_set_dealloc, /* tp_dealloc */
5029 0, /* tp_print */
5030 0, /* tp_getattr */
5031 0, /* tp_setattr */
5032 0, /* tp_reserved */
5033 (reprfunc)cpu_set_repr, /* tp_repr */
5034 &cpu_set_as_number, /* tp_as_number */
5035 &cpu_set_as_sequence, /* tp_as_sequence */
5036 0, /* tp_as_mapping */
5037 PyObject_HashNotImplemented, /* tp_hash */
5038 0, /* tp_call */
5039 0, /* tp_str */
5040 PyObject_GenericGetAttr, /* tp_getattro */
5041 0, /* tp_setattro */
5042 0, /* tp_as_buffer */
5043 Py_TPFLAGS_DEFAULT, /* tp_flags */
5044 cpu_set_doc, /* tp_doc */
5045 0, /* tp_traverse */
5046 0, /* tp_clear */
5047 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5048 0, /* tp_weaklistoffset */
5049 0, /* tp_iter */
5050 0, /* tp_iternext */
5051 cpu_set_methods, /* tp_methods */
5052 0, /* tp_members */
5053 0, /* tp_getset */
5054 0, /* tp_base */
5055 0, /* tp_dict */
5056 0, /* tp_descr_get */
5057 0, /* tp_descr_set */
5058 0, /* tp_dictoffset */
5059 0, /* tp_init */
5060 PyType_GenericAlloc, /* tp_alloc */
5061 cpu_set_new, /* tp_new */
5062 PyObject_Del, /* tp_free */
5063};
5064
5065PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5066"sched_setaffinity(pid, cpu_set)\n\n\
5067Set the affinity of the process with PID *pid* to *cpu_set*.");
5068
5069static PyObject *
5070posix_sched_setaffinity(PyObject *self, PyObject *args)
5071{
5072 pid_t pid;
5073 Py_cpu_set *cpu_set;
5074
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005075 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:schbed_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005076 &pid, &cpu_set_type, &cpu_set))
5077 return NULL;
5078 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5079 return posix_error();
5080 Py_RETURN_NONE;
5081}
5082
5083PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5084"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5085Return the affinity of the process with PID *pid*.\n\
5086The returned cpu_set will be of size *ncpus*.");
5087
5088static PyObject *
5089posix_sched_getaffinity(PyObject *self, PyObject *args)
5090{
5091 pid_t pid;
5092 int ncpus;
5093 Py_cpu_set *res;
5094
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005095 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005096 &pid, &ncpus))
5097 return NULL;
5098 res = make_new_cpu_set(&cpu_set_type, ncpus);
5099 if (!res)
5100 return NULL;
5101 if (sched_getaffinity(pid, res->size, res->set)) {
5102 Py_DECREF(res);
5103 return posix_error();
5104 }
5105 return (PyObject *)res;
5106}
5107
Benjamin Peterson2740af82011-08-02 17:41:34 -05005108#endif /* HAVE_SCHED_SETAFFINITY */
5109
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005110#endif /* HAVE_SCHED_H */
5111
Neal Norwitzb59798b2003-03-21 01:43:31 +00005112/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005113/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5114#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005115#define DEV_PTY_FILE "/dev/ptc"
5116#define HAVE_DEV_PTMX
5117#else
5118#define DEV_PTY_FILE "/dev/ptmx"
5119#endif
5120
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005121#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005122#ifdef HAVE_PTY_H
5123#include <pty.h>
5124#else
5125#ifdef HAVE_LIBUTIL_H
5126#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005127#else
5128#ifdef HAVE_UTIL_H
5129#include <util.h>
5130#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005131#endif /* HAVE_LIBUTIL_H */
5132#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005133#ifdef HAVE_STROPTS_H
5134#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005135#endif
5136#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005137
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005138#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005139PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005140"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005141Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005142
5143static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005144posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005145{
Victor Stinner8c62be82010-05-06 00:08:46 +00005146 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005147#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005148 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005149#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005150#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005151 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005152#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005153 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005154#endif
5155#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005156
Thomas Wouters70c21a12000-07-14 14:28:33 +00005157#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005158 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5159 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005160#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005161 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5162 if (slave_name == NULL)
5163 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005164
Victor Stinner8c62be82010-05-06 00:08:46 +00005165 slave_fd = open(slave_name, O_RDWR);
5166 if (slave_fd < 0)
5167 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005168#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005169 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5170 if (master_fd < 0)
5171 return posix_error();
5172 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5173 /* change permission of slave */
5174 if (grantpt(master_fd) < 0) {
5175 PyOS_setsig(SIGCHLD, sig_saved);
5176 return posix_error();
5177 }
5178 /* unlock slave */
5179 if (unlockpt(master_fd) < 0) {
5180 PyOS_setsig(SIGCHLD, sig_saved);
5181 return posix_error();
5182 }
5183 PyOS_setsig(SIGCHLD, sig_saved);
5184 slave_name = ptsname(master_fd); /* get name of slave */
5185 if (slave_name == NULL)
5186 return posix_error();
5187 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5188 if (slave_fd < 0)
5189 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005190#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005191 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5192 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005193#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005194 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005195#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005196#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005197#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005198
Victor Stinner8c62be82010-05-06 00:08:46 +00005199 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005200
Fred Drake8cef4cf2000-06-28 16:40:38 +00005201}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005202#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005203
5204#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005205PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005206"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005207Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5208Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005209To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005210
5211static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005212posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005213{
Victor Stinner8c62be82010-05-06 00:08:46 +00005214 int master_fd = -1, result = 0;
5215 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005216
Victor Stinner8c62be82010-05-06 00:08:46 +00005217 _PyImport_AcquireLock();
5218 pid = forkpty(&master_fd, NULL, NULL, NULL);
5219 if (pid == 0) {
5220 /* child: this clobbers and resets the import lock. */
5221 PyOS_AfterFork();
5222 } else {
5223 /* parent: release the import lock. */
5224 result = _PyImport_ReleaseLock();
5225 }
5226 if (pid == -1)
5227 return posix_error();
5228 if (result < 0) {
5229 /* Don't clobber the OSError if the fork failed. */
5230 PyErr_SetString(PyExc_RuntimeError,
5231 "not holding the import lock");
5232 return NULL;
5233 }
5234 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005235}
5236#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005237
Ross Lagerwall7807c352011-03-17 20:20:30 +02005238
Guido van Rossumad0ee831995-03-01 10:34:45 +00005239#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005240PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005241"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005242Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005243
Barry Warsaw53699e91996-12-10 23:23:01 +00005244static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005245posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005246{
Victor Stinner8c62be82010-05-06 00:08:46 +00005247 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005248}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005249#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005250
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005251
Guido van Rossumad0ee831995-03-01 10:34:45 +00005252#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005253PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005254"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005255Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005256
Barry Warsaw53699e91996-12-10 23:23:01 +00005257static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005258posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005259{
Victor Stinner8c62be82010-05-06 00:08:46 +00005260 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005261}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005262#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005263
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005264
Guido van Rossumad0ee831995-03-01 10:34:45 +00005265#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005266PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005267"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005268Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005269
Barry Warsaw53699e91996-12-10 23:23:01 +00005270static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005271posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005272{
Victor Stinner8c62be82010-05-06 00:08:46 +00005273 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005274}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005275#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005278PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005279"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005280Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005281
Barry Warsaw53699e91996-12-10 23:23:01 +00005282static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005283posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005284{
Victor Stinner8c62be82010-05-06 00:08:46 +00005285 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005286}
5287
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005288#ifdef HAVE_GETGROUPLIST
5289PyDoc_STRVAR(posix_getgrouplist__doc__,
5290"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5291Returns a list of groups to which a user belongs.\n\n\
5292 user: username to lookup\n\
5293 group: base group id of the user");
5294
5295static PyObject *
5296posix_getgrouplist(PyObject *self, PyObject *args)
5297{
5298#ifdef NGROUPS_MAX
5299#define MAX_GROUPS NGROUPS_MAX
5300#else
5301 /* defined to be 16 on Solaris7, so this should be a small number */
5302#define MAX_GROUPS 64
5303#endif
5304
5305 const char *user;
5306 int i, ngroups;
5307 PyObject *list;
5308#ifdef __APPLE__
5309 int *groups, basegid;
5310#else
5311 gid_t *groups, basegid;
5312#endif
5313 ngroups = MAX_GROUPS;
5314
5315 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5316 return NULL;
5317
5318#ifdef __APPLE__
5319 groups = PyMem_Malloc(ngroups * sizeof(int));
5320#else
5321 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5322#endif
5323 if (groups == NULL)
5324 return PyErr_NoMemory();
5325
5326 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5327 PyMem_Del(groups);
5328 return posix_error();
5329 }
5330
5331 list = PyList_New(ngroups);
5332 if (list == NULL) {
5333 PyMem_Del(groups);
5334 return NULL;
5335 }
5336
5337 for (i = 0; i < ngroups; i++) {
5338 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5339 if (o == NULL) {
5340 Py_DECREF(list);
5341 PyMem_Del(groups);
5342 return NULL;
5343 }
5344 PyList_SET_ITEM(list, i, o);
5345 }
5346
5347 PyMem_Del(groups);
5348
5349 return list;
5350}
5351#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005352
Fred Drakec9680921999-12-13 16:37:25 +00005353#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005354PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005355"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005356Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005357
5358static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005359posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005360{
5361 PyObject *result = NULL;
5362
Fred Drakec9680921999-12-13 16:37:25 +00005363#ifdef NGROUPS_MAX
5364#define MAX_GROUPS NGROUPS_MAX
5365#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005366 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005367#define MAX_GROUPS 64
5368#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005369 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005370
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005371 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005372 * This is a helper variable to store the intermediate result when
5373 * that happens.
5374 *
5375 * To keep the code readable the OSX behaviour is unconditional,
5376 * according to the POSIX spec this should be safe on all unix-y
5377 * systems.
5378 */
5379 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005380 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005381
Victor Stinner8c62be82010-05-06 00:08:46 +00005382 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005383 if (n < 0) {
5384 if (errno == EINVAL) {
5385 n = getgroups(0, NULL);
5386 if (n == -1) {
5387 return posix_error();
5388 }
5389 if (n == 0) {
5390 /* Avoid malloc(0) */
5391 alt_grouplist = grouplist;
5392 } else {
5393 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5394 if (alt_grouplist == NULL) {
5395 errno = EINVAL;
5396 return posix_error();
5397 }
5398 n = getgroups(n, alt_grouplist);
5399 if (n == -1) {
5400 PyMem_Free(alt_grouplist);
5401 return posix_error();
5402 }
5403 }
5404 } else {
5405 return posix_error();
5406 }
5407 }
5408 result = PyList_New(n);
5409 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005410 int i;
5411 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005412 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005413 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005414 Py_DECREF(result);
5415 result = NULL;
5416 break;
Fred Drakec9680921999-12-13 16:37:25 +00005417 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005418 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005419 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005420 }
5421
5422 if (alt_grouplist != grouplist) {
5423 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005424 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005425
Fred Drakec9680921999-12-13 16:37:25 +00005426 return result;
5427}
5428#endif
5429
Antoine Pitroub7572f02009-12-02 20:46:48 +00005430#ifdef HAVE_INITGROUPS
5431PyDoc_STRVAR(posix_initgroups__doc__,
5432"initgroups(username, gid) -> None\n\n\
5433Call the system initgroups() to initialize the group access list with all of\n\
5434the groups of which the specified username is a member, plus the specified\n\
5435group id.");
5436
5437static PyObject *
5438posix_initgroups(PyObject *self, PyObject *args)
5439{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005440 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005441 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005442 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005443 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005444
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005445 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5446 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005447 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005448 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005449
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005450 res = initgroups(username, (gid_t) gid);
5451 Py_DECREF(oname);
5452 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005453 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005454
Victor Stinner8c62be82010-05-06 00:08:46 +00005455 Py_INCREF(Py_None);
5456 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005457}
5458#endif
5459
Martin v. Löwis606edc12002-06-13 21:09:11 +00005460#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005461PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005462"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005463Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005464
5465static PyObject *
5466posix_getpgid(PyObject *self, PyObject *args)
5467{
Victor Stinner8c62be82010-05-06 00:08:46 +00005468 pid_t pid, pgid;
5469 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5470 return NULL;
5471 pgid = getpgid(pid);
5472 if (pgid < 0)
5473 return posix_error();
5474 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005475}
5476#endif /* HAVE_GETPGID */
5477
5478
Guido van Rossumb6775db1994-08-01 11:34:53 +00005479#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005480PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005481"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005482Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005483
Barry Warsaw53699e91996-12-10 23:23:01 +00005484static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005485posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005486{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005487#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005488 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005489#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005490 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005491#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005492}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005493#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005494
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005495
Guido van Rossumb6775db1994-08-01 11:34:53 +00005496#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005497PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005498"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005499Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005500
Barry Warsaw53699e91996-12-10 23:23:01 +00005501static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005502posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005503{
Guido van Rossum64933891994-10-20 21:56:42 +00005504#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005505 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005506#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005507 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005508#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005509 return posix_error();
5510 Py_INCREF(Py_None);
5511 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005512}
5513
Guido van Rossumb6775db1994-08-01 11:34:53 +00005514#endif /* HAVE_SETPGRP */
5515
Guido van Rossumad0ee831995-03-01 10:34:45 +00005516#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005517
5518#ifdef MS_WINDOWS
5519#include <tlhelp32.h>
5520
5521static PyObject*
5522win32_getppid()
5523{
5524 HANDLE snapshot;
5525 pid_t mypid;
5526 PyObject* result = NULL;
5527 BOOL have_record;
5528 PROCESSENTRY32 pe;
5529
5530 mypid = getpid(); /* This function never fails */
5531
5532 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5533 if (snapshot == INVALID_HANDLE_VALUE)
5534 return PyErr_SetFromWindowsErr(GetLastError());
5535
5536 pe.dwSize = sizeof(pe);
5537 have_record = Process32First(snapshot, &pe);
5538 while (have_record) {
5539 if (mypid == (pid_t)pe.th32ProcessID) {
5540 /* We could cache the ulong value in a static variable. */
5541 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5542 break;
5543 }
5544
5545 have_record = Process32Next(snapshot, &pe);
5546 }
5547
5548 /* If our loop exits and our pid was not found (result will be NULL)
5549 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5550 * error anyway, so let's raise it. */
5551 if (!result)
5552 result = PyErr_SetFromWindowsErr(GetLastError());
5553
5554 CloseHandle(snapshot);
5555
5556 return result;
5557}
5558#endif /*MS_WINDOWS*/
5559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005561"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005562Return the parent's process id. If the parent process has already exited,\n\
5563Windows machines will still return its id; others systems will return the id\n\
5564of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005565
Barry Warsaw53699e91996-12-10 23:23:01 +00005566static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005567posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005568{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005569#ifdef MS_WINDOWS
5570 return win32_getppid();
5571#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005572 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005573#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005574}
5575#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005576
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005577
Fred Drake12c6e2d1999-12-14 21:25:03 +00005578#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005579PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005580"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005581Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005582
5583static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005584posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005585{
Victor Stinner8c62be82010-05-06 00:08:46 +00005586 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005587#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005588 wchar_t user_name[UNLEN + 1];
5589 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5590
5591 if (GetUserNameW(user_name, &num_chars)) {
5592 /* num_chars is the number of unicode chars plus null terminator */
5593 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005594 }
5595 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005596 result = PyErr_SetFromWindowsErr(GetLastError());
5597#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005598 char *name;
5599 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005600
Victor Stinner8c62be82010-05-06 00:08:46 +00005601 errno = 0;
5602 name = getlogin();
5603 if (name == NULL) {
5604 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005605 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005606 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005607 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005608 }
5609 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005610 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005611 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005612#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005613 return result;
5614}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005615#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005616
Guido van Rossumad0ee831995-03-01 10:34:45 +00005617#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005618PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005619"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005621
Barry Warsaw53699e91996-12-10 23:23:01 +00005622static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005623posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005624{
Victor Stinner8c62be82010-05-06 00:08:46 +00005625 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005626}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005627#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005628
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005629
Guido van Rossumad0ee831995-03-01 10:34:45 +00005630#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005631PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005632"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005633Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005634
Barry Warsaw53699e91996-12-10 23:23:01 +00005635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005636posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005637{
Victor Stinner8c62be82010-05-06 00:08:46 +00005638 pid_t pid;
5639 int sig;
5640 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5641 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005642#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005643 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5644 APIRET rc;
5645 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005646 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005647
5648 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5649 APIRET rc;
5650 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005651 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005652
5653 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005654 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005655#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005656 if (kill(pid, sig) == -1)
5657 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005658#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005659 Py_INCREF(Py_None);
5660 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005661}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005662#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005663
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005664#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005665PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005666"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005667Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005668
5669static PyObject *
5670posix_killpg(PyObject *self, PyObject *args)
5671{
Victor Stinner8c62be82010-05-06 00:08:46 +00005672 int sig;
5673 pid_t pgid;
5674 /* XXX some man pages make the `pgid` parameter an int, others
5675 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5676 take the same type. Moreover, pid_t is always at least as wide as
5677 int (else compilation of this module fails), which is safe. */
5678 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5679 return NULL;
5680 if (killpg(pgid, sig) == -1)
5681 return posix_error();
5682 Py_INCREF(Py_None);
5683 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005684}
5685#endif
5686
Brian Curtineb24d742010-04-12 17:16:38 +00005687#ifdef MS_WINDOWS
5688PyDoc_STRVAR(win32_kill__doc__,
5689"kill(pid, sig)\n\n\
5690Kill a process with a signal.");
5691
5692static PyObject *
5693win32_kill(PyObject *self, PyObject *args)
5694{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005695 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005696 DWORD pid, sig, err;
5697 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005698
Victor Stinner8c62be82010-05-06 00:08:46 +00005699 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5700 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005701
Victor Stinner8c62be82010-05-06 00:08:46 +00005702 /* Console processes which share a common console can be sent CTRL+C or
5703 CTRL+BREAK events, provided they handle said events. */
5704 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5705 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5706 err = GetLastError();
5707 PyErr_SetFromWindowsErr(err);
5708 }
5709 else
5710 Py_RETURN_NONE;
5711 }
Brian Curtineb24d742010-04-12 17:16:38 +00005712
Victor Stinner8c62be82010-05-06 00:08:46 +00005713 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5714 attempt to open and terminate the process. */
5715 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5716 if (handle == NULL) {
5717 err = GetLastError();
5718 return PyErr_SetFromWindowsErr(err);
5719 }
Brian Curtineb24d742010-04-12 17:16:38 +00005720
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 if (TerminateProcess(handle, sig) == 0) {
5722 err = GetLastError();
5723 result = PyErr_SetFromWindowsErr(err);
5724 } else {
5725 Py_INCREF(Py_None);
5726 result = Py_None;
5727 }
Brian Curtineb24d742010-04-12 17:16:38 +00005728
Victor Stinner8c62be82010-05-06 00:08:46 +00005729 CloseHandle(handle);
5730 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005731}
5732#endif /* MS_WINDOWS */
5733
Guido van Rossumc0125471996-06-28 18:55:32 +00005734#ifdef HAVE_PLOCK
5735
5736#ifdef HAVE_SYS_LOCK_H
5737#include <sys/lock.h>
5738#endif
5739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005740PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005741"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005742Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005743
Barry Warsaw53699e91996-12-10 23:23:01 +00005744static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005745posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005746{
Victor Stinner8c62be82010-05-06 00:08:46 +00005747 int op;
5748 if (!PyArg_ParseTuple(args, "i:plock", &op))
5749 return NULL;
5750 if (plock(op) == -1)
5751 return posix_error();
5752 Py_INCREF(Py_None);
5753 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005754}
5755#endif
5756
Guido van Rossumb6775db1994-08-01 11:34:53 +00005757#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005758PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005759"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005760Set the current process's user id.");
5761
Barry Warsaw53699e91996-12-10 23:23:01 +00005762static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005763posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005764{
Victor Stinner8c62be82010-05-06 00:08:46 +00005765 long uid_arg;
5766 uid_t uid;
5767 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5768 return NULL;
5769 uid = uid_arg;
5770 if (uid != uid_arg) {
5771 PyErr_SetString(PyExc_OverflowError, "user id too big");
5772 return NULL;
5773 }
5774 if (setuid(uid) < 0)
5775 return posix_error();
5776 Py_INCREF(Py_None);
5777 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005778}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005779#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005780
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005781
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005782#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005783PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005784"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005785Set the current process's effective user id.");
5786
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005787static PyObject *
5788posix_seteuid (PyObject *self, PyObject *args)
5789{
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 long euid_arg;
5791 uid_t euid;
5792 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5793 return NULL;
5794 euid = euid_arg;
5795 if (euid != euid_arg) {
5796 PyErr_SetString(PyExc_OverflowError, "user id too big");
5797 return NULL;
5798 }
5799 if (seteuid(euid) < 0) {
5800 return posix_error();
5801 } else {
5802 Py_INCREF(Py_None);
5803 return Py_None;
5804 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005805}
5806#endif /* HAVE_SETEUID */
5807
5808#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005809PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005810"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005811Set the current process's effective group id.");
5812
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005813static PyObject *
5814posix_setegid (PyObject *self, PyObject *args)
5815{
Victor Stinner8c62be82010-05-06 00:08:46 +00005816 long egid_arg;
5817 gid_t egid;
5818 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5819 return NULL;
5820 egid = egid_arg;
5821 if (egid != egid_arg) {
5822 PyErr_SetString(PyExc_OverflowError, "group id too big");
5823 return NULL;
5824 }
5825 if (setegid(egid) < 0) {
5826 return posix_error();
5827 } else {
5828 Py_INCREF(Py_None);
5829 return Py_None;
5830 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005831}
5832#endif /* HAVE_SETEGID */
5833
5834#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005835PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005836"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837Set the current process's real and effective user ids.");
5838
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005839static PyObject *
5840posix_setreuid (PyObject *self, PyObject *args)
5841{
Victor Stinner8c62be82010-05-06 00:08:46 +00005842 long ruid_arg, euid_arg;
5843 uid_t ruid, euid;
5844 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5845 return NULL;
5846 if (ruid_arg == -1)
5847 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5848 else
5849 ruid = ruid_arg; /* otherwise, assign from our long */
5850 if (euid_arg == -1)
5851 euid = (uid_t)-1;
5852 else
5853 euid = euid_arg;
5854 if ((euid_arg != -1 && euid != euid_arg) ||
5855 (ruid_arg != -1 && ruid != ruid_arg)) {
5856 PyErr_SetString(PyExc_OverflowError, "user id too big");
5857 return NULL;
5858 }
5859 if (setreuid(ruid, euid) < 0) {
5860 return posix_error();
5861 } else {
5862 Py_INCREF(Py_None);
5863 return Py_None;
5864 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005865}
5866#endif /* HAVE_SETREUID */
5867
5868#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005869PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005870"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005871Set the current process's real and effective group ids.");
5872
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005873static PyObject *
5874posix_setregid (PyObject *self, PyObject *args)
5875{
Victor Stinner8c62be82010-05-06 00:08:46 +00005876 long rgid_arg, egid_arg;
5877 gid_t rgid, egid;
5878 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5879 return NULL;
5880 if (rgid_arg == -1)
5881 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5882 else
5883 rgid = rgid_arg; /* otherwise, assign from our long */
5884 if (egid_arg == -1)
5885 egid = (gid_t)-1;
5886 else
5887 egid = egid_arg;
5888 if ((egid_arg != -1 && egid != egid_arg) ||
5889 (rgid_arg != -1 && rgid != rgid_arg)) {
5890 PyErr_SetString(PyExc_OverflowError, "group id too big");
5891 return NULL;
5892 }
5893 if (setregid(rgid, egid) < 0) {
5894 return posix_error();
5895 } else {
5896 Py_INCREF(Py_None);
5897 return Py_None;
5898 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005899}
5900#endif /* HAVE_SETREGID */
5901
Guido van Rossumb6775db1994-08-01 11:34:53 +00005902#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005903PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005904"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005906
Barry Warsaw53699e91996-12-10 23:23:01 +00005907static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005908posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005909{
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 long gid_arg;
5911 gid_t gid;
5912 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5913 return NULL;
5914 gid = gid_arg;
5915 if (gid != gid_arg) {
5916 PyErr_SetString(PyExc_OverflowError, "group id too big");
5917 return NULL;
5918 }
5919 if (setgid(gid) < 0)
5920 return posix_error();
5921 Py_INCREF(Py_None);
5922 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005923}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005924#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005925
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005926#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005928"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005929Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005930
5931static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005932posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005933{
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 int i, len;
5935 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005936
Victor Stinner8c62be82010-05-06 00:08:46 +00005937 if (!PySequence_Check(groups)) {
5938 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5939 return NULL;
5940 }
5941 len = PySequence_Size(groups);
5942 if (len > MAX_GROUPS) {
5943 PyErr_SetString(PyExc_ValueError, "too many groups");
5944 return NULL;
5945 }
5946 for(i = 0; i < len; i++) {
5947 PyObject *elem;
5948 elem = PySequence_GetItem(groups, i);
5949 if (!elem)
5950 return NULL;
5951 if (!PyLong_Check(elem)) {
5952 PyErr_SetString(PyExc_TypeError,
5953 "groups must be integers");
5954 Py_DECREF(elem);
5955 return NULL;
5956 } else {
5957 unsigned long x = PyLong_AsUnsignedLong(elem);
5958 if (PyErr_Occurred()) {
5959 PyErr_SetString(PyExc_TypeError,
5960 "group id too big");
5961 Py_DECREF(elem);
5962 return NULL;
5963 }
5964 grouplist[i] = x;
5965 /* read back the value to see if it fitted in gid_t */
5966 if (grouplist[i] != x) {
5967 PyErr_SetString(PyExc_TypeError,
5968 "group id too big");
5969 Py_DECREF(elem);
5970 return NULL;
5971 }
5972 }
5973 Py_DECREF(elem);
5974 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005975
Victor Stinner8c62be82010-05-06 00:08:46 +00005976 if (setgroups(len, grouplist) < 0)
5977 return posix_error();
5978 Py_INCREF(Py_None);
5979 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005980}
5981#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005982
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005983#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5984static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005985wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005986{
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 PyObject *result;
5988 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005989
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 if (pid == -1)
5991 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005992
Victor Stinner8c62be82010-05-06 00:08:46 +00005993 if (struct_rusage == NULL) {
5994 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5995 if (m == NULL)
5996 return NULL;
5997 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5998 Py_DECREF(m);
5999 if (struct_rusage == NULL)
6000 return NULL;
6001 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006002
Victor Stinner8c62be82010-05-06 00:08:46 +00006003 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6004 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6005 if (!result)
6006 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006007
6008#ifndef doubletime
6009#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6010#endif
6011
Victor Stinner8c62be82010-05-06 00:08:46 +00006012 PyStructSequence_SET_ITEM(result, 0,
6013 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6014 PyStructSequence_SET_ITEM(result, 1,
6015 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006016#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006017 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6018 SET_INT(result, 2, ru->ru_maxrss);
6019 SET_INT(result, 3, ru->ru_ixrss);
6020 SET_INT(result, 4, ru->ru_idrss);
6021 SET_INT(result, 5, ru->ru_isrss);
6022 SET_INT(result, 6, ru->ru_minflt);
6023 SET_INT(result, 7, ru->ru_majflt);
6024 SET_INT(result, 8, ru->ru_nswap);
6025 SET_INT(result, 9, ru->ru_inblock);
6026 SET_INT(result, 10, ru->ru_oublock);
6027 SET_INT(result, 11, ru->ru_msgsnd);
6028 SET_INT(result, 12, ru->ru_msgrcv);
6029 SET_INT(result, 13, ru->ru_nsignals);
6030 SET_INT(result, 14, ru->ru_nvcsw);
6031 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006032#undef SET_INT
6033
Victor Stinner8c62be82010-05-06 00:08:46 +00006034 if (PyErr_Occurred()) {
6035 Py_DECREF(result);
6036 return NULL;
6037 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006038
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006040}
6041#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6042
6043#ifdef HAVE_WAIT3
6044PyDoc_STRVAR(posix_wait3__doc__,
6045"wait3(options) -> (pid, status, rusage)\n\n\
6046Wait for completion of a child process.");
6047
6048static PyObject *
6049posix_wait3(PyObject *self, PyObject *args)
6050{
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 pid_t pid;
6052 int options;
6053 struct rusage ru;
6054 WAIT_TYPE status;
6055 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006056
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6058 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006059
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 Py_BEGIN_ALLOW_THREADS
6061 pid = wait3(&status, options, &ru);
6062 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006063
Victor Stinner8c62be82010-05-06 00:08:46 +00006064 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006065}
6066#endif /* HAVE_WAIT3 */
6067
6068#ifdef HAVE_WAIT4
6069PyDoc_STRVAR(posix_wait4__doc__,
6070"wait4(pid, options) -> (pid, status, rusage)\n\n\
6071Wait for completion of a given child process.");
6072
6073static PyObject *
6074posix_wait4(PyObject *self, PyObject *args)
6075{
Victor Stinner8c62be82010-05-06 00:08:46 +00006076 pid_t pid;
6077 int options;
6078 struct rusage ru;
6079 WAIT_TYPE status;
6080 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006081
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6083 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006084
Victor Stinner8c62be82010-05-06 00:08:46 +00006085 Py_BEGIN_ALLOW_THREADS
6086 pid = wait4(pid, &status, options, &ru);
6087 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006088
Victor Stinner8c62be82010-05-06 00:08:46 +00006089 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006090}
6091#endif /* HAVE_WAIT4 */
6092
Ross Lagerwall7807c352011-03-17 20:20:30 +02006093#if defined(HAVE_WAITID) && !defined(__APPLE__)
6094PyDoc_STRVAR(posix_waitid__doc__,
6095"waitid(idtype, id, options) -> waitid_result\n\n\
6096Wait for the completion of one or more child processes.\n\n\
6097idtype can be P_PID, P_PGID or P_ALL.\n\
6098id specifies the pid to wait on.\n\
6099options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6100or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6101Returns either waitid_result or None if WNOHANG is specified and there are\n\
6102no children in a waitable state.");
6103
6104static PyObject *
6105posix_waitid(PyObject *self, PyObject *args)
6106{
6107 PyObject *result;
6108 idtype_t idtype;
6109 id_t id;
6110 int options, res;
6111 siginfo_t si;
6112 si.si_pid = 0;
6113 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6114 return NULL;
6115 Py_BEGIN_ALLOW_THREADS
6116 res = waitid(idtype, id, &si, options);
6117 Py_END_ALLOW_THREADS
6118 if (res == -1)
6119 return posix_error();
6120
6121 if (si.si_pid == 0)
6122 Py_RETURN_NONE;
6123
6124 result = PyStructSequence_New(&WaitidResultType);
6125 if (!result)
6126 return NULL;
6127
6128 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6129 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6130 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6131 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6132 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6133 if (PyErr_Occurred()) {
6134 Py_DECREF(result);
6135 return NULL;
6136 }
6137
6138 return result;
6139}
6140#endif
6141
Guido van Rossumb6775db1994-08-01 11:34:53 +00006142#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006143PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006144"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006145Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006146
Barry Warsaw53699e91996-12-10 23:23:01 +00006147static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006148posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006149{
Victor Stinner8c62be82010-05-06 00:08:46 +00006150 pid_t pid;
6151 int options;
6152 WAIT_TYPE status;
6153 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006154
Victor Stinner8c62be82010-05-06 00:08:46 +00006155 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6156 return NULL;
6157 Py_BEGIN_ALLOW_THREADS
6158 pid = waitpid(pid, &status, options);
6159 Py_END_ALLOW_THREADS
6160 if (pid == -1)
6161 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006162
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006164}
6165
Tim Petersab034fa2002-02-01 11:27:43 +00006166#elif defined(HAVE_CWAIT)
6167
6168/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006169PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006170"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006172
6173static PyObject *
6174posix_waitpid(PyObject *self, PyObject *args)
6175{
Victor Stinner8c62be82010-05-06 00:08:46 +00006176 Py_intptr_t pid;
6177 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006178
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6180 return NULL;
6181 Py_BEGIN_ALLOW_THREADS
6182 pid = _cwait(&status, pid, options);
6183 Py_END_ALLOW_THREADS
6184 if (pid == -1)
6185 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006186
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 /* shift the status left a byte so this is more like the POSIX waitpid */
6188 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006189}
6190#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006191
Guido van Rossumad0ee831995-03-01 10:34:45 +00006192#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006193PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006194"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006195Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006196
Barry Warsaw53699e91996-12-10 23:23:01 +00006197static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006198posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006199{
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 pid_t pid;
6201 WAIT_TYPE status;
6202 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006203
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 Py_BEGIN_ALLOW_THREADS
6205 pid = wait(&status);
6206 Py_END_ALLOW_THREADS
6207 if (pid == -1)
6208 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006209
Victor Stinner8c62be82010-05-06 00:08:46 +00006210 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006211}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006212#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006213
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006214
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006215PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006216"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006217Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006218
Barry Warsaw53699e91996-12-10 23:23:01 +00006219static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006220posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006221{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006222#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006223 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006224#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006225#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006226 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006227 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006228#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006229 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006230#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006231#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006232}
6233
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006234
Guido van Rossumb6775db1994-08-01 11:34:53 +00006235#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006237"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006239
Barry Warsaw53699e91996-12-10 23:23:01 +00006240static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006241posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006242{
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 PyObject* v;
6244 char buf[MAXPATHLEN];
6245 PyObject *opath;
6246 char *path;
6247 int n;
6248 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006249
Victor Stinner8c62be82010-05-06 00:08:46 +00006250 if (!PyArg_ParseTuple(args, "O&:readlink",
6251 PyUnicode_FSConverter, &opath))
6252 return NULL;
6253 path = PyBytes_AsString(opath);
6254 v = PySequence_GetItem(args, 0);
6255 if (v == NULL) {
6256 Py_DECREF(opath);
6257 return NULL;
6258 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006259
Victor Stinner8c62be82010-05-06 00:08:46 +00006260 if (PyUnicode_Check(v)) {
6261 arg_is_unicode = 1;
6262 }
6263 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006264
Victor Stinner8c62be82010-05-06 00:08:46 +00006265 Py_BEGIN_ALLOW_THREADS
6266 n = readlink(path, buf, (int) sizeof buf);
6267 Py_END_ALLOW_THREADS
6268 if (n < 0)
6269 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006270
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006272 if (arg_is_unicode)
6273 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6274 else
6275 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006276}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006277#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006279
Brian Curtin52173d42010-12-02 18:29:18 +00006280#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006281PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006282"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006283Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006284
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006286posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006287{
Victor Stinner8c62be82010-05-06 00:08:46 +00006288 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006289}
6290#endif /* HAVE_SYMLINK */
6291
Brian Curtind40e6f72010-07-08 21:39:08 +00006292#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6293
6294PyDoc_STRVAR(win_readlink__doc__,
6295"readlink(path) -> path\n\n\
6296Return a string representing the path to which the symbolic link points.");
6297
Brian Curtind40e6f72010-07-08 21:39:08 +00006298/* Windows readlink implementation */
6299static PyObject *
6300win_readlink(PyObject *self, PyObject *args)
6301{
6302 wchar_t *path;
6303 DWORD n_bytes_returned;
6304 DWORD io_result;
6305 PyObject *result;
6306 HANDLE reparse_point_handle;
6307
6308 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6309 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6310 wchar_t *print_name;
6311
6312 if (!PyArg_ParseTuple(args,
6313 "u:readlink",
6314 &path))
6315 return NULL;
6316
6317 /* First get a handle to the reparse point */
6318 Py_BEGIN_ALLOW_THREADS
6319 reparse_point_handle = CreateFileW(
6320 path,
6321 0,
6322 0,
6323 0,
6324 OPEN_EXISTING,
6325 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6326 0);
6327 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006328
Brian Curtind40e6f72010-07-08 21:39:08 +00006329 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6330 {
6331 return win32_error_unicode("readlink", path);
6332 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006333
Brian Curtind40e6f72010-07-08 21:39:08 +00006334 Py_BEGIN_ALLOW_THREADS
6335 /* New call DeviceIoControl to read the reparse point */
6336 io_result = DeviceIoControl(
6337 reparse_point_handle,
6338 FSCTL_GET_REPARSE_POINT,
6339 0, 0, /* in buffer */
6340 target_buffer, sizeof(target_buffer),
6341 &n_bytes_returned,
6342 0 /* we're not using OVERLAPPED_IO */
6343 );
6344 CloseHandle(reparse_point_handle);
6345 Py_END_ALLOW_THREADS
6346
6347 if (io_result==0)
6348 {
6349 return win32_error_unicode("readlink", path);
6350 }
6351
6352 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6353 {
6354 PyErr_SetString(PyExc_ValueError,
6355 "not a symbolic link");
6356 return NULL;
6357 }
Brian Curtin74e45612010-07-09 15:58:59 +00006358 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6359 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6360
6361 result = PyUnicode_FromWideChar(print_name,
6362 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006363 return result;
6364}
6365
6366#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6367
Brian Curtin52173d42010-12-02 18:29:18 +00006368#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006369
6370/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6371static int has_CreateSymbolicLinkW = 0;
6372static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6373static int
6374check_CreateSymbolicLinkW()
6375{
6376 HINSTANCE hKernel32;
6377 /* only recheck */
6378 if (has_CreateSymbolicLinkW)
6379 return has_CreateSymbolicLinkW;
6380 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006381 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6382 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006383 if (Py_CreateSymbolicLinkW)
6384 has_CreateSymbolicLinkW = 1;
6385 return has_CreateSymbolicLinkW;
6386}
6387
6388PyDoc_STRVAR(win_symlink__doc__,
6389"symlink(src, dst, target_is_directory=False)\n\n\
6390Create a symbolic link pointing to src named dst.\n\
6391target_is_directory is required if the target is to be interpreted as\n\
6392a directory.\n\
6393This function requires Windows 6.0 or greater, and raises a\n\
6394NotImplementedError otherwise.");
6395
6396static PyObject *
6397win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6398{
6399 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6400 PyObject *src, *dest;
6401 int target_is_directory = 0;
6402 DWORD res;
6403 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006404
Brian Curtind40e6f72010-07-08 21:39:08 +00006405 if (!check_CreateSymbolicLinkW())
6406 {
6407 /* raise NotImplementedError */
6408 return PyErr_Format(PyExc_NotImplementedError,
6409 "CreateSymbolicLinkW not found");
6410 }
6411 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6412 kwlist, &src, &dest, &target_is_directory))
6413 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006414
6415 if (win32_can_symlink == 0)
6416 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6417
Brian Curtind40e6f72010-07-08 21:39:08 +00006418 if (!convert_to_unicode(&src)) { return NULL; }
6419 if (!convert_to_unicode(&dest)) {
6420 Py_DECREF(src);
6421 return NULL;
6422 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006423
Brian Curtind40e6f72010-07-08 21:39:08 +00006424 /* if src is a directory, ensure target_is_directory==1 */
6425 if(
6426 GetFileAttributesExW(
6427 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6428 ))
6429 {
6430 target_is_directory = target_is_directory ||
6431 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6432 }
6433
6434 Py_BEGIN_ALLOW_THREADS
6435 res = Py_CreateSymbolicLinkW(
6436 PyUnicode_AsUnicode(dest),
6437 PyUnicode_AsUnicode(src),
6438 target_is_directory);
6439 Py_END_ALLOW_THREADS
6440 Py_DECREF(src);
6441 Py_DECREF(dest);
6442 if (!res)
6443 {
6444 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6445 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006446
Brian Curtind40e6f72010-07-08 21:39:08 +00006447 Py_INCREF(Py_None);
6448 return Py_None;
6449}
Brian Curtin52173d42010-12-02 18:29:18 +00006450#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006451
6452#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006453#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6454static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006455system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006456{
6457 ULONG value = 0;
6458
6459 Py_BEGIN_ALLOW_THREADS
6460 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6461 Py_END_ALLOW_THREADS
6462
6463 return value;
6464}
6465
6466static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006467posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006468{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006469 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 return Py_BuildValue("ddddd",
6471 (double)0 /* t.tms_utime / HZ */,
6472 (double)0 /* t.tms_stime / HZ */,
6473 (double)0 /* t.tms_cutime / HZ */,
6474 (double)0 /* t.tms_cstime / HZ */,
6475 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006476}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006477#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006478#define NEED_TICKS_PER_SECOND
6479static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006480static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006481posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006482{
Victor Stinner8c62be82010-05-06 00:08:46 +00006483 struct tms t;
6484 clock_t c;
6485 errno = 0;
6486 c = times(&t);
6487 if (c == (clock_t) -1)
6488 return posix_error();
6489 return Py_BuildValue("ddddd",
6490 (double)t.tms_utime / ticks_per_second,
6491 (double)t.tms_stime / ticks_per_second,
6492 (double)t.tms_cutime / ticks_per_second,
6493 (double)t.tms_cstime / ticks_per_second,
6494 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006495}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006496#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006497#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006498
6499
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006500#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006501#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006502static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006503posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006504{
Victor Stinner8c62be82010-05-06 00:08:46 +00006505 FILETIME create, exit, kernel, user;
6506 HANDLE hProc;
6507 hProc = GetCurrentProcess();
6508 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6509 /* The fields of a FILETIME structure are the hi and lo part
6510 of a 64-bit value expressed in 100 nanosecond units.
6511 1e7 is one second in such units; 1e-7 the inverse.
6512 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6513 */
6514 return Py_BuildValue(
6515 "ddddd",
6516 (double)(user.dwHighDateTime*429.4967296 +
6517 user.dwLowDateTime*1e-7),
6518 (double)(kernel.dwHighDateTime*429.4967296 +
6519 kernel.dwLowDateTime*1e-7),
6520 (double)0,
6521 (double)0,
6522 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006523}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006524#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006525
6526#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006527PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006528"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006529Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006530#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006531
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006532
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006533#ifdef HAVE_GETSID
6534PyDoc_STRVAR(posix_getsid__doc__,
6535"getsid(pid) -> sid\n\n\
6536Call the system call getsid().");
6537
6538static PyObject *
6539posix_getsid(PyObject *self, PyObject *args)
6540{
Victor Stinner8c62be82010-05-06 00:08:46 +00006541 pid_t pid;
6542 int sid;
6543 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6544 return NULL;
6545 sid = getsid(pid);
6546 if (sid < 0)
6547 return posix_error();
6548 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006549}
6550#endif /* HAVE_GETSID */
6551
6552
Guido van Rossumb6775db1994-08-01 11:34:53 +00006553#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006554PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006555"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006556Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006557
Barry Warsaw53699e91996-12-10 23:23:01 +00006558static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006559posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006560{
Victor Stinner8c62be82010-05-06 00:08:46 +00006561 if (setsid() < 0)
6562 return posix_error();
6563 Py_INCREF(Py_None);
6564 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006565}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006566#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006567
Guido van Rossumb6775db1994-08-01 11:34:53 +00006568#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006569PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006570"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006571Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006572
Barry Warsaw53699e91996-12-10 23:23:01 +00006573static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006574posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006575{
Victor Stinner8c62be82010-05-06 00:08:46 +00006576 pid_t pid;
6577 int pgrp;
6578 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6579 return NULL;
6580 if (setpgid(pid, pgrp) < 0)
6581 return posix_error();
6582 Py_INCREF(Py_None);
6583 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006584}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006585#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006586
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006587
Guido van Rossumb6775db1994-08-01 11:34:53 +00006588#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006589PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006590"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006591Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006592
Barry Warsaw53699e91996-12-10 23:23:01 +00006593static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006594posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006595{
Victor Stinner8c62be82010-05-06 00:08:46 +00006596 int fd;
6597 pid_t pgid;
6598 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6599 return NULL;
6600 pgid = tcgetpgrp(fd);
6601 if (pgid < 0)
6602 return posix_error();
6603 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006604}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006605#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006606
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006607
Guido van Rossumb6775db1994-08-01 11:34:53 +00006608#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006609PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006610"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006611Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006612
Barry Warsaw53699e91996-12-10 23:23:01 +00006613static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006614posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006615{
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 int fd;
6617 pid_t pgid;
6618 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6619 return NULL;
6620 if (tcsetpgrp(fd, pgid) < 0)
6621 return posix_error();
6622 Py_INCREF(Py_None);
6623 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006624}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006625#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006626
Guido van Rossum687dd131993-05-17 08:34:16 +00006627/* Functions acting on file descriptors */
6628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006629PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006630"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006631Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006632
Barry Warsaw53699e91996-12-10 23:23:01 +00006633static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006634posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006635{
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 PyObject *ofile;
6637 char *file;
6638 int flag;
6639 int mode = 0777;
6640 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006641
6642#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006644 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 Py_BEGIN_ALLOW_THREADS
6646 /* PyUnicode_AS_UNICODE OK without thread
6647 lock as it is a simple dereference. */
6648 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6649 Py_END_ALLOW_THREADS
6650 if (fd < 0)
6651 return posix_error();
6652 return PyLong_FromLong((long)fd);
6653 }
6654 /* Drop the argument parsing error as narrow strings
6655 are also valid. */
6656 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006657#endif
6658
Victor Stinner26de69d2011-06-17 15:15:38 +02006659 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006660 PyUnicode_FSConverter, &ofile,
6661 &flag, &mode))
6662 return NULL;
6663 file = PyBytes_AsString(ofile);
6664 Py_BEGIN_ALLOW_THREADS
6665 fd = open(file, flag, mode);
6666 Py_END_ALLOW_THREADS
6667 if (fd < 0)
6668 return posix_error_with_allocated_filename(ofile);
6669 Py_DECREF(ofile);
6670 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006671}
6672
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006674PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006675"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006676Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006677
Barry Warsaw53699e91996-12-10 23:23:01 +00006678static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006679posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006680{
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 int fd, res;
6682 if (!PyArg_ParseTuple(args, "i:close", &fd))
6683 return NULL;
6684 if (!_PyVerify_fd(fd))
6685 return posix_error();
6686 Py_BEGIN_ALLOW_THREADS
6687 res = close(fd);
6688 Py_END_ALLOW_THREADS
6689 if (res < 0)
6690 return posix_error();
6691 Py_INCREF(Py_None);
6692 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006693}
6694
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006695
Victor Stinner8c62be82010-05-06 00:08:46 +00006696PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006697"closerange(fd_low, fd_high)\n\n\
6698Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6699
6700static PyObject *
6701posix_closerange(PyObject *self, PyObject *args)
6702{
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 int fd_from, fd_to, i;
6704 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6705 return NULL;
6706 Py_BEGIN_ALLOW_THREADS
6707 for (i = fd_from; i < fd_to; i++)
6708 if (_PyVerify_fd(i))
6709 close(i);
6710 Py_END_ALLOW_THREADS
6711 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006712}
6713
6714
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006715PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006716"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006717Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006718
Barry Warsaw53699e91996-12-10 23:23:01 +00006719static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006720posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006721{
Victor Stinner8c62be82010-05-06 00:08:46 +00006722 int fd;
6723 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6724 return NULL;
6725 if (!_PyVerify_fd(fd))
6726 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006728 if (fd < 0)
6729 return posix_error();
6730 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006731}
6732
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006734PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006735"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006736Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006737
Barry Warsaw53699e91996-12-10 23:23:01 +00006738static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006739posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006740{
Victor Stinner8c62be82010-05-06 00:08:46 +00006741 int fd, fd2, res;
6742 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6743 return NULL;
6744 if (!_PyVerify_fd_dup2(fd, fd2))
6745 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006746 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 if (res < 0)
6748 return posix_error();
6749 Py_INCREF(Py_None);
6750 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006751}
6752
Ross Lagerwall7807c352011-03-17 20:20:30 +02006753#ifdef HAVE_LOCKF
6754PyDoc_STRVAR(posix_lockf__doc__,
6755"lockf(fd, cmd, len)\n\n\
6756Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6757fd is an open file descriptor.\n\
6758cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6759F_TEST.\n\
6760len specifies the section of the file to lock.");
6761
6762static PyObject *
6763posix_lockf(PyObject *self, PyObject *args)
6764{
6765 int fd, cmd, res;
6766 off_t len;
6767 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6768 &fd, &cmd, _parse_off_t, &len))
6769 return NULL;
6770
6771 Py_BEGIN_ALLOW_THREADS
6772 res = lockf(fd, cmd, len);
6773 Py_END_ALLOW_THREADS
6774
6775 if (res < 0)
6776 return posix_error();
6777
6778 Py_RETURN_NONE;
6779}
6780#endif
6781
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006783PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006784"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006785Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006786
Barry Warsaw53699e91996-12-10 23:23:01 +00006787static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006788posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006789{
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006791#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006793#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006794 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006795#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006796 PyObject *posobj;
6797 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006799#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6801 switch (how) {
6802 case 0: how = SEEK_SET; break;
6803 case 1: how = SEEK_CUR; break;
6804 case 2: how = SEEK_END; break;
6805 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006806#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006807
Ross Lagerwall8e749672011-03-17 21:54:07 +02006808#if !defined(HAVE_LARGEFILE_SUPPORT)
6809 pos = PyLong_AsLong(posobj);
6810#else
6811 pos = PyLong_AsLongLong(posobj);
6812#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 if (PyErr_Occurred())
6814 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006815
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 if (!_PyVerify_fd(fd))
6817 return posix_error();
6818 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006819#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006821#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006823#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006824 Py_END_ALLOW_THREADS
6825 if (res < 0)
6826 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006827
6828#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006830#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006832#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006833}
6834
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006835
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006836PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006837"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006838Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006839
Barry Warsaw53699e91996-12-10 23:23:01 +00006840static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006841posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006842{
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 int fd, size;
6844 Py_ssize_t n;
6845 PyObject *buffer;
6846 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6847 return NULL;
6848 if (size < 0) {
6849 errno = EINVAL;
6850 return posix_error();
6851 }
6852 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6853 if (buffer == NULL)
6854 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006855 if (!_PyVerify_fd(fd)) {
6856 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006857 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006858 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 Py_BEGIN_ALLOW_THREADS
6860 n = read(fd, PyBytes_AS_STRING(buffer), size);
6861 Py_END_ALLOW_THREADS
6862 if (n < 0) {
6863 Py_DECREF(buffer);
6864 return posix_error();
6865 }
6866 if (n != size)
6867 _PyBytes_Resize(&buffer, n);
6868 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006869}
6870
Ross Lagerwall7807c352011-03-17 20:20:30 +02006871#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6872 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006873static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006874iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6875{
6876 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006877 Py_ssize_t blen, total = 0;
6878
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006879 *iov = PyMem_New(struct iovec, cnt);
6880 if (*iov == NULL) {
6881 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006882 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006883 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006884
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006885 *buf = PyMem_New(Py_buffer, cnt);
6886 if (*buf == NULL) {
6887 PyMem_Del(*iov);
6888 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006889 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006890 }
6891
6892 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006893 PyObject *item = PySequence_GetItem(seq, i);
6894 if (item == NULL)
6895 goto fail;
6896 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6897 Py_DECREF(item);
6898 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006899 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006900 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006901 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006902 blen = (*buf)[i].len;
6903 (*iov)[i].iov_len = blen;
6904 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006905 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006906 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006907
6908fail:
6909 PyMem_Del(*iov);
6910 for (j = 0; j < i; j++) {
6911 PyBuffer_Release(&(*buf)[j]);
6912 }
6913 PyMem_Del(*buf);
6914 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006915}
6916
6917static void
6918iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6919{
6920 int i;
6921 PyMem_Del(iov);
6922 for (i = 0; i < cnt; i++) {
6923 PyBuffer_Release(&buf[i]);
6924 }
6925 PyMem_Del(buf);
6926}
6927#endif
6928
Ross Lagerwall7807c352011-03-17 20:20:30 +02006929#ifdef HAVE_READV
6930PyDoc_STRVAR(posix_readv__doc__,
6931"readv(fd, buffers) -> bytesread\n\n\
6932Read from a file descriptor into a number of writable buffers. buffers\n\
6933is an arbitrary sequence of writable buffers.\n\
6934Returns the total number of bytes read.");
6935
6936static PyObject *
6937posix_readv(PyObject *self, PyObject *args)
6938{
6939 int fd, cnt;
6940 Py_ssize_t n;
6941 PyObject *seq;
6942 struct iovec *iov;
6943 Py_buffer *buf;
6944
6945 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6946 return NULL;
6947 if (!PySequence_Check(seq)) {
6948 PyErr_SetString(PyExc_TypeError,
6949 "readv() arg 2 must be a sequence");
6950 return NULL;
6951 }
6952 cnt = PySequence_Size(seq);
6953
6954 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6955 return NULL;
6956
6957 Py_BEGIN_ALLOW_THREADS
6958 n = readv(fd, iov, cnt);
6959 Py_END_ALLOW_THREADS
6960
6961 iov_cleanup(iov, buf, cnt);
6962 return PyLong_FromSsize_t(n);
6963}
6964#endif
6965
6966#ifdef HAVE_PREAD
6967PyDoc_STRVAR(posix_pread__doc__,
6968"pread(fd, buffersize, offset) -> string\n\n\
6969Read from a file descriptor, fd, at a position of offset. It will read up\n\
6970to buffersize number of bytes. The file offset remains unchanged.");
6971
6972static PyObject *
6973posix_pread(PyObject *self, PyObject *args)
6974{
6975 int fd, size;
6976 off_t offset;
6977 Py_ssize_t n;
6978 PyObject *buffer;
6979 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6980 return NULL;
6981
6982 if (size < 0) {
6983 errno = EINVAL;
6984 return posix_error();
6985 }
6986 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6987 if (buffer == NULL)
6988 return NULL;
6989 if (!_PyVerify_fd(fd)) {
6990 Py_DECREF(buffer);
6991 return posix_error();
6992 }
6993 Py_BEGIN_ALLOW_THREADS
6994 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6995 Py_END_ALLOW_THREADS
6996 if (n < 0) {
6997 Py_DECREF(buffer);
6998 return posix_error();
6999 }
7000 if (n != size)
7001 _PyBytes_Resize(&buffer, n);
7002 return buffer;
7003}
7004#endif
7005
7006PyDoc_STRVAR(posix_write__doc__,
7007"write(fd, string) -> byteswritten\n\n\
7008Write a string to a file descriptor.");
7009
7010static PyObject *
7011posix_write(PyObject *self, PyObject *args)
7012{
7013 Py_buffer pbuf;
7014 int fd;
7015 Py_ssize_t size, len;
7016
7017 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7018 return NULL;
7019 if (!_PyVerify_fd(fd)) {
7020 PyBuffer_Release(&pbuf);
7021 return posix_error();
7022 }
7023 len = pbuf.len;
7024 Py_BEGIN_ALLOW_THREADS
7025#if defined(MS_WIN64) || defined(MS_WINDOWS)
7026 if (len > INT_MAX)
7027 len = INT_MAX;
7028 size = write(fd, pbuf.buf, (int)len);
7029#else
7030 size = write(fd, pbuf.buf, len);
7031#endif
7032 Py_END_ALLOW_THREADS
7033 PyBuffer_Release(&pbuf);
7034 if (size < 0)
7035 return posix_error();
7036 return PyLong_FromSsize_t(size);
7037}
7038
7039#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007040PyDoc_STRVAR(posix_sendfile__doc__,
7041"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7042sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7043 -> byteswritten\n\
7044Copy nbytes bytes from file descriptor in to file descriptor out.");
7045
7046static PyObject *
7047posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7048{
7049 int in, out;
7050 Py_ssize_t ret;
7051 off_t offset;
7052
7053#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7054#ifndef __APPLE__
7055 Py_ssize_t len;
7056#endif
7057 PyObject *headers = NULL, *trailers = NULL;
7058 Py_buffer *hbuf, *tbuf;
7059 off_t sbytes;
7060 struct sf_hdtr sf;
7061 int flags = 0;
7062 sf.headers = NULL;
7063 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007064 static char *keywords[] = {"out", "in",
7065 "offset", "count",
7066 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007067
7068#ifdef __APPLE__
7069 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007070 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007071#else
7072 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007073 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007074#endif
7075 &headers, &trailers, &flags))
7076 return NULL;
7077 if (headers != NULL) {
7078 if (!PySequence_Check(headers)) {
7079 PyErr_SetString(PyExc_TypeError,
7080 "sendfile() headers must be a sequence or None");
7081 return NULL;
7082 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007083 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007084 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007085 if (sf.hdr_cnt > 0 &&
7086 !(i = iov_setup(&(sf.headers), &hbuf,
7087 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007088 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007089#ifdef __APPLE__
7090 sbytes += i;
7091#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007092 }
7093 }
7094 if (trailers != NULL) {
7095 if (!PySequence_Check(trailers)) {
7096 PyErr_SetString(PyExc_TypeError,
7097 "sendfile() trailers must be a sequence or None");
7098 return NULL;
7099 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007100 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007101 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007102 if (sf.trl_cnt > 0 &&
7103 !(i = iov_setup(&(sf.trailers), &tbuf,
7104 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007105 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007106#ifdef __APPLE__
7107 sbytes += i;
7108#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007109 }
7110 }
7111
7112 Py_BEGIN_ALLOW_THREADS
7113#ifdef __APPLE__
7114 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7115#else
7116 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7117#endif
7118 Py_END_ALLOW_THREADS
7119
7120 if (sf.headers != NULL)
7121 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7122 if (sf.trailers != NULL)
7123 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7124
7125 if (ret < 0) {
7126 if ((errno == EAGAIN) || (errno == EBUSY)) {
7127 if (sbytes != 0) {
7128 // some data has been sent
7129 goto done;
7130 }
7131 else {
7132 // no data has been sent; upper application is supposed
7133 // to retry on EAGAIN or EBUSY
7134 return posix_error();
7135 }
7136 }
7137 return posix_error();
7138 }
7139 goto done;
7140
7141done:
7142 #if !defined(HAVE_LARGEFILE_SUPPORT)
7143 return Py_BuildValue("l", sbytes);
7144 #else
7145 return Py_BuildValue("L", sbytes);
7146 #endif
7147
7148#else
7149 Py_ssize_t count;
7150 PyObject *offobj;
7151 static char *keywords[] = {"out", "in",
7152 "offset", "count", NULL};
7153 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7154 keywords, &out, &in, &offobj, &count))
7155 return NULL;
7156#ifdef linux
7157 if (offobj == Py_None) {
7158 Py_BEGIN_ALLOW_THREADS
7159 ret = sendfile(out, in, NULL, count);
7160 Py_END_ALLOW_THREADS
7161 if (ret < 0)
7162 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007163 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007164 }
7165#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007166 if (!_parse_off_t(offobj, &offset))
7167 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007168 Py_BEGIN_ALLOW_THREADS
7169 ret = sendfile(out, in, &offset, count);
7170 Py_END_ALLOW_THREADS
7171 if (ret < 0)
7172 return posix_error();
7173 return Py_BuildValue("n", ret);
7174#endif
7175}
7176#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007178PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007179"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007180Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007181
Barry Warsaw53699e91996-12-10 23:23:01 +00007182static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007183posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007184{
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 int fd;
7186 STRUCT_STAT st;
7187 int res;
7188 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7189 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007190#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007191 /* on OpenVMS we must ensure that all bytes are written to the file */
7192 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007193#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 if (!_PyVerify_fd(fd))
7195 return posix_error();
7196 Py_BEGIN_ALLOW_THREADS
7197 res = FSTAT(fd, &st);
7198 Py_END_ALLOW_THREADS
7199 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007200#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007202#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007204#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 }
Tim Peters5aa91602002-01-30 05:46:57 +00007206
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007208}
7209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007210PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007211"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007212Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007213connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007214
7215static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007216posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007217{
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 int fd;
7219 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7220 return NULL;
7221 if (!_PyVerify_fd(fd))
7222 return PyBool_FromLong(0);
7223 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007224}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007225
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007226#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007227PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007228"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007229Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007230
Barry Warsaw53699e91996-12-10 23:23:01 +00007231static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007232posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007233{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007234#if defined(PYOS_OS2)
7235 HFILE read, write;
7236 APIRET rc;
7237
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007238 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007239 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007240 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007241
7242 return Py_BuildValue("(ii)", read, write);
7243#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007244#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 int fds[2];
7246 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007247 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 if (res != 0)
7249 return posix_error();
7250 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007251#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 HANDLE read, write;
7253 int read_fd, write_fd;
7254 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007256 if (!ok)
7257 return win32_error("CreatePipe", NULL);
7258 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7259 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7260 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007261#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007262#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007263}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007264#endif /* HAVE_PIPE */
7265
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007266#ifdef HAVE_PIPE2
7267PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007268"pipe2(flags) -> (read_end, write_end)\n\n\
7269Create a pipe with flags set atomically.\n\
7270flags can be constructed by ORing together one or more of these values:\n\
7271O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007272");
7273
7274static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007275posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007276{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007277 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007278 int fds[2];
7279 int res;
7280
Charles-François Natali368f34b2011-06-06 19:49:47 +02007281 flags = PyLong_AsLong(arg);
7282 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007283 return NULL;
7284
7285 res = pipe2(fds, flags);
7286 if (res != 0)
7287 return posix_error();
7288 return Py_BuildValue("(ii)", fds[0], fds[1]);
7289}
7290#endif /* HAVE_PIPE2 */
7291
Ross Lagerwall7807c352011-03-17 20:20:30 +02007292#ifdef HAVE_WRITEV
7293PyDoc_STRVAR(posix_writev__doc__,
7294"writev(fd, buffers) -> byteswritten\n\n\
7295Write the contents of buffers to a file descriptor, where buffers is an\n\
7296arbitrary sequence of buffers.\n\
7297Returns the total bytes written.");
7298
7299static PyObject *
7300posix_writev(PyObject *self, PyObject *args)
7301{
7302 int fd, cnt;
7303 Py_ssize_t res;
7304 PyObject *seq;
7305 struct iovec *iov;
7306 Py_buffer *buf;
7307 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7308 return NULL;
7309 if (!PySequence_Check(seq)) {
7310 PyErr_SetString(PyExc_TypeError,
7311 "writev() arg 2 must be a sequence");
7312 return NULL;
7313 }
7314 cnt = PySequence_Size(seq);
7315
7316 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7317 return NULL;
7318 }
7319
7320 Py_BEGIN_ALLOW_THREADS
7321 res = writev(fd, iov, cnt);
7322 Py_END_ALLOW_THREADS
7323
7324 iov_cleanup(iov, buf, cnt);
7325 return PyLong_FromSsize_t(res);
7326}
7327#endif
7328
7329#ifdef HAVE_PWRITE
7330PyDoc_STRVAR(posix_pwrite__doc__,
7331"pwrite(fd, string, offset) -> byteswritten\n\n\
7332Write string to a file descriptor, fd, from offset, leaving the file\n\
7333offset unchanged.");
7334
7335static PyObject *
7336posix_pwrite(PyObject *self, PyObject *args)
7337{
7338 Py_buffer pbuf;
7339 int fd;
7340 off_t offset;
7341 Py_ssize_t size;
7342
7343 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7344 return NULL;
7345
7346 if (!_PyVerify_fd(fd)) {
7347 PyBuffer_Release(&pbuf);
7348 return posix_error();
7349 }
7350 Py_BEGIN_ALLOW_THREADS
7351 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7352 Py_END_ALLOW_THREADS
7353 PyBuffer_Release(&pbuf);
7354 if (size < 0)
7355 return posix_error();
7356 return PyLong_FromSsize_t(size);
7357}
7358#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007359
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007360#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007361PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007362"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007363Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007364
Barry Warsaw53699e91996-12-10 23:23:01 +00007365static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007366posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007367{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007368 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 char *filename;
7370 int mode = 0666;
7371 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007372 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7373 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007375 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 Py_BEGIN_ALLOW_THREADS
7377 res = mkfifo(filename, mode);
7378 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007379 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 if (res < 0)
7381 return posix_error();
7382 Py_INCREF(Py_None);
7383 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007384}
7385#endif
7386
7387
Neal Norwitz11690112002-07-30 01:08:28 +00007388#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007389PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007390"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007391Create a filesystem node (file, device special file or named pipe)\n\
7392named filename. mode specifies both the permissions to use and the\n\
7393type of node to be created, being combined (bitwise OR) with one of\n\
7394S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007395device defines the newly created device special file (probably using\n\
7396os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007397
7398
7399static PyObject *
7400posix_mknod(PyObject *self, PyObject *args)
7401{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007402 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007403 char *filename;
7404 int mode = 0600;
7405 int device = 0;
7406 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007407 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7408 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007409 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007410 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 Py_BEGIN_ALLOW_THREADS
7412 res = mknod(filename, mode, device);
7413 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007414 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007415 if (res < 0)
7416 return posix_error();
7417 Py_INCREF(Py_None);
7418 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007419}
7420#endif
7421
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007422#ifdef HAVE_DEVICE_MACROS
7423PyDoc_STRVAR(posix_major__doc__,
7424"major(device) -> major number\n\
7425Extracts a device major number from a raw device number.");
7426
7427static PyObject *
7428posix_major(PyObject *self, PyObject *args)
7429{
Victor Stinner8c62be82010-05-06 00:08:46 +00007430 int device;
7431 if (!PyArg_ParseTuple(args, "i:major", &device))
7432 return NULL;
7433 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007434}
7435
7436PyDoc_STRVAR(posix_minor__doc__,
7437"minor(device) -> minor number\n\
7438Extracts a device minor number from a raw device number.");
7439
7440static PyObject *
7441posix_minor(PyObject *self, PyObject *args)
7442{
Victor Stinner8c62be82010-05-06 00:08:46 +00007443 int device;
7444 if (!PyArg_ParseTuple(args, "i:minor", &device))
7445 return NULL;
7446 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007447}
7448
7449PyDoc_STRVAR(posix_makedev__doc__,
7450"makedev(major, minor) -> device number\n\
7451Composes a raw device number from the major and minor device numbers.");
7452
7453static PyObject *
7454posix_makedev(PyObject *self, PyObject *args)
7455{
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 int major, minor;
7457 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7458 return NULL;
7459 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007460}
7461#endif /* device macros */
7462
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007463
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007464#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007465PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007466"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007467Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007468
Barry Warsaw53699e91996-12-10 23:23:01 +00007469static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007470posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007471{
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 int fd;
7473 off_t length;
7474 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007475
Ross Lagerwall7807c352011-03-17 20:20:30 +02007476 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007477 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007478
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 Py_BEGIN_ALLOW_THREADS
7480 res = ftruncate(fd, length);
7481 Py_END_ALLOW_THREADS
7482 if (res < 0)
7483 return posix_error();
7484 Py_INCREF(Py_None);
7485 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007486}
7487#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007488
Ross Lagerwall7807c352011-03-17 20:20:30 +02007489#ifdef HAVE_TRUNCATE
7490PyDoc_STRVAR(posix_truncate__doc__,
7491"truncate(path, length)\n\n\
7492Truncate the file given by path to length bytes.");
7493
7494static PyObject *
7495posix_truncate(PyObject *self, PyObject *args)
7496{
7497 PyObject *opath;
7498 const char *path;
7499 off_t length;
7500 int res;
7501
7502 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7503 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7504 return NULL;
7505 path = PyBytes_AsString(opath);
7506
7507 Py_BEGIN_ALLOW_THREADS
7508 res = truncate(path, length);
7509 Py_END_ALLOW_THREADS
7510 Py_DECREF(opath);
7511 if (res < 0)
7512 return posix_error();
7513 Py_RETURN_NONE;
7514}
7515#endif
7516
7517#ifdef HAVE_POSIX_FALLOCATE
7518PyDoc_STRVAR(posix_posix_fallocate__doc__,
7519"posix_fallocate(fd, offset, len)\n\n\
7520Ensures that enough disk space is allocated for the file specified by fd\n\
7521starting from offset and continuing for len bytes.");
7522
7523static PyObject *
7524posix_posix_fallocate(PyObject *self, PyObject *args)
7525{
7526 off_t len, offset;
7527 int res, fd;
7528
7529 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7530 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7531 return NULL;
7532
7533 Py_BEGIN_ALLOW_THREADS
7534 res = posix_fallocate(fd, offset, len);
7535 Py_END_ALLOW_THREADS
7536 if (res != 0) {
7537 errno = res;
7538 return posix_error();
7539 }
7540 Py_RETURN_NONE;
7541}
7542#endif
7543
7544#ifdef HAVE_POSIX_FADVISE
7545PyDoc_STRVAR(posix_posix_fadvise__doc__,
7546"posix_fadvise(fd, offset, len, advice)\n\n\
7547Announces an intention to access data in a specific pattern thus allowing\n\
7548the kernel to make optimizations.\n\
7549The advice applies to the region of the file specified by fd starting at\n\
7550offset and continuing for len bytes.\n\
7551advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7552POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7553POSIX_FADV_DONTNEED.");
7554
7555static PyObject *
7556posix_posix_fadvise(PyObject *self, PyObject *args)
7557{
7558 off_t len, offset;
7559 int res, fd, advice;
7560
7561 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7562 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7563 return NULL;
7564
7565 Py_BEGIN_ALLOW_THREADS
7566 res = posix_fadvise(fd, offset, len, advice);
7567 Py_END_ALLOW_THREADS
7568 if (res != 0) {
7569 errno = res;
7570 return posix_error();
7571 }
7572 Py_RETURN_NONE;
7573}
7574#endif
7575
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007576#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007577PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007578"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007579Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007580
Fred Drake762e2061999-08-26 17:23:54 +00007581/* Save putenv() parameters as values here, so we can collect them when they
7582 * get re-set with another call for the same key. */
7583static PyObject *posix_putenv_garbage;
7584
Tim Peters5aa91602002-01-30 05:46:57 +00007585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007586posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007587{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007588#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 wchar_t *s1, *s2;
7590 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007591#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007592 PyObject *os1, *os2;
7593 char *s1, *s2;
7594 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007595#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007596 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007597 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007598
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007599#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007600 if (!PyArg_ParseTuple(args,
7601 "uu:putenv",
7602 &s1, &s2))
7603 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007604#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 if (!PyArg_ParseTuple(args,
7606 "O&O&:putenv",
7607 PyUnicode_FSConverter, &os1,
7608 PyUnicode_FSConverter, &os2))
7609 return NULL;
7610 s1 = PyBytes_AsString(os1);
7611 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007612#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007613
7614#if defined(PYOS_OS2)
7615 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7616 APIRET rc;
7617
Guido van Rossumd48f2521997-12-05 22:19:34 +00007618 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007619 if (rc != NO_ERROR) {
7620 os2_error(rc);
7621 goto error;
7622 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007623
7624 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7625 APIRET rc;
7626
Guido van Rossumd48f2521997-12-05 22:19:34 +00007627 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007628 if (rc != NO_ERROR) {
7629 os2_error(rc);
7630 goto error;
7631 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007632 } else {
7633#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 /* XXX This can leak memory -- not easy to fix :-( */
7635 /* len includes space for a trailing \0; the size arg to
7636 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007637#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007638 len = wcslen(s1) + wcslen(s2) + 2;
7639 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007640#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007641 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007643#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007644 if (newstr == NULL) {
7645 PyErr_NoMemory();
7646 goto error;
7647 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007648#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 newenv = PyUnicode_AsUnicode(newstr);
7650 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7651 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007652 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007653 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007654 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007655#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007656 newenv = PyBytes_AS_STRING(newstr);
7657 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7658 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007659 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007660 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007662#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007663
Victor Stinner8c62be82010-05-06 00:08:46 +00007664 /* Install the first arg and newstr in posix_putenv_garbage;
7665 * this will cause previous value to be collected. This has to
7666 * happen after the real putenv() call because the old value
7667 * was still accessible until then. */
7668 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007669#ifdef MS_WINDOWS
7670 PyTuple_GET_ITEM(args, 0),
7671#else
7672 os1,
7673#endif
7674 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007675 /* really not much we can do; just leak */
7676 PyErr_Clear();
7677 }
7678 else {
7679 Py_DECREF(newstr);
7680 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007681
7682#if defined(PYOS_OS2)
7683 }
7684#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007685
Martin v. Löwis011e8422009-05-05 04:43:17 +00007686#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 Py_DECREF(os1);
7688 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007689#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007690 Py_RETURN_NONE;
7691
7692error:
7693#ifndef MS_WINDOWS
7694 Py_DECREF(os1);
7695 Py_DECREF(os2);
7696#endif
7697 Py_XDECREF(newstr);
7698 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007699}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007700#endif /* putenv */
7701
Guido van Rossumc524d952001-10-19 01:31:59 +00007702#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007703PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007704"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007705Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007706
7707static PyObject *
7708posix_unsetenv(PyObject *self, PyObject *args)
7709{
Victor Stinner84ae1182010-05-06 22:05:07 +00007710#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007712
Victor Stinner8c62be82010-05-06 00:08:46 +00007713 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7714 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007715#else
7716 PyObject *os1;
7717 char *s1;
7718
7719 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7720 PyUnicode_FSConverter, &os1))
7721 return NULL;
7722 s1 = PyBytes_AsString(os1);
7723#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007724
Victor Stinner8c62be82010-05-06 00:08:46 +00007725 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007726
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 /* Remove the key from posix_putenv_garbage;
7728 * this will cause it to be collected. This has to
7729 * happen after the real unsetenv() call because the
7730 * old value was still accessible until then.
7731 */
7732 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007733#ifdef MS_WINDOWS
7734 PyTuple_GET_ITEM(args, 0)
7735#else
7736 os1
7737#endif
7738 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007739 /* really not much we can do; just leak */
7740 PyErr_Clear();
7741 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007742
Victor Stinner84ae1182010-05-06 22:05:07 +00007743#ifndef MS_WINDOWS
7744 Py_DECREF(os1);
7745#endif
7746 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007747}
7748#endif /* unsetenv */
7749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007750PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007751"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007752Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007753
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007754static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007755posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007756{
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 int code;
7758 char *message;
7759 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7760 return NULL;
7761 message = strerror(code);
7762 if (message == NULL) {
7763 PyErr_SetString(PyExc_ValueError,
7764 "strerror() argument out of range");
7765 return NULL;
7766 }
7767 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007768}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007769
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007770
Guido van Rossumc9641791998-08-04 15:26:23 +00007771#ifdef HAVE_SYS_WAIT_H
7772
Fred Drake106c1a02002-04-23 15:58:02 +00007773#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007774PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007775"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007776Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007777
7778static PyObject *
7779posix_WCOREDUMP(PyObject *self, PyObject *args)
7780{
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 WAIT_TYPE status;
7782 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007783
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7785 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007786
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007788}
7789#endif /* WCOREDUMP */
7790
7791#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007792PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007793"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007794Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007795job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007796
7797static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007798posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007799{
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 WAIT_TYPE status;
7801 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007802
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7804 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007805
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007807}
7808#endif /* WIFCONTINUED */
7809
Guido van Rossumc9641791998-08-04 15:26:23 +00007810#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007811PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007812"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007813Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007814
7815static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007816posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007817{
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 WAIT_TYPE status;
7819 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007820
Victor Stinner8c62be82010-05-06 00:08:46 +00007821 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7822 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007823
Victor Stinner8c62be82010-05-06 00:08:46 +00007824 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007825}
7826#endif /* WIFSTOPPED */
7827
7828#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007829PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007830"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007831Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007832
7833static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007834posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007835{
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 WAIT_TYPE status;
7837 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007838
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7840 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007841
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007843}
7844#endif /* WIFSIGNALED */
7845
7846#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007847PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007848"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007849Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007850system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007851
7852static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007853posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007854{
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 WAIT_TYPE status;
7856 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007857
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7859 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007860
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007862}
7863#endif /* WIFEXITED */
7864
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007865#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007866PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007867"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007868Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007869
7870static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007871posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007872{
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 WAIT_TYPE status;
7874 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007875
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7877 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007878
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007880}
7881#endif /* WEXITSTATUS */
7882
7883#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007884PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007885"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007886Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007887value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007888
7889static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007890posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007891{
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 WAIT_TYPE status;
7893 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007894
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7896 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007897
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007899}
7900#endif /* WTERMSIG */
7901
7902#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007903PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007904"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007905Return the signal that stopped the process that provided\n\
7906the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007907
7908static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007909posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007910{
Victor Stinner8c62be82010-05-06 00:08:46 +00007911 WAIT_TYPE status;
7912 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007913
Victor Stinner8c62be82010-05-06 00:08:46 +00007914 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7915 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007916
Victor Stinner8c62be82010-05-06 00:08:46 +00007917 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007918}
7919#endif /* WSTOPSIG */
7920
7921#endif /* HAVE_SYS_WAIT_H */
7922
7923
Thomas Wouters477c8d52006-05-27 19:21:47 +00007924#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007925#ifdef _SCO_DS
7926/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7927 needed definitions in sys/statvfs.h */
7928#define _SVID3
7929#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007930#include <sys/statvfs.h>
7931
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007932static PyObject*
7933_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7935 if (v == NULL)
7936 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007937
7938#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7940 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7941 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7942 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7943 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7944 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7945 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7946 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7947 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7948 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007949#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007950 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7951 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7952 PyStructSequence_SET_ITEM(v, 2,
7953 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7954 PyStructSequence_SET_ITEM(v, 3,
7955 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7956 PyStructSequence_SET_ITEM(v, 4,
7957 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7958 PyStructSequence_SET_ITEM(v, 5,
7959 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7960 PyStructSequence_SET_ITEM(v, 6,
7961 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7962 PyStructSequence_SET_ITEM(v, 7,
7963 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7964 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7965 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007966#endif
7967
Victor Stinner8c62be82010-05-06 00:08:46 +00007968 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007969}
7970
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007971PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007972"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007973Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007974
7975static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007976posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007977{
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 int fd, res;
7979 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007980
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7982 return NULL;
7983 Py_BEGIN_ALLOW_THREADS
7984 res = fstatvfs(fd, &st);
7985 Py_END_ALLOW_THREADS
7986 if (res != 0)
7987 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007988
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007990}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007991#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007992
7993
Thomas Wouters477c8d52006-05-27 19:21:47 +00007994#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007995#include <sys/statvfs.h>
7996
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007997PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007998"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007999Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008000
8001static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008002posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008003{
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 char *path;
8005 int res;
8006 struct statvfs st;
8007 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
8008 return NULL;
8009 Py_BEGIN_ALLOW_THREADS
8010 res = statvfs(path, &st);
8011 Py_END_ALLOW_THREADS
8012 if (res != 0)
8013 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008014
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008016}
8017#endif /* HAVE_STATVFS */
8018
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008019#ifdef MS_WINDOWS
8020PyDoc_STRVAR(win32__getdiskusage__doc__,
8021"_getdiskusage(path) -> (total, free)\n\n\
8022Return disk usage statistics about the given path as (total, free) tuple.");
8023
8024static PyObject *
8025win32__getdiskusage(PyObject *self, PyObject *args)
8026{
8027 BOOL retval;
8028 ULARGE_INTEGER _, total, free;
8029 LPCTSTR path;
8030
8031 if (! PyArg_ParseTuple(args, "s", &path))
8032 return NULL;
8033
8034 Py_BEGIN_ALLOW_THREADS
8035 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8036 Py_END_ALLOW_THREADS
8037 if (retval == 0)
8038 return PyErr_SetFromWindowsErr(0);
8039
8040 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8041}
8042#endif
8043
8044
Fred Drakec9680921999-12-13 16:37:25 +00008045/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8046 * It maps strings representing configuration variable names to
8047 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008048 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008049 * rarely-used constants. There are three separate tables that use
8050 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008051 *
8052 * This code is always included, even if none of the interfaces that
8053 * need it are included. The #if hackery needed to avoid it would be
8054 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008055 */
8056struct constdef {
8057 char *name;
8058 long value;
8059};
8060
Fred Drake12c6e2d1999-12-14 21:25:03 +00008061static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008062conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008063 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008064{
Christian Heimes217cfd12007-12-02 14:31:20 +00008065 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008066 *valuep = PyLong_AS_LONG(arg);
8067 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008068 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008069 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008070 /* look up the value in the table using a binary search */
8071 size_t lo = 0;
8072 size_t mid;
8073 size_t hi = tablesize;
8074 int cmp;
8075 const char *confname;
8076 if (!PyUnicode_Check(arg)) {
8077 PyErr_SetString(PyExc_TypeError,
8078 "configuration names must be strings or integers");
8079 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008080 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008081 confname = _PyUnicode_AsString(arg);
8082 if (confname == NULL)
8083 return 0;
8084 while (lo < hi) {
8085 mid = (lo + hi) / 2;
8086 cmp = strcmp(confname, table[mid].name);
8087 if (cmp < 0)
8088 hi = mid;
8089 else if (cmp > 0)
8090 lo = mid + 1;
8091 else {
8092 *valuep = table[mid].value;
8093 return 1;
8094 }
8095 }
8096 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8097 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008098 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008099}
8100
8101
8102#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8103static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008104#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008105 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008106#endif
8107#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008108 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008109#endif
Fred Drakec9680921999-12-13 16:37:25 +00008110#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008111 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008114 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008117 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
8131#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008151#endif
8152#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008153 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008154#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008155#ifdef _PC_ACL_ENABLED
8156 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8157#endif
8158#ifdef _PC_MIN_HOLE_SIZE
8159 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8160#endif
8161#ifdef _PC_ALLOC_SIZE_MIN
8162 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8163#endif
8164#ifdef _PC_REC_INCR_XFER_SIZE
8165 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8166#endif
8167#ifdef _PC_REC_MAX_XFER_SIZE
8168 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8169#endif
8170#ifdef _PC_REC_MIN_XFER_SIZE
8171 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8172#endif
8173#ifdef _PC_REC_XFER_ALIGN
8174 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8175#endif
8176#ifdef _PC_SYMLINK_MAX
8177 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8178#endif
8179#ifdef _PC_XATTR_ENABLED
8180 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8181#endif
8182#ifdef _PC_XATTR_EXISTS
8183 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8184#endif
8185#ifdef _PC_TIMESTAMP_RESOLUTION
8186 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8187#endif
Fred Drakec9680921999-12-13 16:37:25 +00008188};
8189
Fred Drakec9680921999-12-13 16:37:25 +00008190static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008191conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008192{
8193 return conv_confname(arg, valuep, posix_constants_pathconf,
8194 sizeof(posix_constants_pathconf)
8195 / sizeof(struct constdef));
8196}
8197#endif
8198
8199#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008200PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008201"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008202Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008203If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008204
8205static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008206posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008207{
8208 PyObject *result = NULL;
8209 int name, fd;
8210
Fred Drake12c6e2d1999-12-14 21:25:03 +00008211 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8212 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008213 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008214
Stefan Krah0e803b32010-11-26 16:16:47 +00008215 errno = 0;
8216 limit = fpathconf(fd, name);
8217 if (limit == -1 && errno != 0)
8218 posix_error();
8219 else
8220 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008221 }
8222 return result;
8223}
8224#endif
8225
8226
8227#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008228PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008229"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008230Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008231If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008232
8233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008234posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008235{
8236 PyObject *result = NULL;
8237 int name;
8238 char *path;
8239
8240 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8241 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008242 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008243
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 errno = 0;
8245 limit = pathconf(path, name);
8246 if (limit == -1 && errno != 0) {
8247 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008248 /* could be a path or name problem */
8249 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008250 else
Stefan Krah99439262010-11-26 12:58:05 +00008251 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 }
8253 else
8254 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008255 }
8256 return result;
8257}
8258#endif
8259
8260#ifdef HAVE_CONFSTR
8261static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008262#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008263 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008264#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008265#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008266 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008267#endif
8268#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008270#endif
Fred Draked86ed291999-12-15 15:34:33 +00008271#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008273#endif
8274#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008276#endif
8277#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008279#endif
8280#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008282#endif
Fred Drakec9680921999-12-13 16:37:25 +00008283#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008284 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008285#endif
8286#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008288#endif
8289#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008291#endif
8292#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008294#endif
8295#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008297#endif
8298#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008300#endif
8301#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008303#endif
8304#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008306#endif
Fred Draked86ed291999-12-15 15:34:33 +00008307#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008308 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008309#endif
Fred Drakec9680921999-12-13 16:37:25 +00008310#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008312#endif
Fred Draked86ed291999-12-15 15:34:33 +00008313#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008315#endif
8316#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008318#endif
8319#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008321#endif
8322#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008324#endif
Fred Drakec9680921999-12-13 16:37:25 +00008325#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008327#endif
8328#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008329 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008330#endif
8331#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008332 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008333#endif
8334#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008336#endif
8337#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008338 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008339#endif
8340#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008341 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008342#endif
8343#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008344 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008345#endif
8346#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008347 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008348#endif
8349#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008350 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008351#endif
8352#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008353 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008354#endif
8355#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008357#endif
8358#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008359 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008360#endif
8361#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008362 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008363#endif
8364#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008365 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008366#endif
8367#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008369#endif
8370#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008372#endif
Fred Draked86ed291999-12-15 15:34:33 +00008373#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008374 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008375#endif
8376#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008378#endif
8379#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008380 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008381#endif
8382#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008383 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008384#endif
8385#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008386 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008387#endif
8388#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008389 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008390#endif
8391#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008392 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008393#endif
8394#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008395 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008396#endif
8397#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008398 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008399#endif
8400#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008401 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008402#endif
8403#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008405#endif
8406#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008407 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008408#endif
8409#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008410 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008411#endif
Fred Drakec9680921999-12-13 16:37:25 +00008412};
8413
8414static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008415conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008416{
8417 return conv_confname(arg, valuep, posix_constants_confstr,
8418 sizeof(posix_constants_confstr)
8419 / sizeof(struct constdef));
8420}
8421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008422PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008423"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008424Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008425
8426static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008427posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008428{
8429 PyObject *result = NULL;
8430 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008431 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008432 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008433
Victor Stinnercb043522010-09-10 23:49:04 +00008434 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8435 return NULL;
8436
8437 errno = 0;
8438 len = confstr(name, buffer, sizeof(buffer));
8439 if (len == 0) {
8440 if (errno) {
8441 posix_error();
8442 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008443 }
8444 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008445 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008446 }
8447 }
Victor Stinnercb043522010-09-10 23:49:04 +00008448
8449 if ((unsigned int)len >= sizeof(buffer)) {
8450 char *buf = PyMem_Malloc(len);
8451 if (buf == NULL)
8452 return PyErr_NoMemory();
8453 confstr(name, buf, len);
8454 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8455 PyMem_Free(buf);
8456 }
8457 else
8458 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008459 return result;
8460}
8461#endif
8462
8463
8464#ifdef HAVE_SYSCONF
8465static struct constdef posix_constants_sysconf[] = {
8466#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008468#endif
8469#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008471#endif
8472#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008474#endif
8475#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008477#endif
8478#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008479 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008480#endif
8481#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008483#endif
8484#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008486#endif
8487#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008488 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008489#endif
8490#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008491 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008492#endif
8493#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008494 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008495#endif
Fred Draked86ed291999-12-15 15:34:33 +00008496#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008497 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008498#endif
8499#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008501#endif
Fred Drakec9680921999-12-13 16:37:25 +00008502#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008504#endif
Fred Drakec9680921999-12-13 16:37:25 +00008505#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008507#endif
8508#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008510#endif
8511#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008512 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008513#endif
8514#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008516#endif
8517#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008519#endif
Fred Draked86ed291999-12-15 15:34:33 +00008520#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008522#endif
Fred Drakec9680921999-12-13 16:37:25 +00008523#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008524 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008525#endif
8526#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008527 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008528#endif
8529#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008530 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008531#endif
8532#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008534#endif
8535#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008537#endif
Fred Draked86ed291999-12-15 15:34:33 +00008538#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008540#endif
Fred Drakec9680921999-12-13 16:37:25 +00008541#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008542 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008543#endif
8544#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008545 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008546#endif
8547#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008548 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008549#endif
8550#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008552#endif
8553#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008554 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008555#endif
8556#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008557 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008558#endif
8559#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008560 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008561#endif
8562#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008563 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008564#endif
8565#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008566 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008567#endif
8568#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008569 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008570#endif
8571#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008572 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008573#endif
8574#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008575 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008576#endif
8577#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008578 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008579#endif
8580#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008581 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008582#endif
8583#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008584 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008585#endif
8586#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008587 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008588#endif
8589#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008590 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008591#endif
8592#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008593 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008594#endif
8595#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008596 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008597#endif
8598#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008600#endif
8601#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008602 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008603#endif
8604#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008605 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008606#endif
8607#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008608 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008609#endif
Fred Draked86ed291999-12-15 15:34:33 +00008610#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008611 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008612#endif
Fred Drakec9680921999-12-13 16:37:25 +00008613#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008614 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008615#endif
8616#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008617 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008618#endif
8619#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008620 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008621#endif
Fred Draked86ed291999-12-15 15:34:33 +00008622#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008623 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008624#endif
Fred Drakec9680921999-12-13 16:37:25 +00008625#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008626 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008627#endif
Fred Draked86ed291999-12-15 15:34:33 +00008628#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008630#endif
8631#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008632 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008633#endif
Fred Drakec9680921999-12-13 16:37:25 +00008634#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008636#endif
8637#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008638 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008639#endif
8640#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008641 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008642#endif
8643#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008644 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008645#endif
Fred Draked86ed291999-12-15 15:34:33 +00008646#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008647 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008648#endif
Fred Drakec9680921999-12-13 16:37:25 +00008649#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008651#endif
8652#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008653 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008654#endif
8655#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008657#endif
8658#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008660#endif
8661#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008662 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008663#endif
8664#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008665 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008666#endif
8667#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008668 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008669#endif
Fred Draked86ed291999-12-15 15:34:33 +00008670#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008672#endif
Fred Drakec9680921999-12-13 16:37:25 +00008673#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008675#endif
8676#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008677 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008678#endif
Fred Draked86ed291999-12-15 15:34:33 +00008679#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008680 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008681#endif
Fred Drakec9680921999-12-13 16:37:25 +00008682#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008683 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008684#endif
8685#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008686 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008687#endif
8688#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008689 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008690#endif
8691#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008692 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008693#endif
8694#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008696#endif
8697#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008698 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008699#endif
8700#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008702#endif
8703#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008705#endif
8706#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008707 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008708#endif
Fred Draked86ed291999-12-15 15:34:33 +00008709#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008710 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008711#endif
8712#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008713 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008714#endif
Fred Drakec9680921999-12-13 16:37:25 +00008715#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008717#endif
8718#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008720#endif
8721#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008723#endif
8724#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008726#endif
8727#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008729#endif
8730#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008731 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008732#endif
8733#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008735#endif
8736#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008738#endif
8739#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008741#endif
8742#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008744#endif
8745#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008746 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008747#endif
8748#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008749 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008750#endif
8751#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008752 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008753#endif
8754#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008756#endif
8757#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008759#endif
8760#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008762#endif
8763#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008764 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008765#endif
8766#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008768#endif
8769#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008770 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008771#endif
8772#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008774#endif
8775#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008777#endif
8778#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008779 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008780#endif
8781#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008782 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008783#endif
8784#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008785 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008786#endif
8787#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008789#endif
8790#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008791 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008792#endif
8793#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008795#endif
8796#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008797 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008798#endif
8799#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008801#endif
8802#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008804#endif
8805#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008807#endif
8808#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008810#endif
8811#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008813#endif
8814#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008816#endif
8817#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008818 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008819#endif
Fred Draked86ed291999-12-15 15:34:33 +00008820#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008821 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008822#endif
Fred Drakec9680921999-12-13 16:37:25 +00008823#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008825#endif
8826#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008827 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008828#endif
8829#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008831#endif
8832#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008834#endif
8835#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008836 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008837#endif
8838#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008839 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008840#endif
8841#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008842 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008843#endif
8844#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008845 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008846#endif
8847#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008848 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008849#endif
8850#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008851 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008852#endif
8853#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008854 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008855#endif
8856#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008857 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008858#endif
8859#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008860 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008861#endif
8862#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008863 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008864#endif
8865#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008866 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008867#endif
8868#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008869 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008870#endif
8871#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008872 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008873#endif
8874#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008876#endif
8877#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008878 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008879#endif
8880#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008882#endif
8883#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008884 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008885#endif
8886#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008888#endif
8889#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008890 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008891#endif
8892#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008894#endif
8895#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008896 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008897#endif
8898#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008900#endif
8901#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008903#endif
8904#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008905 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008906#endif
8907#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008908 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008909#endif
8910#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008911 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008912#endif
8913#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008914 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008915#endif
8916#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008917 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008918#endif
8919#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008920 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008921#endif
8922#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008923 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008924#endif
8925#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008926 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008927#endif
8928#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008930#endif
8931#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008932 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008933#endif
8934#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008935 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008936#endif
8937#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008938 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008939#endif
8940#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008941 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008942#endif
8943#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008944 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008945#endif
8946#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008948#endif
8949#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008950 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008951#endif
8952#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008953 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008954#endif
8955#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008956 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008957#endif
8958};
8959
8960static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008961conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008962{
8963 return conv_confname(arg, valuep, posix_constants_sysconf,
8964 sizeof(posix_constants_sysconf)
8965 / sizeof(struct constdef));
8966}
8967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008968PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008969"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008970Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008971
8972static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008973posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008974{
8975 PyObject *result = NULL;
8976 int name;
8977
8978 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8979 int value;
8980
8981 errno = 0;
8982 value = sysconf(name);
8983 if (value == -1 && errno != 0)
8984 posix_error();
8985 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008986 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008987 }
8988 return result;
8989}
8990#endif
8991
8992
Fred Drakebec628d1999-12-15 18:31:10 +00008993/* This code is used to ensure that the tables of configuration value names
8994 * are in sorted order as required by conv_confname(), and also to build the
8995 * the exported dictionaries that are used to publish information about the
8996 * names available on the host platform.
8997 *
8998 * Sorting the table at runtime ensures that the table is properly ordered
8999 * when used, even for platforms we're not able to test on. It also makes
9000 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009001 */
Fred Drakebec628d1999-12-15 18:31:10 +00009002
9003static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009004cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009005{
9006 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009008 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009009 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009010
9011 return strcmp(c1->name, c2->name);
9012}
9013
9014static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009015setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009016 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009017{
Fred Drakebec628d1999-12-15 18:31:10 +00009018 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009019 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009020
9021 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9022 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009023 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009024 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009025
Barry Warsaw3155db32000-04-13 15:20:40 +00009026 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009027 PyObject *o = PyLong_FromLong(table[i].value);
9028 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9029 Py_XDECREF(o);
9030 Py_DECREF(d);
9031 return -1;
9032 }
9033 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009034 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009035 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009036}
9037
Fred Drakebec628d1999-12-15 18:31:10 +00009038/* Return -1 on failure, 0 on success. */
9039static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009040setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009041{
9042#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009043 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009044 sizeof(posix_constants_pathconf)
9045 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009046 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009047 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009048#endif
9049#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009050 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009051 sizeof(posix_constants_confstr)
9052 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009053 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009054 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009055#endif
9056#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009057 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009058 sizeof(posix_constants_sysconf)
9059 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009060 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009061 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009062#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009063 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009064}
Fred Draked86ed291999-12-15 15:34:33 +00009065
9066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009067PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009068"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009069Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009070in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009071
9072static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009073posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009074{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009075 abort();
9076 /*NOTREACHED*/
9077 Py_FatalError("abort() called from Python code didn't abort!");
9078 return NULL;
9079}
Fred Drakebec628d1999-12-15 18:31:10 +00009080
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009081#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009082PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009083"startfile(filepath [, operation]) - Start a file with its associated\n\
9084application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009085\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009086When \"operation\" is not specified or \"open\", this acts like\n\
9087double-clicking the file in Explorer, or giving the file name as an\n\
9088argument to the DOS \"start\" command: the file is opened with whatever\n\
9089application (if any) its extension is associated.\n\
9090When another \"operation\" is given, it specifies what should be done with\n\
9091the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009092\n\
9093startfile returns as soon as the associated application is launched.\n\
9094There is no option to wait for the application to close, and no way\n\
9095to retrieve the application's exit status.\n\
9096\n\
9097The filepath is relative to the current directory. If you want to use\n\
9098an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009099the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009100
9101static PyObject *
9102win32_startfile(PyObject *self, PyObject *args)
9103{
Victor Stinner8c62be82010-05-06 00:08:46 +00009104 PyObject *ofilepath;
9105 char *filepath;
9106 char *operation = NULL;
9107 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009108
Victor Stinner8c62be82010-05-06 00:08:46 +00009109 PyObject *unipath, *woperation = NULL;
9110 if (!PyArg_ParseTuple(args, "U|s:startfile",
9111 &unipath, &operation)) {
9112 PyErr_Clear();
9113 goto normal;
9114 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009115
Victor Stinner8c62be82010-05-06 00:08:46 +00009116 if (operation) {
9117 woperation = PyUnicode_DecodeASCII(operation,
9118 strlen(operation), NULL);
9119 if (!woperation) {
9120 PyErr_Clear();
9121 operation = NULL;
9122 goto normal;
9123 }
9124 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009125
Victor Stinner8c62be82010-05-06 00:08:46 +00009126 Py_BEGIN_ALLOW_THREADS
9127 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9128 PyUnicode_AS_UNICODE(unipath),
9129 NULL, NULL, SW_SHOWNORMAL);
9130 Py_END_ALLOW_THREADS
9131
9132 Py_XDECREF(woperation);
9133 if (rc <= (HINSTANCE)32) {
9134 PyObject *errval = win32_error_unicode("startfile",
9135 PyUnicode_AS_UNICODE(unipath));
9136 return errval;
9137 }
9138 Py_INCREF(Py_None);
9139 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009140
9141normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009142 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9143 PyUnicode_FSConverter, &ofilepath,
9144 &operation))
9145 return NULL;
9146 filepath = PyBytes_AsString(ofilepath);
9147 Py_BEGIN_ALLOW_THREADS
9148 rc = ShellExecute((HWND)0, operation, filepath,
9149 NULL, NULL, SW_SHOWNORMAL);
9150 Py_END_ALLOW_THREADS
9151 if (rc <= (HINSTANCE)32) {
9152 PyObject *errval = win32_error("startfile", filepath);
9153 Py_DECREF(ofilepath);
9154 return errval;
9155 }
9156 Py_DECREF(ofilepath);
9157 Py_INCREF(Py_None);
9158 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009159}
9160#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009161
Martin v. Löwis438b5342002-12-27 10:16:42 +00009162#ifdef HAVE_GETLOADAVG
9163PyDoc_STRVAR(posix_getloadavg__doc__,
9164"getloadavg() -> (float, float, float)\n\n\
9165Return the number of processes in the system run queue averaged over\n\
9166the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9167was unobtainable");
9168
9169static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009170posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009171{
9172 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009173 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009174 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9175 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009176 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009177 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009178}
9179#endif
9180
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009181#ifdef MS_WINDOWS
9182
9183PyDoc_STRVAR(win32_urandom__doc__,
9184"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009185Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009186
9187typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9188 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9189 DWORD dwFlags );
9190typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9191 BYTE *pbBuffer );
9192
9193static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009194/* This handle is never explicitly released. Instead, the operating
9195 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009196static HCRYPTPROV hCryptProv = 0;
9197
Tim Peters4ad82172004-08-30 17:02:04 +00009198static PyObject*
9199win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009200{
Victor Stinner8c62be82010-05-06 00:08:46 +00009201 int howMany;
9202 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009203
Victor Stinner8c62be82010-05-06 00:08:46 +00009204 /* Read arguments */
9205 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9206 return NULL;
9207 if (howMany < 0)
9208 return PyErr_Format(PyExc_ValueError,
9209 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009210
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 if (hCryptProv == 0) {
9212 HINSTANCE hAdvAPI32 = NULL;
9213 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009214
Victor Stinner8c62be82010-05-06 00:08:46 +00009215 /* Obtain handle to the DLL containing CryptoAPI
9216 This should not fail */
9217 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9218 if(hAdvAPI32 == NULL)
9219 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009220
Victor Stinner8c62be82010-05-06 00:08:46 +00009221 /* Obtain pointers to the CryptoAPI functions
9222 This will fail on some early versions of Win95 */
9223 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9224 hAdvAPI32,
9225 "CryptAcquireContextA");
9226 if (pCryptAcquireContext == NULL)
9227 return PyErr_Format(PyExc_NotImplementedError,
9228 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009229
Victor Stinner8c62be82010-05-06 00:08:46 +00009230 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9231 hAdvAPI32, "CryptGenRandom");
9232 if (pCryptGenRandom == NULL)
9233 return PyErr_Format(PyExc_NotImplementedError,
9234 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009235
Victor Stinner8c62be82010-05-06 00:08:46 +00009236 /* Acquire context */
9237 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9238 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9239 return win32_error("CryptAcquireContext", NULL);
9240 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009241
Victor Stinner8c62be82010-05-06 00:08:46 +00009242 /* Allocate bytes */
9243 result = PyBytes_FromStringAndSize(NULL, howMany);
9244 if (result != NULL) {
9245 /* Get random data */
9246 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9247 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9248 PyBytes_AS_STRING(result))) {
9249 Py_DECREF(result);
9250 return win32_error("CryptGenRandom", NULL);
9251 }
9252 }
9253 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009254}
9255#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009256
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009257PyDoc_STRVAR(device_encoding__doc__,
9258"device_encoding(fd) -> str\n\n\
9259Return a string describing the encoding of the device\n\
9260if the output is a terminal; else return None.");
9261
9262static PyObject *
9263device_encoding(PyObject *self, PyObject *args)
9264{
Victor Stinner8c62be82010-05-06 00:08:46 +00009265 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009266#if defined(MS_WINDOWS) || defined(MS_WIN64)
9267 UINT cp;
9268#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009269 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9270 return NULL;
9271 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9272 Py_INCREF(Py_None);
9273 return Py_None;
9274 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009275#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009276 if (fd == 0)
9277 cp = GetConsoleCP();
9278 else if (fd == 1 || fd == 2)
9279 cp = GetConsoleOutputCP();
9280 else
9281 cp = 0;
9282 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9283 has no console */
9284 if (cp != 0)
9285 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009286#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009287 {
9288 char *codeset = nl_langinfo(CODESET);
9289 if (codeset != NULL && codeset[0] != 0)
9290 return PyUnicode_FromString(codeset);
9291 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009292#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 Py_INCREF(Py_None);
9294 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009295}
9296
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009297#ifdef __VMS
9298/* Use openssl random routine */
9299#include <openssl/rand.h>
9300PyDoc_STRVAR(vms_urandom__doc__,
9301"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009302Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009303
9304static PyObject*
9305vms_urandom(PyObject *self, PyObject *args)
9306{
Victor Stinner8c62be82010-05-06 00:08:46 +00009307 int howMany;
9308 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009309
Victor Stinner8c62be82010-05-06 00:08:46 +00009310 /* Read arguments */
9311 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9312 return NULL;
9313 if (howMany < 0)
9314 return PyErr_Format(PyExc_ValueError,
9315 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009316
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 /* Allocate bytes */
9318 result = PyBytes_FromStringAndSize(NULL, howMany);
9319 if (result != NULL) {
9320 /* Get random data */
9321 if (RAND_pseudo_bytes((unsigned char*)
9322 PyBytes_AS_STRING(result),
9323 howMany) < 0) {
9324 Py_DECREF(result);
9325 return PyErr_Format(PyExc_ValueError,
9326 "RAND_pseudo_bytes");
9327 }
9328 }
9329 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009330}
9331#endif
9332
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009333#ifdef HAVE_SETRESUID
9334PyDoc_STRVAR(posix_setresuid__doc__,
9335"setresuid(ruid, euid, suid)\n\n\
9336Set the current process's real, effective, and saved user ids.");
9337
9338static PyObject*
9339posix_setresuid (PyObject *self, PyObject *args)
9340{
Victor Stinner8c62be82010-05-06 00:08:46 +00009341 /* We assume uid_t is no larger than a long. */
9342 long ruid, euid, suid;
9343 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9344 return NULL;
9345 if (setresuid(ruid, euid, suid) < 0)
9346 return posix_error();
9347 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009348}
9349#endif
9350
9351#ifdef HAVE_SETRESGID
9352PyDoc_STRVAR(posix_setresgid__doc__,
9353"setresgid(rgid, egid, sgid)\n\n\
9354Set the current process's real, effective, and saved group ids.");
9355
9356static PyObject*
9357posix_setresgid (PyObject *self, PyObject *args)
9358{
Victor Stinner8c62be82010-05-06 00:08:46 +00009359 /* We assume uid_t is no larger than a long. */
9360 long rgid, egid, sgid;
9361 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9362 return NULL;
9363 if (setresgid(rgid, egid, sgid) < 0)
9364 return posix_error();
9365 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009366}
9367#endif
9368
9369#ifdef HAVE_GETRESUID
9370PyDoc_STRVAR(posix_getresuid__doc__,
9371"getresuid() -> (ruid, euid, suid)\n\n\
9372Get tuple of the current process's real, effective, and saved user ids.");
9373
9374static PyObject*
9375posix_getresuid (PyObject *self, PyObject *noargs)
9376{
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 uid_t ruid, euid, suid;
9378 long l_ruid, l_euid, l_suid;
9379 if (getresuid(&ruid, &euid, &suid) < 0)
9380 return posix_error();
9381 /* Force the values into long's as we don't know the size of uid_t. */
9382 l_ruid = ruid;
9383 l_euid = euid;
9384 l_suid = suid;
9385 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009386}
9387#endif
9388
9389#ifdef HAVE_GETRESGID
9390PyDoc_STRVAR(posix_getresgid__doc__,
9391"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009392Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009393
9394static PyObject*
9395posix_getresgid (PyObject *self, PyObject *noargs)
9396{
Victor Stinner8c62be82010-05-06 00:08:46 +00009397 uid_t rgid, egid, sgid;
9398 long l_rgid, l_egid, l_sgid;
9399 if (getresgid(&rgid, &egid, &sgid) < 0)
9400 return posix_error();
9401 /* Force the values into long's as we don't know the size of uid_t. */
9402 l_rgid = rgid;
9403 l_egid = egid;
9404 l_sgid = sgid;
9405 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009406}
9407#endif
9408
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009409/* Posix *at family of functions:
9410 faccessat, fchmodat, fchownat, fstatat, futimesat,
9411 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9412 unlinkat, utimensat, mkfifoat */
9413
9414#ifdef HAVE_FACCESSAT
9415PyDoc_STRVAR(posix_faccessat__doc__,
9416"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9417Like access() but if path is relative, it is taken as relative to dirfd.\n\
9418flags is optional and can be constructed by ORing together zero or more\n\
9419of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9420If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9421is interpreted relative to the current working directory.");
9422
9423static PyObject *
9424posix_faccessat(PyObject *self, PyObject *args)
9425{
9426 PyObject *opath;
9427 char *path;
9428 int mode;
9429 int res;
9430 int dirfd, flags = 0;
9431 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9432 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9433 return NULL;
9434 path = PyBytes_AsString(opath);
9435 Py_BEGIN_ALLOW_THREADS
9436 res = faccessat(dirfd, path, mode, flags);
9437 Py_END_ALLOW_THREADS
9438 Py_DECREF(opath);
9439 return PyBool_FromLong(res == 0);
9440}
9441#endif
9442
9443#ifdef HAVE_FCHMODAT
9444PyDoc_STRVAR(posix_fchmodat__doc__,
9445"fchmodat(dirfd, path, mode, flags=0)\n\n\
9446Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9447flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9448If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9449is interpreted relative to the current working directory.");
9450
9451static PyObject *
9452posix_fchmodat(PyObject *self, PyObject *args)
9453{
9454 int dirfd, mode, res;
9455 int flags = 0;
9456 PyObject *opath;
9457 char *path;
9458
9459 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9460 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9461 return NULL;
9462
9463 path = PyBytes_AsString(opath);
9464
9465 Py_BEGIN_ALLOW_THREADS
9466 res = fchmodat(dirfd, path, mode, flags);
9467 Py_END_ALLOW_THREADS
9468 Py_DECREF(opath);
9469 if (res < 0)
9470 return posix_error();
9471 Py_RETURN_NONE;
9472}
9473#endif /* HAVE_FCHMODAT */
9474
9475#ifdef HAVE_FCHOWNAT
9476PyDoc_STRVAR(posix_fchownat__doc__,
9477"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9478Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9479flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9480If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9481is interpreted relative to the current working directory.");
9482
9483static PyObject *
9484posix_fchownat(PyObject *self, PyObject *args)
9485{
9486 PyObject *opath;
9487 int dirfd, res;
9488 long uid, gid;
9489 int flags = 0;
9490 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009491
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009492 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9493 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9494 return NULL;
9495
9496 path = PyBytes_AsString(opath);
9497
9498 Py_BEGIN_ALLOW_THREADS
9499 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9500 Py_END_ALLOW_THREADS
9501 Py_DECREF(opath);
9502 if (res < 0)
9503 return posix_error();
9504 Py_RETURN_NONE;
9505}
9506#endif /* HAVE_FCHOWNAT */
9507
9508#ifdef HAVE_FSTATAT
9509PyDoc_STRVAR(posix_fstatat__doc__,
9510"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9511Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9512flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9513If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9514is interpreted relative to the current working directory.");
9515
9516static PyObject *
9517posix_fstatat(PyObject *self, PyObject *args)
9518{
9519 PyObject *opath;
9520 char *path;
9521 STRUCT_STAT st;
9522 int dirfd, res, flags = 0;
9523
9524 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9525 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9526 return NULL;
9527 path = PyBytes_AsString(opath);
9528
9529 Py_BEGIN_ALLOW_THREADS
9530 res = fstatat(dirfd, path, &st, flags);
9531 Py_END_ALLOW_THREADS
9532 Py_DECREF(opath);
9533 if (res != 0)
9534 return posix_error();
9535
9536 return _pystat_fromstructstat(&st);
9537}
9538#endif
9539
9540#ifdef HAVE_FUTIMESAT
9541PyDoc_STRVAR(posix_futimesat__doc__,
9542"futimesat(dirfd, path, (atime, mtime))\n\
9543futimesat(dirfd, path, None)\n\n\
9544Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9545If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9546is interpreted relative to the current working directory.");
9547
9548static PyObject *
9549posix_futimesat(PyObject *self, PyObject *args)
9550{
9551 PyObject *opath;
9552 char *path;
9553 int res, dirfd;
9554 PyObject* arg;
9555
9556 struct timeval buf[2];
9557
9558 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9559 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9560 return NULL;
9561 path = PyBytes_AsString(opath);
9562 if (arg == Py_None) {
9563 /* optional time values not given */
9564 Py_BEGIN_ALLOW_THREADS
9565 res = futimesat(dirfd, path, NULL);
9566 Py_END_ALLOW_THREADS
9567 }
9568 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9569 PyErr_SetString(PyExc_TypeError,
9570 "futimesat() arg 3 must be a tuple (atime, mtime)");
9571 Py_DECREF(opath);
9572 return NULL;
9573 }
9574 else {
9575 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009576 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009577 Py_DECREF(opath);
9578 return NULL;
9579 }
9580 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009581 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009582 Py_DECREF(opath);
9583 return NULL;
9584 }
9585 Py_BEGIN_ALLOW_THREADS
9586 res = futimesat(dirfd, path, buf);
9587 Py_END_ALLOW_THREADS
9588 }
9589 Py_DECREF(opath);
9590 if (res < 0) {
9591 return posix_error();
9592 }
9593 Py_RETURN_NONE;
9594}
9595#endif
9596
9597#ifdef HAVE_LINKAT
9598PyDoc_STRVAR(posix_linkat__doc__,
9599"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9600Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9601and if dstpath is relative, it is taken as relative to dstfd.\n\
9602flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9603If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9604srcpath is interpreted relative to the current working directory. This\n\
9605also applies for dstpath.");
9606
9607static PyObject *
9608posix_linkat(PyObject *self, PyObject *args)
9609{
9610 PyObject *osrc, *odst;
9611 char *src, *dst;
9612 int res, srcfd, dstfd;
9613 int flags = 0;
9614
9615 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9616 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9617 return NULL;
9618 src = PyBytes_AsString(osrc);
9619 dst = PyBytes_AsString(odst);
9620 Py_BEGIN_ALLOW_THREADS
9621 res = linkat(srcfd, src, dstfd, dst, flags);
9622 Py_END_ALLOW_THREADS
9623 Py_DECREF(osrc);
9624 Py_DECREF(odst);
9625 if (res < 0)
9626 return posix_error();
9627 Py_RETURN_NONE;
9628}
9629#endif /* HAVE_LINKAT */
9630
9631#ifdef HAVE_MKDIRAT
9632PyDoc_STRVAR(posix_mkdirat__doc__,
9633"mkdirat(dirfd, path, mode=0o777)\n\n\
9634Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9635If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9636is interpreted relative to the current working directory.");
9637
9638static PyObject *
9639posix_mkdirat(PyObject *self, PyObject *args)
9640{
9641 int res, dirfd;
9642 PyObject *opath;
9643 char *path;
9644 int mode = 0777;
9645
9646 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9647 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9648 return NULL;
9649 path = PyBytes_AsString(opath);
9650 Py_BEGIN_ALLOW_THREADS
9651 res = mkdirat(dirfd, path, mode);
9652 Py_END_ALLOW_THREADS
9653 Py_DECREF(opath);
9654 if (res < 0)
9655 return posix_error();
9656 Py_RETURN_NONE;
9657}
9658#endif
9659
9660#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9661PyDoc_STRVAR(posix_mknodat__doc__,
9662"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9663Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9664If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9665is interpreted relative to the current working directory.");
9666
9667static PyObject *
9668posix_mknodat(PyObject *self, PyObject *args)
9669{
9670 PyObject *opath;
9671 char *filename;
9672 int mode = 0600;
9673 int device = 0;
9674 int res, dirfd;
9675 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9676 PyUnicode_FSConverter, &opath, &mode, &device))
9677 return NULL;
9678 filename = PyBytes_AS_STRING(opath);
9679 Py_BEGIN_ALLOW_THREADS
9680 res = mknodat(dirfd, filename, mode, device);
9681 Py_END_ALLOW_THREADS
9682 Py_DECREF(opath);
9683 if (res < 0)
9684 return posix_error();
9685 Py_RETURN_NONE;
9686}
9687#endif
9688
9689#ifdef HAVE_OPENAT
9690PyDoc_STRVAR(posix_openat__doc__,
9691"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9692Like open() but if path is relative, it is taken as relative to dirfd.\n\
9693If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9694is interpreted relative to the current working directory.");
9695
9696static PyObject *
9697posix_openat(PyObject *self, PyObject *args)
9698{
9699 PyObject *ofile;
9700 char *file;
9701 int flag, dirfd, fd;
9702 int mode = 0777;
9703
9704 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9705 &dirfd, PyUnicode_FSConverter, &ofile,
9706 &flag, &mode))
9707 return NULL;
9708 file = PyBytes_AsString(ofile);
9709 Py_BEGIN_ALLOW_THREADS
9710 fd = openat(dirfd, file, flag, mode);
9711 Py_END_ALLOW_THREADS
9712 Py_DECREF(ofile);
9713 if (fd < 0)
9714 return posix_error();
9715 return PyLong_FromLong((long)fd);
9716}
9717#endif
9718
9719#ifdef HAVE_READLINKAT
9720PyDoc_STRVAR(posix_readlinkat__doc__,
9721"readlinkat(dirfd, path) -> path\n\n\
9722Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9723If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9724is interpreted relative to the current working directory.");
9725
9726static PyObject *
9727posix_readlinkat(PyObject *self, PyObject *args)
9728{
9729 PyObject *v, *opath;
9730 char buf[MAXPATHLEN];
9731 char *path;
9732 int n, dirfd;
9733 int arg_is_unicode = 0;
9734
9735 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9736 &dirfd, PyUnicode_FSConverter, &opath))
9737 return NULL;
9738 path = PyBytes_AsString(opath);
9739 v = PySequence_GetItem(args, 1);
9740 if (v == NULL) {
9741 Py_DECREF(opath);
9742 return NULL;
9743 }
9744
9745 if (PyUnicode_Check(v)) {
9746 arg_is_unicode = 1;
9747 }
9748 Py_DECREF(v);
9749
9750 Py_BEGIN_ALLOW_THREADS
9751 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9752 Py_END_ALLOW_THREADS
9753 Py_DECREF(opath);
9754 if (n < 0)
9755 return posix_error();
9756
9757 if (arg_is_unicode)
9758 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9759 else
9760 return PyBytes_FromStringAndSize(buf, n);
9761}
9762#endif /* HAVE_READLINKAT */
9763
9764#ifdef HAVE_RENAMEAT
9765PyDoc_STRVAR(posix_renameat__doc__,
9766"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9767Like rename() but if oldpath is relative, it is taken as relative to\n\
9768olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9769If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9770oldpath is interpreted relative to the current working directory. This\n\
9771also applies for newpath.");
9772
9773static PyObject *
9774posix_renameat(PyObject *self, PyObject *args)
9775{
9776 int res;
9777 PyObject *opathold, *opathnew;
9778 char *opath, *npath;
9779 int oldfd, newfd;
9780
9781 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9782 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9783 return NULL;
9784 opath = PyBytes_AsString(opathold);
9785 npath = PyBytes_AsString(opathnew);
9786 Py_BEGIN_ALLOW_THREADS
9787 res = renameat(oldfd, opath, newfd, npath);
9788 Py_END_ALLOW_THREADS
9789 Py_DECREF(opathold);
9790 Py_DECREF(opathnew);
9791 if (res < 0)
9792 return posix_error();
9793 Py_RETURN_NONE;
9794}
9795#endif
9796
9797#if HAVE_SYMLINKAT
9798PyDoc_STRVAR(posix_symlinkat__doc__,
9799"symlinkat(src, dstfd, dst)\n\n\
9800Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9801If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9802is interpreted relative to the current working directory.");
9803
9804static PyObject *
9805posix_symlinkat(PyObject *self, PyObject *args)
9806{
9807 int res, dstfd;
9808 PyObject *osrc, *odst;
9809 char *src, *dst;
9810
9811 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9812 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9813 return NULL;
9814 src = PyBytes_AsString(osrc);
9815 dst = PyBytes_AsString(odst);
9816 Py_BEGIN_ALLOW_THREADS
9817 res = symlinkat(src, dstfd, dst);
9818 Py_END_ALLOW_THREADS
9819 Py_DECREF(osrc);
9820 Py_DECREF(odst);
9821 if (res < 0)
9822 return posix_error();
9823 Py_RETURN_NONE;
9824}
9825#endif /* HAVE_SYMLINKAT */
9826
9827#ifdef HAVE_UNLINKAT
9828PyDoc_STRVAR(posix_unlinkat__doc__,
9829"unlinkat(dirfd, path, flags=0)\n\n\
9830Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9831flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9832specified, unlinkat() behaves like rmdir().\n\
9833If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9834is interpreted relative to the current working directory.");
9835
9836static PyObject *
9837posix_unlinkat(PyObject *self, PyObject *args)
9838{
9839 int dirfd, res, flags = 0;
9840 PyObject *opath;
9841 char *path;
9842
9843 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9844 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9845 return NULL;
9846 path = PyBytes_AsString(opath);
9847 Py_BEGIN_ALLOW_THREADS
9848 res = unlinkat(dirfd, path, flags);
9849 Py_END_ALLOW_THREADS
9850 Py_DECREF(opath);
9851 if (res < 0)
9852 return posix_error();
9853 Py_RETURN_NONE;
9854}
9855#endif
9856
9857#ifdef HAVE_UTIMENSAT
9858PyDoc_STRVAR(posix_utimensat__doc__,
9859"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9860 (mtime_sec, mtime_nsec), flags)\n\
9861utimensat(dirfd, path, None, None, flags)\n\n\
9862Updates the timestamps of a file with nanosecond precision. If path is\n\
9863relative, it is taken as relative to dirfd.\n\
9864The second form sets atime and mtime to the current time.\n\
9865flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9866If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9867is interpreted relative to the current working directory.\n\
9868If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9869current time.\n\
9870If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9871
9872static PyObject *
9873posix_utimensat(PyObject *self, PyObject *args)
9874{
9875 PyObject *opath;
9876 char *path;
9877 int res, dirfd, flags = 0;
9878 PyObject *atime, *mtime;
9879
9880 struct timespec buf[2];
9881
9882 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9883 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9884 return NULL;
9885 path = PyBytes_AsString(opath);
9886 if (atime == Py_None && mtime == Py_None) {
9887 /* optional time values not given */
9888 Py_BEGIN_ALLOW_THREADS
9889 res = utimensat(dirfd, path, NULL, flags);
9890 Py_END_ALLOW_THREADS
9891 }
9892 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9893 PyErr_SetString(PyExc_TypeError,
9894 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9895 Py_DECREF(opath);
9896 return NULL;
9897 }
9898 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9899 PyErr_SetString(PyExc_TypeError,
9900 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9901 Py_DECREF(opath);
9902 return NULL;
9903 }
9904 else {
9905 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9906 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9907 Py_DECREF(opath);
9908 return NULL;
9909 }
9910 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9911 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9912 Py_DECREF(opath);
9913 return NULL;
9914 }
9915 Py_BEGIN_ALLOW_THREADS
9916 res = utimensat(dirfd, path, buf, flags);
9917 Py_END_ALLOW_THREADS
9918 }
9919 Py_DECREF(opath);
9920 if (res < 0) {
9921 return posix_error();
9922 }
9923 Py_RETURN_NONE;
9924}
9925#endif
9926
9927#ifdef HAVE_MKFIFOAT
9928PyDoc_STRVAR(posix_mkfifoat__doc__,
9929"mkfifoat(dirfd, path, mode=0o666)\n\n\
9930Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9931If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9932is interpreted relative to the current working directory.");
9933
9934static PyObject *
9935posix_mkfifoat(PyObject *self, PyObject *args)
9936{
9937 PyObject *opath;
9938 char *filename;
9939 int mode = 0666;
9940 int res, dirfd;
9941 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9942 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9943 return NULL;
9944 filename = PyBytes_AS_STRING(opath);
9945 Py_BEGIN_ALLOW_THREADS
9946 res = mkfifoat(dirfd, filename, mode);
9947 Py_END_ALLOW_THREADS
9948 Py_DECREF(opath);
9949 if (res < 0)
9950 return posix_error();
9951 Py_RETURN_NONE;
9952}
9953#endif
9954
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009955static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009956 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009957#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009959#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009960 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009961#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009962 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009963#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009965#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009966 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009967#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009968#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009969 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009970#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009971#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009972 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009973#endif /* HAVE_LCHMOD */
9974#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009975 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009976#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009977#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009978 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009979#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009980#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009981 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009982#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009983#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009984 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009985#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009986#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009987 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009988#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009989#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009990 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9991 METH_NOARGS, posix_getcwd__doc__},
9992 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9993 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009994#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009995#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009996 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009997#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009998 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009999#ifdef HAVE_FDOPENDIR
10000 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
10001#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010002 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10003 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010004#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010005 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010006#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010007#ifdef HAVE_GETPRIORITY
10008 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10009#endif /* HAVE_GETPRIORITY */
10010#ifdef HAVE_SETPRIORITY
10011 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10012#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010013#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010014 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010015#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010016#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010017 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010018#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010019 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10020 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10021 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010022 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010023#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010024 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010025#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010026#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010027 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010028 win_symlink__doc__},
10029#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010030#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010031 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010032#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010034#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010035 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010036#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010037 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10038 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10039 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010040#ifdef HAVE_FUTIMES
10041 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10042#endif
10043#ifdef HAVE_LUTIMES
10044 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10045#endif
10046#ifdef HAVE_FUTIMENS
10047 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10048#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010049#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010050 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010051#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010052 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010053#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010054 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10055 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010056#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010057#ifdef HAVE_FEXECVE
10058 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10059#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010060#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010061 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10062 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010063#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010064 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10065 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010066#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010067#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010068#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010070#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010071#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010073#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010074#ifdef HAVE_SCHED_H
10075 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10076 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010077#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010078 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010079#endif
10080#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010081 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010082#endif
10083#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010084 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010085#endif
10086#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010087 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010088#endif
10089#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010090 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010091#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010092 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010093#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010094 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10095 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10096#endif
Benjamin Peterson2740af82011-08-02 17:41:34 -050010097#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010098#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010099 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010100#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010101#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010102 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010103#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010104#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010105 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010106#endif /* HAVE_GETEGID */
10107#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010108 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010109#endif /* HAVE_GETEUID */
10110#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010111 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010112#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010113#ifdef HAVE_GETGROUPLIST
10114 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10115#endif
Fred Drakec9680921999-12-13 16:37:25 +000010116#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010117 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010118#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010119 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010120#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010121 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010122#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010123#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010124 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010125#endif /* HAVE_GETPPID */
10126#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010127 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010128#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010129#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010130 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010131#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010132#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010133 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010134#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010135#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010136 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010137#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010138#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010139 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010140#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010141#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010142 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10143 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010144 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010145#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010146#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010148#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010149#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010150 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010151#endif /* HAVE_SETEUID */
10152#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010153 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010154#endif /* HAVE_SETEGID */
10155#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010156 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010157#endif /* HAVE_SETREUID */
10158#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010159 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010160#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010161#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010162 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010163#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010164#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010165 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010166#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010167#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010168 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010169#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010170#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010171 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010172#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010173#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010174 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010175#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010176#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010177 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010178#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010179#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010180 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010181#endif /* HAVE_WAIT3 */
10182#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010183 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010184#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010185#if defined(HAVE_WAITID) && !defined(__APPLE__)
10186 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10187#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010188#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010189 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010190#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010191#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010192 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010193#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010194#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010195 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010196#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010197#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010198 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010199#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010200#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010201 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010202#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010203#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010204 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010205#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010206 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10207 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10208 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10209 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10210 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10211 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010212#ifdef HAVE_LOCKF
10213 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10214#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010215 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10216 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010217#ifdef HAVE_READV
10218 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10219#endif
10220#ifdef HAVE_PREAD
10221 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10222#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010223 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010224#ifdef HAVE_WRITEV
10225 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10226#endif
10227#ifdef HAVE_PWRITE
10228 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10229#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010230#ifdef HAVE_SENDFILE
10231 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10232 posix_sendfile__doc__},
10233#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010234 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10235 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010236#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010237 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010238#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010239#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010240 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010241#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010242#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010243 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010244#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010245#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010246 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010247#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010248#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010249 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10250 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10251 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010252#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010253#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010254 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010255#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010256#ifdef HAVE_TRUNCATE
10257 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10258#endif
10259#ifdef HAVE_POSIX_FALLOCATE
10260 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10261#endif
10262#ifdef HAVE_POSIX_FADVISE
10263 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10264#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010265#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010266 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010267#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010268#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010269 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010270#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010271 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010272#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010273 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010274#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010275#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010276 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010277#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010278#ifdef HAVE_SYNC
10279 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10280#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010281#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010282 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010283#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010284#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010285#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010286 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010287#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010288#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010289 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010290#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010291#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010292 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010293#endif /* WIFSTOPPED */
10294#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010295 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010296#endif /* WIFSIGNALED */
10297#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010298 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010299#endif /* WIFEXITED */
10300#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010301 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010302#endif /* WEXITSTATUS */
10303#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010304 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010305#endif /* WTERMSIG */
10306#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010307 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010308#endif /* WSTOPSIG */
10309#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010310#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010311 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010312#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010313#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010314 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010315#endif
Fred Drakec9680921999-12-13 16:37:25 +000010316#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010317 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010318#endif
10319#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010320 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010321#endif
10322#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010323 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010324#endif
10325#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010326 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010327#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010328 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010329#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010330 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010331 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010332 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010333 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010334 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010335#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010336#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010337 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010338#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010339 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010340 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010341 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010342 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010343 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010344 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010345#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010346 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010347#endif
10348#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010349 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010350#endif
10351#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010352 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010353#endif
10354#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010355 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010356#endif
10357
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010358/* posix *at family of functions */
10359#ifdef HAVE_FACCESSAT
10360 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10361#endif
10362#ifdef HAVE_FCHMODAT
10363 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10364#endif /* HAVE_FCHMODAT */
10365#ifdef HAVE_FCHOWNAT
10366 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10367#endif /* HAVE_FCHOWNAT */
10368#ifdef HAVE_FSTATAT
10369 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10370#endif
10371#ifdef HAVE_FUTIMESAT
10372 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10373#endif
10374#ifdef HAVE_LINKAT
10375 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10376#endif /* HAVE_LINKAT */
10377#ifdef HAVE_MKDIRAT
10378 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10379#endif
10380#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10381 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10382#endif
10383#ifdef HAVE_OPENAT
10384 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10385#endif
10386#ifdef HAVE_READLINKAT
10387 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10388#endif /* HAVE_READLINKAT */
10389#ifdef HAVE_RENAMEAT
10390 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10391#endif
10392#if HAVE_SYMLINKAT
10393 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10394#endif /* HAVE_SYMLINKAT */
10395#ifdef HAVE_UNLINKAT
10396 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10397#endif
10398#ifdef HAVE_UTIMENSAT
10399 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10400#endif
10401#ifdef HAVE_MKFIFOAT
10402 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10403#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010404 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010405};
10406
10407
Barry Warsaw4a342091996-12-19 23:50:02 +000010408static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010409ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010410{
Victor Stinner8c62be82010-05-06 00:08:46 +000010411 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010412}
10413
Guido van Rossumd48f2521997-12-05 22:19:34 +000010414#if defined(PYOS_OS2)
10415/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010416static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010417{
10418 APIRET rc;
10419 ULONG values[QSV_MAX+1];
10420 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010421 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010422
10423 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010424 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010425 Py_END_ALLOW_THREADS
10426
10427 if (rc != NO_ERROR) {
10428 os2_error(rc);
10429 return -1;
10430 }
10431
Fred Drake4d1e64b2002-04-15 19:40:07 +000010432 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10433 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10434 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10435 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10436 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10437 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10438 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010439
10440 switch (values[QSV_VERSION_MINOR]) {
10441 case 0: ver = "2.00"; break;
10442 case 10: ver = "2.10"; break;
10443 case 11: ver = "2.11"; break;
10444 case 30: ver = "3.00"; break;
10445 case 40: ver = "4.00"; break;
10446 case 50: ver = "5.00"; break;
10447 default:
Tim Peters885d4572001-11-28 20:27:42 +000010448 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010449 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010450 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010451 ver = &tmp[0];
10452 }
10453
10454 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010455 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010456 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010457
10458 /* Add Indicator of Which Drive was Used to Boot the System */
10459 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10460 tmp[1] = ':';
10461 tmp[2] = '\0';
10462
Fred Drake4d1e64b2002-04-15 19:40:07 +000010463 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010464}
10465#endif
10466
Brian Curtin52173d42010-12-02 18:29:18 +000010467#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010468static int
Brian Curtin52173d42010-12-02 18:29:18 +000010469enable_symlink()
10470{
10471 HANDLE tok;
10472 TOKEN_PRIVILEGES tok_priv;
10473 LUID luid;
10474 int meth_idx = 0;
10475
10476 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010477 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010478
10479 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010480 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010481
10482 tok_priv.PrivilegeCount = 1;
10483 tok_priv.Privileges[0].Luid = luid;
10484 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10485
10486 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10487 sizeof(TOKEN_PRIVILEGES),
10488 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010489 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010490
Brian Curtin3b4499c2010-12-28 14:31:47 +000010491 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10492 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010493}
10494#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10495
Barry Warsaw4a342091996-12-19 23:50:02 +000010496static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010497all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010498{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010499#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010501#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010502#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010504#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010505#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010507#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010508#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010510#endif
Fred Drakec9680921999-12-13 16:37:25 +000010511#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010513#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010514#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010516#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010517#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010519#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010520#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010522#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010523#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010525#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010526#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010528#endif
10529#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010531#endif
10532#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010534#endif
10535#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010537#endif
10538#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010539 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010540#endif
10541#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010543#endif
10544#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010546#endif
10547#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010549#endif
10550#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010552#endif
10553#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010554 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010555#endif
10556#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010557 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010558#endif
10559#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000010560 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010561#endif
10562#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010563 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010564#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000010565#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010567#endif
10568#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010570#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010571#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010573#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000010574#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010576#endif
10577#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010579#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010580#ifdef PRIO_PROCESS
10581 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
10582#endif
10583#ifdef PRIO_PGRP
10584 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
10585#endif
10586#ifdef PRIO_USER
10587 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
10588#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010589#ifdef O_CLOEXEC
10590 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
10591#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010592/* posix - constants for *at functions */
10593#ifdef AT_SYMLINK_NOFOLLOW
10594 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
10595#endif
10596#ifdef AT_EACCESS
10597 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
10598#endif
10599#ifdef AT_FDCWD
10600 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
10601#endif
10602#ifdef AT_REMOVEDIR
10603 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
10604#endif
10605#ifdef AT_SYMLINK_FOLLOW
10606 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
10607#endif
10608#ifdef UTIME_NOW
10609 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10610#endif
10611#ifdef UTIME_OMIT
10612 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10613#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010614
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010615
Tim Peters5aa91602002-01-30 05:46:57 +000010616/* MS Windows */
10617#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010618 /* Don't inherit in child processes. */
10619 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010620#endif
10621#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000010622 /* Optimize for short life (keep in memory). */
10623 /* MS forgot to define this one with a non-underscore form too. */
10624 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010625#endif
10626#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010627 /* Automatically delete when last handle is closed. */
10628 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010629#endif
10630#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000010631 /* Optimize for random access. */
10632 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010633#endif
10634#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010635 /* Optimize for sequential access. */
10636 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010637#endif
10638
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010639/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010640#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 /* Send a SIGIO signal whenever input or output
10642 becomes available on file descriptor */
10643 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010644#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010645#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010646 /* Direct disk access. */
10647 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010648#endif
10649#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 /* Must be a directory. */
10651 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010652#endif
10653#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000010654 /* Do not follow links. */
10655 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010656#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010657#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010658 /* Do not update the access time. */
10659 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010660#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000010661
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010663#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010665#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010666#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010667 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010668#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010669#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010671#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010672#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010673 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010674#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010675#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000010676 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010677#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010678#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000010679 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010680#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010681#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010682 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010683#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010684#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000010685 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010686#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010687#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010688 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010689#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010690#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010691 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010692#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010693#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010694 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010695#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010696#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010698#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010699#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010701#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010702#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010704#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010705#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010707#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010708#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010710#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010711#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000010712 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010713#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010714
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010715 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010716#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010717 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010718#endif /* ST_RDONLY */
10719#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010720 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010721#endif /* ST_NOSUID */
10722
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010723 /* FreeBSD sendfile() constants */
10724#ifdef SF_NODISKIO
10725 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
10726#endif
10727#ifdef SF_MNOWAIT
10728 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
10729#endif
10730#ifdef SF_SYNC
10731 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
10732#endif
10733
Ross Lagerwall7807c352011-03-17 20:20:30 +020010734 /* constants for posix_fadvise */
10735#ifdef POSIX_FADV_NORMAL
10736 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
10737#endif
10738#ifdef POSIX_FADV_SEQUENTIAL
10739 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
10740#endif
10741#ifdef POSIX_FADV_RANDOM
10742 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
10743#endif
10744#ifdef POSIX_FADV_NOREUSE
10745 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
10746#endif
10747#ifdef POSIX_FADV_WILLNEED
10748 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
10749#endif
10750#ifdef POSIX_FADV_DONTNEED
10751 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
10752#endif
10753
10754 /* constants for waitid */
10755#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
10756 if (ins(d, "P_PID", (long)P_PID)) return -1;
10757 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
10758 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
10759#endif
10760#ifdef WEXITED
10761 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
10762#endif
10763#ifdef WNOWAIT
10764 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
10765#endif
10766#ifdef WSTOPPED
10767 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
10768#endif
10769#ifdef CLD_EXITED
10770 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
10771#endif
10772#ifdef CLD_DUMPED
10773 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
10774#endif
10775#ifdef CLD_TRAPPED
10776 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
10777#endif
10778#ifdef CLD_CONTINUED
10779 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
10780#endif
10781
10782 /* constants for lockf */
10783#ifdef F_LOCK
10784 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
10785#endif
10786#ifdef F_TLOCK
10787 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
10788#endif
10789#ifdef F_ULOCK
10790 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
10791#endif
10792#ifdef F_TEST
10793 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10794#endif
10795
10796 /* constants for futimens */
10797#ifdef UTIME_NOW
10798 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10799#endif
10800#ifdef UTIME_OMIT
10801 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10802#endif
10803
Guido van Rossum246bc171999-02-01 23:54:31 +000010804#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010805#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10807 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10808 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10809 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10810 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10811 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10812 if (ins(d, "P_PM", (long)P_PM)) return -1;
10813 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10814 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10815 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10816 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10817 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10818 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10819 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10820 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10821 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10822 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10823 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10824 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10825 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010826#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010827 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10828 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10829 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10830 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10831 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010832#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010833#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010834
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010835#ifdef HAVE_SCHED_H
10836 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
10837 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
10838#ifdef SCHED_SPORADIC
10839 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
10840#endif
10841 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
10842#ifdef SCHED_BATCH
10843 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
10844#endif
10845#ifdef SCHED_IDLE
10846 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
10847#endif
10848#ifdef SCHED_RESET_ON_FORK
10849 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
10850#endif
10851#endif
10852
Guido van Rossumd48f2521997-12-05 22:19:34 +000010853#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010855#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010856 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010857}
10858
10859
Tim Peters5aa91602002-01-30 05:46:57 +000010860#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010861#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010862#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010863
10864#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010865#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010866#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010867
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010868#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010869#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010870#define MODNAME "posix"
10871#endif
10872
Martin v. Löwis1a214512008-06-11 05:26:20 +000010873static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010874 PyModuleDef_HEAD_INIT,
10875 MODNAME,
10876 posix__doc__,
10877 -1,
10878 posix_methods,
10879 NULL,
10880 NULL,
10881 NULL,
10882 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010883};
10884
10885
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010886PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010887INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010888{
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010890
Brian Curtin52173d42010-12-02 18:29:18 +000010891#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010892 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010893#endif
10894
Victor Stinner8c62be82010-05-06 00:08:46 +000010895 m = PyModule_Create(&posixmodule);
10896 if (m == NULL)
10897 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010898
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 /* Initialize environ dictionary */
10900 v = convertenviron();
10901 Py_XINCREF(v);
10902 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10903 return NULL;
10904 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010905
Victor Stinner8c62be82010-05-06 00:08:46 +000010906 if (all_ins(m))
10907 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010908
Victor Stinner8c62be82010-05-06 00:08:46 +000010909 if (setup_confname_tables(m))
10910 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010911
Victor Stinner8c62be82010-05-06 00:08:46 +000010912 Py_INCREF(PyExc_OSError);
10913 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010914
Benjamin Peterson2740af82011-08-02 17:41:34 -050010915#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010916 if (PyType_Ready(&cpu_set_type) < 0)
10917 return NULL;
10918 Py_INCREF(&cpu_set_type);
10919 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050010920#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010921
Guido van Rossumb3d39562000-01-31 18:41:26 +000010922#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010923 if (posix_putenv_garbage == NULL)
10924 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010925#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010926
Victor Stinner8c62be82010-05-06 00:08:46 +000010927 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010928#if defined(HAVE_WAITID) && !defined(__APPLE__)
10929 waitid_result_desc.name = MODNAME ".waitid_result";
10930 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10931#endif
10932
Victor Stinner8c62be82010-05-06 00:08:46 +000010933 stat_result_desc.name = MODNAME ".stat_result";
10934 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10935 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10936 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10937 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10938 structseq_new = StatResultType.tp_new;
10939 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010940
Victor Stinner8c62be82010-05-06 00:08:46 +000010941 statvfs_result_desc.name = MODNAME ".statvfs_result";
10942 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010943#ifdef NEED_TICKS_PER_SECOND
10944# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010945 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010946# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010947 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010948# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010950# endif
10951#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010952
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050010953#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010954 sched_param_desc.name = MODNAME ".sched_param";
10955 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
10956 SchedParamType.tp_new = sched_param_new;
10957#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010958 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010959#if defined(HAVE_WAITID) && !defined(__APPLE__)
10960 Py_INCREF((PyObject*) &WaitidResultType);
10961 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10962#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010963 Py_INCREF((PyObject*) &StatResultType);
10964 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10965 Py_INCREF((PyObject*) &StatVFSResultType);
10966 PyModule_AddObject(m, "statvfs_result",
10967 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050010968
10969#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010970 Py_INCREF(&SchedParamType);
10971 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050010972#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010973 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010974
10975#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 /*
10977 * Step 2 of weak-linking support on Mac OS X.
10978 *
10979 * The code below removes functions that are not available on the
10980 * currently active platform.
10981 *
10982 * This block allow one to use a python binary that was build on
10983 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10984 * OSX 10.4.
10985 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010986#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010987 if (fstatvfs == NULL) {
10988 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10989 return NULL;
10990 }
10991 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010992#endif /* HAVE_FSTATVFS */
10993
10994#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010995 if (statvfs == NULL) {
10996 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10997 return NULL;
10998 }
10999 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011000#endif /* HAVE_STATVFS */
11001
11002# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 if (lchown == NULL) {
11004 if (PyObject_DelAttrString(m, "lchown") == -1) {
11005 return NULL;
11006 }
11007 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011008#endif /* HAVE_LCHOWN */
11009
11010
11011#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011013
Guido van Rossumb6775db1994-08-01 11:34:53 +000011014}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011015
11016#ifdef __cplusplus
11017}
11018#endif