blob: cc54790b98c104f46d6256802a7b5fee3e481d2f [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
Jesus Ceaab70e2a2012-10-05 01:48:08 +02004/* This file is also used for Windows NT/MS-Win. In that case the
5 module actually calls itself 'nt', not 'posix', and a few
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006 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
Jesus Ceaab70e2a2012-10-05 01:48:08 +02009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Thomas Wouters477c8d52006-05-27 19:21:47 +000011#ifdef __APPLE__
12 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000013 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000014 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
15 * at the end of this file for more information.
16 */
17# pragma weak lchown
18# pragma weak statvfs
19# pragma weak fstatvfs
20
21#endif /* __APPLE__ */
22
Thomas Wouters68bc4f92006-03-01 01:05:10 +000023#define PY_SSIZE_T_CLEAN
24
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000025#include "Python.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020026#ifndef MS_WINDOWS
27#include "posixmodule.h"
28#endif
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
Ross Lagerwall4d076da2011-03-18 06:56:53 +020046#ifdef HAVE_SYS_UIO_H
47#include <sys/uio.h>
48#endif
49
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000051#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052#endif /* HAVE_SYS_TYPES_H */
53
54#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000055#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000057
Guido van Rossum36bc6801995-06-14 22:54:23 +000058#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000059#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000060#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000061
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000063#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000065
Guido van Rossumb6775db1994-08-01 11:34:53 +000066#ifdef HAVE_FCNTL_H
67#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000068#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000069
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070#ifdef HAVE_GRP_H
71#include <grp.h>
72#endif
73
Barry Warsaw5676bd12003-01-07 20:57:09 +000074#ifdef HAVE_SYSEXITS_H
75#include <sysexits.h>
76#endif /* HAVE_SYSEXITS_H */
77
Anthony Baxter8a560de2004-10-13 15:30:56 +000078#ifdef HAVE_SYS_LOADAVG_H
79#include <sys/loadavg.h>
80#endif
81
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000082#ifdef HAVE_LANGINFO_H
83#include <langinfo.h>
84#endif
85
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000086#ifdef HAVE_SYS_SENDFILE_H
87#include <sys/sendfile.h>
88#endif
89
Benjamin Peterson94b580d2011-08-02 17:30:04 -050090#ifdef HAVE_SCHED_H
91#include <sched.h>
92#endif
93
Benjamin Peterson2dbda072012-03-16 10:12:55 -050094#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -050095#undef HAVE_SCHED_SETAFFINITY
96#endif
97
Benjamin Peterson9428d532011-09-14 11:45:52 -040098#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
99#define USE_XATTRS
100#endif
101
102#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400103#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400104#endif
105
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000106#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
107#ifdef HAVE_SYS_SOCKET_H
108#include <sys/socket.h>
109#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000110#endif
111
Victor Stinner8b905bd2011-10-25 13:34:04 +0200112#ifdef HAVE_DLFCN_H
113#include <dlfcn.h>
114#endif
115
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200116#ifdef __hpux
117#include <sys/mpctl.h>
118#endif
119
120#if defined(__DragonFly__) || \
121 defined(__OpenBSD__) || \
122 defined(__FreeBSD__) || \
123 defined(__NetBSD__) || \
124 defined(__APPLE__)
125#include <sys/sysctl.h>
126#endif
127
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100128#if defined(MS_WINDOWS)
129# define TERMSIZE_USE_CONIO
130#elif defined(HAVE_SYS_IOCTL_H)
131# include <sys/ioctl.h>
132# if defined(HAVE_TERMIOS_H)
133# include <termios.h>
134# endif
135# if defined(TIOCGWINSZ)
136# define TERMSIZE_USE_IOCTL
137# endif
138#endif /* MS_WINDOWS */
139
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000141/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000142#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000145#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#include <process.h>
147#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#define HAVE_EXECV 1
150#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_OPENDIR 1
152#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#define HAVE_WAIT 1
155#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000156#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000157#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000158#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000159#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000160#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#define HAVE_EXECV 1
162#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000163#define HAVE_SYSTEM 1
164#define HAVE_CWAIT 1
165#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000166#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000167#else
Jesus Ceaab70e2a2012-10-05 01:48:08 +0200168#if defined(__VMS)
169/* Everything needed is defined in vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000170#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000171/* Unix functions that the configure script doesn't check for */
172#define HAVE_EXECV 1
173#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000174#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000175#define HAVE_FORK1 1
176#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177#define HAVE_GETCWD 1
178#define HAVE_GETEGID 1
179#define HAVE_GETEUID 1
180#define HAVE_GETGID 1
181#define HAVE_GETPPID 1
182#define HAVE_GETUID 1
183#define HAVE_KILL 1
184#define HAVE_OPENDIR 1
185#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000186#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000187#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000188#define HAVE_TTYNAME 1
Jesus Ceaab70e2a2012-10-05 01:48:08 +0200189#endif /* __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#endif /* _MSC_VER */
191#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000192#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000193
Victor Stinnera2f7c002012-02-08 03:36:25 +0100194
195
196
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000198
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000199#if defined(__sgi)&&_COMPILER_VERSION>=700
200/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
201 (default) */
202extern char *ctermid_r(char *);
203#endif
204
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000205#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000206#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000208#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000209#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000213#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000214#endif
215#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000216extern int chdir(char *);
217extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000218#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000219extern int chdir(const char *);
220extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000221#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000222#ifdef __BORLANDC__
223extern int chmod(const char *, int);
224#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000225extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000226#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000227/*#ifdef HAVE_FCHMOD
228extern int fchmod(int, mode_t);
229#endif*/
230/*#ifdef HAVE_LCHMOD
231extern int lchmod(const char *, mode_t);
232#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000233extern int chown(const char *, uid_t, gid_t);
234extern char *getcwd(char *, int);
235extern char *strerror(int);
236extern int link(const char *, const char *);
237extern int rename(const char *, const char *);
238extern int stat(const char *, struct stat *);
239extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000241extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000242#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000244extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000245#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000247
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250#ifdef HAVE_UTIME_H
251#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000252#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000254#ifdef HAVE_SYS_UTIME_H
255#include <sys/utime.h>
256#define HAVE_UTIME_H /* pretend we do for the rest of this file */
257#endif /* HAVE_SYS_UTIME_H */
258
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259#ifdef HAVE_SYS_TIMES_H
260#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000261#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262
263#ifdef HAVE_SYS_PARAM_H
264#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000265#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266
267#ifdef HAVE_SYS_UTSNAME_H
268#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000269#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000271#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#define NAMLEN(dirent) strlen((dirent)->d_name)
274#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000275#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#include <direct.h>
277#define NAMLEN(dirent) strlen((dirent)->d_name)
278#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000281#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000282#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000284#endif
285#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#endif
288#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000289#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#endif
291#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000293#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296#endif
297#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299#endif
300#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000303#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000304#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000305#endif
306#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000307#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000308#endif
309#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000310#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000311#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000313#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000314#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000315#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000316#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000317#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
318#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000319static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000320#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000321#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000322
Tim Petersbc2e10e2002-03-03 23:17:02 +0000323#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324#if defined(PATH_MAX) && PATH_MAX > 1024
325#define MAXPATHLEN PATH_MAX
326#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000327#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000329#endif /* MAXPATHLEN */
330
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000331#ifdef UNION_WAIT
332/* Emulate some macros on systems that have a union instead of macros */
333
334#ifndef WIFEXITED
335#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
336#endif
337
338#ifndef WEXITSTATUS
339#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
340#endif
341
342#ifndef WTERMSIG
343#define WTERMSIG(u_wait) ((u_wait).w_termsig)
344#endif
345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346#define WAIT_TYPE union wait
347#define WAIT_STATUS_INT(s) (s.w_status)
348
349#else /* !UNION_WAIT */
350#define WAIT_TYPE int
351#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000352#endif /* UNION_WAIT */
353
Greg Wardb48bc172000-03-01 21:51:56 +0000354/* Don't use the "_r" form if we don't need it (also, won't have a
355 prototype for it, at least on Solaris -- maybe others as well?). */
356#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
357#define USE_CTERMID_R
358#endif
359
Fred Drake699f3522000-06-29 21:12:41 +0000360/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000361#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000362#undef FSTAT
363#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200364#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000365# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700366# define LSTAT win32_lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000367# define FSTAT win32_fstat
368# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000369#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000370# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700371# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000372# define FSTAT fstat
373# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000374#endif
375
Tim Peters11b23062003-04-23 02:39:17 +0000376#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000377#include <sys/mkdev.h>
378#else
379#if defined(MAJOR_IN_SYSMACROS)
380#include <sys/sysmacros.h>
381#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000382#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
383#include <sys/mkdev.h>
384#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000385#endif
Fred Drake699f3522000-06-29 21:12:41 +0000386
Larry Hastings9cf065c2012-06-22 16:30:09 -0700387
388#ifdef MS_WINDOWS
389static int
390win32_warn_bytes_api()
391{
392 return PyErr_WarnEx(PyExc_DeprecationWarning,
393 "The Windows bytes API has been deprecated, "
394 "use Unicode filenames instead",
395 1);
396}
397#endif
398
399
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200400#ifndef MS_WINDOWS
401PyObject *
402_PyLong_FromUid(uid_t uid)
403{
404 if (uid == (uid_t)-1)
405 return PyLong_FromLong(-1);
406 return PyLong_FromUnsignedLong(uid);
407}
408
409PyObject *
410_PyLong_FromGid(gid_t gid)
411{
412 if (gid == (gid_t)-1)
413 return PyLong_FromLong(-1);
414 return PyLong_FromUnsignedLong(gid);
415}
416
417int
418_Py_Uid_Converter(PyObject *obj, void *p)
419{
420 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200421 long result;
422 if (PyFloat_Check(obj)) {
423 PyErr_SetString(PyExc_TypeError,
424 "integer argument expected, got float");
425 return 0;
426 }
427 result = PyLong_AsLongAndOverflow(obj, &overflow);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200428 if (overflow < 0)
429 goto OverflowDown;
430 if (!overflow && result == -1) {
431 /* error or -1 */
432 if (PyErr_Occurred())
433 return 0;
434 *(uid_t *)p = (uid_t)-1;
435 }
436 else {
437 /* unsigned uid_t */
438 unsigned long uresult;
439 if (overflow > 0) {
440 uresult = PyLong_AsUnsignedLong(obj);
441 if (PyErr_Occurred()) {
442 if (PyErr_ExceptionMatches(PyExc_OverflowError))
443 goto OverflowUp;
444 return 0;
445 }
446 if ((uid_t)uresult == (uid_t)-1)
447 goto OverflowUp;
448 } else {
449 if (result < 0)
450 goto OverflowDown;
451 uresult = result;
452 }
453 if (sizeof(uid_t) < sizeof(long) &&
454 (unsigned long)(uid_t)uresult != uresult)
455 goto OverflowUp;
456 *(uid_t *)p = (uid_t)uresult;
457 }
458 return 1;
459
460OverflowDown:
461 PyErr_SetString(PyExc_OverflowError,
462 "user id is less than minimum");
463 return 0;
464
465OverflowUp:
466 PyErr_SetString(PyExc_OverflowError,
467 "user id is greater than maximum");
468 return 0;
469}
470
471int
472_Py_Gid_Converter(PyObject *obj, void *p)
473{
474 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200475 long result;
476 if (PyFloat_Check(obj)) {
477 PyErr_SetString(PyExc_TypeError,
478 "integer argument expected, got float");
479 return 0;
480 }
481 result = PyLong_AsLongAndOverflow(obj, &overflow);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200482 if (overflow < 0)
483 goto OverflowDown;
484 if (!overflow && result == -1) {
485 /* error or -1 */
486 if (PyErr_Occurred())
487 return 0;
488 *(gid_t *)p = (gid_t)-1;
489 }
490 else {
491 /* unsigned gid_t */
492 unsigned long uresult;
493 if (overflow > 0) {
494 uresult = PyLong_AsUnsignedLong(obj);
495 if (PyErr_Occurred()) {
496 if (PyErr_ExceptionMatches(PyExc_OverflowError))
497 goto OverflowUp;
498 return 0;
499 }
500 if ((gid_t)uresult == (gid_t)-1)
501 goto OverflowUp;
502 } else {
503 if (result < 0)
504 goto OverflowDown;
505 uresult = result;
506 }
507 if (sizeof(gid_t) < sizeof(long) &&
508 (unsigned long)(gid_t)uresult != uresult)
509 goto OverflowUp;
510 *(gid_t *)p = (gid_t)uresult;
511 }
512 return 1;
513
514OverflowDown:
515 PyErr_SetString(PyExc_OverflowError,
516 "group id is less than minimum");
517 return 0;
518
519OverflowUp:
520 PyErr_SetString(PyExc_OverflowError,
521 "group id is greater than maximum");
522 return 0;
523}
524#endif /* MS_WINDOWS */
525
526
Larry Hastings9cf065c2012-06-22 16:30:09 -0700527#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400528/*
529 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
530 * without the int cast, the value gets interpreted as uint (4291925331),
531 * which doesn't play nicely with all the initializer lines in this file that
532 * look like this:
533 * int dir_fd = DEFAULT_DIR_FD;
534 */
535#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700536#else
537#define DEFAULT_DIR_FD (-100)
538#endif
539
540static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200541_fd_converter(PyObject *o, int *p, const char *allowed)
542{
543 int overflow;
544 long long_value = PyLong_AsLongAndOverflow(o, &overflow);
545 if (PyFloat_Check(o) ||
546 (long_value == -1 && !overflow && PyErr_Occurred())) {
547 PyErr_Clear();
548 PyErr_Format(PyExc_TypeError,
549 "argument should be %s, not %.200s",
550 allowed, Py_TYPE(o)->tp_name);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700551 return 0;
552 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200553 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700554 PyErr_SetString(PyExc_OverflowError,
555 "signed integer is greater than maximum");
556 return 0;
557 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200558 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700559 PyErr_SetString(PyExc_OverflowError,
560 "signed integer is less than minimum");
561 return 0;
562 }
563 *p = (int)long_value;
564 return 1;
565}
566
567static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200568dir_fd_converter(PyObject *o, void *p)
569{
570 if (o == Py_None) {
571 *(int *)p = DEFAULT_DIR_FD;
572 return 1;
573 }
574 return _fd_converter(o, (int *)p, "integer");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700575}
576
577
578
579/*
580 * A PyArg_ParseTuple "converter" function
581 * that handles filesystem paths in the manner
582 * preferred by the os module.
583 *
584 * path_converter accepts (Unicode) strings and their
585 * subclasses, and bytes and their subclasses. What
586 * it does with the argument depends on the platform:
587 *
588 * * On Windows, if we get a (Unicode) string we
589 * extract the wchar_t * and return it; if we get
590 * bytes we extract the char * and return that.
591 *
592 * * On all other platforms, strings are encoded
593 * to bytes using PyUnicode_FSConverter, then we
594 * extract the char * from the bytes object and
595 * return that.
596 *
597 * path_converter also optionally accepts signed
598 * integers (representing open file descriptors) instead
599 * of path strings.
600 *
601 * Input fields:
602 * path.nullable
603 * If nonzero, the path is permitted to be None.
604 * path.allow_fd
605 * If nonzero, the path is permitted to be a file handle
606 * (a signed int) instead of a string.
607 * path.function_name
608 * If non-NULL, path_converter will use that as the name
609 * of the function in error messages.
610 * (If path.argument_name is NULL it omits the function name.)
611 * path.argument_name
612 * If non-NULL, path_converter will use that as the name
613 * of the parameter in error messages.
614 * (If path.argument_name is NULL it uses "path".)
615 *
616 * Output fields:
617 * path.wide
618 * Points to the path if it was expressed as Unicode
619 * and was not encoded. (Only used on Windows.)
620 * path.narrow
621 * Points to the path if it was expressed as bytes,
622 * or it was Unicode and was encoded to bytes.
623 * path.fd
624 * Contains a file descriptor if path.accept_fd was true
625 * and the caller provided a signed integer instead of any
626 * sort of string.
627 *
628 * WARNING: if your "path" parameter is optional, and is
629 * unspecified, path_converter will never get called.
630 * So if you set allow_fd, you *MUST* initialize path.fd = -1
631 * yourself!
632 * path.length
633 * The length of the path in characters, if specified as
634 * a string.
635 * path.object
636 * The original object passed in.
637 * path.cleanup
638 * For internal use only. May point to a temporary object.
639 * (Pay no attention to the man behind the curtain.)
640 *
641 * At most one of path.wide or path.narrow will be non-NULL.
642 * If path was None and path.nullable was set,
643 * or if path was an integer and path.allow_fd was set,
644 * both path.wide and path.narrow will be NULL
645 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200646 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700647 * path_converter takes care to not write to the path_t
648 * unless it's successful. However it must reset the
649 * "cleanup" field each time it's called.
650 *
651 * Use as follows:
652 * path_t path;
653 * memset(&path, 0, sizeof(path));
654 * PyArg_ParseTuple(args, "O&", path_converter, &path);
655 * // ... use values from path ...
656 * path_cleanup(&path);
657 *
658 * (Note that if PyArg_Parse fails you don't need to call
659 * path_cleanup(). However it is safe to do so.)
660 */
661typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100662 const char *function_name;
663 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700664 int nullable;
665 int allow_fd;
666 wchar_t *wide;
667 char *narrow;
668 int fd;
669 Py_ssize_t length;
670 PyObject *object;
671 PyObject *cleanup;
672} path_t;
673
674static void
675path_cleanup(path_t *path) {
676 if (path->cleanup) {
677 Py_DECREF(path->cleanup);
678 path->cleanup = NULL;
679 }
680}
681
682static int
683path_converter(PyObject *o, void *p) {
684 path_t *path = (path_t *)p;
685 PyObject *unicode, *bytes;
686 Py_ssize_t length;
687 char *narrow;
688
689#define FORMAT_EXCEPTION(exc, fmt) \
690 PyErr_Format(exc, "%s%s" fmt, \
691 path->function_name ? path->function_name : "", \
692 path->function_name ? ": " : "", \
693 path->argument_name ? path->argument_name : "path")
694
695 /* Py_CLEANUP_SUPPORTED support */
696 if (o == NULL) {
697 path_cleanup(path);
698 return 1;
699 }
700
701 /* ensure it's always safe to call path_cleanup() */
702 path->cleanup = NULL;
703
704 if (o == Py_None) {
705 if (!path->nullable) {
706 FORMAT_EXCEPTION(PyExc_TypeError,
707 "can't specify None for %s argument");
708 return 0;
709 }
710 path->wide = NULL;
711 path->narrow = NULL;
712 path->length = 0;
713 path->object = o;
714 path->fd = -1;
715 return 1;
716 }
717
718 unicode = PyUnicode_FromObject(o);
719 if (unicode) {
720#ifdef MS_WINDOWS
721 wchar_t *wide;
722 length = PyUnicode_GET_SIZE(unicode);
723 if (length > 32767) {
724 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
725 Py_DECREF(unicode);
726 return 0;
727 }
728
729 wide = PyUnicode_AsUnicode(unicode);
730 if (!wide) {
731 Py_DECREF(unicode);
732 return 0;
733 }
734
735 path->wide = wide;
736 path->narrow = NULL;
737 path->length = length;
738 path->object = o;
739 path->fd = -1;
740 path->cleanup = unicode;
741 return Py_CLEANUP_SUPPORTED;
742#else
743 int converted = PyUnicode_FSConverter(unicode, &bytes);
744 Py_DECREF(unicode);
745 if (!converted)
746 bytes = NULL;
747#endif
748 }
749 else {
750 PyErr_Clear();
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200751 if (PyObject_CheckBuffer(o))
752 bytes = PyBytes_FromObject(o);
753 else
754 bytes = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700755 if (!bytes) {
756 PyErr_Clear();
757 if (path->allow_fd) {
758 int fd;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200759 int result = _fd_converter(o, &fd,
760 "string, bytes or integer");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700761 if (result) {
762 path->wide = NULL;
763 path->narrow = NULL;
764 path->length = 0;
765 path->object = o;
766 path->fd = fd;
767 return result;
768 }
769 }
770 }
771 }
772
773 if (!bytes) {
774 if (!PyErr_Occurred())
775 FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter");
776 return 0;
777 }
778
779#ifdef MS_WINDOWS
780 if (win32_warn_bytes_api()) {
781 Py_DECREF(bytes);
782 return 0;
783 }
784#endif
785
786 length = PyBytes_GET_SIZE(bytes);
787#ifdef MS_WINDOWS
788 if (length > MAX_PATH) {
789 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
790 Py_DECREF(bytes);
791 return 0;
792 }
793#endif
794
795 narrow = PyBytes_AS_STRING(bytes);
796 if (length != strlen(narrow)) {
797 FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s");
798 Py_DECREF(bytes);
799 return 0;
800 }
801
802 path->wide = NULL;
803 path->narrow = narrow;
804 path->length = length;
805 path->object = o;
806 path->fd = -1;
807 path->cleanup = bytes;
808 return Py_CLEANUP_SUPPORTED;
809}
810
811static void
812argument_unavailable_error(char *function_name, char *argument_name) {
813 PyErr_Format(PyExc_NotImplementedError,
814 "%s%s%s unavailable on this platform",
815 (function_name != NULL) ? function_name : "",
816 (function_name != NULL) ? ": ": "",
817 argument_name);
818}
819
820static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200821dir_fd_unavailable(PyObject *o, void *p)
822{
823 int dir_fd;
824 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700825 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200826 if (dir_fd != DEFAULT_DIR_FD) {
827 argument_unavailable_error(NULL, "dir_fd");
828 return 0;
829 }
830 *(int *)p = dir_fd;
831 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700832}
833
834static int
835fd_specified(char *function_name, int fd) {
836 if (fd == -1)
837 return 0;
838
839 argument_unavailable_error(function_name, "fd");
840 return 1;
841}
842
843static int
844follow_symlinks_specified(char *function_name, int follow_symlinks) {
845 if (follow_symlinks)
846 return 0;
847
848 argument_unavailable_error(function_name, "follow_symlinks");
849 return 1;
850}
851
852static int
853path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) {
854 if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) {
855 PyErr_Format(PyExc_ValueError,
856 "%s: can't specify dir_fd without matching path",
857 function_name);
858 return 1;
859 }
860 return 0;
861}
862
863static int
864dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) {
865 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
866 PyErr_Format(PyExc_ValueError,
867 "%s: can't specify both dir_fd and fd",
868 function_name);
869 return 1;
870 }
871 return 0;
872}
873
874static int
875fd_and_follow_symlinks_invalid(char *function_name, int fd,
876 int follow_symlinks) {
877 if ((fd > 0) && (!follow_symlinks)) {
878 PyErr_Format(PyExc_ValueError,
879 "%s: cannot use fd and follow_symlinks together",
880 function_name);
881 return 1;
882 }
883 return 0;
884}
885
886static int
887dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd,
888 int follow_symlinks) {
889 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
890 PyErr_Format(PyExc_ValueError,
891 "%s: cannot use dir_fd and follow_symlinks together",
892 function_name);
893 return 1;
894 }
895 return 0;
896}
897
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200898/* A helper used by a number of POSIX-only functions */
899#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000900static int
901_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000902{
903#if !defined(HAVE_LARGEFILE_SUPPORT)
904 *((off_t*)addr) = PyLong_AsLong(arg);
905#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000906 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000907#endif
908 if (PyErr_Occurred())
909 return 0;
910 return 1;
911}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200912#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000913
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000914#if defined _MSC_VER && _MSC_VER >= 1400
915/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Andrew Svetlov737fb892012-12-18 21:14:22 +0200916 * valid and raise an assertion if it isn't.
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000917 * Normally, an invalid fd is likely to be a C program error and therefore
918 * an assertion can be useful, but it does contradict the POSIX standard
919 * which for write(2) states:
920 * "Otherwise, -1 shall be returned and errno set to indicate the error."
921 * "[EBADF] The fildes argument is not a valid file descriptor open for
922 * writing."
923 * Furthermore, python allows the user to enter any old integer
924 * as a fd and should merely raise a python exception on error.
925 * The Microsoft CRT doesn't provide an official way to check for the
926 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000927 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000928 * internal structures involved.
929 * The structures below must be updated for each version of visual studio
930 * according to the file internal.h in the CRT source, until MS comes
931 * up with a less hacky way to do this.
932 * (all of this is to avoid globally modifying the CRT behaviour using
933 * _set_invalid_parameter_handler() and _CrtSetReportMode())
934 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000935/* The actual size of the structure is determined at runtime.
936 * Only the first items must be present.
937 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000938typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000939 intptr_t osfhnd;
940 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000941} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000942
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000943extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000944#define IOINFO_L2E 5
945#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
946#define IOINFO_ARRAYS 64
947#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
948#define FOPEN 0x01
949#define _NO_CONSOLE_FILENO (intptr_t)-2
950
951/* This function emulates what the windows CRT does to validate file handles */
952int
953_PyVerify_fd(int fd)
954{
Victor Stinner8c62be82010-05-06 00:08:46 +0000955 const int i1 = fd >> IOINFO_L2E;
956 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000957
Antoine Pitrou22e41552010-08-15 18:07:50 +0000958 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000959
Victor Stinner8c62be82010-05-06 00:08:46 +0000960 /* Determine the actual size of the ioinfo structure,
961 * as used by the CRT loaded in memory
962 */
963 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
964 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
965 }
966 if (sizeof_ioinfo == 0) {
967 /* This should not happen... */
968 goto fail;
969 }
970
971 /* See that it isn't a special CLEAR fileno */
972 if (fd != _NO_CONSOLE_FILENO) {
973 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
974 * we check pointer validity and other info
975 */
976 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
977 /* finally, check that the file is open */
978 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
979 if (info->osfile & FOPEN) {
980 return 1;
981 }
982 }
983 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000984 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000985 errno = EBADF;
986 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000987}
988
989/* the special case of checking dup2. The target fd must be in a sensible range */
990static int
991_PyVerify_fd_dup2(int fd1, int fd2)
992{
Victor Stinner8c62be82010-05-06 00:08:46 +0000993 if (!_PyVerify_fd(fd1))
994 return 0;
995 if (fd2 == _NO_CONSOLE_FILENO)
996 return 0;
997 if ((unsigned)fd2 < _NHANDLE_)
998 return 1;
999 else
1000 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001001}
1002#else
1003/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
1004#define _PyVerify_fd_dup2(A, B) (1)
1005#endif
1006
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001007#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001008/* The following structure was copied from
1009 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
1010 include doesn't seem to be present in the Windows SDK (at least as included
1011 with Visual Studio Express). */
1012typedef struct _REPARSE_DATA_BUFFER {
1013 ULONG ReparseTag;
1014 USHORT ReparseDataLength;
1015 USHORT Reserved;
1016 union {
1017 struct {
1018 USHORT SubstituteNameOffset;
1019 USHORT SubstituteNameLength;
1020 USHORT PrintNameOffset;
1021 USHORT PrintNameLength;
1022 ULONG Flags;
1023 WCHAR PathBuffer[1];
1024 } SymbolicLinkReparseBuffer;
1025
1026 struct {
1027 USHORT SubstituteNameOffset;
1028 USHORT SubstituteNameLength;
1029 USHORT PrintNameOffset;
1030 USHORT PrintNameLength;
1031 WCHAR PathBuffer[1];
1032 } MountPointReparseBuffer;
1033
1034 struct {
1035 UCHAR DataBuffer[1];
1036 } GenericReparseBuffer;
1037 };
1038} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
1039
1040#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
1041 GenericReparseBuffer)
1042#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
1043
1044static int
Brian Curtind25aef52011-06-13 15:16:04 -05001045win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001046{
1047 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1048 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
1049 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001050
1051 if (0 == DeviceIoControl(
1052 reparse_point_handle,
1053 FSCTL_GET_REPARSE_POINT,
1054 NULL, 0, /* in buffer */
1055 target_buffer, sizeof(target_buffer),
1056 &n_bytes_returned,
1057 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001058 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001059
1060 if (reparse_tag)
1061 *reparse_tag = rdb->ReparseTag;
1062
Brian Curtind25aef52011-06-13 15:16:04 -05001063 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001064}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001065
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001066#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001067
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001068/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001069#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001070/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001071** environ directly, we must obtain it with _NSGetEnviron(). See also
1072** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001073*/
1074#include <crt_externs.h>
1075static char **environ;
1076#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001077extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001078#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001079
Barry Warsaw53699e91996-12-10 23:23:01 +00001080static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001081convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001082{
Victor Stinner8c62be82010-05-06 00:08:46 +00001083 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001084#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001085 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001086#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001087 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001088#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001089
Victor Stinner8c62be82010-05-06 00:08:46 +00001090 d = PyDict_New();
1091 if (d == NULL)
1092 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001093#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001094 if (environ == NULL)
1095 environ = *_NSGetEnviron();
1096#endif
1097#ifdef MS_WINDOWS
1098 /* _wenviron must be initialized in this way if the program is started
1099 through main() instead of wmain(). */
1100 _wgetenv(L"");
1101 if (_wenviron == NULL)
1102 return d;
1103 /* This part ignores errors */
1104 for (e = _wenviron; *e != NULL; e++) {
1105 PyObject *k;
1106 PyObject *v;
1107 wchar_t *p = wcschr(*e, L'=');
1108 if (p == NULL)
1109 continue;
1110 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1111 if (k == NULL) {
1112 PyErr_Clear();
1113 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001114 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001115 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1116 if (v == NULL) {
1117 PyErr_Clear();
1118 Py_DECREF(k);
1119 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001120 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001121 if (PyDict_GetItem(d, k) == NULL) {
1122 if (PyDict_SetItem(d, k, v) != 0)
1123 PyErr_Clear();
1124 }
1125 Py_DECREF(k);
1126 Py_DECREF(v);
1127 }
1128#else
1129 if (environ == NULL)
1130 return d;
1131 /* This part ignores errors */
1132 for (e = environ; *e != NULL; e++) {
1133 PyObject *k;
1134 PyObject *v;
1135 char *p = strchr(*e, '=');
1136 if (p == NULL)
1137 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001138 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001139 if (k == NULL) {
1140 PyErr_Clear();
1141 continue;
1142 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001143 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001144 if (v == NULL) {
1145 PyErr_Clear();
1146 Py_DECREF(k);
1147 continue;
1148 }
1149 if (PyDict_GetItem(d, k) == NULL) {
1150 if (PyDict_SetItem(d, k, v) != 0)
1151 PyErr_Clear();
1152 }
1153 Py_DECREF(k);
1154 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001155 }
1156#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001157 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001158}
1159
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160/* Set a POSIX-specific error from errno, and return NULL */
1161
Barry Warsawd58d7641998-07-23 16:14:40 +00001162static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001163posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001164{
Victor Stinner8c62be82010-05-06 00:08:46 +00001165 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001166}
Mark Hammondef8b6542001-05-13 08:04:26 +00001167
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001168#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001169static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +00001170win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001171{
Victor Stinner8c62be82010-05-06 00:08:46 +00001172 /* XXX We should pass the function name along in the future.
1173 (winreg.c also wants to pass the function name.)
1174 This would however require an additional param to the
1175 Windows error object, which is non-trivial.
1176 */
1177 errno = GetLastError();
1178 if (filename)
1179 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1180 else
1181 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001182}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001183
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001184static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +02001185win32_error_object(char* function, PyObject* filename)
1186{
1187 /* XXX - see win32_error for comments on 'function' */
1188 errno = GetLastError();
1189 if (filename)
1190 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001191 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001192 errno,
1193 filename);
1194 else
1195 return PyErr_SetFromWindowsErr(errno);
1196}
1197
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001198#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001199
Larry Hastings9cf065c2012-06-22 16:30:09 -07001200static PyObject *
Victor Stinner292c8352012-10-30 02:17:38 +01001201path_error(path_t *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001202{
1203#ifdef MS_WINDOWS
Victor Stinner292c8352012-10-30 02:17:38 +01001204 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
1205 0, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001206#else
Victor Stinner292c8352012-10-30 02:17:38 +01001207 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001208#endif
1209}
1210
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001211/* POSIX generic methods */
1212
Barry Warsaw53699e91996-12-10 23:23:01 +00001213static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001214posix_fildes(PyObject *fdobj, int (*func)(int))
1215{
Victor Stinner8c62be82010-05-06 00:08:46 +00001216 int fd;
1217 int res;
1218 fd = PyObject_AsFileDescriptor(fdobj);
1219 if (fd < 0)
1220 return NULL;
1221 if (!_PyVerify_fd(fd))
1222 return posix_error();
1223 Py_BEGIN_ALLOW_THREADS
1224 res = (*func)(fd);
1225 Py_END_ALLOW_THREADS
1226 if (res < 0)
1227 return posix_error();
1228 Py_INCREF(Py_None);
1229 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001230}
Guido van Rossum21142a01999-01-08 21:05:37 +00001231
1232static PyObject *
Victor Stinner292c8352012-10-30 02:17:38 +01001233posix_1str(const char *func_name, PyObject *args, char *format,
1234 int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001235{
Victor Stinner292c8352012-10-30 02:17:38 +01001236 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00001237 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01001238 memset(&path, 0, sizeof(path));
1239 path.function_name = func_name;
Victor Stinner8c62be82010-05-06 00:08:46 +00001240 if (!PyArg_ParseTuple(args, format,
Victor Stinner292c8352012-10-30 02:17:38 +01001241 path_converter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00001242 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001243 Py_BEGIN_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01001244 res = (*func)(path.narrow);
Victor Stinner8c62be82010-05-06 00:08:46 +00001245 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01001246 if (res < 0) {
1247 path_error(&path);
1248 path_cleanup(&path);
1249 return NULL;
1250 }
1251 path_cleanup(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001252 Py_INCREF(Py_None);
1253 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001254}
1255
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001256
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001257#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001258/* This is a reimplementation of the C library's chdir function,
1259 but one that produces Win32 errors instead of DOS error codes.
1260 chdir is essentially a wrapper around SetCurrentDirectory; however,
1261 it also needs to set "magic" environment variables indicating
1262 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001263static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001264win32_chdir(LPCSTR path)
1265{
Victor Stinner8c62be82010-05-06 00:08:46 +00001266 char new_path[MAX_PATH+1];
1267 int result;
1268 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001269
Victor Stinner8c62be82010-05-06 00:08:46 +00001270 if(!SetCurrentDirectoryA(path))
1271 return FALSE;
1272 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
1273 if (!result)
1274 return FALSE;
1275 /* In the ANSI API, there should not be any paths longer
1276 than MAX_PATH. */
1277 assert(result <= MAX_PATH+1);
1278 if (strncmp(new_path, "\\\\", 2) == 0 ||
1279 strncmp(new_path, "//", 2) == 0)
1280 /* UNC path, nothing to do. */
1281 return TRUE;
1282 env[1] = new_path[0];
1283 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001284}
1285
1286/* The Unicode version differs from the ANSI version
1287 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001288static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001289win32_wchdir(LPCWSTR path)
1290{
Victor Stinner8c62be82010-05-06 00:08:46 +00001291 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
1292 int result;
1293 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001294
Victor Stinner8c62be82010-05-06 00:08:46 +00001295 if(!SetCurrentDirectoryW(path))
1296 return FALSE;
1297 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
1298 if (!result)
1299 return FALSE;
1300 if (result > MAX_PATH+1) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001301 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001302 if (!new_path) {
1303 SetLastError(ERROR_OUTOFMEMORY);
1304 return FALSE;
1305 }
1306 result = GetCurrentDirectoryW(result, new_path);
1307 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001308 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001309 return FALSE;
1310 }
1311 }
1312 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1313 wcsncmp(new_path, L"//", 2) == 0)
1314 /* UNC path, nothing to do. */
1315 return TRUE;
1316 env[1] = new_path[0];
1317 result = SetEnvironmentVariableW(env, new_path);
1318 if (new_path != _new_path)
Victor Stinnerb6404912013-07-07 16:21:41 +02001319 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001320 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001321}
1322#endif
1323
Martin v. Löwis14694662006-02-03 12:54:16 +00001324#ifdef MS_WINDOWS
1325/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1326 - time stamps are restricted to second resolution
1327 - file modification times suffer from forth-and-back conversions between
1328 UTC and local time
1329 Therefore, we implement our own stat, based on the Win32 API directly.
1330*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001331#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001332
1333struct win32_stat{
Brian Curtin87e63a22012-12-31 11:59:48 -06001334 unsigned long st_dev;
Martin v. Löwis14694662006-02-03 12:54:16 +00001335 __int64 st_ino;
1336 unsigned short st_mode;
1337 int st_nlink;
1338 int st_uid;
1339 int st_gid;
Brian Curtin87e63a22012-12-31 11:59:48 -06001340 unsigned long st_rdev;
Martin v. Löwis14694662006-02-03 12:54:16 +00001341 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001342 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001343 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001344 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001345 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001346 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001347 int st_ctime_nsec;
1348};
1349
1350static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1351
1352static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001353FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001354{
Victor Stinner8c62be82010-05-06 00:08:46 +00001355 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1356 /* Cannot simply cast and dereference in_ptr,
1357 since it might not be aligned properly */
1358 __int64 in;
1359 memcpy(&in, in_ptr, sizeof(in));
1360 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001361 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001362}
1363
Thomas Wouters477c8d52006-05-27 19:21:47 +00001364static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001365time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001366{
Victor Stinner8c62be82010-05-06 00:08:46 +00001367 /* XXX endianness */
1368 __int64 out;
1369 out = time_in + secs_between_epochs;
1370 out = out * 10000000 + nsec_in / 100;
1371 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372}
1373
Martin v. Löwis14694662006-02-03 12:54:16 +00001374/* Below, we *know* that ugo+r is 0444 */
1375#if _S_IREAD != 0400
1376#error Unsupported C library
1377#endif
1378static int
1379attributes_to_mode(DWORD attr)
1380{
Victor Stinner8c62be82010-05-06 00:08:46 +00001381 int m = 0;
1382 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1383 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1384 else
1385 m |= _S_IFREG;
1386 if (attr & FILE_ATTRIBUTE_READONLY)
1387 m |= 0444;
1388 else
1389 m |= 0666;
1390 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001391}
1392
1393static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001395{
Victor Stinner8c62be82010-05-06 00:08:46 +00001396 memset(result, 0, sizeof(*result));
1397 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1398 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
Brian Curtin490b32a2012-12-26 07:03:03 -06001399 result->st_dev = info->dwVolumeSerialNumber;
1400 result->st_rdev = result->st_dev;
Victor Stinner8c62be82010-05-06 00:08:46 +00001401 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1402 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1403 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001405 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001406 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1407 /* first clear the S_IFMT bits */
Christian Heimes99d61352013-06-23 23:56:05 +02001408 result->st_mode ^= (result->st_mode & S_IFMT);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001409 /* now set the bits that make this a symlink */
Christian Heimes99d61352013-06-23 23:56:05 +02001410 result->st_mode |= S_IFLNK;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001411 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001412
Victor Stinner8c62be82010-05-06 00:08:46 +00001413 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001414}
1415
Guido van Rossumd8faa362007-04-27 19:54:29 +00001416static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001417attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001418{
Victor Stinner8c62be82010-05-06 00:08:46 +00001419 HANDLE hFindFile;
1420 WIN32_FIND_DATAA FileData;
1421 hFindFile = FindFirstFileA(pszFile, &FileData);
1422 if (hFindFile == INVALID_HANDLE_VALUE)
1423 return FALSE;
1424 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001425 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001426 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001427 info->dwFileAttributes = FileData.dwFileAttributes;
1428 info->ftCreationTime = FileData.ftCreationTime;
1429 info->ftLastAccessTime = FileData.ftLastAccessTime;
1430 info->ftLastWriteTime = FileData.ftLastWriteTime;
1431 info->nFileSizeHigh = FileData.nFileSizeHigh;
1432 info->nFileSizeLow = FileData.nFileSizeLow;
1433/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001434 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1435 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001437}
1438
1439static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001440attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001441{
Victor Stinner8c62be82010-05-06 00:08:46 +00001442 HANDLE hFindFile;
1443 WIN32_FIND_DATAW FileData;
1444 hFindFile = FindFirstFileW(pszFile, &FileData);
1445 if (hFindFile == INVALID_HANDLE_VALUE)
1446 return FALSE;
1447 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001448 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001449 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001450 info->dwFileAttributes = FileData.dwFileAttributes;
1451 info->ftCreationTime = FileData.ftCreationTime;
1452 info->ftLastAccessTime = FileData.ftLastAccessTime;
1453 info->ftLastWriteTime = FileData.ftLastWriteTime;
1454 info->nFileSizeHigh = FileData.nFileSizeHigh;
1455 info->nFileSizeLow = FileData.nFileSizeLow;
1456/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001457 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1458 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001460}
1461
Brian Curtind25aef52011-06-13 15:16:04 -05001462/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
Antoine Pitrou06eecea2012-10-21 16:33:33 +02001463static int has_GetFinalPathNameByHandle = -1;
Brian Curtind25aef52011-06-13 15:16:04 -05001464static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1465 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001466static int
Brian Curtind25aef52011-06-13 15:16:04 -05001467check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001468{
Brian Curtind25aef52011-06-13 15:16:04 -05001469 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001470 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1471 DWORD);
1472
Brian Curtind25aef52011-06-13 15:16:04 -05001473 /* only recheck */
Antoine Pitrou06eecea2012-10-21 16:33:33 +02001474 if (-1 == has_GetFinalPathNameByHandle)
Brian Curtind25aef52011-06-13 15:16:04 -05001475 {
Martin v. Löwis50590f12012-01-14 17:54:09 +01001476 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtind25aef52011-06-13 15:16:04 -05001477 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1478 "GetFinalPathNameByHandleA");
1479 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1480 "GetFinalPathNameByHandleW");
1481 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1482 Py_GetFinalPathNameByHandleW;
1483 }
1484 return has_GetFinalPathNameByHandle;
1485}
1486
1487static BOOL
1488get_target_path(HANDLE hdl, wchar_t **target_path)
1489{
1490 int buf_size, result_length;
1491 wchar_t *buf;
1492
1493 /* We have a good handle to the target, use it to determine
1494 the target path name (then we'll call lstat on it). */
1495 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1496 VOLUME_NAME_DOS);
1497 if(!buf_size)
1498 return FALSE;
1499
Victor Stinnerb6404912013-07-07 16:21:41 +02001500 buf = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001501 if (!buf) {
1502 SetLastError(ERROR_OUTOFMEMORY);
1503 return FALSE;
1504 }
1505
Brian Curtind25aef52011-06-13 15:16:04 -05001506 result_length = Py_GetFinalPathNameByHandleW(hdl,
1507 buf, buf_size, VOLUME_NAME_DOS);
1508
1509 if(!result_length) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001510 PyMem_Free(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001511 return FALSE;
1512 }
1513
1514 if(!CloseHandle(hdl)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001515 PyMem_Free(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001516 return FALSE;
1517 }
1518
1519 buf[result_length] = 0;
1520
1521 *target_path = buf;
1522 return TRUE;
1523}
1524
1525static int
1526win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1527 BOOL traverse);
1528static int
1529win32_xstat_impl(const char *path, struct win32_stat *result,
1530 BOOL traverse)
1531{
Victor Stinner26de69d2011-06-17 15:15:38 +02001532 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001533 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001534 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001535 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001536 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001537 const char *dot;
1538
Brian Curtind25aef52011-06-13 15:16:04 -05001539 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001540 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1541 traverse reparse point. */
1542 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001543 }
1544
Brian Curtinf5e76d02010-11-24 13:14:05 +00001545 hFile = CreateFileA(
1546 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001547 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001548 0, /* share mode */
1549 NULL, /* security attributes */
1550 OPEN_EXISTING,
1551 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001552 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1553 Because of this, calls like GetFinalPathNameByHandle will return
1554 the symlink path agin and not the actual final path. */
1555 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1556 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001557 NULL);
1558
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001559 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001560 /* Either the target doesn't exist, or we don't have access to
1561 get a handle to it. If the former, we need to return an error.
1562 If the latter, we can use attributes_from_dir. */
1563 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001564 return -1;
1565 /* Could not get attributes on open file. Fall back to
1566 reading the directory. */
1567 if (!attributes_from_dir(path, &info, &reparse_tag))
1568 /* Very strange. This should not fail now */
1569 return -1;
1570 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1571 if (traverse) {
1572 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001573 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001574 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001575 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001576 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001577 } else {
1578 if (!GetFileInformationByHandle(hFile, &info)) {
1579 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001580 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001581 }
1582 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001583 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1584 return -1;
1585
1586 /* Close the outer open file handle now that we're about to
1587 reopen it with different flags. */
1588 if (!CloseHandle(hFile))
1589 return -1;
1590
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001591 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001592 /* In order to call GetFinalPathNameByHandle we need to open
1593 the file without the reparse handling flag set. */
1594 hFile2 = CreateFileA(
1595 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1596 NULL, OPEN_EXISTING,
1597 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1598 NULL);
1599 if (hFile2 == INVALID_HANDLE_VALUE)
1600 return -1;
1601
1602 if (!get_target_path(hFile2, &target_path))
1603 return -1;
1604
1605 code = win32_xstat_impl_w(target_path, result, FALSE);
Victor Stinnerb6404912013-07-07 16:21:41 +02001606 PyMem_Free(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001607 return code;
1608 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001609 } else
1610 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001611 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001612 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001613
1614 /* Set S_IEXEC if it is an .exe, .bat, ... */
1615 dot = strrchr(path, '.');
1616 if (dot) {
1617 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1618 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1619 result->st_mode |= 0111;
1620 }
1621 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001622}
1623
1624static int
Brian Curtind25aef52011-06-13 15:16:04 -05001625win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1626 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001627{
1628 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001629 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001630 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001631 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001632 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001633 const wchar_t *dot;
1634
Brian Curtind25aef52011-06-13 15:16:04 -05001635 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001636 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1637 traverse reparse point. */
1638 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001639 }
1640
Brian Curtinf5e76d02010-11-24 13:14:05 +00001641 hFile = CreateFileW(
1642 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001643 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001644 0, /* share mode */
1645 NULL, /* security attributes */
1646 OPEN_EXISTING,
1647 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001648 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1649 Because of this, calls like GetFinalPathNameByHandle will return
1650 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001651 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001652 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001653 NULL);
1654
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001655 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001656 /* Either the target doesn't exist, or we don't have access to
1657 get a handle to it. If the former, we need to return an error.
1658 If the latter, we can use attributes_from_dir. */
1659 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001660 return -1;
1661 /* Could not get attributes on open file. Fall back to
1662 reading the directory. */
1663 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1664 /* Very strange. This should not fail now */
1665 return -1;
1666 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1667 if (traverse) {
1668 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001669 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001670 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001671 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001672 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001673 } else {
1674 if (!GetFileInformationByHandle(hFile, &info)) {
1675 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001676 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001677 }
1678 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001679 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1680 return -1;
1681
1682 /* Close the outer open file handle now that we're about to
1683 reopen it with different flags. */
1684 if (!CloseHandle(hFile))
1685 return -1;
1686
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001687 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001688 /* In order to call GetFinalPathNameByHandle we need to open
1689 the file without the reparse handling flag set. */
1690 hFile2 = CreateFileW(
1691 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1692 NULL, OPEN_EXISTING,
1693 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1694 NULL);
1695 if (hFile2 == INVALID_HANDLE_VALUE)
1696 return -1;
1697
1698 if (!get_target_path(hFile2, &target_path))
1699 return -1;
1700
1701 code = win32_xstat_impl_w(target_path, result, FALSE);
Victor Stinnerb6404912013-07-07 16:21:41 +02001702 PyMem_Free(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001703 return code;
1704 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001705 } else
1706 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001707 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001708 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001709
1710 /* Set S_IEXEC if it is an .exe, .bat, ... */
1711 dot = wcsrchr(path, '.');
1712 if (dot) {
1713 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1714 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1715 result->st_mode |= 0111;
1716 }
1717 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001718}
1719
1720static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001721win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001722{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001723 /* Protocol violation: we explicitly clear errno, instead of
1724 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001725 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001726 errno = 0;
1727 return code;
1728}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001729
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001730static int
1731win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1732{
1733 /* Protocol violation: we explicitly clear errno, instead of
1734 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001735 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001736 errno = 0;
1737 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001738}
Brian Curtind25aef52011-06-13 15:16:04 -05001739/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001740
1741 In Posix, stat automatically traverses symlinks and returns the stat
1742 structure for the target. In Windows, the equivalent GetFileAttributes by
1743 default does not traverse symlinks and instead returns attributes for
1744 the symlink.
1745
1746 Therefore, win32_lstat will get the attributes traditionally, and
1747 win32_stat will first explicitly resolve the symlink target and then will
1748 call win32_lstat on that result.
1749
Ezio Melotti4969f702011-03-15 05:59:46 +02001750 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001751
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001752static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001753win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001754{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001755 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001756}
1757
Victor Stinner8c62be82010-05-06 00:08:46 +00001758static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001759win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001760{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001761 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001762}
1763
1764static int
1765win32_stat(const char* path, struct win32_stat *result)
1766{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001767 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001768}
1769
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001770static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001771win32_stat_w(const wchar_t* path, struct win32_stat *result)
1772{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001773 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001774}
1775
1776static int
1777win32_fstat(int file_number, struct win32_stat *result)
1778{
Victor Stinner8c62be82010-05-06 00:08:46 +00001779 BY_HANDLE_FILE_INFORMATION info;
1780 HANDLE h;
1781 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001782
Richard Oudkerk2240ac12012-07-06 12:05:32 +01001783 if (!_PyVerify_fd(file_number))
1784 h = INVALID_HANDLE_VALUE;
1785 else
1786 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001787
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 /* Protocol violation: we explicitly clear errno, instead of
1789 setting it to a POSIX error. Callers should use GetLastError. */
1790 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001791
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 if (h == INVALID_HANDLE_VALUE) {
1793 /* This is really a C library error (invalid file handle).
1794 We set the Win32 error to the closes one matching. */
1795 SetLastError(ERROR_INVALID_HANDLE);
1796 return -1;
1797 }
1798 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001799
Victor Stinner8c62be82010-05-06 00:08:46 +00001800 type = GetFileType(h);
1801 if (type == FILE_TYPE_UNKNOWN) {
1802 DWORD error = GetLastError();
1803 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001804 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001805 }
1806 /* else: valid but unknown file */
1807 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001808
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 if (type != FILE_TYPE_DISK) {
1810 if (type == FILE_TYPE_CHAR)
1811 result->st_mode = _S_IFCHR;
1812 else if (type == FILE_TYPE_PIPE)
1813 result->st_mode = _S_IFIFO;
1814 return 0;
1815 }
1816
1817 if (!GetFileInformationByHandle(h, &info)) {
1818 return -1;
1819 }
1820
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001821 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001822 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1824 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001825}
1826
1827#endif /* MS_WINDOWS */
1828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001829PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001830"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001831This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001832 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001833or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1834\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001835Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1836or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001837\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001838See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001839
1840static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001841 {"st_mode", "protection bits"},
1842 {"st_ino", "inode"},
1843 {"st_dev", "device"},
1844 {"st_nlink", "number of hard links"},
1845 {"st_uid", "user ID of owner"},
1846 {"st_gid", "group ID of owner"},
1847 {"st_size", "total size, in bytes"},
1848 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1849 {NULL, "integer time of last access"},
1850 {NULL, "integer time of last modification"},
1851 {NULL, "integer time of last change"},
1852 {"st_atime", "time of last access"},
1853 {"st_mtime", "time of last modification"},
1854 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001855 {"st_atime_ns", "time of last access in nanoseconds"},
1856 {"st_mtime_ns", "time of last modification in nanoseconds"},
1857 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001858#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001860#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001861#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001862 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001863#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001864#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001866#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001867#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001869#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001870#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001872#endif
1873#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001874 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001875#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001876 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001877};
1878
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001879#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001880#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001881#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001882#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001883#endif
1884
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001885#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001886#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1887#else
1888#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1889#endif
1890
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001891#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001892#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1893#else
1894#define ST_RDEV_IDX ST_BLOCKS_IDX
1895#endif
1896
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001897#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1898#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1899#else
1900#define ST_FLAGS_IDX ST_RDEV_IDX
1901#endif
1902
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001903#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001904#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001905#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001906#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001907#endif
1908
1909#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1910#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1911#else
1912#define ST_BIRTHTIME_IDX ST_GEN_IDX
1913#endif
1914
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001915static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001916 "stat_result", /* name */
1917 stat_result__doc__, /* doc */
1918 stat_result_fields,
1919 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001920};
1921
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001923"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1924This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001926or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001927\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001928See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001929
1930static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 {"f_bsize", },
1932 {"f_frsize", },
1933 {"f_blocks", },
1934 {"f_bfree", },
1935 {"f_bavail", },
1936 {"f_files", },
1937 {"f_ffree", },
1938 {"f_favail", },
1939 {"f_flag", },
1940 {"f_namemax",},
1941 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942};
1943
1944static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001945 "statvfs_result", /* name */
1946 statvfs_result__doc__, /* doc */
1947 statvfs_result_fields,
1948 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001949};
1950
Ross Lagerwall7807c352011-03-17 20:20:30 +02001951#if defined(HAVE_WAITID) && !defined(__APPLE__)
1952PyDoc_STRVAR(waitid_result__doc__,
1953"waitid_result: Result from waitid.\n\n\
1954This object may be accessed either as a tuple of\n\
1955 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1956or via the attributes si_pid, si_uid, and so on.\n\
1957\n\
1958See os.waitid for more information.");
1959
1960static PyStructSequence_Field waitid_result_fields[] = {
1961 {"si_pid", },
1962 {"si_uid", },
1963 {"si_signo", },
1964 {"si_status", },
1965 {"si_code", },
1966 {0}
1967};
1968
1969static PyStructSequence_Desc waitid_result_desc = {
1970 "waitid_result", /* name */
1971 waitid_result__doc__, /* doc */
1972 waitid_result_fields,
1973 5
1974};
1975static PyTypeObject WaitidResultType;
1976#endif
1977
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001978static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001979static PyTypeObject StatResultType;
1980static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001981#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001982static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001983#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001984static newfunc structseq_new;
1985
1986static PyObject *
1987statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1988{
Victor Stinner8c62be82010-05-06 00:08:46 +00001989 PyStructSequence *result;
1990 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001991
Victor Stinner8c62be82010-05-06 00:08:46 +00001992 result = (PyStructSequence*)structseq_new(type, args, kwds);
1993 if (!result)
1994 return NULL;
1995 /* If we have been initialized from a tuple,
1996 st_?time might be set to None. Initialize it
1997 from the int slots. */
1998 for (i = 7; i <= 9; i++) {
1999 if (result->ob_item[i+3] == Py_None) {
2000 Py_DECREF(Py_None);
2001 Py_INCREF(result->ob_item[i]);
2002 result->ob_item[i+3] = result->ob_item[i];
2003 }
2004 }
2005 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002006}
2007
2008
2009
2010/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00002011static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002012
2013PyDoc_STRVAR(stat_float_times__doc__,
2014"stat_float_times([newval]) -> oldval\n\n\
2015Determine whether os.[lf]stat represents time stamps as float objects.\n\
2016If newval is True, future calls to stat() return floats, if it is False,\n\
2017future calls return ints. \n\
2018If newval is omitted, return the current setting.\n");
2019
2020static PyObject*
2021stat_float_times(PyObject* self, PyObject *args)
2022{
Victor Stinner8c62be82010-05-06 00:08:46 +00002023 int newval = -1;
2024 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
2025 return NULL;
Victor Stinner034d0aa2012-06-05 01:22:15 +02002026 if (PyErr_WarnEx(PyExc_DeprecationWarning,
2027 "stat_float_times() is deprecated",
2028 1))
2029 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002030 if (newval == -1)
2031 /* Return old value */
2032 return PyBool_FromLong(_stat_float_times);
2033 _stat_float_times = newval;
2034 Py_INCREF(Py_None);
2035 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002036}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002037
Larry Hastings6fe20b32012-04-19 15:07:49 -07002038static PyObject *billion = NULL;
2039
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002040static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002041fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002042{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002043 PyObject *s = _PyLong_FromTime_t(sec);
2044 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2045 PyObject *s_in_ns = NULL;
2046 PyObject *ns_total = NULL;
2047 PyObject *float_s = NULL;
2048
2049 if (!(s && ns_fractional))
2050 goto exit;
2051
2052 s_in_ns = PyNumber_Multiply(s, billion);
2053 if (!s_in_ns)
2054 goto exit;
2055
2056 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2057 if (!ns_total)
2058 goto exit;
2059
Victor Stinner4195b5c2012-02-08 23:03:19 +01002060 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07002061 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2062 if (!float_s)
2063 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00002064 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07002065 else {
2066 float_s = s;
2067 Py_INCREF(float_s);
2068 }
2069
2070 PyStructSequence_SET_ITEM(v, index, s);
2071 PyStructSequence_SET_ITEM(v, index+3, float_s);
2072 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2073 s = NULL;
2074 float_s = NULL;
2075 ns_total = NULL;
2076exit:
2077 Py_XDECREF(s);
2078 Py_XDECREF(ns_fractional);
2079 Py_XDECREF(s_in_ns);
2080 Py_XDECREF(ns_total);
2081 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002082}
2083
Tim Peters5aa91602002-01-30 05:46:57 +00002084/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002085 (used by posix_stat() and posix_fstat()) */
2086static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002087_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002088{
Victor Stinner8c62be82010-05-06 00:08:46 +00002089 unsigned long ansec, mnsec, cnsec;
2090 PyObject *v = PyStructSequence_New(&StatResultType);
2091 if (v == NULL)
2092 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002093
Victor Stinner8c62be82010-05-06 00:08:46 +00002094 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00002095#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002096 PyStructSequence_SET_ITEM(v, 1,
2097 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002098#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002099 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002100#endif
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002101#ifdef MS_WINDOWS
2102 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
2103#elif defined(HAVE_LONG_LONG)
Victor Stinner8c62be82010-05-06 00:08:46 +00002104 PyStructSequence_SET_ITEM(v, 2,
2105 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002106#else
Brian Curtin9cc43212013-01-01 12:31:06 -06002107 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002108#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002109 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002110#if defined(MS_WINDOWS)
2111 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2112 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2113#else
2114 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2115 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2116#endif
Fred Drake699f3522000-06-29 21:12:41 +00002117#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002118 PyStructSequence_SET_ITEM(v, 6,
2119 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002120#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002121 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002122#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002123
Martin v. Löwis14694662006-02-03 12:54:16 +00002124#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002125 ansec = st->st_atim.tv_nsec;
2126 mnsec = st->st_mtim.tv_nsec;
2127 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002128#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002129 ansec = st->st_atimespec.tv_nsec;
2130 mnsec = st->st_mtimespec.tv_nsec;
2131 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002132#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002133 ansec = st->st_atime_nsec;
2134 mnsec = st->st_mtime_nsec;
2135 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002136#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002137 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002138#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002139 fill_time(v, 7, st->st_atime, ansec);
2140 fill_time(v, 8, st->st_mtime, mnsec);
2141 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002142
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002143#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002144 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2145 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002146#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002147#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002148 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2149 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002150#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002151#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002152 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2153 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002154#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002155#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002156 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2157 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002158#endif
2159#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002160 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002161 PyObject *val;
2162 unsigned long bsec,bnsec;
2163 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002164#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002165 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002166#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002167 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002168#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002169 if (_stat_float_times) {
2170 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2171 } else {
2172 val = PyLong_FromLong((long)bsec);
2173 }
2174 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2175 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002176 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002177#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002178#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002179 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2180 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002181#endif
Fred Drake699f3522000-06-29 21:12:41 +00002182
Victor Stinner8c62be82010-05-06 00:08:46 +00002183 if (PyErr_Occurred()) {
2184 Py_DECREF(v);
2185 return NULL;
2186 }
Fred Drake699f3522000-06-29 21:12:41 +00002187
Victor Stinner8c62be82010-05-06 00:08:46 +00002188 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002189}
2190
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002191/* POSIX methods */
2192
Guido van Rossum94f6f721999-01-06 18:42:14 +00002193
2194static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002195posix_do_stat(char *function_name, path_t *path,
2196 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002197{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002198 STRUCT_STAT st;
2199 int result;
2200
2201#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2202 if (follow_symlinks_specified(function_name, follow_symlinks))
2203 return NULL;
2204#endif
2205
2206 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2207 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2208 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2209 return NULL;
2210
2211 Py_BEGIN_ALLOW_THREADS
2212 if (path->fd != -1)
2213 result = FSTAT(path->fd, &st);
2214 else
2215#ifdef MS_WINDOWS
2216 if (path->wide) {
2217 if (follow_symlinks)
2218 result = win32_stat_w(path->wide, &st);
2219 else
2220 result = win32_lstat_w(path->wide, &st);
2221 }
2222 else
2223#endif
2224#if defined(HAVE_LSTAT) || defined(MS_WINDOWS)
2225 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2226 result = LSTAT(path->narrow, &st);
2227 else
2228#endif
2229#ifdef HAVE_FSTATAT
2230 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2231 result = fstatat(dir_fd, path->narrow, &st,
2232 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2233 else
2234#endif
2235 result = STAT(path->narrow, &st);
2236 Py_END_ALLOW_THREADS
2237
Victor Stinner292c8352012-10-30 02:17:38 +01002238 if (result != 0) {
2239 return path_error(path);
2240 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002241
2242 return _pystat_fromstructstat(&st);
2243}
2244
2245PyDoc_STRVAR(posix_stat__doc__,
2246"stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\
2247Perform a stat system call on the given path.\n\
2248\n\
2249path may be specified as either a string or as an open file descriptor.\n\
2250\n\
2251If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2252 and path should be relative; path will then be relative to that directory.\n\
2253 dir_fd may not be supported on your platform; if it is unavailable, using\n\
2254 it will raise a NotImplementedError.\n\
2255If follow_symlinks is False, and the last element of the path is a symbolic\n\
2256 link, stat will examine the symbolic link itself instead of the file the\n\
2257 link points to.\n\
2258It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2259 an open file descriptor.");
2260
2261static PyObject *
2262posix_stat(PyObject *self, PyObject *args, PyObject *kwargs)
2263{
2264 static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
2265 path_t path;
2266 int dir_fd = DEFAULT_DIR_FD;
2267 int follow_symlinks = 1;
2268 PyObject *return_value;
2269
2270 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002271 path.function_name = "stat";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002272 path.allow_fd = 1;
2273 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords,
2274 path_converter, &path,
2275#ifdef HAVE_FSTATAT
2276 dir_fd_converter, &dir_fd,
2277#else
2278 dir_fd_unavailable, &dir_fd,
2279#endif
2280 &follow_symlinks))
2281 return NULL;
2282 return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks);
2283 path_cleanup(&path);
2284 return return_value;
2285}
2286
2287PyDoc_STRVAR(posix_lstat__doc__,
2288"lstat(path, *, dir_fd=None) -> stat result\n\n\
2289Like stat(), but do not follow symbolic links.\n\
2290Equivalent to stat(path, follow_symlinks=False).");
2291
2292static PyObject *
2293posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs)
2294{
2295 static char *keywords[] = {"path", "dir_fd", NULL};
2296 path_t path;
2297 int dir_fd = DEFAULT_DIR_FD;
2298 int follow_symlinks = 0;
2299 PyObject *return_value;
2300
2301 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002302 path.function_name = "lstat";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002303 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords,
2304 path_converter, &path,
2305#ifdef HAVE_FSTATAT
2306 dir_fd_converter, &dir_fd
2307#else
2308 dir_fd_unavailable, &dir_fd
2309#endif
2310 ))
2311 return NULL;
2312 return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks);
2313 path_cleanup(&path);
2314 return return_value;
2315}
2316
2317PyDoc_STRVAR(posix_access__doc__,
2318"access(path, mode, *, dir_fd=None, effective_ids=False,\
2319 follow_symlinks=True)\n\n\
2320Use the real uid/gid to test for access to a path. Returns True if granted,\n\
2321False otherwise.\n\
2322\n\
2323If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2324 and path should be relative; path will then be relative to that directory.\n\
2325If effective_ids is True, access will use the effective uid/gid instead of\n\
2326 the real uid/gid.\n\
2327If follow_symlinks is False, and the last element of the path is a symbolic\n\
2328 link, access will examine the symbolic link itself instead of the file the\n\
2329 link points to.\n\
2330dir_fd, effective_ids, and follow_symlinks may not be implemented\n\
2331 on your platform. If they are unavailable, using them will raise a\n\
2332 NotImplementedError.\n\
2333\n\
2334Note that most operations will use the effective uid/gid, therefore this\n\
2335 routine can be used in a suid/sgid environment to test if the invoking user\n\
2336 has the specified access to the path.\n\
2337The mode argument can be F_OK to test existence, or the inclusive-OR\n\
2338 of R_OK, W_OK, and X_OK.");
2339
2340static PyObject *
2341posix_access(PyObject *self, PyObject *args, PyObject *kwargs)
2342{
2343 static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids",
2344 "follow_symlinks", NULL};
2345 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002346 int mode;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002347 int dir_fd = DEFAULT_DIR_FD;
2348 int effective_ids = 0;
2349 int follow_symlinks = 1;
2350 PyObject *return_value = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002351
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002352#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002353 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002354#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002355 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002356#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002357
2358 memset(&path, 0, sizeof(path));
2359 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords,
2360 path_converter, &path, &mode,
2361#ifdef HAVE_FACCESSAT
2362 dir_fd_converter, &dir_fd,
2363#else
2364 dir_fd_unavailable, &dir_fd,
2365#endif
2366 &effective_ids, &follow_symlinks))
2367 return NULL;
2368
2369#ifndef HAVE_FACCESSAT
2370 if (follow_symlinks_specified("access", follow_symlinks))
2371 goto exit;
2372
2373 if (effective_ids) {
2374 argument_unavailable_error("access", "effective_ids");
2375 goto exit;
2376 }
2377#endif
2378
2379#ifdef MS_WINDOWS
2380 Py_BEGIN_ALLOW_THREADS
2381 if (path.wide != NULL)
2382 attr = GetFileAttributesW(path.wide);
2383 else
2384 attr = GetFileAttributesA(path.narrow);
2385 Py_END_ALLOW_THREADS
2386
2387 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002388 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002389 * * we didn't get a -1, and
2390 * * write access wasn't requested,
2391 * * or the file isn't read-only,
2392 * * or it's a directory.
2393 * (Directories cannot be read-only on Windows.)
2394 */
2395 return_value = PyBool_FromLong(
2396 (attr != 0xFFFFFFFF) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002397 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002398 !(attr & FILE_ATTRIBUTE_READONLY) ||
2399 (attr & FILE_ATTRIBUTE_DIRECTORY)));
2400#else
2401
2402 Py_BEGIN_ALLOW_THREADS
2403#ifdef HAVE_FACCESSAT
2404 if ((dir_fd != DEFAULT_DIR_FD) ||
2405 effective_ids ||
2406 !follow_symlinks) {
2407 int flags = 0;
2408 if (!follow_symlinks)
2409 flags |= AT_SYMLINK_NOFOLLOW;
2410 if (effective_ids)
2411 flags |= AT_EACCESS;
2412 result = faccessat(dir_fd, path.narrow, mode, flags);
2413 }
2414 else
2415#endif
2416 result = access(path.narrow, mode);
2417 Py_END_ALLOW_THREADS
2418 return_value = PyBool_FromLong(!result);
2419#endif
2420
2421#ifndef HAVE_FACCESSAT
2422exit:
2423#endif
2424 path_cleanup(&path);
2425 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002426}
2427
Guido van Rossumd371ff11999-01-25 16:12:23 +00002428#ifndef F_OK
2429#define F_OK 0
2430#endif
2431#ifndef R_OK
2432#define R_OK 4
2433#endif
2434#ifndef W_OK
2435#define W_OK 2
2436#endif
2437#ifndef X_OK
2438#define X_OK 1
2439#endif
2440
2441#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002442PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002443"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002444Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00002445
2446static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002447posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002448{
Victor Stinner8c62be82010-05-06 00:08:46 +00002449 int id;
2450 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002451
Victor Stinner8c62be82010-05-06 00:08:46 +00002452 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
2453 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002454
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002455#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002456 /* file descriptor 0 only, the default input device (stdin) */
2457 if (id == 0) {
2458 ret = ttyname();
2459 }
2460 else {
2461 ret = NULL;
2462 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002463#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002464 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002465#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002466 if (ret == NULL)
2467 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002468 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002469}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002470#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002471
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002472#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002473PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002474"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002476
2477static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002478posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002479{
Victor Stinner8c62be82010-05-06 00:08:46 +00002480 char *ret;
2481 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002482
Greg Wardb48bc172000-03-01 21:51:56 +00002483#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002484 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002485#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002487#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002488 if (ret == NULL)
2489 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002490 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002491}
2492#endif
2493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002494PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002495"chdir(path)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002496Change the current working directory to the specified path.\n\
2497\n\
2498path may always be specified as a string.\n\
2499On some platforms, path may also be specified as an open file descriptor.\n\
2500 If this functionality is unavailable, using it raises an exception.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501
Barry Warsaw53699e91996-12-10 23:23:01 +00002502static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002503posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002504{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002505 path_t path;
2506 int result;
2507 PyObject *return_value = NULL;
2508 static char *keywords[] = {"path", NULL};
2509
2510 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002511 path.function_name = "chdir";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002512#ifdef HAVE_FCHDIR
2513 path.allow_fd = 1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002514#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002515 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords,
2516 path_converter, &path
2517 ))
2518 return NULL;
2519
2520 Py_BEGIN_ALLOW_THREADS
2521#ifdef MS_WINDOWS
2522 if (path.wide)
2523 result = win32_wchdir(path.wide);
2524 else
2525 result = win32_chdir(path.narrow);
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03002526 result = !result; /* on unix, success = 0, on windows, success = !0 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002527#else
2528#ifdef HAVE_FCHDIR
2529 if (path.fd != -1)
2530 result = fchdir(path.fd);
2531 else
2532#endif
2533 result = chdir(path.narrow);
2534#endif
2535 Py_END_ALLOW_THREADS
2536
2537 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01002538 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002539 goto exit;
2540 }
2541
2542 return_value = Py_None;
2543 Py_INCREF(Py_None);
Georg Brandlf7875592012-06-24 13:58:31 +02002544
Larry Hastings9cf065c2012-06-22 16:30:09 -07002545exit:
2546 path_cleanup(&path);
2547 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002548}
2549
Fred Drake4d1e64b2002-04-15 19:40:07 +00002550#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002551PyDoc_STRVAR(posix_fchdir__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002552"fchdir(fd)\n\n\
2553Change to the directory of the given file descriptor. fd must be\n\
2554opened on a directory, not a file. Equivalent to os.chdir(fd).");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002555
2556static PyObject *
2557posix_fchdir(PyObject *self, PyObject *fdobj)
2558{
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002560}
2561#endif /* HAVE_FCHDIR */
2562
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002564PyDoc_STRVAR(posix_chmod__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002565"chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\
2566Change the access permissions of a file.\n\
2567\n\
2568path may always be specified as a string.\n\
2569On some platforms, path may also be specified as an open file descriptor.\n\
2570 If this functionality is unavailable, using it raises an exception.\n\
2571If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2572 and path should be relative; path will then be relative to that directory.\n\
2573If follow_symlinks is False, and the last element of the path is a symbolic\n\
2574 link, chmod will modify the symbolic link itself instead of the file the\n\
2575 link points to.\n\
2576It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2577 an open file descriptor.\n\
2578dir_fd and follow_symlinks may not be implemented on your platform.\n\
2579 If they are unavailable, using them will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002580
Barry Warsaw53699e91996-12-10 23:23:01 +00002581static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002582posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002583{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002584 path_t path;
2585 int mode;
2586 int dir_fd = DEFAULT_DIR_FD;
2587 int follow_symlinks = 1;
2588 int result;
2589 PyObject *return_value = NULL;
2590 static char *keywords[] = {"path", "mode", "dir_fd",
2591 "follow_symlinks", NULL};
2592
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002593#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002595#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002596
Larry Hastings9cf065c2012-06-22 16:30:09 -07002597#ifdef HAVE_FCHMODAT
2598 int fchmodat_nofollow_unsupported = 0;
2599#endif
2600
2601 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002602 path.function_name = "chmod";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002603#ifdef HAVE_FCHMOD
2604 path.allow_fd = 1;
2605#endif
2606 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords,
2607 path_converter, &path,
2608 &mode,
2609#ifdef HAVE_FCHMODAT
2610 dir_fd_converter, &dir_fd,
2611#else
2612 dir_fd_unavailable, &dir_fd,
2613#endif
2614 &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002615 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002616
2617#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2618 if (follow_symlinks_specified("chmod", follow_symlinks))
2619 goto exit;
2620#endif
2621
2622#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002623 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002624 if (path.wide)
2625 attr = GetFileAttributesW(path.wide);
2626 else
2627 attr = GetFileAttributesA(path.narrow);
2628 if (attr == 0xFFFFFFFF)
2629 result = 0;
2630 else {
2631 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 attr &= ~FILE_ATTRIBUTE_READONLY;
2633 else
2634 attr |= FILE_ATTRIBUTE_READONLY;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002635 if (path.wide)
2636 result = SetFileAttributesW(path.wide, attr);
2637 else
2638 result = SetFileAttributesA(path.narrow, attr);
2639 }
2640 Py_END_ALLOW_THREADS
2641
2642 if (!result) {
Victor Stinnerb024e842012-10-31 22:24:06 +01002643 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002644 goto exit;
2645 }
2646#else /* MS_WINDOWS */
2647 Py_BEGIN_ALLOW_THREADS
2648#ifdef HAVE_FCHMOD
2649 if (path.fd != -1)
2650 result = fchmod(path.fd, mode);
2651 else
2652#endif
2653#ifdef HAVE_LCHMOD
2654 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2655 result = lchmod(path.narrow, mode);
2656 else
2657#endif
2658#ifdef HAVE_FCHMODAT
2659 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2660 /*
2661 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2662 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002663 * and then says it isn't implemented yet.
2664 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002665 *
2666 * Once it is supported, os.chmod will automatically
2667 * support dir_fd and follow_symlinks=False. (Hopefully.)
2668 * Until then, we need to be careful what exception we raise.
2669 */
2670 result = fchmodat(dir_fd, path.narrow, mode,
2671 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2672 /*
2673 * But wait! We can't throw the exception without allowing threads,
2674 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2675 */
2676 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002677 result &&
2678 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2679 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002680 }
2681 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002682#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002683 result = chmod(path.narrow, mode);
2684 Py_END_ALLOW_THREADS
2685
2686 if (result) {
2687#ifdef HAVE_FCHMODAT
2688 if (fchmodat_nofollow_unsupported) {
2689 if (dir_fd != DEFAULT_DIR_FD)
2690 dir_fd_and_follow_symlinks_invalid("chmod",
2691 dir_fd, follow_symlinks);
2692 else
2693 follow_symlinks_specified("chmod", follow_symlinks);
2694 }
2695 else
2696#endif
Victor Stinner292c8352012-10-30 02:17:38 +01002697 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002698 goto exit;
2699 }
2700#endif
2701
2702 Py_INCREF(Py_None);
2703 return_value = Py_None;
2704exit:
2705 path_cleanup(&path);
2706 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002707}
2708
Larry Hastings9cf065c2012-06-22 16:30:09 -07002709
Christian Heimes4e30a842007-11-30 22:12:06 +00002710#ifdef HAVE_FCHMOD
2711PyDoc_STRVAR(posix_fchmod__doc__,
2712"fchmod(fd, mode)\n\n\
2713Change the access permissions of the file given by file\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002714descriptor fd. Equivalent to os.chmod(fd, mode).");
Christian Heimes4e30a842007-11-30 22:12:06 +00002715
2716static PyObject *
2717posix_fchmod(PyObject *self, PyObject *args)
2718{
Victor Stinner8c62be82010-05-06 00:08:46 +00002719 int fd, mode, res;
2720 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2721 return NULL;
2722 Py_BEGIN_ALLOW_THREADS
2723 res = fchmod(fd, mode);
2724 Py_END_ALLOW_THREADS
2725 if (res < 0)
2726 return posix_error();
2727 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002728}
2729#endif /* HAVE_FCHMOD */
2730
2731#ifdef HAVE_LCHMOD
2732PyDoc_STRVAR(posix_lchmod__doc__,
2733"lchmod(path, mode)\n\n\
2734Change the access permissions of a file. If path is a symlink, this\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002735affects the link itself rather than the target.\n\
2736Equivalent to chmod(path, mode, follow_symlinks=False).");
Christian Heimes4e30a842007-11-30 22:12:06 +00002737
2738static PyObject *
2739posix_lchmod(PyObject *self, PyObject *args)
2740{
Victor Stinner292c8352012-10-30 02:17:38 +01002741 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002742 int i;
2743 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01002744 memset(&path, 0, sizeof(path));
2745 path.function_name = "lchmod";
2746 if (!PyArg_ParseTuple(args, "O&i:lchmod",
2747 path_converter, &path, &i))
Victor Stinner8c62be82010-05-06 00:08:46 +00002748 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 Py_BEGIN_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002750 res = lchmod(path.narrow, i);
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002752 if (res < 0) {
2753 path_error(&path);
2754 path_cleanup(&path);
2755 return NULL;
2756 }
2757 path_cleanup(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002758 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002759}
2760#endif /* HAVE_LCHMOD */
2761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762
Thomas Wouterscf297e42007-02-23 15:07:44 +00002763#ifdef HAVE_CHFLAGS
2764PyDoc_STRVAR(posix_chflags__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002765"chflags(path, flags, *, follow_symlinks=True)\n\n\
2766Set file flags.\n\
2767\n\
2768If follow_symlinks is False, and the last element of the path is a symbolic\n\
2769 link, chflags will change flags on the symbolic link itself instead of the\n\
2770 file the link points to.\n\
2771follow_symlinks may not be implemented on your platform. If it is\n\
2772unavailable, using it will raise a NotImplementedError.");
Thomas Wouterscf297e42007-02-23 15:07:44 +00002773
2774static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002775posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002776{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002777 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002778 unsigned long flags;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002779 int follow_symlinks = 1;
2780 int result;
Victor Stinner45e90392013-07-18 23:57:35 +02002781 PyObject *return_value = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002782 static char *keywords[] = {"path", "flags", "follow_symlinks", NULL};
2783
2784 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002785 path.function_name = "chflags";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002786 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords,
2787 path_converter, &path,
2788 &flags, &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002789 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002790
2791#ifndef HAVE_LCHFLAGS
2792 if (follow_symlinks_specified("chflags", follow_symlinks))
2793 goto exit;
2794#endif
2795
Victor Stinner8c62be82010-05-06 00:08:46 +00002796 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002797#ifdef HAVE_LCHFLAGS
2798 if (!follow_symlinks)
2799 result = lchflags(path.narrow, flags);
2800 else
2801#endif
2802 result = chflags(path.narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002803 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002804
2805 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01002806 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002807 goto exit;
2808 }
2809
2810 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00002811 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002812
2813exit:
2814 path_cleanup(&path);
2815 return return_value;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002816}
2817#endif /* HAVE_CHFLAGS */
2818
2819#ifdef HAVE_LCHFLAGS
2820PyDoc_STRVAR(posix_lchflags__doc__,
2821"lchflags(path, flags)\n\n\
2822Set file flags.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002823This function will not follow symbolic links.\n\
2824Equivalent to chflags(path, flags, follow_symlinks=False).");
Thomas Wouterscf297e42007-02-23 15:07:44 +00002825
2826static PyObject *
2827posix_lchflags(PyObject *self, PyObject *args)
2828{
Victor Stinner292c8352012-10-30 02:17:38 +01002829 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002830 unsigned long flags;
2831 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01002832 memset(&path, 0, sizeof(path));
2833 path.function_name = "lchflags";
Victor Stinner8c62be82010-05-06 00:08:46 +00002834 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Victor Stinner292c8352012-10-30 02:17:38 +01002835 path_converter, &path, &flags))
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002837 Py_BEGIN_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002838 res = lchflags(path.narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002839 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002840 if (res < 0) {
2841 path_error(&path);
2842 path_cleanup(&path);
2843 return NULL;
2844 }
2845 path_cleanup(&path);
2846 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002847}
2848#endif /* HAVE_LCHFLAGS */
2849
Martin v. Löwis244edc82001-10-04 22:44:26 +00002850#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002851PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002852"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002853Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002854
2855static PyObject *
2856posix_chroot(PyObject *self, PyObject *args)
2857{
Victor Stinner292c8352012-10-30 02:17:38 +01002858 return posix_1str("chroot", args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002859}
2860#endif
2861
Guido van Rossum21142a01999-01-08 21:05:37 +00002862#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002863PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002864"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002865force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002866
2867static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002868posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002869{
Stefan Krah0e803b32010-11-26 16:16:47 +00002870 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002871}
2872#endif /* HAVE_FSYNC */
2873
Ross Lagerwall7807c352011-03-17 20:20:30 +02002874#ifdef HAVE_SYNC
2875PyDoc_STRVAR(posix_sync__doc__,
2876"sync()\n\n\
2877Force write of everything to disk.");
2878
2879static PyObject *
2880posix_sync(PyObject *self, PyObject *noargs)
2881{
2882 Py_BEGIN_ALLOW_THREADS
2883 sync();
2884 Py_END_ALLOW_THREADS
2885 Py_RETURN_NONE;
2886}
2887#endif
2888
Guido van Rossum21142a01999-01-08 21:05:37 +00002889#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002890
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002891#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002892extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2893#endif
2894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002895PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002896"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002897force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002898 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002899
2900static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002901posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002902{
Stefan Krah0e803b32010-11-26 16:16:47 +00002903 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002904}
2905#endif /* HAVE_FDATASYNC */
2906
2907
Fredrik Lundh10723342000-07-10 16:38:09 +00002908#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002909PyDoc_STRVAR(posix_chown__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002910"chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\
2911Change the owner and group id of path to the numeric uid and gid.\n\
2912\n\
2913path may always be specified as a string.\n\
2914On some platforms, path may also be specified as an open file descriptor.\n\
2915 If this functionality is unavailable, using it raises an exception.\n\
2916If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2917 and path should be relative; path will then be relative to that directory.\n\
2918If follow_symlinks is False, and the last element of the path is a symbolic\n\
2919 link, chown will modify the symbolic link itself instead of the file the\n\
2920 link points to.\n\
2921It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2922 an open file descriptor.\n\
2923dir_fd and follow_symlinks may not be implemented on your platform.\n\
2924 If they are unavailable, using them will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002925
Barry Warsaw53699e91996-12-10 23:23:01 +00002926static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002927posix_chown(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002928{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002929 path_t path;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002930 uid_t uid;
2931 gid_t gid;
2932 int dir_fd = DEFAULT_DIR_FD;
2933 int follow_symlinks = 1;
2934 int result;
2935 PyObject *return_value = NULL;
2936 static char *keywords[] = {"path", "uid", "gid", "dir_fd",
2937 "follow_symlinks", NULL};
2938
2939 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002940 path.function_name = "chown";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002941#ifdef HAVE_FCHOWN
2942 path.allow_fd = 1;
2943#endif
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002944 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002945 path_converter, &path,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002946 _Py_Uid_Converter, &uid,
2947 _Py_Gid_Converter, &gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002948#ifdef HAVE_FCHOWNAT
2949 dir_fd_converter, &dir_fd,
2950#else
2951 dir_fd_unavailable, &dir_fd,
2952#endif
2953 &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002954 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002955
2956#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
2957 if (follow_symlinks_specified("chown", follow_symlinks))
2958 goto exit;
2959#endif
2960 if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) ||
2961 fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks))
2962 goto exit;
2963
2964#ifdef __APPLE__
2965 /*
2966 * This is for Mac OS X 10.3, which doesn't have lchown.
2967 * (But we still have an lchown symbol because of weak-linking.)
2968 * It doesn't have fchownat either. So there's no possibility
2969 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02002970 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002971 if ((!follow_symlinks) && (lchown == NULL)) {
2972 follow_symlinks_specified("chown", follow_symlinks);
2973 goto exit;
2974 }
2975#endif
2976
Victor Stinner8c62be82010-05-06 00:08:46 +00002977 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002978#ifdef HAVE_FCHOWN
2979 if (path.fd != -1)
2980 result = fchown(path.fd, uid, gid);
2981 else
2982#endif
2983#ifdef HAVE_LCHOWN
2984 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2985 result = lchown(path.narrow, uid, gid);
2986 else
2987#endif
2988#ifdef HAVE_FCHOWNAT
2989 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
2990 result = fchownat(dir_fd, path.narrow, uid, gid,
2991 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2992 else
2993#endif
2994 result = chown(path.narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00002995 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002996
2997 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01002998 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002999 goto exit;
3000 }
3001
3002 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003004
3005exit:
3006 path_cleanup(&path);
3007 return return_value;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003008}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003009#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003010
Christian Heimes4e30a842007-11-30 22:12:06 +00003011#ifdef HAVE_FCHOWN
3012PyDoc_STRVAR(posix_fchown__doc__,
3013"fchown(fd, uid, gid)\n\n\
3014Change the owner and group id of the file given by file descriptor\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003015fd to the numeric uid and gid. Equivalent to os.chown(fd, uid, gid).");
Christian Heimes4e30a842007-11-30 22:12:06 +00003016
3017static PyObject *
3018posix_fchown(PyObject *self, PyObject *args)
3019{
Victor Stinner8c62be82010-05-06 00:08:46 +00003020 int fd;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003021 uid_t uid;
3022 gid_t gid;
Victor Stinner8c62be82010-05-06 00:08:46 +00003023 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003024 if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd,
3025 _Py_Uid_Converter, &uid,
3026 _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00003027 return NULL;
3028 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003029 res = fchown(fd, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003030 Py_END_ALLOW_THREADS
3031 if (res < 0)
3032 return posix_error();
3033 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003034}
3035#endif /* HAVE_FCHOWN */
3036
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003037#ifdef HAVE_LCHOWN
3038PyDoc_STRVAR(posix_lchown__doc__,
3039"lchown(path, uid, gid)\n\n\
3040Change the owner and group id of path to the numeric uid and gid.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003041This function will not follow symbolic links.\n\
3042Equivalent to os.chown(path, uid, gid, follow_symlinks=False).");
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003043
3044static PyObject *
3045posix_lchown(PyObject *self, PyObject *args)
3046{
Victor Stinner292c8352012-10-30 02:17:38 +01003047 path_t path;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003048 uid_t uid;
3049 gid_t gid;
Victor Stinner8c62be82010-05-06 00:08:46 +00003050 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01003051 memset(&path, 0, sizeof(path));
3052 path.function_name = "lchown";
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003053 if (!PyArg_ParseTuple(args, "O&O&O&:lchown",
Victor Stinner292c8352012-10-30 02:17:38 +01003054 path_converter, &path,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003055 _Py_Uid_Converter, &uid,
3056 _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00003057 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003058 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakac2d02002013-02-10 22:03:08 +02003059 res = lchown(path.narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003060 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003061 if (res < 0) {
3062 path_error(&path);
3063 path_cleanup(&path);
3064 return NULL;
3065 }
3066 path_cleanup(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003067 Py_INCREF(Py_None);
3068 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003069}
3070#endif /* HAVE_LCHOWN */
3071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003072
Guido van Rossum36bc6801995-06-14 22:54:23 +00003073#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00003074static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003075posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003076{
Victor Stinner8c62be82010-05-06 00:08:46 +00003077 char buf[1026];
3078 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003079
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003080#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003081 if (!use_bytes) {
3082 wchar_t wbuf[1026];
3083 wchar_t *wbuf2 = wbuf;
3084 PyObject *resobj;
3085 DWORD len;
3086 Py_BEGIN_ALLOW_THREADS
3087 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
3088 /* If the buffer is large enough, len does not include the
3089 terminating \0. If the buffer is too small, len includes
3090 the space needed for the terminator. */
3091 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003092 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003093 if (wbuf2)
3094 len = GetCurrentDirectoryW(len, wbuf2);
3095 }
3096 Py_END_ALLOW_THREADS
3097 if (!wbuf2) {
3098 PyErr_NoMemory();
3099 return NULL;
3100 }
3101 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003102 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003103 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003104 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003105 }
3106 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003107 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003108 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003109 return resobj;
3110 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003111
3112 if (win32_warn_bytes_api())
3113 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003114#endif
3115
Victor Stinner8c62be82010-05-06 00:08:46 +00003116 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003117 res = getcwd(buf, sizeof buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003118 Py_END_ALLOW_THREADS
3119 if (res == NULL)
3120 return posix_error();
3121 if (use_bytes)
3122 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00003123 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003124}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003125
3126PyDoc_STRVAR(posix_getcwd__doc__,
3127"getcwd() -> path\n\n\
3128Return a unicode string representing the current working directory.");
3129
3130static PyObject *
3131posix_getcwd_unicode(PyObject *self)
3132{
3133 return posix_getcwd(0);
3134}
3135
3136PyDoc_STRVAR(posix_getcwdb__doc__,
3137"getcwdb() -> path\n\n\
3138Return a bytes string representing the current working directory.");
3139
3140static PyObject *
3141posix_getcwd_bytes(PyObject *self)
3142{
3143 return posix_getcwd(1);
3144}
Guido van Rossum36bc6801995-06-14 22:54:23 +00003145#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003146
Larry Hastings9cf065c2012-06-22 16:30:09 -07003147#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3148#define HAVE_LINK 1
3149#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003150
Guido van Rossumb6775db1994-08-01 11:34:53 +00003151#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003152PyDoc_STRVAR(posix_link__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003153"link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\
3154Create a hard link to a file.\n\
3155\n\
3156If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
3157 descriptor open to a directory, and the respective path string (src or dst)\n\
3158 should be relative; the path will then be relative to that directory.\n\
3159If follow_symlinks is False, and the last element of src is a symbolic\n\
3160 link, link will create a link to the symbolic link itself instead of the\n\
3161 file the link points to.\n\
3162src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\
3163 platform. If they are unavailable, using them will raise a\n\
3164 NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165
Barry Warsaw53699e91996-12-10 23:23:01 +00003166static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003167posix_link(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003168{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003169 path_t src, dst;
3170 int src_dir_fd = DEFAULT_DIR_FD;
3171 int dst_dir_fd = DEFAULT_DIR_FD;
3172 int follow_symlinks = 1;
3173 PyObject *return_value = NULL;
3174 static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd",
3175 "follow_symlinks", NULL};
3176#ifdef MS_WINDOWS
3177 BOOL result;
3178#else
3179 int result;
3180#endif
3181
3182 memset(&src, 0, sizeof(src));
3183 memset(&dst, 0, sizeof(dst));
Victor Stinner292c8352012-10-30 02:17:38 +01003184 src.function_name = "link";
3185 dst.function_name = "link";
Larry Hastings9cf065c2012-06-22 16:30:09 -07003186 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords,
3187 path_converter, &src,
3188 path_converter, &dst,
3189 dir_fd_converter, &src_dir_fd,
3190 dir_fd_converter, &dst_dir_fd,
3191 &follow_symlinks))
3192 return NULL;
3193
3194#ifndef HAVE_LINKAT
3195 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3196 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
3197 goto exit;
3198 }
3199#endif
3200
3201 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
3202 PyErr_SetString(PyExc_NotImplementedError,
3203 "link: src and dst must be the same type");
3204 goto exit;
3205 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003206
Brian Curtin1b9df392010-11-24 20:24:31 +00003207#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003208 Py_BEGIN_ALLOW_THREADS
3209 if (src.wide)
3210 result = CreateHardLinkW(dst.wide, src.wide, NULL);
3211 else
3212 result = CreateHardLinkA(dst.narrow, src.narrow, NULL);
3213 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003214
Larry Hastings9cf065c2012-06-22 16:30:09 -07003215 if (!result) {
Victor Stinnerafe17062012-10-31 22:47:43 +01003216 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003217 goto exit;
Brian Curtinfc889c42010-11-28 23:59:46 +00003218 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003219#else
3220 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003221#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003222 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3223 (dst_dir_fd != DEFAULT_DIR_FD) ||
3224 (!follow_symlinks))
3225 result = linkat(src_dir_fd, src.narrow,
3226 dst_dir_fd, dst.narrow,
3227 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3228 else
3229#endif
3230 result = link(src.narrow, dst.narrow);
3231 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003232
Larry Hastings9cf065c2012-06-22 16:30:09 -07003233 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01003234 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003235 goto exit;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003236 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003237#endif
3238
3239 return_value = Py_None;
3240 Py_INCREF(Py_None);
3241
3242exit:
3243 path_cleanup(&src);
3244 path_cleanup(&dst);
3245 return return_value;
Brian Curtin1b9df392010-11-24 20:24:31 +00003246}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003247#endif
3248
Brian Curtin1b9df392010-11-24 20:24:31 +00003249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003251PyDoc_STRVAR(posix_listdir__doc__,
Larry Hastingsfdaea062012-06-25 04:42:23 -07003252"listdir(path='.') -> list_of_filenames\n\n\
3253Return a list containing the names of the files in the directory.\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003254The list is in arbitrary order. It does not include the special\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003255entries '.' and '..' even if they are present in the directory.\n\
3256\n\
Larry Hastingsfdaea062012-06-25 04:42:23 -07003257path can be specified as either str or bytes. If path is bytes,\n\
3258 the filenames returned will also be bytes; in all other circumstances\n\
3259 the filenames returned will be str.\n\
3260On some platforms, path may also be specified as an open file descriptor;\n\
3261 the file descriptor must refer to a directory.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003262 If this functionality is unavailable, using it raises NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003263
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003264#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003265static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003266_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003267{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003268 static char *keywords[] = {"path", NULL};
Larry Hastings9cf065c2012-06-22 16:30:09 -07003269 PyObject *v;
3270 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3271 BOOL result;
3272 WIN32_FIND_DATA FileData;
3273 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
3274 char *bufptr = namebuf;
3275 /* only claim to have space for MAX_PATH */
3276 Py_ssize_t len = sizeof(namebuf)-5;
3277 PyObject *po = NULL;
3278 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003279
Gregory P. Smith40a21602013-03-20 20:52:50 -07003280 if (!path->narrow) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003281 WIN32_FIND_DATAW wFileData;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003282 wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003283
Gregory P. Smith40a21602013-03-20 20:52:50 -07003284 if (!path->wide) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003285 po_wchars = L".";
3286 len = 1;
3287 } else {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003288 po_wchars = path->wide;
3289 len = wcslen(path->wide);
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003290 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003291 /* The +5 is so we can append "\\*.*\0" */
Victor Stinnerb6404912013-07-07 16:21:41 +02003292 wnamebuf = PyMem_Malloc((len + 5) * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003293 if (!wnamebuf) {
3294 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003295 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003297 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003299 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00003300 if (wch != L'/' && wch != L'\\' && wch != L':')
3301 wnamebuf[len++] = L'\\';
3302 wcscpy(wnamebuf + len, L"*.*");
3303 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003304 if ((list = PyList_New(0)) == NULL) {
3305 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003307 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003308 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003309 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 if (hFindFile == INVALID_HANDLE_VALUE) {
3311 int error = GetLastError();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003312 if (error == ERROR_FILE_NOT_FOUND)
3313 goto exit;
3314 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003315 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003316 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003317 }
3318 do {
3319 /* Skip over . and .. */
3320 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3321 wcscmp(wFileData.cFileName, L"..") != 0) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003322 v = PyUnicode_FromWideChar(wFileData.cFileName,
3323 wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003325 Py_DECREF(list);
3326 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003327 break;
3328 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003329 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003331 Py_DECREF(list);
3332 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 break;
3334 }
3335 Py_DECREF(v);
3336 }
3337 Py_BEGIN_ALLOW_THREADS
3338 result = FindNextFileW(hFindFile, &wFileData);
3339 Py_END_ALLOW_THREADS
3340 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3341 it got to the end of the directory. */
3342 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003343 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003344 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003345 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003346 }
3347 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003348
Larry Hastings9cf065c2012-06-22 16:30:09 -07003349 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 }
Gregory P. Smith40a21602013-03-20 20:52:50 -07003351 strcpy(namebuf, path->narrow);
3352 len = path->length;
Victor Stinner8c62be82010-05-06 00:08:46 +00003353 if (len > 0) {
3354 char ch = namebuf[len-1];
3355 if (ch != SEP && ch != ALTSEP && ch != ':')
3356 namebuf[len++] = '/';
3357 strcpy(namebuf + len, "*.*");
3358 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00003359
Larry Hastings9cf065c2012-06-22 16:30:09 -07003360 if ((list = PyList_New(0)) == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00003361 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003362
Antoine Pitroub73caab2010-08-09 23:39:31 +00003363 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003364 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003365 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 if (hFindFile == INVALID_HANDLE_VALUE) {
3367 int error = GetLastError();
3368 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003369 goto exit;
3370 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003371 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003372 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003373 }
3374 do {
3375 /* Skip over . and .. */
3376 if (strcmp(FileData.cFileName, ".") != 0 &&
3377 strcmp(FileData.cFileName, "..") != 0) {
3378 v = PyBytes_FromString(FileData.cFileName);
3379 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003380 Py_DECREF(list);
3381 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003382 break;
3383 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003384 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003385 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003386 Py_DECREF(list);
3387 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003388 break;
3389 }
3390 Py_DECREF(v);
3391 }
3392 Py_BEGIN_ALLOW_THREADS
3393 result = FindNextFile(hFindFile, &FileData);
3394 Py_END_ALLOW_THREADS
3395 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3396 it got to the end of the directory. */
3397 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003398 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003399 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003400 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 }
3402 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003403
Larry Hastings9cf065c2012-06-22 16:30:09 -07003404exit:
3405 if (hFindFile != INVALID_HANDLE_VALUE) {
3406 if (FindClose(hFindFile) == FALSE) {
3407 if (list != NULL) {
3408 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003409 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003410 }
3411 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003413 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003414
Larry Hastings9cf065c2012-06-22 16:30:09 -07003415 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003416} /* end of _listdir_windows_no_opendir */
3417
3418#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3419
3420static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003421_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003422{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003423 int fd = -1;
3424
3425 PyObject *v;
3426 DIR *dirp = NULL;
3427 struct dirent *ep;
3428 int return_str; /* if false, return bytes */
3429
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003431#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003432 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003433 /* closedir() closes the FD, so we duplicate it */
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003434 Py_BEGIN_ALLOW_THREADS
Gregory P. Smith40a21602013-03-20 20:52:50 -07003435 fd = dup(path->fd);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003436 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003437
3438 if (fd == -1) {
3439 list = posix_error();
3440 goto exit;
3441 }
3442
Larry Hastingsfdaea062012-06-25 04:42:23 -07003443 return_str = 1;
3444
Larry Hastings9cf065c2012-06-22 16:30:09 -07003445 Py_BEGIN_ALLOW_THREADS
3446 dirp = fdopendir(fd);
3447 Py_END_ALLOW_THREADS
3448 }
3449 else
3450#endif
3451 {
Larry Hastingsfdaea062012-06-25 04:42:23 -07003452 char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003453 if (path->narrow) {
3454 name = path->narrow;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003455 /* only return bytes if they specified a bytes object */
Gregory P. Smith40a21602013-03-20 20:52:50 -07003456 return_str = !(PyBytes_Check(path->object));
Larry Hastingsfdaea062012-06-25 04:42:23 -07003457 }
3458 else {
3459 name = ".";
3460 return_str = 1;
3461 }
3462
Larry Hastings9cf065c2012-06-22 16:30:09 -07003463 Py_BEGIN_ALLOW_THREADS
3464 dirp = opendir(name);
3465 Py_END_ALLOW_THREADS
3466 }
3467
3468 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003469 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003470 goto exit;
3471 }
3472 if ((list = PyList_New(0)) == NULL) {
3473 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003474 }
3475 for (;;) {
3476 errno = 0;
3477 Py_BEGIN_ALLOW_THREADS
3478 ep = readdir(dirp);
3479 Py_END_ALLOW_THREADS
3480 if (ep == NULL) {
3481 if (errno == 0) {
3482 break;
3483 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003484 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003485 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003486 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 }
3488 }
3489 if (ep->d_name[0] == '.' &&
3490 (NAMLEN(ep) == 1 ||
3491 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3492 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003493 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003494 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3495 else
3496 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003497 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003498 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 break;
3500 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003501 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003502 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003503 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 break;
3505 }
3506 Py_DECREF(v);
3507 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003508
Larry Hastings9cf065c2012-06-22 16:30:09 -07003509exit:
3510 if (dirp != NULL) {
3511 Py_BEGIN_ALLOW_THREADS
3512 if (fd > -1)
3513 rewinddir(dirp);
3514 closedir(dirp);
3515 Py_END_ALLOW_THREADS
3516 }
3517
Larry Hastings9cf065c2012-06-22 16:30:09 -07003518 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003519} /* end of _posix_listdir */
3520#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003521
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003522static PyObject *
3523posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
3524{
Gregory P. Smith40a21602013-03-20 20:52:50 -07003525 path_t path;
3526 PyObject *list = NULL;
3527 static char *keywords[] = {"path", NULL};
3528 PyObject *return_value;
3529
3530 memset(&path, 0, sizeof(path));
3531 path.function_name = "listdir";
3532 path.nullable = 1;
3533#ifdef HAVE_FDOPENDIR
3534 path.allow_fd = 1;
3535 path.fd = -1;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003536#endif
Gregory P. Smith40a21602013-03-20 20:52:50 -07003537
3538 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
3539 path_converter, &path)) {
3540 return NULL;
3541 }
3542
3543#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3544 return_value = _listdir_windows_no_opendir(&path, list);
3545#else
3546 return_value = _posix_listdir(&path, list);
3547#endif
3548 path_cleanup(&path);
3549 return return_value;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003550}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003551
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003552#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003553/* A helper function for abspath on win32 */
3554static PyObject *
3555posix__getfullpathname(PyObject *self, PyObject *args)
3556{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003557 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 char outbuf[MAX_PATH*2];
3559 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003560 PyObject *po;
3561
3562 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
3563 {
3564 wchar_t *wpath;
3565 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
3566 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00003567 DWORD result;
3568 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003569
3570 wpath = PyUnicode_AsUnicode(po);
3571 if (wpath == NULL)
3572 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003573 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02003574 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02003576 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003577 woutbufp = PyMem_Malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003578 if (!woutbufp)
3579 return PyErr_NoMemory();
3580 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
3581 }
3582 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003583 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00003584 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02003585 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003586 if (woutbufp != woutbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003587 PyMem_Free(woutbufp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 return v;
3589 }
3590 /* Drop the argument parsing error as narrow strings
3591 are also valid. */
3592 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02003593
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003594 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
3595 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00003596 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003597 if (win32_warn_bytes_api())
3598 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02003599 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003600 outbuf, &temp)) {
3601 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003602 return NULL;
3603 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003604 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
3605 return PyUnicode_Decode(outbuf, strlen(outbuf),
3606 Py_FileSystemDefaultEncoding, NULL);
3607 }
3608 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00003609} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00003610
Brian Curtind25aef52011-06-13 15:16:04 -05003611
Brian Curtinf5e76d02010-11-24 13:14:05 +00003612
Brian Curtind40e6f72010-07-08 21:39:08 +00003613/* A helper function for samepath on windows */
3614static PyObject *
3615posix__getfinalpathname(PyObject *self, PyObject *args)
3616{
3617 HANDLE hFile;
3618 int buf_size;
3619 wchar_t *target_path;
3620 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003621 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003622 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003623
Victor Stinnereb5657a2011-09-30 01:44:27 +02003624 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003625 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003626 path = PyUnicode_AsUnicode(po);
3627 if (path == NULL)
3628 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003629
3630 if(!check_GetFinalPathNameByHandle()) {
3631 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3632 NotImplementedError. */
3633 return PyErr_Format(PyExc_NotImplementedError,
3634 "GetFinalPathNameByHandle not available on this platform");
3635 }
3636
3637 hFile = CreateFileW(
3638 path,
3639 0, /* desired access */
3640 0, /* share mode */
3641 NULL, /* security attributes */
3642 OPEN_EXISTING,
3643 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3644 FILE_FLAG_BACKUP_SEMANTICS,
3645 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003646
Victor Stinnereb5657a2011-09-30 01:44:27 +02003647 if(hFile == INVALID_HANDLE_VALUE)
3648 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003649
3650 /* We have a good handle to the target, use it to determine the
3651 target path name. */
3652 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3653
3654 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003655 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003656
Victor Stinnerb6404912013-07-07 16:21:41 +02003657 target_path = (wchar_t *)PyMem_Malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtind40e6f72010-07-08 21:39:08 +00003658 if(!target_path)
3659 return PyErr_NoMemory();
3660
3661 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3662 buf_size, VOLUME_NAME_DOS);
3663 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003664 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003665
3666 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003667 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003668
3669 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003670 result = PyUnicode_FromWideChar(target_path, result_length);
Victor Stinnerb6404912013-07-07 16:21:41 +02003671 PyMem_Free(target_path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003672 return result;
3673
3674} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003675
Brian Curtin95d028f2011-06-09 09:10:38 -05003676PyDoc_STRVAR(posix__isdir__doc__,
3677"Return true if the pathname refers to an existing directory.");
3678
Brian Curtin9c669cc2011-06-08 18:17:18 -05003679static PyObject *
3680posix__isdir(PyObject *self, PyObject *args)
3681{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003682 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003683 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003684 DWORD attributes;
3685
3686 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003687 wchar_t *wpath = PyUnicode_AsUnicode(po);
3688 if (wpath == NULL)
3689 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003690
3691 attributes = GetFileAttributesW(wpath);
3692 if (attributes == INVALID_FILE_ATTRIBUTES)
3693 Py_RETURN_FALSE;
3694 goto check;
3695 }
3696 /* Drop the argument parsing error as narrow strings
3697 are also valid. */
3698 PyErr_Clear();
3699
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003700 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003701 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003702 if (win32_warn_bytes_api())
3703 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003704 attributes = GetFileAttributesA(path);
3705 if (attributes == INVALID_FILE_ATTRIBUTES)
3706 Py_RETURN_FALSE;
3707
3708check:
3709 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3710 Py_RETURN_TRUE;
3711 else
3712 Py_RETURN_FALSE;
3713}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003714#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003716PyDoc_STRVAR(posix_mkdir__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003717"mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3718Create a directory.\n\
3719\n\
3720If dir_fd is not None, it should be a file descriptor open to a directory,\n\
3721 and path should be relative; path will then be relative to that directory.\n\
3722dir_fd may not be implemented on your platform.\n\
3723 If it is unavailable, using it will raise a NotImplementedError.\n\
3724\n\
3725The mode argument is ignored on Windows.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003726
Barry Warsaw53699e91996-12-10 23:23:01 +00003727static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003728posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003729{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003730 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003731 int mode = 0777;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003732 int dir_fd = DEFAULT_DIR_FD;
3733 static char *keywords[] = {"path", "mode", "dir_fd", NULL};
3734 PyObject *return_value = NULL;
3735 int result;
3736
3737 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01003738 path.function_name = "mkdir";
Larry Hastings9cf065c2012-06-22 16:30:09 -07003739 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords,
3740 path_converter, &path, &mode,
3741#ifdef HAVE_MKDIRAT
3742 dir_fd_converter, &dir_fd
3743#else
3744 dir_fd_unavailable, &dir_fd
3745#endif
3746 ))
3747 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003748
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003749#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003750 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003751 if (path.wide)
3752 result = CreateDirectoryW(path.wide, NULL);
3753 else
3754 result = CreateDirectoryA(path.narrow, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003755 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003756
Larry Hastings9cf065c2012-06-22 16:30:09 -07003757 if (!result) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003758 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003759 goto exit;
3760 }
3761#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003762 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003763#if HAVE_MKDIRAT
3764 if (dir_fd != DEFAULT_DIR_FD)
3765 result = mkdirat(dir_fd, path.narrow, mode);
3766 else
3767#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003768#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003769 result = mkdir(path.narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003770#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003771 result = mkdir(path.narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003772#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003773 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003774 if (result < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01003775 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003776 goto exit;
3777 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003778#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003779 return_value = Py_None;
3780 Py_INCREF(Py_None);
3781exit:
3782 path_cleanup(&path);
3783 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003784}
3785
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003786
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003787/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3788#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003789#include <sys/resource.h>
3790#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003791
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003792
3793#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003794PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003795"nice(inc) -> new_priority\n\n\
3796Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003797
Barry Warsaw53699e91996-12-10 23:23:01 +00003798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003799posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003800{
Victor Stinner8c62be82010-05-06 00:08:46 +00003801 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003802
Victor Stinner8c62be82010-05-06 00:08:46 +00003803 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3804 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003805
Victor Stinner8c62be82010-05-06 00:08:46 +00003806 /* There are two flavours of 'nice': one that returns the new
3807 priority (as required by almost all standards out there) and the
3808 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3809 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003810
Victor Stinner8c62be82010-05-06 00:08:46 +00003811 If we are of the nice family that returns the new priority, we
3812 need to clear errno before the call, and check if errno is filled
3813 before calling posix_error() on a returnvalue of -1, because the
3814 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003815
Victor Stinner8c62be82010-05-06 00:08:46 +00003816 errno = 0;
3817 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003818#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 if (value == 0)
3820 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003821#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003822 if (value == -1 && errno != 0)
3823 /* either nice() or getpriority() returned an error */
3824 return posix_error();
3825 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003826}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003827#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003828
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003829
3830#ifdef HAVE_GETPRIORITY
3831PyDoc_STRVAR(posix_getpriority__doc__,
3832"getpriority(which, who) -> current_priority\n\n\
3833Get program scheduling priority.");
3834
3835static PyObject *
3836posix_getpriority(PyObject *self, PyObject *args)
3837{
3838 int which, who, retval;
3839
3840 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3841 return NULL;
3842 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003843 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003844 if (errno != 0)
3845 return posix_error();
3846 return PyLong_FromLong((long)retval);
3847}
3848#endif /* HAVE_GETPRIORITY */
3849
3850
3851#ifdef HAVE_SETPRIORITY
3852PyDoc_STRVAR(posix_setpriority__doc__,
3853"setpriority(which, who, prio) -> None\n\n\
3854Set program scheduling priority.");
3855
3856static PyObject *
3857posix_setpriority(PyObject *self, PyObject *args)
3858{
3859 int which, who, prio, retval;
3860
3861 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3862 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003863 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003864 if (retval == -1)
3865 return posix_error();
3866 Py_RETURN_NONE;
3867}
3868#endif /* HAVE_SETPRIORITY */
3869
3870
Barry Warsaw53699e91996-12-10 23:23:01 +00003871static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003872internal_rename(PyObject *args, PyObject *kwargs, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003873{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003874 char *function_name = is_replace ? "replace" : "rename";
3875 path_t src;
3876 path_t dst;
3877 int src_dir_fd = DEFAULT_DIR_FD;
3878 int dst_dir_fd = DEFAULT_DIR_FD;
3879 int dir_fd_specified;
3880 PyObject *return_value = NULL;
3881 char format[24];
3882 static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
3883
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003884#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003885 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003886 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003887#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003888 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003889#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003890
3891 memset(&src, 0, sizeof(src));
3892 memset(&dst, 0, sizeof(dst));
Victor Stinner292c8352012-10-30 02:17:38 +01003893 src.function_name = function_name;
3894 dst.function_name = function_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003895 strcpy(format, "O&O&|$O&O&:");
3896 strcat(format, function_name);
3897 if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords,
3898 path_converter, &src,
3899 path_converter, &dst,
3900 dir_fd_converter, &src_dir_fd,
3901 dir_fd_converter, &dst_dir_fd))
3902 return NULL;
3903
3904 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
3905 (dst_dir_fd != DEFAULT_DIR_FD);
3906#ifndef HAVE_RENAMEAT
3907 if (dir_fd_specified) {
3908 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
3909 goto exit;
3910 }
3911#endif
3912
3913 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
3914 PyErr_Format(PyExc_ValueError,
3915 "%s: src and dst must be the same type", function_name);
3916 goto exit;
3917 }
3918
3919#ifdef MS_WINDOWS
3920 Py_BEGIN_ALLOW_THREADS
3921 if (src.wide)
3922 result = MoveFileExW(src.wide, dst.wide, flags);
3923 else
3924 result = MoveFileExA(src.narrow, dst.narrow, flags);
3925 Py_END_ALLOW_THREADS
3926
3927 if (!result) {
Victor Stinnerafe17062012-10-31 22:47:43 +01003928 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003929 goto exit;
3930 }
3931
3932#else
3933 Py_BEGIN_ALLOW_THREADS
3934#ifdef HAVE_RENAMEAT
3935 if (dir_fd_specified)
3936 result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow);
3937 else
3938#endif
3939 result = rename(src.narrow, dst.narrow);
3940 Py_END_ALLOW_THREADS
3941
3942 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01003943 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003944 goto exit;
3945 }
3946#endif
3947
3948 Py_INCREF(Py_None);
3949 return_value = Py_None;
3950exit:
3951 path_cleanup(&src);
3952 path_cleanup(&dst);
3953 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003954}
3955
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003956PyDoc_STRVAR(posix_rename__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003957"rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\
3958Rename a file or directory.\n\
3959\n\
3960If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
3961 descriptor open to a directory, and the respective path string (src or dst)\n\
3962 should be relative; the path will then be relative to that directory.\n\
3963src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\
3964 If they are unavailable, using them will raise a NotImplementedError.");
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003965
3966static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003967posix_rename(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003968{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003969 return internal_rename(args, kwargs, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003970}
3971
3972PyDoc_STRVAR(posix_replace__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003973"replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\
3974Rename a file or directory, overwriting the destination.\n\
3975\n\
3976If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
3977 descriptor open to a directory, and the respective path string (src or dst)\n\
3978 should be relative; the path will then be relative to that directory.\n\
3979src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\
3980 If they are unavailable, using them will raise a NotImplementedError.");
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003981
3982static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003983posix_replace(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003984{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003985 return internal_rename(args, kwargs, 1);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003986}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003987
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003988PyDoc_STRVAR(posix_rmdir__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07003989"rmdir(path, *, dir_fd=None)\n\n\
3990Remove a directory.\n\
3991\n\
3992If dir_fd is not None, it should be a file descriptor open to a directory,\n\
3993 and path should be relative; path will then be relative to that directory.\n\
3994dir_fd may not be implemented on your platform.\n\
3995 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003996
Barry Warsaw53699e91996-12-10 23:23:01 +00003997static PyObject *
Larry Hastingsb698d8e2012-06-23 16:55:07 -07003998posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003999{
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004000 path_t path;
4001 int dir_fd = DEFAULT_DIR_FD;
4002 static char *keywords[] = {"path", "dir_fd", NULL};
4003 int result;
4004 PyObject *return_value = NULL;
4005
4006 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01004007 path.function_name = "rmdir";
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004008 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords,
4009 path_converter, &path,
4010#ifdef HAVE_UNLINKAT
4011 dir_fd_converter, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004012#else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004013 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004014#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004015 ))
4016 return NULL;
4017
4018 Py_BEGIN_ALLOW_THREADS
4019#ifdef MS_WINDOWS
4020 if (path.wide)
4021 result = RemoveDirectoryW(path.wide);
4022 else
4023 result = RemoveDirectoryA(path.narrow);
4024 result = !result; /* Windows, success=1, UNIX, success=0 */
4025#else
4026#ifdef HAVE_UNLINKAT
4027 if (dir_fd != DEFAULT_DIR_FD)
4028 result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR);
4029 else
4030#endif
4031 result = rmdir(path.narrow);
4032#endif
4033 Py_END_ALLOW_THREADS
4034
4035 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01004036 return_value = path_error(&path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004037 goto exit;
4038 }
4039
4040 return_value = Py_None;
4041 Py_INCREF(Py_None);
4042
4043exit:
4044 path_cleanup(&path);
4045 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004046}
4047
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004048
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004049#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004050PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004051"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004052Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004053
Barry Warsaw53699e91996-12-10 23:23:01 +00004054static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004055posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004056{
Victor Stinner8c62be82010-05-06 00:08:46 +00004057 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00004058#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004059 wchar_t *command;
4060 if (!PyArg_ParseTuple(args, "u:system", &command))
4061 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00004062
Victor Stinner8c62be82010-05-06 00:08:46 +00004063 Py_BEGIN_ALLOW_THREADS
4064 sts = _wsystem(command);
4065 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00004066#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004067 PyObject *command_obj;
4068 char *command;
4069 if (!PyArg_ParseTuple(args, "O&:system",
4070 PyUnicode_FSConverter, &command_obj))
4071 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00004072
Victor Stinner8c62be82010-05-06 00:08:46 +00004073 command = PyBytes_AsString(command_obj);
4074 Py_BEGIN_ALLOW_THREADS
4075 sts = system(command);
4076 Py_END_ALLOW_THREADS
4077 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00004078#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004079 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004080}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004081#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004082
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004084PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004085"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004086Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004087
Barry Warsaw53699e91996-12-10 23:23:01 +00004088static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004089posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004090{
Victor Stinner8c62be82010-05-06 00:08:46 +00004091 int i;
4092 if (!PyArg_ParseTuple(args, "i:umask", &i))
4093 return NULL;
4094 i = (int)umask(i);
4095 if (i < 0)
4096 return posix_error();
4097 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004098}
4099
Brian Curtind40e6f72010-07-08 21:39:08 +00004100#ifdef MS_WINDOWS
4101
4102/* override the default DeleteFileW behavior so that directory
4103symlinks can be removed with this function, the same as with
4104Unix symlinks */
4105BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4106{
4107 WIN32_FILE_ATTRIBUTE_DATA info;
4108 WIN32_FIND_DATAW find_data;
4109 HANDLE find_data_handle;
4110 int is_directory = 0;
4111 int is_link = 0;
4112
4113 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4114 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004115
Brian Curtind40e6f72010-07-08 21:39:08 +00004116 /* Get WIN32_FIND_DATA structure for the path to determine if
4117 it is a symlink */
4118 if(is_directory &&
4119 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4120 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4121
4122 if(find_data_handle != INVALID_HANDLE_VALUE) {
4123 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
4124 FindClose(find_data_handle);
4125 }
4126 }
4127 }
4128
4129 if (is_directory && is_link)
4130 return RemoveDirectoryW(lpFileName);
4131
4132 return DeleteFileW(lpFileName);
4133}
4134#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004136PyDoc_STRVAR(posix_unlink__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004137"unlink(path, *, dir_fd=None)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004138Remove a file (same as remove()).\n\
4139\n\
4140If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4141 and path should be relative; path will then be relative to that directory.\n\
4142dir_fd may not be implemented on your platform.\n\
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004143 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004144
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004145PyDoc_STRVAR(posix_remove__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004146"remove(path, *, dir_fd=None)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004147Remove a file (same as unlink()).\n\
4148\n\
4149If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4150 and path should be relative; path will then be relative to that directory.\n\
4151dir_fd may not be implemented on your platform.\n\
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004152 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004153
Barry Warsaw53699e91996-12-10 23:23:01 +00004154static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004155posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004156{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004157 path_t path;
4158 int dir_fd = DEFAULT_DIR_FD;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004159 static char *keywords[] = {"path", "dir_fd", NULL};
Larry Hastings9cf065c2012-06-22 16:30:09 -07004160 int result;
4161 PyObject *return_value = NULL;
4162
4163 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01004164 path.function_name = "unlink";
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004165 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004166 path_converter, &path,
4167#ifdef HAVE_UNLINKAT
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004168 dir_fd_converter, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004169#else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004170 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004171#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004172 ))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004173 return NULL;
4174
4175 Py_BEGIN_ALLOW_THREADS
4176#ifdef MS_WINDOWS
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004177 if (path.wide)
4178 result = Py_DeleteFileW(path.wide);
4179 else
4180 result = DeleteFileA(path.narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004181 result = !result; /* Windows, success=1, UNIX, success=0 */
4182#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004183#ifdef HAVE_UNLINKAT
4184 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004185 result = unlinkat(dir_fd, path.narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004186 else
4187#endif /* HAVE_UNLINKAT */
4188 result = unlink(path.narrow);
4189#endif
4190 Py_END_ALLOW_THREADS
4191
4192 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01004193 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004194 goto exit;
4195 }
4196
4197 return_value = Py_None;
4198 Py_INCREF(Py_None);
4199
4200exit:
4201 path_cleanup(&path);
4202 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004203}
4204
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004206PyDoc_STRVAR(posix_uname__doc__,
Larry Hastings605a62d2012-06-24 04:33:36 -07004207"uname() -> uname_result\n\n\
4208Return an object identifying the current operating system.\n\
4209The object behaves like a named tuple with the following fields:\n\
4210 (sysname, nodename, release, version, machine)");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004211
Larry Hastings605a62d2012-06-24 04:33:36 -07004212static PyStructSequence_Field uname_result_fields[] = {
4213 {"sysname", "operating system name"},
4214 {"nodename", "name of machine on network (implementation-defined)"},
4215 {"release", "operating system release"},
4216 {"version", "operating system version"},
4217 {"machine", "hardware identifier"},
4218 {NULL}
4219};
4220
4221PyDoc_STRVAR(uname_result__doc__,
4222"uname_result: Result from os.uname().\n\n\
4223This object may be accessed either as a tuple of\n\
4224 (sysname, nodename, release, version, machine),\n\
4225or via the attributes sysname, nodename, release, version, and machine.\n\
4226\n\
4227See os.uname for more information.");
4228
4229static PyStructSequence_Desc uname_result_desc = {
4230 "uname_result", /* name */
4231 uname_result__doc__, /* doc */
4232 uname_result_fields,
4233 5
4234};
4235
4236static PyTypeObject UnameResultType;
4237
4238
4239#ifdef HAVE_UNAME
Barry Warsaw53699e91996-12-10 23:23:01 +00004240static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004241posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004242{
Victor Stinner8c62be82010-05-06 00:08:46 +00004243 struct utsname u;
4244 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004245 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004246
Victor Stinner8c62be82010-05-06 00:08:46 +00004247 Py_BEGIN_ALLOW_THREADS
4248 res = uname(&u);
4249 Py_END_ALLOW_THREADS
4250 if (res < 0)
4251 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004252
4253 value = PyStructSequence_New(&UnameResultType);
4254 if (value == NULL)
4255 return NULL;
4256
4257#define SET(i, field) \
4258 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004259 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004260 if (!o) { \
4261 Py_DECREF(value); \
4262 return NULL; \
4263 } \
4264 PyStructSequence_SET_ITEM(value, i, o); \
4265 } \
4266
4267 SET(0, u.sysname);
4268 SET(1, u.nodename);
4269 SET(2, u.release);
4270 SET(3, u.version);
4271 SET(4, u.machine);
4272
4273#undef SET
4274
4275 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004276}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004277#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004278
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004279
Larry Hastings9cf065c2012-06-22 16:30:09 -07004280PyDoc_STRVAR(posix_utime__doc__,
4281"utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\
4282Set the access and modified time of path.\n\
4283\n\
4284path may always be specified as a string.\n\
4285On some platforms, path may also be specified as an open file descriptor.\n\
4286 If this functionality is unavailable, using it raises an exception.\n\
4287\n\
4288If times is not None, it must be a tuple (atime, mtime);\n\
4289 atime and mtime should be expressed as float seconds since the epoch.\n\
4290If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\
4291 atime_ns and mtime_ns should be expressed as integer nanoseconds\n\
4292 since the epoch.\n\
4293If both times and ns are None, utime uses the current time.\n\
4294Specifying tuples for both times and ns is an error.\n\
4295\n\
4296If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4297 and path should be relative; path will then be relative to that directory.\n\
4298If follow_symlinks is False, and the last element of the path is a symbolic\n\
4299 link, utime will modify the symbolic link itself instead of the file the\n\
4300 link points to.\n\
4301It is an error to use dir_fd or follow_symlinks when specifying path\n\
4302 as an open file descriptor.\n\
4303dir_fd and follow_symlinks may not be available on your platform.\n\
4304 If they are unavailable, using them will raise a NotImplementedError.");
4305
4306typedef struct {
4307 int now;
4308 time_t atime_s;
4309 long atime_ns;
4310 time_t mtime_s;
4311 long mtime_ns;
4312} utime_t;
4313
4314/*
4315 * these macros assume that "utime" is a pointer to a utime_t
4316 * they also intentionally leak the declaration of a pointer named "time"
4317 */
4318#define UTIME_TO_TIMESPEC \
4319 struct timespec ts[2]; \
4320 struct timespec *time; \
4321 if (utime->now) \
4322 time = NULL; \
4323 else { \
4324 ts[0].tv_sec = utime->atime_s; \
4325 ts[0].tv_nsec = utime->atime_ns; \
4326 ts[1].tv_sec = utime->mtime_s; \
4327 ts[1].tv_nsec = utime->mtime_ns; \
4328 time = ts; \
4329 } \
4330
4331#define UTIME_TO_TIMEVAL \
4332 struct timeval tv[2]; \
4333 struct timeval *time; \
4334 if (utime->now) \
4335 time = NULL; \
4336 else { \
4337 tv[0].tv_sec = utime->atime_s; \
4338 tv[0].tv_usec = utime->atime_ns / 1000; \
4339 tv[1].tv_sec = utime->mtime_s; \
4340 tv[1].tv_usec = utime->mtime_ns / 1000; \
4341 time = tv; \
4342 } \
4343
4344#define UTIME_TO_UTIMBUF \
4345 struct utimbuf u[2]; \
4346 struct utimbuf *time; \
4347 if (utime->now) \
4348 time = NULL; \
4349 else { \
4350 u.actime = utime->atime_s; \
4351 u.modtime = utime->mtime_s; \
4352 time = u; \
4353 }
4354
4355#define UTIME_TO_TIME_T \
4356 time_t timet[2]; \
4357 struct timet time; \
4358 if (utime->now) \
4359 time = NULL; \
4360 else { \
4361 timet[0] = utime->atime_s; \
4362 timet[1] = utime->mtime_s; \
4363 time = &timet; \
4364 } \
4365
4366
4367#define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT))
4368
4369#if UTIME_HAVE_DIR_FD
4370
4371static int
4372utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks)
4373{
4374#ifdef HAVE_UTIMENSAT
4375 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4376 UTIME_TO_TIMESPEC;
4377 return utimensat(dir_fd, path, time, flags);
4378#elif defined(HAVE_FUTIMESAT)
4379 UTIME_TO_TIMEVAL;
4380 /*
4381 * follow_symlinks will never be false here;
4382 * we only allow !follow_symlinks and dir_fd together
4383 * if we have utimensat()
4384 */
4385 assert(follow_symlinks);
4386 return futimesat(dir_fd, path, time);
4387#endif
4388}
4389
4390#endif
4391
4392#define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS))
4393
4394#if UTIME_HAVE_FD
4395
4396static int
4397utime_fd(utime_t *utime, int fd)
4398{
4399#ifdef HAVE_FUTIMENS
4400 UTIME_TO_TIMESPEC;
4401 return futimens(fd, time);
4402#else
4403 UTIME_TO_TIMEVAL;
4404 return futimes(fd, time);
4405#endif
4406}
4407
4408#endif
4409
4410
4411#define UTIME_HAVE_NOFOLLOW_SYMLINKS \
4412 (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES))
4413
4414#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4415
4416static int
4417utime_nofollow_symlinks(utime_t *utime, char *path)
4418{
4419#ifdef HAVE_UTIMENSAT
4420 UTIME_TO_TIMESPEC;
4421 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4422#else
4423 UTIME_TO_TIMEVAL;
4424 return lutimes(path, time);
4425#endif
4426}
4427
4428#endif
4429
4430#ifndef MS_WINDOWS
4431
4432static int
4433utime_default(utime_t *utime, char *path)
4434{
4435#ifdef HAVE_UTIMENSAT
4436 UTIME_TO_TIMESPEC;
4437 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4438#elif defined(HAVE_UTIMES)
4439 UTIME_TO_TIMEVAL;
4440 return utimes(path, time);
4441#elif defined(HAVE_UTIME_H)
4442 UTIME_TO_UTIMBUF;
4443 return utime(path, time);
4444#else
4445 UTIME_TO_TIME_T;
4446 return utime(path, time);
4447#endif
4448}
4449
4450#endif
4451
Larry Hastings76ad59b2012-05-03 00:30:07 -07004452static int
4453split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4454{
4455 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004456 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004457 divmod = PyNumber_Divmod(py_long, billion);
4458 if (!divmod)
4459 goto exit;
4460 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4461 if ((*s == -1) && PyErr_Occurred())
4462 goto exit;
4463 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004464 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004465 goto exit;
4466
4467 result = 1;
4468exit:
4469 Py_XDECREF(divmod);
4470 return result;
4471}
4472
Larry Hastings9cf065c2012-06-22 16:30:09 -07004473static PyObject *
4474posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
Larry Hastings76ad59b2012-05-03 00:30:07 -07004475{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004476 path_t path;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004477 PyObject *times = NULL;
4478 PyObject *ns = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004479 int dir_fd = DEFAULT_DIR_FD;
4480 int follow_symlinks = 1;
4481 char *keywords[] = {"path", "times", "ns", "dir_fd",
4482 "follow_symlinks", NULL};
Larry Hastings76ad59b2012-05-03 00:30:07 -07004483
Larry Hastings9cf065c2012-06-22 16:30:09 -07004484 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004485
Larry Hastings9cf065c2012-06-22 16:30:09 -07004486#ifdef MS_WINDOWS
4487 HANDLE hFile;
4488 FILETIME atime, mtime;
4489#else
4490 int result;
4491#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004492
Larry Hastings9cf065c2012-06-22 16:30:09 -07004493 PyObject *return_value = NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004494
Larry Hastings9cf065c2012-06-22 16:30:09 -07004495 memset(&path, 0, sizeof(path));
Victor Stinnerb024e842012-10-31 22:24:06 +01004496 path.function_name = "utime";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004497#if UTIME_HAVE_FD
4498 path.allow_fd = 1;
4499#endif
4500 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
4501 "O&|O$OO&p:utime", keywords,
4502 path_converter, &path,
4503 &times, &ns,
4504#if UTIME_HAVE_DIR_FD
4505 dir_fd_converter, &dir_fd,
4506#else
4507 dir_fd_unavailable, &dir_fd,
4508#endif
4509 &follow_symlinks
4510 ))
4511 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004512
Larry Hastings9cf065c2012-06-22 16:30:09 -07004513 if (times && (times != Py_None) && ns) {
4514 PyErr_SetString(PyExc_ValueError,
4515 "utime: you may specify either 'times'"
4516 " or 'ns' but not both");
4517 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004518 }
4519
4520 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004521 time_t a_sec, m_sec;
4522 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004523 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004524 PyErr_SetString(PyExc_TypeError,
4525 "utime: 'times' must be either"
4526 " a tuple of two ints or None");
4527 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004528 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004529 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004530 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004531 &a_sec, &a_nsec) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004532 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004533 &m_sec, &m_nsec) == -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004534 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004535 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004536 utime.atime_s = a_sec;
4537 utime.atime_ns = a_nsec;
4538 utime.mtime_s = m_sec;
4539 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004540 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004541 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004542 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004543 PyErr_SetString(PyExc_TypeError,
4544 "utime: 'ns' must be a tuple of two ints");
4545 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004546 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004547 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004548 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004549 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004550 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004551 &utime.mtime_s, &utime.mtime_ns)) {
4552 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004553 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004554 }
4555 else {
4556 /* times and ns are both None/unspecified. use "now". */
4557 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004558 }
4559
Larry Hastings9cf065c2012-06-22 16:30:09 -07004560#if !UTIME_HAVE_NOFOLLOW_SYMLINKS
4561 if (follow_symlinks_specified("utime", follow_symlinks))
4562 goto exit;
4563#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004564
Larry Hastings9cf065c2012-06-22 16:30:09 -07004565 if (path_and_dir_fd_invalid("utime", &path, dir_fd) ||
4566 dir_fd_and_fd_invalid("utime", dir_fd, path.fd) ||
4567 fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks))
4568 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004569
Larry Hastings9cf065c2012-06-22 16:30:09 -07004570#if !defined(HAVE_UTIMENSAT)
4571 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004572 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004573 "utime: cannot use dir_fd and follow_symlinks "
4574 "together on this platform");
4575 goto exit;
4576 }
4577#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004578
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004579#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004580 Py_BEGIN_ALLOW_THREADS
4581 if (path.wide)
4582 hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004583 NULL, OPEN_EXISTING,
4584 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004585 else
4586 hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004587 NULL, OPEN_EXISTING,
4588 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004589 Py_END_ALLOW_THREADS
4590 if (hFile == INVALID_HANDLE_VALUE) {
Victor Stinnerb024e842012-10-31 22:24:06 +01004591 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004592 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004593 }
4594
Larry Hastings9cf065c2012-06-22 16:30:09 -07004595 if (utime.now) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004596 SYSTEMTIME now;
4597 GetSystemTime(&now);
4598 if (!SystemTimeToFileTime(&now, &mtime) ||
4599 !SystemTimeToFileTime(&now, &atime)) {
Victor Stinnerb024e842012-10-31 22:24:06 +01004600 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004601 goto exit;
Stefan Krah0e803b32010-11-26 16:16:47 +00004602 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004603 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004605 time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4606 time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004607 }
4608 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4609 /* Avoid putting the file name into the error here,
4610 as that may confuse the user into believing that
4611 something is wrong with the file, when it also
4612 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004613 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004614 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004615 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004616#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004617 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004618
Larry Hastings9cf065c2012-06-22 16:30:09 -07004619#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4620 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
4621 result = utime_nofollow_symlinks(&utime, path.narrow);
4622 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004623#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004624
4625#if UTIME_HAVE_DIR_FD
4626 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
4627 result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks);
4628 else
4629#endif
4630
4631#if UTIME_HAVE_FD
4632 if (path.fd != -1)
4633 result = utime_fd(&utime, path.fd);
4634 else
4635#endif
4636
4637 result = utime_default(&utime, path.narrow);
4638
4639 Py_END_ALLOW_THREADS
4640
4641 if (result < 0) {
4642 /* see previous comment about not putting filename in error here */
4643 return_value = posix_error();
4644 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004645 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004646
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004647#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004648
4649 Py_INCREF(Py_None);
4650 return_value = Py_None;
4651
4652exit:
4653 path_cleanup(&path);
4654#ifdef MS_WINDOWS
4655 if (hFile != INVALID_HANDLE_VALUE)
4656 CloseHandle(hFile);
4657#endif
4658 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004659}
4660
Guido van Rossum3b066191991-06-04 19:40:25 +00004661/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004663PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004664"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004665Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004666
Barry Warsaw53699e91996-12-10 23:23:01 +00004667static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004668posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004669{
Victor Stinner8c62be82010-05-06 00:08:46 +00004670 int sts;
4671 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
4672 return NULL;
4673 _exit(sts);
4674 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004675}
4676
Martin v. Löwis114619e2002-10-07 06:44:21 +00004677#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4678static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00004679free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004680{
Victor Stinner8c62be82010-05-06 00:08:46 +00004681 Py_ssize_t i;
4682 for (i = 0; i < count; i++)
4683 PyMem_Free(array[i]);
4684 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004685}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004686
Antoine Pitrou69f71142009-05-24 21:25:49 +00004687static
Martin v. Löwis011e8422009-05-05 04:43:17 +00004688int fsconvert_strdup(PyObject *o, char**out)
4689{
Victor Stinner8c62be82010-05-06 00:08:46 +00004690 PyObject *bytes;
4691 Py_ssize_t size;
4692 if (!PyUnicode_FSConverter(o, &bytes))
4693 return 0;
4694 size = PyBytes_GET_SIZE(bytes);
4695 *out = PyMem_Malloc(size+1);
4696 if (!*out)
4697 return 0;
4698 memcpy(*out, PyBytes_AsString(bytes), size+1);
4699 Py_DECREF(bytes);
4700 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004701}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004702#endif
4703
Ross Lagerwall7807c352011-03-17 20:20:30 +02004704#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00004705static char**
4706parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4707{
Victor Stinner8c62be82010-05-06 00:08:46 +00004708 char **envlist;
4709 Py_ssize_t i, pos, envc;
4710 PyObject *keys=NULL, *vals=NULL;
4711 PyObject *key, *val, *key2, *val2;
4712 char *p, *k, *v;
4713 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004714
Victor Stinner8c62be82010-05-06 00:08:46 +00004715 i = PyMapping_Size(env);
4716 if (i < 0)
4717 return NULL;
4718 envlist = PyMem_NEW(char *, i + 1);
4719 if (envlist == NULL) {
4720 PyErr_NoMemory();
4721 return NULL;
4722 }
4723 envc = 0;
4724 keys = PyMapping_Keys(env);
4725 vals = PyMapping_Values(env);
4726 if (!keys || !vals)
4727 goto error;
4728 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4729 PyErr_Format(PyExc_TypeError,
4730 "env.keys() or env.values() is not a list");
4731 goto error;
4732 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004733
Victor Stinner8c62be82010-05-06 00:08:46 +00004734 for (pos = 0; pos < i; pos++) {
4735 key = PyList_GetItem(keys, pos);
4736 val = PyList_GetItem(vals, pos);
4737 if (!key || !val)
4738 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004739
Victor Stinner8c62be82010-05-06 00:08:46 +00004740 if (PyUnicode_FSConverter(key, &key2) == 0)
4741 goto error;
4742 if (PyUnicode_FSConverter(val, &val2) == 0) {
4743 Py_DECREF(key2);
4744 goto error;
4745 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004746
Victor Stinner8c62be82010-05-06 00:08:46 +00004747 k = PyBytes_AsString(key2);
4748 v = PyBytes_AsString(val2);
4749 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004750
Victor Stinner8c62be82010-05-06 00:08:46 +00004751 p = PyMem_NEW(char, len);
4752 if (p == NULL) {
4753 PyErr_NoMemory();
4754 Py_DECREF(key2);
4755 Py_DECREF(val2);
4756 goto error;
4757 }
4758 PyOS_snprintf(p, len, "%s=%s", k, v);
4759 envlist[envc++] = p;
4760 Py_DECREF(key2);
4761 Py_DECREF(val2);
Victor Stinner8c62be82010-05-06 00:08:46 +00004762 }
4763 Py_DECREF(vals);
4764 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004765
Victor Stinner8c62be82010-05-06 00:08:46 +00004766 envlist[envc] = 0;
4767 *envc_ptr = envc;
4768 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004769
4770error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004771 Py_XDECREF(keys);
4772 Py_XDECREF(vals);
4773 while (--envc >= 0)
4774 PyMem_DEL(envlist[envc]);
4775 PyMem_DEL(envlist);
4776 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004777}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004778
Ross Lagerwall7807c352011-03-17 20:20:30 +02004779static char**
4780parse_arglist(PyObject* argv, Py_ssize_t *argc)
4781{
4782 int i;
4783 char **argvlist = PyMem_NEW(char *, *argc+1);
4784 if (argvlist == NULL) {
4785 PyErr_NoMemory();
4786 return NULL;
4787 }
4788 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004789 PyObject* item = PySequence_ITEM(argv, i);
4790 if (item == NULL)
4791 goto fail;
4792 if (!fsconvert_strdup(item, &argvlist[i])) {
4793 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004794 goto fail;
4795 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004796 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004797 }
4798 argvlist[*argc] = NULL;
4799 return argvlist;
4800fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004801 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004802 free_string_array(argvlist, *argc);
4803 return NULL;
4804}
4805#endif
4806
4807#ifdef HAVE_EXECV
4808PyDoc_STRVAR(posix_execv__doc__,
4809"execv(path, args)\n\n\
4810Execute an executable path with arguments, replacing current process.\n\
4811\n\
4812 path: path of executable file\n\
4813 args: tuple or list of strings");
4814
4815static PyObject *
4816posix_execv(PyObject *self, PyObject *args)
4817{
4818 PyObject *opath;
4819 char *path;
4820 PyObject *argv;
4821 char **argvlist;
4822 Py_ssize_t argc;
4823
4824 /* execv has two arguments: (path, argv), where
4825 argv is a list or tuple of strings. */
4826
4827 if (!PyArg_ParseTuple(args, "O&O:execv",
4828 PyUnicode_FSConverter,
4829 &opath, &argv))
4830 return NULL;
4831 path = PyBytes_AsString(opath);
4832 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4833 PyErr_SetString(PyExc_TypeError,
4834 "execv() arg 2 must be a tuple or list");
4835 Py_DECREF(opath);
4836 return NULL;
4837 }
4838 argc = PySequence_Size(argv);
4839 if (argc < 1) {
4840 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4841 Py_DECREF(opath);
4842 return NULL;
4843 }
4844
4845 argvlist = parse_arglist(argv, &argc);
4846 if (argvlist == NULL) {
4847 Py_DECREF(opath);
4848 return NULL;
4849 }
4850
4851 execv(path, argvlist);
4852
4853 /* If we get here it's definitely an error */
4854
4855 free_string_array(argvlist, argc);
4856 Py_DECREF(opath);
4857 return posix_error();
4858}
4859
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004860PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004861"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004862Execute a path with arguments and environment, replacing current process.\n\
4863\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004864 path: path of executable file\n\
4865 args: tuple or list of arguments\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004866 env: dictionary of strings mapping to strings\n\
4867\n\
4868On some platforms, you may specify an open file descriptor for path;\n\
4869 execve will execute the program the file descriptor is open to.\n\
4870 If this functionality is unavailable, using it raises NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004871
Barry Warsaw53699e91996-12-10 23:23:01 +00004872static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004873posix_execve(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004874{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004875 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00004876 PyObject *argv, *env;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004877 char **argvlist = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004878 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004879 Py_ssize_t argc, envc;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004880 static char *keywords[] = {"path", "argv", "environment", NULL};
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004881
Victor Stinner8c62be82010-05-06 00:08:46 +00004882 /* execve has three arguments: (path, argv, env), where
4883 argv is a list or tuple of strings and env is a dictionary
4884 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004885
Larry Hastings9cf065c2012-06-22 16:30:09 -07004886 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01004887 path.function_name = "execve";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004888#ifdef HAVE_FEXECVE
4889 path.allow_fd = 1;
4890#endif
4891 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords,
4892 path_converter, &path,
4893 &argv, &env
4894 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00004895 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004896
Ross Lagerwall7807c352011-03-17 20:20:30 +02004897 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004898 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004899 "execve: argv must be a tuple or list");
4900 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004901 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004902 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004903 if (!PyMapping_Check(env)) {
4904 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004905 "execve: environment must be a mapping object");
4906 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004907 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004908
Ross Lagerwall7807c352011-03-17 20:20:30 +02004909 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004910 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004911 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004912 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004913
Victor Stinner8c62be82010-05-06 00:08:46 +00004914 envlist = parse_envlist(env, &envc);
4915 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02004916 goto fail;
4917
Larry Hastings9cf065c2012-06-22 16:30:09 -07004918#ifdef HAVE_FEXECVE
4919 if (path.fd > -1)
4920 fexecve(path.fd, argvlist, envlist);
4921 else
4922#endif
4923 execve(path.narrow, argvlist, envlist);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004924
4925 /* If we get here it's definitely an error */
4926
Victor Stinner292c8352012-10-30 02:17:38 +01004927 path_error(&path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004928
4929 while (--envc >= 0)
4930 PyMem_DEL(envlist[envc]);
4931 PyMem_DEL(envlist);
4932 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004933 if (argvlist)
4934 free_string_array(argvlist, argc);
4935 path_cleanup(&path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004936 return NULL;
4937}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004938#endif /* HAVE_EXECV */
4939
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004940
Guido van Rossuma1065681999-01-25 23:20:23 +00004941#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004942PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004943"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004944Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004945\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004946 mode: mode of process creation\n\
4947 path: path of executable file\n\
4948 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004949
4950static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004951posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004952{
Victor Stinner8c62be82010-05-06 00:08:46 +00004953 PyObject *opath;
4954 char *path;
4955 PyObject *argv;
4956 char **argvlist;
4957 int mode, i;
4958 Py_ssize_t argc;
4959 Py_intptr_t spawnval;
4960 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004961
Victor Stinner8c62be82010-05-06 00:08:46 +00004962 /* spawnv has three arguments: (mode, path, argv), where
4963 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004964
Victor Stinner8c62be82010-05-06 00:08:46 +00004965 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4966 PyUnicode_FSConverter,
4967 &opath, &argv))
4968 return NULL;
4969 path = PyBytes_AsString(opath);
4970 if (PyList_Check(argv)) {
4971 argc = PyList_Size(argv);
4972 getitem = PyList_GetItem;
4973 }
4974 else if (PyTuple_Check(argv)) {
4975 argc = PyTuple_Size(argv);
4976 getitem = PyTuple_GetItem;
4977 }
4978 else {
4979 PyErr_SetString(PyExc_TypeError,
4980 "spawnv() arg 2 must be a tuple or list");
4981 Py_DECREF(opath);
4982 return NULL;
4983 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004984
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 argvlist = PyMem_NEW(char *, argc+1);
4986 if (argvlist == NULL) {
4987 Py_DECREF(opath);
4988 return PyErr_NoMemory();
4989 }
4990 for (i = 0; i < argc; i++) {
4991 if (!fsconvert_strdup((*getitem)(argv, i),
4992 &argvlist[i])) {
4993 free_string_array(argvlist, i);
4994 PyErr_SetString(
4995 PyExc_TypeError,
4996 "spawnv() arg 2 must contain only strings");
4997 Py_DECREF(opath);
4998 return NULL;
4999 }
5000 }
5001 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005002
Victor Stinner8c62be82010-05-06 00:08:46 +00005003 if (mode == _OLD_P_OVERLAY)
5004 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005005
Victor Stinner8c62be82010-05-06 00:08:46 +00005006 Py_BEGIN_ALLOW_THREADS
5007 spawnval = _spawnv(mode, path, argvlist);
5008 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005009
Victor Stinner8c62be82010-05-06 00:08:46 +00005010 free_string_array(argvlist, argc);
5011 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00005012
Victor Stinner8c62be82010-05-06 00:08:46 +00005013 if (spawnval == -1)
5014 return posix_error();
5015 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005016 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005017}
5018
5019
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005020PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005021"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00005022Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00005023\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00005024 mode: mode of process creation\n\
5025 path: path of executable file\n\
5026 args: tuple or list of arguments\n\
5027 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00005028
5029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005030posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00005031{
Victor Stinner8c62be82010-05-06 00:08:46 +00005032 PyObject *opath;
5033 char *path;
5034 PyObject *argv, *env;
5035 char **argvlist;
5036 char **envlist;
5037 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005038 int mode;
5039 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 Py_intptr_t spawnval;
5041 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5042 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005043
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 /* spawnve has four arguments: (mode, path, argv, env), where
5045 argv is a list or tuple of strings and env is a dictionary
5046 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005047
Victor Stinner8c62be82010-05-06 00:08:46 +00005048 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
5049 PyUnicode_FSConverter,
5050 &opath, &argv, &env))
5051 return NULL;
5052 path = PyBytes_AsString(opath);
5053 if (PyList_Check(argv)) {
5054 argc = PyList_Size(argv);
5055 getitem = PyList_GetItem;
5056 }
5057 else if (PyTuple_Check(argv)) {
5058 argc = PyTuple_Size(argv);
5059 getitem = PyTuple_GetItem;
5060 }
5061 else {
5062 PyErr_SetString(PyExc_TypeError,
5063 "spawnve() arg 2 must be a tuple or list");
5064 goto fail_0;
5065 }
5066 if (!PyMapping_Check(env)) {
5067 PyErr_SetString(PyExc_TypeError,
5068 "spawnve() arg 3 must be a mapping object");
5069 goto fail_0;
5070 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005071
Victor Stinner8c62be82010-05-06 00:08:46 +00005072 argvlist = PyMem_NEW(char *, argc+1);
5073 if (argvlist == NULL) {
5074 PyErr_NoMemory();
5075 goto fail_0;
5076 }
5077 for (i = 0; i < argc; i++) {
5078 if (!fsconvert_strdup((*getitem)(argv, i),
5079 &argvlist[i]))
5080 {
5081 lastarg = i;
5082 goto fail_1;
5083 }
5084 }
5085 lastarg = argc;
5086 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005087
Victor Stinner8c62be82010-05-06 00:08:46 +00005088 envlist = parse_envlist(env, &envc);
5089 if (envlist == NULL)
5090 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005091
Victor Stinner8c62be82010-05-06 00:08:46 +00005092 if (mode == _OLD_P_OVERLAY)
5093 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005094
Victor Stinner8c62be82010-05-06 00:08:46 +00005095 Py_BEGIN_ALLOW_THREADS
5096 spawnval = _spawnve(mode, path, argvlist, envlist);
5097 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005098
Victor Stinner8c62be82010-05-06 00:08:46 +00005099 if (spawnval == -1)
5100 (void) posix_error();
5101 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005102 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005103
Victor Stinner8c62be82010-05-06 00:08:46 +00005104 while (--envc >= 0)
5105 PyMem_DEL(envlist[envc]);
5106 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005107 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00005108 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005109 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005110 Py_DECREF(opath);
5111 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005112}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005113
Guido van Rossuma1065681999-01-25 23:20:23 +00005114#endif /* HAVE_SPAWNV */
5115
5116
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005117#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005118PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005119"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005120Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
5121\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005122Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005123
5124static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005125posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005126{
Victor Stinner8c62be82010-05-06 00:08:46 +00005127 pid_t pid;
5128 int result = 0;
5129 _PyImport_AcquireLock();
5130 pid = fork1();
5131 if (pid == 0) {
5132 /* child: this clobbers and resets the import lock. */
5133 PyOS_AfterFork();
5134 } else {
5135 /* parent: release the import lock. */
5136 result = _PyImport_ReleaseLock();
5137 }
5138 if (pid == -1)
5139 return posix_error();
5140 if (result < 0) {
5141 /* Don't clobber the OSError if the fork failed. */
5142 PyErr_SetString(PyExc_RuntimeError,
5143 "not holding the import lock");
5144 return NULL;
5145 }
5146 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005147}
5148#endif
5149
5150
Guido van Rossumad0ee831995-03-01 10:34:45 +00005151#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005152PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005153"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005154Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005155Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005156
Barry Warsaw53699e91996-12-10 23:23:01 +00005157static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005158posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005159{
Victor Stinner8c62be82010-05-06 00:08:46 +00005160 pid_t pid;
5161 int result = 0;
5162 _PyImport_AcquireLock();
5163 pid = fork();
5164 if (pid == 0) {
5165 /* child: this clobbers and resets the import lock. */
5166 PyOS_AfterFork();
5167 } else {
5168 /* parent: release the import lock. */
5169 result = _PyImport_ReleaseLock();
5170 }
5171 if (pid == -1)
5172 return posix_error();
5173 if (result < 0) {
5174 /* Don't clobber the OSError if the fork failed. */
5175 PyErr_SetString(PyExc_RuntimeError,
5176 "not holding the import lock");
5177 return NULL;
5178 }
5179 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005180}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005181#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005182
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005183#ifdef HAVE_SCHED_H
5184
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005185#ifdef HAVE_SCHED_GET_PRIORITY_MAX
5186
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005187PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
5188"sched_get_priority_max(policy)\n\n\
5189Get the maximum scheduling priority for *policy*.");
5190
5191static PyObject *
5192posix_sched_get_priority_max(PyObject *self, PyObject *args)
5193{
5194 int policy, max;
5195
5196 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
5197 return NULL;
5198 max = sched_get_priority_max(policy);
5199 if (max < 0)
5200 return posix_error();
5201 return PyLong_FromLong(max);
5202}
5203
5204PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
5205"sched_get_priority_min(policy)\n\n\
5206Get the minimum scheduling priority for *policy*.");
5207
5208static PyObject *
5209posix_sched_get_priority_min(PyObject *self, PyObject *args)
5210{
5211 int policy, min;
5212
5213 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
5214 return NULL;
5215 min = sched_get_priority_min(policy);
5216 if (min < 0)
5217 return posix_error();
5218 return PyLong_FromLong(min);
5219}
5220
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005221#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5222
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005223#ifdef HAVE_SCHED_SETSCHEDULER
5224
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005225PyDoc_STRVAR(posix_sched_getscheduler__doc__,
5226"sched_getscheduler(pid)\n\n\
5227Get the scheduling policy for the process with a PID of *pid*.\n\
5228Passing a PID of 0 returns the scheduling policy for the calling process.");
5229
5230static PyObject *
5231posix_sched_getscheduler(PyObject *self, PyObject *args)
5232{
5233 pid_t pid;
5234 int policy;
5235
5236 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
5237 return NULL;
5238 policy = sched_getscheduler(pid);
5239 if (policy < 0)
5240 return posix_error();
5241 return PyLong_FromLong(policy);
5242}
5243
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005244#endif
5245
5246#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
5247
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005248static PyObject *
5249sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5250{
5251 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05005252 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005253
5254 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
5255 return NULL;
5256 res = PyStructSequence_New(type);
5257 if (!res)
5258 return NULL;
5259 Py_INCREF(priority);
5260 PyStructSequence_SET_ITEM(res, 0, priority);
5261 return res;
5262}
5263
5264PyDoc_STRVAR(sched_param__doc__,
5265"sched_param(sched_priority): A scheduling parameter.\n\n\
5266Current has only one field: sched_priority");
5267
5268static PyStructSequence_Field sched_param_fields[] = {
5269 {"sched_priority", "the scheduling priority"},
5270 {0}
5271};
5272
5273static PyStructSequence_Desc sched_param_desc = {
5274 "sched_param", /* name */
5275 sched_param__doc__, /* doc */
5276 sched_param_fields,
5277 1
5278};
5279
5280static int
5281convert_sched_param(PyObject *param, struct sched_param *res)
5282{
5283 long priority;
5284
5285 if (Py_TYPE(param) != &SchedParamType) {
5286 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5287 return 0;
5288 }
5289 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5290 if (priority == -1 && PyErr_Occurred())
5291 return 0;
5292 if (priority > INT_MAX || priority < INT_MIN) {
5293 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5294 return 0;
5295 }
5296 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5297 return 1;
5298}
5299
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005300#endif
5301
5302#ifdef HAVE_SCHED_SETSCHEDULER
5303
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005304PyDoc_STRVAR(posix_sched_setscheduler__doc__,
5305"sched_setscheduler(pid, policy, param)\n\n\
5306Set the scheduling policy, *policy*, for *pid*.\n\
5307If *pid* is 0, the calling process is changed.\n\
5308*param* is an instance of sched_param.");
5309
5310static PyObject *
5311posix_sched_setscheduler(PyObject *self, PyObject *args)
5312{
5313 pid_t pid;
5314 int policy;
5315 struct sched_param param;
5316
5317 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
5318 &pid, &policy, &convert_sched_param, &param))
5319 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02005320
5321 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005322 ** sched_setscheduler() returns 0 in Linux, but the previous
5323 ** scheduling policy under Solaris/Illumos, and others.
5324 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005325 */
5326 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005327 return posix_error();
5328 Py_RETURN_NONE;
5329}
5330
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005331#endif
5332
5333#ifdef HAVE_SCHED_SETPARAM
5334
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005335PyDoc_STRVAR(posix_sched_getparam__doc__,
5336"sched_getparam(pid) -> sched_param\n\n\
5337Returns scheduling parameters for the process with *pid* as an instance of the\n\
5338sched_param class. A PID of 0 means the calling process.");
5339
5340static PyObject *
5341posix_sched_getparam(PyObject *self, PyObject *args)
5342{
5343 pid_t pid;
5344 struct sched_param param;
5345 PyObject *res, *priority;
5346
5347 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
5348 return NULL;
5349 if (sched_getparam(pid, &param))
5350 return posix_error();
5351 res = PyStructSequence_New(&SchedParamType);
5352 if (!res)
5353 return NULL;
5354 priority = PyLong_FromLong(param.sched_priority);
5355 if (!priority) {
5356 Py_DECREF(res);
5357 return NULL;
5358 }
5359 PyStructSequence_SET_ITEM(res, 0, priority);
5360 return res;
5361}
5362
5363PyDoc_STRVAR(posix_sched_setparam__doc__,
5364"sched_setparam(pid, param)\n\n\
5365Set scheduling parameters for a process with PID *pid*.\n\
5366A PID of 0 means the calling process.");
5367
5368static PyObject *
5369posix_sched_setparam(PyObject *self, PyObject *args)
5370{
5371 pid_t pid;
5372 struct sched_param param;
5373
5374 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
5375 &pid, &convert_sched_param, &param))
5376 return NULL;
5377 if (sched_setparam(pid, &param))
5378 return posix_error();
5379 Py_RETURN_NONE;
5380}
5381
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005382#endif
5383
5384#ifdef HAVE_SCHED_RR_GET_INTERVAL
5385
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005386PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
5387"sched_rr_get_interval(pid) -> float\n\n\
5388Return the round-robin quantum for the process with PID *pid* in seconds.");
5389
5390static PyObject *
5391posix_sched_rr_get_interval(PyObject *self, PyObject *args)
5392{
5393 pid_t pid;
5394 struct timespec interval;
5395
5396 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
5397 return NULL;
5398 if (sched_rr_get_interval(pid, &interval))
5399 return posix_error();
5400 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
5401}
5402
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005403#endif
5404
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005405PyDoc_STRVAR(posix_sched_yield__doc__,
5406"sched_yield()\n\n\
5407Voluntarily relinquish the CPU.");
5408
5409static PyObject *
5410posix_sched_yield(PyObject *self, PyObject *noargs)
5411{
5412 if (sched_yield())
5413 return posix_error();
5414 Py_RETURN_NONE;
5415}
5416
Benjamin Peterson2740af82011-08-02 17:41:34 -05005417#ifdef HAVE_SCHED_SETAFFINITY
5418
Antoine Pitrou84869872012-08-04 16:16:35 +02005419/* The minimum number of CPUs allocated in a cpu_set_t */
5420static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005421
5422PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5423"sched_setaffinity(pid, cpu_set)\n\n\
5424Set the affinity of the process with PID *pid* to *cpu_set*.");
5425
5426static PyObject *
5427posix_sched_setaffinity(PyObject *self, PyObject *args)
5428{
5429 pid_t pid;
Antoine Pitrou84869872012-08-04 16:16:35 +02005430 int ncpus;
5431 size_t setsize;
5432 cpu_set_t *mask = NULL;
5433 PyObject *iterable, *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005434
Antoine Pitrou84869872012-08-04 16:16:35 +02005435 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity",
5436 &pid, &iterable))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005437 return NULL;
Antoine Pitrou84869872012-08-04 16:16:35 +02005438
5439 iterator = PyObject_GetIter(iterable);
5440 if (iterator == NULL)
5441 return NULL;
5442
5443 ncpus = NCPUS_START;
5444 setsize = CPU_ALLOC_SIZE(ncpus);
5445 mask = CPU_ALLOC(ncpus);
5446 if (mask == NULL) {
5447 PyErr_NoMemory();
5448 goto error;
5449 }
5450 CPU_ZERO_S(setsize, mask);
5451
5452 while ((item = PyIter_Next(iterator))) {
5453 long cpu;
5454 if (!PyLong_Check(item)) {
5455 PyErr_Format(PyExc_TypeError,
5456 "expected an iterator of ints, "
5457 "but iterator yielded %R",
5458 Py_TYPE(item));
5459 Py_DECREF(item);
5460 goto error;
5461 }
5462 cpu = PyLong_AsLong(item);
5463 Py_DECREF(item);
5464 if (cpu < 0) {
5465 if (!PyErr_Occurred())
5466 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5467 goto error;
5468 }
5469 if (cpu > INT_MAX - 1) {
5470 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5471 goto error;
5472 }
5473 if (cpu >= ncpus) {
5474 /* Grow CPU mask to fit the CPU number */
5475 int newncpus = ncpus;
5476 cpu_set_t *newmask;
5477 size_t newsetsize;
5478 while (newncpus <= cpu) {
5479 if (newncpus > INT_MAX / 2)
5480 newncpus = cpu + 1;
5481 else
5482 newncpus = newncpus * 2;
5483 }
5484 newmask = CPU_ALLOC(newncpus);
5485 if (newmask == NULL) {
5486 PyErr_NoMemory();
5487 goto error;
5488 }
5489 newsetsize = CPU_ALLOC_SIZE(newncpus);
5490 CPU_ZERO_S(newsetsize, newmask);
5491 memcpy(newmask, mask, setsize);
5492 CPU_FREE(mask);
5493 setsize = newsetsize;
5494 mask = newmask;
5495 ncpus = newncpus;
5496 }
5497 CPU_SET_S(cpu, setsize, mask);
5498 }
5499 Py_CLEAR(iterator);
5500
5501 if (sched_setaffinity(pid, setsize, mask)) {
5502 posix_error();
5503 goto error;
5504 }
5505 CPU_FREE(mask);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005506 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005507
5508error:
5509 if (mask)
5510 CPU_FREE(mask);
5511 Py_XDECREF(iterator);
5512 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005513}
5514
5515PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5516"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5517Return the affinity of the process with PID *pid*.\n\
5518The returned cpu_set will be of size *ncpus*.");
5519
5520static PyObject *
5521posix_sched_getaffinity(PyObject *self, PyObject *args)
5522{
5523 pid_t pid;
Antoine Pitrou84869872012-08-04 16:16:35 +02005524 int cpu, ncpus, count;
5525 size_t setsize;
5526 cpu_set_t *mask = NULL;
5527 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005528
Antoine Pitrou84869872012-08-04 16:16:35 +02005529 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity",
5530 &pid))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005531 return NULL;
Antoine Pitrou84869872012-08-04 16:16:35 +02005532
5533 ncpus = NCPUS_START;
5534 while (1) {
5535 setsize = CPU_ALLOC_SIZE(ncpus);
5536 mask = CPU_ALLOC(ncpus);
5537 if (mask == NULL)
5538 return PyErr_NoMemory();
5539 if (sched_getaffinity(pid, setsize, mask) == 0)
5540 break;
5541 CPU_FREE(mask);
5542 if (errno != EINVAL)
5543 return posix_error();
5544 if (ncpus > INT_MAX / 2) {
5545 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5546 "a large enough CPU set");
5547 return NULL;
5548 }
5549 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005550 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005551
5552 res = PySet_New(NULL);
5553 if (res == NULL)
5554 goto error;
5555 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5556 if (CPU_ISSET_S(cpu, setsize, mask)) {
5557 PyObject *cpu_num = PyLong_FromLong(cpu);
5558 --count;
5559 if (cpu_num == NULL)
5560 goto error;
5561 if (PySet_Add(res, cpu_num)) {
5562 Py_DECREF(cpu_num);
5563 goto error;
5564 }
5565 Py_DECREF(cpu_num);
5566 }
5567 }
5568 CPU_FREE(mask);
5569 return res;
5570
5571error:
5572 if (mask)
5573 CPU_FREE(mask);
5574 Py_XDECREF(res);
5575 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005576}
5577
Benjamin Peterson2740af82011-08-02 17:41:34 -05005578#endif /* HAVE_SCHED_SETAFFINITY */
5579
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005580#endif /* HAVE_SCHED_H */
5581
Neal Norwitzb59798b2003-03-21 01:43:31 +00005582/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005583/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5584#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005585#define DEV_PTY_FILE "/dev/ptc"
5586#define HAVE_DEV_PTMX
5587#else
5588#define DEV_PTY_FILE "/dev/ptmx"
5589#endif
5590
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005591#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005592#ifdef HAVE_PTY_H
5593#include <pty.h>
5594#else
5595#ifdef HAVE_LIBUTIL_H
5596#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005597#else
5598#ifdef HAVE_UTIL_H
5599#include <util.h>
5600#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005601#endif /* HAVE_LIBUTIL_H */
5602#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005603#ifdef HAVE_STROPTS_H
5604#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005605#endif
5606#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005607
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005608#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005609PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005610"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005611Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005612
5613static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005614posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005615{
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005617#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005618 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005619#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005620#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005621 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005622#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005623 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005624#endif
5625#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005626
Thomas Wouters70c21a12000-07-14 14:28:33 +00005627#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005628 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5629 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005630#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5632 if (slave_name == NULL)
5633 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005634
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 slave_fd = open(slave_name, O_RDWR);
5636 if (slave_fd < 0)
5637 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005638#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005639 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5640 if (master_fd < 0)
5641 return posix_error();
5642 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5643 /* change permission of slave */
5644 if (grantpt(master_fd) < 0) {
5645 PyOS_setsig(SIGCHLD, sig_saved);
5646 return posix_error();
5647 }
5648 /* unlock slave */
5649 if (unlockpt(master_fd) < 0) {
5650 PyOS_setsig(SIGCHLD, sig_saved);
5651 return posix_error();
5652 }
5653 PyOS_setsig(SIGCHLD, sig_saved);
5654 slave_name = ptsname(master_fd); /* get name of slave */
5655 if (slave_name == NULL)
5656 return posix_error();
5657 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5658 if (slave_fd < 0)
5659 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005660#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005661 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5662 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005663#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005664 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005665#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005666#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005667#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005668
Victor Stinner8c62be82010-05-06 00:08:46 +00005669 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005670
Fred Drake8cef4cf2000-06-28 16:40:38 +00005671}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005672#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005673
5674#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005675PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005676"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005677Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5678Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005679To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005680
5681static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005682posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005683{
Victor Stinner8c62be82010-05-06 00:08:46 +00005684 int master_fd = -1, result = 0;
5685 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005686
Victor Stinner8c62be82010-05-06 00:08:46 +00005687 _PyImport_AcquireLock();
5688 pid = forkpty(&master_fd, NULL, NULL, NULL);
5689 if (pid == 0) {
5690 /* child: this clobbers and resets the import lock. */
5691 PyOS_AfterFork();
5692 } else {
5693 /* parent: release the import lock. */
5694 result = _PyImport_ReleaseLock();
5695 }
5696 if (pid == -1)
5697 return posix_error();
5698 if (result < 0) {
5699 /* Don't clobber the OSError if the fork failed. */
5700 PyErr_SetString(PyExc_RuntimeError,
5701 "not holding the import lock");
5702 return NULL;
5703 }
5704 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005705}
5706#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005707
Ross Lagerwall7807c352011-03-17 20:20:30 +02005708
Guido van Rossumad0ee831995-03-01 10:34:45 +00005709#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005710PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005711"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005712Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005713
Barry Warsaw53699e91996-12-10 23:23:01 +00005714static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005715posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005716{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005717 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005718}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005719#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005720
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005721
Guido van Rossumad0ee831995-03-01 10:34:45 +00005722#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005723PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005724"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005725Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005726
Barry Warsaw53699e91996-12-10 23:23:01 +00005727static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005728posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005729{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005730 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005731}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005732#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005733
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005734
Guido van Rossumad0ee831995-03-01 10:34:45 +00005735#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005736PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005737"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005738Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005739
Barry Warsaw53699e91996-12-10 23:23:01 +00005740static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005741posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005742{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005743 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005744}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005745#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005746
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005748PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005749"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005750Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005751
Barry Warsaw53699e91996-12-10 23:23:01 +00005752static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005753posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005754{
Victor Stinner8c62be82010-05-06 00:08:46 +00005755 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005756}
5757
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005758#ifdef HAVE_GETGROUPLIST
5759PyDoc_STRVAR(posix_getgrouplist__doc__,
5760"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5761Returns a list of groups to which a user belongs.\n\n\
5762 user: username to lookup\n\
5763 group: base group id of the user");
5764
5765static PyObject *
5766posix_getgrouplist(PyObject *self, PyObject *args)
5767{
5768#ifdef NGROUPS_MAX
5769#define MAX_GROUPS NGROUPS_MAX
5770#else
5771 /* defined to be 16 on Solaris7, so this should be a small number */
5772#define MAX_GROUPS 64
5773#endif
5774
5775 const char *user;
5776 int i, ngroups;
5777 PyObject *list;
5778#ifdef __APPLE__
5779 int *groups, basegid;
5780#else
5781 gid_t *groups, basegid;
5782#endif
5783 ngroups = MAX_GROUPS;
5784
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005785#ifdef __APPLE__
5786 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005787 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005788#else
5789 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
5790 _Py_Gid_Converter, &basegid))
5791 return NULL;
5792#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005793
5794#ifdef __APPLE__
5795 groups = PyMem_Malloc(ngroups * sizeof(int));
5796#else
5797 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5798#endif
5799 if (groups == NULL)
5800 return PyErr_NoMemory();
5801
5802 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5803 PyMem_Del(groups);
5804 return posix_error();
5805 }
5806
5807 list = PyList_New(ngroups);
5808 if (list == NULL) {
5809 PyMem_Del(groups);
5810 return NULL;
5811 }
5812
5813 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005814#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005815 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005816#else
5817 PyObject *o = _PyLong_FromGid(groups[i]);
5818#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005819 if (o == NULL) {
5820 Py_DECREF(list);
5821 PyMem_Del(groups);
5822 return NULL;
5823 }
5824 PyList_SET_ITEM(list, i, o);
5825 }
5826
5827 PyMem_Del(groups);
5828
5829 return list;
5830}
5831#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005832
Fred Drakec9680921999-12-13 16:37:25 +00005833#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005834PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005835"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005836Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005837
5838static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005839posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005840{
5841 PyObject *result = NULL;
5842
Fred Drakec9680921999-12-13 16:37:25 +00005843#ifdef NGROUPS_MAX
5844#define MAX_GROUPS NGROUPS_MAX
5845#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005846 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005847#define MAX_GROUPS 64
5848#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005849 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005850
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005851 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005852 * This is a helper variable to store the intermediate result when
5853 * that happens.
5854 *
5855 * To keep the code readable the OSX behaviour is unconditional,
5856 * according to the POSIX spec this should be safe on all unix-y
5857 * systems.
5858 */
5859 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005860 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005861
Victor Stinner8c62be82010-05-06 00:08:46 +00005862 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005863 if (n < 0) {
5864 if (errno == EINVAL) {
5865 n = getgroups(0, NULL);
5866 if (n == -1) {
5867 return posix_error();
5868 }
5869 if (n == 0) {
5870 /* Avoid malloc(0) */
5871 alt_grouplist = grouplist;
5872 } else {
5873 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5874 if (alt_grouplist == NULL) {
5875 errno = EINVAL;
5876 return posix_error();
5877 }
5878 n = getgroups(n, alt_grouplist);
5879 if (n == -1) {
5880 PyMem_Free(alt_grouplist);
5881 return posix_error();
5882 }
5883 }
5884 } else {
5885 return posix_error();
5886 }
5887 }
5888 result = PyList_New(n);
5889 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005890 int i;
5891 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005892 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005893 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005894 Py_DECREF(result);
5895 result = NULL;
5896 break;
Fred Drakec9680921999-12-13 16:37:25 +00005897 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005898 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005899 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005900 }
5901
5902 if (alt_grouplist != grouplist) {
5903 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005904 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005905
Fred Drakec9680921999-12-13 16:37:25 +00005906 return result;
5907}
5908#endif
5909
Antoine Pitroub7572f02009-12-02 20:46:48 +00005910#ifdef HAVE_INITGROUPS
5911PyDoc_STRVAR(posix_initgroups__doc__,
5912"initgroups(username, gid) -> None\n\n\
5913Call the system initgroups() to initialize the group access list with all of\n\
5914the groups of which the specified username is a member, plus the specified\n\
5915group id.");
5916
5917static PyObject *
5918posix_initgroups(PyObject *self, PyObject *args)
5919{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005920 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005922 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005923#ifdef __APPLE__
5924 int gid;
5925#else
5926 gid_t gid;
5927#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00005928
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005929#ifdef __APPLE__
5930 if (!PyArg_ParseTuple(args, "O&i:initgroups",
5931 PyUnicode_FSConverter, &oname,
5932 &gid))
5933#else
5934 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
5935 PyUnicode_FSConverter, &oname,
5936 _Py_Gid_Converter, &gid))
5937#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005938 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005939 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005940
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005941 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005942 Py_DECREF(oname);
5943 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005944 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005945
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 Py_INCREF(Py_None);
5947 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005948}
5949#endif
5950
Martin v. Löwis606edc12002-06-13 21:09:11 +00005951#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005952PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005953"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005954Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005955
5956static PyObject *
5957posix_getpgid(PyObject *self, PyObject *args)
5958{
Victor Stinner8c62be82010-05-06 00:08:46 +00005959 pid_t pid, pgid;
5960 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5961 return NULL;
5962 pgid = getpgid(pid);
5963 if (pgid < 0)
5964 return posix_error();
5965 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005966}
5967#endif /* HAVE_GETPGID */
5968
5969
Guido van Rossumb6775db1994-08-01 11:34:53 +00005970#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005971PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005972"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005973Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005974
Barry Warsaw53699e91996-12-10 23:23:01 +00005975static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005976posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005977{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005978#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005980#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005981 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005982#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005983}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005984#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005985
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005986
Guido van Rossumb6775db1994-08-01 11:34:53 +00005987#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005988PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005989"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005990Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005991
Barry Warsaw53699e91996-12-10 23:23:01 +00005992static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005993posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005994{
Guido van Rossum64933891994-10-20 21:56:42 +00005995#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005996 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005997#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005998 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005999#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 return posix_error();
6001 Py_INCREF(Py_None);
6002 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006003}
6004
Guido van Rossumb6775db1994-08-01 11:34:53 +00006005#endif /* HAVE_SETPGRP */
6006
Guido van Rossumad0ee831995-03-01 10:34:45 +00006007#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006008
6009#ifdef MS_WINDOWS
6010#include <tlhelp32.h>
6011
6012static PyObject*
6013win32_getppid()
6014{
6015 HANDLE snapshot;
6016 pid_t mypid;
6017 PyObject* result = NULL;
6018 BOOL have_record;
6019 PROCESSENTRY32 pe;
6020
6021 mypid = getpid(); /* This function never fails */
6022
6023 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6024 if (snapshot == INVALID_HANDLE_VALUE)
6025 return PyErr_SetFromWindowsErr(GetLastError());
6026
6027 pe.dwSize = sizeof(pe);
6028 have_record = Process32First(snapshot, &pe);
6029 while (have_record) {
6030 if (mypid == (pid_t)pe.th32ProcessID) {
6031 /* We could cache the ulong value in a static variable. */
6032 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6033 break;
6034 }
6035
6036 have_record = Process32Next(snapshot, &pe);
6037 }
6038
6039 /* If our loop exits and our pid was not found (result will be NULL)
6040 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6041 * error anyway, so let's raise it. */
6042 if (!result)
6043 result = PyErr_SetFromWindowsErr(GetLastError());
6044
6045 CloseHandle(snapshot);
6046
6047 return result;
6048}
6049#endif /*MS_WINDOWS*/
6050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006051PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006052"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006053Return the parent's process id. If the parent process has already exited,\n\
6054Windows machines will still return its id; others systems will return the id\n\
6055of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006056
Barry Warsaw53699e91996-12-10 23:23:01 +00006057static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006058posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006059{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006060#ifdef MS_WINDOWS
6061 return win32_getppid();
6062#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006064#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006065}
6066#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006067
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006068
Fred Drake12c6e2d1999-12-14 21:25:03 +00006069#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006070PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006071"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006072Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00006073
6074static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006075posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006076{
Victor Stinner8c62be82010-05-06 00:08:46 +00006077 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006078#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006079 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006080 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006081
6082 if (GetUserNameW(user_name, &num_chars)) {
6083 /* num_chars is the number of unicode chars plus null terminator */
6084 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006085 }
6086 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006087 result = PyErr_SetFromWindowsErr(GetLastError());
6088#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006089 char *name;
6090 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006091
Victor Stinner8c62be82010-05-06 00:08:46 +00006092 errno = 0;
6093 name = getlogin();
6094 if (name == NULL) {
6095 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006096 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006097 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006098 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 }
6100 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006101 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006102 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006103#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006104 return result;
6105}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006106#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006107
Guido van Rossumad0ee831995-03-01 10:34:45 +00006108#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006109PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006110"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006111Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006112
Barry Warsaw53699e91996-12-10 23:23:01 +00006113static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006114posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00006115{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006116 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006117}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006118#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00006119
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006120
Guido van Rossumad0ee831995-03-01 10:34:45 +00006121#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006122PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006123"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006124Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006125
Barry Warsaw53699e91996-12-10 23:23:01 +00006126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006127posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006128{
Victor Stinner8c62be82010-05-06 00:08:46 +00006129 pid_t pid;
6130 int sig;
6131 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
6132 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006133 if (kill(pid, sig) == -1)
6134 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 Py_INCREF(Py_None);
6136 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00006137}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006138#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006139
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006140#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006141PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006142"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006143Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006144
6145static PyObject *
6146posix_killpg(PyObject *self, PyObject *args)
6147{
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 int sig;
6149 pid_t pgid;
6150 /* XXX some man pages make the `pgid` parameter an int, others
6151 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6152 take the same type. Moreover, pid_t is always at least as wide as
6153 int (else compilation of this module fails), which is safe. */
6154 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
6155 return NULL;
6156 if (killpg(pgid, sig) == -1)
6157 return posix_error();
6158 Py_INCREF(Py_None);
6159 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006160}
6161#endif
6162
Brian Curtineb24d742010-04-12 17:16:38 +00006163#ifdef MS_WINDOWS
6164PyDoc_STRVAR(win32_kill__doc__,
6165"kill(pid, sig)\n\n\
6166Kill a process with a signal.");
6167
6168static PyObject *
6169win32_kill(PyObject *self, PyObject *args)
6170{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006171 PyObject *result;
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006172 pid_t pid;
6173 DWORD sig, err;
Victor Stinner8c62be82010-05-06 00:08:46 +00006174 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006175
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006176 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "k:kill", &pid, &sig))
Victor Stinner8c62be82010-05-06 00:08:46 +00006177 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00006178
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 /* Console processes which share a common console can be sent CTRL+C or
6180 CTRL+BREAK events, provided they handle said events. */
6181 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006182 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006183 err = GetLastError();
6184 PyErr_SetFromWindowsErr(err);
6185 }
6186 else
6187 Py_RETURN_NONE;
6188 }
Brian Curtineb24d742010-04-12 17:16:38 +00006189
Victor Stinner8c62be82010-05-06 00:08:46 +00006190 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6191 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006192 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006193 if (handle == NULL) {
6194 err = GetLastError();
6195 return PyErr_SetFromWindowsErr(err);
6196 }
Brian Curtineb24d742010-04-12 17:16:38 +00006197
Victor Stinner8c62be82010-05-06 00:08:46 +00006198 if (TerminateProcess(handle, sig) == 0) {
6199 err = GetLastError();
6200 result = PyErr_SetFromWindowsErr(err);
6201 } else {
6202 Py_INCREF(Py_None);
6203 result = Py_None;
6204 }
Brian Curtineb24d742010-04-12 17:16:38 +00006205
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 CloseHandle(handle);
6207 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006208}
6209#endif /* MS_WINDOWS */
6210
Guido van Rossumc0125471996-06-28 18:55:32 +00006211#ifdef HAVE_PLOCK
6212
6213#ifdef HAVE_SYS_LOCK_H
6214#include <sys/lock.h>
6215#endif
6216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006217PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006218"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006219Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006220
Barry Warsaw53699e91996-12-10 23:23:01 +00006221static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006222posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00006223{
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 int op;
6225 if (!PyArg_ParseTuple(args, "i:plock", &op))
6226 return NULL;
6227 if (plock(op) == -1)
6228 return posix_error();
6229 Py_INCREF(Py_None);
6230 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00006231}
6232#endif
6233
Guido van Rossumb6775db1994-08-01 11:34:53 +00006234#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006235PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006236"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006237Set the current process's user id.");
6238
Barry Warsaw53699e91996-12-10 23:23:01 +00006239static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006240posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006241{
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 uid_t uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006243 if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 if (setuid(uid) < 0)
6246 return posix_error();
6247 Py_INCREF(Py_None);
6248 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006249}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006250#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006251
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006252
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006253#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006254PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006255"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006256Set the current process's effective user id.");
6257
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006258static PyObject *
6259posix_seteuid (PyObject *self, PyObject *args)
6260{
Victor Stinner8c62be82010-05-06 00:08:46 +00006261 uid_t euid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006262 if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 if (seteuid(euid) < 0) {
6265 return posix_error();
6266 } else {
6267 Py_INCREF(Py_None);
6268 return Py_None;
6269 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006270}
6271#endif /* HAVE_SETEUID */
6272
6273#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006275"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006276Set the current process's effective group id.");
6277
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006278static PyObject *
6279posix_setegid (PyObject *self, PyObject *args)
6280{
Victor Stinner8c62be82010-05-06 00:08:46 +00006281 gid_t egid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006282 if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006284 if (setegid(egid) < 0) {
6285 return posix_error();
6286 } else {
6287 Py_INCREF(Py_None);
6288 return Py_None;
6289 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006290}
6291#endif /* HAVE_SETEGID */
6292
6293#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006294PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006295"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006296Set the current process's real and effective user ids.");
6297
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006298static PyObject *
6299posix_setreuid (PyObject *self, PyObject *args)
6300{
Victor Stinner8c62be82010-05-06 00:08:46 +00006301 uid_t ruid, euid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006302 if (!PyArg_ParseTuple(args, "O&O&:setreuid",
6303 _Py_Uid_Converter, &ruid,
6304 _Py_Uid_Converter, &euid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006306 if (setreuid(ruid, euid) < 0) {
6307 return posix_error();
6308 } else {
6309 Py_INCREF(Py_None);
6310 return Py_None;
6311 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006312}
6313#endif /* HAVE_SETREUID */
6314
6315#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006316PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006317"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006318Set the current process's real and effective group ids.");
6319
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006320static PyObject *
6321posix_setregid (PyObject *self, PyObject *args)
6322{
Victor Stinner8c62be82010-05-06 00:08:46 +00006323 gid_t rgid, egid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006324 if (!PyArg_ParseTuple(args, "O&O&:setregid",
6325 _Py_Gid_Converter, &rgid,
6326 _Py_Gid_Converter, &egid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006327 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 if (setregid(rgid, egid) < 0) {
6329 return posix_error();
6330 } else {
6331 Py_INCREF(Py_None);
6332 return Py_None;
6333 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006334}
6335#endif /* HAVE_SETREGID */
6336
Guido van Rossumb6775db1994-08-01 11:34:53 +00006337#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006338PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006339"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006340Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006341
Barry Warsaw53699e91996-12-10 23:23:01 +00006342static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006343posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006344{
Victor Stinner8c62be82010-05-06 00:08:46 +00006345 gid_t gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006346 if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006347 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006348 if (setgid(gid) < 0)
6349 return posix_error();
6350 Py_INCREF(Py_None);
6351 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006352}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006353#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006354
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006355#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006356PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006357"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006358Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006359
6360static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006361posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006362{
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 int i, len;
6364 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006365
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 if (!PySequence_Check(groups)) {
6367 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6368 return NULL;
6369 }
6370 len = PySequence_Size(groups);
6371 if (len > MAX_GROUPS) {
6372 PyErr_SetString(PyExc_ValueError, "too many groups");
6373 return NULL;
6374 }
6375 for(i = 0; i < len; i++) {
6376 PyObject *elem;
6377 elem = PySequence_GetItem(groups, i);
6378 if (!elem)
6379 return NULL;
6380 if (!PyLong_Check(elem)) {
6381 PyErr_SetString(PyExc_TypeError,
6382 "groups must be integers");
6383 Py_DECREF(elem);
6384 return NULL;
6385 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006386 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006387 Py_DECREF(elem);
6388 return NULL;
6389 }
6390 }
6391 Py_DECREF(elem);
6392 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006393
Victor Stinner8c62be82010-05-06 00:08:46 +00006394 if (setgroups(len, grouplist) < 0)
6395 return posix_error();
6396 Py_INCREF(Py_None);
6397 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006398}
6399#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006400
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006401#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6402static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006403wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006404{
Victor Stinner8c62be82010-05-06 00:08:46 +00006405 PyObject *result;
6406 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006407 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006408
Victor Stinner8c62be82010-05-06 00:08:46 +00006409 if (pid == -1)
6410 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006411
Victor Stinner8c62be82010-05-06 00:08:46 +00006412 if (struct_rusage == NULL) {
6413 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6414 if (m == NULL)
6415 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006416 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006417 Py_DECREF(m);
6418 if (struct_rusage == NULL)
6419 return NULL;
6420 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006421
Victor Stinner8c62be82010-05-06 00:08:46 +00006422 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6423 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6424 if (!result)
6425 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006426
6427#ifndef doubletime
6428#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6429#endif
6430
Victor Stinner8c62be82010-05-06 00:08:46 +00006431 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006432 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006433 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006434 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006435#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006436 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6437 SET_INT(result, 2, ru->ru_maxrss);
6438 SET_INT(result, 3, ru->ru_ixrss);
6439 SET_INT(result, 4, ru->ru_idrss);
6440 SET_INT(result, 5, ru->ru_isrss);
6441 SET_INT(result, 6, ru->ru_minflt);
6442 SET_INT(result, 7, ru->ru_majflt);
6443 SET_INT(result, 8, ru->ru_nswap);
6444 SET_INT(result, 9, ru->ru_inblock);
6445 SET_INT(result, 10, ru->ru_oublock);
6446 SET_INT(result, 11, ru->ru_msgsnd);
6447 SET_INT(result, 12, ru->ru_msgrcv);
6448 SET_INT(result, 13, ru->ru_nsignals);
6449 SET_INT(result, 14, ru->ru_nvcsw);
6450 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006451#undef SET_INT
6452
Victor Stinner8c62be82010-05-06 00:08:46 +00006453 if (PyErr_Occurred()) {
6454 Py_DECREF(result);
6455 return NULL;
6456 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006457
Victor Stinner8c62be82010-05-06 00:08:46 +00006458 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006459}
6460#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6461
6462#ifdef HAVE_WAIT3
6463PyDoc_STRVAR(posix_wait3__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006464"wait3(options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006465Wait for completion of a child process.");
6466
6467static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006468posix_wait3(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006469{
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 pid_t pid;
6471 int options;
6472 struct rusage ru;
6473 WAIT_TYPE status;
6474 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006475
Victor Stinner4195b5c2012-02-08 23:03:19 +01006476 if (!PyArg_ParseTuple(args, "i:wait3", &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006477 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006478
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 Py_BEGIN_ALLOW_THREADS
6480 pid = wait3(&status, options, &ru);
6481 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006482
Victor Stinner4195b5c2012-02-08 23:03:19 +01006483 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006484}
6485#endif /* HAVE_WAIT3 */
6486
6487#ifdef HAVE_WAIT4
6488PyDoc_STRVAR(posix_wait4__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006489"wait4(pid, options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006490Wait for completion of a given child process.");
6491
6492static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006493posix_wait4(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006494{
Victor Stinner8c62be82010-05-06 00:08:46 +00006495 pid_t pid;
6496 int options;
6497 struct rusage ru;
6498 WAIT_TYPE status;
6499 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006500
Victor Stinner4195b5c2012-02-08 23:03:19 +01006501 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006502 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006503
Victor Stinner8c62be82010-05-06 00:08:46 +00006504 Py_BEGIN_ALLOW_THREADS
6505 pid = wait4(pid, &status, options, &ru);
6506 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006507
Victor Stinner4195b5c2012-02-08 23:03:19 +01006508 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006509}
6510#endif /* HAVE_WAIT4 */
6511
Ross Lagerwall7807c352011-03-17 20:20:30 +02006512#if defined(HAVE_WAITID) && !defined(__APPLE__)
6513PyDoc_STRVAR(posix_waitid__doc__,
6514"waitid(idtype, id, options) -> waitid_result\n\n\
6515Wait for the completion of one or more child processes.\n\n\
6516idtype can be P_PID, P_PGID or P_ALL.\n\
6517id specifies the pid to wait on.\n\
6518options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6519or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6520Returns either waitid_result or None if WNOHANG is specified and there are\n\
6521no children in a waitable state.");
6522
6523static PyObject *
6524posix_waitid(PyObject *self, PyObject *args)
6525{
6526 PyObject *result;
6527 idtype_t idtype;
6528 id_t id;
6529 int options, res;
6530 siginfo_t si;
6531 si.si_pid = 0;
6532 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6533 return NULL;
6534 Py_BEGIN_ALLOW_THREADS
6535 res = waitid(idtype, id, &si, options);
6536 Py_END_ALLOW_THREADS
6537 if (res == -1)
6538 return posix_error();
6539
6540 if (si.si_pid == 0)
6541 Py_RETURN_NONE;
6542
6543 result = PyStructSequence_New(&WaitidResultType);
6544 if (!result)
6545 return NULL;
6546
6547 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006548 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02006549 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6550 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6551 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6552 if (PyErr_Occurred()) {
6553 Py_DECREF(result);
6554 return NULL;
6555 }
6556
6557 return result;
6558}
6559#endif
6560
Guido van Rossumb6775db1994-08-01 11:34:53 +00006561#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006562PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006563"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006564Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006565
Barry Warsaw53699e91996-12-10 23:23:01 +00006566static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006567posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006568{
Victor Stinner8c62be82010-05-06 00:08:46 +00006569 pid_t pid;
6570 int options;
6571 WAIT_TYPE status;
6572 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006573
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6575 return NULL;
6576 Py_BEGIN_ALLOW_THREADS
6577 pid = waitpid(pid, &status, options);
6578 Py_END_ALLOW_THREADS
6579 if (pid == -1)
6580 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006581
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006583}
6584
Tim Petersab034fa2002-02-01 11:27:43 +00006585#elif defined(HAVE_CWAIT)
6586
6587/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006588PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006589"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006590"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006591
6592static PyObject *
6593posix_waitpid(PyObject *self, PyObject *args)
6594{
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 Py_intptr_t pid;
6596 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006597
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006598 if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:waitpid", &pid, &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 return NULL;
6600 Py_BEGIN_ALLOW_THREADS
6601 pid = _cwait(&status, pid, options);
6602 Py_END_ALLOW_THREADS
6603 if (pid == -1)
6604 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006605
Victor Stinner8c62be82010-05-06 00:08:46 +00006606 /* shift the status left a byte so this is more like the POSIX waitpid */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006607 return Py_BuildValue(_Py_PARSE_INTPTR "i", pid, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006608}
6609#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006610
Guido van Rossumad0ee831995-03-01 10:34:45 +00006611#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006612PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006613"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006614Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006615
Barry Warsaw53699e91996-12-10 23:23:01 +00006616static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006617posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006618{
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 pid_t pid;
6620 WAIT_TYPE status;
6621 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006622
Victor Stinner8c62be82010-05-06 00:08:46 +00006623 Py_BEGIN_ALLOW_THREADS
6624 pid = wait(&status);
6625 Py_END_ALLOW_THREADS
6626 if (pid == -1)
6627 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006628
Victor Stinner8c62be82010-05-06 00:08:46 +00006629 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006630}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006631#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006633
Larry Hastings9cf065c2012-06-22 16:30:09 -07006634#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
6635PyDoc_STRVAR(readlink__doc__,
6636"readlink(path, *, dir_fd=None) -> path\n\n\
6637Return a string representing the path to which the symbolic link points.\n\
6638\n\
6639If dir_fd is not None, it should be a file descriptor open to a directory,\n\
6640 and path should be relative; path will then be relative to that directory.\n\
6641dir_fd may not be implemented on your platform.\n\
6642 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006643#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006644
Guido van Rossumb6775db1994-08-01 11:34:53 +00006645#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006646
Barry Warsaw53699e91996-12-10 23:23:01 +00006647static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006648posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006649{
Larry Hastings9cf065c2012-06-22 16:30:09 -07006650 path_t path;
6651 int dir_fd = DEFAULT_DIR_FD;
6652 char buffer[MAXPATHLEN];
6653 ssize_t length;
6654 PyObject *return_value = NULL;
6655 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00006656
Larry Hastings9cf065c2012-06-22 16:30:09 -07006657 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01006658 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006659 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
6660 path_converter, &path,
6661#ifdef HAVE_READLINKAT
6662 dir_fd_converter, &dir_fd
6663#else
6664 dir_fd_unavailable, &dir_fd
6665#endif
6666 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006668
Victor Stinner8c62be82010-05-06 00:08:46 +00006669 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07006670#ifdef HAVE_READLINKAT
6671 if (dir_fd != DEFAULT_DIR_FD)
6672 length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer));
Victor Stinnera45598a2010-05-14 16:35:39 +00006673 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07006674#endif
6675 length = readlink(path.narrow, buffer, sizeof(buffer));
6676 Py_END_ALLOW_THREADS
6677
6678 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01006679 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006680 goto exit;
6681 }
6682
6683 if (PyUnicode_Check(path.object))
6684 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
6685 else
6686 return_value = PyBytes_FromStringAndSize(buffer, length);
6687exit:
6688 path_cleanup(&path);
6689 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006690}
Larry Hastings9cf065c2012-06-22 16:30:09 -07006691
6692
Guido van Rossumb6775db1994-08-01 11:34:53 +00006693#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006694
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006695
Larry Hastings9cf065c2012-06-22 16:30:09 -07006696#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006697PyDoc_STRVAR(posix_symlink__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07006698"symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
6699Create a symbolic link pointing to src named dst.\n\n\
6700target_is_directory is required on Windows if the target is to be\n\
6701 interpreted as a directory. (On Windows, symlink requires\n\
6702 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\
6703 target_is_directory is ignored on non-Windows platforms.\n\
6704\n\
6705If dir_fd is not None, it should be a file descriptor open to a directory,\n\
6706 and path should be relative; path will then be relative to that directory.\n\
6707dir_fd may not be implemented on your platform.\n\
6708 If it is unavailable, using it will raise a NotImplementedError.");
6709
6710#if defined(MS_WINDOWS)
6711
6712/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6713static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL;
6714static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02006715
Larry Hastings9cf065c2012-06-22 16:30:09 -07006716static int
Victor Stinner31b3b922013-06-05 01:49:17 +02006717check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07006718{
6719 HINSTANCE hKernel32;
6720 /* only recheck */
6721 if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA)
6722 return 1;
6723 hKernel32 = GetModuleHandleW(L"KERNEL32");
6724 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6725 "CreateSymbolicLinkW");
6726 *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32,
6727 "CreateSymbolicLinkA");
6728 return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA);
6729}
6730
Victor Stinner31b3b922013-06-05 01:49:17 +02006731/* Remove the last portion of the path */
6732static void
6733_dirnameW(WCHAR *path)
6734{
Jason R. Coombs3a092862013-05-27 23:21:28 -04006735 WCHAR *ptr;
6736
6737 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02006738 for(ptr = path + wcslen(path); ptr != path; ptr--) {
Victor Stinner072318b2013-06-05 02:07:46 +02006739 if (*ptr == L'\\' || *ptr == L'/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04006740 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04006741 }
6742 *ptr = 0;
6743}
6744
Victor Stinner31b3b922013-06-05 01:49:17 +02006745/* Remove the last portion of the path */
6746static void
6747_dirnameA(char *path)
6748{
Jason R. Coombs3a092862013-05-27 23:21:28 -04006749 char *ptr;
6750
6751 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02006752 for(ptr = path + strlen(path); ptr != path; ptr--) {
6753 if (*ptr == '\\' || *ptr == '/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04006754 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04006755 }
6756 *ptr = 0;
6757}
6758
Victor Stinner31b3b922013-06-05 01:49:17 +02006759/* Is this path absolute? */
6760static int
6761_is_absW(const WCHAR *path)
6762{
Jason R. Coombs3a092862013-05-27 23:21:28 -04006763 return path[0] == L'\\' || path[0] == L'/' || path[1] == L':';
6764
6765}
6766
Victor Stinner31b3b922013-06-05 01:49:17 +02006767/* Is this path absolute? */
6768static int
6769_is_absA(const char *path)
6770{
Jason R. Coombs3a092862013-05-27 23:21:28 -04006771 return path[0] == '\\' || path[0] == '/' || path[1] == ':';
6772
6773}
6774
Victor Stinner31b3b922013-06-05 01:49:17 +02006775/* join root and rest with a backslash */
6776static void
6777_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
6778{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02006779 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04006780
Victor Stinner31b3b922013-06-05 01:49:17 +02006781 if (_is_absW(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04006782 wcscpy(dest_path, rest);
6783 return;
6784 }
6785
6786 root_len = wcslen(root);
6787
6788 wcscpy(dest_path, root);
6789 if(root_len) {
Victor Stinner31b3b922013-06-05 01:49:17 +02006790 dest_path[root_len] = L'\\';
6791 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04006792 }
6793 wcscpy(dest_path+root_len, rest);
6794}
6795
Victor Stinner31b3b922013-06-05 01:49:17 +02006796/* join root and rest with a backslash */
6797static void
6798_joinA(char *dest_path, const char *root, const char *rest)
6799{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02006800 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04006801
Victor Stinner31b3b922013-06-05 01:49:17 +02006802 if (_is_absA(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04006803 strcpy(dest_path, rest);
6804 return;
6805 }
6806
6807 root_len = strlen(root);
6808
6809 strcpy(dest_path, root);
6810 if(root_len) {
6811 dest_path[root_len] = '\\';
Victor Stinner31b3b922013-06-05 01:49:17 +02006812 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04006813 }
6814 strcpy(dest_path+root_len, rest);
6815}
6816
Victor Stinner31b3b922013-06-05 01:49:17 +02006817/* Return True if the path at src relative to dest is a directory */
6818static int
6819_check_dirW(WCHAR *src, WCHAR *dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04006820{
Jason R. Coombs3a092862013-05-27 23:21:28 -04006821 WIN32_FILE_ATTRIBUTE_DATA src_info;
6822 WCHAR dest_parent[MAX_PATH];
6823 WCHAR src_resolved[MAX_PATH] = L"";
6824
6825 /* dest_parent = os.path.dirname(dest) */
6826 wcscpy(dest_parent, dest);
6827 _dirnameW(dest_parent);
6828 /* src_resolved = os.path.join(dest_parent, src) */
6829 _joinW(src_resolved, dest_parent, src);
6830 return (
6831 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
6832 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
6833 );
6834}
6835
Victor Stinner31b3b922013-06-05 01:49:17 +02006836/* Return True if the path at src relative to dest is a directory */
6837static int
6838_check_dirA(char *src, char *dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04006839{
Jason R. Coombs3a092862013-05-27 23:21:28 -04006840 WIN32_FILE_ATTRIBUTE_DATA src_info;
6841 char dest_parent[MAX_PATH];
6842 char src_resolved[MAX_PATH] = "";
6843
6844 /* dest_parent = os.path.dirname(dest) */
6845 strcpy(dest_parent, dest);
Victor Stinner5a436762013-06-05 00:37:12 +02006846 _dirnameA(dest_parent);
Jason R. Coombs3a092862013-05-27 23:21:28 -04006847 /* src_resolved = os.path.join(dest_parent, src) */
Victor Stinner5a436762013-06-05 00:37:12 +02006848 _joinA(src_resolved, dest_parent, src);
Jason R. Coombs3a092862013-05-27 23:21:28 -04006849 return (
6850 GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info)
6851 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
6852 );
6853}
6854
Larry Hastings9cf065c2012-06-22 16:30:09 -07006855#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006856
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006857static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006858posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006859{
Larry Hastings9cf065c2012-06-22 16:30:09 -07006860 path_t src;
6861 path_t dst;
6862 int dir_fd = DEFAULT_DIR_FD;
6863 int target_is_directory = 0;
6864 static char *keywords[] = {"src", "dst", "target_is_directory",
6865 "dir_fd", NULL};
6866 PyObject *return_value;
6867#ifdef MS_WINDOWS
6868 DWORD result;
6869#else
6870 int result;
6871#endif
6872
6873 memset(&src, 0, sizeof(src));
Victor Stinner292c8352012-10-30 02:17:38 +01006874 src.function_name = "symlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006875 src.argument_name = "src";
6876 memset(&dst, 0, sizeof(dst));
Victor Stinner292c8352012-10-30 02:17:38 +01006877 dst.function_name = "symlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006878 dst.argument_name = "dst";
6879
6880#ifdef MS_WINDOWS
6881 if (!check_CreateSymbolicLink()) {
6882 PyErr_SetString(PyExc_NotImplementedError,
6883 "CreateSymbolicLink functions not found");
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006884 return NULL;
6885 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006886 if (!win32_can_symlink) {
6887 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006888 return NULL;
6889 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006890#endif
6891
6892 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink",
6893 keywords,
6894 path_converter, &src,
6895 path_converter, &dst,
6896 &target_is_directory,
6897#ifdef HAVE_SYMLINKAT
6898 dir_fd_converter, &dir_fd
6899#else
6900 dir_fd_unavailable, &dir_fd
6901#endif
6902 ))
6903 return NULL;
6904
6905 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
6906 PyErr_SetString(PyExc_ValueError,
6907 "symlink: src and dst must be the same type");
6908 return_value = NULL;
6909 goto exit;
6910 }
6911
6912#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04006913
Larry Hastings9cf065c2012-06-22 16:30:09 -07006914 Py_BEGIN_ALLOW_THREADS
Jason R. Coombs3a092862013-05-27 23:21:28 -04006915 if (dst.wide) {
6916 /* if src is a directory, ensure target_is_directory==1 */
6917 target_is_directory |= _check_dirW(src.wide, dst.wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006918 result = Py_CreateSymbolicLinkW(dst.wide, src.wide,
6919 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04006920 }
6921 else {
6922 /* if src is a directory, ensure target_is_directory==1 */
6923 target_is_directory |= _check_dirA(src.narrow, dst.narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006924 result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow,
6925 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04006926 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006927 Py_END_ALLOW_THREADS
6928
6929 if (!result) {
Victor Stinnerb024e842012-10-31 22:24:06 +01006930 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006931 goto exit;
6932 }
6933
6934#else
6935
6936 Py_BEGIN_ALLOW_THREADS
6937#if HAVE_SYMLINKAT
6938 if (dir_fd != DEFAULT_DIR_FD)
6939 result = symlinkat(src.narrow, dir_fd, dst.narrow);
6940 else
6941#endif
6942 result = symlink(src.narrow, dst.narrow);
6943 Py_END_ALLOW_THREADS
6944
6945 if (result) {
Victor Stinnerafe17062012-10-31 22:47:43 +01006946 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006947 goto exit;
6948 }
6949#endif
6950
6951 return_value = Py_None;
6952 Py_INCREF(Py_None);
6953 goto exit; /* silence "unused label" warning */
6954exit:
6955 path_cleanup(&src);
6956 path_cleanup(&dst);
6957 return return_value;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006958}
Larry Hastings9cf065c2012-06-22 16:30:09 -07006959
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006960#endif /* HAVE_SYMLINK */
6961
Larry Hastings9cf065c2012-06-22 16:30:09 -07006962
Brian Curtind40e6f72010-07-08 21:39:08 +00006963#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6964
Brian Curtind40e6f72010-07-08 21:39:08 +00006965static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006966win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtind40e6f72010-07-08 21:39:08 +00006967{
6968 wchar_t *path;
6969 DWORD n_bytes_returned;
6970 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006971 PyObject *po, *result;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006972 int dir_fd;
Brian Curtind40e6f72010-07-08 21:39:08 +00006973 HANDLE reparse_point_handle;
6974
6975 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6976 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6977 wchar_t *print_name;
6978
Larry Hastings9cf065c2012-06-22 16:30:09 -07006979 static char *keywords[] = {"path", "dir_fd", NULL};
6980
6981 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
6982 &po,
6983 dir_fd_unavailable, &dir_fd
6984 ))
Victor Stinnereb5657a2011-09-30 01:44:27 +02006985 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07006986
Victor Stinnereb5657a2011-09-30 01:44:27 +02006987 path = PyUnicode_AsUnicode(po);
6988 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006989 return NULL;
6990
6991 /* First get a handle to the reparse point */
6992 Py_BEGIN_ALLOW_THREADS
6993 reparse_point_handle = CreateFileW(
6994 path,
6995 0,
6996 0,
6997 0,
6998 OPEN_EXISTING,
6999 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7000 0);
7001 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007002
Brian Curtind40e6f72010-07-08 21:39:08 +00007003 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02007004 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007005
Brian Curtind40e6f72010-07-08 21:39:08 +00007006 Py_BEGIN_ALLOW_THREADS
7007 /* New call DeviceIoControl to read the reparse point */
7008 io_result = DeviceIoControl(
7009 reparse_point_handle,
7010 FSCTL_GET_REPARSE_POINT,
7011 0, 0, /* in buffer */
7012 target_buffer, sizeof(target_buffer),
7013 &n_bytes_returned,
7014 0 /* we're not using OVERLAPPED_IO */
7015 );
7016 CloseHandle(reparse_point_handle);
7017 Py_END_ALLOW_THREADS
7018
7019 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02007020 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00007021
7022 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7023 {
7024 PyErr_SetString(PyExc_ValueError,
7025 "not a symbolic link");
7026 return NULL;
7027 }
Brian Curtin74e45612010-07-09 15:58:59 +00007028 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
7029 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
7030
7031 result = PyUnicode_FromWideChar(print_name,
7032 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00007033 return result;
7034}
7035
7036#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7037
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007038
Larry Hastings605a62d2012-06-24 04:33:36 -07007039static PyStructSequence_Field times_result_fields[] = {
7040 {"user", "user time"},
7041 {"system", "system time"},
7042 {"children_user", "user time of children"},
7043 {"children_system", "system time of children"},
7044 {"elapsed", "elapsed time since an arbitrary point in the past"},
7045 {NULL}
7046};
7047
7048PyDoc_STRVAR(times_result__doc__,
7049"times_result: Result from os.times().\n\n\
7050This object may be accessed either as a tuple of\n\
7051 (user, system, children_user, children_system, elapsed),\n\
7052or via the attributes user, system, children_user, children_system,\n\
7053and elapsed.\n\
7054\n\
7055See os.times for more information.");
7056
7057static PyStructSequence_Desc times_result_desc = {
7058 "times_result", /* name */
7059 times_result__doc__, /* doc */
7060 times_result_fields,
7061 5
7062};
7063
7064static PyTypeObject TimesResultType;
7065
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007066#ifdef MS_WINDOWS
7067#define HAVE_TIMES /* mandatory, for the method table */
7068#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007069
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007070#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007071
7072static PyObject *
7073build_times_result(double user, double system,
7074 double children_user, double children_system,
7075 double elapsed)
7076{
7077 PyObject *value = PyStructSequence_New(&TimesResultType);
7078 if (value == NULL)
7079 return NULL;
7080
7081#define SET(i, field) \
7082 { \
7083 PyObject *o = PyFloat_FromDouble(field); \
7084 if (!o) { \
7085 Py_DECREF(value); \
7086 return NULL; \
7087 } \
7088 PyStructSequence_SET_ITEM(value, i, o); \
7089 } \
7090
7091 SET(0, user);
7092 SET(1, system);
7093 SET(2, children_user);
7094 SET(3, children_system);
7095 SET(4, elapsed);
7096
7097#undef SET
7098
7099 return value;
7100}
7101
7102PyDoc_STRVAR(posix_times__doc__,
7103"times() -> times_result\n\n\
7104Return an object containing floating point numbers indicating process\n\
7105times. The object behaves like a named tuple with these fields:\n\
7106 (utime, stime, cutime, cstime, elapsed_time)");
7107
Jesus Ceaab70e2a2012-10-05 01:48:08 +02007108#if defined(MS_WINDOWS)
Barry Warsaw53699e91996-12-10 23:23:01 +00007109static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007110posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007111{
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 FILETIME create, exit, kernel, user;
7113 HANDLE hProc;
7114 hProc = GetCurrentProcess();
7115 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7116 /* The fields of a FILETIME structure are the hi and lo part
7117 of a 64-bit value expressed in 100 nanosecond units.
7118 1e7 is one second in such units; 1e-7 the inverse.
7119 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7120 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007121 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 (double)(user.dwHighDateTime*429.4967296 +
7123 user.dwLowDateTime*1e-7),
7124 (double)(kernel.dwHighDateTime*429.4967296 +
7125 kernel.dwLowDateTime*1e-7),
7126 (double)0,
7127 (double)0,
7128 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007129}
Jesus Ceaab70e2a2012-10-05 01:48:08 +02007130#else /* Not Windows */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007131#define NEED_TICKS_PER_SECOND
7132static long ticks_per_second = -1;
7133static PyObject *
7134posix_times(PyObject *self, PyObject *noargs)
7135{
7136 struct tms t;
7137 clock_t c;
7138 errno = 0;
7139 c = times(&t);
7140 if (c == (clock_t) -1)
7141 return posix_error();
7142 return build_times_result(
7143 (double)t.tms_utime / ticks_per_second,
7144 (double)t.tms_stime / ticks_per_second,
7145 (double)t.tms_cutime / ticks_per_second,
7146 (double)t.tms_cstime / ticks_per_second,
7147 (double)c / ticks_per_second);
7148}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007149#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007150
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007151#endif /* HAVE_TIMES */
7152
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007153
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007154#ifdef HAVE_GETSID
7155PyDoc_STRVAR(posix_getsid__doc__,
7156"getsid(pid) -> sid\n\n\
7157Call the system call getsid().");
7158
7159static PyObject *
7160posix_getsid(PyObject *self, PyObject *args)
7161{
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 pid_t pid;
7163 int sid;
7164 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
7165 return NULL;
7166 sid = getsid(pid);
7167 if (sid < 0)
7168 return posix_error();
7169 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007170}
7171#endif /* HAVE_GETSID */
7172
7173
Guido van Rossumb6775db1994-08-01 11:34:53 +00007174#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007175PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007176"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007177Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007178
Barry Warsaw53699e91996-12-10 23:23:01 +00007179static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007180posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007181{
Victor Stinner8c62be82010-05-06 00:08:46 +00007182 if (setsid() < 0)
7183 return posix_error();
7184 Py_INCREF(Py_None);
7185 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007186}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007187#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007188
Guido van Rossumb6775db1994-08-01 11:34:53 +00007189#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007190PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007191"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007192Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007193
Barry Warsaw53699e91996-12-10 23:23:01 +00007194static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007195posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007196{
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 pid_t pid;
7198 int pgrp;
7199 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
7200 return NULL;
7201 if (setpgid(pid, pgrp) < 0)
7202 return posix_error();
7203 Py_INCREF(Py_None);
7204 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007205}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007206#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007208
Guido van Rossumb6775db1994-08-01 11:34:53 +00007209#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007210PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007211"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007212Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007213
Barry Warsaw53699e91996-12-10 23:23:01 +00007214static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007215posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007216{
Victor Stinner8c62be82010-05-06 00:08:46 +00007217 int fd;
7218 pid_t pgid;
7219 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
7220 return NULL;
7221 pgid = tcgetpgrp(fd);
7222 if (pgid < 0)
7223 return posix_error();
7224 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007225}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007226#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007227
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007228
Guido van Rossumb6775db1994-08-01 11:34:53 +00007229#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007230PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007231"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007232Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007233
Barry Warsaw53699e91996-12-10 23:23:01 +00007234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007235posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007236{
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 int fd;
7238 pid_t pgid;
7239 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
7240 return NULL;
7241 if (tcsetpgrp(fd, pgid) < 0)
7242 return posix_error();
7243 Py_INCREF(Py_None);
7244 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007245}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007246#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007247
Guido van Rossum687dd131993-05-17 08:34:16 +00007248/* Functions acting on file descriptors */
7249
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007250PyDoc_STRVAR(posix_open__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007251"open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7252Open a file for low level IO. Returns a file handle (integer).\n\
7253\n\
7254If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7255 and path should be relative; path will then be relative to that directory.\n\
7256dir_fd may not be implemented on your platform.\n\
7257 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007258
Barry Warsaw53699e91996-12-10 23:23:01 +00007259static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007260posix_open(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007261{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007262 path_t path;
7263 int flags;
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 int mode = 0777;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007265 int dir_fd = DEFAULT_DIR_FD;
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 int fd;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007267 PyObject *return_value = NULL;
7268 static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007269
Larry Hastings9cf065c2012-06-22 16:30:09 -07007270 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007271 path.function_name = "open";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007272 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords,
7273 path_converter, &path,
7274 &flags, &mode,
7275#ifdef HAVE_OPENAT
7276 dir_fd_converter, &dir_fd
7277#else
7278 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007279#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07007280 ))
7281 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007282
Victor Stinner8c62be82010-05-06 00:08:46 +00007283 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007284#ifdef MS_WINDOWS
7285 if (path.wide)
7286 fd = _wopen(path.wide, flags, mode);
7287 else
7288#endif
7289#ifdef HAVE_OPENAT
7290 if (dir_fd != DEFAULT_DIR_FD)
7291 fd = openat(dir_fd, path.narrow, flags, mode);
7292 else
7293#endif
7294 fd = open(path.narrow, flags, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00007295 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00007296
Larry Hastings9cf065c2012-06-22 16:30:09 -07007297 if (fd == -1) {
Victor Stinner4e7d2d42012-11-05 01:20:58 +01007298 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007299 goto exit;
7300 }
7301
7302 return_value = PyLong_FromLong((long)fd);
7303
7304exit:
7305 path_cleanup(&path);
7306 return return_value;
7307}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007309PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007310"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007311Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007312
Barry Warsaw53699e91996-12-10 23:23:01 +00007313static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007314posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007315{
Victor Stinner8c62be82010-05-06 00:08:46 +00007316 int fd, res;
7317 if (!PyArg_ParseTuple(args, "i:close", &fd))
7318 return NULL;
7319 if (!_PyVerify_fd(fd))
7320 return posix_error();
7321 Py_BEGIN_ALLOW_THREADS
7322 res = close(fd);
7323 Py_END_ALLOW_THREADS
7324 if (res < 0)
7325 return posix_error();
7326 Py_INCREF(Py_None);
7327 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007328}
7329
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007330
Victor Stinner8c62be82010-05-06 00:08:46 +00007331PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00007332"closerange(fd_low, fd_high)\n\n\
7333Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
7334
7335static PyObject *
7336posix_closerange(PyObject *self, PyObject *args)
7337{
Victor Stinner8c62be82010-05-06 00:08:46 +00007338 int fd_from, fd_to, i;
7339 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
7340 return NULL;
7341 Py_BEGIN_ALLOW_THREADS
7342 for (i = fd_from; i < fd_to; i++)
7343 if (_PyVerify_fd(i))
7344 close(i);
7345 Py_END_ALLOW_THREADS
7346 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007347}
7348
7349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007350PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007351"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007352Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007353
Barry Warsaw53699e91996-12-10 23:23:01 +00007354static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007355posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007356{
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 int fd;
7358 if (!PyArg_ParseTuple(args, "i:dup", &fd))
7359 return NULL;
7360 if (!_PyVerify_fd(fd))
7361 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007363 if (fd < 0)
7364 return posix_error();
7365 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007366}
7367
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007369PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00007370"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007371Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007372
Barry Warsaw53699e91996-12-10 23:23:01 +00007373static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007374posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007375{
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 int fd, fd2, res;
7377 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
7378 return NULL;
7379 if (!_PyVerify_fd_dup2(fd, fd2))
7380 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 if (res < 0)
7383 return posix_error();
7384 Py_INCREF(Py_None);
7385 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007386}
7387
Ross Lagerwall7807c352011-03-17 20:20:30 +02007388#ifdef HAVE_LOCKF
7389PyDoc_STRVAR(posix_lockf__doc__,
7390"lockf(fd, cmd, len)\n\n\
7391Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
7392fd is an open file descriptor.\n\
7393cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
7394F_TEST.\n\
7395len specifies the section of the file to lock.");
7396
7397static PyObject *
7398posix_lockf(PyObject *self, PyObject *args)
7399{
7400 int fd, cmd, res;
7401 off_t len;
7402 if (!PyArg_ParseTuple(args, "iiO&:lockf",
7403 &fd, &cmd, _parse_off_t, &len))
7404 return NULL;
7405
7406 Py_BEGIN_ALLOW_THREADS
7407 res = lockf(fd, cmd, len);
7408 Py_END_ALLOW_THREADS
7409
7410 if (res < 0)
7411 return posix_error();
7412
7413 Py_RETURN_NONE;
7414}
7415#endif
7416
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007418PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007419"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01007420Set the current position of a file descriptor.\n\
7421Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007422
Barry Warsaw53699e91996-12-10 23:23:01 +00007423static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007424posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007425{
Victor Stinner8c62be82010-05-06 00:08:46 +00007426 int fd, how;
Victor Stinner14b9b112013-06-25 00:37:25 +02007427#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007428 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007429#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007430 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007431#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02007432 PyObject *posobj;
7433 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00007434 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00007435#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007436 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7437 switch (how) {
7438 case 0: how = SEEK_SET; break;
7439 case 1: how = SEEK_CUR; break;
7440 case 2: how = SEEK_END; break;
7441 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007442#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007443
Ross Lagerwall8e749672011-03-17 21:54:07 +02007444#if !defined(HAVE_LARGEFILE_SUPPORT)
7445 pos = PyLong_AsLong(posobj);
7446#else
7447 pos = PyLong_AsLongLong(posobj);
7448#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007449 if (PyErr_Occurred())
7450 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007451
Victor Stinner8c62be82010-05-06 00:08:46 +00007452 if (!_PyVerify_fd(fd))
7453 return posix_error();
7454 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +02007455#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007457#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007458 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007459#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007460 Py_END_ALLOW_THREADS
7461 if (res < 0)
7462 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00007463
7464#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007465 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007466#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007467 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007468#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007469}
7470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007472PyDoc_STRVAR(posix_read__doc__,
Benjamin Peterson3b08a292013-05-24 14:35:57 -07007473"read(fd, buffersize) -> bytes\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007474Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007475
Barry Warsaw53699e91996-12-10 23:23:01 +00007476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007477posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007478{
Victor Stinner8c62be82010-05-06 00:08:46 +00007479 int fd, size;
7480 Py_ssize_t n;
7481 PyObject *buffer;
7482 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7483 return NULL;
7484 if (size < 0) {
7485 errno = EINVAL;
7486 return posix_error();
7487 }
7488 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7489 if (buffer == NULL)
7490 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007491 if (!_PyVerify_fd(fd)) {
7492 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007493 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007494 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007495 Py_BEGIN_ALLOW_THREADS
7496 n = read(fd, PyBytes_AS_STRING(buffer), size);
7497 Py_END_ALLOW_THREADS
7498 if (n < 0) {
7499 Py_DECREF(buffer);
7500 return posix_error();
7501 }
7502 if (n != size)
7503 _PyBytes_Resize(&buffer, n);
7504 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007505}
7506
Ross Lagerwall7807c352011-03-17 20:20:30 +02007507#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7508 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007509static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007510iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7511{
7512 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007513 Py_ssize_t blen, total = 0;
7514
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007515 *iov = PyMem_New(struct iovec, cnt);
7516 if (*iov == NULL) {
7517 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007518 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007519 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007520
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007521 *buf = PyMem_New(Py_buffer, cnt);
7522 if (*buf == NULL) {
7523 PyMem_Del(*iov);
7524 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007525 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007526 }
7527
7528 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007529 PyObject *item = PySequence_GetItem(seq, i);
7530 if (item == NULL)
7531 goto fail;
7532 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7533 Py_DECREF(item);
7534 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007535 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007536 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007537 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007538 blen = (*buf)[i].len;
7539 (*iov)[i].iov_len = blen;
7540 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007541 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007542 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007543
7544fail:
7545 PyMem_Del(*iov);
7546 for (j = 0; j < i; j++) {
7547 PyBuffer_Release(&(*buf)[j]);
7548 }
7549 PyMem_Del(*buf);
7550 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007551}
7552
7553static void
7554iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7555{
7556 int i;
7557 PyMem_Del(iov);
7558 for (i = 0; i < cnt; i++) {
7559 PyBuffer_Release(&buf[i]);
7560 }
7561 PyMem_Del(buf);
7562}
7563#endif
7564
Ross Lagerwall7807c352011-03-17 20:20:30 +02007565#ifdef HAVE_READV
7566PyDoc_STRVAR(posix_readv__doc__,
7567"readv(fd, buffers) -> bytesread\n\n\
7568Read from a file descriptor into a number of writable buffers. buffers\n\
7569is an arbitrary sequence of writable buffers.\n\
7570Returns the total number of bytes read.");
7571
7572static PyObject *
7573posix_readv(PyObject *self, PyObject *args)
7574{
7575 int fd, cnt;
7576 Py_ssize_t n;
7577 PyObject *seq;
7578 struct iovec *iov;
7579 Py_buffer *buf;
7580
7581 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7582 return NULL;
7583 if (!PySequence_Check(seq)) {
7584 PyErr_SetString(PyExc_TypeError,
7585 "readv() arg 2 must be a sequence");
7586 return NULL;
7587 }
7588 cnt = PySequence_Size(seq);
7589
7590 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7591 return NULL;
7592
7593 Py_BEGIN_ALLOW_THREADS
7594 n = readv(fd, iov, cnt);
7595 Py_END_ALLOW_THREADS
7596
7597 iov_cleanup(iov, buf, cnt);
7598 return PyLong_FromSsize_t(n);
7599}
7600#endif
7601
7602#ifdef HAVE_PREAD
7603PyDoc_STRVAR(posix_pread__doc__,
7604"pread(fd, buffersize, offset) -> string\n\n\
7605Read from a file descriptor, fd, at a position of offset. It will read up\n\
7606to buffersize number of bytes. The file offset remains unchanged.");
7607
7608static PyObject *
7609posix_pread(PyObject *self, PyObject *args)
7610{
7611 int fd, size;
7612 off_t offset;
7613 Py_ssize_t n;
7614 PyObject *buffer;
7615 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7616 return NULL;
7617
7618 if (size < 0) {
7619 errno = EINVAL;
7620 return posix_error();
7621 }
7622 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7623 if (buffer == NULL)
7624 return NULL;
7625 if (!_PyVerify_fd(fd)) {
7626 Py_DECREF(buffer);
7627 return posix_error();
7628 }
7629 Py_BEGIN_ALLOW_THREADS
7630 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7631 Py_END_ALLOW_THREADS
7632 if (n < 0) {
7633 Py_DECREF(buffer);
7634 return posix_error();
7635 }
7636 if (n != size)
7637 _PyBytes_Resize(&buffer, n);
7638 return buffer;
7639}
7640#endif
7641
7642PyDoc_STRVAR(posix_write__doc__,
Benjamin Peterson3b08a292013-05-24 14:35:57 -07007643"write(fd, data) -> byteswritten\n\n\
7644Write bytes to a file descriptor.");
Ross Lagerwall7807c352011-03-17 20:20:30 +02007645
7646static PyObject *
7647posix_write(PyObject *self, PyObject *args)
7648{
7649 Py_buffer pbuf;
7650 int fd;
7651 Py_ssize_t size, len;
7652
7653 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7654 return NULL;
7655 if (!_PyVerify_fd(fd)) {
7656 PyBuffer_Release(&pbuf);
7657 return posix_error();
7658 }
7659 len = pbuf.len;
7660 Py_BEGIN_ALLOW_THREADS
Victor Stinner14b9b112013-06-25 00:37:25 +02007661#ifdef MS_WINDOWS
Ross Lagerwall7807c352011-03-17 20:20:30 +02007662 if (len > INT_MAX)
7663 len = INT_MAX;
7664 size = write(fd, pbuf.buf, (int)len);
7665#else
7666 size = write(fd, pbuf.buf, len);
7667#endif
7668 Py_END_ALLOW_THREADS
7669 PyBuffer_Release(&pbuf);
7670 if (size < 0)
7671 return posix_error();
7672 return PyLong_FromSsize_t(size);
7673}
7674
7675#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007676PyDoc_STRVAR(posix_sendfile__doc__,
7677"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7678sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7679 -> byteswritten\n\
7680Copy nbytes bytes from file descriptor in to file descriptor out.");
7681
7682static PyObject *
7683posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7684{
7685 int in, out;
7686 Py_ssize_t ret;
7687 off_t offset;
7688
7689#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7690#ifndef __APPLE__
7691 Py_ssize_t len;
7692#endif
7693 PyObject *headers = NULL, *trailers = NULL;
7694 Py_buffer *hbuf, *tbuf;
7695 off_t sbytes;
7696 struct sf_hdtr sf;
7697 int flags = 0;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007698 static char *keywords[] = {"out", "in",
7699 "offset", "count",
7700 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007701
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02007702 sf.headers = NULL;
7703 sf.trailers = NULL;
7704
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007705#ifdef __APPLE__
7706 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007707 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007708#else
7709 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007710 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007711#endif
7712 &headers, &trailers, &flags))
7713 return NULL;
7714 if (headers != NULL) {
7715 if (!PySequence_Check(headers)) {
7716 PyErr_SetString(PyExc_TypeError,
7717 "sendfile() headers must be a sequence or None");
7718 return NULL;
7719 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007720 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007721 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007722 if (sf.hdr_cnt > 0 &&
7723 !(i = iov_setup(&(sf.headers), &hbuf,
7724 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007725 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007726#ifdef __APPLE__
7727 sbytes += i;
7728#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007729 }
7730 }
7731 if (trailers != NULL) {
7732 if (!PySequence_Check(trailers)) {
7733 PyErr_SetString(PyExc_TypeError,
7734 "sendfile() trailers must be a sequence or None");
7735 return NULL;
7736 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007737 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007738 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007739 if (sf.trl_cnt > 0 &&
7740 !(i = iov_setup(&(sf.trailers), &tbuf,
7741 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007742 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007743#ifdef __APPLE__
7744 sbytes += i;
7745#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007746 }
7747 }
7748
7749 Py_BEGIN_ALLOW_THREADS
7750#ifdef __APPLE__
7751 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7752#else
7753 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7754#endif
7755 Py_END_ALLOW_THREADS
7756
7757 if (sf.headers != NULL)
7758 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7759 if (sf.trailers != NULL)
7760 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7761
7762 if (ret < 0) {
7763 if ((errno == EAGAIN) || (errno == EBUSY)) {
7764 if (sbytes != 0) {
7765 // some data has been sent
7766 goto done;
7767 }
7768 else {
7769 // no data has been sent; upper application is supposed
7770 // to retry on EAGAIN or EBUSY
7771 return posix_error();
7772 }
7773 }
7774 return posix_error();
7775 }
7776 goto done;
7777
7778done:
7779 #if !defined(HAVE_LARGEFILE_SUPPORT)
7780 return Py_BuildValue("l", sbytes);
7781 #else
7782 return Py_BuildValue("L", sbytes);
7783 #endif
7784
7785#else
7786 Py_ssize_t count;
7787 PyObject *offobj;
7788 static char *keywords[] = {"out", "in",
7789 "offset", "count", NULL};
7790 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7791 keywords, &out, &in, &offobj, &count))
7792 return NULL;
7793#ifdef linux
7794 if (offobj == Py_None) {
7795 Py_BEGIN_ALLOW_THREADS
7796 ret = sendfile(out, in, NULL, count);
7797 Py_END_ALLOW_THREADS
7798 if (ret < 0)
7799 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007800 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007801 }
7802#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007803 if (!_parse_off_t(offobj, &offset))
7804 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007805 Py_BEGIN_ALLOW_THREADS
7806 ret = sendfile(out, in, &offset, count);
7807 Py_END_ALLOW_THREADS
7808 if (ret < 0)
7809 return posix_error();
7810 return Py_BuildValue("n", ret);
7811#endif
7812}
7813#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007815PyDoc_STRVAR(posix_fstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007816"fstat(fd) -> stat result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07007817Like stat(), but for an open file descriptor.\n\
7818Equivalent to stat(fd=fd).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007819
Barry Warsaw53699e91996-12-10 23:23:01 +00007820static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007821posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007822{
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 int fd;
7824 STRUCT_STAT st;
7825 int res;
Victor Stinner4195b5c2012-02-08 23:03:19 +01007826 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007828#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 /* on OpenVMS we must ensure that all bytes are written to the file */
7830 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007831#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 Py_BEGIN_ALLOW_THREADS
7833 res = FSTAT(fd, &st);
7834 Py_END_ALLOW_THREADS
7835 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007836#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01007837 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00007838#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007840#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 }
Tim Peters5aa91602002-01-30 05:46:57 +00007842
Victor Stinner4195b5c2012-02-08 23:03:19 +01007843 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007844}
7845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007846PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007847"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007848Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007849connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007850
7851static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007852posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007853{
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 int fd;
7855 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7856 return NULL;
7857 if (!_PyVerify_fd(fd))
7858 return PyBool_FromLong(0);
7859 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007860}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007861
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007862#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007863PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007864"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007865Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007866
Barry Warsaw53699e91996-12-10 23:23:01 +00007867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007868posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007869{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007870#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 int fds[2];
7872 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 if (res != 0)
7875 return posix_error();
7876 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007877#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 HANDLE read, write;
7879 int read_fd, write_fd;
7880 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01007883 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007884 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7885 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7886 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007887#endif /* MS_WINDOWS */
Guido van Rossum687dd131993-05-17 08:34:16 +00007888}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007889#endif /* HAVE_PIPE */
7890
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007891#ifdef HAVE_PIPE2
7892PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007893"pipe2(flags) -> (read_end, write_end)\n\n\
7894Create a pipe with flags set atomically.\n\
7895flags can be constructed by ORing together one or more of these values:\n\
7896O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007897");
7898
7899static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007900posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007901{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007902 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007903 int fds[2];
7904 int res;
7905
Serhiy Storchaka78980432013-01-15 01:12:17 +02007906 flags = _PyLong_AsInt(arg);
Charles-François Natali368f34b2011-06-06 19:49:47 +02007907 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007908 return NULL;
7909
7910 res = pipe2(fds, flags);
7911 if (res != 0)
7912 return posix_error();
7913 return Py_BuildValue("(ii)", fds[0], fds[1]);
7914}
7915#endif /* HAVE_PIPE2 */
7916
Ross Lagerwall7807c352011-03-17 20:20:30 +02007917#ifdef HAVE_WRITEV
7918PyDoc_STRVAR(posix_writev__doc__,
7919"writev(fd, buffers) -> byteswritten\n\n\
7920Write the contents of buffers to a file descriptor, where buffers is an\n\
7921arbitrary sequence of buffers.\n\
7922Returns the total bytes written.");
7923
7924static PyObject *
7925posix_writev(PyObject *self, PyObject *args)
7926{
7927 int fd, cnt;
7928 Py_ssize_t res;
7929 PyObject *seq;
7930 struct iovec *iov;
7931 Py_buffer *buf;
7932 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7933 return NULL;
7934 if (!PySequence_Check(seq)) {
7935 PyErr_SetString(PyExc_TypeError,
7936 "writev() arg 2 must be a sequence");
7937 return NULL;
7938 }
7939 cnt = PySequence_Size(seq);
7940
7941 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7942 return NULL;
7943 }
7944
7945 Py_BEGIN_ALLOW_THREADS
7946 res = writev(fd, iov, cnt);
7947 Py_END_ALLOW_THREADS
7948
7949 iov_cleanup(iov, buf, cnt);
7950 return PyLong_FromSsize_t(res);
7951}
7952#endif
7953
7954#ifdef HAVE_PWRITE
7955PyDoc_STRVAR(posix_pwrite__doc__,
7956"pwrite(fd, string, offset) -> byteswritten\n\n\
7957Write string to a file descriptor, fd, from offset, leaving the file\n\
7958offset unchanged.");
7959
7960static PyObject *
7961posix_pwrite(PyObject *self, PyObject *args)
7962{
7963 Py_buffer pbuf;
7964 int fd;
7965 off_t offset;
7966 Py_ssize_t size;
7967
7968 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7969 return NULL;
7970
7971 if (!_PyVerify_fd(fd)) {
7972 PyBuffer_Release(&pbuf);
7973 return posix_error();
7974 }
7975 Py_BEGIN_ALLOW_THREADS
7976 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7977 Py_END_ALLOW_THREADS
7978 PyBuffer_Release(&pbuf);
7979 if (size < 0)
7980 return posix_error();
7981 return PyLong_FromSsize_t(size);
7982}
7983#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007984
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007985#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007986PyDoc_STRVAR(posix_mkfifo__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007987"mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\
7988Create a FIFO (a POSIX named pipe).\n\
7989\n\
7990If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7991 and path should be relative; path will then be relative to that directory.\n\
7992dir_fd may not be implemented on your platform.\n\
7993 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007994
Barry Warsaw53699e91996-12-10 23:23:01 +00007995static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007996posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007997{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007998 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 int mode = 0666;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008000 int dir_fd = DEFAULT_DIR_FD;
8001 int result;
8002 PyObject *return_value = NULL;
8003 static char *keywords[] = {"path", "mode", "dir_fd", NULL};
8004
8005 memset(&path, 0, sizeof(path));
8006 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords,
8007 path_converter, &path,
8008 &mode,
8009#ifdef HAVE_MKFIFOAT
8010 dir_fd_converter, &dir_fd
8011#else
8012 dir_fd_unavailable, &dir_fd
8013#endif
8014 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008016
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008018#ifdef HAVE_MKFIFOAT
8019 if (dir_fd != DEFAULT_DIR_FD)
8020 result = mkfifoat(dir_fd, path.narrow, mode);
8021 else
8022#endif
8023 result = mkfifo(path.narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008025
8026 if (result < 0) {
8027 return_value = posix_error();
8028 goto exit;
8029 }
8030
8031 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008033
8034exit:
8035 path_cleanup(&path);
8036 return return_value;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008037}
8038#endif
8039
Neal Norwitz11690112002-07-30 01:08:28 +00008040#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008041PyDoc_STRVAR(posix_mknod__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008042"mknod(filename, mode=0o600, device=0, *, dir_fd=None)\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008043Create a filesystem node (file, device special file or named pipe)\n\
8044named filename. mode specifies both the permissions to use and the\n\
8045type of node to be created, being combined (bitwise OR) with one of\n\
8046S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008047device defines the newly created device special file (probably using\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008048os.makedev()), otherwise it is ignored.\n\
8049\n\
8050If dir_fd is not None, it should be a file descriptor open to a directory,\n\
8051 and path should be relative; path will then be relative to that directory.\n\
8052dir_fd may not be implemented on your platform.\n\
8053 If it is unavailable, using it will raise a NotImplementedError.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008054
8055
8056static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008057posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008058{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008059 path_t path;
8060 int mode = 0666;
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 int device = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008062 int dir_fd = DEFAULT_DIR_FD;
8063 int result;
8064 PyObject *return_value = NULL;
8065 static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL};
8066
8067 memset(&path, 0, sizeof(path));
8068 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords,
8069 path_converter, &path,
8070 &mode, &device,
8071#ifdef HAVE_MKNODAT
8072 dir_fd_converter, &dir_fd
8073#else
8074 dir_fd_unavailable, &dir_fd
8075#endif
8076 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00008077 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008078
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008080#ifdef HAVE_MKNODAT
8081 if (dir_fd != DEFAULT_DIR_FD)
8082 result = mknodat(dir_fd, path.narrow, mode, device);
8083 else
8084#endif
8085 result = mknod(path.narrow, mode, device);
Victor Stinner8c62be82010-05-06 00:08:46 +00008086 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008087
8088 if (result < 0) {
8089 return_value = posix_error();
8090 goto exit;
8091 }
8092
8093 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00008094 Py_INCREF(Py_None);
Georg Brandlf7875592012-06-24 13:58:31 +02008095
Larry Hastings9cf065c2012-06-22 16:30:09 -07008096exit:
8097 path_cleanup(&path);
8098 return return_value;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008099}
8100#endif
8101
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008102#ifdef HAVE_DEVICE_MACROS
8103PyDoc_STRVAR(posix_major__doc__,
8104"major(device) -> major number\n\
8105Extracts a device major number from a raw device number.");
8106
8107static PyObject *
8108posix_major(PyObject *self, PyObject *args)
8109{
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 int device;
8111 if (!PyArg_ParseTuple(args, "i:major", &device))
8112 return NULL;
8113 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008114}
8115
8116PyDoc_STRVAR(posix_minor__doc__,
8117"minor(device) -> minor number\n\
8118Extracts a device minor number from a raw device number.");
8119
8120static PyObject *
8121posix_minor(PyObject *self, PyObject *args)
8122{
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 int device;
8124 if (!PyArg_ParseTuple(args, "i:minor", &device))
8125 return NULL;
8126 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008127}
8128
8129PyDoc_STRVAR(posix_makedev__doc__,
8130"makedev(major, minor) -> device number\n\
8131Composes a raw device number from the major and minor device numbers.");
8132
8133static PyObject *
8134posix_makedev(PyObject *self, PyObject *args)
8135{
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 int major, minor;
8137 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
8138 return NULL;
8139 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008140}
8141#endif /* device macros */
8142
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008143
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008144#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008145PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008146"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008147Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008148
Barry Warsaw53699e91996-12-10 23:23:01 +00008149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008150posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008151{
Victor Stinner8c62be82010-05-06 00:08:46 +00008152 int fd;
8153 off_t length;
8154 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008155
Ross Lagerwall7807c352011-03-17 20:20:30 +02008156 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00008157 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008158
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 Py_BEGIN_ALLOW_THREADS
8160 res = ftruncate(fd, length);
8161 Py_END_ALLOW_THREADS
8162 if (res < 0)
8163 return posix_error();
8164 Py_INCREF(Py_None);
8165 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008166}
8167#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00008168
Ross Lagerwall7807c352011-03-17 20:20:30 +02008169#ifdef HAVE_TRUNCATE
8170PyDoc_STRVAR(posix_truncate__doc__,
8171"truncate(path, length)\n\n\
Georg Brandl306336b2012-06-24 12:55:33 +02008172Truncate the file given by path to length bytes.\n\
8173On some platforms, path may also be specified as an open file descriptor.\n\
8174 If this functionality is unavailable, using it raises an exception.");
Ross Lagerwall7807c352011-03-17 20:20:30 +02008175
8176static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02008177posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008178{
Georg Brandl306336b2012-06-24 12:55:33 +02008179 path_t path;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008180 off_t length;
8181 int res;
Georg Brandl306336b2012-06-24 12:55:33 +02008182 PyObject *result = NULL;
8183 static char *keywords[] = {"path", "length", NULL};
Ross Lagerwall7807c352011-03-17 20:20:30 +02008184
Georg Brandl306336b2012-06-24 12:55:33 +02008185 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008186 path.function_name = "truncate";
Georg Brandl306336b2012-06-24 12:55:33 +02008187#ifdef HAVE_FTRUNCATE
8188 path.allow_fd = 1;
8189#endif
8190 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords,
8191 path_converter, &path,
8192 _parse_off_t, &length))
Ross Lagerwall7807c352011-03-17 20:20:30 +02008193 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008194
8195 Py_BEGIN_ALLOW_THREADS
Georg Brandl306336b2012-06-24 12:55:33 +02008196#ifdef HAVE_FTRUNCATE
8197 if (path.fd != -1)
8198 res = ftruncate(path.fd, length);
8199 else
8200#endif
8201 res = truncate(path.narrow, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008202 Py_END_ALLOW_THREADS
Ross Lagerwall7807c352011-03-17 20:20:30 +02008203 if (res < 0)
Victor Stinner292c8352012-10-30 02:17:38 +01008204 result = path_error(&path);
Georg Brandl306336b2012-06-24 12:55:33 +02008205 else {
8206 Py_INCREF(Py_None);
8207 result = Py_None;
8208 }
8209 path_cleanup(&path);
8210 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008211}
8212#endif
8213
8214#ifdef HAVE_POSIX_FALLOCATE
8215PyDoc_STRVAR(posix_posix_fallocate__doc__,
8216"posix_fallocate(fd, offset, len)\n\n\
8217Ensures that enough disk space is allocated for the file specified by fd\n\
8218starting from offset and continuing for len bytes.");
8219
8220static PyObject *
8221posix_posix_fallocate(PyObject *self, PyObject *args)
8222{
8223 off_t len, offset;
8224 int res, fd;
8225
8226 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
8227 &fd, _parse_off_t, &offset, _parse_off_t, &len))
8228 return NULL;
8229
8230 Py_BEGIN_ALLOW_THREADS
8231 res = posix_fallocate(fd, offset, len);
8232 Py_END_ALLOW_THREADS
8233 if (res != 0) {
8234 errno = res;
8235 return posix_error();
8236 }
8237 Py_RETURN_NONE;
8238}
8239#endif
8240
8241#ifdef HAVE_POSIX_FADVISE
8242PyDoc_STRVAR(posix_posix_fadvise__doc__,
8243"posix_fadvise(fd, offset, len, advice)\n\n\
8244Announces an intention to access data in a specific pattern thus allowing\n\
8245the kernel to make optimizations.\n\
8246The advice applies to the region of the file specified by fd starting at\n\
8247offset and continuing for len bytes.\n\
8248advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
8249POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
8250POSIX_FADV_DONTNEED.");
8251
8252static PyObject *
8253posix_posix_fadvise(PyObject *self, PyObject *args)
8254{
8255 off_t len, offset;
8256 int res, fd, advice;
8257
8258 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
8259 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
8260 return NULL;
8261
8262 Py_BEGIN_ALLOW_THREADS
8263 res = posix_fadvise(fd, offset, len, advice);
8264 Py_END_ALLOW_THREADS
8265 if (res != 0) {
8266 errno = res;
8267 return posix_error();
8268 }
8269 Py_RETURN_NONE;
8270}
8271#endif
8272
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008273#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008274PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008275"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008276Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008277
Fred Drake762e2061999-08-26 17:23:54 +00008278/* Save putenv() parameters as values here, so we can collect them when they
8279 * get re-set with another call for the same key. */
8280static PyObject *posix_putenv_garbage;
8281
Tim Peters5aa91602002-01-30 05:46:57 +00008282static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008283posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008284{
Victor Stinner84ae1182010-05-06 22:05:07 +00008285 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008286#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01008287 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008288 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01008289
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008291 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01008292 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008294
Victor Stinner65170952011-11-22 22:16:17 +01008295 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00008296 if (newstr == NULL) {
8297 PyErr_NoMemory();
8298 goto error;
8299 }
Victor Stinner65170952011-11-22 22:16:17 +01008300 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
8301 PyErr_Format(PyExc_ValueError,
8302 "the environment variable is longer than %u characters",
8303 _MAX_ENV);
8304 goto error;
8305 }
8306
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02008308 if (newenv == NULL)
8309 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008312 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008314#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008315 PyObject *os1, *os2;
8316 char *s1, *s2;
8317 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008318
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008319 if (!PyArg_ParseTuple(args,
8320 "O&O&:putenv",
8321 PyUnicode_FSConverter, &os1,
8322 PyUnicode_FSConverter, &os2))
8323 return NULL;
8324 s1 = PyBytes_AsString(os1);
8325 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00008326
Victor Stinner65170952011-11-22 22:16:17 +01008327 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008328 if (newstr == NULL) {
8329 PyErr_NoMemory();
8330 goto error;
8331 }
8332
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008336 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008338#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008339
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 /* Install the first arg and newstr in posix_putenv_garbage;
8341 * this will cause previous value to be collected. This has to
8342 * happen after the real putenv() call because the old value
8343 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01008344 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 /* really not much we can do; just leak */
8346 PyErr_Clear();
8347 }
8348 else {
8349 Py_DECREF(newstr);
8350 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00008351
Martin v. Löwis011e8422009-05-05 04:43:17 +00008352#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008353 Py_DECREF(os1);
8354 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00008355#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008356 Py_RETURN_NONE;
8357
8358error:
8359#ifndef MS_WINDOWS
8360 Py_DECREF(os1);
8361 Py_DECREF(os2);
8362#endif
8363 Py_XDECREF(newstr);
8364 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00008365}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008366#endif /* putenv */
8367
Guido van Rossumc524d952001-10-19 01:31:59 +00008368#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008369PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008370"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008371Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00008372
8373static PyObject *
8374posix_unsetenv(PyObject *self, PyObject *args)
8375{
Victor Stinner65170952011-11-22 22:16:17 +01008376 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01008377#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01008378 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01008379#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008380
8381 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00008382
Victor Stinner65170952011-11-22 22:16:17 +01008383 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00008384 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00008385
Victor Stinner984890f2011-11-24 13:53:38 +01008386#ifdef HAVE_BROKEN_UNSETENV
8387 unsetenv(PyBytes_AS_STRING(name));
8388#else
Victor Stinner65170952011-11-22 22:16:17 +01008389 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008390 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06008391 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01008392 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008393 }
Victor Stinner984890f2011-11-24 13:53:38 +01008394#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008395
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 /* Remove the key from posix_putenv_garbage;
8397 * this will cause it to be collected. This has to
8398 * happen after the real unsetenv() call because the
8399 * old value was still accessible until then.
8400 */
Victor Stinner65170952011-11-22 22:16:17 +01008401 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 /* really not much we can do; just leak */
8403 PyErr_Clear();
8404 }
Victor Stinner65170952011-11-22 22:16:17 +01008405 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00008406 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00008407}
8408#endif /* unsetenv */
8409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008410PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008411"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008412Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008413
Guido van Rossumf68d8e52001-04-14 17:55:09 +00008414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008415posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00008416{
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 int code;
8418 char *message;
8419 if (!PyArg_ParseTuple(args, "i:strerror", &code))
8420 return NULL;
8421 message = strerror(code);
8422 if (message == NULL) {
8423 PyErr_SetString(PyExc_ValueError,
8424 "strerror() argument out of range");
8425 return NULL;
8426 }
Victor Stinner1b579672011-12-17 05:47:23 +01008427 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008428}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008429
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008430
Guido van Rossumc9641791998-08-04 15:26:23 +00008431#ifdef HAVE_SYS_WAIT_H
8432
Fred Drake106c1a02002-04-23 15:58:02 +00008433#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008434PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008435"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008436Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00008437
8438static PyObject *
8439posix_WCOREDUMP(PyObject *self, PyObject *args)
8440{
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 WAIT_TYPE status;
8442 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008443
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
8445 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008446
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008448}
8449#endif /* WCOREDUMP */
8450
8451#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008452PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008453"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00008454Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008455job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00008456
8457static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008458posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00008459{
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 WAIT_TYPE status;
8461 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008462
Victor Stinner8c62be82010-05-06 00:08:46 +00008463 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
8464 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008465
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008467}
8468#endif /* WIFCONTINUED */
8469
Guido van Rossumc9641791998-08-04 15:26:23 +00008470#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008471PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008472"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008473Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008474
8475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008476posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008477{
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 WAIT_TYPE status;
8479 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008480
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
8482 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008483
Victor Stinner8c62be82010-05-06 00:08:46 +00008484 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008485}
8486#endif /* WIFSTOPPED */
8487
8488#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008489PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008490"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008491Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008492
8493static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008494posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008495{
Victor Stinner8c62be82010-05-06 00:08:46 +00008496 WAIT_TYPE status;
8497 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008498
Victor Stinner8c62be82010-05-06 00:08:46 +00008499 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
8500 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008501
Victor Stinner8c62be82010-05-06 00:08:46 +00008502 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008503}
8504#endif /* WIFSIGNALED */
8505
8506#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008507PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008508"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008509Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008510system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008511
8512static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008513posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008514{
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 WAIT_TYPE status;
8516 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008517
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
8519 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008520
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008522}
8523#endif /* WIFEXITED */
8524
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00008525#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008526PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008527"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008528Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008529
8530static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008531posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008532{
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 WAIT_TYPE status;
8534 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008535
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
8537 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008538
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008540}
8541#endif /* WEXITSTATUS */
8542
8543#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008544PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008545"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008546Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008547value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008548
8549static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008550posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008551{
Victor Stinner8c62be82010-05-06 00:08:46 +00008552 WAIT_TYPE status;
8553 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008554
Victor Stinner8c62be82010-05-06 00:08:46 +00008555 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8556 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008557
Victor Stinner8c62be82010-05-06 00:08:46 +00008558 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008559}
8560#endif /* WTERMSIG */
8561
8562#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008563PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008564"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008565Return the signal that stopped the process that provided\n\
8566the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008567
8568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008569posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008570{
Victor Stinner8c62be82010-05-06 00:08:46 +00008571 WAIT_TYPE status;
8572 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008573
Victor Stinner8c62be82010-05-06 00:08:46 +00008574 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8575 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008576
Victor Stinner8c62be82010-05-06 00:08:46 +00008577 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008578}
8579#endif /* WSTOPSIG */
8580
8581#endif /* HAVE_SYS_WAIT_H */
8582
8583
Thomas Wouters477c8d52006-05-27 19:21:47 +00008584#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008585#ifdef _SCO_DS
8586/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8587 needed definitions in sys/statvfs.h */
8588#define _SVID3
8589#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008590#include <sys/statvfs.h>
8591
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008592static PyObject*
8593_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008594 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8595 if (v == NULL)
8596 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008597
8598#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8600 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8601 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8602 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8603 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8604 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8605 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8606 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8607 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8608 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008609#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008610 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8611 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8612 PyStructSequence_SET_ITEM(v, 2,
8613 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8614 PyStructSequence_SET_ITEM(v, 3,
8615 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8616 PyStructSequence_SET_ITEM(v, 4,
8617 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8618 PyStructSequence_SET_ITEM(v, 5,
8619 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8620 PyStructSequence_SET_ITEM(v, 6,
8621 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8622 PyStructSequence_SET_ITEM(v, 7,
8623 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8624 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8625 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008626#endif
8627
Victor Stinner8c62be82010-05-06 00:08:46 +00008628 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008629}
8630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008631PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008632"fstatvfs(fd) -> statvfs result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008633Perform an fstatvfs system call on the given fd.\n\
8634Equivalent to statvfs(fd).");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008635
8636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008637posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008638{
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 int fd, res;
8640 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008641
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8643 return NULL;
8644 Py_BEGIN_ALLOW_THREADS
8645 res = fstatvfs(fd, &st);
8646 Py_END_ALLOW_THREADS
8647 if (res != 0)
8648 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008649
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008651}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008652#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008653
8654
Thomas Wouters477c8d52006-05-27 19:21:47 +00008655#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008656#include <sys/statvfs.h>
8657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008658PyDoc_STRVAR(posix_statvfs__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008659"statvfs(path)\n\n\
8660Perform a statvfs system call on the given path.\n\
8661\n\
8662path may always be specified as a string.\n\
8663On some platforms, path may also be specified as an open file descriptor.\n\
8664 If this functionality is unavailable, using it raises an exception.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008665
8666static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008667posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008668{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008669 static char *keywords[] = {"path", NULL};
8670 path_t path;
8671 int result;
8672 PyObject *return_value = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008673 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008674
Larry Hastings9cf065c2012-06-22 16:30:09 -07008675 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008676 path.function_name = "statvfs";
Larry Hastings9cf065c2012-06-22 16:30:09 -07008677#ifdef HAVE_FSTATVFS
8678 path.allow_fd = 1;
8679#endif
8680 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords,
8681 path_converter, &path
8682 ))
8683 return NULL;
8684
8685 Py_BEGIN_ALLOW_THREADS
8686#ifdef HAVE_FSTATVFS
8687 if (path.fd != -1) {
8688#ifdef __APPLE__
8689 /* handle weak-linking on Mac OS X 10.3 */
8690 if (fstatvfs == NULL) {
8691 fd_specified("statvfs", path.fd);
8692 goto exit;
8693 }
8694#endif
8695 result = fstatvfs(path.fd, &st);
8696 }
8697 else
8698#endif
8699 result = statvfs(path.narrow, &st);
8700 Py_END_ALLOW_THREADS
8701
8702 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01008703 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008704 goto exit;
8705 }
8706
8707 return_value = _pystatvfs_fromstructstatvfs(st);
8708
8709exit:
8710 path_cleanup(&path);
8711 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00008712}
8713#endif /* HAVE_STATVFS */
8714
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008715#ifdef MS_WINDOWS
8716PyDoc_STRVAR(win32__getdiskusage__doc__,
8717"_getdiskusage(path) -> (total, free)\n\n\
8718Return disk usage statistics about the given path as (total, free) tuple.");
8719
8720static PyObject *
8721win32__getdiskusage(PyObject *self, PyObject *args)
8722{
8723 BOOL retval;
8724 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008725 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008726
Victor Stinner6139c1b2011-11-09 22:14:14 +01008727 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008728 return NULL;
8729
8730 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008731 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008732 Py_END_ALLOW_THREADS
8733 if (retval == 0)
8734 return PyErr_SetFromWindowsErr(0);
8735
8736 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8737}
8738#endif
8739
8740
Fred Drakec9680921999-12-13 16:37:25 +00008741/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8742 * It maps strings representing configuration variable names to
8743 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008744 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008745 * rarely-used constants. There are three separate tables that use
8746 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008747 *
8748 * This code is always included, even if none of the interfaces that
8749 * need it are included. The #if hackery needed to avoid it would be
8750 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008751 */
8752struct constdef {
8753 char *name;
8754 long value;
8755};
8756
Fred Drake12c6e2d1999-12-14 21:25:03 +00008757static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008758conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008759 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008760{
Christian Heimes217cfd12007-12-02 14:31:20 +00008761 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008762 *valuep = PyLong_AS_LONG(arg);
8763 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008764 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008765 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008766 /* look up the value in the table using a binary search */
8767 size_t lo = 0;
8768 size_t mid;
8769 size_t hi = tablesize;
8770 int cmp;
8771 const char *confname;
8772 if (!PyUnicode_Check(arg)) {
8773 PyErr_SetString(PyExc_TypeError,
8774 "configuration names must be strings or integers");
8775 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008777 confname = _PyUnicode_AsString(arg);
8778 if (confname == NULL)
8779 return 0;
8780 while (lo < hi) {
8781 mid = (lo + hi) / 2;
8782 cmp = strcmp(confname, table[mid].name);
8783 if (cmp < 0)
8784 hi = mid;
8785 else if (cmp > 0)
8786 lo = mid + 1;
8787 else {
8788 *valuep = table[mid].value;
8789 return 1;
8790 }
8791 }
8792 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8793 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008795}
8796
8797
8798#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8799static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008800#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008802#endif
8803#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008805#endif
Fred Drakec9680921999-12-13 16:37:25 +00008806#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008808#endif
8809#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008811#endif
8812#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008814#endif
8815#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008817#endif
8818#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008820#endif
8821#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
8842#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008844#endif
8845#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008847#endif
8848#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008851#ifdef _PC_ACL_ENABLED
8852 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8853#endif
8854#ifdef _PC_MIN_HOLE_SIZE
8855 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8856#endif
8857#ifdef _PC_ALLOC_SIZE_MIN
8858 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8859#endif
8860#ifdef _PC_REC_INCR_XFER_SIZE
8861 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8862#endif
8863#ifdef _PC_REC_MAX_XFER_SIZE
8864 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8865#endif
8866#ifdef _PC_REC_MIN_XFER_SIZE
8867 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8868#endif
8869#ifdef _PC_REC_XFER_ALIGN
8870 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8871#endif
8872#ifdef _PC_SYMLINK_MAX
8873 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8874#endif
8875#ifdef _PC_XATTR_ENABLED
8876 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8877#endif
8878#ifdef _PC_XATTR_EXISTS
8879 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8880#endif
8881#ifdef _PC_TIMESTAMP_RESOLUTION
8882 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8883#endif
Fred Drakec9680921999-12-13 16:37:25 +00008884};
8885
Fred Drakec9680921999-12-13 16:37:25 +00008886static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008887conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008888{
8889 return conv_confname(arg, valuep, posix_constants_pathconf,
8890 sizeof(posix_constants_pathconf)
8891 / sizeof(struct constdef));
8892}
8893#endif
8894
8895#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008896PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008897"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008898Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008899If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008900
8901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008902posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008903{
8904 PyObject *result = NULL;
8905 int name, fd;
8906
Fred Drake12c6e2d1999-12-14 21:25:03 +00008907 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8908 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008909 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008910
Stefan Krah0e803b32010-11-26 16:16:47 +00008911 errno = 0;
8912 limit = fpathconf(fd, name);
8913 if (limit == -1 && errno != 0)
8914 posix_error();
8915 else
8916 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008917 }
8918 return result;
8919}
8920#endif
8921
8922
8923#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008924PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008925"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008926Return the configuration limit name for the file or directory path.\n\
Georg Brandl306336b2012-06-24 12:55:33 +02008927If there is no limit, return -1.\n\
8928On some platforms, path may also be specified as an open file descriptor.\n\
8929 If this functionality is unavailable, using it raises an exception.");
Fred Drakec9680921999-12-13 16:37:25 +00008930
8931static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02008932posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs)
Fred Drakec9680921999-12-13 16:37:25 +00008933{
Georg Brandl306336b2012-06-24 12:55:33 +02008934 path_t path;
Fred Drakec9680921999-12-13 16:37:25 +00008935 PyObject *result = NULL;
8936 int name;
Georg Brandl306336b2012-06-24 12:55:33 +02008937 static char *keywords[] = {"path", "name", NULL};
Fred Drakec9680921999-12-13 16:37:25 +00008938
Georg Brandl306336b2012-06-24 12:55:33 +02008939 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008940 path.function_name = "pathconf";
Georg Brandl306336b2012-06-24 12:55:33 +02008941#ifdef HAVE_FPATHCONF
8942 path.allow_fd = 1;
8943#endif
8944 if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords,
8945 path_converter, &path,
8946 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008948
Victor Stinner8c62be82010-05-06 00:08:46 +00008949 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02008950#ifdef HAVE_FPATHCONF
8951 if (path.fd != -1)
8952 limit = fpathconf(path.fd, name);
8953 else
8954#endif
8955 limit = pathconf(path.narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00008956 if (limit == -1 && errno != 0) {
8957 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008958 /* could be a path or name problem */
8959 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008960 else
Victor Stinner292c8352012-10-30 02:17:38 +01008961 result = path_error(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008962 }
8963 else
8964 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008965 }
Georg Brandl306336b2012-06-24 12:55:33 +02008966 path_cleanup(&path);
Fred Drakec9680921999-12-13 16:37:25 +00008967 return result;
8968}
8969#endif
8970
8971#ifdef HAVE_CONFSTR
8972static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008973#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008974 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008975#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008976#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008977 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008978#endif
8979#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008980 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008981#endif
Fred Draked86ed291999-12-15 15:34:33 +00008982#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008983 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008984#endif
8985#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008986 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008987#endif
8988#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008989 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008990#endif
8991#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008992 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008993#endif
Fred Drakec9680921999-12-13 16:37:25 +00008994#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008995 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008996#endif
8997#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008998 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008999#endif
9000#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009001 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009002#endif
9003#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009004 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009005#endif
9006#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009008#endif
9009#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009010 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009011#endif
9012#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009013 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009014#endif
9015#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009016 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009017#endif
Fred Draked86ed291999-12-15 15:34:33 +00009018#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009019 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009020#endif
Fred Drakec9680921999-12-13 16:37:25 +00009021#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009022 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009023#endif
Fred Draked86ed291999-12-15 15:34:33 +00009024#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009025 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009026#endif
9027#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009028 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009029#endif
9030#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009031 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009032#endif
9033#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009034 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009035#endif
Fred Drakec9680921999-12-13 16:37:25 +00009036#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009037 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009038#endif
9039#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009040 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009041#endif
9042#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009043 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009044#endif
9045#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009046 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009047#endif
9048#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009049 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009050#endif
9051#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009052 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009053#endif
9054#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009055 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009056#endif
9057#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009058 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009059#endif
9060#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009061 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009062#endif
9063#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009064 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009065#endif
9066#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009067 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009068#endif
9069#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009070 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009071#endif
9072#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009073 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009074#endif
9075#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009076 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009077#endif
9078#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009079 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009080#endif
9081#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009082 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009083#endif
Fred Draked86ed291999-12-15 15:34:33 +00009084#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009085 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009086#endif
9087#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009088 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00009089#endif
9090#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00009091 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00009092#endif
9093#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009094 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009095#endif
9096#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009097 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009098#endif
9099#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00009100 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00009101#endif
9102#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009103 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00009104#endif
9105#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00009106 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00009107#endif
9108#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009109 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009110#endif
9111#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009112 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009113#endif
9114#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009115 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009116#endif
9117#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009119#endif
9120#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00009121 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00009122#endif
Fred Drakec9680921999-12-13 16:37:25 +00009123};
9124
9125static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009126conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009127{
9128 return conv_confname(arg, valuep, posix_constants_confstr,
9129 sizeof(posix_constants_confstr)
9130 / sizeof(struct constdef));
9131}
9132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009133PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009134"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009135Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009136
9137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009138posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009139{
9140 PyObject *result = NULL;
9141 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00009142 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009143 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +00009144
Victor Stinnercb043522010-09-10 23:49:04 +00009145 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
9146 return NULL;
9147
9148 errno = 0;
9149 len = confstr(name, buffer, sizeof(buffer));
9150 if (len == 0) {
9151 if (errno) {
9152 posix_error();
9153 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009154 }
9155 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009156 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009157 }
9158 }
Victor Stinnercb043522010-09-10 23:49:04 +00009159
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009160 if (len >= sizeof(buffer)) {
Victor Stinnercb043522010-09-10 23:49:04 +00009161 char *buf = PyMem_Malloc(len);
9162 if (buf == NULL)
9163 return PyErr_NoMemory();
9164 confstr(name, buf, len);
9165 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
9166 PyMem_Free(buf);
9167 }
9168 else
9169 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009170 return result;
9171}
9172#endif
9173
9174
9175#ifdef HAVE_SYSCONF
9176static struct constdef posix_constants_sysconf[] = {
9177#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009178 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009179#endif
9180#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009181 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009182#endif
9183#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009184 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009185#endif
9186#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009187 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009188#endif
9189#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009190 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009191#endif
9192#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00009193 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00009194#endif
9195#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00009196 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00009197#endif
9198#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009199 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009200#endif
9201#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009202 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00009203#endif
9204#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009205 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009206#endif
Fred Draked86ed291999-12-15 15:34:33 +00009207#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009208 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009209#endif
9210#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00009212#endif
Fred Drakec9680921999-12-13 16:37:25 +00009213#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009214 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009215#endif
Fred Drakec9680921999-12-13 16:37:25 +00009216#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009218#endif
9219#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009220 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009221#endif
9222#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009223 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009224#endif
9225#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009226 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009227#endif
9228#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009229 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009230#endif
Fred Draked86ed291999-12-15 15:34:33 +00009231#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009232 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009233#endif
Fred Drakec9680921999-12-13 16:37:25 +00009234#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009235 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009236#endif
9237#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009238 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009239#endif
9240#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009241 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009242#endif
9243#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009245#endif
9246#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009247 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009248#endif
Fred Draked86ed291999-12-15 15:34:33 +00009249#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009250 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009251#endif
Fred Drakec9680921999-12-13 16:37:25 +00009252#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009253 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009254#endif
9255#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009256 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009257#endif
9258#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009259 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009260#endif
9261#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009262 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009263#endif
9264#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009265 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009266#endif
9267#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009268 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00009269#endif
9270#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009271 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009272#endif
9273#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009274 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009275#endif
9276#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009277 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009278#endif
9279#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009280 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009281#endif
9282#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009283 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009284#endif
9285#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009286 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009287#endif
9288#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009289 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009290#endif
9291#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009292 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009293#endif
9294#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009295 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009296#endif
9297#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009298 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009299#endif
9300#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009301 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00009302#endif
9303#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009304 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009305#endif
9306#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009307 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009308#endif
9309#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009310 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009311#endif
9312#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009313 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009314#endif
9315#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009316 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009317#endif
9318#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009319 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009320#endif
Fred Draked86ed291999-12-15 15:34:33 +00009321#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00009322 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00009323#endif
Fred Drakec9680921999-12-13 16:37:25 +00009324#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009325 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009326#endif
9327#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009328 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009329#endif
9330#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009331 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009332#endif
Fred Draked86ed291999-12-15 15:34:33 +00009333#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009334 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00009335#endif
Fred Drakec9680921999-12-13 16:37:25 +00009336#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00009337 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00009338#endif
Fred Draked86ed291999-12-15 15:34:33 +00009339#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009340 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00009341#endif
9342#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00009343 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00009344#endif
Fred Drakec9680921999-12-13 16:37:25 +00009345#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009346 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009347#endif
9348#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009349 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009350#endif
9351#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009352 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009353#endif
9354#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009355 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009356#endif
Fred Draked86ed291999-12-15 15:34:33 +00009357#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00009358 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00009359#endif
Fred Drakec9680921999-12-13 16:37:25 +00009360#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00009361 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00009362#endif
9363#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009364 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00009365#endif
9366#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009367 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009368#endif
9369#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009370 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00009371#endif
9372#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009373 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00009374#endif
9375#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00009376 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00009377#endif
9378#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00009379 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00009380#endif
Fred Draked86ed291999-12-15 15:34:33 +00009381#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00009382 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00009383#endif
Fred Drakec9680921999-12-13 16:37:25 +00009384#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009385 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009386#endif
9387#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009388 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009389#endif
Fred Draked86ed291999-12-15 15:34:33 +00009390#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009391 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009392#endif
Fred Drakec9680921999-12-13 16:37:25 +00009393#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009394 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009395#endif
9396#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009397 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009398#endif
9399#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009400 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009401#endif
9402#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009403 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009404#endif
9405#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009406 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009407#endif
9408#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009409 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009410#endif
9411#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009412 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009413#endif
9414#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009415 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00009416#endif
9417#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009418 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00009419#endif
Fred Draked86ed291999-12-15 15:34:33 +00009420#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009421 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00009422#endif
9423#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009424 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00009425#endif
Fred Drakec9680921999-12-13 16:37:25 +00009426#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00009427 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00009428#endif
9429#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009430 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009431#endif
9432#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009433 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009434#endif
9435#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009436 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009437#endif
9438#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009439 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009440#endif
9441#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009442 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009443#endif
9444#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00009445 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00009446#endif
9447#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00009448 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00009449#endif
9450#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009451 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00009452#endif
9453#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009454 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00009455#endif
9456#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00009457 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00009458#endif
9459#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009460 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00009461#endif
9462#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009463 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00009464#endif
9465#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00009466 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00009467#endif
9468#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00009469 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00009470#endif
9471#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00009472 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00009473#endif
9474#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00009475 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00009476#endif
9477#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009478 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009479#endif
9480#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009481 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009482#endif
9483#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00009484 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00009485#endif
9486#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009487 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009488#endif
9489#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009490 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009491#endif
9492#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00009493 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00009494#endif
9495#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009496 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009497#endif
9498#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009499 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009500#endif
9501#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009502 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00009503#endif
9504#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00009505 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00009506#endif
9507#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009508 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009509#endif
9510#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009511 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009512#endif
9513#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009514 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00009515#endif
9516#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009517 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009518#endif
9519#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009520 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009521#endif
9522#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009523 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009524#endif
9525#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009526 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009527#endif
9528#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009529 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009530#endif
Fred Draked86ed291999-12-15 15:34:33 +00009531#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00009533#endif
Fred Drakec9680921999-12-13 16:37:25 +00009534#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00009535 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00009536#endif
9537#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009539#endif
9540#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00009541 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00009542#endif
9543#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009544 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009545#endif
9546#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009547 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009548#endif
9549#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009551#endif
9552#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00009553 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00009554#endif
9555#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009556 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009557#endif
9558#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009559 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009560#endif
9561#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009562 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009563#endif
9564#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009565 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009566#endif
9567#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009568 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00009569#endif
9570#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009571 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00009572#endif
9573#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00009574 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00009575#endif
9576#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009577 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009578#endif
9579#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009580 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009581#endif
9582#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009583 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009584#endif
9585#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009586 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009587#endif
9588#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009589 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009590#endif
9591#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009592 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009593#endif
9594#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009595 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009596#endif
9597#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009598 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009599#endif
9600#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009601 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009602#endif
9603#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009604 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009605#endif
9606#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009607 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009608#endif
9609#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009610 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009611#endif
9612#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009613 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009614#endif
9615#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009616 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009617#endif
9618#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009619 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009620#endif
9621#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009622 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009623#endif
9624#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009625 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009626#endif
9627#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009628 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009629#endif
9630#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009631 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009632#endif
9633#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009634 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009635#endif
9636#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009637 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009638#endif
9639#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009640 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009641#endif
9642#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009643 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009644#endif
9645#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009646 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009647#endif
9648#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009649 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009650#endif
9651#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009652 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009653#endif
9654#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009655 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009656#endif
9657#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009658 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009659#endif
9660#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009661 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009662#endif
9663#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009664 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009665#endif
9666#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009667 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009668#endif
9669};
9670
9671static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009672conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009673{
9674 return conv_confname(arg, valuep, posix_constants_sysconf,
9675 sizeof(posix_constants_sysconf)
9676 / sizeof(struct constdef));
9677}
9678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009679PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009680"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009681Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009682
9683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009684posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009685{
9686 PyObject *result = NULL;
9687 int name;
9688
9689 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
Victor Stinner6fdd7b82013-05-16 22:26:29 +02009690 long value;
Fred Drakec9680921999-12-13 16:37:25 +00009691
9692 errno = 0;
9693 value = sysconf(name);
9694 if (value == -1 && errno != 0)
9695 posix_error();
9696 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009697 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009698 }
9699 return result;
9700}
9701#endif
9702
9703
Fred Drakebec628d1999-12-15 18:31:10 +00009704/* This code is used to ensure that the tables of configuration value names
9705 * are in sorted order as required by conv_confname(), and also to build the
9706 * the exported dictionaries that are used to publish information about the
9707 * names available on the host platform.
9708 *
9709 * Sorting the table at runtime ensures that the table is properly ordered
9710 * when used, even for platforms we're not able to test on. It also makes
9711 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009712 */
Fred Drakebec628d1999-12-15 18:31:10 +00009713
9714static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009715cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009716{
9717 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009718 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009719 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009720 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009721
9722 return strcmp(c1->name, c2->name);
9723}
9724
9725static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009726setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009727 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009728{
Fred Drakebec628d1999-12-15 18:31:10 +00009729 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009730 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009731
9732 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9733 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009734 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009735 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009736
Barry Warsaw3155db32000-04-13 15:20:40 +00009737 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009738 PyObject *o = PyLong_FromLong(table[i].value);
9739 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9740 Py_XDECREF(o);
9741 Py_DECREF(d);
9742 return -1;
9743 }
9744 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009745 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009746 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009747}
9748
Fred Drakebec628d1999-12-15 18:31:10 +00009749/* Return -1 on failure, 0 on success. */
9750static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009751setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009752{
9753#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009754 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009755 sizeof(posix_constants_pathconf)
9756 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009757 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009758 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009759#endif
9760#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009761 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009762 sizeof(posix_constants_confstr)
9763 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009764 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009765 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009766#endif
9767#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009768 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009769 sizeof(posix_constants_sysconf)
9770 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009771 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009772 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009773#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009774 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009775}
Fred Draked86ed291999-12-15 15:34:33 +00009776
9777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009778PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009779"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009780Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009781in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009782
9783static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009784posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009785{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009786 abort();
9787 /*NOTREACHED*/
9788 Py_FatalError("abort() called from Python code didn't abort!");
9789 return NULL;
9790}
Fred Drakebec628d1999-12-15 18:31:10 +00009791
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009792#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009793PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009794"startfile(filepath [, operation]) - Start a file with its associated\n\
9795application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009796\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009797When \"operation\" is not specified or \"open\", this acts like\n\
9798double-clicking the file in Explorer, or giving the file name as an\n\
9799argument to the DOS \"start\" command: the file is opened with whatever\n\
9800application (if any) its extension is associated.\n\
9801When another \"operation\" is given, it specifies what should be done with\n\
9802the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009803\n\
9804startfile returns as soon as the associated application is launched.\n\
9805There is no option to wait for the application to close, and no way\n\
9806to retrieve the application's exit status.\n\
9807\n\
9808The filepath is relative to the current directory. If you want to use\n\
9809an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009810the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009811
9812static PyObject *
9813win32_startfile(PyObject *self, PyObject *args)
9814{
Victor Stinner8c62be82010-05-06 00:08:46 +00009815 PyObject *ofilepath;
9816 char *filepath;
9817 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009818 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009819 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009820
Victor Stinnereb5657a2011-09-30 01:44:27 +02009821 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009822 if (!PyArg_ParseTuple(args, "U|s:startfile",
9823 &unipath, &operation)) {
9824 PyErr_Clear();
9825 goto normal;
9826 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009827
Victor Stinner8c62be82010-05-06 00:08:46 +00009828 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009829 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009830 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009831 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009832 PyErr_Clear();
9833 operation = NULL;
9834 goto normal;
9835 }
9836 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009837
Victor Stinnereb5657a2011-09-30 01:44:27 +02009838 wpath = PyUnicode_AsUnicode(unipath);
9839 if (wpath == NULL)
9840 goto normal;
9841 if (uoperation) {
9842 woperation = PyUnicode_AsUnicode(uoperation);
9843 if (woperation == NULL)
9844 goto normal;
9845 }
9846 else
9847 woperation = NULL;
9848
Victor Stinner8c62be82010-05-06 00:08:46 +00009849 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009850 rc = ShellExecuteW((HWND)0, woperation, wpath,
9851 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009852 Py_END_ALLOW_THREADS
9853
Victor Stinnereb5657a2011-09-30 01:44:27 +02009854 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009855 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009856 win32_error_object("startfile", unipath);
9857 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009858 }
9859 Py_INCREF(Py_None);
9860 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009861
9862normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009863 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9864 PyUnicode_FSConverter, &ofilepath,
9865 &operation))
9866 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009867 if (win32_warn_bytes_api()) {
9868 Py_DECREF(ofilepath);
9869 return NULL;
9870 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009871 filepath = PyBytes_AsString(ofilepath);
9872 Py_BEGIN_ALLOW_THREADS
9873 rc = ShellExecute((HWND)0, operation, filepath,
9874 NULL, NULL, SW_SHOWNORMAL);
9875 Py_END_ALLOW_THREADS
9876 if (rc <= (HINSTANCE)32) {
9877 PyObject *errval = win32_error("startfile", filepath);
9878 Py_DECREF(ofilepath);
9879 return errval;
9880 }
9881 Py_DECREF(ofilepath);
9882 Py_INCREF(Py_None);
9883 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009884}
9885#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009886
Martin v. Löwis438b5342002-12-27 10:16:42 +00009887#ifdef HAVE_GETLOADAVG
9888PyDoc_STRVAR(posix_getloadavg__doc__,
9889"getloadavg() -> (float, float, float)\n\n\
9890Return the number of processes in the system run queue averaged over\n\
9891the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9892was unobtainable");
9893
9894static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009895posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009896{
9897 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009898 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009899 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9900 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009901 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009902 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009903}
9904#endif
9905
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009906PyDoc_STRVAR(device_encoding__doc__,
9907"device_encoding(fd) -> str\n\n\
9908Return a string describing the encoding of the device\n\
9909if the output is a terminal; else return None.");
9910
9911static PyObject *
9912device_encoding(PyObject *self, PyObject *args)
9913{
Victor Stinner8c62be82010-05-06 00:08:46 +00009914 int fd;
Brett Cannonefb00c02012-02-29 18:31:31 -05009915
Victor Stinner8c62be82010-05-06 00:08:46 +00009916 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9917 return NULL;
Brett Cannonefb00c02012-02-29 18:31:31 -05009918
9919 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009920}
9921
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009922#ifdef HAVE_SETRESUID
9923PyDoc_STRVAR(posix_setresuid__doc__,
9924"setresuid(ruid, euid, suid)\n\n\
9925Set the current process's real, effective, and saved user ids.");
9926
9927static PyObject*
9928posix_setresuid (PyObject *self, PyObject *args)
9929{
Victor Stinner8c62be82010-05-06 00:08:46 +00009930 /* We assume uid_t is no larger than a long. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009931 uid_t ruid, euid, suid;
9932 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
9933 _Py_Uid_Converter, &ruid,
9934 _Py_Uid_Converter, &euid,
9935 _Py_Uid_Converter, &suid))
Victor Stinner8c62be82010-05-06 00:08:46 +00009936 return NULL;
9937 if (setresuid(ruid, euid, suid) < 0)
9938 return posix_error();
9939 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009940}
9941#endif
9942
9943#ifdef HAVE_SETRESGID
9944PyDoc_STRVAR(posix_setresgid__doc__,
9945"setresgid(rgid, egid, sgid)\n\n\
9946Set the current process's real, effective, and saved group ids.");
9947
9948static PyObject*
9949posix_setresgid (PyObject *self, PyObject *args)
9950{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009951 gid_t rgid, egid, sgid;
9952 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
9953 _Py_Gid_Converter, &rgid,
9954 _Py_Gid_Converter, &egid,
9955 _Py_Gid_Converter, &sgid))
Victor Stinner8c62be82010-05-06 00:08:46 +00009956 return NULL;
9957 if (setresgid(rgid, egid, sgid) < 0)
9958 return posix_error();
9959 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009960}
9961#endif
9962
9963#ifdef HAVE_GETRESUID
9964PyDoc_STRVAR(posix_getresuid__doc__,
9965"getresuid() -> (ruid, euid, suid)\n\n\
9966Get tuple of the current process's real, effective, and saved user ids.");
9967
9968static PyObject*
9969posix_getresuid (PyObject *self, PyObject *noargs)
9970{
Victor Stinner8c62be82010-05-06 00:08:46 +00009971 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +00009972 if (getresuid(&ruid, &euid, &suid) < 0)
9973 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009974 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
9975 _PyLong_FromUid(euid),
9976 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009977}
9978#endif
9979
9980#ifdef HAVE_GETRESGID
9981PyDoc_STRVAR(posix_getresgid__doc__,
9982"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009983Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009984
9985static PyObject*
9986posix_getresgid (PyObject *self, PyObject *noargs)
9987{
Victor Stinner8c62be82010-05-06 00:08:46 +00009988 uid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +00009989 if (getresgid(&rgid, &egid, &sgid) < 0)
9990 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009991 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
9992 _PyLong_FromGid(egid),
9993 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009994}
9995#endif
9996
Benjamin Peterson9428d532011-09-14 11:45:52 -04009997#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -04009998
Benjamin Peterson799bd802011-08-31 22:15:17 -04009999PyDoc_STRVAR(posix_getxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010000"getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\
10001Return the value of extended attribute attribute on path.\n\
10002\n\
10003path may be either a string or an open file descriptor.\n\
10004If follow_symlinks is False, and the last element of the path is a symbolic\n\
10005 link, getxattr will examine the symbolic link itself instead of the file\n\
10006 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010007
10008static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010009posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010010{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010011 path_t path;
10012 path_t attribute;
10013 int follow_symlinks = 1;
10014 PyObject *buffer = NULL;
10015 int i;
10016 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010017
Larry Hastings9cf065c2012-06-22 16:30:09 -070010018 memset(&path, 0, sizeof(path));
10019 memset(&attribute, 0, sizeof(attribute));
Victor Stinner292c8352012-10-30 02:17:38 +010010020 path.function_name = "getxattr";
10021 attribute.function_name = "getxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010022 path.allow_fd = 1;
10023 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords,
10024 path_converter, &path,
10025 path_converter, &attribute,
10026 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010027 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010028
Larry Hastings9cf065c2012-06-22 16:30:09 -070010029 if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks))
10030 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010031
Larry Hastings9cf065c2012-06-22 16:30:09 -070010032 for (i = 0; ; i++) {
10033 void *ptr;
10034 ssize_t result;
10035 static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
10036 Py_ssize_t buffer_size = buffer_sizes[i];
10037 if (!buffer_size) {
Victor Stinner292c8352012-10-30 02:17:38 +010010038 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010039 goto exit;
10040 }
10041 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
10042 if (!buffer)
10043 goto exit;
10044 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010045
Larry Hastings9cf065c2012-06-22 16:30:09 -070010046 Py_BEGIN_ALLOW_THREADS;
10047 if (path.fd >= 0)
10048 result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size);
10049 else if (follow_symlinks)
10050 result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size);
10051 else
10052 result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size);
10053 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010054
Larry Hastings9cf065c2012-06-22 16:30:09 -070010055 if (result < 0) {
10056 Py_DECREF(buffer);
10057 buffer = NULL;
10058 if (errno == ERANGE)
10059 continue;
Victor Stinner292c8352012-10-30 02:17:38 +010010060 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010061 goto exit;
10062 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010063
Larry Hastings9cf065c2012-06-22 16:30:09 -070010064 if (result != buffer_size) {
10065 /* Can only shrink. */
10066 _PyBytes_Resize(&buffer, result);
10067 }
10068 break;
10069 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010070
Larry Hastings9cf065c2012-06-22 16:30:09 -070010071exit:
10072 path_cleanup(&path);
10073 path_cleanup(&attribute);
10074 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010075}
10076
10077PyDoc_STRVAR(posix_setxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010078"setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\
10079Set extended attribute attribute on path to value.\n\
10080path may be either a string or an open file descriptor.\n\
10081If follow_symlinks is False, and the last element of the path is a symbolic\n\
10082 link, setxattr will modify the symbolic link itself instead of the file\n\
10083 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010084
10085static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010086posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010087{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010088 path_t path;
10089 path_t attribute;
10090 Py_buffer value;
10091 int flags = 0;
10092 int follow_symlinks = 1;
10093 int result;
10094 PyObject *return_value = NULL;
10095 static char *keywords[] = {"path", "attribute", "value",
10096 "flags", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010097
Larry Hastings9cf065c2012-06-22 16:30:09 -070010098 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010099 path.function_name = "setxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010100 path.allow_fd = 1;
10101 memset(&attribute, 0, sizeof(attribute));
10102 memset(&value, 0, sizeof(value));
10103 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr",
10104 keywords,
10105 path_converter, &path,
10106 path_converter, &attribute,
10107 &value, &flags,
10108 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010109 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010110
10111 if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks))
10112 goto exit;
10113
Benjamin Peterson799bd802011-08-31 22:15:17 -040010114 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010115 if (path.fd > -1)
10116 result = fsetxattr(path.fd, attribute.narrow,
10117 value.buf, value.len, flags);
10118 else if (follow_symlinks)
10119 result = setxattr(path.narrow, attribute.narrow,
10120 value.buf, value.len, flags);
10121 else
10122 result = lsetxattr(path.narrow, attribute.narrow,
10123 value.buf, value.len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010124 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010125
Larry Hastings9cf065c2012-06-22 16:30:09 -070010126 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +010010127 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010128 goto exit;
10129 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010130
Larry Hastings9cf065c2012-06-22 16:30:09 -070010131 return_value = Py_None;
10132 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010133
Larry Hastings9cf065c2012-06-22 16:30:09 -070010134exit:
10135 path_cleanup(&path);
10136 path_cleanup(&attribute);
10137 PyBuffer_Release(&value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010138
Larry Hastings9cf065c2012-06-22 16:30:09 -070010139 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010140}
10141
10142PyDoc_STRVAR(posix_removexattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010143"removexattr(path, attribute, *, follow_symlinks=True)\n\n\
10144Remove extended attribute attribute on path.\n\
10145path may be either a string or an open file descriptor.\n\
10146If follow_symlinks is False, and the last element of the path is a symbolic\n\
10147 link, removexattr will modify the symbolic link itself instead of the file\n\
10148 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010149
10150static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010151posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010152{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010153 path_t path;
10154 path_t attribute;
10155 int follow_symlinks = 1;
10156 int result;
10157 PyObject *return_value = NULL;
10158 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010159
Larry Hastings9cf065c2012-06-22 16:30:09 -070010160 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010161 path.function_name = "removexattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010162 memset(&attribute, 0, sizeof(attribute));
Victor Stinner292c8352012-10-30 02:17:38 +010010163 attribute.function_name = "removexattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010164 path.allow_fd = 1;
10165 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr",
10166 keywords,
10167 path_converter, &path,
10168 path_converter, &attribute,
10169 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010170 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010171
10172 if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks))
10173 goto exit;
10174
Benjamin Peterson799bd802011-08-31 22:15:17 -040010175 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010176 if (path.fd > -1)
10177 result = fremovexattr(path.fd, attribute.narrow);
10178 else if (follow_symlinks)
10179 result = removexattr(path.narrow, attribute.narrow);
10180 else
10181 result = lremovexattr(path.narrow, attribute.narrow);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010182 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010183
Larry Hastings9cf065c2012-06-22 16:30:09 -070010184 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +010010185 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010186 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010187 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010188
Larry Hastings9cf065c2012-06-22 16:30:09 -070010189 return_value = Py_None;
10190 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010191
Larry Hastings9cf065c2012-06-22 16:30:09 -070010192exit:
10193 path_cleanup(&path);
10194 path_cleanup(&attribute);
10195
10196 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010197}
10198
10199PyDoc_STRVAR(posix_listxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010200"listxattr(path='.', *, follow_symlinks=True)\n\n\
10201Return a list of extended attributes on path.\n\
10202\n\
10203path may be either None, a string, or an open file descriptor.\n\
10204if path is None, listxattr will examine the current directory.\n\
10205If follow_symlinks is False, and the last element of the path is a symbolic\n\
10206 link, listxattr will examine the symbolic link itself instead of the file\n\
10207 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010208
10209static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010210posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010211{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010212 path_t path;
10213 int follow_symlinks = 1;
10214 Py_ssize_t i;
10215 PyObject *result = NULL;
10216 char *buffer = NULL;
10217 char *name;
10218 static char *keywords[] = {"path", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010219
Larry Hastings9cf065c2012-06-22 16:30:09 -070010220 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010221 path.function_name = "listxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010222 path.allow_fd = 1;
10223 path.fd = -1;
10224 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords,
10225 path_converter, &path,
10226 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010227 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010228
Larry Hastings9cf065c2012-06-22 16:30:09 -070010229 if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks))
10230 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010231
Larry Hastings9cf065c2012-06-22 16:30:09 -070010232 name = path.narrow ? path.narrow : ".";
10233 for (i = 0; ; i++) {
10234 char *start, *trace, *end;
10235 ssize_t length;
10236 static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
10237 Py_ssize_t buffer_size = buffer_sizes[i];
10238 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020010239 /* ERANGE */
Victor Stinner292c8352012-10-30 02:17:38 +010010240 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010241 break;
10242 }
10243 buffer = PyMem_MALLOC(buffer_size);
10244 if (!buffer) {
10245 PyErr_NoMemory();
10246 break;
10247 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010248
Larry Hastings9cf065c2012-06-22 16:30:09 -070010249 Py_BEGIN_ALLOW_THREADS;
10250 if (path.fd > -1)
10251 length = flistxattr(path.fd, buffer, buffer_size);
10252 else if (follow_symlinks)
10253 length = listxattr(name, buffer, buffer_size);
10254 else
10255 length = llistxattr(name, buffer, buffer_size);
10256 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010257
Larry Hastings9cf065c2012-06-22 16:30:09 -070010258 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020010259 if (errno == ERANGE) {
10260 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050010261 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010262 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020010263 }
Victor Stinner292c8352012-10-30 02:17:38 +010010264 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010265 break;
10266 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010267
Larry Hastings9cf065c2012-06-22 16:30:09 -070010268 result = PyList_New(0);
10269 if (!result) {
10270 goto exit;
10271 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010272
Larry Hastings9cf065c2012-06-22 16:30:09 -070010273 end = buffer + length;
10274 for (trace = start = buffer; trace != end; trace++) {
10275 if (!*trace) {
10276 int error;
10277 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
10278 trace - start);
10279 if (!attribute) {
10280 Py_DECREF(result);
10281 result = NULL;
10282 goto exit;
10283 }
10284 error = PyList_Append(result, attribute);
10285 Py_DECREF(attribute);
10286 if (error) {
10287 Py_DECREF(result);
10288 result = NULL;
10289 goto exit;
10290 }
10291 start = trace + 1;
10292 }
10293 }
10294 break;
10295 }
10296exit:
10297 path_cleanup(&path);
10298 if (buffer)
10299 PyMem_FREE(buffer);
10300 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010301}
10302
Benjamin Peterson9428d532011-09-14 11:45:52 -040010303#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010304
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010305
Georg Brandl2fb477c2012-02-21 00:33:36 +010010306PyDoc_STRVAR(posix_urandom__doc__,
10307"urandom(n) -> str\n\n\
10308Return n random bytes suitable for cryptographic use.");
10309
10310static PyObject *
10311posix_urandom(PyObject *self, PyObject *args)
10312{
10313 Py_ssize_t size;
10314 PyObject *result;
10315 int ret;
10316
10317 /* Read arguments */
10318 if (!PyArg_ParseTuple(args, "n:urandom", &size))
10319 return NULL;
10320 if (size < 0)
10321 return PyErr_Format(PyExc_ValueError,
10322 "negative argument not allowed");
10323 result = PyBytes_FromStringAndSize(NULL, size);
10324 if (result == NULL)
10325 return NULL;
10326
10327 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
10328 PyBytes_GET_SIZE(result));
10329 if (ret == -1) {
10330 Py_DECREF(result);
10331 return NULL;
10332 }
10333 return result;
10334}
10335
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010336/* Terminal size querying */
10337
10338static PyTypeObject TerminalSizeType;
10339
10340PyDoc_STRVAR(TerminalSize_docstring,
10341 "A tuple of (columns, lines) for holding terminal window size");
10342
10343static PyStructSequence_Field TerminalSize_fields[] = {
10344 {"columns", "width of the terminal window in characters"},
10345 {"lines", "height of the terminal window in characters"},
10346 {NULL, NULL}
10347};
10348
10349static PyStructSequence_Desc TerminalSize_desc = {
10350 "os.terminal_size",
10351 TerminalSize_docstring,
10352 TerminalSize_fields,
10353 2,
10354};
10355
10356#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10357PyDoc_STRVAR(termsize__doc__,
10358 "Return the size of the terminal window as (columns, lines).\n" \
10359 "\n" \
10360 "The optional argument fd (default standard output) specifies\n" \
10361 "which file descriptor should be queried.\n" \
10362 "\n" \
10363 "If the file descriptor is not connected to a terminal, an OSError\n" \
10364 "is thrown.\n" \
10365 "\n" \
10366 "This function will only be defined if an implementation is\n" \
10367 "available for this system.\n" \
10368 "\n" \
10369 "shutil.get_terminal_size is the high-level function which should \n" \
10370 "normally be used, os.get_terminal_size is the low-level implementation.");
10371
10372static PyObject*
10373get_terminal_size(PyObject *self, PyObject *args)
10374{
10375 int columns, lines;
10376 PyObject *termsize;
10377
10378 int fd = fileno(stdout);
10379 /* Under some conditions stdout may not be connected and
10380 * fileno(stdout) may point to an invalid file descriptor. For example
10381 * GUI apps don't have valid standard streams by default.
10382 *
10383 * If this happens, and the optional fd argument is not present,
10384 * the ioctl below will fail returning EBADF. This is what we want.
10385 */
10386
10387 if (!PyArg_ParseTuple(args, "|i", &fd))
10388 return NULL;
10389
10390#ifdef TERMSIZE_USE_IOCTL
10391 {
10392 struct winsize w;
10393 if (ioctl(fd, TIOCGWINSZ, &w))
10394 return PyErr_SetFromErrno(PyExc_OSError);
10395 columns = w.ws_col;
10396 lines = w.ws_row;
10397 }
10398#endif /* TERMSIZE_USE_IOCTL */
10399
10400#ifdef TERMSIZE_USE_CONIO
10401 {
10402 DWORD nhandle;
10403 HANDLE handle;
10404 CONSOLE_SCREEN_BUFFER_INFO csbi;
10405 switch (fd) {
10406 case 0: nhandle = STD_INPUT_HANDLE;
10407 break;
10408 case 1: nhandle = STD_OUTPUT_HANDLE;
10409 break;
10410 case 2: nhandle = STD_ERROR_HANDLE;
10411 break;
10412 default:
10413 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10414 }
10415 handle = GetStdHandle(nhandle);
10416 if (handle == NULL)
10417 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10418 if (handle == INVALID_HANDLE_VALUE)
10419 return PyErr_SetFromWindowsErr(0);
10420
10421 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10422 return PyErr_SetFromWindowsErr(0);
10423
10424 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10425 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10426 }
10427#endif /* TERMSIZE_USE_CONIO */
10428
10429 termsize = PyStructSequence_New(&TerminalSizeType);
10430 if (termsize == NULL)
10431 return NULL;
10432 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10433 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10434 if (PyErr_Occurred()) {
10435 Py_DECREF(termsize);
10436 return NULL;
10437 }
10438 return termsize;
10439}
10440#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10441
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010442PyDoc_STRVAR(posix_cpu_count__doc__,
10443"cpu_count() -> integer\n\n\
10444Return the number of CPUs in the system, or None if this value cannot be\n\
10445established.");
10446
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010447static PyObject *
10448posix_cpu_count(PyObject *self)
10449{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010450 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010451#ifdef MS_WINDOWS
10452 SYSTEM_INFO sysinfo;
10453 GetSystemInfo(&sysinfo);
10454 ncpu = sysinfo.dwNumberOfProcessors;
10455#elif defined(__hpux)
10456 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
10457#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
10458 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010459#elif defined(__DragonFly__) || \
10460 defined(__OpenBSD__) || \
10461 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010462 defined(__NetBSD__) || \
10463 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020010464 int mib[2];
10465 size_t len = sizeof(ncpu);
10466 mib[0] = CTL_HW;
10467 mib[1] = HW_NCPU;
10468 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
10469 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010470#endif
10471 if (ncpu >= 1)
10472 return PyLong_FromLong(ncpu);
10473 else
10474 Py_RETURN_NONE;
10475}
10476
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010477
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010478static PyMethodDef posix_methods[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010479 {"access", (PyCFunction)posix_access,
10480 METH_VARARGS | METH_KEYWORDS,
10481 posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010482#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010483 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010484#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010485 {"chdir", (PyCFunction)posix_chdir,
10486 METH_VARARGS | METH_KEYWORDS,
10487 posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010488#ifdef HAVE_CHFLAGS
Larry Hastings9cf065c2012-06-22 16:30:09 -070010489 {"chflags", (PyCFunction)posix_chflags,
10490 METH_VARARGS | METH_KEYWORDS,
10491 posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010492#endif /* HAVE_CHFLAGS */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010493 {"chmod", (PyCFunction)posix_chmod,
10494 METH_VARARGS | METH_KEYWORDS,
10495 posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010496#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010498#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010499#ifdef HAVE_CHOWN
Larry Hastings9cf065c2012-06-22 16:30:09 -070010500 {"chown", (PyCFunction)posix_chown,
10501 METH_VARARGS | METH_KEYWORDS,
10502 posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010503#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010504#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010505 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010506#endif /* HAVE_LCHMOD */
10507#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010508 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010509#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010510#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010511 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010512#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010513#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010514 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010515#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010516#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010517 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010518#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010519#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010520 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010521#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010522#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010523 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10524 METH_NOARGS, posix_getcwd__doc__},
10525 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10526 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010527#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010528#if defined(HAVE_LINK) || defined(MS_WINDOWS)
10529 {"link", (PyCFunction)posix_link,
10530 METH_VARARGS | METH_KEYWORDS,
10531 posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010532#endif /* HAVE_LINK */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010533 {"listdir", (PyCFunction)posix_listdir,
10534 METH_VARARGS | METH_KEYWORDS,
10535 posix_listdir__doc__},
10536 {"lstat", (PyCFunction)posix_lstat,
10537 METH_VARARGS | METH_KEYWORDS,
10538 posix_lstat__doc__},
10539 {"mkdir", (PyCFunction)posix_mkdir,
10540 METH_VARARGS | METH_KEYWORDS,
10541 posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010542#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010543 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010544#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010545#ifdef HAVE_GETPRIORITY
10546 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10547#endif /* HAVE_GETPRIORITY */
10548#ifdef HAVE_SETPRIORITY
10549 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10550#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010551#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070010552 {"readlink", (PyCFunction)posix_readlink,
10553 METH_VARARGS | METH_KEYWORDS,
10554 readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010555#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010556#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010557 {"readlink", (PyCFunction)win_readlink,
10558 METH_VARARGS | METH_KEYWORDS,
10559 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010560#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010561 {"rename", (PyCFunction)posix_rename,
10562 METH_VARARGS | METH_KEYWORDS,
10563 posix_rename__doc__},
10564 {"replace", (PyCFunction)posix_replace,
10565 METH_VARARGS | METH_KEYWORDS,
10566 posix_replace__doc__},
Larry Hastingsb698d8e2012-06-23 16:55:07 -070010567 {"rmdir", (PyCFunction)posix_rmdir,
10568 METH_VARARGS | METH_KEYWORDS,
10569 posix_rmdir__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010570 {"stat", (PyCFunction)posix_stat,
10571 METH_VARARGS | METH_KEYWORDS,
10572 posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010574#if defined(HAVE_SYMLINK)
10575 {"symlink", (PyCFunction)posix_symlink,
10576 METH_VARARGS | METH_KEYWORDS,
10577 posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010578#endif /* HAVE_SYMLINK */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010579#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010580 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010581#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010582 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010583#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010585#endif /* HAVE_UNAME */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010586 {"unlink", (PyCFunction)posix_unlink,
10587 METH_VARARGS | METH_KEYWORDS,
10588 posix_unlink__doc__},
10589 {"remove", (PyCFunction)posix_unlink,
10590 METH_VARARGS | METH_KEYWORDS,
10591 posix_remove__doc__},
Larry Hastings76ad59b2012-05-03 00:30:07 -070010592 {"utime", (PyCFunction)posix_utime,
10593 METH_VARARGS | METH_KEYWORDS, posix_utime__doc__},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010594#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010595 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010596#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010597 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010598#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010600 {"execve", (PyCFunction)posix_execve,
10601 METH_VARARGS | METH_KEYWORDS,
10602 posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010603#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +000010604#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10606 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +000010607#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010608#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010610#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010611#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010612 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010613#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010614#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010615#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010616 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10617 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010618#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010619#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010620 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010621#endif
10622#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010623 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010624#endif
10625#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010626 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010627#endif
10628#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010629 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010630#endif
10631#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010632 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010633#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010634 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010635#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010636 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10637 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10638#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010639#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010640#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010642#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010643#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010645#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010646#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010647 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010648#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010649#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010651#endif /* HAVE_GETEUID */
10652#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010654#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010655#ifdef HAVE_GETGROUPLIST
10656 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10657#endif
Fred Drakec9680921999-12-13 16:37:25 +000010658#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010660#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010662#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010663 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010664#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010665#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010666 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010667#endif /* HAVE_GETPPID */
10668#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010669 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010670#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010671#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010672 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010673#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010674#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010675 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010676#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010677#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010679#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010680#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010682#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010683#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010684 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10685 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010686#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010687#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010688 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010689#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010690#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010691 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010692#endif /* HAVE_SETEUID */
10693#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010694 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010695#endif /* HAVE_SETEGID */
10696#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010698#endif /* HAVE_SETREUID */
10699#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010701#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010702#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010704#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010705#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010707#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010708#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010710#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010711#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010712 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010713#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010714#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010715 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010716#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010717#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010718 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010719#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010720#ifdef HAVE_WAIT3
Victor Stinner4195b5c2012-02-08 23:03:19 +010010721 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010722#endif /* HAVE_WAIT3 */
10723#ifdef HAVE_WAIT4
Victor Stinner4195b5c2012-02-08 23:03:19 +010010724 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010725#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010726#if defined(HAVE_WAITID) && !defined(__APPLE__)
10727 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10728#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010729#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010730 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010731#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010732#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010733 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010734#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010735#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010736 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010737#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010738#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010739 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010740#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010741#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010742 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010743#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010744#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010745 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010746#endif /* HAVE_TCSETPGRP */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010747 {"open", (PyCFunction)posix_open,\
10748 METH_VARARGS | METH_KEYWORDS,
10749 posix_open__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010750 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10751 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10752 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10753 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10754 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010755#ifdef HAVE_LOCKF
10756 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10757#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10759 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010760#ifdef HAVE_READV
10761 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10762#endif
10763#ifdef HAVE_PREAD
10764 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10765#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010766 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010767#ifdef HAVE_WRITEV
10768 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10769#endif
10770#ifdef HAVE_PWRITE
10771 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10772#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010773#ifdef HAVE_SENDFILE
10774 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10775 posix_sendfile__doc__},
10776#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010777 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010778 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010779#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010780 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010781#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010782#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010783 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010784#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010785#ifdef HAVE_MKFIFO
Larry Hastings9cf065c2012-06-22 16:30:09 -070010786 {"mkfifo", (PyCFunction)posix_mkfifo,
10787 METH_VARARGS | METH_KEYWORDS,
10788 posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010789#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010790#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010791 {"mknod", (PyCFunction)posix_mknod,
10792 METH_VARARGS | METH_KEYWORDS,
10793 posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010794#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010795#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10797 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10798 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010799#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010800#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010801 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010802#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010803#ifdef HAVE_TRUNCATE
Georg Brandl306336b2012-06-24 12:55:33 +020010804 {"truncate", (PyCFunction)posix_truncate,
10805 METH_VARARGS | METH_KEYWORDS,
10806 posix_truncate__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010807#endif
10808#ifdef HAVE_POSIX_FALLOCATE
10809 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10810#endif
10811#ifdef HAVE_POSIX_FADVISE
10812 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10813#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010814#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010816#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010817#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010819#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010820 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010821#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010823#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010824#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010825 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010826#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010827#ifdef HAVE_SYNC
10828 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10829#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010830#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010831 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010832#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010833#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010834#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010835 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010836#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010837#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010838 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010839#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010840#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010841 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010842#endif /* WIFSTOPPED */
10843#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010844 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010845#endif /* WIFSIGNALED */
10846#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010847 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010848#endif /* WIFEXITED */
10849#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010850 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010851#endif /* WEXITSTATUS */
10852#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010853 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010854#endif /* WTERMSIG */
10855#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010856 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010857#endif /* WSTOPSIG */
10858#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010859#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010861#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010862#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010863 {"statvfs", (PyCFunction)posix_statvfs,
10864 METH_VARARGS | METH_KEYWORDS,
10865 posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010866#endif
Fred Drakec9680921999-12-13 16:37:25 +000010867#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010869#endif
10870#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010872#endif
10873#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010874 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010875#endif
10876#ifdef HAVE_PATHCONF
Georg Brandl306336b2012-06-24 12:55:33 +020010877 {"pathconf", (PyCFunction)posix_pathconf,
10878 METH_VARARGS | METH_KEYWORDS,
10879 posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010880#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010882#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010884 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010885 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010886 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010887#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010888#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010890#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010010891 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010892#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010894#endif
10895#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010897#endif
10898#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010900#endif
10901#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010903#endif
10904
Benjamin Peterson9428d532011-09-14 11:45:52 -040010905#ifdef USE_XATTRS
Larry Hastings9cf065c2012-06-22 16:30:09 -070010906 {"setxattr", (PyCFunction)posix_setxattr,
10907 METH_VARARGS | METH_KEYWORDS,
10908 posix_setxattr__doc__},
10909 {"getxattr", (PyCFunction)posix_getxattr,
10910 METH_VARARGS | METH_KEYWORDS,
10911 posix_getxattr__doc__},
10912 {"removexattr", (PyCFunction)posix_removexattr,
10913 METH_VARARGS | METH_KEYWORDS,
10914 posix_removexattr__doc__},
10915 {"listxattr", (PyCFunction)posix_listxattr,
10916 METH_VARARGS | METH_KEYWORDS,
10917 posix_listxattr__doc__},
Benjamin Peterson799bd802011-08-31 22:15:17 -040010918#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010919#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10920 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
10921#endif
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010922 {"cpu_count", (PyCFunction)posix_cpu_count,
10923 METH_NOARGS, posix_cpu_count__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010924 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010925};
10926
10927
Brian Curtin52173d42010-12-02 18:29:18 +000010928#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010929static int
Brian Curtin52173d42010-12-02 18:29:18 +000010930enable_symlink()
10931{
10932 HANDLE tok;
10933 TOKEN_PRIVILEGES tok_priv;
10934 LUID luid;
10935 int meth_idx = 0;
10936
10937 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010938 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010939
10940 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010941 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010942
10943 tok_priv.PrivilegeCount = 1;
10944 tok_priv.Privileges[0].Luid = luid;
10945 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10946
10947 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10948 sizeof(TOKEN_PRIVILEGES),
10949 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010950 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010951
Brian Curtin3b4499c2010-12-28 14:31:47 +000010952 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10953 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010954}
10955#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10956
Barry Warsaw4a342091996-12-19 23:50:02 +000010957static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010958all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000010959{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010960#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010961 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010962#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010963#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010964 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010965#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010966#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010967 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010968#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010969#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010970 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010971#endif
Fred Drakec9680921999-12-13 16:37:25 +000010972#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010973 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010974#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010975#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010976 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010977#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010978#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010979 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010980#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010981#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010982 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010983#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010984#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010985 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010986#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010987#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010988 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010989#endif
10990#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010991 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010992#endif
10993#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010994 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010995#endif
10996#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010997 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010998#endif
10999#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011000 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011001#endif
11002#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011003 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011004#endif
11005#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011006 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011007#endif
11008#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011009 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011010#endif
11011#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011012 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011013#endif
11014#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011015 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011016#endif
11017#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011018 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011019#endif
11020#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011021 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011022#endif
11023#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011024 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011025#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011026#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011027 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011028#endif
11029#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011030 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011031#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011032#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011033 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020011034#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011035#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011036 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011037#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011038#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011039 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011040#endif
11041#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011042 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011043#endif
Jesus Ceacf381202012-04-24 20:44:40 +020011044#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011045 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020011046#endif
11047#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011048 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020011049#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050011050#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011051 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050011052#endif
Jesus Ceacf381202012-04-24 20:44:40 +020011053#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011054 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020011055#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011056#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011057 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011058#endif
11059#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011060 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011061#endif
11062#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011063 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011064#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011065#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011066 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020011067#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011068#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011069 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020011070#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011071
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011072
Jesus Cea94363612012-06-22 18:32:07 +020011073#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011074 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020011075#endif
11076#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011077 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020011078#endif
11079
Tim Peters5aa91602002-01-30 05:46:57 +000011080/* MS Windows */
11081#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011083 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011084#endif
11085#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011086 /* Optimize for short life (keep in memory). */
11087 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011088 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011089#endif
11090#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011092 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011093#endif
11094#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011095 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011096 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011097#endif
11098#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011100 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011101#endif
11102
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011103/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011104#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 /* Send a SIGIO signal whenever input or output
11106 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011107 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011108#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011109#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011110 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011111 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011112#endif
11113#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011115 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011116#endif
11117#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011119 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011120#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011121#ifdef O_NOLINKS
11122 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011123 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020011124#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011125#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011127 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011128#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011129
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011131#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011132 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011133#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011134#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011135 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011136#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011137#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011138 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011139#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011140#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011141 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011142#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011143#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011144 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011145#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011146#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011147 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011148#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011149#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011150 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011151#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011152#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011153 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011154#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011155#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011156 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011157#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011158#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011159 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011160#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011161#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011162 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011163#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011164#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011165 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011166#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011167#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011168 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011169#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011170#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011171 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011172#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011173#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011174 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011175#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011176#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011177 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011178#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011179#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011180 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011181#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011182
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011183 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011184#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011185 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011186#endif /* ST_RDONLY */
11187#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011188 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011189#endif /* ST_NOSUID */
11190
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011191 /* FreeBSD sendfile() constants */
11192#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011193 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011194#endif
11195#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011196 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011197#endif
11198#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011199 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011200#endif
11201
Ross Lagerwall7807c352011-03-17 20:20:30 +020011202 /* constants for posix_fadvise */
11203#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011204 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011205#endif
11206#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011207 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011208#endif
11209#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011210 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011211#endif
11212#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011213 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011214#endif
11215#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011216 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011217#endif
11218#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011219 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011220#endif
11221
11222 /* constants for waitid */
11223#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011224 if (PyModule_AddIntMacro(m, P_PID)) return -1;
11225 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
11226 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011227#endif
11228#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011229 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011230#endif
11231#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011232 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011233#endif
11234#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011235 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011236#endif
11237#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011238 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011239#endif
11240#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011241 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011242#endif
11243#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011244 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011245#endif
11246#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011247 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011248#endif
11249
11250 /* constants for lockf */
11251#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011252 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011253#endif
11254#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011255 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011256#endif
11257#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011258 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011259#endif
11260#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011261 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011262#endif
11263
Guido van Rossum246bc171999-02-01 23:54:31 +000011264#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011265 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
11266 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
11267 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
11268 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
11269 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011270#endif
11271
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011272#ifdef HAVE_SCHED_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011273 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
11274 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
11275 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011276#ifdef SCHED_SPORADIC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011277 if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011278#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011279#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011280 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011281#endif
11282#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011283 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011284#endif
11285#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011286 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011287#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011288#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011289 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011290#endif
11291#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011292 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011293#endif
11294#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011295 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011296#endif
11297#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011298 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011299#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011300#endif
11301
Benjamin Peterson9428d532011-09-14 11:45:52 -040011302#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011303 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
11304 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
11305 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011306#endif
11307
Victor Stinner8b905bd2011-10-25 13:34:04 +020011308#ifdef RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011309 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011310#endif
11311#ifdef RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011312 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011313#endif
11314#ifdef RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011315 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011316#endif
11317#ifdef RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011318 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011319#endif
11320#ifdef RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011321 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011322#endif
11323#ifdef RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011324 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011325#endif
11326#ifdef RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011327 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011328#endif
11329
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011331}
11332
11333
Tim Peters5aa91602002-01-30 05:46:57 +000011334#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011335#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011336#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011337
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011338#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011339#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011340#define MODNAME "posix"
11341#endif
11342
Martin v. Löwis1a214512008-06-11 05:26:20 +000011343static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011344 PyModuleDef_HEAD_INIT,
11345 MODNAME,
11346 posix__doc__,
11347 -1,
11348 posix_methods,
11349 NULL,
11350 NULL,
11351 NULL,
11352 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011353};
11354
11355
Larry Hastings9cf065c2012-06-22 16:30:09 -070011356static char *have_functions[] = {
11357
11358#ifdef HAVE_FACCESSAT
11359 "HAVE_FACCESSAT",
11360#endif
11361
11362#ifdef HAVE_FCHDIR
11363 "HAVE_FCHDIR",
11364#endif
11365
11366#ifdef HAVE_FCHMOD
11367 "HAVE_FCHMOD",
11368#endif
11369
11370#ifdef HAVE_FCHMODAT
11371 "HAVE_FCHMODAT",
11372#endif
11373
11374#ifdef HAVE_FCHOWN
11375 "HAVE_FCHOWN",
11376#endif
11377
11378#ifdef HAVE_FEXECVE
11379 "HAVE_FEXECVE",
11380#endif
11381
11382#ifdef HAVE_FDOPENDIR
11383 "HAVE_FDOPENDIR",
11384#endif
11385
Georg Brandl306336b2012-06-24 12:55:33 +020011386#ifdef HAVE_FPATHCONF
11387 "HAVE_FPATHCONF",
11388#endif
11389
Larry Hastings9cf065c2012-06-22 16:30:09 -070011390#ifdef HAVE_FSTATAT
11391 "HAVE_FSTATAT",
11392#endif
11393
11394#ifdef HAVE_FSTATVFS
11395 "HAVE_FSTATVFS",
11396#endif
11397
Georg Brandl306336b2012-06-24 12:55:33 +020011398#ifdef HAVE_FTRUNCATE
11399 "HAVE_FTRUNCATE",
11400#endif
11401
Larry Hastings9cf065c2012-06-22 16:30:09 -070011402#ifdef HAVE_FUTIMENS
11403 "HAVE_FUTIMENS",
11404#endif
11405
11406#ifdef HAVE_FUTIMES
11407 "HAVE_FUTIMES",
11408#endif
11409
11410#ifdef HAVE_FUTIMESAT
11411 "HAVE_FUTIMESAT",
11412#endif
11413
11414#ifdef HAVE_LINKAT
11415 "HAVE_LINKAT",
11416#endif
11417
11418#ifdef HAVE_LCHFLAGS
11419 "HAVE_LCHFLAGS",
11420#endif
11421
11422#ifdef HAVE_LCHMOD
11423 "HAVE_LCHMOD",
11424#endif
11425
11426#ifdef HAVE_LCHOWN
11427 "HAVE_LCHOWN",
11428#endif
11429
11430#ifdef HAVE_LSTAT
11431 "HAVE_LSTAT",
11432#endif
11433
11434#ifdef HAVE_LUTIMES
11435 "HAVE_LUTIMES",
11436#endif
11437
11438#ifdef HAVE_MKDIRAT
11439 "HAVE_MKDIRAT",
11440#endif
11441
11442#ifdef HAVE_MKFIFOAT
11443 "HAVE_MKFIFOAT",
11444#endif
11445
11446#ifdef HAVE_MKNODAT
11447 "HAVE_MKNODAT",
11448#endif
11449
11450#ifdef HAVE_OPENAT
11451 "HAVE_OPENAT",
11452#endif
11453
11454#ifdef HAVE_READLINKAT
11455 "HAVE_READLINKAT",
11456#endif
11457
11458#ifdef HAVE_RENAMEAT
11459 "HAVE_RENAMEAT",
11460#endif
11461
11462#ifdef HAVE_SYMLINKAT
11463 "HAVE_SYMLINKAT",
11464#endif
11465
11466#ifdef HAVE_UNLINKAT
11467 "HAVE_UNLINKAT",
11468#endif
11469
11470#ifdef HAVE_UTIMENSAT
11471 "HAVE_UTIMENSAT",
11472#endif
11473
11474#ifdef MS_WINDOWS
11475 "MS_WINDOWS",
11476#endif
11477
11478 NULL
11479};
11480
11481
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011482PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011483INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011484{
Victor Stinner8c62be82010-05-06 00:08:46 +000011485 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011486 PyObject *list;
11487 char **trace;
Tim Peters5aa91602002-01-30 05:46:57 +000011488
Brian Curtin52173d42010-12-02 18:29:18 +000011489#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011490 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011491#endif
11492
Victor Stinner8c62be82010-05-06 00:08:46 +000011493 m = PyModule_Create(&posixmodule);
11494 if (m == NULL)
11495 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011496
Victor Stinner8c62be82010-05-06 00:08:46 +000011497 /* Initialize environ dictionary */
11498 v = convertenviron();
11499 Py_XINCREF(v);
11500 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11501 return NULL;
11502 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011503
Victor Stinner8c62be82010-05-06 00:08:46 +000011504 if (all_ins(m))
11505 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011506
Victor Stinner8c62be82010-05-06 00:08:46 +000011507 if (setup_confname_tables(m))
11508 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011509
Victor Stinner8c62be82010-05-06 00:08:46 +000011510 Py_INCREF(PyExc_OSError);
11511 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011512
Guido van Rossumb3d39562000-01-31 18:41:26 +000011513#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011514 if (posix_putenv_garbage == NULL)
11515 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011516#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011517
Victor Stinner8c62be82010-05-06 00:08:46 +000011518 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011519#if defined(HAVE_WAITID) && !defined(__APPLE__)
11520 waitid_result_desc.name = MODNAME ".waitid_result";
11521 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11522#endif
11523
Victor Stinner8c62be82010-05-06 00:08:46 +000011524 stat_result_desc.name = MODNAME ".stat_result";
11525 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11526 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11527 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11528 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11529 structseq_new = StatResultType.tp_new;
11530 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011531
Victor Stinner8c62be82010-05-06 00:08:46 +000011532 statvfs_result_desc.name = MODNAME ".statvfs_result";
11533 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011534#ifdef NEED_TICKS_PER_SECOND
11535# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011536 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011537# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011538 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011539# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011540 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011541# endif
11542#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011543
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011544#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011545 sched_param_desc.name = MODNAME ".sched_param";
11546 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11547 SchedParamType.tp_new = sched_param_new;
11548#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011549
11550 /* initialize TerminalSize_info */
11551 PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011553#if defined(HAVE_WAITID) && !defined(__APPLE__)
11554 Py_INCREF((PyObject*) &WaitidResultType);
11555 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11556#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011557 Py_INCREF((PyObject*) &StatResultType);
11558 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11559 Py_INCREF((PyObject*) &StatVFSResultType);
11560 PyModule_AddObject(m, "statvfs_result",
11561 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011562
11563#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011564 Py_INCREF(&SchedParamType);
11565 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011566#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000011567
Larry Hastings605a62d2012-06-24 04:33:36 -070011568 times_result_desc.name = MODNAME ".times_result";
11569 PyStructSequence_InitType(&TimesResultType, &times_result_desc);
11570 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
11571
11572 uname_result_desc.name = MODNAME ".uname_result";
11573 PyStructSequence_InitType(&UnameResultType, &uname_result_desc);
11574 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
11575
Thomas Wouters477c8d52006-05-27 19:21:47 +000011576#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011577 /*
11578 * Step 2 of weak-linking support on Mac OS X.
11579 *
11580 * The code below removes functions that are not available on the
11581 * currently active platform.
11582 *
11583 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070011584 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000011585 * OSX 10.4.
11586 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011587#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011588 if (fstatvfs == NULL) {
11589 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11590 return NULL;
11591 }
11592 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011593#endif /* HAVE_FSTATVFS */
11594
11595#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011596 if (statvfs == NULL) {
11597 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11598 return NULL;
11599 }
11600 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011601#endif /* HAVE_STATVFS */
11602
11603# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011604 if (lchown == NULL) {
11605 if (PyObject_DelAttrString(m, "lchown") == -1) {
11606 return NULL;
11607 }
11608 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011609#endif /* HAVE_LCHOWN */
11610
11611
11612#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011613
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020011614 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011615 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
11616
Larry Hastings6fe20b32012-04-19 15:07:49 -070011617 billion = PyLong_FromLong(1000000000);
11618 if (!billion)
11619 return NULL;
11620
Larry Hastings9cf065c2012-06-22 16:30:09 -070011621 /* suppress "function not used" warnings */
11622 {
11623 int ignored;
11624 fd_specified("", -1);
11625 follow_symlinks_specified("", 1);
11626 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
11627 dir_fd_converter(Py_None, &ignored);
11628 dir_fd_unavailable(Py_None, &ignored);
11629 }
11630
11631 /*
11632 * provide list of locally available functions
11633 * so os.py can populate support_* lists
11634 */
11635 list = PyList_New(0);
11636 if (!list)
11637 return NULL;
11638 for (trace = have_functions; *trace; trace++) {
11639 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
11640 if (!unicode)
11641 return NULL;
11642 if (PyList_Append(list, unicode))
11643 return NULL;
11644 Py_DECREF(unicode);
11645 }
11646 PyModule_AddObject(m, "_have_functions", list);
11647
11648 initialized = 1;
11649
Victor Stinner8c62be82010-05-06 00:08:46 +000011650 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011651
Guido van Rossumb6775db1994-08-01 11:34:53 +000011652}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011653
11654#ifdef __cplusplus
11655}
11656#endif