blob: d701690a5d0fd5fffc70b16109a9ffa4d66b42d4 [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
Brian Curtindfc80e32011-08-10 20:28:54 -05004928 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
4929 Py_RETURN_NOTIMPLEMENTED;
4930
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004931 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
4932 if ((op == Py_EQ) ? eq : !eq)
4933 Py_RETURN_TRUE;
4934 else
4935 Py_RETURN_FALSE;
4936}
4937
4938#define CPU_SET_BINOP(name, op) \
4939 static PyObject * \
4940 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
4941 if (res) { \
4942 Py_INCREF(res); \
4943 } \
4944 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05004945 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004946 if (!res) \
4947 return NULL; \
4948 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05004949 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004950 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05004951 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004952 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05004953 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004954 op(res->size, res->set, left->set, right->set); \
4955 return (PyObject *)res; \
4956 } \
4957 static PyObject * \
4958 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
4959 return do_cpu_set_##name(left, right, NULL); \
4960 } \
4961 static PyObject * \
4962 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
4963 return do_cpu_set_##name(left, right, left); \
4964 } \
4965
4966CPU_SET_BINOP(and, CPU_AND_S)
4967CPU_SET_BINOP(or, CPU_OR_S)
4968CPU_SET_BINOP(xor, CPU_XOR_S)
4969#undef CPU_SET_BINOP
4970
4971PyDoc_STRVAR(cpu_set_doc,
4972"cpu_set(size)\n\n\
4973Create an empty mask of CPUs.");
4974
4975static PyNumberMethods cpu_set_as_number = {
4976 0, /*nb_add*/
4977 0, /*nb_subtract*/
4978 0, /*nb_multiply*/
4979 0, /*nb_remainder*/
4980 0, /*nb_divmod*/
4981 0, /*nb_power*/
4982 0, /*nb_negative*/
4983 0, /*nb_positive*/
4984 0, /*nb_absolute*/
4985 0, /*nb_bool*/
4986 0, /*nb_invert*/
4987 0, /*nb_lshift*/
4988 0, /*nb_rshift*/
4989 (binaryfunc)cpu_set_and, /*nb_and*/
4990 (binaryfunc)cpu_set_xor, /*nb_xor*/
4991 (binaryfunc)cpu_set_or, /*nb_or*/
4992 0, /*nb_int*/
4993 0, /*nb_reserved*/
4994 0, /*nb_float*/
4995 0, /*nb_inplace_add*/
4996 0, /*nb_inplace_subtract*/
4997 0, /*nb_inplace_multiply*/
4998 0, /*nb_inplace_remainder*/
4999 0, /*nb_inplace_power*/
5000 0, /*nb_inplace_lshift*/
5001 0, /*nb_inplace_rshift*/
5002 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5003 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5004 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5005};
5006
5007static PySequenceMethods cpu_set_as_sequence = {
5008 (lenfunc)cpu_set_len, /* sq_length */
5009};
5010
5011static PyMethodDef cpu_set_methods[] = {
5012 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5013 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5014 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5015 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5016 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5017 {NULL, NULL} /* sentinel */
5018};
5019
5020static PyTypeObject cpu_set_type = {
5021 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5022 "posix.cpu_set", /* tp_name */
5023 sizeof(Py_cpu_set), /* tp_basicsize */
5024 0, /* tp_itemsize */
5025 /* methods */
5026 (destructor)cpu_set_dealloc, /* tp_dealloc */
5027 0, /* tp_print */
5028 0, /* tp_getattr */
5029 0, /* tp_setattr */
5030 0, /* tp_reserved */
5031 (reprfunc)cpu_set_repr, /* tp_repr */
5032 &cpu_set_as_number, /* tp_as_number */
5033 &cpu_set_as_sequence, /* tp_as_sequence */
5034 0, /* tp_as_mapping */
5035 PyObject_HashNotImplemented, /* tp_hash */
5036 0, /* tp_call */
5037 0, /* tp_str */
5038 PyObject_GenericGetAttr, /* tp_getattro */
5039 0, /* tp_setattro */
5040 0, /* tp_as_buffer */
5041 Py_TPFLAGS_DEFAULT, /* tp_flags */
5042 cpu_set_doc, /* tp_doc */
5043 0, /* tp_traverse */
5044 0, /* tp_clear */
5045 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5046 0, /* tp_weaklistoffset */
5047 0, /* tp_iter */
5048 0, /* tp_iternext */
5049 cpu_set_methods, /* tp_methods */
5050 0, /* tp_members */
5051 0, /* tp_getset */
5052 0, /* tp_base */
5053 0, /* tp_dict */
5054 0, /* tp_descr_get */
5055 0, /* tp_descr_set */
5056 0, /* tp_dictoffset */
5057 0, /* tp_init */
5058 PyType_GenericAlloc, /* tp_alloc */
5059 cpu_set_new, /* tp_new */
5060 PyObject_Del, /* tp_free */
5061};
5062
5063PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5064"sched_setaffinity(pid, cpu_set)\n\n\
5065Set the affinity of the process with PID *pid* to *cpu_set*.");
5066
5067static PyObject *
5068posix_sched_setaffinity(PyObject *self, PyObject *args)
5069{
5070 pid_t pid;
5071 Py_cpu_set *cpu_set;
5072
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005073 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005074 &pid, &cpu_set_type, &cpu_set))
5075 return NULL;
5076 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5077 return posix_error();
5078 Py_RETURN_NONE;
5079}
5080
5081PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5082"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5083Return the affinity of the process with PID *pid*.\n\
5084The returned cpu_set will be of size *ncpus*.");
5085
5086static PyObject *
5087posix_sched_getaffinity(PyObject *self, PyObject *args)
5088{
5089 pid_t pid;
5090 int ncpus;
5091 Py_cpu_set *res;
5092
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005093 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005094 &pid, &ncpus))
5095 return NULL;
5096 res = make_new_cpu_set(&cpu_set_type, ncpus);
5097 if (!res)
5098 return NULL;
5099 if (sched_getaffinity(pid, res->size, res->set)) {
5100 Py_DECREF(res);
5101 return posix_error();
5102 }
5103 return (PyObject *)res;
5104}
5105
Benjamin Peterson2740af82011-08-02 17:41:34 -05005106#endif /* HAVE_SCHED_SETAFFINITY */
5107
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005108#endif /* HAVE_SCHED_H */
5109
Neal Norwitzb59798b2003-03-21 01:43:31 +00005110/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005111/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5112#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005113#define DEV_PTY_FILE "/dev/ptc"
5114#define HAVE_DEV_PTMX
5115#else
5116#define DEV_PTY_FILE "/dev/ptmx"
5117#endif
5118
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005119#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005120#ifdef HAVE_PTY_H
5121#include <pty.h>
5122#else
5123#ifdef HAVE_LIBUTIL_H
5124#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005125#else
5126#ifdef HAVE_UTIL_H
5127#include <util.h>
5128#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005129#endif /* HAVE_LIBUTIL_H */
5130#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005131#ifdef HAVE_STROPTS_H
5132#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005133#endif
5134#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005135
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005136#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005137PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005138"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005139Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005140
5141static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005142posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005143{
Victor Stinner8c62be82010-05-06 00:08:46 +00005144 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005145#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005146 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005147#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005148#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005149 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005150#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005151 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005152#endif
5153#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005154
Thomas Wouters70c21a12000-07-14 14:28:33 +00005155#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005156 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5157 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005158#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005159 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5160 if (slave_name == NULL)
5161 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005162
Victor Stinner8c62be82010-05-06 00:08:46 +00005163 slave_fd = open(slave_name, O_RDWR);
5164 if (slave_fd < 0)
5165 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005166#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005167 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5168 if (master_fd < 0)
5169 return posix_error();
5170 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5171 /* change permission of slave */
5172 if (grantpt(master_fd) < 0) {
5173 PyOS_setsig(SIGCHLD, sig_saved);
5174 return posix_error();
5175 }
5176 /* unlock slave */
5177 if (unlockpt(master_fd) < 0) {
5178 PyOS_setsig(SIGCHLD, sig_saved);
5179 return posix_error();
5180 }
5181 PyOS_setsig(SIGCHLD, sig_saved);
5182 slave_name = ptsname(master_fd); /* get name of slave */
5183 if (slave_name == NULL)
5184 return posix_error();
5185 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5186 if (slave_fd < 0)
5187 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005188#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005189 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5190 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005191#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005192 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005193#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005194#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005195#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005196
Victor Stinner8c62be82010-05-06 00:08:46 +00005197 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005198
Fred Drake8cef4cf2000-06-28 16:40:38 +00005199}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005200#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005201
5202#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005203PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005204"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005205Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5206Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005207To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005208
5209static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005210posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005211{
Victor Stinner8c62be82010-05-06 00:08:46 +00005212 int master_fd = -1, result = 0;
5213 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005214
Victor Stinner8c62be82010-05-06 00:08:46 +00005215 _PyImport_AcquireLock();
5216 pid = forkpty(&master_fd, NULL, NULL, NULL);
5217 if (pid == 0) {
5218 /* child: this clobbers and resets the import lock. */
5219 PyOS_AfterFork();
5220 } else {
5221 /* parent: release the import lock. */
5222 result = _PyImport_ReleaseLock();
5223 }
5224 if (pid == -1)
5225 return posix_error();
5226 if (result < 0) {
5227 /* Don't clobber the OSError if the fork failed. */
5228 PyErr_SetString(PyExc_RuntimeError,
5229 "not holding the import lock");
5230 return NULL;
5231 }
5232 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005233}
5234#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005235
Ross Lagerwall7807c352011-03-17 20:20:30 +02005236
Guido van Rossumad0ee831995-03-01 10:34:45 +00005237#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005238PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005239"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005240Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005241
Barry Warsaw53699e91996-12-10 23:23:01 +00005242static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005243posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005244{
Victor Stinner8c62be82010-05-06 00:08:46 +00005245 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005246}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005247#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005248
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005249
Guido van Rossumad0ee831995-03-01 10:34:45 +00005250#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005251PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005252"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005253Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005254
Barry Warsaw53699e91996-12-10 23:23:01 +00005255static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005256posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005257{
Victor Stinner8c62be82010-05-06 00:08:46 +00005258 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005259}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005260#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005261
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005262
Guido van Rossumad0ee831995-03-01 10:34:45 +00005263#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005264PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005265"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005266Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005267
Barry Warsaw53699e91996-12-10 23:23:01 +00005268static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005269posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005270{
Victor Stinner8c62be82010-05-06 00:08:46 +00005271 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005272}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005273#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005274
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005275
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005276PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005277"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005278Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005279
Barry Warsaw53699e91996-12-10 23:23:01 +00005280static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005281posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005282{
Victor Stinner8c62be82010-05-06 00:08:46 +00005283 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005284}
5285
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005286#ifdef HAVE_GETGROUPLIST
5287PyDoc_STRVAR(posix_getgrouplist__doc__,
5288"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5289Returns a list of groups to which a user belongs.\n\n\
5290 user: username to lookup\n\
5291 group: base group id of the user");
5292
5293static PyObject *
5294posix_getgrouplist(PyObject *self, PyObject *args)
5295{
5296#ifdef NGROUPS_MAX
5297#define MAX_GROUPS NGROUPS_MAX
5298#else
5299 /* defined to be 16 on Solaris7, so this should be a small number */
5300#define MAX_GROUPS 64
5301#endif
5302
5303 const char *user;
5304 int i, ngroups;
5305 PyObject *list;
5306#ifdef __APPLE__
5307 int *groups, basegid;
5308#else
5309 gid_t *groups, basegid;
5310#endif
5311 ngroups = MAX_GROUPS;
5312
5313 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5314 return NULL;
5315
5316#ifdef __APPLE__
5317 groups = PyMem_Malloc(ngroups * sizeof(int));
5318#else
5319 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5320#endif
5321 if (groups == NULL)
5322 return PyErr_NoMemory();
5323
5324 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5325 PyMem_Del(groups);
5326 return posix_error();
5327 }
5328
5329 list = PyList_New(ngroups);
5330 if (list == NULL) {
5331 PyMem_Del(groups);
5332 return NULL;
5333 }
5334
5335 for (i = 0; i < ngroups; i++) {
5336 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5337 if (o == NULL) {
5338 Py_DECREF(list);
5339 PyMem_Del(groups);
5340 return NULL;
5341 }
5342 PyList_SET_ITEM(list, i, o);
5343 }
5344
5345 PyMem_Del(groups);
5346
5347 return list;
5348}
5349#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005350
Fred Drakec9680921999-12-13 16:37:25 +00005351#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005352PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005353"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005354Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005355
5356static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005357posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005358{
5359 PyObject *result = NULL;
5360
Fred Drakec9680921999-12-13 16:37:25 +00005361#ifdef NGROUPS_MAX
5362#define MAX_GROUPS NGROUPS_MAX
5363#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005364 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005365#define MAX_GROUPS 64
5366#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005367 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005368
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005369 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005370 * This is a helper variable to store the intermediate result when
5371 * that happens.
5372 *
5373 * To keep the code readable the OSX behaviour is unconditional,
5374 * according to the POSIX spec this should be safe on all unix-y
5375 * systems.
5376 */
5377 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005378 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005379
Victor Stinner8c62be82010-05-06 00:08:46 +00005380 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005381 if (n < 0) {
5382 if (errno == EINVAL) {
5383 n = getgroups(0, NULL);
5384 if (n == -1) {
5385 return posix_error();
5386 }
5387 if (n == 0) {
5388 /* Avoid malloc(0) */
5389 alt_grouplist = grouplist;
5390 } else {
5391 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5392 if (alt_grouplist == NULL) {
5393 errno = EINVAL;
5394 return posix_error();
5395 }
5396 n = getgroups(n, alt_grouplist);
5397 if (n == -1) {
5398 PyMem_Free(alt_grouplist);
5399 return posix_error();
5400 }
5401 }
5402 } else {
5403 return posix_error();
5404 }
5405 }
5406 result = PyList_New(n);
5407 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005408 int i;
5409 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005410 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005411 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005412 Py_DECREF(result);
5413 result = NULL;
5414 break;
Fred Drakec9680921999-12-13 16:37:25 +00005415 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005416 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005417 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005418 }
5419
5420 if (alt_grouplist != grouplist) {
5421 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005422 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005423
Fred Drakec9680921999-12-13 16:37:25 +00005424 return result;
5425}
5426#endif
5427
Antoine Pitroub7572f02009-12-02 20:46:48 +00005428#ifdef HAVE_INITGROUPS
5429PyDoc_STRVAR(posix_initgroups__doc__,
5430"initgroups(username, gid) -> None\n\n\
5431Call the system initgroups() to initialize the group access list with all of\n\
5432the groups of which the specified username is a member, plus the specified\n\
5433group id.");
5434
5435static PyObject *
5436posix_initgroups(PyObject *self, PyObject *args)
5437{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005438 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005439 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005440 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005441 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005442
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005443 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5444 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005445 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005446 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005447
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005448 res = initgroups(username, (gid_t) gid);
5449 Py_DECREF(oname);
5450 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005451 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005452
Victor Stinner8c62be82010-05-06 00:08:46 +00005453 Py_INCREF(Py_None);
5454 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005455}
5456#endif
5457
Martin v. Löwis606edc12002-06-13 21:09:11 +00005458#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005459PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005460"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005461Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005462
5463static PyObject *
5464posix_getpgid(PyObject *self, PyObject *args)
5465{
Victor Stinner8c62be82010-05-06 00:08:46 +00005466 pid_t pid, pgid;
5467 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5468 return NULL;
5469 pgid = getpgid(pid);
5470 if (pgid < 0)
5471 return posix_error();
5472 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005473}
5474#endif /* HAVE_GETPGID */
5475
5476
Guido van Rossumb6775db1994-08-01 11:34:53 +00005477#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005478PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005479"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005480Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005481
Barry Warsaw53699e91996-12-10 23:23:01 +00005482static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005483posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005484{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005485#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005486 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005487#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005488 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005489#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005490}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005491#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005492
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005493
Guido van Rossumb6775db1994-08-01 11:34:53 +00005494#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005495PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005496"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005497Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005498
Barry Warsaw53699e91996-12-10 23:23:01 +00005499static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005500posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005501{
Guido van Rossum64933891994-10-20 21:56:42 +00005502#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005503 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005504#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005505 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005506#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005507 return posix_error();
5508 Py_INCREF(Py_None);
5509 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005510}
5511
Guido van Rossumb6775db1994-08-01 11:34:53 +00005512#endif /* HAVE_SETPGRP */
5513
Guido van Rossumad0ee831995-03-01 10:34:45 +00005514#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005515
5516#ifdef MS_WINDOWS
5517#include <tlhelp32.h>
5518
5519static PyObject*
5520win32_getppid()
5521{
5522 HANDLE snapshot;
5523 pid_t mypid;
5524 PyObject* result = NULL;
5525 BOOL have_record;
5526 PROCESSENTRY32 pe;
5527
5528 mypid = getpid(); /* This function never fails */
5529
5530 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5531 if (snapshot == INVALID_HANDLE_VALUE)
5532 return PyErr_SetFromWindowsErr(GetLastError());
5533
5534 pe.dwSize = sizeof(pe);
5535 have_record = Process32First(snapshot, &pe);
5536 while (have_record) {
5537 if (mypid == (pid_t)pe.th32ProcessID) {
5538 /* We could cache the ulong value in a static variable. */
5539 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5540 break;
5541 }
5542
5543 have_record = Process32Next(snapshot, &pe);
5544 }
5545
5546 /* If our loop exits and our pid was not found (result will be NULL)
5547 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5548 * error anyway, so let's raise it. */
5549 if (!result)
5550 result = PyErr_SetFromWindowsErr(GetLastError());
5551
5552 CloseHandle(snapshot);
5553
5554 return result;
5555}
5556#endif /*MS_WINDOWS*/
5557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005559"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005560Return the parent's process id. If the parent process has already exited,\n\
5561Windows machines will still return its id; others systems will return the id\n\
5562of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005563
Barry Warsaw53699e91996-12-10 23:23:01 +00005564static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005565posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005566{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005567#ifdef MS_WINDOWS
5568 return win32_getppid();
5569#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005570 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005571#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005572}
5573#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005574
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005575
Fred Drake12c6e2d1999-12-14 21:25:03 +00005576#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005577PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005578"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005579Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005580
5581static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005582posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005583{
Victor Stinner8c62be82010-05-06 00:08:46 +00005584 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005585#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005586 wchar_t user_name[UNLEN + 1];
5587 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5588
5589 if (GetUserNameW(user_name, &num_chars)) {
5590 /* num_chars is the number of unicode chars plus null terminator */
5591 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005592 }
5593 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005594 result = PyErr_SetFromWindowsErr(GetLastError());
5595#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005596 char *name;
5597 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005598
Victor Stinner8c62be82010-05-06 00:08:46 +00005599 errno = 0;
5600 name = getlogin();
5601 if (name == NULL) {
5602 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005603 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005604 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005605 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005606 }
5607 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005608 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005609 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005610#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005611 return result;
5612}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005613#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005614
Guido van Rossumad0ee831995-03-01 10:34:45 +00005615#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005616PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005617"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005618Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005619
Barry Warsaw53699e91996-12-10 23:23:01 +00005620static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005621posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005622{
Victor Stinner8c62be82010-05-06 00:08:46 +00005623 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005624}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005625#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005627
Guido van Rossumad0ee831995-03-01 10:34:45 +00005628#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005629PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005630"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005631Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005632
Barry Warsaw53699e91996-12-10 23:23:01 +00005633static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005634posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005635{
Victor Stinner8c62be82010-05-06 00:08:46 +00005636 pid_t pid;
5637 int sig;
5638 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5639 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005640#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005641 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5642 APIRET rc;
5643 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005644 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005645
5646 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5647 APIRET rc;
5648 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005649 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005650
5651 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005652 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005653#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005654 if (kill(pid, sig) == -1)
5655 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005656#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005657 Py_INCREF(Py_None);
5658 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005659}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005660#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005661
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005662#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005663PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005664"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005665Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005666
5667static PyObject *
5668posix_killpg(PyObject *self, PyObject *args)
5669{
Victor Stinner8c62be82010-05-06 00:08:46 +00005670 int sig;
5671 pid_t pgid;
5672 /* XXX some man pages make the `pgid` parameter an int, others
5673 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5674 take the same type. Moreover, pid_t is always at least as wide as
5675 int (else compilation of this module fails), which is safe. */
5676 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5677 return NULL;
5678 if (killpg(pgid, sig) == -1)
5679 return posix_error();
5680 Py_INCREF(Py_None);
5681 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005682}
5683#endif
5684
Brian Curtineb24d742010-04-12 17:16:38 +00005685#ifdef MS_WINDOWS
5686PyDoc_STRVAR(win32_kill__doc__,
5687"kill(pid, sig)\n\n\
5688Kill a process with a signal.");
5689
5690static PyObject *
5691win32_kill(PyObject *self, PyObject *args)
5692{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005693 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005694 DWORD pid, sig, err;
5695 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005696
Victor Stinner8c62be82010-05-06 00:08:46 +00005697 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5698 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005699
Victor Stinner8c62be82010-05-06 00:08:46 +00005700 /* Console processes which share a common console can be sent CTRL+C or
5701 CTRL+BREAK events, provided they handle said events. */
5702 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5703 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5704 err = GetLastError();
5705 PyErr_SetFromWindowsErr(err);
5706 }
5707 else
5708 Py_RETURN_NONE;
5709 }
Brian Curtineb24d742010-04-12 17:16:38 +00005710
Victor Stinner8c62be82010-05-06 00:08:46 +00005711 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5712 attempt to open and terminate the process. */
5713 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5714 if (handle == NULL) {
5715 err = GetLastError();
5716 return PyErr_SetFromWindowsErr(err);
5717 }
Brian Curtineb24d742010-04-12 17:16:38 +00005718
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 if (TerminateProcess(handle, sig) == 0) {
5720 err = GetLastError();
5721 result = PyErr_SetFromWindowsErr(err);
5722 } else {
5723 Py_INCREF(Py_None);
5724 result = Py_None;
5725 }
Brian Curtineb24d742010-04-12 17:16:38 +00005726
Victor Stinner8c62be82010-05-06 00:08:46 +00005727 CloseHandle(handle);
5728 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005729}
5730#endif /* MS_WINDOWS */
5731
Guido van Rossumc0125471996-06-28 18:55:32 +00005732#ifdef HAVE_PLOCK
5733
5734#ifdef HAVE_SYS_LOCK_H
5735#include <sys/lock.h>
5736#endif
5737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005738PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005739"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005740Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005741
Barry Warsaw53699e91996-12-10 23:23:01 +00005742static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005743posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005744{
Victor Stinner8c62be82010-05-06 00:08:46 +00005745 int op;
5746 if (!PyArg_ParseTuple(args, "i:plock", &op))
5747 return NULL;
5748 if (plock(op) == -1)
5749 return posix_error();
5750 Py_INCREF(Py_None);
5751 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005752}
5753#endif
5754
Guido van Rossumb6775db1994-08-01 11:34:53 +00005755#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005756PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005757"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005758Set the current process's user id.");
5759
Barry Warsaw53699e91996-12-10 23:23:01 +00005760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005761posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005762{
Victor Stinner8c62be82010-05-06 00:08:46 +00005763 long uid_arg;
5764 uid_t uid;
5765 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5766 return NULL;
5767 uid = uid_arg;
5768 if (uid != uid_arg) {
5769 PyErr_SetString(PyExc_OverflowError, "user id too big");
5770 return NULL;
5771 }
5772 if (setuid(uid) < 0)
5773 return posix_error();
5774 Py_INCREF(Py_None);
5775 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005776}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005777#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005778
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005779
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005780#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005781PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005782"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005783Set the current process's effective user id.");
5784
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005785static PyObject *
5786posix_seteuid (PyObject *self, PyObject *args)
5787{
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 long euid_arg;
5789 uid_t euid;
5790 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5791 return NULL;
5792 euid = euid_arg;
5793 if (euid != euid_arg) {
5794 PyErr_SetString(PyExc_OverflowError, "user id too big");
5795 return NULL;
5796 }
5797 if (seteuid(euid) < 0) {
5798 return posix_error();
5799 } else {
5800 Py_INCREF(Py_None);
5801 return Py_None;
5802 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005803}
5804#endif /* HAVE_SETEUID */
5805
5806#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005807PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005808"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005809Set the current process's effective group id.");
5810
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005811static PyObject *
5812posix_setegid (PyObject *self, PyObject *args)
5813{
Victor Stinner8c62be82010-05-06 00:08:46 +00005814 long egid_arg;
5815 gid_t egid;
5816 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5817 return NULL;
5818 egid = egid_arg;
5819 if (egid != egid_arg) {
5820 PyErr_SetString(PyExc_OverflowError, "group id too big");
5821 return NULL;
5822 }
5823 if (setegid(egid) < 0) {
5824 return posix_error();
5825 } else {
5826 Py_INCREF(Py_None);
5827 return Py_None;
5828 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005829}
5830#endif /* HAVE_SETEGID */
5831
5832#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005833PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005834"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005835Set the current process's real and effective user ids.");
5836
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005837static PyObject *
5838posix_setreuid (PyObject *self, PyObject *args)
5839{
Victor Stinner8c62be82010-05-06 00:08:46 +00005840 long ruid_arg, euid_arg;
5841 uid_t ruid, euid;
5842 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5843 return NULL;
5844 if (ruid_arg == -1)
5845 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5846 else
5847 ruid = ruid_arg; /* otherwise, assign from our long */
5848 if (euid_arg == -1)
5849 euid = (uid_t)-1;
5850 else
5851 euid = euid_arg;
5852 if ((euid_arg != -1 && euid != euid_arg) ||
5853 (ruid_arg != -1 && ruid != ruid_arg)) {
5854 PyErr_SetString(PyExc_OverflowError, "user id too big");
5855 return NULL;
5856 }
5857 if (setreuid(ruid, euid) < 0) {
5858 return posix_error();
5859 } else {
5860 Py_INCREF(Py_None);
5861 return Py_None;
5862 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005863}
5864#endif /* HAVE_SETREUID */
5865
5866#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005867PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005868"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005869Set the current process's real and effective group ids.");
5870
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005871static PyObject *
5872posix_setregid (PyObject *self, PyObject *args)
5873{
Victor Stinner8c62be82010-05-06 00:08:46 +00005874 long rgid_arg, egid_arg;
5875 gid_t rgid, egid;
5876 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5877 return NULL;
5878 if (rgid_arg == -1)
5879 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5880 else
5881 rgid = rgid_arg; /* otherwise, assign from our long */
5882 if (egid_arg == -1)
5883 egid = (gid_t)-1;
5884 else
5885 egid = egid_arg;
5886 if ((egid_arg != -1 && egid != egid_arg) ||
5887 (rgid_arg != -1 && rgid != rgid_arg)) {
5888 PyErr_SetString(PyExc_OverflowError, "group id too big");
5889 return NULL;
5890 }
5891 if (setregid(rgid, egid) < 0) {
5892 return posix_error();
5893 } else {
5894 Py_INCREF(Py_None);
5895 return Py_None;
5896 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005897}
5898#endif /* HAVE_SETREGID */
5899
Guido van Rossumb6775db1994-08-01 11:34:53 +00005900#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005901PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005902"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005903Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005904
Barry Warsaw53699e91996-12-10 23:23:01 +00005905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005906posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005907{
Victor Stinner8c62be82010-05-06 00:08:46 +00005908 long gid_arg;
5909 gid_t gid;
5910 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5911 return NULL;
5912 gid = gid_arg;
5913 if (gid != gid_arg) {
5914 PyErr_SetString(PyExc_OverflowError, "group id too big");
5915 return NULL;
5916 }
5917 if (setgid(gid) < 0)
5918 return posix_error();
5919 Py_INCREF(Py_None);
5920 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005921}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005922#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005923
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005924#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005925PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005926"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005928
5929static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005930posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005931{
Victor Stinner8c62be82010-05-06 00:08:46 +00005932 int i, len;
5933 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005934
Victor Stinner8c62be82010-05-06 00:08:46 +00005935 if (!PySequence_Check(groups)) {
5936 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5937 return NULL;
5938 }
5939 len = PySequence_Size(groups);
5940 if (len > MAX_GROUPS) {
5941 PyErr_SetString(PyExc_ValueError, "too many groups");
5942 return NULL;
5943 }
5944 for(i = 0; i < len; i++) {
5945 PyObject *elem;
5946 elem = PySequence_GetItem(groups, i);
5947 if (!elem)
5948 return NULL;
5949 if (!PyLong_Check(elem)) {
5950 PyErr_SetString(PyExc_TypeError,
5951 "groups must be integers");
5952 Py_DECREF(elem);
5953 return NULL;
5954 } else {
5955 unsigned long x = PyLong_AsUnsignedLong(elem);
5956 if (PyErr_Occurred()) {
5957 PyErr_SetString(PyExc_TypeError,
5958 "group id too big");
5959 Py_DECREF(elem);
5960 return NULL;
5961 }
5962 grouplist[i] = x;
5963 /* read back the value to see if it fitted in gid_t */
5964 if (grouplist[i] != x) {
5965 PyErr_SetString(PyExc_TypeError,
5966 "group id too big");
5967 Py_DECREF(elem);
5968 return NULL;
5969 }
5970 }
5971 Py_DECREF(elem);
5972 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005973
Victor Stinner8c62be82010-05-06 00:08:46 +00005974 if (setgroups(len, grouplist) < 0)
5975 return posix_error();
5976 Py_INCREF(Py_None);
5977 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005978}
5979#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005980
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005981#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5982static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005983wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005984{
Victor Stinner8c62be82010-05-06 00:08:46 +00005985 PyObject *result;
5986 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005987
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 if (pid == -1)
5989 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005990
Victor Stinner8c62be82010-05-06 00:08:46 +00005991 if (struct_rusage == NULL) {
5992 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5993 if (m == NULL)
5994 return NULL;
5995 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5996 Py_DECREF(m);
5997 if (struct_rusage == NULL)
5998 return NULL;
5999 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006000
Victor Stinner8c62be82010-05-06 00:08:46 +00006001 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6002 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6003 if (!result)
6004 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006005
6006#ifndef doubletime
6007#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6008#endif
6009
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 PyStructSequence_SET_ITEM(result, 0,
6011 PyFloat_FromDouble(doubletime(ru->ru_utime)));
6012 PyStructSequence_SET_ITEM(result, 1,
6013 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006014#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6016 SET_INT(result, 2, ru->ru_maxrss);
6017 SET_INT(result, 3, ru->ru_ixrss);
6018 SET_INT(result, 4, ru->ru_idrss);
6019 SET_INT(result, 5, ru->ru_isrss);
6020 SET_INT(result, 6, ru->ru_minflt);
6021 SET_INT(result, 7, ru->ru_majflt);
6022 SET_INT(result, 8, ru->ru_nswap);
6023 SET_INT(result, 9, ru->ru_inblock);
6024 SET_INT(result, 10, ru->ru_oublock);
6025 SET_INT(result, 11, ru->ru_msgsnd);
6026 SET_INT(result, 12, ru->ru_msgrcv);
6027 SET_INT(result, 13, ru->ru_nsignals);
6028 SET_INT(result, 14, ru->ru_nvcsw);
6029 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006030#undef SET_INT
6031
Victor Stinner8c62be82010-05-06 00:08:46 +00006032 if (PyErr_Occurred()) {
6033 Py_DECREF(result);
6034 return NULL;
6035 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006036
Victor Stinner8c62be82010-05-06 00:08:46 +00006037 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006038}
6039#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6040
6041#ifdef HAVE_WAIT3
6042PyDoc_STRVAR(posix_wait3__doc__,
6043"wait3(options) -> (pid, status, rusage)\n\n\
6044Wait for completion of a child process.");
6045
6046static PyObject *
6047posix_wait3(PyObject *self, PyObject *args)
6048{
Victor Stinner8c62be82010-05-06 00:08:46 +00006049 pid_t pid;
6050 int options;
6051 struct rusage ru;
6052 WAIT_TYPE status;
6053 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006054
Victor Stinner8c62be82010-05-06 00:08:46 +00006055 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6056 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006057
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 Py_BEGIN_ALLOW_THREADS
6059 pid = wait3(&status, options, &ru);
6060 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006061
Victor Stinner8c62be82010-05-06 00:08:46 +00006062 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006063}
6064#endif /* HAVE_WAIT3 */
6065
6066#ifdef HAVE_WAIT4
6067PyDoc_STRVAR(posix_wait4__doc__,
6068"wait4(pid, options) -> (pid, status, rusage)\n\n\
6069Wait for completion of a given child process.");
6070
6071static PyObject *
6072posix_wait4(PyObject *self, PyObject *args)
6073{
Victor Stinner8c62be82010-05-06 00:08:46 +00006074 pid_t pid;
6075 int options;
6076 struct rusage ru;
6077 WAIT_TYPE status;
6078 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006079
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6081 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006082
Victor Stinner8c62be82010-05-06 00:08:46 +00006083 Py_BEGIN_ALLOW_THREADS
6084 pid = wait4(pid, &status, options, &ru);
6085 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006086
Victor Stinner8c62be82010-05-06 00:08:46 +00006087 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006088}
6089#endif /* HAVE_WAIT4 */
6090
Ross Lagerwall7807c352011-03-17 20:20:30 +02006091#if defined(HAVE_WAITID) && !defined(__APPLE__)
6092PyDoc_STRVAR(posix_waitid__doc__,
6093"waitid(idtype, id, options) -> waitid_result\n\n\
6094Wait for the completion of one or more child processes.\n\n\
6095idtype can be P_PID, P_PGID or P_ALL.\n\
6096id specifies the pid to wait on.\n\
6097options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6098or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6099Returns either waitid_result or None if WNOHANG is specified and there are\n\
6100no children in a waitable state.");
6101
6102static PyObject *
6103posix_waitid(PyObject *self, PyObject *args)
6104{
6105 PyObject *result;
6106 idtype_t idtype;
6107 id_t id;
6108 int options, res;
6109 siginfo_t si;
6110 si.si_pid = 0;
6111 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6112 return NULL;
6113 Py_BEGIN_ALLOW_THREADS
6114 res = waitid(idtype, id, &si, options);
6115 Py_END_ALLOW_THREADS
6116 if (res == -1)
6117 return posix_error();
6118
6119 if (si.si_pid == 0)
6120 Py_RETURN_NONE;
6121
6122 result = PyStructSequence_New(&WaitidResultType);
6123 if (!result)
6124 return NULL;
6125
6126 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6127 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6128 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6129 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6130 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6131 if (PyErr_Occurred()) {
6132 Py_DECREF(result);
6133 return NULL;
6134 }
6135
6136 return result;
6137}
6138#endif
6139
Guido van Rossumb6775db1994-08-01 11:34:53 +00006140#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006141PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006142"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006143Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006144
Barry Warsaw53699e91996-12-10 23:23:01 +00006145static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006146posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006147{
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 pid_t pid;
6149 int options;
6150 WAIT_TYPE status;
6151 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006152
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6154 return NULL;
6155 Py_BEGIN_ALLOW_THREADS
6156 pid = waitpid(pid, &status, options);
6157 Py_END_ALLOW_THREADS
6158 if (pid == -1)
6159 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006160
Victor Stinner8c62be82010-05-06 00:08:46 +00006161 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006162}
6163
Tim Petersab034fa2002-02-01 11:27:43 +00006164#elif defined(HAVE_CWAIT)
6165
6166/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006167PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006168"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006169"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006170
6171static PyObject *
6172posix_waitpid(PyObject *self, PyObject *args)
6173{
Victor Stinner8c62be82010-05-06 00:08:46 +00006174 Py_intptr_t pid;
6175 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006176
Victor Stinner8c62be82010-05-06 00:08:46 +00006177 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6178 return NULL;
6179 Py_BEGIN_ALLOW_THREADS
6180 pid = _cwait(&status, pid, options);
6181 Py_END_ALLOW_THREADS
6182 if (pid == -1)
6183 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006184
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 /* shift the status left a byte so this is more like the POSIX waitpid */
6186 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006187}
6188#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006189
Guido van Rossumad0ee831995-03-01 10:34:45 +00006190#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006191PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006192"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006193Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006194
Barry Warsaw53699e91996-12-10 23:23:01 +00006195static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006196posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006197{
Victor Stinner8c62be82010-05-06 00:08:46 +00006198 pid_t pid;
6199 WAIT_TYPE status;
6200 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006201
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 Py_BEGIN_ALLOW_THREADS
6203 pid = wait(&status);
6204 Py_END_ALLOW_THREADS
6205 if (pid == -1)
6206 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006207
Victor Stinner8c62be82010-05-06 00:08:46 +00006208 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006209}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006210#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006211
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006213PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006214"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006215Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006216
Barry Warsaw53699e91996-12-10 23:23:01 +00006217static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006218posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006219{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006220#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006222#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006223#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006224 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006225 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006226#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006228#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006229#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006230}
6231
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006232
Guido van Rossumb6775db1994-08-01 11:34:53 +00006233#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006235"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006237
Barry Warsaw53699e91996-12-10 23:23:01 +00006238static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006239posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006240{
Victor Stinner8c62be82010-05-06 00:08:46 +00006241 PyObject* v;
6242 char buf[MAXPATHLEN];
6243 PyObject *opath;
6244 char *path;
6245 int n;
6246 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006247
Victor Stinner8c62be82010-05-06 00:08:46 +00006248 if (!PyArg_ParseTuple(args, "O&:readlink",
6249 PyUnicode_FSConverter, &opath))
6250 return NULL;
6251 path = PyBytes_AsString(opath);
6252 v = PySequence_GetItem(args, 0);
6253 if (v == NULL) {
6254 Py_DECREF(opath);
6255 return NULL;
6256 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006257
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 if (PyUnicode_Check(v)) {
6259 arg_is_unicode = 1;
6260 }
6261 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006262
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 Py_BEGIN_ALLOW_THREADS
6264 n = readlink(path, buf, (int) sizeof buf);
6265 Py_END_ALLOW_THREADS
6266 if (n < 0)
6267 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006268
Victor Stinner8c62be82010-05-06 00:08:46 +00006269 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006270 if (arg_is_unicode)
6271 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6272 else
6273 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006274}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006275#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006276
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006277
Brian Curtin52173d42010-12-02 18:29:18 +00006278#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006279PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006280"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006281Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006282
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006283static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006284posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006285{
Victor Stinner8c62be82010-05-06 00:08:46 +00006286 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006287}
6288#endif /* HAVE_SYMLINK */
6289
Brian Curtind40e6f72010-07-08 21:39:08 +00006290#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6291
6292PyDoc_STRVAR(win_readlink__doc__,
6293"readlink(path) -> path\n\n\
6294Return a string representing the path to which the symbolic link points.");
6295
Brian Curtind40e6f72010-07-08 21:39:08 +00006296/* Windows readlink implementation */
6297static PyObject *
6298win_readlink(PyObject *self, PyObject *args)
6299{
6300 wchar_t *path;
6301 DWORD n_bytes_returned;
6302 DWORD io_result;
6303 PyObject *result;
6304 HANDLE reparse_point_handle;
6305
6306 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6307 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6308 wchar_t *print_name;
6309
6310 if (!PyArg_ParseTuple(args,
6311 "u:readlink",
6312 &path))
6313 return NULL;
6314
6315 /* First get a handle to the reparse point */
6316 Py_BEGIN_ALLOW_THREADS
6317 reparse_point_handle = CreateFileW(
6318 path,
6319 0,
6320 0,
6321 0,
6322 OPEN_EXISTING,
6323 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6324 0);
6325 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006326
Brian Curtind40e6f72010-07-08 21:39:08 +00006327 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6328 {
6329 return win32_error_unicode("readlink", path);
6330 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006331
Brian Curtind40e6f72010-07-08 21:39:08 +00006332 Py_BEGIN_ALLOW_THREADS
6333 /* New call DeviceIoControl to read the reparse point */
6334 io_result = DeviceIoControl(
6335 reparse_point_handle,
6336 FSCTL_GET_REPARSE_POINT,
6337 0, 0, /* in buffer */
6338 target_buffer, sizeof(target_buffer),
6339 &n_bytes_returned,
6340 0 /* we're not using OVERLAPPED_IO */
6341 );
6342 CloseHandle(reparse_point_handle);
6343 Py_END_ALLOW_THREADS
6344
6345 if (io_result==0)
6346 {
6347 return win32_error_unicode("readlink", path);
6348 }
6349
6350 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6351 {
6352 PyErr_SetString(PyExc_ValueError,
6353 "not a symbolic link");
6354 return NULL;
6355 }
Brian Curtin74e45612010-07-09 15:58:59 +00006356 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6357 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6358
6359 result = PyUnicode_FromWideChar(print_name,
6360 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006361 return result;
6362}
6363
6364#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6365
Brian Curtin52173d42010-12-02 18:29:18 +00006366#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006367
6368/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6369static int has_CreateSymbolicLinkW = 0;
6370static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6371static int
6372check_CreateSymbolicLinkW()
6373{
6374 HINSTANCE hKernel32;
6375 /* only recheck */
6376 if (has_CreateSymbolicLinkW)
6377 return has_CreateSymbolicLinkW;
6378 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006379 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6380 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006381 if (Py_CreateSymbolicLinkW)
6382 has_CreateSymbolicLinkW = 1;
6383 return has_CreateSymbolicLinkW;
6384}
6385
6386PyDoc_STRVAR(win_symlink__doc__,
6387"symlink(src, dst, target_is_directory=False)\n\n\
6388Create a symbolic link pointing to src named dst.\n\
6389target_is_directory is required if the target is to be interpreted as\n\
6390a directory.\n\
6391This function requires Windows 6.0 or greater, and raises a\n\
6392NotImplementedError otherwise.");
6393
6394static PyObject *
6395win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6396{
6397 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6398 PyObject *src, *dest;
6399 int target_is_directory = 0;
6400 DWORD res;
6401 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006402
Brian Curtind40e6f72010-07-08 21:39:08 +00006403 if (!check_CreateSymbolicLinkW())
6404 {
6405 /* raise NotImplementedError */
6406 return PyErr_Format(PyExc_NotImplementedError,
6407 "CreateSymbolicLinkW not found");
6408 }
6409 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6410 kwlist, &src, &dest, &target_is_directory))
6411 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006412
6413 if (win32_can_symlink == 0)
6414 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6415
Brian Curtind40e6f72010-07-08 21:39:08 +00006416 if (!convert_to_unicode(&src)) { return NULL; }
6417 if (!convert_to_unicode(&dest)) {
6418 Py_DECREF(src);
6419 return NULL;
6420 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006421
Brian Curtind40e6f72010-07-08 21:39:08 +00006422 /* if src is a directory, ensure target_is_directory==1 */
6423 if(
6424 GetFileAttributesExW(
6425 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6426 ))
6427 {
6428 target_is_directory = target_is_directory ||
6429 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6430 }
6431
6432 Py_BEGIN_ALLOW_THREADS
6433 res = Py_CreateSymbolicLinkW(
6434 PyUnicode_AsUnicode(dest),
6435 PyUnicode_AsUnicode(src),
6436 target_is_directory);
6437 Py_END_ALLOW_THREADS
6438 Py_DECREF(src);
6439 Py_DECREF(dest);
6440 if (!res)
6441 {
6442 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6443 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006444
Brian Curtind40e6f72010-07-08 21:39:08 +00006445 Py_INCREF(Py_None);
6446 return Py_None;
6447}
Brian Curtin52173d42010-12-02 18:29:18 +00006448#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006449
6450#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006451#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6452static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006453system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006454{
6455 ULONG value = 0;
6456
6457 Py_BEGIN_ALLOW_THREADS
6458 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6459 Py_END_ALLOW_THREADS
6460
6461 return value;
6462}
6463
6464static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006465posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006466{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006467 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006468 return Py_BuildValue("ddddd",
6469 (double)0 /* t.tms_utime / HZ */,
6470 (double)0 /* t.tms_stime / HZ */,
6471 (double)0 /* t.tms_cutime / HZ */,
6472 (double)0 /* t.tms_cstime / HZ */,
6473 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006474}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006475#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006476#define NEED_TICKS_PER_SECOND
6477static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006478static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006479posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006480{
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 struct tms t;
6482 clock_t c;
6483 errno = 0;
6484 c = times(&t);
6485 if (c == (clock_t) -1)
6486 return posix_error();
6487 return Py_BuildValue("ddddd",
6488 (double)t.tms_utime / ticks_per_second,
6489 (double)t.tms_stime / ticks_per_second,
6490 (double)t.tms_cutime / ticks_per_second,
6491 (double)t.tms_cstime / ticks_per_second,
6492 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006493}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006494#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006495#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006496
6497
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006498#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006499#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006500static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006501posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006502{
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 FILETIME create, exit, kernel, user;
6504 HANDLE hProc;
6505 hProc = GetCurrentProcess();
6506 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6507 /* The fields of a FILETIME structure are the hi and lo part
6508 of a 64-bit value expressed in 100 nanosecond units.
6509 1e7 is one second in such units; 1e-7 the inverse.
6510 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6511 */
6512 return Py_BuildValue(
6513 "ddddd",
6514 (double)(user.dwHighDateTime*429.4967296 +
6515 user.dwLowDateTime*1e-7),
6516 (double)(kernel.dwHighDateTime*429.4967296 +
6517 kernel.dwLowDateTime*1e-7),
6518 (double)0,
6519 (double)0,
6520 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006521}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006522#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006523
6524#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006525PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006526"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006527Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006528#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006529
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006530
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006531#ifdef HAVE_GETSID
6532PyDoc_STRVAR(posix_getsid__doc__,
6533"getsid(pid) -> sid\n\n\
6534Call the system call getsid().");
6535
6536static PyObject *
6537posix_getsid(PyObject *self, PyObject *args)
6538{
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 pid_t pid;
6540 int sid;
6541 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6542 return NULL;
6543 sid = getsid(pid);
6544 if (sid < 0)
6545 return posix_error();
6546 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006547}
6548#endif /* HAVE_GETSID */
6549
6550
Guido van Rossumb6775db1994-08-01 11:34:53 +00006551#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006552PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006553"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006554Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006555
Barry Warsaw53699e91996-12-10 23:23:01 +00006556static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006557posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006558{
Victor Stinner8c62be82010-05-06 00:08:46 +00006559 if (setsid() < 0)
6560 return posix_error();
6561 Py_INCREF(Py_None);
6562 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006563}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006564#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006565
Guido van Rossumb6775db1994-08-01 11:34:53 +00006566#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006567PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006568"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006569Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006570
Barry Warsaw53699e91996-12-10 23:23:01 +00006571static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006572posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006573{
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 pid_t pid;
6575 int pgrp;
6576 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6577 return NULL;
6578 if (setpgid(pid, pgrp) < 0)
6579 return posix_error();
6580 Py_INCREF(Py_None);
6581 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006582}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006583#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006584
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006585
Guido van Rossumb6775db1994-08-01 11:34:53 +00006586#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006587PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006588"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006589Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006590
Barry Warsaw53699e91996-12-10 23:23:01 +00006591static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006592posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006593{
Victor Stinner8c62be82010-05-06 00:08:46 +00006594 int fd;
6595 pid_t pgid;
6596 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6597 return NULL;
6598 pgid = tcgetpgrp(fd);
6599 if (pgid < 0)
6600 return posix_error();
6601 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006602}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006603#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006604
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006605
Guido van Rossumb6775db1994-08-01 11:34:53 +00006606#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006607PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006608"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006609Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006610
Barry Warsaw53699e91996-12-10 23:23:01 +00006611static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006612posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006613{
Victor Stinner8c62be82010-05-06 00:08:46 +00006614 int fd;
6615 pid_t pgid;
6616 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6617 return NULL;
6618 if (tcsetpgrp(fd, pgid) < 0)
6619 return posix_error();
6620 Py_INCREF(Py_None);
6621 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006622}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006623#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006624
Guido van Rossum687dd131993-05-17 08:34:16 +00006625/* Functions acting on file descriptors */
6626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006627PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006628"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006629Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006630
Barry Warsaw53699e91996-12-10 23:23:01 +00006631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006632posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006633{
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 PyObject *ofile;
6635 char *file;
6636 int flag;
6637 int mode = 0777;
6638 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006639
6640#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006641 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006642 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 Py_BEGIN_ALLOW_THREADS
6644 /* PyUnicode_AS_UNICODE OK without thread
6645 lock as it is a simple dereference. */
6646 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6647 Py_END_ALLOW_THREADS
6648 if (fd < 0)
6649 return posix_error();
6650 return PyLong_FromLong((long)fd);
6651 }
6652 /* Drop the argument parsing error as narrow strings
6653 are also valid. */
6654 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006655#endif
6656
Victor Stinner26de69d2011-06-17 15:15:38 +02006657 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 PyUnicode_FSConverter, &ofile,
6659 &flag, &mode))
6660 return NULL;
6661 file = PyBytes_AsString(ofile);
6662 Py_BEGIN_ALLOW_THREADS
6663 fd = open(file, flag, mode);
6664 Py_END_ALLOW_THREADS
6665 if (fd < 0)
6666 return posix_error_with_allocated_filename(ofile);
6667 Py_DECREF(ofile);
6668 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006669}
6670
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006672PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006673"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006674Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006675
Barry Warsaw53699e91996-12-10 23:23:01 +00006676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006677posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006678{
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 int fd, res;
6680 if (!PyArg_ParseTuple(args, "i:close", &fd))
6681 return NULL;
6682 if (!_PyVerify_fd(fd))
6683 return posix_error();
6684 Py_BEGIN_ALLOW_THREADS
6685 res = close(fd);
6686 Py_END_ALLOW_THREADS
6687 if (res < 0)
6688 return posix_error();
6689 Py_INCREF(Py_None);
6690 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006691}
6692
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006693
Victor Stinner8c62be82010-05-06 00:08:46 +00006694PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006695"closerange(fd_low, fd_high)\n\n\
6696Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6697
6698static PyObject *
6699posix_closerange(PyObject *self, PyObject *args)
6700{
Victor Stinner8c62be82010-05-06 00:08:46 +00006701 int fd_from, fd_to, i;
6702 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6703 return NULL;
6704 Py_BEGIN_ALLOW_THREADS
6705 for (i = fd_from; i < fd_to; i++)
6706 if (_PyVerify_fd(i))
6707 close(i);
6708 Py_END_ALLOW_THREADS
6709 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006710}
6711
6712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006713PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006714"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006715Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006716
Barry Warsaw53699e91996-12-10 23:23:01 +00006717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006718posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006719{
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 int fd;
6721 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6722 return NULL;
6723 if (!_PyVerify_fd(fd))
6724 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006725 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 if (fd < 0)
6727 return posix_error();
6728 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006729}
6730
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006732PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006733"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006734Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006735
Barry Warsaw53699e91996-12-10 23:23:01 +00006736static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006737posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006738{
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 int fd, fd2, res;
6740 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6741 return NULL;
6742 if (!_PyVerify_fd_dup2(fd, fd2))
6743 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006744 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 if (res < 0)
6746 return posix_error();
6747 Py_INCREF(Py_None);
6748 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006749}
6750
Ross Lagerwall7807c352011-03-17 20:20:30 +02006751#ifdef HAVE_LOCKF
6752PyDoc_STRVAR(posix_lockf__doc__,
6753"lockf(fd, cmd, len)\n\n\
6754Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6755fd is an open file descriptor.\n\
6756cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6757F_TEST.\n\
6758len specifies the section of the file to lock.");
6759
6760static PyObject *
6761posix_lockf(PyObject *self, PyObject *args)
6762{
6763 int fd, cmd, res;
6764 off_t len;
6765 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6766 &fd, &cmd, _parse_off_t, &len))
6767 return NULL;
6768
6769 Py_BEGIN_ALLOW_THREADS
6770 res = lockf(fd, cmd, len);
6771 Py_END_ALLOW_THREADS
6772
6773 if (res < 0)
6774 return posix_error();
6775
6776 Py_RETURN_NONE;
6777}
6778#endif
6779
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006781PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006782"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006783Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006784
Barry Warsaw53699e91996-12-10 23:23:01 +00006785static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006786posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006787{
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006789#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006791#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006793#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006794 PyObject *posobj;
6795 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006797#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6799 switch (how) {
6800 case 0: how = SEEK_SET; break;
6801 case 1: how = SEEK_CUR; break;
6802 case 2: how = SEEK_END; break;
6803 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006804#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006805
Ross Lagerwall8e749672011-03-17 21:54:07 +02006806#if !defined(HAVE_LARGEFILE_SUPPORT)
6807 pos = PyLong_AsLong(posobj);
6808#else
6809 pos = PyLong_AsLongLong(posobj);
6810#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 if (PyErr_Occurred())
6812 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006813
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 if (!_PyVerify_fd(fd))
6815 return posix_error();
6816 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006817#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006818 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006819#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006821#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 Py_END_ALLOW_THREADS
6823 if (res < 0)
6824 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006825
6826#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006827 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006828#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006830#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006831}
6832
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006833
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006834PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006835"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006836Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006837
Barry Warsaw53699e91996-12-10 23:23:01 +00006838static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006839posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006840{
Victor Stinner8c62be82010-05-06 00:08:46 +00006841 int fd, size;
6842 Py_ssize_t n;
6843 PyObject *buffer;
6844 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6845 return NULL;
6846 if (size < 0) {
6847 errno = EINVAL;
6848 return posix_error();
6849 }
6850 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6851 if (buffer == NULL)
6852 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006853 if (!_PyVerify_fd(fd)) {
6854 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006856 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006857 Py_BEGIN_ALLOW_THREADS
6858 n = read(fd, PyBytes_AS_STRING(buffer), size);
6859 Py_END_ALLOW_THREADS
6860 if (n < 0) {
6861 Py_DECREF(buffer);
6862 return posix_error();
6863 }
6864 if (n != size)
6865 _PyBytes_Resize(&buffer, n);
6866 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006867}
6868
Ross Lagerwall7807c352011-03-17 20:20:30 +02006869#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6870 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006871static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006872iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6873{
6874 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006875 Py_ssize_t blen, total = 0;
6876
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006877 *iov = PyMem_New(struct iovec, cnt);
6878 if (*iov == NULL) {
6879 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006880 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006881 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006882
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006883 *buf = PyMem_New(Py_buffer, cnt);
6884 if (*buf == NULL) {
6885 PyMem_Del(*iov);
6886 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006887 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006888 }
6889
6890 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006891 PyObject *item = PySequence_GetItem(seq, i);
6892 if (item == NULL)
6893 goto fail;
6894 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6895 Py_DECREF(item);
6896 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006897 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006898 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006899 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006900 blen = (*buf)[i].len;
6901 (*iov)[i].iov_len = blen;
6902 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006903 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006904 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006905
6906fail:
6907 PyMem_Del(*iov);
6908 for (j = 0; j < i; j++) {
6909 PyBuffer_Release(&(*buf)[j]);
6910 }
6911 PyMem_Del(*buf);
6912 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006913}
6914
6915static void
6916iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6917{
6918 int i;
6919 PyMem_Del(iov);
6920 for (i = 0; i < cnt; i++) {
6921 PyBuffer_Release(&buf[i]);
6922 }
6923 PyMem_Del(buf);
6924}
6925#endif
6926
Ross Lagerwall7807c352011-03-17 20:20:30 +02006927#ifdef HAVE_READV
6928PyDoc_STRVAR(posix_readv__doc__,
6929"readv(fd, buffers) -> bytesread\n\n\
6930Read from a file descriptor into a number of writable buffers. buffers\n\
6931is an arbitrary sequence of writable buffers.\n\
6932Returns the total number of bytes read.");
6933
6934static PyObject *
6935posix_readv(PyObject *self, PyObject *args)
6936{
6937 int fd, cnt;
6938 Py_ssize_t n;
6939 PyObject *seq;
6940 struct iovec *iov;
6941 Py_buffer *buf;
6942
6943 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6944 return NULL;
6945 if (!PySequence_Check(seq)) {
6946 PyErr_SetString(PyExc_TypeError,
6947 "readv() arg 2 must be a sequence");
6948 return NULL;
6949 }
6950 cnt = PySequence_Size(seq);
6951
6952 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6953 return NULL;
6954
6955 Py_BEGIN_ALLOW_THREADS
6956 n = readv(fd, iov, cnt);
6957 Py_END_ALLOW_THREADS
6958
6959 iov_cleanup(iov, buf, cnt);
6960 return PyLong_FromSsize_t(n);
6961}
6962#endif
6963
6964#ifdef HAVE_PREAD
6965PyDoc_STRVAR(posix_pread__doc__,
6966"pread(fd, buffersize, offset) -> string\n\n\
6967Read from a file descriptor, fd, at a position of offset. It will read up\n\
6968to buffersize number of bytes. The file offset remains unchanged.");
6969
6970static PyObject *
6971posix_pread(PyObject *self, PyObject *args)
6972{
6973 int fd, size;
6974 off_t offset;
6975 Py_ssize_t n;
6976 PyObject *buffer;
6977 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6978 return NULL;
6979
6980 if (size < 0) {
6981 errno = EINVAL;
6982 return posix_error();
6983 }
6984 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6985 if (buffer == NULL)
6986 return NULL;
6987 if (!_PyVerify_fd(fd)) {
6988 Py_DECREF(buffer);
6989 return posix_error();
6990 }
6991 Py_BEGIN_ALLOW_THREADS
6992 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6993 Py_END_ALLOW_THREADS
6994 if (n < 0) {
6995 Py_DECREF(buffer);
6996 return posix_error();
6997 }
6998 if (n != size)
6999 _PyBytes_Resize(&buffer, n);
7000 return buffer;
7001}
7002#endif
7003
7004PyDoc_STRVAR(posix_write__doc__,
7005"write(fd, string) -> byteswritten\n\n\
7006Write a string to a file descriptor.");
7007
7008static PyObject *
7009posix_write(PyObject *self, PyObject *args)
7010{
7011 Py_buffer pbuf;
7012 int fd;
7013 Py_ssize_t size, len;
7014
7015 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7016 return NULL;
7017 if (!_PyVerify_fd(fd)) {
7018 PyBuffer_Release(&pbuf);
7019 return posix_error();
7020 }
7021 len = pbuf.len;
7022 Py_BEGIN_ALLOW_THREADS
7023#if defined(MS_WIN64) || defined(MS_WINDOWS)
7024 if (len > INT_MAX)
7025 len = INT_MAX;
7026 size = write(fd, pbuf.buf, (int)len);
7027#else
7028 size = write(fd, pbuf.buf, len);
7029#endif
7030 Py_END_ALLOW_THREADS
7031 PyBuffer_Release(&pbuf);
7032 if (size < 0)
7033 return posix_error();
7034 return PyLong_FromSsize_t(size);
7035}
7036
7037#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007038PyDoc_STRVAR(posix_sendfile__doc__,
7039"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7040sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7041 -> byteswritten\n\
7042Copy nbytes bytes from file descriptor in to file descriptor out.");
7043
7044static PyObject *
7045posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7046{
7047 int in, out;
7048 Py_ssize_t ret;
7049 off_t offset;
7050
7051#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7052#ifndef __APPLE__
7053 Py_ssize_t len;
7054#endif
7055 PyObject *headers = NULL, *trailers = NULL;
7056 Py_buffer *hbuf, *tbuf;
7057 off_t sbytes;
7058 struct sf_hdtr sf;
7059 int flags = 0;
7060 sf.headers = NULL;
7061 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007062 static char *keywords[] = {"out", "in",
7063 "offset", "count",
7064 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007065
7066#ifdef __APPLE__
7067 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007068 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007069#else
7070 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007071 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007072#endif
7073 &headers, &trailers, &flags))
7074 return NULL;
7075 if (headers != NULL) {
7076 if (!PySequence_Check(headers)) {
7077 PyErr_SetString(PyExc_TypeError,
7078 "sendfile() headers must be a sequence or None");
7079 return NULL;
7080 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007081 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007082 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007083 if (sf.hdr_cnt > 0 &&
7084 !(i = iov_setup(&(sf.headers), &hbuf,
7085 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007086 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007087#ifdef __APPLE__
7088 sbytes += i;
7089#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007090 }
7091 }
7092 if (trailers != NULL) {
7093 if (!PySequence_Check(trailers)) {
7094 PyErr_SetString(PyExc_TypeError,
7095 "sendfile() trailers must be a sequence or None");
7096 return NULL;
7097 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007098 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007099 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007100 if (sf.trl_cnt > 0 &&
7101 !(i = iov_setup(&(sf.trailers), &tbuf,
7102 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007103 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007104#ifdef __APPLE__
7105 sbytes += i;
7106#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007107 }
7108 }
7109
7110 Py_BEGIN_ALLOW_THREADS
7111#ifdef __APPLE__
7112 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7113#else
7114 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7115#endif
7116 Py_END_ALLOW_THREADS
7117
7118 if (sf.headers != NULL)
7119 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7120 if (sf.trailers != NULL)
7121 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7122
7123 if (ret < 0) {
7124 if ((errno == EAGAIN) || (errno == EBUSY)) {
7125 if (sbytes != 0) {
7126 // some data has been sent
7127 goto done;
7128 }
7129 else {
7130 // no data has been sent; upper application is supposed
7131 // to retry on EAGAIN or EBUSY
7132 return posix_error();
7133 }
7134 }
7135 return posix_error();
7136 }
7137 goto done;
7138
7139done:
7140 #if !defined(HAVE_LARGEFILE_SUPPORT)
7141 return Py_BuildValue("l", sbytes);
7142 #else
7143 return Py_BuildValue("L", sbytes);
7144 #endif
7145
7146#else
7147 Py_ssize_t count;
7148 PyObject *offobj;
7149 static char *keywords[] = {"out", "in",
7150 "offset", "count", NULL};
7151 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7152 keywords, &out, &in, &offobj, &count))
7153 return NULL;
7154#ifdef linux
7155 if (offobj == Py_None) {
7156 Py_BEGIN_ALLOW_THREADS
7157 ret = sendfile(out, in, NULL, count);
7158 Py_END_ALLOW_THREADS
7159 if (ret < 0)
7160 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007161 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007162 }
7163#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007164 if (!_parse_off_t(offobj, &offset))
7165 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007166 Py_BEGIN_ALLOW_THREADS
7167 ret = sendfile(out, in, &offset, count);
7168 Py_END_ALLOW_THREADS
7169 if (ret < 0)
7170 return posix_error();
7171 return Py_BuildValue("n", ret);
7172#endif
7173}
7174#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007176PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007177"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007178Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007179
Barry Warsaw53699e91996-12-10 23:23:01 +00007180static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007181posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007182{
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 int fd;
7184 STRUCT_STAT st;
7185 int res;
7186 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7187 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007188#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 /* on OpenVMS we must ensure that all bytes are written to the file */
7190 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007191#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 if (!_PyVerify_fd(fd))
7193 return posix_error();
7194 Py_BEGIN_ALLOW_THREADS
7195 res = FSTAT(fd, &st);
7196 Py_END_ALLOW_THREADS
7197 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007198#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007199 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007200#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007202#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 }
Tim Peters5aa91602002-01-30 05:46:57 +00007204
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007206}
7207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007208PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007209"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007210Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007211connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007212
7213static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007214posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007215{
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 int fd;
7217 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7218 return NULL;
7219 if (!_PyVerify_fd(fd))
7220 return PyBool_FromLong(0);
7221 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007222}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007223
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007224#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007225PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007226"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007227Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007228
Barry Warsaw53699e91996-12-10 23:23:01 +00007229static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007230posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007231{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007232#if defined(PYOS_OS2)
7233 HFILE read, write;
7234 APIRET rc;
7235
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007236 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007237 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007238 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007239
7240 return Py_BuildValue("(ii)", read, write);
7241#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007242#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 int fds[2];
7244 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 if (res != 0)
7247 return posix_error();
7248 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007249#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007250 HANDLE read, write;
7251 int read_fd, write_fd;
7252 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 if (!ok)
7255 return win32_error("CreatePipe", NULL);
7256 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7257 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7258 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007259#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007260#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007261}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007262#endif /* HAVE_PIPE */
7263
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007264#ifdef HAVE_PIPE2
7265PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007266"pipe2(flags) -> (read_end, write_end)\n\n\
7267Create a pipe with flags set atomically.\n\
7268flags can be constructed by ORing together one or more of these values:\n\
7269O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007270");
7271
7272static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007273posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007274{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007275 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007276 int fds[2];
7277 int res;
7278
Charles-François Natali368f34b2011-06-06 19:49:47 +02007279 flags = PyLong_AsLong(arg);
7280 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007281 return NULL;
7282
7283 res = pipe2(fds, flags);
7284 if (res != 0)
7285 return posix_error();
7286 return Py_BuildValue("(ii)", fds[0], fds[1]);
7287}
7288#endif /* HAVE_PIPE2 */
7289
Ross Lagerwall7807c352011-03-17 20:20:30 +02007290#ifdef HAVE_WRITEV
7291PyDoc_STRVAR(posix_writev__doc__,
7292"writev(fd, buffers) -> byteswritten\n\n\
7293Write the contents of buffers to a file descriptor, where buffers is an\n\
7294arbitrary sequence of buffers.\n\
7295Returns the total bytes written.");
7296
7297static PyObject *
7298posix_writev(PyObject *self, PyObject *args)
7299{
7300 int fd, cnt;
7301 Py_ssize_t res;
7302 PyObject *seq;
7303 struct iovec *iov;
7304 Py_buffer *buf;
7305 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7306 return NULL;
7307 if (!PySequence_Check(seq)) {
7308 PyErr_SetString(PyExc_TypeError,
7309 "writev() arg 2 must be a sequence");
7310 return NULL;
7311 }
7312 cnt = PySequence_Size(seq);
7313
7314 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7315 return NULL;
7316 }
7317
7318 Py_BEGIN_ALLOW_THREADS
7319 res = writev(fd, iov, cnt);
7320 Py_END_ALLOW_THREADS
7321
7322 iov_cleanup(iov, buf, cnt);
7323 return PyLong_FromSsize_t(res);
7324}
7325#endif
7326
7327#ifdef HAVE_PWRITE
7328PyDoc_STRVAR(posix_pwrite__doc__,
7329"pwrite(fd, string, offset) -> byteswritten\n\n\
7330Write string to a file descriptor, fd, from offset, leaving the file\n\
7331offset unchanged.");
7332
7333static PyObject *
7334posix_pwrite(PyObject *self, PyObject *args)
7335{
7336 Py_buffer pbuf;
7337 int fd;
7338 off_t offset;
7339 Py_ssize_t size;
7340
7341 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7342 return NULL;
7343
7344 if (!_PyVerify_fd(fd)) {
7345 PyBuffer_Release(&pbuf);
7346 return posix_error();
7347 }
7348 Py_BEGIN_ALLOW_THREADS
7349 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7350 Py_END_ALLOW_THREADS
7351 PyBuffer_Release(&pbuf);
7352 if (size < 0)
7353 return posix_error();
7354 return PyLong_FromSsize_t(size);
7355}
7356#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007357
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007358#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007359PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007360"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007361Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007362
Barry Warsaw53699e91996-12-10 23:23:01 +00007363static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007364posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007365{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007366 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 char *filename;
7368 int mode = 0666;
7369 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007370 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7371 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007372 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007373 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 Py_BEGIN_ALLOW_THREADS
7375 res = mkfifo(filename, mode);
7376 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007377 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 if (res < 0)
7379 return posix_error();
7380 Py_INCREF(Py_None);
7381 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007382}
7383#endif
7384
7385
Neal Norwitz11690112002-07-30 01:08:28 +00007386#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007387PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007388"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007389Create a filesystem node (file, device special file or named pipe)\n\
7390named filename. mode specifies both the permissions to use and the\n\
7391type of node to be created, being combined (bitwise OR) with one of\n\
7392S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007393device defines the newly created device special file (probably using\n\
7394os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007395
7396
7397static PyObject *
7398posix_mknod(PyObject *self, PyObject *args)
7399{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007400 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007401 char *filename;
7402 int mode = 0600;
7403 int device = 0;
7404 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007405 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7406 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007407 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007408 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007409 Py_BEGIN_ALLOW_THREADS
7410 res = mknod(filename, mode, device);
7411 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007412 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007413 if (res < 0)
7414 return posix_error();
7415 Py_INCREF(Py_None);
7416 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007417}
7418#endif
7419
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007420#ifdef HAVE_DEVICE_MACROS
7421PyDoc_STRVAR(posix_major__doc__,
7422"major(device) -> major number\n\
7423Extracts a device major number from a raw device number.");
7424
7425static PyObject *
7426posix_major(PyObject *self, PyObject *args)
7427{
Victor Stinner8c62be82010-05-06 00:08:46 +00007428 int device;
7429 if (!PyArg_ParseTuple(args, "i:major", &device))
7430 return NULL;
7431 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007432}
7433
7434PyDoc_STRVAR(posix_minor__doc__,
7435"minor(device) -> minor number\n\
7436Extracts a device minor number from a raw device number.");
7437
7438static PyObject *
7439posix_minor(PyObject *self, PyObject *args)
7440{
Victor Stinner8c62be82010-05-06 00:08:46 +00007441 int device;
7442 if (!PyArg_ParseTuple(args, "i:minor", &device))
7443 return NULL;
7444 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007445}
7446
7447PyDoc_STRVAR(posix_makedev__doc__,
7448"makedev(major, minor) -> device number\n\
7449Composes a raw device number from the major and minor device numbers.");
7450
7451static PyObject *
7452posix_makedev(PyObject *self, PyObject *args)
7453{
Victor Stinner8c62be82010-05-06 00:08:46 +00007454 int major, minor;
7455 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7456 return NULL;
7457 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007458}
7459#endif /* device macros */
7460
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007461
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007462#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007463PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007464"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007465Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007466
Barry Warsaw53699e91996-12-10 23:23:01 +00007467static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007468posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007469{
Victor Stinner8c62be82010-05-06 00:08:46 +00007470 int fd;
7471 off_t length;
7472 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007473
Ross Lagerwall7807c352011-03-17 20:20:30 +02007474 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007475 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007476
Victor Stinner8c62be82010-05-06 00:08:46 +00007477 Py_BEGIN_ALLOW_THREADS
7478 res = ftruncate(fd, length);
7479 Py_END_ALLOW_THREADS
7480 if (res < 0)
7481 return posix_error();
7482 Py_INCREF(Py_None);
7483 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007484}
7485#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007486
Ross Lagerwall7807c352011-03-17 20:20:30 +02007487#ifdef HAVE_TRUNCATE
7488PyDoc_STRVAR(posix_truncate__doc__,
7489"truncate(path, length)\n\n\
7490Truncate the file given by path to length bytes.");
7491
7492static PyObject *
7493posix_truncate(PyObject *self, PyObject *args)
7494{
7495 PyObject *opath;
7496 const char *path;
7497 off_t length;
7498 int res;
7499
7500 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7501 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7502 return NULL;
7503 path = PyBytes_AsString(opath);
7504
7505 Py_BEGIN_ALLOW_THREADS
7506 res = truncate(path, length);
7507 Py_END_ALLOW_THREADS
7508 Py_DECREF(opath);
7509 if (res < 0)
7510 return posix_error();
7511 Py_RETURN_NONE;
7512}
7513#endif
7514
7515#ifdef HAVE_POSIX_FALLOCATE
7516PyDoc_STRVAR(posix_posix_fallocate__doc__,
7517"posix_fallocate(fd, offset, len)\n\n\
7518Ensures that enough disk space is allocated for the file specified by fd\n\
7519starting from offset and continuing for len bytes.");
7520
7521static PyObject *
7522posix_posix_fallocate(PyObject *self, PyObject *args)
7523{
7524 off_t len, offset;
7525 int res, fd;
7526
7527 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7528 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7529 return NULL;
7530
7531 Py_BEGIN_ALLOW_THREADS
7532 res = posix_fallocate(fd, offset, len);
7533 Py_END_ALLOW_THREADS
7534 if (res != 0) {
7535 errno = res;
7536 return posix_error();
7537 }
7538 Py_RETURN_NONE;
7539}
7540#endif
7541
7542#ifdef HAVE_POSIX_FADVISE
7543PyDoc_STRVAR(posix_posix_fadvise__doc__,
7544"posix_fadvise(fd, offset, len, advice)\n\n\
7545Announces an intention to access data in a specific pattern thus allowing\n\
7546the kernel to make optimizations.\n\
7547The advice applies to the region of the file specified by fd starting at\n\
7548offset and continuing for len bytes.\n\
7549advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7550POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7551POSIX_FADV_DONTNEED.");
7552
7553static PyObject *
7554posix_posix_fadvise(PyObject *self, PyObject *args)
7555{
7556 off_t len, offset;
7557 int res, fd, advice;
7558
7559 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7560 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7561 return NULL;
7562
7563 Py_BEGIN_ALLOW_THREADS
7564 res = posix_fadvise(fd, offset, len, advice);
7565 Py_END_ALLOW_THREADS
7566 if (res != 0) {
7567 errno = res;
7568 return posix_error();
7569 }
7570 Py_RETURN_NONE;
7571}
7572#endif
7573
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007574#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007575PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007576"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007577Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007578
Fred Drake762e2061999-08-26 17:23:54 +00007579/* Save putenv() parameters as values here, so we can collect them when they
7580 * get re-set with another call for the same key. */
7581static PyObject *posix_putenv_garbage;
7582
Tim Peters5aa91602002-01-30 05:46:57 +00007583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007584posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007585{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007586#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007587 wchar_t *s1, *s2;
7588 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007589#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007590 PyObject *os1, *os2;
7591 char *s1, *s2;
7592 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007593#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007594 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007596
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007597#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 if (!PyArg_ParseTuple(args,
7599 "uu:putenv",
7600 &s1, &s2))
7601 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007602#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007603 if (!PyArg_ParseTuple(args,
7604 "O&O&:putenv",
7605 PyUnicode_FSConverter, &os1,
7606 PyUnicode_FSConverter, &os2))
7607 return NULL;
7608 s1 = PyBytes_AsString(os1);
7609 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007610#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007611
7612#if defined(PYOS_OS2)
7613 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7614 APIRET rc;
7615
Guido van Rossumd48f2521997-12-05 22:19:34 +00007616 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007617 if (rc != NO_ERROR) {
7618 os2_error(rc);
7619 goto error;
7620 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007621
7622 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7623 APIRET rc;
7624
Guido van Rossumd48f2521997-12-05 22:19:34 +00007625 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007626 if (rc != NO_ERROR) {
7627 os2_error(rc);
7628 goto error;
7629 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007630 } else {
7631#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 /* XXX This can leak memory -- not easy to fix :-( */
7633 /* len includes space for a trailing \0; the size arg to
7634 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007635#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007636 len = wcslen(s1) + wcslen(s2) + 2;
7637 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007638#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007639 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007641#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007642 if (newstr == NULL) {
7643 PyErr_NoMemory();
7644 goto error;
7645 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007646#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007647 newenv = PyUnicode_AsUnicode(newstr);
7648 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7649 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007650 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007651 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007652 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007653#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007654 newenv = PyBytes_AS_STRING(newstr);
7655 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7656 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007657 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007658 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007659 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007660#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007661
Victor Stinner8c62be82010-05-06 00:08:46 +00007662 /* Install the first arg and newstr in posix_putenv_garbage;
7663 * this will cause previous value to be collected. This has to
7664 * happen after the real putenv() call because the old value
7665 * was still accessible until then. */
7666 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007667#ifdef MS_WINDOWS
7668 PyTuple_GET_ITEM(args, 0),
7669#else
7670 os1,
7671#endif
7672 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007673 /* really not much we can do; just leak */
7674 PyErr_Clear();
7675 }
7676 else {
7677 Py_DECREF(newstr);
7678 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007679
7680#if defined(PYOS_OS2)
7681 }
7682#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007683
Martin v. Löwis011e8422009-05-05 04:43:17 +00007684#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007685 Py_DECREF(os1);
7686 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007687#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007688 Py_RETURN_NONE;
7689
7690error:
7691#ifndef MS_WINDOWS
7692 Py_DECREF(os1);
7693 Py_DECREF(os2);
7694#endif
7695 Py_XDECREF(newstr);
7696 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007697}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007698#endif /* putenv */
7699
Guido van Rossumc524d952001-10-19 01:31:59 +00007700#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007701PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007702"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007703Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007704
7705static PyObject *
7706posix_unsetenv(PyObject *self, PyObject *args)
7707{
Victor Stinner84ae1182010-05-06 22:05:07 +00007708#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007709 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007710
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7712 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007713#else
7714 PyObject *os1;
7715 char *s1;
7716
7717 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7718 PyUnicode_FSConverter, &os1))
7719 return NULL;
7720 s1 = PyBytes_AsString(os1);
7721#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007722
Victor Stinner8c62be82010-05-06 00:08:46 +00007723 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007724
Victor Stinner8c62be82010-05-06 00:08:46 +00007725 /* Remove the key from posix_putenv_garbage;
7726 * this will cause it to be collected. This has to
7727 * happen after the real unsetenv() call because the
7728 * old value was still accessible until then.
7729 */
7730 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007731#ifdef MS_WINDOWS
7732 PyTuple_GET_ITEM(args, 0)
7733#else
7734 os1
7735#endif
7736 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007737 /* really not much we can do; just leak */
7738 PyErr_Clear();
7739 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007740
Victor Stinner84ae1182010-05-06 22:05:07 +00007741#ifndef MS_WINDOWS
7742 Py_DECREF(os1);
7743#endif
7744 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007745}
7746#endif /* unsetenv */
7747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007748PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007749"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007750Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007751
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007753posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007754{
Victor Stinner8c62be82010-05-06 00:08:46 +00007755 int code;
7756 char *message;
7757 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7758 return NULL;
7759 message = strerror(code);
7760 if (message == NULL) {
7761 PyErr_SetString(PyExc_ValueError,
7762 "strerror() argument out of range");
7763 return NULL;
7764 }
7765 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007766}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007767
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007768
Guido van Rossumc9641791998-08-04 15:26:23 +00007769#ifdef HAVE_SYS_WAIT_H
7770
Fred Drake106c1a02002-04-23 15:58:02 +00007771#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007772PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007773"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007774Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007775
7776static PyObject *
7777posix_WCOREDUMP(PyObject *self, PyObject *args)
7778{
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 WAIT_TYPE status;
7780 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007781
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7783 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007784
Victor Stinner8c62be82010-05-06 00:08:46 +00007785 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007786}
7787#endif /* WCOREDUMP */
7788
7789#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007790PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007791"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007792Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007793job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007794
7795static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007796posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007797{
Victor Stinner8c62be82010-05-06 00:08:46 +00007798 WAIT_TYPE status;
7799 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007800
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7802 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007803
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007805}
7806#endif /* WIFCONTINUED */
7807
Guido van Rossumc9641791998-08-04 15:26:23 +00007808#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007809PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007810"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007811Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007812
7813static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007814posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007815{
Victor Stinner8c62be82010-05-06 00:08:46 +00007816 WAIT_TYPE status;
7817 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007818
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7820 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007821
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007823}
7824#endif /* WIFSTOPPED */
7825
7826#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007827PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007828"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007829Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007830
7831static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007832posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007833{
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 WAIT_TYPE status;
7835 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007836
Victor Stinner8c62be82010-05-06 00:08:46 +00007837 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7838 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007839
Victor Stinner8c62be82010-05-06 00:08:46 +00007840 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007841}
7842#endif /* WIFSIGNALED */
7843
7844#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007845PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007846"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007847Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007848system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007849
7850static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007851posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007852{
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 WAIT_TYPE status;
7854 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007855
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7857 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007858
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007860}
7861#endif /* WIFEXITED */
7862
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007863#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007864PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007865"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007866Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007867
7868static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007869posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007870{
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 WAIT_TYPE status;
7872 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007873
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7875 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007876
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007878}
7879#endif /* WEXITSTATUS */
7880
7881#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007882PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007883"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007884Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007885value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007886
7887static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007888posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007889{
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 WAIT_TYPE status;
7891 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007892
Victor Stinner8c62be82010-05-06 00:08:46 +00007893 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7894 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007895
Victor Stinner8c62be82010-05-06 00:08:46 +00007896 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007897}
7898#endif /* WTERMSIG */
7899
7900#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007901PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007902"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007903Return the signal that stopped the process that provided\n\
7904the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007905
7906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007907posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007908{
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 WAIT_TYPE status;
7910 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007911
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7913 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007914
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007916}
7917#endif /* WSTOPSIG */
7918
7919#endif /* HAVE_SYS_WAIT_H */
7920
7921
Thomas Wouters477c8d52006-05-27 19:21:47 +00007922#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007923#ifdef _SCO_DS
7924/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7925 needed definitions in sys/statvfs.h */
7926#define _SVID3
7927#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007928#include <sys/statvfs.h>
7929
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007930static PyObject*
7931_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007932 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7933 if (v == NULL)
7934 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007935
7936#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7938 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7939 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7940 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7941 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7942 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7943 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7944 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7945 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7946 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007947#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7949 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7950 PyStructSequence_SET_ITEM(v, 2,
7951 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7952 PyStructSequence_SET_ITEM(v, 3,
7953 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7954 PyStructSequence_SET_ITEM(v, 4,
7955 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7956 PyStructSequence_SET_ITEM(v, 5,
7957 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7958 PyStructSequence_SET_ITEM(v, 6,
7959 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7960 PyStructSequence_SET_ITEM(v, 7,
7961 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7962 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7963 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007964#endif
7965
Victor Stinner8c62be82010-05-06 00:08:46 +00007966 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007967}
7968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007969PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007970"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007971Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007972
7973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007974posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007975{
Victor Stinner8c62be82010-05-06 00:08:46 +00007976 int fd, res;
7977 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007978
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7980 return NULL;
7981 Py_BEGIN_ALLOW_THREADS
7982 res = fstatvfs(fd, &st);
7983 Py_END_ALLOW_THREADS
7984 if (res != 0)
7985 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007986
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007988}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007989#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007990
7991
Thomas Wouters477c8d52006-05-27 19:21:47 +00007992#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007993#include <sys/statvfs.h>
7994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007995PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007996"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007997Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007998
7999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008000posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008001{
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 char *path;
8003 int res;
8004 struct statvfs st;
8005 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
8006 return NULL;
8007 Py_BEGIN_ALLOW_THREADS
8008 res = statvfs(path, &st);
8009 Py_END_ALLOW_THREADS
8010 if (res != 0)
8011 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008012
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008014}
8015#endif /* HAVE_STATVFS */
8016
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008017#ifdef MS_WINDOWS
8018PyDoc_STRVAR(win32__getdiskusage__doc__,
8019"_getdiskusage(path) -> (total, free)\n\n\
8020Return disk usage statistics about the given path as (total, free) tuple.");
8021
8022static PyObject *
8023win32__getdiskusage(PyObject *self, PyObject *args)
8024{
8025 BOOL retval;
8026 ULARGE_INTEGER _, total, free;
8027 LPCTSTR path;
8028
8029 if (! PyArg_ParseTuple(args, "s", &path))
8030 return NULL;
8031
8032 Py_BEGIN_ALLOW_THREADS
8033 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8034 Py_END_ALLOW_THREADS
8035 if (retval == 0)
8036 return PyErr_SetFromWindowsErr(0);
8037
8038 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8039}
8040#endif
8041
8042
Fred Drakec9680921999-12-13 16:37:25 +00008043/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8044 * It maps strings representing configuration variable names to
8045 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008046 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008047 * rarely-used constants. There are three separate tables that use
8048 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008049 *
8050 * This code is always included, even if none of the interfaces that
8051 * need it are included. The #if hackery needed to avoid it would be
8052 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008053 */
8054struct constdef {
8055 char *name;
8056 long value;
8057};
8058
Fred Drake12c6e2d1999-12-14 21:25:03 +00008059static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008060conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008061 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008062{
Christian Heimes217cfd12007-12-02 14:31:20 +00008063 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008064 *valuep = PyLong_AS_LONG(arg);
8065 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008066 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008067 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008068 /* look up the value in the table using a binary search */
8069 size_t lo = 0;
8070 size_t mid;
8071 size_t hi = tablesize;
8072 int cmp;
8073 const char *confname;
8074 if (!PyUnicode_Check(arg)) {
8075 PyErr_SetString(PyExc_TypeError,
8076 "configuration names must be strings or integers");
8077 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008079 confname = _PyUnicode_AsString(arg);
8080 if (confname == NULL)
8081 return 0;
8082 while (lo < hi) {
8083 mid = (lo + hi) / 2;
8084 cmp = strcmp(confname, table[mid].name);
8085 if (cmp < 0)
8086 hi = mid;
8087 else if (cmp > 0)
8088 lo = mid + 1;
8089 else {
8090 *valuep = table[mid].value;
8091 return 1;
8092 }
8093 }
8094 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8095 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008096 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008097}
8098
8099
8100#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8101static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008102#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008104#endif
8105#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008107#endif
Fred Drakec9680921999-12-13 16:37:25 +00008108#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008110#endif
8111#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008113#endif
8114#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008116#endif
8117#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008119#endif
8120#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008121 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008122#endif
8123#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008124 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008125#endif
8126#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008127 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008128#endif
8129#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008131#endif
8132#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008134#endif
8135#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008137#endif
8138#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008139 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008140#endif
8141#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008143#endif
8144#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008145 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008146#endif
8147#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008148 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008149#endif
8150#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008151 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008152#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008153#ifdef _PC_ACL_ENABLED
8154 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8155#endif
8156#ifdef _PC_MIN_HOLE_SIZE
8157 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8158#endif
8159#ifdef _PC_ALLOC_SIZE_MIN
8160 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8161#endif
8162#ifdef _PC_REC_INCR_XFER_SIZE
8163 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8164#endif
8165#ifdef _PC_REC_MAX_XFER_SIZE
8166 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8167#endif
8168#ifdef _PC_REC_MIN_XFER_SIZE
8169 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8170#endif
8171#ifdef _PC_REC_XFER_ALIGN
8172 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8173#endif
8174#ifdef _PC_SYMLINK_MAX
8175 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8176#endif
8177#ifdef _PC_XATTR_ENABLED
8178 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8179#endif
8180#ifdef _PC_XATTR_EXISTS
8181 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8182#endif
8183#ifdef _PC_TIMESTAMP_RESOLUTION
8184 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8185#endif
Fred Drakec9680921999-12-13 16:37:25 +00008186};
8187
Fred Drakec9680921999-12-13 16:37:25 +00008188static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008189conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008190{
8191 return conv_confname(arg, valuep, posix_constants_pathconf,
8192 sizeof(posix_constants_pathconf)
8193 / sizeof(struct constdef));
8194}
8195#endif
8196
8197#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008198PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008199"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008200Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008201If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008202
8203static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008204posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008205{
8206 PyObject *result = NULL;
8207 int name, fd;
8208
Fred Drake12c6e2d1999-12-14 21:25:03 +00008209 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8210 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008211 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008212
Stefan Krah0e803b32010-11-26 16:16:47 +00008213 errno = 0;
8214 limit = fpathconf(fd, name);
8215 if (limit == -1 && errno != 0)
8216 posix_error();
8217 else
8218 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008219 }
8220 return result;
8221}
8222#endif
8223
8224
8225#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008226PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008227"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008228Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008229If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008230
8231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008232posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008233{
8234 PyObject *result = NULL;
8235 int name;
8236 char *path;
8237
8238 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8239 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008240 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008241
Victor Stinner8c62be82010-05-06 00:08:46 +00008242 errno = 0;
8243 limit = pathconf(path, name);
8244 if (limit == -1 && errno != 0) {
8245 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008246 /* could be a path or name problem */
8247 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008248 else
Stefan Krah99439262010-11-26 12:58:05 +00008249 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 }
8251 else
8252 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008253 }
8254 return result;
8255}
8256#endif
8257
8258#ifdef HAVE_CONFSTR
8259static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008260#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008262#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008263#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008265#endif
8266#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008267 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008268#endif
Fred Draked86ed291999-12-15 15:34:33 +00008269#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008271#endif
8272#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008274#endif
8275#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008277#endif
8278#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008280#endif
Fred Drakec9680921999-12-13 16:37:25 +00008281#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008282 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008283#endif
8284#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008286#endif
8287#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008288 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008289#endif
8290#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008291 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008292#endif
8293#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008294 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008295#endif
8296#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008297 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008298#endif
8299#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008300 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008301#endif
8302#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008303 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008304#endif
Fred Draked86ed291999-12-15 15:34:33 +00008305#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008306 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008307#endif
Fred Drakec9680921999-12-13 16:37:25 +00008308#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008309 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008310#endif
Fred Draked86ed291999-12-15 15:34:33 +00008311#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008312 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008313#endif
8314#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008315 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008316#endif
8317#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008318 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008319#endif
8320#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008321 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008322#endif
Fred Drakec9680921999-12-13 16:37:25 +00008323#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008324 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008325#endif
8326#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008327 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008328#endif
8329#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008330 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008331#endif
8332#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008334#endif
8335#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008336 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008337#endif
8338#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008340#endif
8341#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008343#endif
8344#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008346#endif
8347#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008349#endif
8350#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008351 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008352#endif
8353#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008354 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008355#endif
8356#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008357 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008358#endif
8359#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008360 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008361#endif
8362#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008363 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008364#endif
8365#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008366 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008367#endif
8368#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008370#endif
Fred Draked86ed291999-12-15 15:34:33 +00008371#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008372 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008373#endif
8374#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008376#endif
8377#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008379#endif
8380#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008381 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008382#endif
8383#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008384 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008385#endif
8386#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008388#endif
8389#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008390 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008391#endif
8392#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008393 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008394#endif
8395#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008397#endif
8398#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008399 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008400#endif
8401#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008403#endif
8404#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008405 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008406#endif
8407#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008408 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008409#endif
Fred Drakec9680921999-12-13 16:37:25 +00008410};
8411
8412static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008413conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008414{
8415 return conv_confname(arg, valuep, posix_constants_confstr,
8416 sizeof(posix_constants_confstr)
8417 / sizeof(struct constdef));
8418}
8419
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008420PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008421"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008422Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008423
8424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008425posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008426{
8427 PyObject *result = NULL;
8428 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008429 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008430 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008431
Victor Stinnercb043522010-09-10 23:49:04 +00008432 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8433 return NULL;
8434
8435 errno = 0;
8436 len = confstr(name, buffer, sizeof(buffer));
8437 if (len == 0) {
8438 if (errno) {
8439 posix_error();
8440 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008441 }
8442 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008443 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008444 }
8445 }
Victor Stinnercb043522010-09-10 23:49:04 +00008446
8447 if ((unsigned int)len >= sizeof(buffer)) {
8448 char *buf = PyMem_Malloc(len);
8449 if (buf == NULL)
8450 return PyErr_NoMemory();
8451 confstr(name, buf, len);
8452 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8453 PyMem_Free(buf);
8454 }
8455 else
8456 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008457 return result;
8458}
8459#endif
8460
8461
8462#ifdef HAVE_SYSCONF
8463static struct constdef posix_constants_sysconf[] = {
8464#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008466#endif
8467#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008468 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008469#endif
8470#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008471 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008472#endif
8473#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008475#endif
8476#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008477 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008478#endif
8479#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008480 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008481#endif
8482#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008483 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008484#endif
8485#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008486 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008487#endif
8488#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008490#endif
8491#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008493#endif
Fred Draked86ed291999-12-15 15:34:33 +00008494#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008495 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008496#endif
8497#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008498 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008499#endif
Fred Drakec9680921999-12-13 16:37:25 +00008500#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008501 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008502#endif
Fred Drakec9680921999-12-13 16:37:25 +00008503#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008504 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008505#endif
8506#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008507 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008508#endif
8509#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008510 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008511#endif
8512#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008513 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008514#endif
8515#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008516 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008517#endif
Fred Draked86ed291999-12-15 15:34:33 +00008518#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008519 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008520#endif
Fred Drakec9680921999-12-13 16:37:25 +00008521#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008522 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008523#endif
8524#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008525 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008526#endif
8527#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008528 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008529#endif
8530#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008531 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008532#endif
8533#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008534 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008535#endif
Fred Draked86ed291999-12-15 15:34:33 +00008536#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008537 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008538#endif
Fred Drakec9680921999-12-13 16:37:25 +00008539#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008540 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008541#endif
8542#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008543 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008544#endif
8545#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008546 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008547#endif
8548#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008549 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008550#endif
8551#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008552 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008553#endif
8554#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008555 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008556#endif
8557#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008558 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008559#endif
8560#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008561 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008562#endif
8563#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008564 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008565#endif
8566#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008567 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008568#endif
8569#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008570 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008571#endif
8572#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008573 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008574#endif
8575#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008576 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008577#endif
8578#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008579 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008580#endif
8581#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008582 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008583#endif
8584#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008585 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008586#endif
8587#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008588 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008589#endif
8590#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008591 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008592#endif
8593#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008594 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008595#endif
8596#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008597 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008598#endif
8599#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008600 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008601#endif
8602#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008603 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008604#endif
8605#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008606 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008607#endif
Fred Draked86ed291999-12-15 15:34:33 +00008608#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008609 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008610#endif
Fred Drakec9680921999-12-13 16:37:25 +00008611#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008612 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008613#endif
8614#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008615 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008616#endif
8617#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008618 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008619#endif
Fred Draked86ed291999-12-15 15:34:33 +00008620#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008621 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008622#endif
Fred Drakec9680921999-12-13 16:37:25 +00008623#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008625#endif
Fred Draked86ed291999-12-15 15:34:33 +00008626#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008627 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008628#endif
8629#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008630 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008631#endif
Fred Drakec9680921999-12-13 16:37:25 +00008632#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008633 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008634#endif
8635#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008636 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008637#endif
8638#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008640#endif
8641#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008643#endif
Fred Draked86ed291999-12-15 15:34:33 +00008644#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008645 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008646#endif
Fred Drakec9680921999-12-13 16:37:25 +00008647#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008649#endif
8650#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008651 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008652#endif
8653#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008654 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008655#endif
8656#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008657 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008658#endif
8659#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008660 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008661#endif
8662#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008663 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008664#endif
8665#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008666 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008667#endif
Fred Draked86ed291999-12-15 15:34:33 +00008668#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008670#endif
Fred Drakec9680921999-12-13 16:37:25 +00008671#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008672 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008673#endif
8674#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008675 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008676#endif
Fred Draked86ed291999-12-15 15:34:33 +00008677#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008678 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008679#endif
Fred Drakec9680921999-12-13 16:37:25 +00008680#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008681 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008682#endif
8683#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008684 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008685#endif
8686#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008687 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008688#endif
8689#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008690 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008691#endif
8692#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008693 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008694#endif
8695#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008696 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008697#endif
8698#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008699 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008700#endif
8701#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008702 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008703#endif
8704#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008705 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008706#endif
Fred Draked86ed291999-12-15 15:34:33 +00008707#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008709#endif
8710#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008711 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008712#endif
Fred Drakec9680921999-12-13 16:37:25 +00008713#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008715#endif
8716#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008717 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008718#endif
8719#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008720 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008721#endif
8722#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008723 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008724#endif
8725#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008726 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008727#endif
8728#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008729 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008730#endif
8731#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008732 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008733#endif
8734#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008735 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008736#endif
8737#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008738 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008739#endif
8740#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008741 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008742#endif
8743#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008744 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008745#endif
8746#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008747 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008748#endif
8749#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008750 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008751#endif
8752#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008753 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008754#endif
8755#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008757#endif
8758#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008760#endif
8761#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008762 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008763#endif
8764#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008766#endif
8767#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008768 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008769#endif
8770#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008772#endif
8773#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008775#endif
8776#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008777 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008778#endif
8779#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008780 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008781#endif
8782#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008783 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008784#endif
8785#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008786 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008787#endif
8788#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008789 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008790#endif
8791#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008793#endif
8794#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008796#endif
8797#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008798 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008799#endif
8800#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008802#endif
8803#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008805#endif
8806#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008808#endif
8809#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008811#endif
8812#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008814#endif
8815#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008817#endif
Fred Draked86ed291999-12-15 15:34:33 +00008818#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008820#endif
Fred Drakec9680921999-12-13 16:37:25 +00008821#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
8842#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008844#endif
8845#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008847#endif
8848#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
8851#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008852 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008853#endif
8854#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008855 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008856#endif
8857#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008859#endif
8860#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008861 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008862#endif
8863#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008864 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008865#endif
8866#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008868#endif
8869#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008871#endif
8872#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008874#endif
8875#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008876 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008877#endif
8878#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008879 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008880#endif
8881#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008882 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008883#endif
8884#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008886#endif
8887#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008889#endif
8890#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008892#endif
8893#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008895#endif
8896#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008898#endif
8899#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008901#endif
8902#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008904#endif
8905#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008906 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008907#endif
8908#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008910#endif
8911#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008913#endif
8914#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008915 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008916#endif
8917#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008918 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008919#endif
8920#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008921 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008922#endif
8923#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008924 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008925#endif
8926#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008928#endif
8929#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008930 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008931#endif
8932#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008933 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008934#endif
8935#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008936 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008937#endif
8938#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008939 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008940#endif
8941#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008942 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008943#endif
8944#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008945 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008946#endif
8947#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008948 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008949#endif
8950#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008951 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008952#endif
8953#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008954 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008955#endif
8956};
8957
8958static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008959conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008960{
8961 return conv_confname(arg, valuep, posix_constants_sysconf,
8962 sizeof(posix_constants_sysconf)
8963 / sizeof(struct constdef));
8964}
8965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008966PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008967"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008968Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008969
8970static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008971posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008972{
8973 PyObject *result = NULL;
8974 int name;
8975
8976 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8977 int value;
8978
8979 errno = 0;
8980 value = sysconf(name);
8981 if (value == -1 && errno != 0)
8982 posix_error();
8983 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008984 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008985 }
8986 return result;
8987}
8988#endif
8989
8990
Fred Drakebec628d1999-12-15 18:31:10 +00008991/* This code is used to ensure that the tables of configuration value names
8992 * are in sorted order as required by conv_confname(), and also to build the
8993 * the exported dictionaries that are used to publish information about the
8994 * names available on the host platform.
8995 *
8996 * Sorting the table at runtime ensures that the table is properly ordered
8997 * when used, even for platforms we're not able to test on. It also makes
8998 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008999 */
Fred Drakebec628d1999-12-15 18:31:10 +00009000
9001static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009002cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009003{
9004 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009005 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009006 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009008
9009 return strcmp(c1->name, c2->name);
9010}
9011
9012static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009013setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009014 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009015{
Fred Drakebec628d1999-12-15 18:31:10 +00009016 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009017 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009018
9019 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9020 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009021 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009022 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009023
Barry Warsaw3155db32000-04-13 15:20:40 +00009024 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009025 PyObject *o = PyLong_FromLong(table[i].value);
9026 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9027 Py_XDECREF(o);
9028 Py_DECREF(d);
9029 return -1;
9030 }
9031 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009032 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009033 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009034}
9035
Fred Drakebec628d1999-12-15 18:31:10 +00009036/* Return -1 on failure, 0 on success. */
9037static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009038setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009039{
9040#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009041 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009042 sizeof(posix_constants_pathconf)
9043 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009044 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009045 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009046#endif
9047#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009048 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009049 sizeof(posix_constants_confstr)
9050 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009051 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009052 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009053#endif
9054#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009055 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009056 sizeof(posix_constants_sysconf)
9057 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009058 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009059 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009060#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009061 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009062}
Fred Draked86ed291999-12-15 15:34:33 +00009063
9064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009065PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009066"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009067Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009068in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009069
9070static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009071posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009072{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009073 abort();
9074 /*NOTREACHED*/
9075 Py_FatalError("abort() called from Python code didn't abort!");
9076 return NULL;
9077}
Fred Drakebec628d1999-12-15 18:31:10 +00009078
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009079#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009080PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009081"startfile(filepath [, operation]) - Start a file with its associated\n\
9082application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009083\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009084When \"operation\" is not specified or \"open\", this acts like\n\
9085double-clicking the file in Explorer, or giving the file name as an\n\
9086argument to the DOS \"start\" command: the file is opened with whatever\n\
9087application (if any) its extension is associated.\n\
9088When another \"operation\" is given, it specifies what should be done with\n\
9089the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009090\n\
9091startfile returns as soon as the associated application is launched.\n\
9092There is no option to wait for the application to close, and no way\n\
9093to retrieve the application's exit status.\n\
9094\n\
9095The filepath is relative to the current directory. If you want to use\n\
9096an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009097the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009098
9099static PyObject *
9100win32_startfile(PyObject *self, PyObject *args)
9101{
Victor Stinner8c62be82010-05-06 00:08:46 +00009102 PyObject *ofilepath;
9103 char *filepath;
9104 char *operation = NULL;
9105 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009106
Victor Stinner8c62be82010-05-06 00:08:46 +00009107 PyObject *unipath, *woperation = NULL;
9108 if (!PyArg_ParseTuple(args, "U|s:startfile",
9109 &unipath, &operation)) {
9110 PyErr_Clear();
9111 goto normal;
9112 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009113
Victor Stinner8c62be82010-05-06 00:08:46 +00009114 if (operation) {
9115 woperation = PyUnicode_DecodeASCII(operation,
9116 strlen(operation), NULL);
9117 if (!woperation) {
9118 PyErr_Clear();
9119 operation = NULL;
9120 goto normal;
9121 }
9122 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009123
Victor Stinner8c62be82010-05-06 00:08:46 +00009124 Py_BEGIN_ALLOW_THREADS
9125 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9126 PyUnicode_AS_UNICODE(unipath),
9127 NULL, NULL, SW_SHOWNORMAL);
9128 Py_END_ALLOW_THREADS
9129
9130 Py_XDECREF(woperation);
9131 if (rc <= (HINSTANCE)32) {
9132 PyObject *errval = win32_error_unicode("startfile",
9133 PyUnicode_AS_UNICODE(unipath));
9134 return errval;
9135 }
9136 Py_INCREF(Py_None);
9137 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009138
9139normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009140 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9141 PyUnicode_FSConverter, &ofilepath,
9142 &operation))
9143 return NULL;
9144 filepath = PyBytes_AsString(ofilepath);
9145 Py_BEGIN_ALLOW_THREADS
9146 rc = ShellExecute((HWND)0, operation, filepath,
9147 NULL, NULL, SW_SHOWNORMAL);
9148 Py_END_ALLOW_THREADS
9149 if (rc <= (HINSTANCE)32) {
9150 PyObject *errval = win32_error("startfile", filepath);
9151 Py_DECREF(ofilepath);
9152 return errval;
9153 }
9154 Py_DECREF(ofilepath);
9155 Py_INCREF(Py_None);
9156 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009157}
9158#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009159
Martin v. Löwis438b5342002-12-27 10:16:42 +00009160#ifdef HAVE_GETLOADAVG
9161PyDoc_STRVAR(posix_getloadavg__doc__,
9162"getloadavg() -> (float, float, float)\n\n\
9163Return the number of processes in the system run queue averaged over\n\
9164the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9165was unobtainable");
9166
9167static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009168posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009169{
9170 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009171 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009172 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9173 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009174 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009175 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009176}
9177#endif
9178
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009179#ifdef MS_WINDOWS
9180
9181PyDoc_STRVAR(win32_urandom__doc__,
9182"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009183Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009184
9185typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9186 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9187 DWORD dwFlags );
9188typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9189 BYTE *pbBuffer );
9190
9191static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009192/* This handle is never explicitly released. Instead, the operating
9193 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009194static HCRYPTPROV hCryptProv = 0;
9195
Tim Peters4ad82172004-08-30 17:02:04 +00009196static PyObject*
9197win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009198{
Victor Stinner8c62be82010-05-06 00:08:46 +00009199 int howMany;
9200 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009201
Victor Stinner8c62be82010-05-06 00:08:46 +00009202 /* Read arguments */
9203 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9204 return NULL;
9205 if (howMany < 0)
9206 return PyErr_Format(PyExc_ValueError,
9207 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009208
Victor Stinner8c62be82010-05-06 00:08:46 +00009209 if (hCryptProv == 0) {
9210 HINSTANCE hAdvAPI32 = NULL;
9211 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009212
Victor Stinner8c62be82010-05-06 00:08:46 +00009213 /* Obtain handle to the DLL containing CryptoAPI
9214 This should not fail */
9215 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9216 if(hAdvAPI32 == NULL)
9217 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009218
Victor Stinner8c62be82010-05-06 00:08:46 +00009219 /* Obtain pointers to the CryptoAPI functions
9220 This will fail on some early versions of Win95 */
9221 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9222 hAdvAPI32,
9223 "CryptAcquireContextA");
9224 if (pCryptAcquireContext == NULL)
9225 return PyErr_Format(PyExc_NotImplementedError,
9226 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009227
Victor Stinner8c62be82010-05-06 00:08:46 +00009228 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9229 hAdvAPI32, "CryptGenRandom");
9230 if (pCryptGenRandom == NULL)
9231 return PyErr_Format(PyExc_NotImplementedError,
9232 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009233
Victor Stinner8c62be82010-05-06 00:08:46 +00009234 /* Acquire context */
9235 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9236 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9237 return win32_error("CryptAcquireContext", NULL);
9238 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009239
Victor Stinner8c62be82010-05-06 00:08:46 +00009240 /* Allocate bytes */
9241 result = PyBytes_FromStringAndSize(NULL, howMany);
9242 if (result != NULL) {
9243 /* Get random data */
9244 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9245 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9246 PyBytes_AS_STRING(result))) {
9247 Py_DECREF(result);
9248 return win32_error("CryptGenRandom", NULL);
9249 }
9250 }
9251 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009252}
9253#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009254
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009255PyDoc_STRVAR(device_encoding__doc__,
9256"device_encoding(fd) -> str\n\n\
9257Return a string describing the encoding of the device\n\
9258if the output is a terminal; else return None.");
9259
9260static PyObject *
9261device_encoding(PyObject *self, PyObject *args)
9262{
Victor Stinner8c62be82010-05-06 00:08:46 +00009263 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009264#if defined(MS_WINDOWS) || defined(MS_WIN64)
9265 UINT cp;
9266#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009267 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9268 return NULL;
9269 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9270 Py_INCREF(Py_None);
9271 return Py_None;
9272 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009273#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009274 if (fd == 0)
9275 cp = GetConsoleCP();
9276 else if (fd == 1 || fd == 2)
9277 cp = GetConsoleOutputCP();
9278 else
9279 cp = 0;
9280 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9281 has no console */
9282 if (cp != 0)
9283 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009284#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009285 {
9286 char *codeset = nl_langinfo(CODESET);
9287 if (codeset != NULL && codeset[0] != 0)
9288 return PyUnicode_FromString(codeset);
9289 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009290#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009291 Py_INCREF(Py_None);
9292 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009293}
9294
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009295#ifdef __VMS
9296/* Use openssl random routine */
9297#include <openssl/rand.h>
9298PyDoc_STRVAR(vms_urandom__doc__,
9299"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009300Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009301
9302static PyObject*
9303vms_urandom(PyObject *self, PyObject *args)
9304{
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 int howMany;
9306 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009307
Victor Stinner8c62be82010-05-06 00:08:46 +00009308 /* Read arguments */
9309 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9310 return NULL;
9311 if (howMany < 0)
9312 return PyErr_Format(PyExc_ValueError,
9313 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009314
Victor Stinner8c62be82010-05-06 00:08:46 +00009315 /* Allocate bytes */
9316 result = PyBytes_FromStringAndSize(NULL, howMany);
9317 if (result != NULL) {
9318 /* Get random data */
9319 if (RAND_pseudo_bytes((unsigned char*)
9320 PyBytes_AS_STRING(result),
9321 howMany) < 0) {
9322 Py_DECREF(result);
9323 return PyErr_Format(PyExc_ValueError,
9324 "RAND_pseudo_bytes");
9325 }
9326 }
9327 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009328}
9329#endif
9330
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009331#ifdef HAVE_SETRESUID
9332PyDoc_STRVAR(posix_setresuid__doc__,
9333"setresuid(ruid, euid, suid)\n\n\
9334Set the current process's real, effective, and saved user ids.");
9335
9336static PyObject*
9337posix_setresuid (PyObject *self, PyObject *args)
9338{
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 /* We assume uid_t is no larger than a long. */
9340 long ruid, euid, suid;
9341 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9342 return NULL;
9343 if (setresuid(ruid, euid, suid) < 0)
9344 return posix_error();
9345 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009346}
9347#endif
9348
9349#ifdef HAVE_SETRESGID
9350PyDoc_STRVAR(posix_setresgid__doc__,
9351"setresgid(rgid, egid, sgid)\n\n\
9352Set the current process's real, effective, and saved group ids.");
9353
9354static PyObject*
9355posix_setresgid (PyObject *self, PyObject *args)
9356{
Victor Stinner8c62be82010-05-06 00:08:46 +00009357 /* We assume uid_t is no larger than a long. */
9358 long rgid, egid, sgid;
9359 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9360 return NULL;
9361 if (setresgid(rgid, egid, sgid) < 0)
9362 return posix_error();
9363 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009364}
9365#endif
9366
9367#ifdef HAVE_GETRESUID
9368PyDoc_STRVAR(posix_getresuid__doc__,
9369"getresuid() -> (ruid, euid, suid)\n\n\
9370Get tuple of the current process's real, effective, and saved user ids.");
9371
9372static PyObject*
9373posix_getresuid (PyObject *self, PyObject *noargs)
9374{
Victor Stinner8c62be82010-05-06 00:08:46 +00009375 uid_t ruid, euid, suid;
9376 long l_ruid, l_euid, l_suid;
9377 if (getresuid(&ruid, &euid, &suid) < 0)
9378 return posix_error();
9379 /* Force the values into long's as we don't know the size of uid_t. */
9380 l_ruid = ruid;
9381 l_euid = euid;
9382 l_suid = suid;
9383 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009384}
9385#endif
9386
9387#ifdef HAVE_GETRESGID
9388PyDoc_STRVAR(posix_getresgid__doc__,
9389"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009390Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009391
9392static PyObject*
9393posix_getresgid (PyObject *self, PyObject *noargs)
9394{
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 uid_t rgid, egid, sgid;
9396 long l_rgid, l_egid, l_sgid;
9397 if (getresgid(&rgid, &egid, &sgid) < 0)
9398 return posix_error();
9399 /* Force the values into long's as we don't know the size of uid_t. */
9400 l_rgid = rgid;
9401 l_egid = egid;
9402 l_sgid = sgid;
9403 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009404}
9405#endif
9406
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009407/* Posix *at family of functions:
9408 faccessat, fchmodat, fchownat, fstatat, futimesat,
9409 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9410 unlinkat, utimensat, mkfifoat */
9411
9412#ifdef HAVE_FACCESSAT
9413PyDoc_STRVAR(posix_faccessat__doc__,
9414"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9415Like access() but if path is relative, it is taken as relative to dirfd.\n\
9416flags is optional and can be constructed by ORing together zero or more\n\
9417of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9418If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9419is interpreted relative to the current working directory.");
9420
9421static PyObject *
9422posix_faccessat(PyObject *self, PyObject *args)
9423{
9424 PyObject *opath;
9425 char *path;
9426 int mode;
9427 int res;
9428 int dirfd, flags = 0;
9429 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9430 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9431 return NULL;
9432 path = PyBytes_AsString(opath);
9433 Py_BEGIN_ALLOW_THREADS
9434 res = faccessat(dirfd, path, mode, flags);
9435 Py_END_ALLOW_THREADS
9436 Py_DECREF(opath);
9437 return PyBool_FromLong(res == 0);
9438}
9439#endif
9440
9441#ifdef HAVE_FCHMODAT
9442PyDoc_STRVAR(posix_fchmodat__doc__,
9443"fchmodat(dirfd, path, mode, flags=0)\n\n\
9444Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9445flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9446If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9447is interpreted relative to the current working directory.");
9448
9449static PyObject *
9450posix_fchmodat(PyObject *self, PyObject *args)
9451{
9452 int dirfd, mode, res;
9453 int flags = 0;
9454 PyObject *opath;
9455 char *path;
9456
9457 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9458 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9459 return NULL;
9460
9461 path = PyBytes_AsString(opath);
9462
9463 Py_BEGIN_ALLOW_THREADS
9464 res = fchmodat(dirfd, path, mode, flags);
9465 Py_END_ALLOW_THREADS
9466 Py_DECREF(opath);
9467 if (res < 0)
9468 return posix_error();
9469 Py_RETURN_NONE;
9470}
9471#endif /* HAVE_FCHMODAT */
9472
9473#ifdef HAVE_FCHOWNAT
9474PyDoc_STRVAR(posix_fchownat__doc__,
9475"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9476Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9477flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9478If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9479is interpreted relative to the current working directory.");
9480
9481static PyObject *
9482posix_fchownat(PyObject *self, PyObject *args)
9483{
9484 PyObject *opath;
9485 int dirfd, res;
9486 long uid, gid;
9487 int flags = 0;
9488 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009489
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009490 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9491 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9492 return NULL;
9493
9494 path = PyBytes_AsString(opath);
9495
9496 Py_BEGIN_ALLOW_THREADS
9497 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9498 Py_END_ALLOW_THREADS
9499 Py_DECREF(opath);
9500 if (res < 0)
9501 return posix_error();
9502 Py_RETURN_NONE;
9503}
9504#endif /* HAVE_FCHOWNAT */
9505
9506#ifdef HAVE_FSTATAT
9507PyDoc_STRVAR(posix_fstatat__doc__,
9508"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9509Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9510flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9511If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9512is interpreted relative to the current working directory.");
9513
9514static PyObject *
9515posix_fstatat(PyObject *self, PyObject *args)
9516{
9517 PyObject *opath;
9518 char *path;
9519 STRUCT_STAT st;
9520 int dirfd, res, flags = 0;
9521
9522 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9523 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9524 return NULL;
9525 path = PyBytes_AsString(opath);
9526
9527 Py_BEGIN_ALLOW_THREADS
9528 res = fstatat(dirfd, path, &st, flags);
9529 Py_END_ALLOW_THREADS
9530 Py_DECREF(opath);
9531 if (res != 0)
9532 return posix_error();
9533
9534 return _pystat_fromstructstat(&st);
9535}
9536#endif
9537
9538#ifdef HAVE_FUTIMESAT
9539PyDoc_STRVAR(posix_futimesat__doc__,
9540"futimesat(dirfd, path, (atime, mtime))\n\
9541futimesat(dirfd, path, None)\n\n\
9542Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9543If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9544is interpreted relative to the current working directory.");
9545
9546static PyObject *
9547posix_futimesat(PyObject *self, PyObject *args)
9548{
9549 PyObject *opath;
9550 char *path;
9551 int res, dirfd;
9552 PyObject* arg;
9553
9554 struct timeval buf[2];
9555
9556 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9557 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9558 return NULL;
9559 path = PyBytes_AsString(opath);
9560 if (arg == Py_None) {
9561 /* optional time values not given */
9562 Py_BEGIN_ALLOW_THREADS
9563 res = futimesat(dirfd, path, NULL);
9564 Py_END_ALLOW_THREADS
9565 }
9566 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9567 PyErr_SetString(PyExc_TypeError,
9568 "futimesat() arg 3 must be a tuple (atime, mtime)");
9569 Py_DECREF(opath);
9570 return NULL;
9571 }
9572 else {
9573 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009574 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009575 Py_DECREF(opath);
9576 return NULL;
9577 }
9578 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009579 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009580 Py_DECREF(opath);
9581 return NULL;
9582 }
9583 Py_BEGIN_ALLOW_THREADS
9584 res = futimesat(dirfd, path, buf);
9585 Py_END_ALLOW_THREADS
9586 }
9587 Py_DECREF(opath);
9588 if (res < 0) {
9589 return posix_error();
9590 }
9591 Py_RETURN_NONE;
9592}
9593#endif
9594
9595#ifdef HAVE_LINKAT
9596PyDoc_STRVAR(posix_linkat__doc__,
9597"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9598Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9599and if dstpath is relative, it is taken as relative to dstfd.\n\
9600flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9601If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9602srcpath is interpreted relative to the current working directory. This\n\
9603also applies for dstpath.");
9604
9605static PyObject *
9606posix_linkat(PyObject *self, PyObject *args)
9607{
9608 PyObject *osrc, *odst;
9609 char *src, *dst;
9610 int res, srcfd, dstfd;
9611 int flags = 0;
9612
9613 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9614 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9615 return NULL;
9616 src = PyBytes_AsString(osrc);
9617 dst = PyBytes_AsString(odst);
9618 Py_BEGIN_ALLOW_THREADS
9619 res = linkat(srcfd, src, dstfd, dst, flags);
9620 Py_END_ALLOW_THREADS
9621 Py_DECREF(osrc);
9622 Py_DECREF(odst);
9623 if (res < 0)
9624 return posix_error();
9625 Py_RETURN_NONE;
9626}
9627#endif /* HAVE_LINKAT */
9628
9629#ifdef HAVE_MKDIRAT
9630PyDoc_STRVAR(posix_mkdirat__doc__,
9631"mkdirat(dirfd, path, mode=0o777)\n\n\
9632Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9633If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9634is interpreted relative to the current working directory.");
9635
9636static PyObject *
9637posix_mkdirat(PyObject *self, PyObject *args)
9638{
9639 int res, dirfd;
9640 PyObject *opath;
9641 char *path;
9642 int mode = 0777;
9643
9644 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9645 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9646 return NULL;
9647 path = PyBytes_AsString(opath);
9648 Py_BEGIN_ALLOW_THREADS
9649 res = mkdirat(dirfd, path, mode);
9650 Py_END_ALLOW_THREADS
9651 Py_DECREF(opath);
9652 if (res < 0)
9653 return posix_error();
9654 Py_RETURN_NONE;
9655}
9656#endif
9657
9658#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9659PyDoc_STRVAR(posix_mknodat__doc__,
9660"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9661Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9662If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9663is interpreted relative to the current working directory.");
9664
9665static PyObject *
9666posix_mknodat(PyObject *self, PyObject *args)
9667{
9668 PyObject *opath;
9669 char *filename;
9670 int mode = 0600;
9671 int device = 0;
9672 int res, dirfd;
9673 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9674 PyUnicode_FSConverter, &opath, &mode, &device))
9675 return NULL;
9676 filename = PyBytes_AS_STRING(opath);
9677 Py_BEGIN_ALLOW_THREADS
9678 res = mknodat(dirfd, filename, mode, device);
9679 Py_END_ALLOW_THREADS
9680 Py_DECREF(opath);
9681 if (res < 0)
9682 return posix_error();
9683 Py_RETURN_NONE;
9684}
9685#endif
9686
9687#ifdef HAVE_OPENAT
9688PyDoc_STRVAR(posix_openat__doc__,
9689"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9690Like open() but if path is relative, it is taken as relative to dirfd.\n\
9691If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9692is interpreted relative to the current working directory.");
9693
9694static PyObject *
9695posix_openat(PyObject *self, PyObject *args)
9696{
9697 PyObject *ofile;
9698 char *file;
9699 int flag, dirfd, fd;
9700 int mode = 0777;
9701
9702 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9703 &dirfd, PyUnicode_FSConverter, &ofile,
9704 &flag, &mode))
9705 return NULL;
9706 file = PyBytes_AsString(ofile);
9707 Py_BEGIN_ALLOW_THREADS
9708 fd = openat(dirfd, file, flag, mode);
9709 Py_END_ALLOW_THREADS
9710 Py_DECREF(ofile);
9711 if (fd < 0)
9712 return posix_error();
9713 return PyLong_FromLong((long)fd);
9714}
9715#endif
9716
9717#ifdef HAVE_READLINKAT
9718PyDoc_STRVAR(posix_readlinkat__doc__,
9719"readlinkat(dirfd, path) -> path\n\n\
9720Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9721If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9722is interpreted relative to the current working directory.");
9723
9724static PyObject *
9725posix_readlinkat(PyObject *self, PyObject *args)
9726{
9727 PyObject *v, *opath;
9728 char buf[MAXPATHLEN];
9729 char *path;
9730 int n, dirfd;
9731 int arg_is_unicode = 0;
9732
9733 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9734 &dirfd, PyUnicode_FSConverter, &opath))
9735 return NULL;
9736 path = PyBytes_AsString(opath);
9737 v = PySequence_GetItem(args, 1);
9738 if (v == NULL) {
9739 Py_DECREF(opath);
9740 return NULL;
9741 }
9742
9743 if (PyUnicode_Check(v)) {
9744 arg_is_unicode = 1;
9745 }
9746 Py_DECREF(v);
9747
9748 Py_BEGIN_ALLOW_THREADS
9749 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9750 Py_END_ALLOW_THREADS
9751 Py_DECREF(opath);
9752 if (n < 0)
9753 return posix_error();
9754
9755 if (arg_is_unicode)
9756 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9757 else
9758 return PyBytes_FromStringAndSize(buf, n);
9759}
9760#endif /* HAVE_READLINKAT */
9761
9762#ifdef HAVE_RENAMEAT
9763PyDoc_STRVAR(posix_renameat__doc__,
9764"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9765Like rename() but if oldpath is relative, it is taken as relative to\n\
9766olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9767If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9768oldpath is interpreted relative to the current working directory. This\n\
9769also applies for newpath.");
9770
9771static PyObject *
9772posix_renameat(PyObject *self, PyObject *args)
9773{
9774 int res;
9775 PyObject *opathold, *opathnew;
9776 char *opath, *npath;
9777 int oldfd, newfd;
9778
9779 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9780 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9781 return NULL;
9782 opath = PyBytes_AsString(opathold);
9783 npath = PyBytes_AsString(opathnew);
9784 Py_BEGIN_ALLOW_THREADS
9785 res = renameat(oldfd, opath, newfd, npath);
9786 Py_END_ALLOW_THREADS
9787 Py_DECREF(opathold);
9788 Py_DECREF(opathnew);
9789 if (res < 0)
9790 return posix_error();
9791 Py_RETURN_NONE;
9792}
9793#endif
9794
9795#if HAVE_SYMLINKAT
9796PyDoc_STRVAR(posix_symlinkat__doc__,
9797"symlinkat(src, dstfd, dst)\n\n\
9798Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9799If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9800is interpreted relative to the current working directory.");
9801
9802static PyObject *
9803posix_symlinkat(PyObject *self, PyObject *args)
9804{
9805 int res, dstfd;
9806 PyObject *osrc, *odst;
9807 char *src, *dst;
9808
9809 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9810 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9811 return NULL;
9812 src = PyBytes_AsString(osrc);
9813 dst = PyBytes_AsString(odst);
9814 Py_BEGIN_ALLOW_THREADS
9815 res = symlinkat(src, dstfd, dst);
9816 Py_END_ALLOW_THREADS
9817 Py_DECREF(osrc);
9818 Py_DECREF(odst);
9819 if (res < 0)
9820 return posix_error();
9821 Py_RETURN_NONE;
9822}
9823#endif /* HAVE_SYMLINKAT */
9824
9825#ifdef HAVE_UNLINKAT
9826PyDoc_STRVAR(posix_unlinkat__doc__,
9827"unlinkat(dirfd, path, flags=0)\n\n\
9828Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9829flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9830specified, unlinkat() behaves like rmdir().\n\
9831If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9832is interpreted relative to the current working directory.");
9833
9834static PyObject *
9835posix_unlinkat(PyObject *self, PyObject *args)
9836{
9837 int dirfd, res, flags = 0;
9838 PyObject *opath;
9839 char *path;
9840
9841 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9842 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9843 return NULL;
9844 path = PyBytes_AsString(opath);
9845 Py_BEGIN_ALLOW_THREADS
9846 res = unlinkat(dirfd, path, flags);
9847 Py_END_ALLOW_THREADS
9848 Py_DECREF(opath);
9849 if (res < 0)
9850 return posix_error();
9851 Py_RETURN_NONE;
9852}
9853#endif
9854
9855#ifdef HAVE_UTIMENSAT
9856PyDoc_STRVAR(posix_utimensat__doc__,
9857"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9858 (mtime_sec, mtime_nsec), flags)\n\
9859utimensat(dirfd, path, None, None, flags)\n\n\
9860Updates the timestamps of a file with nanosecond precision. If path is\n\
9861relative, it is taken as relative to dirfd.\n\
9862The second form sets atime and mtime to the current time.\n\
9863flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9864If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9865is interpreted relative to the current working directory.\n\
9866If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9867current time.\n\
9868If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9869
9870static PyObject *
9871posix_utimensat(PyObject *self, PyObject *args)
9872{
9873 PyObject *opath;
9874 char *path;
9875 int res, dirfd, flags = 0;
9876 PyObject *atime, *mtime;
9877
9878 struct timespec buf[2];
9879
9880 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9881 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9882 return NULL;
9883 path = PyBytes_AsString(opath);
9884 if (atime == Py_None && mtime == Py_None) {
9885 /* optional time values not given */
9886 Py_BEGIN_ALLOW_THREADS
9887 res = utimensat(dirfd, path, NULL, flags);
9888 Py_END_ALLOW_THREADS
9889 }
9890 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9891 PyErr_SetString(PyExc_TypeError,
9892 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9893 Py_DECREF(opath);
9894 return NULL;
9895 }
9896 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9897 PyErr_SetString(PyExc_TypeError,
9898 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9899 Py_DECREF(opath);
9900 return NULL;
9901 }
9902 else {
9903 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9904 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9905 Py_DECREF(opath);
9906 return NULL;
9907 }
9908 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9909 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9910 Py_DECREF(opath);
9911 return NULL;
9912 }
9913 Py_BEGIN_ALLOW_THREADS
9914 res = utimensat(dirfd, path, buf, flags);
9915 Py_END_ALLOW_THREADS
9916 }
9917 Py_DECREF(opath);
9918 if (res < 0) {
9919 return posix_error();
9920 }
9921 Py_RETURN_NONE;
9922}
9923#endif
9924
9925#ifdef HAVE_MKFIFOAT
9926PyDoc_STRVAR(posix_mkfifoat__doc__,
9927"mkfifoat(dirfd, path, mode=0o666)\n\n\
9928Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9929If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9930is interpreted relative to the current working directory.");
9931
9932static PyObject *
9933posix_mkfifoat(PyObject *self, PyObject *args)
9934{
9935 PyObject *opath;
9936 char *filename;
9937 int mode = 0666;
9938 int res, dirfd;
9939 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9940 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9941 return NULL;
9942 filename = PyBytes_AS_STRING(opath);
9943 Py_BEGIN_ALLOW_THREADS
9944 res = mkfifoat(dirfd, filename, mode);
9945 Py_END_ALLOW_THREADS
9946 Py_DECREF(opath);
9947 if (res < 0)
9948 return posix_error();
9949 Py_RETURN_NONE;
9950}
9951#endif
9952
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009953static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009954 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009955#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009956 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009957#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009959#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009960 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009961#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009962 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009963#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009965#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009966#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009967 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009968#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009969#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009970 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009971#endif /* HAVE_LCHMOD */
9972#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009973 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009974#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009975#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009976 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009977#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009978#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009979 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009980#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009981#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009982 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009983#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009984#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009985 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009986#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009987#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009988 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9989 METH_NOARGS, posix_getcwd__doc__},
9990 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9991 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009992#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009993#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009995#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009996 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009997#ifdef HAVE_FDOPENDIR
9998 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9999#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
10001 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010002#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010003 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010004#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010005#ifdef HAVE_GETPRIORITY
10006 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10007#endif /* HAVE_GETPRIORITY */
10008#ifdef HAVE_SETPRIORITY
10009 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10010#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010011#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010012 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010013#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010014#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010015 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010016#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010017 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
10018 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
10019 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010020 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010021#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010022 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010023#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010024#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010025 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010026 win_symlink__doc__},
10027#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010028#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010029 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010030#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010031 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010032#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010034#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010035 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10036 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10037 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010038#ifdef HAVE_FUTIMES
10039 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10040#endif
10041#ifdef HAVE_LUTIMES
10042 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10043#endif
10044#ifdef HAVE_FUTIMENS
10045 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10046#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010047#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010048 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010049#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010050 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010051#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010052 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10053 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010054#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010055#ifdef HAVE_FEXECVE
10056 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10057#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010058#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010059 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10060 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010061#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010062 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10063 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010064#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010065#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010066#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010067 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010068#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010069#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010070 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010071#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010072#ifdef HAVE_SCHED_H
10073 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10074 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010075#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010076 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010077#endif
10078#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010079 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010080#endif
10081#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010082 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010083#endif
10084#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010085 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010086#endif
10087#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010088 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010089#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010090 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010091#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010092 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10093 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10094#endif
Benjamin Peterson2740af82011-08-02 17:41:34 -050010095#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010096#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010097 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010098#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010099#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010100 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010101#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010102#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010103 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010104#endif /* HAVE_GETEGID */
10105#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010106 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010107#endif /* HAVE_GETEUID */
10108#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010110#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010111#ifdef HAVE_GETGROUPLIST
10112 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10113#endif
Fred Drakec9680921999-12-13 16:37:25 +000010114#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010115 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010116#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010117 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010118#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010119 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010120#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010121#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010122 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010123#endif /* HAVE_GETPPID */
10124#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010125 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010126#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010127#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010128 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010129#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010130#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010131 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010132#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010133#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010134 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010135#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010136#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010137 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010138#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010139#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010140 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10141 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010142 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010143#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010144#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010145 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010146#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010147#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010148 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010149#endif /* HAVE_SETEUID */
10150#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010151 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010152#endif /* HAVE_SETEGID */
10153#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010154 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010155#endif /* HAVE_SETREUID */
10156#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010157 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010158#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010159#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010160 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010161#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010162#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010163 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010164#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010165#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010166 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010167#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010168#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010169 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010170#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010171#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010172 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010173#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010174#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010175 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010176#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010177#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010178 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010179#endif /* HAVE_WAIT3 */
10180#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010181 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010182#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010183#if defined(HAVE_WAITID) && !defined(__APPLE__)
10184 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10185#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010186#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010187 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010188#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010189#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010190 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010191#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010192#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010193 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010194#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010195#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010196 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010197#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010198#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010199 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010200#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010201#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010202 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010203#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010204 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10205 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10206 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10207 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10208 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10209 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010210#ifdef HAVE_LOCKF
10211 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10212#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010213 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10214 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010215#ifdef HAVE_READV
10216 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10217#endif
10218#ifdef HAVE_PREAD
10219 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10220#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010221 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010222#ifdef HAVE_WRITEV
10223 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10224#endif
10225#ifdef HAVE_PWRITE
10226 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10227#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010228#ifdef HAVE_SENDFILE
10229 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10230 posix_sendfile__doc__},
10231#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010232 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10233 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010234#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010235 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010236#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010237#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010238 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010239#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010240#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010241 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010242#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010243#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010244 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010245#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010246#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010247 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10248 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10249 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010250#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010251#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010252 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010253#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010254#ifdef HAVE_TRUNCATE
10255 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10256#endif
10257#ifdef HAVE_POSIX_FALLOCATE
10258 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10259#endif
10260#ifdef HAVE_POSIX_FADVISE
10261 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10262#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010263#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010264 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010265#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010266#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010267 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010268#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010269 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010270#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010271 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010272#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010273#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010274 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010275#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010276#ifdef HAVE_SYNC
10277 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10278#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010279#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010280 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010281#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010282#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010283#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010284 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010285#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010286#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010287 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010288#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010289#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010290 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010291#endif /* WIFSTOPPED */
10292#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010293 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010294#endif /* WIFSIGNALED */
10295#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010296 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010297#endif /* WIFEXITED */
10298#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010299 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010300#endif /* WEXITSTATUS */
10301#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010302 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010303#endif /* WTERMSIG */
10304#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010305 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010306#endif /* WSTOPSIG */
10307#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010308#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010309 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010310#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010311#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010312 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010313#endif
Fred Drakec9680921999-12-13 16:37:25 +000010314#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010315 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010316#endif
10317#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010318 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010319#endif
10320#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010321 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010322#endif
10323#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010324 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010325#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010326 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010327#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010328 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010329 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010330 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010331 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010332 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010333#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010334#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010336#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010337 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010338 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010339 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010340 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010341 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010342 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010343#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010344 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010345#endif
10346#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010347 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010348#endif
10349#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010350 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010351#endif
10352#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010353 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010354#endif
10355
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010356/* posix *at family of functions */
10357#ifdef HAVE_FACCESSAT
10358 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10359#endif
10360#ifdef HAVE_FCHMODAT
10361 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10362#endif /* HAVE_FCHMODAT */
10363#ifdef HAVE_FCHOWNAT
10364 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10365#endif /* HAVE_FCHOWNAT */
10366#ifdef HAVE_FSTATAT
10367 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10368#endif
10369#ifdef HAVE_FUTIMESAT
10370 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10371#endif
10372#ifdef HAVE_LINKAT
10373 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10374#endif /* HAVE_LINKAT */
10375#ifdef HAVE_MKDIRAT
10376 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10377#endif
10378#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10379 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10380#endif
10381#ifdef HAVE_OPENAT
10382 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10383#endif
10384#ifdef HAVE_READLINKAT
10385 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10386#endif /* HAVE_READLINKAT */
10387#ifdef HAVE_RENAMEAT
10388 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10389#endif
10390#if HAVE_SYMLINKAT
10391 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10392#endif /* HAVE_SYMLINKAT */
10393#ifdef HAVE_UNLINKAT
10394 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10395#endif
10396#ifdef HAVE_UTIMENSAT
10397 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10398#endif
10399#ifdef HAVE_MKFIFOAT
10400 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10401#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010402 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010403};
10404
10405
Barry Warsaw4a342091996-12-19 23:50:02 +000010406static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010407ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010408{
Victor Stinner8c62be82010-05-06 00:08:46 +000010409 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010410}
10411
Guido van Rossumd48f2521997-12-05 22:19:34 +000010412#if defined(PYOS_OS2)
10413/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010414static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010415{
10416 APIRET rc;
10417 ULONG values[QSV_MAX+1];
10418 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010419 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010420
10421 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010422 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010423 Py_END_ALLOW_THREADS
10424
10425 if (rc != NO_ERROR) {
10426 os2_error(rc);
10427 return -1;
10428 }
10429
Fred Drake4d1e64b2002-04-15 19:40:07 +000010430 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10431 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10432 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10433 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10434 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10435 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10436 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010437
10438 switch (values[QSV_VERSION_MINOR]) {
10439 case 0: ver = "2.00"; break;
10440 case 10: ver = "2.10"; break;
10441 case 11: ver = "2.11"; break;
10442 case 30: ver = "3.00"; break;
10443 case 40: ver = "4.00"; break;
10444 case 50: ver = "5.00"; break;
10445 default:
Tim Peters885d4572001-11-28 20:27:42 +000010446 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010447 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010448 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010449 ver = &tmp[0];
10450 }
10451
10452 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010453 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010454 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010455
10456 /* Add Indicator of Which Drive was Used to Boot the System */
10457 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10458 tmp[1] = ':';
10459 tmp[2] = '\0';
10460
Fred Drake4d1e64b2002-04-15 19:40:07 +000010461 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010462}
10463#endif
10464
Brian Curtin52173d42010-12-02 18:29:18 +000010465#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010466static int
Brian Curtin52173d42010-12-02 18:29:18 +000010467enable_symlink()
10468{
10469 HANDLE tok;
10470 TOKEN_PRIVILEGES tok_priv;
10471 LUID luid;
10472 int meth_idx = 0;
10473
10474 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010475 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010476
10477 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010478 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010479
10480 tok_priv.PrivilegeCount = 1;
10481 tok_priv.Privileges[0].Luid = luid;
10482 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10483
10484 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10485 sizeof(TOKEN_PRIVILEGES),
10486 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010487 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010488
Brian Curtin3b4499c2010-12-28 14:31:47 +000010489 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10490 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010491}
10492#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10493
Barry Warsaw4a342091996-12-19 23:50:02 +000010494static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010495all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010496{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010497#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010498 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010499#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010500#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010501 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010502#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010503#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010504 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010505#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010506#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010507 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010508#endif
Fred Drakec9680921999-12-13 16:37:25 +000010509#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010510 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010511#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010512#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010513 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010514#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010515#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010516 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010517#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010518#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010520#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010521#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010523#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010524#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010525 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010526#endif
10527#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010528 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010529#endif
10530#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000010531 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010532#endif
10533#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000010534 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010535#endif
10536#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010537 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010538#endif
10539#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000010540 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010541#endif
10542#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010543 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010544#endif
10545#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010546 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010547#endif
10548#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010549 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010550#endif
10551#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010552 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010553#endif
10554#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010555 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010556#endif
10557#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000010558 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010559#endif
10560#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010561 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010562#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000010563#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010564 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010565#endif
10566#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000010567 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010568#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010569#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010570 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010571#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000010572#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010574#endif
10575#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010576 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010577#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010578#ifdef PRIO_PROCESS
10579 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
10580#endif
10581#ifdef PRIO_PGRP
10582 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
10583#endif
10584#ifdef PRIO_USER
10585 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
10586#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010587#ifdef O_CLOEXEC
10588 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
10589#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010590/* posix - constants for *at functions */
10591#ifdef AT_SYMLINK_NOFOLLOW
10592 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
10593#endif
10594#ifdef AT_EACCESS
10595 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
10596#endif
10597#ifdef AT_FDCWD
10598 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
10599#endif
10600#ifdef AT_REMOVEDIR
10601 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
10602#endif
10603#ifdef AT_SYMLINK_FOLLOW
10604 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
10605#endif
10606#ifdef UTIME_NOW
10607 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10608#endif
10609#ifdef UTIME_OMIT
10610 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10611#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010612
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010613
Tim Peters5aa91602002-01-30 05:46:57 +000010614/* MS Windows */
10615#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010616 /* Don't inherit in child processes. */
10617 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010618#endif
10619#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 /* Optimize for short life (keep in memory). */
10621 /* MS forgot to define this one with a non-underscore form too. */
10622 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010623#endif
10624#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010625 /* Automatically delete when last handle is closed. */
10626 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010627#endif
10628#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 /* Optimize for random access. */
10630 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010631#endif
10632#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010633 /* Optimize for sequential access. */
10634 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010635#endif
10636
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010637/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010638#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010639 /* Send a SIGIO signal whenever input or output
10640 becomes available on file descriptor */
10641 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010642#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010643#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 /* Direct disk access. */
10645 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010646#endif
10647#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000010648 /* Must be a directory. */
10649 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010650#endif
10651#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 /* Do not follow links. */
10653 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010654#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010655#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 /* Do not update the access time. */
10657 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010658#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000010659
Victor Stinner8c62be82010-05-06 00:08:46 +000010660 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010661#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010663#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010664#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010665 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010666#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010667#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010669#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010670#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010672#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010673#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010675#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010676#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010678#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010679#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010681#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010682#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010684#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010685#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010687#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010688#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010690#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010691#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010693#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010694#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010696#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010697#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010699#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010700#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010702#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010703#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010705#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010706#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010707 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010708#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010709#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010711#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010712
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010713 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010714#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010715 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010716#endif /* ST_RDONLY */
10717#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010718 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010719#endif /* ST_NOSUID */
10720
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010721 /* FreeBSD sendfile() constants */
10722#ifdef SF_NODISKIO
10723 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
10724#endif
10725#ifdef SF_MNOWAIT
10726 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
10727#endif
10728#ifdef SF_SYNC
10729 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
10730#endif
10731
Ross Lagerwall7807c352011-03-17 20:20:30 +020010732 /* constants for posix_fadvise */
10733#ifdef POSIX_FADV_NORMAL
10734 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
10735#endif
10736#ifdef POSIX_FADV_SEQUENTIAL
10737 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
10738#endif
10739#ifdef POSIX_FADV_RANDOM
10740 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
10741#endif
10742#ifdef POSIX_FADV_NOREUSE
10743 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
10744#endif
10745#ifdef POSIX_FADV_WILLNEED
10746 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
10747#endif
10748#ifdef POSIX_FADV_DONTNEED
10749 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
10750#endif
10751
10752 /* constants for waitid */
10753#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
10754 if (ins(d, "P_PID", (long)P_PID)) return -1;
10755 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
10756 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
10757#endif
10758#ifdef WEXITED
10759 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
10760#endif
10761#ifdef WNOWAIT
10762 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
10763#endif
10764#ifdef WSTOPPED
10765 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
10766#endif
10767#ifdef CLD_EXITED
10768 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
10769#endif
10770#ifdef CLD_DUMPED
10771 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
10772#endif
10773#ifdef CLD_TRAPPED
10774 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
10775#endif
10776#ifdef CLD_CONTINUED
10777 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
10778#endif
10779
10780 /* constants for lockf */
10781#ifdef F_LOCK
10782 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
10783#endif
10784#ifdef F_TLOCK
10785 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
10786#endif
10787#ifdef F_ULOCK
10788 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
10789#endif
10790#ifdef F_TEST
10791 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10792#endif
10793
10794 /* constants for futimens */
10795#ifdef UTIME_NOW
10796 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10797#endif
10798#ifdef UTIME_OMIT
10799 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10800#endif
10801
Guido van Rossum246bc171999-02-01 23:54:31 +000010802#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010803#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010804 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10805 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10806 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10807 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10808 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10809 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10810 if (ins(d, "P_PM", (long)P_PM)) return -1;
10811 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10812 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10813 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10814 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10815 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10816 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10817 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10818 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10819 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10820 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10821 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10822 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10823 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010824#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010825 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10826 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10827 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10828 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10829 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010830#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010831#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010832
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010833#ifdef HAVE_SCHED_H
10834 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
10835 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
10836#ifdef SCHED_SPORADIC
10837 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
10838#endif
10839 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
10840#ifdef SCHED_BATCH
10841 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
10842#endif
10843#ifdef SCHED_IDLE
10844 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
10845#endif
10846#ifdef SCHED_RESET_ON_FORK
10847 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
10848#endif
10849#endif
10850
Guido van Rossumd48f2521997-12-05 22:19:34 +000010851#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010852 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010853#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010855}
10856
10857
Tim Peters5aa91602002-01-30 05:46:57 +000010858#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010859#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010860#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010861
10862#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010863#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010864#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010865
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010866#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010867#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010868#define MODNAME "posix"
10869#endif
10870
Martin v. Löwis1a214512008-06-11 05:26:20 +000010871static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010872 PyModuleDef_HEAD_INIT,
10873 MODNAME,
10874 posix__doc__,
10875 -1,
10876 posix_methods,
10877 NULL,
10878 NULL,
10879 NULL,
10880 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010881};
10882
10883
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010884PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010885INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010886{
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010888
Brian Curtin52173d42010-12-02 18:29:18 +000010889#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010890 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010891#endif
10892
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 m = PyModule_Create(&posixmodule);
10894 if (m == NULL)
10895 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010896
Victor Stinner8c62be82010-05-06 00:08:46 +000010897 /* Initialize environ dictionary */
10898 v = convertenviron();
10899 Py_XINCREF(v);
10900 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10901 return NULL;
10902 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010903
Victor Stinner8c62be82010-05-06 00:08:46 +000010904 if (all_ins(m))
10905 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010906
Victor Stinner8c62be82010-05-06 00:08:46 +000010907 if (setup_confname_tables(m))
10908 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010909
Victor Stinner8c62be82010-05-06 00:08:46 +000010910 Py_INCREF(PyExc_OSError);
10911 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010912
Benjamin Peterson2740af82011-08-02 17:41:34 -050010913#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010914 if (PyType_Ready(&cpu_set_type) < 0)
10915 return NULL;
10916 Py_INCREF(&cpu_set_type);
10917 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050010918#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010919
Guido van Rossumb3d39562000-01-31 18:41:26 +000010920#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010921 if (posix_putenv_garbage == NULL)
10922 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010923#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010924
Victor Stinner8c62be82010-05-06 00:08:46 +000010925 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010926#if defined(HAVE_WAITID) && !defined(__APPLE__)
10927 waitid_result_desc.name = MODNAME ".waitid_result";
10928 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10929#endif
10930
Victor Stinner8c62be82010-05-06 00:08:46 +000010931 stat_result_desc.name = MODNAME ".stat_result";
10932 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10933 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10934 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10935 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10936 structseq_new = StatResultType.tp_new;
10937 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010938
Victor Stinner8c62be82010-05-06 00:08:46 +000010939 statvfs_result_desc.name = MODNAME ".statvfs_result";
10940 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010941#ifdef NEED_TICKS_PER_SECOND
10942# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010944# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010945 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010946# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010947 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010948# endif
10949#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010950
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050010951#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010952 sched_param_desc.name = MODNAME ".sched_param";
10953 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
10954 SchedParamType.tp_new = sched_param_new;
10955#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010956 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010957#if defined(HAVE_WAITID) && !defined(__APPLE__)
10958 Py_INCREF((PyObject*) &WaitidResultType);
10959 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10960#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 Py_INCREF((PyObject*) &StatResultType);
10962 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10963 Py_INCREF((PyObject*) &StatVFSResultType);
10964 PyModule_AddObject(m, "statvfs_result",
10965 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050010966
10967#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010968 Py_INCREF(&SchedParamType);
10969 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050010970#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010972
10973#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 /*
10975 * Step 2 of weak-linking support on Mac OS X.
10976 *
10977 * The code below removes functions that are not available on the
10978 * currently active platform.
10979 *
10980 * This block allow one to use a python binary that was build on
10981 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10982 * OSX 10.4.
10983 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010984#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 if (fstatvfs == NULL) {
10986 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10987 return NULL;
10988 }
10989 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010990#endif /* HAVE_FSTATVFS */
10991
10992#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010993 if (statvfs == NULL) {
10994 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10995 return NULL;
10996 }
10997 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010998#endif /* HAVE_STATVFS */
10999
11000# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011001 if (lchown == NULL) {
11002 if (PyObject_DelAttrString(m, "lchown") == -1) {
11003 return NULL;
11004 }
11005 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011006#endif /* HAVE_LCHOWN */
11007
11008
11009#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000011010 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011011
Guido van Rossumb6775db1994-08-01 11:34:53 +000011012}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011013
11014#ifdef __cplusplus
11015}
11016#endif