blob: c7d48b05ddae822bbaa69964a623ba8959a0f856 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Thomas Wouters477c8d52006-05-27 19:21:47 +000014#ifdef __APPLE__
15 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000016 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
18 * at the end of this file for more information.
19 */
20# pragma weak lchown
21# pragma weak statvfs
22# pragma weak fstatvfs
23
24#endif /* __APPLE__ */
25
Thomas Wouters68bc4f92006-03-01 01:05:10 +000026#define PY_SSIZE_T_CLEAN
27
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000028#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020031# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#endif /* defined(__VMS) */
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000040"This module provides access to operating system functionality that is\n\
41standardized by the C Standard and the POSIX standard (a thinly\n\
42disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000045
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000046#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020047#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Ross Lagerwall4d076da2011-03-18 06:56:53 +020062#ifdef HAVE_SYS_UIO_H
63#include <sys/uio.h>
64#endif
65
Thomas Wouters0e3f5912006-08-11 14:57:12 +000066#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_TYPES_H */
69
70#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000073
Guido van Rossum36bc6801995-06-14 22:54:23 +000074#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000075#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000079#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000081
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#ifdef HAVE_FCNTL_H
83#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000084#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000085
Guido van Rossuma6535fd2001-10-18 19:44:10 +000086#ifdef HAVE_GRP_H
87#include <grp.h>
88#endif
89
Barry Warsaw5676bd12003-01-07 20:57:09 +000090#ifdef HAVE_SYSEXITS_H
91#include <sysexits.h>
92#endif /* HAVE_SYSEXITS_H */
93
Anthony Baxter8a560de2004-10-13 15:30:56 +000094#ifdef HAVE_SYS_LOADAVG_H
95#include <sys/loadavg.h>
96#endif
97
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000098#ifdef HAVE_LANGINFO_H
99#include <langinfo.h>
100#endif
101
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000102#ifdef HAVE_SYS_SENDFILE_H
103#include <sys/sendfile.h>
104#endif
105
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500106#ifdef HAVE_SCHED_H
107#include <sched.h>
108#endif
109
Benjamin Peterson9428d532011-09-14 11:45:52 -0400110#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
111#define USE_XATTRS
112#endif
113
114#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400115#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400116#endif
117
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
119#ifdef HAVE_SYS_SOCKET_H
120#include <sys/socket.h>
121#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#endif
123
Victor Stinner8b905bd2011-10-25 13:34:04 +0200124#ifdef HAVE_DLFCN_H
125#include <dlfcn.h>
126#endif
127
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 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000142#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000143#include <process.h>
144#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000145#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#define HAVE_GETCWD 1
147#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#if defined(__OS2__)
150#define HAVE_EXECV 1
151#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000152#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#include <process.h>
154#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_EXECV 1
157#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000158#define HAVE_OPENDIR 1
159#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000160#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#define HAVE_WAIT 1
162#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000163#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000164#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000165#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000166#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000168#define HAVE_EXECV 1
169#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000170#define HAVE_SYSTEM 1
171#define HAVE_CWAIT 1
172#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000173#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000174#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000175#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
176/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000177#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000178/* Unix functions that the configure script doesn't check for */
179#define HAVE_EXECV 1
180#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000182#define HAVE_FORK1 1
183#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000184#define HAVE_GETCWD 1
185#define HAVE_GETEGID 1
186#define HAVE_GETEUID 1
187#define HAVE_GETGID 1
188#define HAVE_GETPPID 1
189#define HAVE_GETUID 1
190#define HAVE_KILL 1
191#define HAVE_OPENDIR 1
192#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000193#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000194#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000195#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000196#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197#endif /* _MSC_VER */
198#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000199#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000200#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000201
Victor Stinnera2f7c002012-02-08 03:36:25 +0100202
203
204
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000205#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000206
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000207#if defined(__sgi)&&_COMPILER_VERSION>=700
208/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
209 (default) */
210extern char *ctermid_r(char *);
211#endif
212
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000213#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000214#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000215extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000216#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000217#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000219#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000220extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000221#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000222#endif
223#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int chdir(char *);
225extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000226#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000227extern int chdir(const char *);
228extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000229#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000230#ifdef __BORLANDC__
231extern int chmod(const char *, int);
232#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000233extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000234#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000235/*#ifdef HAVE_FCHMOD
236extern int fchmod(int, mode_t);
237#endif*/
238/*#ifdef HAVE_LCHMOD
239extern int lchmod(const char *, mode_t);
240#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000241extern int chown(const char *, uid_t, gid_t);
242extern char *getcwd(char *, int);
243extern char *strerror(int);
244extern int link(const char *, const char *);
245extern int rename(const char *, const char *);
246extern int stat(const char *, struct stat *);
247extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000249extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000250#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000252extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000253#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#ifdef HAVE_UTIME_H
259#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000260#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000262#ifdef HAVE_SYS_UTIME_H
263#include <sys/utime.h>
264#define HAVE_UTIME_H /* pretend we do for the rest of this file */
265#endif /* HAVE_SYS_UTIME_H */
266
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#ifdef HAVE_SYS_TIMES_H
268#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000269#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
271#ifdef HAVE_SYS_PARAM_H
272#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274
275#ifdef HAVE_SYS_UTSNAME_H
276#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000277#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000279#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000281#define NAMLEN(dirent) strlen((dirent)->d_name)
282#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000283#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#include <direct.h>
285#define NAMLEN(dirent) strlen((dirent)->d_name)
286#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000288#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000289#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292#endif
293#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000295#endif
296#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000298#endif
299#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000300
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000301#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000304#endif
305#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000306#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307#endif
308#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000309#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000310#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000311#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000312#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000313#endif
314#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000315#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000316#endif
317#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000318#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000319#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000320#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000321#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000322#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000323#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000324#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000325#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
326#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000327static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000328#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000329#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000330
Guido van Rossumd48f2521997-12-05 22:19:34 +0000331#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000332#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000333#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000334
Tim Petersbc2e10e2002-03-03 23:17:02 +0000335#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336#if defined(PATH_MAX) && PATH_MAX > 1024
337#define MAXPATHLEN PATH_MAX
338#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000339#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000341#endif /* MAXPATHLEN */
342
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000343#ifdef UNION_WAIT
344/* Emulate some macros on systems that have a union instead of macros */
345
346#ifndef WIFEXITED
347#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
348#endif
349
350#ifndef WEXITSTATUS
351#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
352#endif
353
354#ifndef WTERMSIG
355#define WTERMSIG(u_wait) ((u_wait).w_termsig)
356#endif
357
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358#define WAIT_TYPE union wait
359#define WAIT_STATUS_INT(s) (s.w_status)
360
361#else /* !UNION_WAIT */
362#define WAIT_TYPE int
363#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000364#endif /* UNION_WAIT */
365
Greg Wardb48bc172000-03-01 21:51:56 +0000366/* Don't use the "_r" form if we don't need it (also, won't have a
367 prototype for it, at least on Solaris -- maybe others as well?). */
368#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
369#define USE_CTERMID_R
370#endif
371
Fred Drake699f3522000-06-29 21:12:41 +0000372/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000373#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000374#undef FSTAT
375#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000376#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000377# define STAT win32_stat
378# define FSTAT win32_fstat
379# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000380#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000381# define STAT stat
382# define FSTAT fstat
383# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000384#endif
385
Tim Peters11b23062003-04-23 02:39:17 +0000386#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000387#include <sys/mkdev.h>
388#else
389#if defined(MAJOR_IN_SYSMACROS)
390#include <sys/sysmacros.h>
391#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000392#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
393#include <sys/mkdev.h>
394#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000395#endif
Fred Drake699f3522000-06-29 21:12:41 +0000396
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200397/* A helper used by a number of POSIX-only functions */
398#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000399static int
400_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000401{
402#if !defined(HAVE_LARGEFILE_SUPPORT)
403 *((off_t*)addr) = PyLong_AsLong(arg);
404#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000405 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000406#endif
407 if (PyErr_Occurred())
408 return 0;
409 return 1;
410}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200411#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000412
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000413#if defined _MSC_VER && _MSC_VER >= 1400
414/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
415 * valid and throw an assertion if it isn't.
416 * Normally, an invalid fd is likely to be a C program error and therefore
417 * an assertion can be useful, but it does contradict the POSIX standard
418 * which for write(2) states:
419 * "Otherwise, -1 shall be returned and errno set to indicate the error."
420 * "[EBADF] The fildes argument is not a valid file descriptor open for
421 * writing."
422 * Furthermore, python allows the user to enter any old integer
423 * as a fd and should merely raise a python exception on error.
424 * The Microsoft CRT doesn't provide an official way to check for the
425 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000426 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000427 * internal structures involved.
428 * The structures below must be updated for each version of visual studio
429 * according to the file internal.h in the CRT source, until MS comes
430 * up with a less hacky way to do this.
431 * (all of this is to avoid globally modifying the CRT behaviour using
432 * _set_invalid_parameter_handler() and _CrtSetReportMode())
433 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000434/* The actual size of the structure is determined at runtime.
435 * Only the first items must be present.
436 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000437typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000438 intptr_t osfhnd;
439 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000440} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000441
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000442extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000443#define IOINFO_L2E 5
444#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
445#define IOINFO_ARRAYS 64
446#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
447#define FOPEN 0x01
448#define _NO_CONSOLE_FILENO (intptr_t)-2
449
450/* This function emulates what the windows CRT does to validate file handles */
451int
452_PyVerify_fd(int fd)
453{
Victor Stinner8c62be82010-05-06 00:08:46 +0000454 const int i1 = fd >> IOINFO_L2E;
455 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000456
Antoine Pitrou22e41552010-08-15 18:07:50 +0000457 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000458
Victor Stinner8c62be82010-05-06 00:08:46 +0000459 /* Determine the actual size of the ioinfo structure,
460 * as used by the CRT loaded in memory
461 */
462 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
463 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
464 }
465 if (sizeof_ioinfo == 0) {
466 /* This should not happen... */
467 goto fail;
468 }
469
470 /* See that it isn't a special CLEAR fileno */
471 if (fd != _NO_CONSOLE_FILENO) {
472 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
473 * we check pointer validity and other info
474 */
475 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
476 /* finally, check that the file is open */
477 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
478 if (info->osfile & FOPEN) {
479 return 1;
480 }
481 }
482 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000483 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000484 errno = EBADF;
485 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000486}
487
488/* the special case of checking dup2. The target fd must be in a sensible range */
489static int
490_PyVerify_fd_dup2(int fd1, int fd2)
491{
Victor Stinner8c62be82010-05-06 00:08:46 +0000492 if (!_PyVerify_fd(fd1))
493 return 0;
494 if (fd2 == _NO_CONSOLE_FILENO)
495 return 0;
496 if ((unsigned)fd2 < _NHANDLE_)
497 return 1;
498 else
499 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000500}
501#else
502/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
503#define _PyVerify_fd_dup2(A, B) (1)
504#endif
505
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000506#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000507/* The following structure was copied from
508 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
509 include doesn't seem to be present in the Windows SDK (at least as included
510 with Visual Studio Express). */
511typedef struct _REPARSE_DATA_BUFFER {
512 ULONG ReparseTag;
513 USHORT ReparseDataLength;
514 USHORT Reserved;
515 union {
516 struct {
517 USHORT SubstituteNameOffset;
518 USHORT SubstituteNameLength;
519 USHORT PrintNameOffset;
520 USHORT PrintNameLength;
521 ULONG Flags;
522 WCHAR PathBuffer[1];
523 } SymbolicLinkReparseBuffer;
524
525 struct {
526 USHORT SubstituteNameOffset;
527 USHORT SubstituteNameLength;
528 USHORT PrintNameOffset;
529 USHORT PrintNameLength;
530 WCHAR PathBuffer[1];
531 } MountPointReparseBuffer;
532
533 struct {
534 UCHAR DataBuffer[1];
535 } GenericReparseBuffer;
536 };
537} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
538
539#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
540 GenericReparseBuffer)
541#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
542
543static int
Brian Curtind25aef52011-06-13 15:16:04 -0500544win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000545{
546 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
547 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
548 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000549
550 if (0 == DeviceIoControl(
551 reparse_point_handle,
552 FSCTL_GET_REPARSE_POINT,
553 NULL, 0, /* in buffer */
554 target_buffer, sizeof(target_buffer),
555 &n_bytes_returned,
556 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500557 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000558
559 if (reparse_tag)
560 *reparse_tag = rdb->ReparseTag;
561
Brian Curtind25aef52011-06-13 15:16:04 -0500562 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000563}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100564
565static int
566win32_warn_bytes_api()
567{
568 return PyErr_WarnEx(PyExc_DeprecationWarning,
569 "The Windows bytes API has been deprecated, "
570 "use Unicode filenames instead",
571 1);
572}
573
574static PyObject*
575win32_decode_filename(PyObject *obj)
576{
577 PyObject *unicode;
578 if (PyUnicode_Check(obj)) {
579 if (PyUnicode_READY(obj))
580 return NULL;
581 Py_INCREF(obj);
582 return obj;
583 }
584 if (!PyUnicode_FSDecoder(obj, &unicode))
585 return NULL;
586 if (win32_warn_bytes_api()) {
587 Py_DECREF(unicode);
588 return NULL;
589 }
590 return unicode;
591}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000592#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000593
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000595#ifdef WITH_NEXT_FRAMEWORK
596/* On Darwin/MacOSX a shared library or framework has no access to
597** environ directly, we must obtain it with _NSGetEnviron().
598*/
599#include <crt_externs.h>
600static char **environ;
601#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000603#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604
Barry Warsaw53699e91996-12-10 23:23:01 +0000605static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000606convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607{
Victor Stinner8c62be82010-05-06 00:08:46 +0000608 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000609#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000610 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000611#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000612 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000613#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000614#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000615 APIRET rc;
616 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
617#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000618
Victor Stinner8c62be82010-05-06 00:08:46 +0000619 d = PyDict_New();
620 if (d == NULL)
621 return NULL;
622#ifdef WITH_NEXT_FRAMEWORK
623 if (environ == NULL)
624 environ = *_NSGetEnviron();
625#endif
626#ifdef MS_WINDOWS
627 /* _wenviron must be initialized in this way if the program is started
628 through main() instead of wmain(). */
629 _wgetenv(L"");
630 if (_wenviron == NULL)
631 return d;
632 /* This part ignores errors */
633 for (e = _wenviron; *e != NULL; e++) {
634 PyObject *k;
635 PyObject *v;
636 wchar_t *p = wcschr(*e, L'=');
637 if (p == NULL)
638 continue;
639 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
640 if (k == NULL) {
641 PyErr_Clear();
642 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000643 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000644 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
645 if (v == NULL) {
646 PyErr_Clear();
647 Py_DECREF(k);
648 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000649 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000650 if (PyDict_GetItem(d, k) == NULL) {
651 if (PyDict_SetItem(d, k, v) != 0)
652 PyErr_Clear();
653 }
654 Py_DECREF(k);
655 Py_DECREF(v);
656 }
657#else
658 if (environ == NULL)
659 return d;
660 /* This part ignores errors */
661 for (e = environ; *e != NULL; e++) {
662 PyObject *k;
663 PyObject *v;
664 char *p = strchr(*e, '=');
665 if (p == NULL)
666 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000667 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 if (k == NULL) {
669 PyErr_Clear();
670 continue;
671 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000672 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000673 if (v == NULL) {
674 PyErr_Clear();
675 Py_DECREF(k);
676 continue;
677 }
678 if (PyDict_GetItem(d, k) == NULL) {
679 if (PyDict_SetItem(d, k, v) != 0)
680 PyErr_Clear();
681 }
682 Py_DECREF(k);
683 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000684 }
685#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000686#if defined(PYOS_OS2)
687 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
688 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
689 PyObject *v = PyBytes_FromString(buffer);
690 PyDict_SetItemString(d, "BEGINLIBPATH", v);
691 Py_DECREF(v);
692 }
693 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
694 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
695 PyObject *v = PyBytes_FromString(buffer);
696 PyDict_SetItemString(d, "ENDLIBPATH", v);
697 Py_DECREF(v);
698 }
699#endif
700 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701}
702
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703/* Set a POSIX-specific error from errno, and return NULL */
704
Barry Warsawd58d7641998-07-23 16:14:40 +0000705static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000706posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000707{
Victor Stinner8c62be82010-05-06 00:08:46 +0000708 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709}
Barry Warsawd58d7641998-07-23 16:14:40 +0000710static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000711posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000712{
Victor Stinner8c62be82010-05-06 00:08:46 +0000713 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000714}
715
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716
Mark Hammondef8b6542001-05-13 08:04:26 +0000717static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000718posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000719{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000720 PyObject *name_str, *rc;
721 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
722 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000723 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000724 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
725 name_str);
726 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000727 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000728}
729
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000730#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000731static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000732win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000733{
Victor Stinner8c62be82010-05-06 00:08:46 +0000734 /* XXX We should pass the function name along in the future.
735 (winreg.c also wants to pass the function name.)
736 This would however require an additional param to the
737 Windows error object, which is non-trivial.
738 */
739 errno = GetLastError();
740 if (filename)
741 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
742 else
743 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000744}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000745
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000746static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +0200747win32_error_unicode(char* function, wchar_t* filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000748{
Victor Stinner8c62be82010-05-06 00:08:46 +0000749 /* XXX - see win32_error for comments on 'function' */
750 errno = GetLastError();
751 if (filename)
752 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
753 else
754 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000755}
756
Victor Stinnereb5657a2011-09-30 01:44:27 +0200757static PyObject *
758win32_error_object(char* function, PyObject* filename)
759{
760 /* XXX - see win32_error for comments on 'function' */
761 errno = GetLastError();
762 if (filename)
763 return PyErr_SetExcFromWindowsErrWithFilenameObject(
764 PyExc_WindowsError,
765 errno,
766 filename);
767 else
768 return PyErr_SetFromWindowsErr(errno);
769}
770
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000771#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772
Guido van Rossumd48f2521997-12-05 22:19:34 +0000773#if defined(PYOS_OS2)
774/**********************************************************************
775 * Helper Function to Trim and Format OS/2 Messages
776 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000777static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000778os2_formatmsg(char *msgbuf, int msglen, char *reason)
779{
780 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
781
782 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
783 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
784
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000785 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000786 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
787 }
788
789 /* Add Optional Reason Text */
790 if (reason) {
791 strcat(msgbuf, " : ");
792 strcat(msgbuf, reason);
793 }
794}
795
796/**********************************************************************
797 * Decode an OS/2 Operating System Error Code
798 *
799 * A convenience function to lookup an OS/2 error code and return a
800 * text message we can use to raise a Python exception.
801 *
802 * Notes:
803 * The messages for errors returned from the OS/2 kernel reside in
804 * the file OSO001.MSG in the \OS2 directory hierarchy.
805 *
806 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000807static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000808os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
809{
810 APIRET rc;
811 ULONG msglen;
812
813 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
814 Py_BEGIN_ALLOW_THREADS
815 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
816 errorcode, "oso001.msg", &msglen);
817 Py_END_ALLOW_THREADS
818
819 if (rc == NO_ERROR)
820 os2_formatmsg(msgbuf, msglen, reason);
821 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000822 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000823 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000824
825 return msgbuf;
826}
827
828/* Set an OS/2-specific error and return NULL. OS/2 kernel
829 errors are not in a global variable e.g. 'errno' nor are
830 they congruent with posix error numbers. */
831
Victor Stinner8c62be82010-05-06 00:08:46 +0000832static PyObject *
833os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000834{
835 char text[1024];
836 PyObject *v;
837
838 os2_strerror(text, sizeof(text), code, "");
839
840 v = Py_BuildValue("(is)", code, text);
841 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000842 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000843 Py_DECREF(v);
844 }
845 return NULL; /* Signal to Python that an Exception is Pending */
846}
847
848#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000849
850/* POSIX generic methods */
851
Barry Warsaw53699e91996-12-10 23:23:01 +0000852static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000853posix_fildes(PyObject *fdobj, int (*func)(int))
854{
Victor Stinner8c62be82010-05-06 00:08:46 +0000855 int fd;
856 int res;
857 fd = PyObject_AsFileDescriptor(fdobj);
858 if (fd < 0)
859 return NULL;
860 if (!_PyVerify_fd(fd))
861 return posix_error();
862 Py_BEGIN_ALLOW_THREADS
863 res = (*func)(fd);
864 Py_END_ALLOW_THREADS
865 if (res < 0)
866 return posix_error();
867 Py_INCREF(Py_None);
868 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000869}
Guido van Rossum21142a01999-01-08 21:05:37 +0000870
871static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000872posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873{
Victor Stinner8c62be82010-05-06 00:08:46 +0000874 PyObject *opath1 = NULL;
875 char *path1;
876 int res;
877 if (!PyArg_ParseTuple(args, format,
878 PyUnicode_FSConverter, &opath1))
879 return NULL;
880 path1 = PyBytes_AsString(opath1);
881 Py_BEGIN_ALLOW_THREADS
882 res = (*func)(path1);
883 Py_END_ALLOW_THREADS
884 if (res < 0)
885 return posix_error_with_allocated_filename(opath1);
886 Py_DECREF(opath1);
887 Py_INCREF(Py_None);
888 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889}
890
Barry Warsaw53699e91996-12-10 23:23:01 +0000891static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000892posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000893 char *format,
894 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895{
Victor Stinner8c62be82010-05-06 00:08:46 +0000896 PyObject *opath1 = NULL, *opath2 = NULL;
897 char *path1, *path2;
898 int res;
899 if (!PyArg_ParseTuple(args, format,
900 PyUnicode_FSConverter, &opath1,
901 PyUnicode_FSConverter, &opath2)) {
902 return NULL;
903 }
904 path1 = PyBytes_AsString(opath1);
905 path2 = PyBytes_AsString(opath2);
906 Py_BEGIN_ALLOW_THREADS
907 res = (*func)(path1, path2);
908 Py_END_ALLOW_THREADS
909 Py_DECREF(opath1);
910 Py_DECREF(opath2);
911 if (res != 0)
912 /* XXX how to report both path1 and path2??? */
913 return posix_error();
914 Py_INCREF(Py_None);
915 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000916}
917
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000918#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000919static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000920win32_1str(PyObject* args, char* func,
921 char* format, BOOL (__stdcall *funcA)(LPCSTR),
922 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923{
Victor Stinner8c62be82010-05-06 00:08:46 +0000924 PyObject *uni;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100925 const char *ansi;
Victor Stinner8c62be82010-05-06 00:08:46 +0000926 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000927
Victor Stinnereb5657a2011-09-30 01:44:27 +0200928 if (PyArg_ParseTuple(args, wformat, &uni))
929 {
930 wchar_t *wstr = PyUnicode_AsUnicode(uni);
931 if (wstr == NULL)
932 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +0200934 result = funcW(wstr);
Victor Stinner8c62be82010-05-06 00:08:46 +0000935 Py_END_ALLOW_THREADS
936 if (!result)
Victor Stinnereb5657a2011-09-30 01:44:27 +0200937 return win32_error_object(func, uni);
Victor Stinner8c62be82010-05-06 00:08:46 +0000938 Py_INCREF(Py_None);
939 return Py_None;
940 }
Victor Stinnereb5657a2011-09-30 01:44:27 +0200941 PyErr_Clear();
942
Victor Stinner8c62be82010-05-06 00:08:46 +0000943 if (!PyArg_ParseTuple(args, format, &ansi))
944 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100945 if (win32_warn_bytes_api())
946 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +0000947 Py_BEGIN_ALLOW_THREADS
948 result = funcA(ansi);
949 Py_END_ALLOW_THREADS
950 if (!result)
951 return win32_error(func, ansi);
952 Py_INCREF(Py_None);
953 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000954
955}
956
957/* This is a reimplementation of the C library's chdir function,
958 but one that produces Win32 errors instead of DOS error codes.
959 chdir is essentially a wrapper around SetCurrentDirectory; however,
960 it also needs to set "magic" environment variables indicating
961 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000962static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000963win32_chdir(LPCSTR path)
964{
Victor Stinner8c62be82010-05-06 00:08:46 +0000965 char new_path[MAX_PATH+1];
966 int result;
967 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000968
Victor Stinner8c62be82010-05-06 00:08:46 +0000969 if(!SetCurrentDirectoryA(path))
970 return FALSE;
971 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
972 if (!result)
973 return FALSE;
974 /* In the ANSI API, there should not be any paths longer
975 than MAX_PATH. */
976 assert(result <= MAX_PATH+1);
977 if (strncmp(new_path, "\\\\", 2) == 0 ||
978 strncmp(new_path, "//", 2) == 0)
979 /* UNC path, nothing to do. */
980 return TRUE;
981 env[1] = new_path[0];
982 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000983}
984
985/* The Unicode version differs from the ANSI version
986 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000987static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000988win32_wchdir(LPCWSTR path)
989{
Victor Stinner8c62be82010-05-06 00:08:46 +0000990 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
991 int result;
992 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000993
Victor Stinner8c62be82010-05-06 00:08:46 +0000994 if(!SetCurrentDirectoryW(path))
995 return FALSE;
996 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
997 if (!result)
998 return FALSE;
999 if (result > MAX_PATH+1) {
1000 new_path = malloc(result * sizeof(wchar_t));
1001 if (!new_path) {
1002 SetLastError(ERROR_OUTOFMEMORY);
1003 return FALSE;
1004 }
1005 result = GetCurrentDirectoryW(result, new_path);
1006 if (!result) {
1007 free(new_path);
1008 return FALSE;
1009 }
1010 }
1011 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1012 wcsncmp(new_path, L"//", 2) == 0)
1013 /* UNC path, nothing to do. */
1014 return TRUE;
1015 env[1] = new_path[0];
1016 result = SetEnvironmentVariableW(env, new_path);
1017 if (new_path != _new_path)
1018 free(new_path);
1019 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001020}
1021#endif
1022
Martin v. Löwis14694662006-02-03 12:54:16 +00001023#ifdef MS_WINDOWS
1024/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1025 - time stamps are restricted to second resolution
1026 - file modification times suffer from forth-and-back conversions between
1027 UTC and local time
1028 Therefore, we implement our own stat, based on the Win32 API directly.
1029*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001030#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001031
1032struct win32_stat{
1033 int st_dev;
1034 __int64 st_ino;
1035 unsigned short st_mode;
1036 int st_nlink;
1037 int st_uid;
1038 int st_gid;
1039 int st_rdev;
1040 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001041 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001042 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001043 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001044 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001045 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001046 int st_ctime_nsec;
1047};
1048
1049static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1050
1051static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001052FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001053{
Victor Stinner8c62be82010-05-06 00:08:46 +00001054 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1055 /* Cannot simply cast and dereference in_ptr,
1056 since it might not be aligned properly */
1057 __int64 in;
1058 memcpy(&in, in_ptr, sizeof(in));
1059 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001060 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001061}
1062
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001064time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065{
Victor Stinner8c62be82010-05-06 00:08:46 +00001066 /* XXX endianness */
1067 __int64 out;
1068 out = time_in + secs_between_epochs;
1069 out = out * 10000000 + nsec_in / 100;
1070 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071}
1072
Martin v. Löwis14694662006-02-03 12:54:16 +00001073/* Below, we *know* that ugo+r is 0444 */
1074#if _S_IREAD != 0400
1075#error Unsupported C library
1076#endif
1077static int
1078attributes_to_mode(DWORD attr)
1079{
Victor Stinner8c62be82010-05-06 00:08:46 +00001080 int m = 0;
1081 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1082 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1083 else
1084 m |= _S_IFREG;
1085 if (attr & FILE_ATTRIBUTE_READONLY)
1086 m |= 0444;
1087 else
1088 m |= 0666;
1089 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001090}
1091
1092static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001093attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001094{
Victor Stinner8c62be82010-05-06 00:08:46 +00001095 memset(result, 0, sizeof(*result));
1096 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1097 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1098 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1099 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1100 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001101 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001102 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001103 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1104 /* first clear the S_IFMT bits */
1105 result->st_mode ^= (result->st_mode & 0170000);
1106 /* now set the bits that make this a symlink */
1107 result->st_mode |= 0120000;
1108 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001109
Victor Stinner8c62be82010-05-06 00:08:46 +00001110 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001111}
1112
Guido van Rossumd8faa362007-04-27 19:54:29 +00001113static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001114attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001115{
Victor Stinner8c62be82010-05-06 00:08:46 +00001116 HANDLE hFindFile;
1117 WIN32_FIND_DATAA FileData;
1118 hFindFile = FindFirstFileA(pszFile, &FileData);
1119 if (hFindFile == INVALID_HANDLE_VALUE)
1120 return FALSE;
1121 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001122 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001123 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001124 info->dwFileAttributes = FileData.dwFileAttributes;
1125 info->ftCreationTime = FileData.ftCreationTime;
1126 info->ftLastAccessTime = FileData.ftLastAccessTime;
1127 info->ftLastWriteTime = FileData.ftLastWriteTime;
1128 info->nFileSizeHigh = FileData.nFileSizeHigh;
1129 info->nFileSizeLow = FileData.nFileSizeLow;
1130/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1132 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001133 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001134}
1135
1136static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001137attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001138{
Victor Stinner8c62be82010-05-06 00:08:46 +00001139 HANDLE hFindFile;
1140 WIN32_FIND_DATAW FileData;
1141 hFindFile = FindFirstFileW(pszFile, &FileData);
1142 if (hFindFile == INVALID_HANDLE_VALUE)
1143 return FALSE;
1144 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001145 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001146 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001147 info->dwFileAttributes = FileData.dwFileAttributes;
1148 info->ftCreationTime = FileData.ftCreationTime;
1149 info->ftLastAccessTime = FileData.ftLastAccessTime;
1150 info->ftLastWriteTime = FileData.ftLastWriteTime;
1151 info->nFileSizeHigh = FileData.nFileSizeHigh;
1152 info->nFileSizeLow = FileData.nFileSizeLow;
1153/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001154 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1155 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001156 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001157}
1158
Brian Curtind25aef52011-06-13 15:16:04 -05001159/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1160static int has_GetFinalPathNameByHandle = 0;
Brian Curtind25aef52011-06-13 15:16:04 -05001161static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1162 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001163static int
Brian Curtind25aef52011-06-13 15:16:04 -05001164check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001165{
Brian Curtind25aef52011-06-13 15:16:04 -05001166 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001167 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1168 DWORD);
1169
Brian Curtind25aef52011-06-13 15:16:04 -05001170 /* only recheck */
1171 if (!has_GetFinalPathNameByHandle)
1172 {
Martin v. Löwis50590f12012-01-14 17:54:09 +01001173 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtind25aef52011-06-13 15:16:04 -05001174 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1175 "GetFinalPathNameByHandleA");
1176 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1177 "GetFinalPathNameByHandleW");
1178 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1179 Py_GetFinalPathNameByHandleW;
1180 }
1181 return has_GetFinalPathNameByHandle;
1182}
1183
1184static BOOL
1185get_target_path(HANDLE hdl, wchar_t **target_path)
1186{
1187 int buf_size, result_length;
1188 wchar_t *buf;
1189
1190 /* We have a good handle to the target, use it to determine
1191 the target path name (then we'll call lstat on it). */
1192 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1193 VOLUME_NAME_DOS);
1194 if(!buf_size)
1195 return FALSE;
1196
1197 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001198 if (!buf) {
1199 SetLastError(ERROR_OUTOFMEMORY);
1200 return FALSE;
1201 }
1202
Brian Curtind25aef52011-06-13 15:16:04 -05001203 result_length = Py_GetFinalPathNameByHandleW(hdl,
1204 buf, buf_size, VOLUME_NAME_DOS);
1205
1206 if(!result_length) {
1207 free(buf);
1208 return FALSE;
1209 }
1210
1211 if(!CloseHandle(hdl)) {
1212 free(buf);
1213 return FALSE;
1214 }
1215
1216 buf[result_length] = 0;
1217
1218 *target_path = buf;
1219 return TRUE;
1220}
1221
1222static int
1223win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1224 BOOL traverse);
1225static int
1226win32_xstat_impl(const char *path, struct win32_stat *result,
1227 BOOL traverse)
1228{
Victor Stinner26de69d2011-06-17 15:15:38 +02001229 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001230 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001231 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001232 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001233 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001234 const char *dot;
1235
Brian Curtind25aef52011-06-13 15:16:04 -05001236 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001237 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1238 traverse reparse point. */
1239 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001240 }
1241
Brian Curtinf5e76d02010-11-24 13:14:05 +00001242 hFile = CreateFileA(
1243 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001244 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001245 0, /* share mode */
1246 NULL, /* security attributes */
1247 OPEN_EXISTING,
1248 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001249 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1250 Because of this, calls like GetFinalPathNameByHandle will return
1251 the symlink path agin and not the actual final path. */
1252 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1253 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001254 NULL);
1255
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001256 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001257 /* Either the target doesn't exist, or we don't have access to
1258 get a handle to it. If the former, we need to return an error.
1259 If the latter, we can use attributes_from_dir. */
1260 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001261 return -1;
1262 /* Could not get attributes on open file. Fall back to
1263 reading the directory. */
1264 if (!attributes_from_dir(path, &info, &reparse_tag))
1265 /* Very strange. This should not fail now */
1266 return -1;
1267 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1268 if (traverse) {
1269 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001270 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001271 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001272 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001273 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001274 } else {
1275 if (!GetFileInformationByHandle(hFile, &info)) {
1276 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001277 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001278 }
1279 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001280 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1281 return -1;
1282
1283 /* Close the outer open file handle now that we're about to
1284 reopen it with different flags. */
1285 if (!CloseHandle(hFile))
1286 return -1;
1287
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001288 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001289 /* In order to call GetFinalPathNameByHandle we need to open
1290 the file without the reparse handling flag set. */
1291 hFile2 = CreateFileA(
1292 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1293 NULL, OPEN_EXISTING,
1294 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1295 NULL);
1296 if (hFile2 == INVALID_HANDLE_VALUE)
1297 return -1;
1298
1299 if (!get_target_path(hFile2, &target_path))
1300 return -1;
1301
1302 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001303 free(target_path);
1304 return code;
1305 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001306 } else
1307 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001309 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001310
1311 /* Set S_IEXEC if it is an .exe, .bat, ... */
1312 dot = strrchr(path, '.');
1313 if (dot) {
1314 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1315 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1316 result->st_mode |= 0111;
1317 }
1318 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001319}
1320
1321static int
Brian Curtind25aef52011-06-13 15:16:04 -05001322win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1323 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001324{
1325 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001326 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001327 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001328 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001329 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001330 const wchar_t *dot;
1331
Brian Curtind25aef52011-06-13 15:16:04 -05001332 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001333 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1334 traverse reparse point. */
1335 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001336 }
1337
Brian Curtinf5e76d02010-11-24 13:14:05 +00001338 hFile = CreateFileW(
1339 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001340 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001341 0, /* share mode */
1342 NULL, /* security attributes */
1343 OPEN_EXISTING,
1344 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001345 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1346 Because of this, calls like GetFinalPathNameByHandle will return
1347 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001348 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001349 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001350 NULL);
1351
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001352 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001353 /* Either the target doesn't exist, or we don't have access to
1354 get a handle to it. If the former, we need to return an error.
1355 If the latter, we can use attributes_from_dir. */
1356 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001357 return -1;
1358 /* Could not get attributes on open file. Fall back to
1359 reading the directory. */
1360 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1361 /* Very strange. This should not fail now */
1362 return -1;
1363 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1364 if (traverse) {
1365 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001366 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001367 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001368 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001369 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001370 } else {
1371 if (!GetFileInformationByHandle(hFile, &info)) {
1372 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001373 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001374 }
1375 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001376 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1377 return -1;
1378
1379 /* Close the outer open file handle now that we're about to
1380 reopen it with different flags. */
1381 if (!CloseHandle(hFile))
1382 return -1;
1383
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001384 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001385 /* In order to call GetFinalPathNameByHandle we need to open
1386 the file without the reparse handling flag set. */
1387 hFile2 = CreateFileW(
1388 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1389 NULL, OPEN_EXISTING,
1390 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1391 NULL);
1392 if (hFile2 == INVALID_HANDLE_VALUE)
1393 return -1;
1394
1395 if (!get_target_path(hFile2, &target_path))
1396 return -1;
1397
1398 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001399 free(target_path);
1400 return code;
1401 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001402 } else
1403 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001405 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001406
1407 /* Set S_IEXEC if it is an .exe, .bat, ... */
1408 dot = wcsrchr(path, '.');
1409 if (dot) {
1410 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1411 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1412 result->st_mode |= 0111;
1413 }
1414 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001415}
1416
1417static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001418win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001419{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001420 /* Protocol violation: we explicitly clear errno, instead of
1421 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001422 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001423 errno = 0;
1424 return code;
1425}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001426
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001427static int
1428win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1429{
1430 /* Protocol violation: we explicitly clear errno, instead of
1431 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001432 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001433 errno = 0;
1434 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001435}
Brian Curtind25aef52011-06-13 15:16:04 -05001436/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001437
1438 In Posix, stat automatically traverses symlinks and returns the stat
1439 structure for the target. In Windows, the equivalent GetFileAttributes by
1440 default does not traverse symlinks and instead returns attributes for
1441 the symlink.
1442
1443 Therefore, win32_lstat will get the attributes traditionally, and
1444 win32_stat will first explicitly resolve the symlink target and then will
1445 call win32_lstat on that result.
1446
Ezio Melotti4969f702011-03-15 05:59:46 +02001447 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001448
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001449static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001450win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001451{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001452 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001453}
1454
Victor Stinner8c62be82010-05-06 00:08:46 +00001455static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001456win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001457{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001458 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001459}
1460
1461static int
1462win32_stat(const char* path, struct win32_stat *result)
1463{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001464 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001465}
1466
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001467static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001468win32_stat_w(const wchar_t* path, struct win32_stat *result)
1469{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001470 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001471}
1472
1473static int
1474win32_fstat(int file_number, struct win32_stat *result)
1475{
Victor Stinner8c62be82010-05-06 00:08:46 +00001476 BY_HANDLE_FILE_INFORMATION info;
1477 HANDLE h;
1478 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001479
Victor Stinner8c62be82010-05-06 00:08:46 +00001480 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001481
Victor Stinner8c62be82010-05-06 00:08:46 +00001482 /* Protocol violation: we explicitly clear errno, instead of
1483 setting it to a POSIX error. Callers should use GetLastError. */
1484 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001485
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 if (h == INVALID_HANDLE_VALUE) {
1487 /* This is really a C library error (invalid file handle).
1488 We set the Win32 error to the closes one matching. */
1489 SetLastError(ERROR_INVALID_HANDLE);
1490 return -1;
1491 }
1492 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001493
Victor Stinner8c62be82010-05-06 00:08:46 +00001494 type = GetFileType(h);
1495 if (type == FILE_TYPE_UNKNOWN) {
1496 DWORD error = GetLastError();
1497 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001498 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001499 }
1500 /* else: valid but unknown file */
1501 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001502
Victor Stinner8c62be82010-05-06 00:08:46 +00001503 if (type != FILE_TYPE_DISK) {
1504 if (type == FILE_TYPE_CHAR)
1505 result->st_mode = _S_IFCHR;
1506 else if (type == FILE_TYPE_PIPE)
1507 result->st_mode = _S_IFIFO;
1508 return 0;
1509 }
1510
1511 if (!GetFileInformationByHandle(h, &info)) {
1512 return -1;
1513 }
1514
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001515 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001516 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001517 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1518 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001519}
1520
1521#endif /* MS_WINDOWS */
1522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001523PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001524"stat_result: Result from stat or lstat.\n\n\
1525This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001526 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001527or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1528\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001529Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1530or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001531\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001533
1534static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 {"st_mode", "protection bits"},
1536 {"st_ino", "inode"},
1537 {"st_dev", "device"},
1538 {"st_nlink", "number of hard links"},
1539 {"st_uid", "user ID of owner"},
1540 {"st_gid", "group ID of owner"},
1541 {"st_size", "total size, in bytes"},
1542 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1543 {NULL, "integer time of last access"},
1544 {NULL, "integer time of last modification"},
1545 {NULL, "integer time of last change"},
1546 {"st_atime", "time of last access"},
1547 {"st_mtime", "time of last modification"},
1548 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001549#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001550 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001551#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001552#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001553 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001554#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001555#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001557#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001558#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001559 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001560#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001561#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001562 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001563#endif
1564#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001565 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001566#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001567 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001568};
1569
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001570#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001571#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001572#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001573#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001574#endif
1575
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001576#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001577#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1578#else
1579#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1580#endif
1581
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001582#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001583#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1584#else
1585#define ST_RDEV_IDX ST_BLOCKS_IDX
1586#endif
1587
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001588#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1589#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1590#else
1591#define ST_FLAGS_IDX ST_RDEV_IDX
1592#endif
1593
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001594#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001595#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001596#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001597#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001598#endif
1599
1600#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1601#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1602#else
1603#define ST_BIRTHTIME_IDX ST_GEN_IDX
1604#endif
1605
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001606static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001607 "stat_result", /* name */
1608 stat_result__doc__, /* doc */
1609 stat_result_fields,
1610 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001611};
1612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001614"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1615This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001616 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001617or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001618\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001619See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001620
1621static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 {"f_bsize", },
1623 {"f_frsize", },
1624 {"f_blocks", },
1625 {"f_bfree", },
1626 {"f_bavail", },
1627 {"f_files", },
1628 {"f_ffree", },
1629 {"f_favail", },
1630 {"f_flag", },
1631 {"f_namemax",},
1632 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001633};
1634
1635static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001636 "statvfs_result", /* name */
1637 statvfs_result__doc__, /* doc */
1638 statvfs_result_fields,
1639 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001640};
1641
Ross Lagerwall7807c352011-03-17 20:20:30 +02001642#if defined(HAVE_WAITID) && !defined(__APPLE__)
1643PyDoc_STRVAR(waitid_result__doc__,
1644"waitid_result: Result from waitid.\n\n\
1645This object may be accessed either as a tuple of\n\
1646 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1647or via the attributes si_pid, si_uid, and so on.\n\
1648\n\
1649See os.waitid for more information.");
1650
1651static PyStructSequence_Field waitid_result_fields[] = {
1652 {"si_pid", },
1653 {"si_uid", },
1654 {"si_signo", },
1655 {"si_status", },
1656 {"si_code", },
1657 {0}
1658};
1659
1660static PyStructSequence_Desc waitid_result_desc = {
1661 "waitid_result", /* name */
1662 waitid_result__doc__, /* doc */
1663 waitid_result_fields,
1664 5
1665};
1666static PyTypeObject WaitidResultType;
1667#endif
1668
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001669static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001670static PyTypeObject StatResultType;
1671static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001672#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001673static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001674#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001675static newfunc structseq_new;
1676
1677static PyObject *
1678statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1679{
Victor Stinner8c62be82010-05-06 00:08:46 +00001680 PyStructSequence *result;
1681 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001682
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 result = (PyStructSequence*)structseq_new(type, args, kwds);
1684 if (!result)
1685 return NULL;
1686 /* If we have been initialized from a tuple,
1687 st_?time might be set to None. Initialize it
1688 from the int slots. */
1689 for (i = 7; i <= 9; i++) {
1690 if (result->ob_item[i+3] == Py_None) {
1691 Py_DECREF(Py_None);
1692 Py_INCREF(result->ob_item[i]);
1693 result->ob_item[i+3] = result->ob_item[i];
1694 }
1695 }
1696 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001697}
1698
1699
1700
1701/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001702static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001703
1704PyDoc_STRVAR(stat_float_times__doc__,
1705"stat_float_times([newval]) -> oldval\n\n\
1706Determine whether os.[lf]stat represents time stamps as float objects.\n\
1707If newval is True, future calls to stat() return floats, if it is False,\n\
1708future calls return ints. \n\
1709If newval is omitted, return the current setting.\n");
1710
1711static PyObject*
1712stat_float_times(PyObject* self, PyObject *args)
1713{
Victor Stinner8c62be82010-05-06 00:08:46 +00001714 int newval = -1;
1715 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1716 return NULL;
1717 if (newval == -1)
1718 /* Return old value */
1719 return PyBool_FromLong(_stat_float_times);
1720 _stat_float_times = newval;
1721 Py_INCREF(Py_None);
1722 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001723}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001724
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001725static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001726fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001727{
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001729#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001731#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001733#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001734 if (!ival)
1735 return;
Victor Stinner4195b5c2012-02-08 23:03:19 +01001736 if (_stat_float_times) {
1737 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1738 } else {
Victor Stinner8c62be82010-05-06 00:08:46 +00001739 fval = ival;
1740 Py_INCREF(fval);
1741 }
1742 PyStructSequence_SET_ITEM(v, index, ival);
1743 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001744}
1745
Tim Peters5aa91602002-01-30 05:46:57 +00001746/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001747 (used by posix_stat() and posix_fstat()) */
1748static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01001749_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001750{
Victor Stinner8c62be82010-05-06 00:08:46 +00001751 unsigned long ansec, mnsec, cnsec;
1752 PyObject *v = PyStructSequence_New(&StatResultType);
1753 if (v == NULL)
1754 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001755
Victor Stinner8c62be82010-05-06 00:08:46 +00001756 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001757#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 PyStructSequence_SET_ITEM(v, 1,
1759 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001760#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001761 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001762#endif
1763#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001764 PyStructSequence_SET_ITEM(v, 2,
1765 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001766#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001767 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001768#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1770 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1771 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001772#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001773 PyStructSequence_SET_ITEM(v, 6,
1774 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001775#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001777#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001778
Martin v. Löwis14694662006-02-03 12:54:16 +00001779#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 ansec = st->st_atim.tv_nsec;
1781 mnsec = st->st_mtim.tv_nsec;
1782 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001783#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 ansec = st->st_atimespec.tv_nsec;
1785 mnsec = st->st_mtimespec.tv_nsec;
1786 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001787#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 ansec = st->st_atime_nsec;
1789 mnsec = st->st_mtime_nsec;
1790 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001791#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001793#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01001794 fill_time(v, 7, st->st_atime, ansec);
1795 fill_time(v, 8, st->st_mtime, mnsec);
1796 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001797
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001798#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1800 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001801#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001802#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001803 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1804 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001805#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001806#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001807 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1808 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001809#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001810#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001811 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1812 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001813#endif
1814#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001815 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01001816 PyObject *val;
1817 unsigned long bsec,bnsec;
1818 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001819#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01001820 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001821#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01001822 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001823#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01001824 if (_stat_float_times) {
1825 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1826 } else {
1827 val = PyLong_FromLong((long)bsec);
1828 }
1829 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1830 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001832#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001833#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001834 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1835 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001836#endif
Fred Drake699f3522000-06-29 21:12:41 +00001837
Victor Stinner8c62be82010-05-06 00:08:46 +00001838 if (PyErr_Occurred()) {
1839 Py_DECREF(v);
1840 return NULL;
1841 }
Fred Drake699f3522000-06-29 21:12:41 +00001842
Victor Stinner8c62be82010-05-06 00:08:46 +00001843 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001844}
1845
Barry Warsaw53699e91996-12-10 23:23:01 +00001846static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01001847posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001848 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001849#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001851#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001852 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001853#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001854 char *wformat,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001855 int (*wstatfunc)(const wchar_t *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856{
Victor Stinner8c62be82010-05-06 00:08:46 +00001857 STRUCT_STAT st;
1858 PyObject *opath;
1859 char *path;
1860 int res;
1861 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001862
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001863#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001864 PyObject *po;
Victor Stinner4195b5c2012-02-08 23:03:19 +01001865 if (PyArg_ParseTuple(args, wformat, &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001866 wchar_t *wpath = PyUnicode_AsUnicode(po);
1867 if (wpath == NULL)
1868 return NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00001869
Victor Stinner8c62be82010-05-06 00:08:46 +00001870 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 res = wstatfunc(wpath, &st);
1872 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001873
Victor Stinner8c62be82010-05-06 00:08:46 +00001874 if (res != 0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001875 return win32_error_object("stat", po);
Victor Stinner4195b5c2012-02-08 23:03:19 +01001876 return _pystat_fromstructstat(&st);
Victor Stinner8c62be82010-05-06 00:08:46 +00001877 }
1878 /* Drop the argument parsing error as narrow strings
1879 are also valid. */
1880 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001881#endif
1882
Victor Stinner4195b5c2012-02-08 23:03:19 +01001883 if (!PyArg_ParseTuple(args, format,
1884 PyUnicode_FSConverter, &opath))
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001886#ifdef MS_WINDOWS
1887 if (win32_warn_bytes_api()) {
1888 Py_DECREF(opath);
1889 return NULL;
1890 }
1891#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001892 path = PyBytes_AsString(opath);
1893 Py_BEGIN_ALLOW_THREADS
1894 res = (*statfunc)(path, &st);
1895 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001896
Victor Stinner8c62be82010-05-06 00:08:46 +00001897 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001898#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001899 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001900#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001901 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001902#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001903 }
1904 else
Victor Stinner4195b5c2012-02-08 23:03:19 +01001905 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001906
Victor Stinner8c62be82010-05-06 00:08:46 +00001907 Py_DECREF(opath);
1908 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001909}
1910
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001911/* POSIX methods */
1912
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001913PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001914"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001915Use the real uid/gid to test for access to a path. Note that most\n\
1916operations will use the effective uid/gid, therefore this routine can\n\
1917be used in a suid/sgid environment to test if the invoking user has the\n\
1918specified access to the path. The mode argument can be F_OK to test\n\
1919existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001920
1921static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001922posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001923{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001924 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00001925 int mode;
1926
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001927#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001928 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02001929 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00001930 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02001931 wchar_t* wpath = PyUnicode_AsUnicode(po);
1932 if (wpath == NULL)
1933 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001935 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00001936 Py_END_ALLOW_THREADS
1937 goto finish;
1938 }
1939 /* Drop the argument parsing error as narrow strings
1940 are also valid. */
1941 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001942 if (!PyArg_ParseTuple(args, "yi:access", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00001943 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001944 if (win32_warn_bytes_api())
1945 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001946 Py_BEGIN_ALLOW_THREADS
1947 attr = GetFileAttributesA(path);
1948 Py_END_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001949finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001950 if (attr == 0xFFFFFFFF)
1951 /* File does not exist, or cannot read attributes */
1952 return PyBool_FromLong(0);
1953 /* Access is possible if either write access wasn't requested, or
1954 the file isn't read-only, or if it's a directory, as there are
1955 no read-only directories on Windows. */
1956 return PyBool_FromLong(!(mode & 2)
1957 || !(attr & FILE_ATTRIBUTE_READONLY)
1958 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001959#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001960 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00001961 int res;
1962 if (!PyArg_ParseTuple(args, "O&i:access",
1963 PyUnicode_FSConverter, &opath, &mode))
1964 return NULL;
1965 path = PyBytes_AsString(opath);
1966 Py_BEGIN_ALLOW_THREADS
1967 res = access(path, mode);
1968 Py_END_ALLOW_THREADS
1969 Py_DECREF(opath);
1970 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001971#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001972}
1973
Guido van Rossumd371ff11999-01-25 16:12:23 +00001974#ifndef F_OK
1975#define F_OK 0
1976#endif
1977#ifndef R_OK
1978#define R_OK 4
1979#endif
1980#ifndef W_OK
1981#define W_OK 2
1982#endif
1983#ifndef X_OK
1984#define X_OK 1
1985#endif
1986
1987#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001988PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001989"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001990Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001991
1992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001993posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001994{
Victor Stinner8c62be82010-05-06 00:08:46 +00001995 int id;
1996 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001997
Victor Stinner8c62be82010-05-06 00:08:46 +00001998 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1999 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002000
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002001#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002002 /* file descriptor 0 only, the default input device (stdin) */
2003 if (id == 0) {
2004 ret = ttyname();
2005 }
2006 else {
2007 ret = NULL;
2008 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002009#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002010 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002011#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002012 if (ret == NULL)
2013 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002014 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002015}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002016#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002017
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002018#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002019PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002020"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002021Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002022
2023static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002024posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002025{
Victor Stinner8c62be82010-05-06 00:08:46 +00002026 char *ret;
2027 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002028
Greg Wardb48bc172000-03-01 21:51:56 +00002029#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002030 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002031#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002032 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002033#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 if (ret == NULL)
2035 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002036 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002037}
2038#endif
2039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002040PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002041"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002042Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002043
Barry Warsaw53699e91996-12-10 23:23:01 +00002044static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002045posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002046{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002047#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002048 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002049#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002050 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00002051#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002053#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002054 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002055#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002056}
2057
Fred Drake4d1e64b2002-04-15 19:40:07 +00002058#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002059PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002060"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002061Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002063
2064static PyObject *
2065posix_fchdir(PyObject *self, PyObject *fdobj)
2066{
Victor Stinner8c62be82010-05-06 00:08:46 +00002067 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002068}
2069#endif /* HAVE_FCHDIR */
2070
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002073"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002075
Barry Warsaw53699e91996-12-10 23:23:01 +00002076static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002077posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078{
Victor Stinner8c62be82010-05-06 00:08:46 +00002079 PyObject *opath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002080 const char *path = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002081 int i;
2082 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002083#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002084 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002085 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00002086 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002087 wchar_t *wpath = PyUnicode_AsUnicode(po);
2088 if (wpath == NULL)
2089 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002091 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002092 if (attr != 0xFFFFFFFF) {
2093 if (i & _S_IWRITE)
2094 attr &= ~FILE_ATTRIBUTE_READONLY;
2095 else
2096 attr |= FILE_ATTRIBUTE_READONLY;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002097 res = SetFileAttributesW(wpath, attr);
Victor Stinner8c62be82010-05-06 00:08:46 +00002098 }
2099 else
2100 res = 0;
2101 Py_END_ALLOW_THREADS
2102 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02002103 return win32_error_object("chmod", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002104 Py_INCREF(Py_None);
2105 return Py_None;
2106 }
2107 /* Drop the argument parsing error as narrow strings
2108 are also valid. */
2109 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002110
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002111 if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i))
Victor Stinner8c62be82010-05-06 00:08:46 +00002112 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002113 if (win32_warn_bytes_api())
2114 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002115 Py_BEGIN_ALLOW_THREADS
2116 attr = GetFileAttributesA(path);
2117 if (attr != 0xFFFFFFFF) {
2118 if (i & _S_IWRITE)
2119 attr &= ~FILE_ATTRIBUTE_READONLY;
2120 else
2121 attr |= FILE_ATTRIBUTE_READONLY;
2122 res = SetFileAttributesA(path, attr);
2123 }
2124 else
2125 res = 0;
2126 Py_END_ALLOW_THREADS
2127 if (!res) {
2128 win32_error("chmod", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002129 return NULL;
2130 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002131 Py_INCREF(Py_None);
2132 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002133#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002134 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2135 &opath, &i))
2136 return NULL;
2137 path = PyBytes_AsString(opath);
2138 Py_BEGIN_ALLOW_THREADS
2139 res = chmod(path, i);
2140 Py_END_ALLOW_THREADS
2141 if (res < 0)
2142 return posix_error_with_allocated_filename(opath);
2143 Py_DECREF(opath);
2144 Py_INCREF(Py_None);
2145 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002146#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002147}
2148
Christian Heimes4e30a842007-11-30 22:12:06 +00002149#ifdef HAVE_FCHMOD
2150PyDoc_STRVAR(posix_fchmod__doc__,
2151"fchmod(fd, mode)\n\n\
2152Change the access permissions of the file given by file\n\
2153descriptor fd.");
2154
2155static PyObject *
2156posix_fchmod(PyObject *self, PyObject *args)
2157{
Victor Stinner8c62be82010-05-06 00:08:46 +00002158 int fd, mode, res;
2159 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2160 return NULL;
2161 Py_BEGIN_ALLOW_THREADS
2162 res = fchmod(fd, mode);
2163 Py_END_ALLOW_THREADS
2164 if (res < 0)
2165 return posix_error();
2166 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002167}
2168#endif /* HAVE_FCHMOD */
2169
2170#ifdef HAVE_LCHMOD
2171PyDoc_STRVAR(posix_lchmod__doc__,
2172"lchmod(path, mode)\n\n\
2173Change the access permissions of a file. If path is a symlink, this\n\
2174affects the link itself rather than the target.");
2175
2176static PyObject *
2177posix_lchmod(PyObject *self, PyObject *args)
2178{
Victor Stinner8c62be82010-05-06 00:08:46 +00002179 PyObject *opath;
2180 char *path;
2181 int i;
2182 int res;
2183 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2184 &opath, &i))
2185 return NULL;
2186 path = PyBytes_AsString(opath);
2187 Py_BEGIN_ALLOW_THREADS
2188 res = lchmod(path, i);
2189 Py_END_ALLOW_THREADS
2190 if (res < 0)
2191 return posix_error_with_allocated_filename(opath);
2192 Py_DECREF(opath);
2193 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002194}
2195#endif /* HAVE_LCHMOD */
2196
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002197
Thomas Wouterscf297e42007-02-23 15:07:44 +00002198#ifdef HAVE_CHFLAGS
2199PyDoc_STRVAR(posix_chflags__doc__,
2200"chflags(path, flags)\n\n\
2201Set file flags.");
2202
2203static PyObject *
2204posix_chflags(PyObject *self, PyObject *args)
2205{
Victor Stinner8c62be82010-05-06 00:08:46 +00002206 PyObject *opath;
2207 char *path;
2208 unsigned long flags;
2209 int res;
2210 if (!PyArg_ParseTuple(args, "O&k:chflags",
2211 PyUnicode_FSConverter, &opath, &flags))
2212 return NULL;
2213 path = PyBytes_AsString(opath);
2214 Py_BEGIN_ALLOW_THREADS
2215 res = chflags(path, flags);
2216 Py_END_ALLOW_THREADS
2217 if (res < 0)
2218 return posix_error_with_allocated_filename(opath);
2219 Py_DECREF(opath);
2220 Py_INCREF(Py_None);
2221 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002222}
2223#endif /* HAVE_CHFLAGS */
2224
2225#ifdef HAVE_LCHFLAGS
2226PyDoc_STRVAR(posix_lchflags__doc__,
2227"lchflags(path, flags)\n\n\
2228Set file flags.\n\
2229This function will not follow symbolic links.");
2230
2231static PyObject *
2232posix_lchflags(PyObject *self, PyObject *args)
2233{
Victor Stinner8c62be82010-05-06 00:08:46 +00002234 PyObject *opath;
2235 char *path;
2236 unsigned long flags;
2237 int res;
2238 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2239 PyUnicode_FSConverter, &opath, &flags))
2240 return NULL;
2241 path = PyBytes_AsString(opath);
2242 Py_BEGIN_ALLOW_THREADS
2243 res = lchflags(path, flags);
2244 Py_END_ALLOW_THREADS
2245 if (res < 0)
2246 return posix_error_with_allocated_filename(opath);
2247 Py_DECREF(opath);
2248 Py_INCREF(Py_None);
2249 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002250}
2251#endif /* HAVE_LCHFLAGS */
2252
Martin v. Löwis244edc82001-10-04 22:44:26 +00002253#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002254PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002255"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002256Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002257
2258static PyObject *
2259posix_chroot(PyObject *self, PyObject *args)
2260{
Victor Stinner8c62be82010-05-06 00:08:46 +00002261 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002262}
2263#endif
2264
Guido van Rossum21142a01999-01-08 21:05:37 +00002265#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002266PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002267"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002268force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002269
2270static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002271posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002272{
Stefan Krah0e803b32010-11-26 16:16:47 +00002273 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002274}
2275#endif /* HAVE_FSYNC */
2276
Ross Lagerwall7807c352011-03-17 20:20:30 +02002277#ifdef HAVE_SYNC
2278PyDoc_STRVAR(posix_sync__doc__,
2279"sync()\n\n\
2280Force write of everything to disk.");
2281
2282static PyObject *
2283posix_sync(PyObject *self, PyObject *noargs)
2284{
2285 Py_BEGIN_ALLOW_THREADS
2286 sync();
2287 Py_END_ALLOW_THREADS
2288 Py_RETURN_NONE;
2289}
2290#endif
2291
Guido van Rossum21142a01999-01-08 21:05:37 +00002292#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002293
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002294#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002295extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2296#endif
2297
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002298PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002299"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002300force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002301 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002302
2303static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002304posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002305{
Stefan Krah0e803b32010-11-26 16:16:47 +00002306 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002307}
2308#endif /* HAVE_FDATASYNC */
2309
2310
Fredrik Lundh10723342000-07-10 16:38:09 +00002311#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002312PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002313"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002314Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002315
Barry Warsaw53699e91996-12-10 23:23:01 +00002316static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002317posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002318{
Victor Stinner8c62be82010-05-06 00:08:46 +00002319 PyObject *opath;
2320 char *path;
2321 long uid, gid;
2322 int res;
2323 if (!PyArg_ParseTuple(args, "O&ll:chown",
2324 PyUnicode_FSConverter, &opath,
2325 &uid, &gid))
2326 return NULL;
2327 path = PyBytes_AsString(opath);
2328 Py_BEGIN_ALLOW_THREADS
2329 res = chown(path, (uid_t) uid, (gid_t) gid);
2330 Py_END_ALLOW_THREADS
2331 if (res < 0)
2332 return posix_error_with_allocated_filename(opath);
2333 Py_DECREF(opath);
2334 Py_INCREF(Py_None);
2335 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002336}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002337#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002338
Christian Heimes4e30a842007-11-30 22:12:06 +00002339#ifdef HAVE_FCHOWN
2340PyDoc_STRVAR(posix_fchown__doc__,
2341"fchown(fd, uid, gid)\n\n\
2342Change the owner and group id of the file given by file descriptor\n\
2343fd to the numeric uid and gid.");
2344
2345static PyObject *
2346posix_fchown(PyObject *self, PyObject *args)
2347{
Victor Stinner8c62be82010-05-06 00:08:46 +00002348 int fd;
2349 long uid, gid;
2350 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002351 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002352 return NULL;
2353 Py_BEGIN_ALLOW_THREADS
2354 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2355 Py_END_ALLOW_THREADS
2356 if (res < 0)
2357 return posix_error();
2358 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002359}
2360#endif /* HAVE_FCHOWN */
2361
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002362#ifdef HAVE_LCHOWN
2363PyDoc_STRVAR(posix_lchown__doc__,
2364"lchown(path, uid, gid)\n\n\
2365Change the owner and group id of path to the numeric uid and gid.\n\
2366This function will not follow symbolic links.");
2367
2368static PyObject *
2369posix_lchown(PyObject *self, PyObject *args)
2370{
Victor Stinner8c62be82010-05-06 00:08:46 +00002371 PyObject *opath;
2372 char *path;
2373 long uid, gid;
2374 int res;
2375 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2376 PyUnicode_FSConverter, &opath,
2377 &uid, &gid))
2378 return NULL;
2379 path = PyBytes_AsString(opath);
2380 Py_BEGIN_ALLOW_THREADS
2381 res = lchown(path, (uid_t) uid, (gid_t) gid);
2382 Py_END_ALLOW_THREADS
2383 if (res < 0)
2384 return posix_error_with_allocated_filename(opath);
2385 Py_DECREF(opath);
2386 Py_INCREF(Py_None);
2387 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002388}
2389#endif /* HAVE_LCHOWN */
2390
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002391
Guido van Rossum36bc6801995-06-14 22:54:23 +00002392#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002393static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002394posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002395{
Victor Stinner8c62be82010-05-06 00:08:46 +00002396 char buf[1026];
2397 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002398
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002399#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002400 if (!use_bytes) {
2401 wchar_t wbuf[1026];
2402 wchar_t *wbuf2 = wbuf;
2403 PyObject *resobj;
2404 DWORD len;
2405 Py_BEGIN_ALLOW_THREADS
2406 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2407 /* If the buffer is large enough, len does not include the
2408 terminating \0. If the buffer is too small, len includes
2409 the space needed for the terminator. */
2410 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2411 wbuf2 = malloc(len * sizeof(wchar_t));
2412 if (wbuf2)
2413 len = GetCurrentDirectoryW(len, wbuf2);
2414 }
2415 Py_END_ALLOW_THREADS
2416 if (!wbuf2) {
2417 PyErr_NoMemory();
2418 return NULL;
2419 }
2420 if (!len) {
2421 if (wbuf2 != wbuf) free(wbuf2);
2422 return win32_error("getcwdu", NULL);
2423 }
2424 resobj = PyUnicode_FromWideChar(wbuf2, len);
2425 if (wbuf2 != wbuf) free(wbuf2);
2426 return resobj;
2427 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01002428
2429 if (win32_warn_bytes_api())
2430 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002431#endif
2432
Victor Stinner8c62be82010-05-06 00:08:46 +00002433 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002434#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002435 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002436#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002437 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002438#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002439 Py_END_ALLOW_THREADS
2440 if (res == NULL)
2441 return posix_error();
2442 if (use_bytes)
2443 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002444 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002445}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002446
2447PyDoc_STRVAR(posix_getcwd__doc__,
2448"getcwd() -> path\n\n\
2449Return a unicode string representing the current working directory.");
2450
2451static PyObject *
2452posix_getcwd_unicode(PyObject *self)
2453{
2454 return posix_getcwd(0);
2455}
2456
2457PyDoc_STRVAR(posix_getcwdb__doc__,
2458"getcwdb() -> path\n\n\
2459Return a bytes string representing the current working directory.");
2460
2461static PyObject *
2462posix_getcwd_bytes(PyObject *self)
2463{
2464 return posix_getcwd(1);
2465}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002466#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002468
Guido van Rossumb6775db1994-08-01 11:34:53 +00002469#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002470PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002471"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002472Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002473
Barry Warsaw53699e91996-12-10 23:23:01 +00002474static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002475posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002476{
Victor Stinner8c62be82010-05-06 00:08:46 +00002477 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002478}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002479#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002480
Brian Curtin1b9df392010-11-24 20:24:31 +00002481#ifdef MS_WINDOWS
2482PyDoc_STRVAR(win32_link__doc__,
2483"link(src, dst)\n\n\
2484Create a hard link to a file.");
2485
2486static PyObject *
2487win32_link(PyObject *self, PyObject *args)
2488{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002489 PyObject *src, *dst;
2490 BOOL ok;
Brian Curtin1b9df392010-11-24 20:24:31 +00002491
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002492 if (PyArg_ParseTuple(args, "UU:link", &src, &dst))
Victor Stinnereb5657a2011-09-30 01:44:27 +02002493 {
2494 wchar_t *wsrc, *wdst;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002495
2496 wsrc = PyUnicode_AsUnicode(src);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002497 if (wsrc == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002498 goto error;
2499 wdst = PyUnicode_AsUnicode(dst);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002500 if (wdst == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002501 goto error;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002502
Brian Curtinfc889c42010-11-28 23:59:46 +00002503 Py_BEGIN_ALLOW_THREADS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002504 ok = CreateHardLinkW(wdst, wsrc, NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002505 Py_END_ALLOW_THREADS
2506
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002507 if (!ok)
Brian Curtinfc889c42010-11-28 23:59:46 +00002508 return win32_error("link", NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002509 Py_RETURN_NONE;
2510 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002511 else {
2512 PyErr_Clear();
2513 if (!PyArg_ParseTuple(args, "O&O&:link",
2514 PyUnicode_FSConverter, &src,
2515 PyUnicode_FSConverter, &dst))
2516 return NULL;
Brian Curtinfc889c42010-11-28 23:59:46 +00002517
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002518 if (win32_warn_bytes_api())
2519 goto error;
Brian Curtinfc889c42010-11-28 23:59:46 +00002520
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002521 Py_BEGIN_ALLOW_THREADS
2522 ok = CreateHardLinkA(PyBytes_AS_STRING(dst),
2523 PyBytes_AS_STRING(src),
2524 NULL);
2525 Py_END_ALLOW_THREADS
2526
2527 Py_XDECREF(src);
2528 Py_XDECREF(dst);
2529
2530 if (!ok)
2531 return win32_error("link", NULL);
2532 Py_RETURN_NONE;
2533
2534 error:
2535 Py_XDECREF(src);
2536 Py_XDECREF(dst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002537 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002538 }
Brian Curtin1b9df392010-11-24 20:24:31 +00002539}
2540#endif /* MS_WINDOWS */
2541
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002543PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002544"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002545Return a list containing the names of the entries in the directory.\n\
2546\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002547 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002548\n\
2549The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002550entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002551
Barry Warsaw53699e91996-12-10 23:23:01 +00002552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002553posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002554{
Victor Stinner8c62be82010-05-06 00:08:46 +00002555 /* XXX Should redo this putting the (now four) versions of opendir
2556 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002557#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002558
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 PyObject *d, *v;
2560 HANDLE hFindFile;
2561 BOOL result;
2562 WIN32_FIND_DATA FileData;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002563 const char *path;
2564 Py_ssize_t pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002565 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2566 char *bufptr = namebuf;
2567 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002568
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002569 PyObject *po = NULL;
2570 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002571 WIN32_FIND_DATAW wFileData;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002572 wchar_t *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002573
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002574 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002575 po_wchars = L".";
2576 len = 1;
2577 } else {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02002578 po_wchars = PyUnicode_AsUnicodeAndSize(po, &len);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002579 if (po_wchars == NULL)
2580 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002581 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002582 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2584 if (!wnamebuf) {
2585 PyErr_NoMemory();
2586 return NULL;
2587 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002588 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002589 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002590 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00002591 if (wch != L'/' && wch != L'\\' && wch != L':')
2592 wnamebuf[len++] = L'\\';
2593 wcscpy(wnamebuf + len, L"*.*");
2594 }
2595 if ((d = PyList_New(0)) == NULL) {
2596 free(wnamebuf);
2597 return NULL;
2598 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002599 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002600 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002601 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002602 if (hFindFile == INVALID_HANDLE_VALUE) {
2603 int error = GetLastError();
2604 if (error == ERROR_FILE_NOT_FOUND) {
2605 free(wnamebuf);
2606 return d;
2607 }
2608 Py_DECREF(d);
2609 win32_error_unicode("FindFirstFileW", wnamebuf);
2610 free(wnamebuf);
2611 return NULL;
2612 }
2613 do {
2614 /* Skip over . and .. */
2615 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2616 wcscmp(wFileData.cFileName, L"..") != 0) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002617 v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00002618 if (v == NULL) {
2619 Py_DECREF(d);
2620 d = NULL;
2621 break;
2622 }
2623 if (PyList_Append(d, v) != 0) {
2624 Py_DECREF(v);
2625 Py_DECREF(d);
2626 d = NULL;
2627 break;
2628 }
2629 Py_DECREF(v);
2630 }
2631 Py_BEGIN_ALLOW_THREADS
2632 result = FindNextFileW(hFindFile, &wFileData);
2633 Py_END_ALLOW_THREADS
2634 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2635 it got to the end of the directory. */
2636 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2637 Py_DECREF(d);
2638 win32_error_unicode("FindNextFileW", wnamebuf);
2639 FindClose(hFindFile);
2640 free(wnamebuf);
2641 return NULL;
2642 }
2643 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002644
Victor Stinner8c62be82010-05-06 00:08:46 +00002645 if (FindClose(hFindFile) == FALSE) {
2646 Py_DECREF(d);
2647 win32_error_unicode("FindClose", wnamebuf);
2648 free(wnamebuf);
2649 return NULL;
2650 }
2651 free(wnamebuf);
2652 return d;
2653 }
2654 /* Drop the argument parsing error as narrow strings
2655 are also valid. */
2656 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002657
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002658 if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen))
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002660 if (win32_warn_bytes_api())
2661 return NULL;
2662 if (pathlen+1 > MAX_PATH) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 PyErr_SetString(PyExc_ValueError, "path too long");
Victor Stinner8c62be82010-05-06 00:08:46 +00002664 return NULL;
2665 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002666 strcpy(namebuf, path);
2667 len = pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002668 if (len > 0) {
2669 char ch = namebuf[len-1];
2670 if (ch != SEP && ch != ALTSEP && ch != ':')
2671 namebuf[len++] = '/';
2672 strcpy(namebuf + len, "*.*");
2673 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002674
Victor Stinner8c62be82010-05-06 00:08:46 +00002675 if ((d = PyList_New(0)) == NULL)
2676 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002677
Antoine Pitroub73caab2010-08-09 23:39:31 +00002678 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002679 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002680 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002681 if (hFindFile == INVALID_HANDLE_VALUE) {
2682 int error = GetLastError();
2683 if (error == ERROR_FILE_NOT_FOUND)
2684 return d;
2685 Py_DECREF(d);
2686 return win32_error("FindFirstFile", namebuf);
2687 }
2688 do {
2689 /* Skip over . and .. */
2690 if (strcmp(FileData.cFileName, ".") != 0 &&
2691 strcmp(FileData.cFileName, "..") != 0) {
2692 v = PyBytes_FromString(FileData.cFileName);
2693 if (v == NULL) {
2694 Py_DECREF(d);
2695 d = NULL;
2696 break;
2697 }
2698 if (PyList_Append(d, v) != 0) {
2699 Py_DECREF(v);
2700 Py_DECREF(d);
2701 d = NULL;
2702 break;
2703 }
2704 Py_DECREF(v);
2705 }
2706 Py_BEGIN_ALLOW_THREADS
2707 result = FindNextFile(hFindFile, &FileData);
2708 Py_END_ALLOW_THREADS
2709 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2710 it got to the end of the directory. */
2711 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2712 Py_DECREF(d);
2713 win32_error("FindNextFile", namebuf);
2714 FindClose(hFindFile);
2715 return NULL;
2716 }
2717 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002718
Victor Stinner8c62be82010-05-06 00:08:46 +00002719 if (FindClose(hFindFile) == FALSE) {
2720 Py_DECREF(d);
2721 return win32_error("FindClose", namebuf);
2722 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002723
Victor Stinner8c62be82010-05-06 00:08:46 +00002724 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002725
Tim Peters0bb44a42000-09-15 07:44:49 +00002726#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002727
2728#ifndef MAX_PATH
2729#define MAX_PATH CCHMAXPATH
2730#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002731 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002732 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002733 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002734 PyObject *d, *v;
2735 char namebuf[MAX_PATH+5];
2736 HDIR hdir = 1;
2737 ULONG srchcnt = 1;
2738 FILEFINDBUF3 ep;
2739 APIRET rc;
2740
Victor Stinner8c62be82010-05-06 00:08:46 +00002741 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002742 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002743 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002744 name = PyBytes_AsString(oname);
2745 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002746 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002747 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002748 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002749 return NULL;
2750 }
2751 strcpy(namebuf, name);
2752 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002753 if (*pt == ALTSEP)
2754 *pt = SEP;
2755 if (namebuf[len-1] != SEP)
2756 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002757 strcpy(namebuf + len, "*.*");
2758
Neal Norwitz6c913782007-10-14 03:23:09 +00002759 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002760 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002761 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002762 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002763
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002764 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2765 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002766 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002767 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2768 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2769 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002770
2771 if (rc != NO_ERROR) {
2772 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002773 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002774 }
2775
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002776 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002777 do {
2778 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002779 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002780 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002781
2782 strcpy(namebuf, ep.achName);
2783
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002784 /* Leave Case of Name Alone -- In Native Form */
2785 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002786
Christian Heimes72b710a2008-05-26 13:28:38 +00002787 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002788 if (v == NULL) {
2789 Py_DECREF(d);
2790 d = NULL;
2791 break;
2792 }
2793 if (PyList_Append(d, v) != 0) {
2794 Py_DECREF(v);
2795 Py_DECREF(d);
2796 d = NULL;
2797 break;
2798 }
2799 Py_DECREF(v);
2800 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2801 }
2802
Victor Stinnerdcb24032010-04-22 12:08:36 +00002803 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002804 return d;
2805#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 PyObject *oname;
2807 char *name;
2808 PyObject *d, *v;
2809 DIR *dirp;
2810 struct dirent *ep;
2811 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002812
Victor Stinner8c62be82010-05-06 00:08:46 +00002813 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002814 /* v is never read, so it does not need to be initialized yet. */
2815 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002816 arg_is_unicode = 0;
2817 PyErr_Clear();
2818 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002819 oname = NULL;
2820 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002821 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002822 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002823 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002824 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002825 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002826 Py_BEGIN_ALLOW_THREADS
2827 dirp = opendir(name);
2828 Py_END_ALLOW_THREADS
2829 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002830 return posix_error_with_allocated_filename(oname);
2831 }
2832 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002833 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002834 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002835 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 Py_DECREF(oname);
2837 return NULL;
2838 }
2839 for (;;) {
2840 errno = 0;
2841 Py_BEGIN_ALLOW_THREADS
2842 ep = readdir(dirp);
2843 Py_END_ALLOW_THREADS
2844 if (ep == NULL) {
2845 if (errno == 0) {
2846 break;
2847 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002848 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002849 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002850 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002851 Py_DECREF(d);
2852 return posix_error_with_allocated_filename(oname);
2853 }
2854 }
2855 if (ep->d_name[0] == '.' &&
2856 (NAMLEN(ep) == 1 ||
2857 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2858 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002859 if (arg_is_unicode)
2860 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2861 else
2862 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002863 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002864 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002865 break;
2866 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002867 if (PyList_Append(d, v) != 0) {
2868 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002869 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002870 break;
2871 }
2872 Py_DECREF(v);
2873 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002874 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002875 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002876 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002878
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002880
Tim Peters0bb44a42000-09-15 07:44:49 +00002881#endif /* which OS */
2882} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002883
Antoine Pitrou8250e232011-02-25 23:41:16 +00002884#ifdef HAVE_FDOPENDIR
Charles-François Natali77940902012-02-06 19:54:48 +01002885PyDoc_STRVAR(posix_flistdir__doc__,
2886"flistdir(fd) -> list_of_strings\n\n\
Charles-François Natali76961fa2012-01-10 20:25:09 +01002887Like listdir(), but uses a file descriptor instead.");
Antoine Pitrou8250e232011-02-25 23:41:16 +00002888
2889static PyObject *
Charles-François Natali77940902012-02-06 19:54:48 +01002890posix_flistdir(PyObject *self, PyObject *args)
Antoine Pitrou8250e232011-02-25 23:41:16 +00002891{
2892 PyObject *d, *v;
2893 DIR *dirp;
2894 struct dirent *ep;
2895 int fd;
2896
2897 errno = 0;
Charles-François Natali77940902012-02-06 19:54:48 +01002898 if (!PyArg_ParseTuple(args, "i:flistdir", &fd))
Antoine Pitrou8250e232011-02-25 23:41:16 +00002899 return NULL;
Charles-François Natali76961fa2012-01-10 20:25:09 +01002900 /* closedir() closes the FD, so we duplicate it */
2901 fd = dup(fd);
2902 if (fd < 0)
2903 return posix_error();
Antoine Pitrou8250e232011-02-25 23:41:16 +00002904 Py_BEGIN_ALLOW_THREADS
2905 dirp = fdopendir(fd);
2906 Py_END_ALLOW_THREADS
2907 if (dirp == NULL) {
2908 close(fd);
2909 return posix_error();
2910 }
2911 if ((d = PyList_New(0)) == NULL) {
2912 Py_BEGIN_ALLOW_THREADS
2913 closedir(dirp);
2914 Py_END_ALLOW_THREADS
2915 return NULL;
2916 }
2917 for (;;) {
2918 errno = 0;
2919 Py_BEGIN_ALLOW_THREADS
2920 ep = readdir(dirp);
2921 Py_END_ALLOW_THREADS
2922 if (ep == NULL) {
2923 if (errno == 0) {
2924 break;
2925 } else {
2926 Py_BEGIN_ALLOW_THREADS
Charles-François Natalif2840a82012-01-08 20:30:47 +01002927 rewinddir(dirp);
Antoine Pitrou8250e232011-02-25 23:41:16 +00002928 closedir(dirp);
2929 Py_END_ALLOW_THREADS
2930 Py_DECREF(d);
2931 return posix_error();
2932 }
2933 }
2934 if (ep->d_name[0] == '.' &&
2935 (NAMLEN(ep) == 1 ||
2936 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2937 continue;
2938 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2939 if (v == NULL) {
2940 Py_CLEAR(d);
2941 break;
2942 }
2943 if (PyList_Append(d, v) != 0) {
2944 Py_DECREF(v);
2945 Py_CLEAR(d);
2946 break;
2947 }
2948 Py_DECREF(v);
2949 }
2950 Py_BEGIN_ALLOW_THREADS
Charles-François Natalif2840a82012-01-08 20:30:47 +01002951 rewinddir(dirp);
Antoine Pitrou8250e232011-02-25 23:41:16 +00002952 closedir(dirp);
2953 Py_END_ALLOW_THREADS
2954
2955 return d;
2956}
2957#endif
2958
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002959#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002960/* A helper function for abspath on win32 */
2961static PyObject *
2962posix__getfullpathname(PyObject *self, PyObject *args)
2963{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002964 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002965 char outbuf[MAX_PATH*2];
2966 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002967 PyObject *po;
2968
2969 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
2970 {
2971 wchar_t *wpath;
2972 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2973 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00002974 DWORD result;
2975 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002976
2977 wpath = PyUnicode_AsUnicode(po);
2978 if (wpath == NULL)
2979 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002980 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02002981 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00002982 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02002983 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002984 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00002985 if (!woutbufp)
2986 return PyErr_NoMemory();
2987 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2988 }
2989 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002990 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02002992 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002993 if (woutbufp != woutbuf)
2994 free(woutbufp);
2995 return v;
2996 }
2997 /* Drop the argument parsing error as narrow strings
2998 are also valid. */
2999 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02003000
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003001 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
3002 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003004 if (win32_warn_bytes_api())
3005 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02003006 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003007 outbuf, &temp)) {
3008 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003009 return NULL;
3010 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003011 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
3012 return PyUnicode_Decode(outbuf, strlen(outbuf),
3013 Py_FileSystemDefaultEncoding, NULL);
3014 }
3015 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00003016} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00003017
Brian Curtind25aef52011-06-13 15:16:04 -05003018
Brian Curtinf5e76d02010-11-24 13:14:05 +00003019
Brian Curtind40e6f72010-07-08 21:39:08 +00003020/* A helper function for samepath on windows */
3021static PyObject *
3022posix__getfinalpathname(PyObject *self, PyObject *args)
3023{
3024 HANDLE hFile;
3025 int buf_size;
3026 wchar_t *target_path;
3027 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003028 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003029 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003030
Victor Stinnereb5657a2011-09-30 01:44:27 +02003031 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003032 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003033 path = PyUnicode_AsUnicode(po);
3034 if (path == NULL)
3035 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003036
3037 if(!check_GetFinalPathNameByHandle()) {
3038 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3039 NotImplementedError. */
3040 return PyErr_Format(PyExc_NotImplementedError,
3041 "GetFinalPathNameByHandle not available on this platform");
3042 }
3043
3044 hFile = CreateFileW(
3045 path,
3046 0, /* desired access */
3047 0, /* share mode */
3048 NULL, /* security attributes */
3049 OPEN_EXISTING,
3050 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3051 FILE_FLAG_BACKUP_SEMANTICS,
3052 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003053
Victor Stinnereb5657a2011-09-30 01:44:27 +02003054 if(hFile == INVALID_HANDLE_VALUE)
3055 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003056
3057 /* We have a good handle to the target, use it to determine the
3058 target path name. */
3059 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3060
3061 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003062 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003063
3064 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3065 if(!target_path)
3066 return PyErr_NoMemory();
3067
3068 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3069 buf_size, VOLUME_NAME_DOS);
3070 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003071 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003072
3073 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003074 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003075
3076 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003077 result = PyUnicode_FromWideChar(target_path, result_length);
Brian Curtind40e6f72010-07-08 21:39:08 +00003078 free(target_path);
3079 return result;
3080
3081} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003082
3083static PyObject *
3084posix__getfileinformation(PyObject *self, PyObject *args)
3085{
3086 HANDLE hFile;
3087 BY_HANDLE_FILE_INFORMATION info;
3088 int fd;
3089
3090 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3091 return NULL;
3092
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003093 if (!_PyVerify_fd(fd))
3094 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003095
3096 hFile = (HANDLE)_get_osfhandle(fd);
3097 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003098 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003099
3100 if (!GetFileInformationByHandle(hFile, &info))
3101 return win32_error("_getfileinformation", NULL);
3102
3103 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3104 info.nFileIndexHigh,
3105 info.nFileIndexLow);
3106}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003107
Brian Curtin95d028f2011-06-09 09:10:38 -05003108PyDoc_STRVAR(posix__isdir__doc__,
3109"Return true if the pathname refers to an existing directory.");
3110
Brian Curtin9c669cc2011-06-08 18:17:18 -05003111static PyObject *
3112posix__isdir(PyObject *self, PyObject *args)
3113{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003114 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003115 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003116 DWORD attributes;
3117
3118 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003119 wchar_t *wpath = PyUnicode_AsUnicode(po);
3120 if (wpath == NULL)
3121 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003122
3123 attributes = GetFileAttributesW(wpath);
3124 if (attributes == INVALID_FILE_ATTRIBUTES)
3125 Py_RETURN_FALSE;
3126 goto check;
3127 }
3128 /* Drop the argument parsing error as narrow strings
3129 are also valid. */
3130 PyErr_Clear();
3131
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003132 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003133 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003134 if (win32_warn_bytes_api())
3135 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003136 attributes = GetFileAttributesA(path);
3137 if (attributes == INVALID_FILE_ATTRIBUTES)
3138 Py_RETURN_FALSE;
3139
3140check:
3141 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3142 Py_RETURN_TRUE;
3143 else
3144 Py_RETURN_FALSE;
3145}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003146#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003148PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003149"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003150Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003151
Barry Warsaw53699e91996-12-10 23:23:01 +00003152static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003153posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003154{
Victor Stinner8c62be82010-05-06 00:08:46 +00003155 int res;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003156 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003157 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003158
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003159#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003160 PyObject *po;
3161 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode))
3162 {
3163 wchar_t *wpath = PyUnicode_AsUnicode(po);
3164 if (wpath == NULL)
3165 return NULL;
3166
Victor Stinner8c62be82010-05-06 00:08:46 +00003167 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003168 res = CreateDirectoryW(wpath, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003169 Py_END_ALLOW_THREADS
3170 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003171 return win32_error_object("mkdir", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003172 Py_INCREF(Py_None);
3173 return Py_None;
3174 }
3175 /* Drop the argument parsing error as narrow strings
3176 are also valid. */
3177 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003178 if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00003179 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003180 if (win32_warn_bytes_api())
3181 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003182 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003183 res = CreateDirectoryA(path, NULL);
3184 Py_END_ALLOW_THREADS
3185 if (!res) {
3186 win32_error("mkdir", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003187 return NULL;
3188 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 Py_INCREF(Py_None);
3190 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003191#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003192 PyObject *opath;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003193
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3195 PyUnicode_FSConverter, &opath, &mode))
3196 return NULL;
3197 path = PyBytes_AsString(opath);
3198 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003199#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003200 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003201#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003203#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003204 Py_END_ALLOW_THREADS
3205 if (res < 0)
3206 return posix_error_with_allocated_filename(opath);
3207 Py_DECREF(opath);
3208 Py_INCREF(Py_None);
3209 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003210#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003211}
3212
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003214/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3215#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003216#include <sys/resource.h>
3217#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003218
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003219
3220#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003222"nice(inc) -> new_priority\n\n\
3223Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003224
Barry Warsaw53699e91996-12-10 23:23:01 +00003225static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003226posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003227{
Victor Stinner8c62be82010-05-06 00:08:46 +00003228 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003229
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3231 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003232
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 /* There are two flavours of 'nice': one that returns the new
3234 priority (as required by almost all standards out there) and the
3235 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3236 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003237
Victor Stinner8c62be82010-05-06 00:08:46 +00003238 If we are of the nice family that returns the new priority, we
3239 need to clear errno before the call, and check if errno is filled
3240 before calling posix_error() on a returnvalue of -1, because the
3241 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003242
Victor Stinner8c62be82010-05-06 00:08:46 +00003243 errno = 0;
3244 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003245#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003246 if (value == 0)
3247 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003248#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 if (value == -1 && errno != 0)
3250 /* either nice() or getpriority() returned an error */
3251 return posix_error();
3252 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003253}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003254#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003255
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003256
3257#ifdef HAVE_GETPRIORITY
3258PyDoc_STRVAR(posix_getpriority__doc__,
3259"getpriority(which, who) -> current_priority\n\n\
3260Get program scheduling priority.");
3261
3262static PyObject *
3263posix_getpriority(PyObject *self, PyObject *args)
3264{
3265 int which, who, retval;
3266
3267 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3268 return NULL;
3269 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003270 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003271 if (errno != 0)
3272 return posix_error();
3273 return PyLong_FromLong((long)retval);
3274}
3275#endif /* HAVE_GETPRIORITY */
3276
3277
3278#ifdef HAVE_SETPRIORITY
3279PyDoc_STRVAR(posix_setpriority__doc__,
3280"setpriority(which, who, prio) -> None\n\n\
3281Set program scheduling priority.");
3282
3283static PyObject *
3284posix_setpriority(PyObject *self, PyObject *args)
3285{
3286 int which, who, prio, retval;
3287
3288 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3289 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003290 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003291 if (retval == -1)
3292 return posix_error();
3293 Py_RETURN_NONE;
3294}
3295#endif /* HAVE_SETPRIORITY */
3296
3297
Barry Warsaw53699e91996-12-10 23:23:01 +00003298static PyObject *
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003299internal_rename(PyObject *self, PyObject *args, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003300{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003301#ifdef MS_WINDOWS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003302 PyObject *src, *dst;
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003304 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
3305 if (PyArg_ParseTuple(args,
3306 is_replace ? "UU:replace" : "UU:rename",
3307 &src, &dst))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003308 {
3309 wchar_t *wsrc, *wdst;
3310
3311 wsrc = PyUnicode_AsUnicode(src);
3312 if (wsrc == NULL)
3313 return NULL;
3314 wdst = PyUnicode_AsUnicode(dst);
3315 if (wdst == NULL)
3316 return NULL;
3317 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003318 result = MoveFileExW(wsrc, wdst, flags);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003319 Py_END_ALLOW_THREADS
3320 if (!result)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003321 return win32_error(is_replace ? "replace" : "rename", NULL);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003322 Py_INCREF(Py_None);
3323 return Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003325 else {
3326 PyErr_Clear();
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003327 if (!PyArg_ParseTuple(args,
3328 is_replace ? "O&O&:replace" : "O&O&:rename",
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003329 PyUnicode_FSConverter, &src,
3330 PyUnicode_FSConverter, &dst))
3331 return NULL;
3332
3333 if (win32_warn_bytes_api())
3334 goto error;
3335
3336 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003337 result = MoveFileExA(PyBytes_AS_STRING(src),
3338 PyBytes_AS_STRING(dst), flags);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003339 Py_END_ALLOW_THREADS
3340
3341 Py_XDECREF(src);
3342 Py_XDECREF(dst);
3343
3344 if (!result)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003345 return win32_error(is_replace ? "replace" : "rename", NULL);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003346 Py_INCREF(Py_None);
3347 return Py_None;
3348
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003349error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003350 Py_XDECREF(src);
3351 Py_XDECREF(dst);
Victor Stinner8c62be82010-05-06 00:08:46 +00003352 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003353 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003354#else
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003355 return posix_2str(args,
3356 is_replace ? "O&O&:replace" : "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003357#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003358}
3359
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003360PyDoc_STRVAR(posix_rename__doc__,
3361"rename(old, new)\n\n\
3362Rename a file or directory.");
3363
3364static PyObject *
3365posix_rename(PyObject *self, PyObject *args)
3366{
3367 return internal_rename(self, args, 0);
3368}
3369
3370PyDoc_STRVAR(posix_replace__doc__,
3371"replace(old, new)\n\n\
3372Rename a file or directory, overwriting the destination.");
3373
3374static PyObject *
3375posix_replace(PyObject *self, PyObject *args)
3376{
3377 return internal_rename(self, args, 1);
3378}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003379
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003380PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003381"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003382Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003383
Barry Warsaw53699e91996-12-10 23:23:01 +00003384static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003385posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003386{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003387#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003388 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003389#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003390 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003391#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003392}
3393
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003395PyDoc_STRVAR(posix_stat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01003396"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003397Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003398
Barry Warsaw53699e91996-12-10 23:23:01 +00003399static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01003400posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003401{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003402#ifdef MS_WINDOWS
Victor Stinner4195b5c2012-02-08 23:03:19 +01003403 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003404#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01003405 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003406#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003407}
3408
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003409
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003410#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003411PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003412"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003413Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003414
Barry Warsaw53699e91996-12-10 23:23:01 +00003415static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003416posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003417{
Victor Stinner8c62be82010-05-06 00:08:46 +00003418 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003419#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003420 wchar_t *command;
3421 if (!PyArg_ParseTuple(args, "u:system", &command))
3422 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003423
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 Py_BEGIN_ALLOW_THREADS
3425 sts = _wsystem(command);
3426 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003427#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 PyObject *command_obj;
3429 char *command;
3430 if (!PyArg_ParseTuple(args, "O&:system",
3431 PyUnicode_FSConverter, &command_obj))
3432 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003433
Victor Stinner8c62be82010-05-06 00:08:46 +00003434 command = PyBytes_AsString(command_obj);
3435 Py_BEGIN_ALLOW_THREADS
3436 sts = system(command);
3437 Py_END_ALLOW_THREADS
3438 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003439#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003441}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003442#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003443
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003445PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003446"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003447Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003448
Barry Warsaw53699e91996-12-10 23:23:01 +00003449static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003450posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003451{
Victor Stinner8c62be82010-05-06 00:08:46 +00003452 int i;
3453 if (!PyArg_ParseTuple(args, "i:umask", &i))
3454 return NULL;
3455 i = (int)umask(i);
3456 if (i < 0)
3457 return posix_error();
3458 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003459}
3460
Brian Curtind40e6f72010-07-08 21:39:08 +00003461#ifdef MS_WINDOWS
3462
3463/* override the default DeleteFileW behavior so that directory
3464symlinks can be removed with this function, the same as with
3465Unix symlinks */
3466BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3467{
3468 WIN32_FILE_ATTRIBUTE_DATA info;
3469 WIN32_FIND_DATAW find_data;
3470 HANDLE find_data_handle;
3471 int is_directory = 0;
3472 int is_link = 0;
3473
3474 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3475 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003476
Brian Curtind40e6f72010-07-08 21:39:08 +00003477 /* Get WIN32_FIND_DATA structure for the path to determine if
3478 it is a symlink */
3479 if(is_directory &&
3480 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3481 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3482
3483 if(find_data_handle != INVALID_HANDLE_VALUE) {
3484 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3485 FindClose(find_data_handle);
3486 }
3487 }
3488 }
3489
3490 if (is_directory && is_link)
3491 return RemoveDirectoryW(lpFileName);
3492
3493 return DeleteFileW(lpFileName);
3494}
3495#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003496
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003497PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003498"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003499Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003501PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003502"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003503Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003504
Barry Warsaw53699e91996-12-10 23:23:01 +00003505static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003506posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003507{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003508#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003509 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3510 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003511#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003512 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003513#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003514}
3515
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003516
Guido van Rossumb6775db1994-08-01 11:34:53 +00003517#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003518PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003519"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003520Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003521
Barry Warsaw53699e91996-12-10 23:23:01 +00003522static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003523posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003524{
Victor Stinner8c62be82010-05-06 00:08:46 +00003525 struct utsname u;
3526 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003527
Victor Stinner8c62be82010-05-06 00:08:46 +00003528 Py_BEGIN_ALLOW_THREADS
3529 res = uname(&u);
3530 Py_END_ALLOW_THREADS
3531 if (res < 0)
3532 return posix_error();
3533 return Py_BuildValue("(sssss)",
3534 u.sysname,
3535 u.nodename,
3536 u.release,
3537 u.version,
3538 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003539}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003540#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003541
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003543PyDoc_STRVAR(posix_utime__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003544"utime(path[, (atime, mtime)])\n\
3545Set the access and modified time of the file to the given values.\n\
3546If no second argument is used, set the access and modified times to\n\
3547the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003548
Barry Warsaw53699e91996-12-10 23:23:01 +00003549static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003550posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003551{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003552#ifdef MS_WINDOWS
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003553 PyObject *arg = Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003554 PyObject *obwpath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 wchar_t *wpath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003556 const char *apath;
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003558 time_t atimesec, mtimesec;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003559 long ansec, mnsec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003560 FILETIME atime, mtime;
3561 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003562
Brian Curtin52fbea12011-11-06 13:41:17 -06003563 if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003564 wpath = PyUnicode_AsUnicode(obwpath);
3565 if (wpath == NULL)
3566 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003567 Py_BEGIN_ALLOW_THREADS
3568 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3569 NULL, OPEN_EXISTING,
3570 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3571 Py_END_ALLOW_THREADS
3572 if (hFile == INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003573 return win32_error_object("utime", obwpath);
3574 }
3575 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00003576 /* Drop the argument parsing error as narrow strings
3577 are also valid. */
3578 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003579
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003580 if (!PyArg_ParseTuple(args, "y|O:utime", &apath, &arg))
3581 return NULL;
3582 if (win32_warn_bytes_api())
Victor Stinner8c62be82010-05-06 00:08:46 +00003583 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003584
Victor Stinner8c62be82010-05-06 00:08:46 +00003585 Py_BEGIN_ALLOW_THREADS
3586 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3587 NULL, OPEN_EXISTING,
3588 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3589 Py_END_ALLOW_THREADS
3590 if (hFile == INVALID_HANDLE_VALUE) {
3591 win32_error("utime", apath);
Victor Stinner8c62be82010-05-06 00:08:46 +00003592 return NULL;
3593 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003594 }
3595
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003596 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003597 SYSTEMTIME now;
3598 GetSystemTime(&now);
3599 if (!SystemTimeToFileTime(&now, &mtime) ||
3600 !SystemTimeToFileTime(&now, &atime)) {
3601 win32_error("utime", NULL);
3602 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003603 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003604 }
3605 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3606 PyErr_SetString(PyExc_TypeError,
3607 "utime() arg 2 must be a tuple (atime, mtime)");
3608 goto done;
3609 }
3610 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +01003611 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0),
3612 &atimesec, &ansec) == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00003613 goto done;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003614 time_t_to_FILE_TIME(atimesec, ansec, &atime);
Victor Stinner5d272cc2012-03-13 13:35:55 +01003615 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1),
3616 &mtimesec, &mnsec) == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00003617 goto done;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003618 time_t_to_FILE_TIME(mtimesec, mnsec, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00003619 }
3620 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3621 /* Avoid putting the file name into the error here,
3622 as that may confuse the user into believing that
3623 something is wrong with the file, when it also
3624 could be the time stamp that gives a problem. */
3625 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003626 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003627 }
3628 Py_INCREF(Py_None);
3629 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003630done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003631 CloseHandle(hFile);
3632 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003633#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003634
Victor Stinner8c62be82010-05-06 00:08:46 +00003635 PyObject *opath;
3636 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003637 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003638 long ansec, mnsec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003639 int res;
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003640 PyObject* arg = Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003641
Brian Curtin52fbea12011-11-06 13:41:17 -06003642 if (!PyArg_ParseTuple(args, "O&|O:utime",
Victor Stinner8c62be82010-05-06 00:08:46 +00003643 PyUnicode_FSConverter, &opath, &arg))
3644 return NULL;
3645 path = PyBytes_AsString(opath);
Brian Curtinb0d5b5d2011-11-07 10:51:18 -06003646 if (arg == Py_None) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003647 /* optional time values not given */
3648 Py_BEGIN_ALLOW_THREADS
3649 res = utime(path, NULL);
3650 Py_END_ALLOW_THREADS
3651 }
3652 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3653 PyErr_SetString(PyExc_TypeError,
3654 "utime() arg 2 must be a tuple (atime, mtime)");
3655 Py_DECREF(opath);
3656 return NULL;
3657 }
3658 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +01003659 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0),
3660 &atime, &ansec) == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003661 Py_DECREF(opath);
3662 return NULL;
3663 }
Victor Stinner5d272cc2012-03-13 13:35:55 +01003664 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1),
3665 &mtime, &mnsec) == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003666 Py_DECREF(opath);
3667 return NULL;
3668 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003669
3670 Py_BEGIN_ALLOW_THREADS
3671 {
3672#ifdef HAVE_UTIMENSAT
3673 struct timespec buf[2];
3674 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003675 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003676 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003677 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003678 res = utimensat(AT_FDCWD, path, buf, 0);
3679#elif defined(HAVE_UTIMES)
3680 struct timeval buf[2];
3681 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003682 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003683 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003684 buf[1].tv_usec = mnsec / 1000;
Victor Stinner8c62be82010-05-06 00:08:46 +00003685 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003686#elif defined(HAVE_UTIME_H)
3687 /* XXX should define struct utimbuf instead, above */
3688 struct utimbuf buf;
3689 buf.actime = atime;
3690 buf.modtime = mtime;
3691 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003692#else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003693 time_t buf[2];
3694 buf[0] = atime;
3695 buf[1] = mtime;
3696 res = utime(path, buf);
3697#endif
3698 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 }
3701 if (res < 0) {
3702 return posix_error_with_allocated_filename(opath);
3703 }
3704 Py_DECREF(opath);
3705 Py_INCREF(Py_None);
3706 return Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003707#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003708#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003709}
3710
Ross Lagerwall7807c352011-03-17 20:20:30 +02003711#ifdef HAVE_FUTIMES
3712PyDoc_STRVAR(posix_futimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003713"futimes(fd[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003714Set the access and modified time of the file specified by the file\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003715descriptor fd to the given values. If no second argument is used, set the\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003716access and modified times to the current time.");
3717
3718static PyObject *
3719posix_futimes(PyObject *self, PyObject *args)
3720{
3721 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003722 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003723 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003724 long ansec, mnsec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003725
Brian Curtinc1b65d12011-11-07 14:18:54 -06003726 if (!PyArg_ParseTuple(args, "i|O:futimes", &fd, &arg))
Ross Lagerwall7807c352011-03-17 20:20:30 +02003727 return NULL;
3728
3729 if (arg == Py_None) {
3730 /* optional time values not given */
3731 Py_BEGIN_ALLOW_THREADS
3732 res = futimes(fd, NULL);
3733 Py_END_ALLOW_THREADS
3734 }
3735 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3736 PyErr_SetString(PyExc_TypeError,
3737 "futimes() arg 2 must be a tuple (atime, mtime)");
3738 return NULL;
3739 }
3740 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +01003741 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0),
3742 &atime, &ansec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003743 return NULL;
3744 }
Victor Stinner5d272cc2012-03-13 13:35:55 +01003745 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1),
3746 &mtime, &mnsec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003747 return NULL;
3748 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003749 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003750 {
3751#ifdef HAVE_FUTIMENS
3752 struct timespec buf[2];
3753 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003754 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003755 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003756 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003757 res = futimens(fd, buf);
3758#else
3759 struct timeval buf[2];
3760 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003761 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003762 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003763 buf[1].tv_usec = mnsec / 1000;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003764 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003765#endif
3766 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003767 Py_END_ALLOW_THREADS
3768 }
3769 if (res < 0)
3770 return posix_error();
3771 Py_RETURN_NONE;
3772}
3773#endif
3774
3775#ifdef HAVE_LUTIMES
3776PyDoc_STRVAR(posix_lutimes__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003777"lutimes(path[, (atime, mtime)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003778Like utime(), but if path is a symbolic link, it is not dereferenced.");
3779
3780static PyObject *
3781posix_lutimes(PyObject *self, PyObject *args)
3782{
Brian Curtinc1b65d12011-11-07 14:18:54 -06003783 PyObject *opath;
3784 PyObject *arg = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003785 const char *path;
3786 int res;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003787 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003788 long ansec, mnsec;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003789
Brian Curtinc1b65d12011-11-07 14:18:54 -06003790 if (!PyArg_ParseTuple(args, "O&|O:lutimes",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003791 PyUnicode_FSConverter, &opath, &arg))
3792 return NULL;
3793 path = PyBytes_AsString(opath);
3794 if (arg == Py_None) {
3795 /* optional time values not given */
3796 Py_BEGIN_ALLOW_THREADS
3797 res = lutimes(path, NULL);
3798 Py_END_ALLOW_THREADS
3799 }
3800 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3801 PyErr_SetString(PyExc_TypeError,
3802 "lutimes() arg 2 must be a tuple (atime, mtime)");
3803 Py_DECREF(opath);
3804 return NULL;
3805 }
3806 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +01003807 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0),
3808 &atime, &ansec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003809 Py_DECREF(opath);
3810 return NULL;
3811 }
Victor Stinner5d272cc2012-03-13 13:35:55 +01003812 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1),
3813 &mtime, &mnsec) == -1) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02003814 Py_DECREF(opath);
3815 return NULL;
3816 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003817 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003818 {
3819#ifdef HAVE_UTIMENSAT
3820 struct timespec buf[2];
3821 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003822 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003823 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003824 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003825 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
3826#else
3827 struct timeval buf[2];
3828 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003829 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003830 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01003831 buf[1].tv_usec = mnsec / 1000;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003832 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003833#endif
3834 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003835 Py_END_ALLOW_THREADS
3836 }
3837 Py_DECREF(opath);
3838 if (res < 0)
3839 return posix_error();
3840 Py_RETURN_NONE;
3841}
3842#endif
3843
3844#ifdef HAVE_FUTIMENS
3845PyDoc_STRVAR(posix_futimens__doc__,
Brian Curtinc1b65d12011-11-07 14:18:54 -06003846"futimens(fd[, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003847Updates the timestamps of a file specified by the file descriptor fd, with\n\
3848nanosecond precision.\n\
Brian Curtinc1b65d12011-11-07 14:18:54 -06003849If no second argument is given, set atime and mtime to the current time.\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02003850If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3851current time.\n\
3852If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3853
3854static PyObject *
3855posix_futimens(PyObject *self, PyObject *args)
3856{
3857 int res, fd;
Brian Curtinc1b65d12011-11-07 14:18:54 -06003858 PyObject *atime = Py_None;
3859 PyObject *mtime = Py_None;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003860 struct timespec buf[2];
3861
Brian Curtinc1b65d12011-11-07 14:18:54 -06003862 if (!PyArg_ParseTuple(args, "i|OO:futimens",
Ross Lagerwall7807c352011-03-17 20:20:30 +02003863 &fd, &atime, &mtime))
3864 return NULL;
3865 if (atime == Py_None && mtime == Py_None) {
3866 /* optional time values not given */
3867 Py_BEGIN_ALLOW_THREADS
3868 res = futimens(fd, NULL);
3869 Py_END_ALLOW_THREADS
3870 }
3871 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3872 PyErr_SetString(PyExc_TypeError,
3873 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3874 return NULL;
3875 }
3876 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3877 PyErr_SetString(PyExc_TypeError,
3878 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3879 return NULL;
3880 }
3881 else {
3882 if (!PyArg_ParseTuple(atime, "ll:futimens",
3883 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3884 return NULL;
3885 }
3886 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3887 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3888 return NULL;
3889 }
3890 Py_BEGIN_ALLOW_THREADS
3891 res = futimens(fd, buf);
3892 Py_END_ALLOW_THREADS
3893 }
3894 if (res < 0)
3895 return posix_error();
3896 Py_RETURN_NONE;
3897}
3898#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003899
Guido van Rossum3b066191991-06-04 19:40:25 +00003900/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003902PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003903"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003904Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003905
Barry Warsaw53699e91996-12-10 23:23:01 +00003906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003907posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003908{
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 int sts;
3910 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3911 return NULL;
3912 _exit(sts);
3913 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003914}
3915
Martin v. Löwis114619e2002-10-07 06:44:21 +00003916#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3917static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003918free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003919{
Victor Stinner8c62be82010-05-06 00:08:46 +00003920 Py_ssize_t i;
3921 for (i = 0; i < count; i++)
3922 PyMem_Free(array[i]);
3923 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003924}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003925
Antoine Pitrou69f71142009-05-24 21:25:49 +00003926static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003927int fsconvert_strdup(PyObject *o, char**out)
3928{
Victor Stinner8c62be82010-05-06 00:08:46 +00003929 PyObject *bytes;
3930 Py_ssize_t size;
3931 if (!PyUnicode_FSConverter(o, &bytes))
3932 return 0;
3933 size = PyBytes_GET_SIZE(bytes);
3934 *out = PyMem_Malloc(size+1);
3935 if (!*out)
3936 return 0;
3937 memcpy(*out, PyBytes_AsString(bytes), size+1);
3938 Py_DECREF(bytes);
3939 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003940}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003941#endif
3942
Ross Lagerwall7807c352011-03-17 20:20:30 +02003943#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003944static char**
3945parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3946{
Victor Stinner8c62be82010-05-06 00:08:46 +00003947 char **envlist;
3948 Py_ssize_t i, pos, envc;
3949 PyObject *keys=NULL, *vals=NULL;
3950 PyObject *key, *val, *key2, *val2;
3951 char *p, *k, *v;
3952 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003953
Victor Stinner8c62be82010-05-06 00:08:46 +00003954 i = PyMapping_Size(env);
3955 if (i < 0)
3956 return NULL;
3957 envlist = PyMem_NEW(char *, i + 1);
3958 if (envlist == NULL) {
3959 PyErr_NoMemory();
3960 return NULL;
3961 }
3962 envc = 0;
3963 keys = PyMapping_Keys(env);
3964 vals = PyMapping_Values(env);
3965 if (!keys || !vals)
3966 goto error;
3967 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3968 PyErr_Format(PyExc_TypeError,
3969 "env.keys() or env.values() is not a list");
3970 goto error;
3971 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003972
Victor Stinner8c62be82010-05-06 00:08:46 +00003973 for (pos = 0; pos < i; pos++) {
3974 key = PyList_GetItem(keys, pos);
3975 val = PyList_GetItem(vals, pos);
3976 if (!key || !val)
3977 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003978
Victor Stinner8c62be82010-05-06 00:08:46 +00003979 if (PyUnicode_FSConverter(key, &key2) == 0)
3980 goto error;
3981 if (PyUnicode_FSConverter(val, &val2) == 0) {
3982 Py_DECREF(key2);
3983 goto error;
3984 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003985
3986#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003987 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3988 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003989#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003990 k = PyBytes_AsString(key2);
3991 v = PyBytes_AsString(val2);
3992 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003993
Victor Stinner8c62be82010-05-06 00:08:46 +00003994 p = PyMem_NEW(char, len);
3995 if (p == NULL) {
3996 PyErr_NoMemory();
3997 Py_DECREF(key2);
3998 Py_DECREF(val2);
3999 goto error;
4000 }
4001 PyOS_snprintf(p, len, "%s=%s", k, v);
4002 envlist[envc++] = p;
4003 Py_DECREF(key2);
4004 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004005#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004006 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004007#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004008 }
4009 Py_DECREF(vals);
4010 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004011
Victor Stinner8c62be82010-05-06 00:08:46 +00004012 envlist[envc] = 0;
4013 *envc_ptr = envc;
4014 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004015
4016error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004017 Py_XDECREF(keys);
4018 Py_XDECREF(vals);
4019 while (--envc >= 0)
4020 PyMem_DEL(envlist[envc]);
4021 PyMem_DEL(envlist);
4022 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004023}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004024
Ross Lagerwall7807c352011-03-17 20:20:30 +02004025static char**
4026parse_arglist(PyObject* argv, Py_ssize_t *argc)
4027{
4028 int i;
4029 char **argvlist = PyMem_NEW(char *, *argc+1);
4030 if (argvlist == NULL) {
4031 PyErr_NoMemory();
4032 return NULL;
4033 }
4034 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004035 PyObject* item = PySequence_ITEM(argv, i);
4036 if (item == NULL)
4037 goto fail;
4038 if (!fsconvert_strdup(item, &argvlist[i])) {
4039 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004040 goto fail;
4041 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004042 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004043 }
4044 argvlist[*argc] = NULL;
4045 return argvlist;
4046fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004047 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004048 free_string_array(argvlist, *argc);
4049 return NULL;
4050}
4051#endif
4052
4053#ifdef HAVE_EXECV
4054PyDoc_STRVAR(posix_execv__doc__,
4055"execv(path, args)\n\n\
4056Execute an executable path with arguments, replacing current process.\n\
4057\n\
4058 path: path of executable file\n\
4059 args: tuple or list of strings");
4060
4061static PyObject *
4062posix_execv(PyObject *self, PyObject *args)
4063{
4064 PyObject *opath;
4065 char *path;
4066 PyObject *argv;
4067 char **argvlist;
4068 Py_ssize_t argc;
4069
4070 /* execv has two arguments: (path, argv), where
4071 argv is a list or tuple of strings. */
4072
4073 if (!PyArg_ParseTuple(args, "O&O:execv",
4074 PyUnicode_FSConverter,
4075 &opath, &argv))
4076 return NULL;
4077 path = PyBytes_AsString(opath);
4078 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4079 PyErr_SetString(PyExc_TypeError,
4080 "execv() arg 2 must be a tuple or list");
4081 Py_DECREF(opath);
4082 return NULL;
4083 }
4084 argc = PySequence_Size(argv);
4085 if (argc < 1) {
4086 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4087 Py_DECREF(opath);
4088 return NULL;
4089 }
4090
4091 argvlist = parse_arglist(argv, &argc);
4092 if (argvlist == NULL) {
4093 Py_DECREF(opath);
4094 return NULL;
4095 }
4096
4097 execv(path, argvlist);
4098
4099 /* If we get here it's definitely an error */
4100
4101 free_string_array(argvlist, argc);
4102 Py_DECREF(opath);
4103 return posix_error();
4104}
4105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004106PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004107"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004108Execute a path with arguments and environment, replacing current process.\n\
4109\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004110 path: path of executable file\n\
4111 args: tuple or list of arguments\n\
4112 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004113
Barry Warsaw53699e91996-12-10 23:23:01 +00004114static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004115posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004116{
Victor Stinner8c62be82010-05-06 00:08:46 +00004117 PyObject *opath;
4118 char *path;
4119 PyObject *argv, *env;
4120 char **argvlist;
4121 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004122 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004123
Victor Stinner8c62be82010-05-06 00:08:46 +00004124 /* execve has three arguments: (path, argv, env), where
4125 argv is a list or tuple of strings and env is a dictionary
4126 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004127
Victor Stinner8c62be82010-05-06 00:08:46 +00004128 if (!PyArg_ParseTuple(args, "O&OO:execve",
4129 PyUnicode_FSConverter,
4130 &opath, &argv, &env))
4131 return NULL;
4132 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004133 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004134 PyErr_SetString(PyExc_TypeError,
4135 "execve() arg 2 must be a tuple or list");
4136 goto fail_0;
4137 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004138 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004139 if (!PyMapping_Check(env)) {
4140 PyErr_SetString(PyExc_TypeError,
4141 "execve() arg 3 must be a mapping object");
4142 goto fail_0;
4143 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004144
Ross Lagerwall7807c352011-03-17 20:20:30 +02004145 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004146 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004147 goto fail_0;
4148 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004149
Victor Stinner8c62be82010-05-06 00:08:46 +00004150 envlist = parse_envlist(env, &envc);
4151 if (envlist == NULL)
4152 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004153
Victor Stinner8c62be82010-05-06 00:08:46 +00004154 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004155
Victor Stinner8c62be82010-05-06 00:08:46 +00004156 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004157
Victor Stinner8c62be82010-05-06 00:08:46 +00004158 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004159
Victor Stinner8c62be82010-05-06 00:08:46 +00004160 while (--envc >= 0)
4161 PyMem_DEL(envlist[envc]);
4162 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004163 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004164 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004165 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004166 Py_DECREF(opath);
4167 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004168}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004169#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004170
Ross Lagerwall7807c352011-03-17 20:20:30 +02004171#ifdef HAVE_FEXECVE
4172PyDoc_STRVAR(posix_fexecve__doc__,
4173"fexecve(fd, args, env)\n\n\
4174Execute the program specified by a file descriptor with arguments and\n\
4175environment, replacing the current process.\n\
4176\n\
4177 fd: file descriptor of executable\n\
4178 args: tuple or list of arguments\n\
4179 env: dictionary of strings mapping to strings");
4180
4181static PyObject *
4182posix_fexecve(PyObject *self, PyObject *args)
4183{
4184 int fd;
4185 PyObject *argv, *env;
4186 char **argvlist;
4187 char **envlist;
4188 Py_ssize_t argc, envc;
4189
4190 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4191 &fd, &argv, &env))
4192 return NULL;
4193 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4194 PyErr_SetString(PyExc_TypeError,
4195 "fexecve() arg 2 must be a tuple or list");
4196 return NULL;
4197 }
4198 argc = PySequence_Size(argv);
4199 if (!PyMapping_Check(env)) {
4200 PyErr_SetString(PyExc_TypeError,
4201 "fexecve() arg 3 must be a mapping object");
4202 return NULL;
4203 }
4204
4205 argvlist = parse_arglist(argv, &argc);
4206 if (argvlist == NULL)
4207 return NULL;
4208
4209 envlist = parse_envlist(env, &envc);
4210 if (envlist == NULL)
4211 goto fail;
4212
4213 fexecve(fd, argvlist, envlist);
4214
4215 /* If we get here it's definitely an error */
4216
4217 (void) posix_error();
4218
4219 while (--envc >= 0)
4220 PyMem_DEL(envlist[envc]);
4221 PyMem_DEL(envlist);
4222 fail:
4223 free_string_array(argvlist, argc);
4224 return NULL;
4225}
4226#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004227
Guido van Rossuma1065681999-01-25 23:20:23 +00004228#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004229PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004230"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004231Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004232\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004233 mode: mode of process creation\n\
4234 path: path of executable file\n\
4235 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004236
4237static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004238posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004239{
Victor Stinner8c62be82010-05-06 00:08:46 +00004240 PyObject *opath;
4241 char *path;
4242 PyObject *argv;
4243 char **argvlist;
4244 int mode, i;
4245 Py_ssize_t argc;
4246 Py_intptr_t spawnval;
4247 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004248
Victor Stinner8c62be82010-05-06 00:08:46 +00004249 /* spawnv has three arguments: (mode, path, argv), where
4250 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004251
Victor Stinner8c62be82010-05-06 00:08:46 +00004252 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4253 PyUnicode_FSConverter,
4254 &opath, &argv))
4255 return NULL;
4256 path = PyBytes_AsString(opath);
4257 if (PyList_Check(argv)) {
4258 argc = PyList_Size(argv);
4259 getitem = PyList_GetItem;
4260 }
4261 else if (PyTuple_Check(argv)) {
4262 argc = PyTuple_Size(argv);
4263 getitem = PyTuple_GetItem;
4264 }
4265 else {
4266 PyErr_SetString(PyExc_TypeError,
4267 "spawnv() arg 2 must be a tuple or list");
4268 Py_DECREF(opath);
4269 return NULL;
4270 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004271
Victor Stinner8c62be82010-05-06 00:08:46 +00004272 argvlist = PyMem_NEW(char *, argc+1);
4273 if (argvlist == NULL) {
4274 Py_DECREF(opath);
4275 return PyErr_NoMemory();
4276 }
4277 for (i = 0; i < argc; i++) {
4278 if (!fsconvert_strdup((*getitem)(argv, i),
4279 &argvlist[i])) {
4280 free_string_array(argvlist, i);
4281 PyErr_SetString(
4282 PyExc_TypeError,
4283 "spawnv() arg 2 must contain only strings");
4284 Py_DECREF(opath);
4285 return NULL;
4286 }
4287 }
4288 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004289
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004290#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 Py_BEGIN_ALLOW_THREADS
4292 spawnval = spawnv(mode, path, argvlist);
4293 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004294#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004295 if (mode == _OLD_P_OVERLAY)
4296 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004297
Victor Stinner8c62be82010-05-06 00:08:46 +00004298 Py_BEGIN_ALLOW_THREADS
4299 spawnval = _spawnv(mode, path, argvlist);
4300 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004301#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004302
Victor Stinner8c62be82010-05-06 00:08:46 +00004303 free_string_array(argvlist, argc);
4304 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004305
Victor Stinner8c62be82010-05-06 00:08:46 +00004306 if (spawnval == -1)
4307 return posix_error();
4308 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004309#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004310 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004311#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004312 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004313#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004314}
4315
4316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004317PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004318"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004319Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004320\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004321 mode: mode of process creation\n\
4322 path: path of executable file\n\
4323 args: tuple or list of arguments\n\
4324 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004325
4326static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004327posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004328{
Victor Stinner8c62be82010-05-06 00:08:46 +00004329 PyObject *opath;
4330 char *path;
4331 PyObject *argv, *env;
4332 char **argvlist;
4333 char **envlist;
4334 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004335 int mode;
4336 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004337 Py_intptr_t spawnval;
4338 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4339 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004340
Victor Stinner8c62be82010-05-06 00:08:46 +00004341 /* spawnve has four arguments: (mode, path, argv, env), where
4342 argv is a list or tuple of strings and env is a dictionary
4343 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004344
Victor Stinner8c62be82010-05-06 00:08:46 +00004345 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4346 PyUnicode_FSConverter,
4347 &opath, &argv, &env))
4348 return NULL;
4349 path = PyBytes_AsString(opath);
4350 if (PyList_Check(argv)) {
4351 argc = PyList_Size(argv);
4352 getitem = PyList_GetItem;
4353 }
4354 else if (PyTuple_Check(argv)) {
4355 argc = PyTuple_Size(argv);
4356 getitem = PyTuple_GetItem;
4357 }
4358 else {
4359 PyErr_SetString(PyExc_TypeError,
4360 "spawnve() arg 2 must be a tuple or list");
4361 goto fail_0;
4362 }
4363 if (!PyMapping_Check(env)) {
4364 PyErr_SetString(PyExc_TypeError,
4365 "spawnve() arg 3 must be a mapping object");
4366 goto fail_0;
4367 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004368
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 argvlist = PyMem_NEW(char *, argc+1);
4370 if (argvlist == NULL) {
4371 PyErr_NoMemory();
4372 goto fail_0;
4373 }
4374 for (i = 0; i < argc; i++) {
4375 if (!fsconvert_strdup((*getitem)(argv, i),
4376 &argvlist[i]))
4377 {
4378 lastarg = i;
4379 goto fail_1;
4380 }
4381 }
4382 lastarg = argc;
4383 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004384
Victor Stinner8c62be82010-05-06 00:08:46 +00004385 envlist = parse_envlist(env, &envc);
4386 if (envlist == NULL)
4387 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004388
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004389#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 Py_BEGIN_ALLOW_THREADS
4391 spawnval = spawnve(mode, path, argvlist, envlist);
4392 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004393#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004394 if (mode == _OLD_P_OVERLAY)
4395 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004396
Victor Stinner8c62be82010-05-06 00:08:46 +00004397 Py_BEGIN_ALLOW_THREADS
4398 spawnval = _spawnve(mode, path, argvlist, envlist);
4399 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004400#endif
Tim Peters25059d32001-12-07 20:35:43 +00004401
Victor Stinner8c62be82010-05-06 00:08:46 +00004402 if (spawnval == -1)
4403 (void) posix_error();
4404 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004405#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004407#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004408 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004409#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004410
Victor Stinner8c62be82010-05-06 00:08:46 +00004411 while (--envc >= 0)
4412 PyMem_DEL(envlist[envc]);
4413 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004414 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004415 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004416 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004417 Py_DECREF(opath);
4418 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004419}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004420
4421/* OS/2 supports spawnvp & spawnvpe natively */
4422#if defined(PYOS_OS2)
4423PyDoc_STRVAR(posix_spawnvp__doc__,
4424"spawnvp(mode, file, args)\n\n\
4425Execute the program 'file' in a new process, using the environment\n\
4426search path to find the file.\n\
4427\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004428 mode: mode of process creation\n\
4429 file: executable file name\n\
4430 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004431
4432static PyObject *
4433posix_spawnvp(PyObject *self, PyObject *args)
4434{
Victor Stinner8c62be82010-05-06 00:08:46 +00004435 PyObject *opath;
4436 char *path;
4437 PyObject *argv;
4438 char **argvlist;
4439 int mode, i, argc;
4440 Py_intptr_t spawnval;
4441 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004442
Victor Stinner8c62be82010-05-06 00:08:46 +00004443 /* spawnvp has three arguments: (mode, path, argv), where
4444 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004445
Victor Stinner8c62be82010-05-06 00:08:46 +00004446 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4447 PyUnicode_FSConverter,
4448 &opath, &argv))
4449 return NULL;
4450 path = PyBytes_AsString(opath);
4451 if (PyList_Check(argv)) {
4452 argc = PyList_Size(argv);
4453 getitem = PyList_GetItem;
4454 }
4455 else if (PyTuple_Check(argv)) {
4456 argc = PyTuple_Size(argv);
4457 getitem = PyTuple_GetItem;
4458 }
4459 else {
4460 PyErr_SetString(PyExc_TypeError,
4461 "spawnvp() arg 2 must be a tuple or list");
4462 Py_DECREF(opath);
4463 return NULL;
4464 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004465
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 argvlist = PyMem_NEW(char *, argc+1);
4467 if (argvlist == NULL) {
4468 Py_DECREF(opath);
4469 return PyErr_NoMemory();
4470 }
4471 for (i = 0; i < argc; i++) {
4472 if (!fsconvert_strdup((*getitem)(argv, i),
4473 &argvlist[i])) {
4474 free_string_array(argvlist, i);
4475 PyErr_SetString(
4476 PyExc_TypeError,
4477 "spawnvp() arg 2 must contain only strings");
4478 Py_DECREF(opath);
4479 return NULL;
4480 }
4481 }
4482 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004483
Victor Stinner8c62be82010-05-06 00:08:46 +00004484 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004485#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004486 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004487#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004488 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004489#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004490 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004491
Victor Stinner8c62be82010-05-06 00:08:46 +00004492 free_string_array(argvlist, argc);
4493 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004494
Victor Stinner8c62be82010-05-06 00:08:46 +00004495 if (spawnval == -1)
4496 return posix_error();
4497 else
4498 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004499}
4500
4501
4502PyDoc_STRVAR(posix_spawnvpe__doc__,
4503"spawnvpe(mode, file, args, env)\n\n\
4504Execute the program 'file' in a new process, using the environment\n\
4505search path to find the file.\n\
4506\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004507 mode: mode of process creation\n\
4508 file: executable file name\n\
4509 args: tuple or list of arguments\n\
4510 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004511
4512static PyObject *
4513posix_spawnvpe(PyObject *self, PyObject *args)
4514{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02004515 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004516 char *path;
4517 PyObject *argv, *env;
4518 char **argvlist;
4519 char **envlist;
4520 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004521 int mode;
4522 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004523 Py_intptr_t spawnval;
4524 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4525 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004526
Victor Stinner8c62be82010-05-06 00:08:46 +00004527 /* spawnvpe has four arguments: (mode, path, argv, env), where
4528 argv is a list or tuple of strings and env is a dictionary
4529 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004530
Victor Stinner8c62be82010-05-06 00:08:46 +00004531 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4532 PyUnicode_FSConverter,
4533 &opath, &argv, &env))
4534 return NULL;
4535 path = PyBytes_AsString(opath);
4536 if (PyList_Check(argv)) {
4537 argc = PyList_Size(argv);
4538 getitem = PyList_GetItem;
4539 }
4540 else if (PyTuple_Check(argv)) {
4541 argc = PyTuple_Size(argv);
4542 getitem = PyTuple_GetItem;
4543 }
4544 else {
4545 PyErr_SetString(PyExc_TypeError,
4546 "spawnvpe() arg 2 must be a tuple or list");
4547 goto fail_0;
4548 }
4549 if (!PyMapping_Check(env)) {
4550 PyErr_SetString(PyExc_TypeError,
4551 "spawnvpe() arg 3 must be a mapping object");
4552 goto fail_0;
4553 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004554
Victor Stinner8c62be82010-05-06 00:08:46 +00004555 argvlist = PyMem_NEW(char *, argc+1);
4556 if (argvlist == NULL) {
4557 PyErr_NoMemory();
4558 goto fail_0;
4559 }
4560 for (i = 0; i < argc; i++) {
4561 if (!fsconvert_strdup((*getitem)(argv, i),
4562 &argvlist[i]))
4563 {
4564 lastarg = i;
4565 goto fail_1;
4566 }
4567 }
4568 lastarg = argc;
4569 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004570
Victor Stinner8c62be82010-05-06 00:08:46 +00004571 envlist = parse_envlist(env, &envc);
4572 if (envlist == NULL)
4573 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004574
Victor Stinner8c62be82010-05-06 00:08:46 +00004575 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004576#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004577 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004578#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004579 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004580#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004581 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004582
Victor Stinner8c62be82010-05-06 00:08:46 +00004583 if (spawnval == -1)
4584 (void) posix_error();
4585 else
4586 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004587
Victor Stinner8c62be82010-05-06 00:08:46 +00004588 while (--envc >= 0)
4589 PyMem_DEL(envlist[envc]);
4590 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004591 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004592 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004593 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004594 Py_DECREF(opath);
4595 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004596}
4597#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004598#endif /* HAVE_SPAWNV */
4599
4600
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004601#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004603"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004604Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4605\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004606Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004607
4608static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004609posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004610{
Victor Stinner8c62be82010-05-06 00:08:46 +00004611 pid_t pid;
4612 int result = 0;
4613 _PyImport_AcquireLock();
4614 pid = fork1();
4615 if (pid == 0) {
4616 /* child: this clobbers and resets the import lock. */
4617 PyOS_AfterFork();
4618 } else {
4619 /* parent: release the import lock. */
4620 result = _PyImport_ReleaseLock();
4621 }
4622 if (pid == -1)
4623 return posix_error();
4624 if (result < 0) {
4625 /* Don't clobber the OSError if the fork failed. */
4626 PyErr_SetString(PyExc_RuntimeError,
4627 "not holding the import lock");
4628 return NULL;
4629 }
4630 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004631}
4632#endif
4633
4634
Guido van Rossumad0ee831995-03-01 10:34:45 +00004635#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004636PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004637"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004638Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004640
Barry Warsaw53699e91996-12-10 23:23:01 +00004641static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004642posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004643{
Victor Stinner8c62be82010-05-06 00:08:46 +00004644 pid_t pid;
4645 int result = 0;
4646 _PyImport_AcquireLock();
4647 pid = fork();
4648 if (pid == 0) {
4649 /* child: this clobbers and resets the import lock. */
4650 PyOS_AfterFork();
4651 } else {
4652 /* parent: release the import lock. */
4653 result = _PyImport_ReleaseLock();
4654 }
4655 if (pid == -1)
4656 return posix_error();
4657 if (result < 0) {
4658 /* Don't clobber the OSError if the fork failed. */
4659 PyErr_SetString(PyExc_RuntimeError,
4660 "not holding the import lock");
4661 return NULL;
4662 }
4663 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004664}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004665#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004666
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004667#ifdef HAVE_SCHED_H
4668
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004669#ifdef HAVE_SCHED_GET_PRIORITY_MAX
4670
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004671PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4672"sched_get_priority_max(policy)\n\n\
4673Get the maximum scheduling priority for *policy*.");
4674
4675static PyObject *
4676posix_sched_get_priority_max(PyObject *self, PyObject *args)
4677{
4678 int policy, max;
4679
4680 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4681 return NULL;
4682 max = sched_get_priority_max(policy);
4683 if (max < 0)
4684 return posix_error();
4685 return PyLong_FromLong(max);
4686}
4687
4688PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4689"sched_get_priority_min(policy)\n\n\
4690Get the minimum scheduling priority for *policy*.");
4691
4692static PyObject *
4693posix_sched_get_priority_min(PyObject *self, PyObject *args)
4694{
4695 int policy, min;
4696
4697 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4698 return NULL;
4699 min = sched_get_priority_min(policy);
4700 if (min < 0)
4701 return posix_error();
4702 return PyLong_FromLong(min);
4703}
4704
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02004705#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
4706
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004707#ifdef HAVE_SCHED_SETSCHEDULER
4708
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004709PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4710"sched_getscheduler(pid)\n\n\
4711Get the scheduling policy for the process with a PID of *pid*.\n\
4712Passing a PID of 0 returns the scheduling policy for the calling process.");
4713
4714static PyObject *
4715posix_sched_getscheduler(PyObject *self, PyObject *args)
4716{
4717 pid_t pid;
4718 int policy;
4719
4720 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4721 return NULL;
4722 policy = sched_getscheduler(pid);
4723 if (policy < 0)
4724 return posix_error();
4725 return PyLong_FromLong(policy);
4726}
4727
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004728#endif
4729
4730#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
4731
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004732static PyObject *
4733sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4734{
4735 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05004736 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004737
4738 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4739 return NULL;
4740 res = PyStructSequence_New(type);
4741 if (!res)
4742 return NULL;
4743 Py_INCREF(priority);
4744 PyStructSequence_SET_ITEM(res, 0, priority);
4745 return res;
4746}
4747
4748PyDoc_STRVAR(sched_param__doc__,
4749"sched_param(sched_priority): A scheduling parameter.\n\n\
4750Current has only one field: sched_priority");
4751
4752static PyStructSequence_Field sched_param_fields[] = {
4753 {"sched_priority", "the scheduling priority"},
4754 {0}
4755};
4756
4757static PyStructSequence_Desc sched_param_desc = {
4758 "sched_param", /* name */
4759 sched_param__doc__, /* doc */
4760 sched_param_fields,
4761 1
4762};
4763
4764static int
4765convert_sched_param(PyObject *param, struct sched_param *res)
4766{
4767 long priority;
4768
4769 if (Py_TYPE(param) != &SchedParamType) {
4770 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4771 return 0;
4772 }
4773 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4774 if (priority == -1 && PyErr_Occurred())
4775 return 0;
4776 if (priority > INT_MAX || priority < INT_MIN) {
4777 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4778 return 0;
4779 }
4780 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4781 return 1;
4782}
4783
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004784#endif
4785
4786#ifdef HAVE_SCHED_SETSCHEDULER
4787
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004788PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4789"sched_setscheduler(pid, policy, param)\n\n\
4790Set the scheduling policy, *policy*, for *pid*.\n\
4791If *pid* is 0, the calling process is changed.\n\
4792*param* is an instance of sched_param.");
4793
4794static PyObject *
4795posix_sched_setscheduler(PyObject *self, PyObject *args)
4796{
4797 pid_t pid;
4798 int policy;
4799 struct sched_param param;
4800
4801 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4802 &pid, &policy, &convert_sched_param, &param))
4803 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02004804
4805 /*
Jesus Cea54b01492011-09-10 01:53:19 +02004806 ** sched_setscheduler() returns 0 in Linux, but the previous
4807 ** scheduling policy under Solaris/Illumos, and others.
4808 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02004809 */
4810 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004811 return posix_error();
4812 Py_RETURN_NONE;
4813}
4814
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004815#endif
4816
4817#ifdef HAVE_SCHED_SETPARAM
4818
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004819PyDoc_STRVAR(posix_sched_getparam__doc__,
4820"sched_getparam(pid) -> sched_param\n\n\
4821Returns scheduling parameters for the process with *pid* as an instance of the\n\
4822sched_param class. A PID of 0 means the calling process.");
4823
4824static PyObject *
4825posix_sched_getparam(PyObject *self, PyObject *args)
4826{
4827 pid_t pid;
4828 struct sched_param param;
4829 PyObject *res, *priority;
4830
4831 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4832 return NULL;
4833 if (sched_getparam(pid, &param))
4834 return posix_error();
4835 res = PyStructSequence_New(&SchedParamType);
4836 if (!res)
4837 return NULL;
4838 priority = PyLong_FromLong(param.sched_priority);
4839 if (!priority) {
4840 Py_DECREF(res);
4841 return NULL;
4842 }
4843 PyStructSequence_SET_ITEM(res, 0, priority);
4844 return res;
4845}
4846
4847PyDoc_STRVAR(posix_sched_setparam__doc__,
4848"sched_setparam(pid, param)\n\n\
4849Set scheduling parameters for a process with PID *pid*.\n\
4850A PID of 0 means the calling process.");
4851
4852static PyObject *
4853posix_sched_setparam(PyObject *self, PyObject *args)
4854{
4855 pid_t pid;
4856 struct sched_param param;
4857
4858 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4859 &pid, &convert_sched_param, &param))
4860 return NULL;
4861 if (sched_setparam(pid, &param))
4862 return posix_error();
4863 Py_RETURN_NONE;
4864}
4865
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004866#endif
4867
4868#ifdef HAVE_SCHED_RR_GET_INTERVAL
4869
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004870PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4871"sched_rr_get_interval(pid) -> float\n\n\
4872Return the round-robin quantum for the process with PID *pid* in seconds.");
4873
4874static PyObject *
4875posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4876{
4877 pid_t pid;
4878 struct timespec interval;
4879
4880 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4881 return NULL;
4882 if (sched_rr_get_interval(pid, &interval))
4883 return posix_error();
4884 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4885}
4886
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05004887#endif
4888
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004889PyDoc_STRVAR(posix_sched_yield__doc__,
4890"sched_yield()\n\n\
4891Voluntarily relinquish the CPU.");
4892
4893static PyObject *
4894posix_sched_yield(PyObject *self, PyObject *noargs)
4895{
4896 if (sched_yield())
4897 return posix_error();
4898 Py_RETURN_NONE;
4899}
4900
Benjamin Peterson2740af82011-08-02 17:41:34 -05004901#ifdef HAVE_SCHED_SETAFFINITY
4902
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004903typedef struct {
4904 PyObject_HEAD;
4905 Py_ssize_t size;
4906 int ncpus;
4907 cpu_set_t *set;
4908} Py_cpu_set;
4909
4910static PyTypeObject cpu_set_type;
4911
4912static void
4913cpu_set_dealloc(Py_cpu_set *set)
4914{
4915 assert(set->set);
4916 CPU_FREE(set->set);
4917 Py_TYPE(set)->tp_free(set);
4918}
4919
4920static Py_cpu_set *
4921make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4922{
4923 Py_cpu_set *set;
4924
4925 if (size < 0) {
4926 PyErr_SetString(PyExc_ValueError, "negative size");
4927 return NULL;
4928 }
4929 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4930 if (!set)
4931 return NULL;
4932 set->ncpus = size;
4933 set->size = CPU_ALLOC_SIZE(size);
4934 set->set = CPU_ALLOC(size);
4935 if (!set->set) {
4936 type->tp_free(set);
4937 PyErr_NoMemory();
4938 return NULL;
4939 }
4940 CPU_ZERO_S(set->size, set->set);
4941 return set;
4942}
4943
4944static PyObject *
4945cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4946{
4947 int size;
4948
4949 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4950 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4951 return NULL;
4952 return (PyObject *)make_new_cpu_set(type, size);
4953}
4954
4955static PyObject *
4956cpu_set_repr(Py_cpu_set *set)
4957{
4958 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
Victor Stinner63941882011-09-29 00:42:28 +02004959}
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004960
4961static Py_ssize_t
4962cpu_set_len(Py_cpu_set *set)
4963{
4964 return set->ncpus;
4965}
4966
4967static int
4968_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4969{
4970 int cpu;
4971 if (!PyArg_ParseTuple(args, requester, &cpu))
4972 return -1;
4973 if (cpu < 0) {
4974 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4975 return -1;
4976 }
4977 if (cpu >= set->ncpus) {
4978 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4979 return -1;
4980 }
4981 return cpu;
4982}
4983
4984PyDoc_STRVAR(cpu_set_set_doc,
4985"cpu_set.set(i)\n\n\
4986Add CPU *i* to the set.");
4987
4988static PyObject *
4989cpu_set_set(Py_cpu_set *set, PyObject *args)
4990{
4991 int cpu = _get_cpu(set, "i|set", args);
4992 if (cpu == -1)
4993 return NULL;
4994 CPU_SET_S(cpu, set->size, set->set);
4995 Py_RETURN_NONE;
4996}
4997
4998PyDoc_STRVAR(cpu_set_count_doc,
4999"cpu_set.count() -> int\n\n\
5000Return the number of CPUs active in the set.");
5001
5002static PyObject *
5003cpu_set_count(Py_cpu_set *set, PyObject *noargs)
5004{
5005 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
5006}
5007
5008PyDoc_STRVAR(cpu_set_clear_doc,
5009"cpu_set.clear(i)\n\n\
5010Remove CPU *i* from the set.");
5011
5012static PyObject *
5013cpu_set_clear(Py_cpu_set *set, PyObject *args)
5014{
5015 int cpu = _get_cpu(set, "i|clear", args);
5016 if (cpu == -1)
5017 return NULL;
5018 CPU_CLR_S(cpu, set->size, set->set);
5019 Py_RETURN_NONE;
5020}
5021
5022PyDoc_STRVAR(cpu_set_isset_doc,
5023"cpu_set.isset(i) -> bool\n\n\
5024Test if CPU *i* is in the set.");
5025
5026static PyObject *
5027cpu_set_isset(Py_cpu_set *set, PyObject *args)
5028{
5029 int cpu = _get_cpu(set, "i|isset", args);
5030 if (cpu == -1)
5031 return NULL;
5032 if (CPU_ISSET_S(cpu, set->size, set->set))
5033 Py_RETURN_TRUE;
5034 Py_RETURN_FALSE;
5035}
5036
5037PyDoc_STRVAR(cpu_set_zero_doc,
5038"cpu_set.zero()\n\n\
5039Clear the cpu_set.");
5040
5041static PyObject *
5042cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
5043{
5044 CPU_ZERO_S(set->size, set->set);
5045 Py_RETURN_NONE;
5046}
5047
5048static PyObject *
5049cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
5050{
5051 int eq;
5052
Brian Curtindfc80e32011-08-10 20:28:54 -05005053 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
5054 Py_RETURN_NOTIMPLEMENTED;
5055
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005056 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
5057 if ((op == Py_EQ) ? eq : !eq)
5058 Py_RETURN_TRUE;
5059 else
5060 Py_RETURN_FALSE;
5061}
5062
5063#define CPU_SET_BINOP(name, op) \
5064 static PyObject * \
5065 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5066 if (res) { \
5067 Py_INCREF(res); \
5068 } \
5069 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005070 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005071 if (!res) \
5072 return NULL; \
5073 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005074 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005075 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005076 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005077 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005078 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005079 op(res->size, res->set, left->set, right->set); \
5080 return (PyObject *)res; \
5081 } \
5082 static PyObject * \
5083 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5084 return do_cpu_set_##name(left, right, NULL); \
5085 } \
5086 static PyObject * \
5087 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5088 return do_cpu_set_##name(left, right, left); \
5089 } \
5090
5091CPU_SET_BINOP(and, CPU_AND_S)
5092CPU_SET_BINOP(or, CPU_OR_S)
5093CPU_SET_BINOP(xor, CPU_XOR_S)
5094#undef CPU_SET_BINOP
5095
5096PyDoc_STRVAR(cpu_set_doc,
5097"cpu_set(size)\n\n\
5098Create an empty mask of CPUs.");
5099
5100static PyNumberMethods cpu_set_as_number = {
5101 0, /*nb_add*/
5102 0, /*nb_subtract*/
5103 0, /*nb_multiply*/
5104 0, /*nb_remainder*/
5105 0, /*nb_divmod*/
5106 0, /*nb_power*/
5107 0, /*nb_negative*/
5108 0, /*nb_positive*/
5109 0, /*nb_absolute*/
5110 0, /*nb_bool*/
5111 0, /*nb_invert*/
5112 0, /*nb_lshift*/
5113 0, /*nb_rshift*/
5114 (binaryfunc)cpu_set_and, /*nb_and*/
5115 (binaryfunc)cpu_set_xor, /*nb_xor*/
5116 (binaryfunc)cpu_set_or, /*nb_or*/
5117 0, /*nb_int*/
5118 0, /*nb_reserved*/
5119 0, /*nb_float*/
5120 0, /*nb_inplace_add*/
5121 0, /*nb_inplace_subtract*/
5122 0, /*nb_inplace_multiply*/
5123 0, /*nb_inplace_remainder*/
5124 0, /*nb_inplace_power*/
5125 0, /*nb_inplace_lshift*/
5126 0, /*nb_inplace_rshift*/
5127 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5128 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5129 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5130};
5131
5132static PySequenceMethods cpu_set_as_sequence = {
5133 (lenfunc)cpu_set_len, /* sq_length */
5134};
5135
5136static PyMethodDef cpu_set_methods[] = {
5137 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5138 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5139 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5140 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5141 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5142 {NULL, NULL} /* sentinel */
5143};
5144
5145static PyTypeObject cpu_set_type = {
5146 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5147 "posix.cpu_set", /* tp_name */
5148 sizeof(Py_cpu_set), /* tp_basicsize */
5149 0, /* tp_itemsize */
5150 /* methods */
5151 (destructor)cpu_set_dealloc, /* tp_dealloc */
5152 0, /* tp_print */
5153 0, /* tp_getattr */
5154 0, /* tp_setattr */
5155 0, /* tp_reserved */
5156 (reprfunc)cpu_set_repr, /* tp_repr */
5157 &cpu_set_as_number, /* tp_as_number */
5158 &cpu_set_as_sequence, /* tp_as_sequence */
5159 0, /* tp_as_mapping */
5160 PyObject_HashNotImplemented, /* tp_hash */
5161 0, /* tp_call */
5162 0, /* tp_str */
5163 PyObject_GenericGetAttr, /* tp_getattro */
5164 0, /* tp_setattro */
5165 0, /* tp_as_buffer */
5166 Py_TPFLAGS_DEFAULT, /* tp_flags */
5167 cpu_set_doc, /* tp_doc */
5168 0, /* tp_traverse */
5169 0, /* tp_clear */
5170 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5171 0, /* tp_weaklistoffset */
5172 0, /* tp_iter */
5173 0, /* tp_iternext */
5174 cpu_set_methods, /* tp_methods */
5175 0, /* tp_members */
5176 0, /* tp_getset */
5177 0, /* tp_base */
5178 0, /* tp_dict */
5179 0, /* tp_descr_get */
5180 0, /* tp_descr_set */
5181 0, /* tp_dictoffset */
5182 0, /* tp_init */
5183 PyType_GenericAlloc, /* tp_alloc */
5184 cpu_set_new, /* tp_new */
5185 PyObject_Del, /* tp_free */
5186};
5187
5188PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5189"sched_setaffinity(pid, cpu_set)\n\n\
5190Set the affinity of the process with PID *pid* to *cpu_set*.");
5191
5192static PyObject *
5193posix_sched_setaffinity(PyObject *self, PyObject *args)
5194{
5195 pid_t pid;
5196 Py_cpu_set *cpu_set;
5197
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005198 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005199 &pid, &cpu_set_type, &cpu_set))
5200 return NULL;
5201 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5202 return posix_error();
5203 Py_RETURN_NONE;
5204}
5205
5206PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5207"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5208Return the affinity of the process with PID *pid*.\n\
5209The returned cpu_set will be of size *ncpus*.");
5210
5211static PyObject *
5212posix_sched_getaffinity(PyObject *self, PyObject *args)
5213{
5214 pid_t pid;
5215 int ncpus;
5216 Py_cpu_set *res;
5217
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005218 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005219 &pid, &ncpus))
5220 return NULL;
5221 res = make_new_cpu_set(&cpu_set_type, ncpus);
5222 if (!res)
5223 return NULL;
5224 if (sched_getaffinity(pid, res->size, res->set)) {
5225 Py_DECREF(res);
5226 return posix_error();
5227 }
5228 return (PyObject *)res;
5229}
5230
Benjamin Peterson2740af82011-08-02 17:41:34 -05005231#endif /* HAVE_SCHED_SETAFFINITY */
5232
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005233#endif /* HAVE_SCHED_H */
5234
Neal Norwitzb59798b2003-03-21 01:43:31 +00005235/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005236/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5237#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005238#define DEV_PTY_FILE "/dev/ptc"
5239#define HAVE_DEV_PTMX
5240#else
5241#define DEV_PTY_FILE "/dev/ptmx"
5242#endif
5243
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005244#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005245#ifdef HAVE_PTY_H
5246#include <pty.h>
5247#else
5248#ifdef HAVE_LIBUTIL_H
5249#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005250#else
5251#ifdef HAVE_UTIL_H
5252#include <util.h>
5253#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005254#endif /* HAVE_LIBUTIL_H */
5255#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005256#ifdef HAVE_STROPTS_H
5257#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005258#endif
5259#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005260
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005261#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005262PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005263"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005264Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005265
5266static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005267posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005268{
Victor Stinner8c62be82010-05-06 00:08:46 +00005269 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005270#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005271 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005272#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005273#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005274 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005275#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005277#endif
5278#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005279
Thomas Wouters70c21a12000-07-14 14:28:33 +00005280#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005281 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5282 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005283#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005284 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5285 if (slave_name == NULL)
5286 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005287
Victor Stinner8c62be82010-05-06 00:08:46 +00005288 slave_fd = open(slave_name, O_RDWR);
5289 if (slave_fd < 0)
5290 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005291#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005292 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5293 if (master_fd < 0)
5294 return posix_error();
5295 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5296 /* change permission of slave */
5297 if (grantpt(master_fd) < 0) {
5298 PyOS_setsig(SIGCHLD, sig_saved);
5299 return posix_error();
5300 }
5301 /* unlock slave */
5302 if (unlockpt(master_fd) < 0) {
5303 PyOS_setsig(SIGCHLD, sig_saved);
5304 return posix_error();
5305 }
5306 PyOS_setsig(SIGCHLD, sig_saved);
5307 slave_name = ptsname(master_fd); /* get name of slave */
5308 if (slave_name == NULL)
5309 return posix_error();
5310 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5311 if (slave_fd < 0)
5312 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005313#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005314 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5315 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005316#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005317 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005318#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005319#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005320#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005321
Victor Stinner8c62be82010-05-06 00:08:46 +00005322 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005323
Fred Drake8cef4cf2000-06-28 16:40:38 +00005324}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005325#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005326
5327#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005328PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005329"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005330Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5331Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005332To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005333
5334static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005335posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005336{
Victor Stinner8c62be82010-05-06 00:08:46 +00005337 int master_fd = -1, result = 0;
5338 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005339
Victor Stinner8c62be82010-05-06 00:08:46 +00005340 _PyImport_AcquireLock();
5341 pid = forkpty(&master_fd, NULL, NULL, NULL);
5342 if (pid == 0) {
5343 /* child: this clobbers and resets the import lock. */
5344 PyOS_AfterFork();
5345 } else {
5346 /* parent: release the import lock. */
5347 result = _PyImport_ReleaseLock();
5348 }
5349 if (pid == -1)
5350 return posix_error();
5351 if (result < 0) {
5352 /* Don't clobber the OSError if the fork failed. */
5353 PyErr_SetString(PyExc_RuntimeError,
5354 "not holding the import lock");
5355 return NULL;
5356 }
5357 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005358}
5359#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005360
Ross Lagerwall7807c352011-03-17 20:20:30 +02005361
Guido van Rossumad0ee831995-03-01 10:34:45 +00005362#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005363PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005364"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005365Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005366
Barry Warsaw53699e91996-12-10 23:23:01 +00005367static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005368posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005369{
Victor Stinner8c62be82010-05-06 00:08:46 +00005370 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005371}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005372#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005373
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005374
Guido van Rossumad0ee831995-03-01 10:34:45 +00005375#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005376PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005377"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005378Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005379
Barry Warsaw53699e91996-12-10 23:23:01 +00005380static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005381posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005382{
Victor Stinner8c62be82010-05-06 00:08:46 +00005383 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005384}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005385#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005386
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005387
Guido van Rossumad0ee831995-03-01 10:34:45 +00005388#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005389PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005390"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005391Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005392
Barry Warsaw53699e91996-12-10 23:23:01 +00005393static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005394posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005395{
Victor Stinner8c62be82010-05-06 00:08:46 +00005396 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005397}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005398#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005399
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005400
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005401PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005402"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005403Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005404
Barry Warsaw53699e91996-12-10 23:23:01 +00005405static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005406posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005407{
Victor Stinner8c62be82010-05-06 00:08:46 +00005408 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005409}
5410
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005411#ifdef HAVE_GETGROUPLIST
5412PyDoc_STRVAR(posix_getgrouplist__doc__,
5413"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5414Returns a list of groups to which a user belongs.\n\n\
5415 user: username to lookup\n\
5416 group: base group id of the user");
5417
5418static PyObject *
5419posix_getgrouplist(PyObject *self, PyObject *args)
5420{
5421#ifdef NGROUPS_MAX
5422#define MAX_GROUPS NGROUPS_MAX
5423#else
5424 /* defined to be 16 on Solaris7, so this should be a small number */
5425#define MAX_GROUPS 64
5426#endif
5427
5428 const char *user;
5429 int i, ngroups;
5430 PyObject *list;
5431#ifdef __APPLE__
5432 int *groups, basegid;
5433#else
5434 gid_t *groups, basegid;
5435#endif
5436 ngroups = MAX_GROUPS;
5437
5438 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5439 return NULL;
5440
5441#ifdef __APPLE__
5442 groups = PyMem_Malloc(ngroups * sizeof(int));
5443#else
5444 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5445#endif
5446 if (groups == NULL)
5447 return PyErr_NoMemory();
5448
5449 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5450 PyMem_Del(groups);
5451 return posix_error();
5452 }
5453
5454 list = PyList_New(ngroups);
5455 if (list == NULL) {
5456 PyMem_Del(groups);
5457 return NULL;
5458 }
5459
5460 for (i = 0; i < ngroups; i++) {
5461 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5462 if (o == NULL) {
5463 Py_DECREF(list);
5464 PyMem_Del(groups);
5465 return NULL;
5466 }
5467 PyList_SET_ITEM(list, i, o);
5468 }
5469
5470 PyMem_Del(groups);
5471
5472 return list;
5473}
5474#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005475
Fred Drakec9680921999-12-13 16:37:25 +00005476#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005477PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005478"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005479Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005480
5481static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005482posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005483{
5484 PyObject *result = NULL;
5485
Fred Drakec9680921999-12-13 16:37:25 +00005486#ifdef NGROUPS_MAX
5487#define MAX_GROUPS NGROUPS_MAX
5488#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005489 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005490#define MAX_GROUPS 64
5491#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005492 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005493
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005494 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005495 * This is a helper variable to store the intermediate result when
5496 * that happens.
5497 *
5498 * To keep the code readable the OSX behaviour is unconditional,
5499 * according to the POSIX spec this should be safe on all unix-y
5500 * systems.
5501 */
5502 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005503 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005504
Victor Stinner8c62be82010-05-06 00:08:46 +00005505 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005506 if (n < 0) {
5507 if (errno == EINVAL) {
5508 n = getgroups(0, NULL);
5509 if (n == -1) {
5510 return posix_error();
5511 }
5512 if (n == 0) {
5513 /* Avoid malloc(0) */
5514 alt_grouplist = grouplist;
5515 } else {
5516 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5517 if (alt_grouplist == NULL) {
5518 errno = EINVAL;
5519 return posix_error();
5520 }
5521 n = getgroups(n, alt_grouplist);
5522 if (n == -1) {
5523 PyMem_Free(alt_grouplist);
5524 return posix_error();
5525 }
5526 }
5527 } else {
5528 return posix_error();
5529 }
5530 }
5531 result = PyList_New(n);
5532 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005533 int i;
5534 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005535 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005537 Py_DECREF(result);
5538 result = NULL;
5539 break;
Fred Drakec9680921999-12-13 16:37:25 +00005540 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005541 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005542 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005543 }
5544
5545 if (alt_grouplist != grouplist) {
5546 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005547 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005548
Fred Drakec9680921999-12-13 16:37:25 +00005549 return result;
5550}
5551#endif
5552
Antoine Pitroub7572f02009-12-02 20:46:48 +00005553#ifdef HAVE_INITGROUPS
5554PyDoc_STRVAR(posix_initgroups__doc__,
5555"initgroups(username, gid) -> None\n\n\
5556Call the system initgroups() to initialize the group access list with all of\n\
5557the groups of which the specified username is a member, plus the specified\n\
5558group id.");
5559
5560static PyObject *
5561posix_initgroups(PyObject *self, PyObject *args)
5562{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005563 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005564 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005565 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005566 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005567
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005568 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5569 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005570 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005571 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005572
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005573 res = initgroups(username, (gid_t) gid);
5574 Py_DECREF(oname);
5575 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005576 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005577
Victor Stinner8c62be82010-05-06 00:08:46 +00005578 Py_INCREF(Py_None);
5579 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005580}
5581#endif
5582
Martin v. Löwis606edc12002-06-13 21:09:11 +00005583#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005584PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005585"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005586Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005587
5588static PyObject *
5589posix_getpgid(PyObject *self, PyObject *args)
5590{
Victor Stinner8c62be82010-05-06 00:08:46 +00005591 pid_t pid, pgid;
5592 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5593 return NULL;
5594 pgid = getpgid(pid);
5595 if (pgid < 0)
5596 return posix_error();
5597 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005598}
5599#endif /* HAVE_GETPGID */
5600
5601
Guido van Rossumb6775db1994-08-01 11:34:53 +00005602#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005604"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005605Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005606
Barry Warsaw53699e91996-12-10 23:23:01 +00005607static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005608posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005609{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005610#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005611 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005612#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005613 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005614#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005615}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005616#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005618
Guido van Rossumb6775db1994-08-01 11:34:53 +00005619#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005621"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005622Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005623
Barry Warsaw53699e91996-12-10 23:23:01 +00005624static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005625posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005626{
Guido van Rossum64933891994-10-20 21:56:42 +00005627#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005628 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005629#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005630 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005631#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 return posix_error();
5633 Py_INCREF(Py_None);
5634 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005635}
5636
Guido van Rossumb6775db1994-08-01 11:34:53 +00005637#endif /* HAVE_SETPGRP */
5638
Guido van Rossumad0ee831995-03-01 10:34:45 +00005639#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005640
5641#ifdef MS_WINDOWS
5642#include <tlhelp32.h>
5643
5644static PyObject*
5645win32_getppid()
5646{
5647 HANDLE snapshot;
5648 pid_t mypid;
5649 PyObject* result = NULL;
5650 BOOL have_record;
5651 PROCESSENTRY32 pe;
5652
5653 mypid = getpid(); /* This function never fails */
5654
5655 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5656 if (snapshot == INVALID_HANDLE_VALUE)
5657 return PyErr_SetFromWindowsErr(GetLastError());
5658
5659 pe.dwSize = sizeof(pe);
5660 have_record = Process32First(snapshot, &pe);
5661 while (have_record) {
5662 if (mypid == (pid_t)pe.th32ProcessID) {
5663 /* We could cache the ulong value in a static variable. */
5664 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5665 break;
5666 }
5667
5668 have_record = Process32Next(snapshot, &pe);
5669 }
5670
5671 /* If our loop exits and our pid was not found (result will be NULL)
5672 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5673 * error anyway, so let's raise it. */
5674 if (!result)
5675 result = PyErr_SetFromWindowsErr(GetLastError());
5676
5677 CloseHandle(snapshot);
5678
5679 return result;
5680}
5681#endif /*MS_WINDOWS*/
5682
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005683PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005684"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005685Return the parent's process id. If the parent process has already exited,\n\
5686Windows machines will still return its id; others systems will return the id\n\
5687of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005688
Barry Warsaw53699e91996-12-10 23:23:01 +00005689static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005690posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005691{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005692#ifdef MS_WINDOWS
5693 return win32_getppid();
5694#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005696#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005697}
5698#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005699
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005700
Fred Drake12c6e2d1999-12-14 21:25:03 +00005701#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005702PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005703"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005704Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005705
5706static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005707posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005708{
Victor Stinner8c62be82010-05-06 00:08:46 +00005709 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005710#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005711 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02005712 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005713
5714 if (GetUserNameW(user_name, &num_chars)) {
5715 /* num_chars is the number of unicode chars plus null terminator */
5716 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005717 }
5718 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005719 result = PyErr_SetFromWindowsErr(GetLastError());
5720#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 char *name;
5722 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005723
Victor Stinner8c62be82010-05-06 00:08:46 +00005724 errno = 0;
5725 name = getlogin();
5726 if (name == NULL) {
5727 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005728 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005729 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005730 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 }
5732 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005733 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005734 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005735#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005736 return result;
5737}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005738#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005739
Guido van Rossumad0ee831995-03-01 10:34:45 +00005740#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005741PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005742"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005743Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005744
Barry Warsaw53699e91996-12-10 23:23:01 +00005745static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005746posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005747{
Victor Stinner8c62be82010-05-06 00:08:46 +00005748 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005749}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005750#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005751
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005752
Guido van Rossumad0ee831995-03-01 10:34:45 +00005753#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005754PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005755"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005756Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005757
Barry Warsaw53699e91996-12-10 23:23:01 +00005758static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005759posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005760{
Victor Stinner8c62be82010-05-06 00:08:46 +00005761 pid_t pid;
5762 int sig;
5763 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5764 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005765#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005766 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5767 APIRET rc;
5768 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005769 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005770
5771 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5772 APIRET rc;
5773 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005774 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005775
5776 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005777 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005778#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005779 if (kill(pid, sig) == -1)
5780 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005781#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005782 Py_INCREF(Py_None);
5783 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005784}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005785#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005786
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005787#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005788PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005789"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005790Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005791
5792static PyObject *
5793posix_killpg(PyObject *self, PyObject *args)
5794{
Victor Stinner8c62be82010-05-06 00:08:46 +00005795 int sig;
5796 pid_t pgid;
5797 /* XXX some man pages make the `pgid` parameter an int, others
5798 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5799 take the same type. Moreover, pid_t is always at least as wide as
5800 int (else compilation of this module fails), which is safe. */
5801 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5802 return NULL;
5803 if (killpg(pgid, sig) == -1)
5804 return posix_error();
5805 Py_INCREF(Py_None);
5806 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005807}
5808#endif
5809
Brian Curtineb24d742010-04-12 17:16:38 +00005810#ifdef MS_WINDOWS
5811PyDoc_STRVAR(win32_kill__doc__,
5812"kill(pid, sig)\n\n\
5813Kill a process with a signal.");
5814
5815static PyObject *
5816win32_kill(PyObject *self, PyObject *args)
5817{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005818 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005819 DWORD pid, sig, err;
5820 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005821
Victor Stinner8c62be82010-05-06 00:08:46 +00005822 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5823 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005824
Victor Stinner8c62be82010-05-06 00:08:46 +00005825 /* Console processes which share a common console can be sent CTRL+C or
5826 CTRL+BREAK events, provided they handle said events. */
5827 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5828 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5829 err = GetLastError();
5830 PyErr_SetFromWindowsErr(err);
5831 }
5832 else
5833 Py_RETURN_NONE;
5834 }
Brian Curtineb24d742010-04-12 17:16:38 +00005835
Victor Stinner8c62be82010-05-06 00:08:46 +00005836 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5837 attempt to open and terminate the process. */
5838 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5839 if (handle == NULL) {
5840 err = GetLastError();
5841 return PyErr_SetFromWindowsErr(err);
5842 }
Brian Curtineb24d742010-04-12 17:16:38 +00005843
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 if (TerminateProcess(handle, sig) == 0) {
5845 err = GetLastError();
5846 result = PyErr_SetFromWindowsErr(err);
5847 } else {
5848 Py_INCREF(Py_None);
5849 result = Py_None;
5850 }
Brian Curtineb24d742010-04-12 17:16:38 +00005851
Victor Stinner8c62be82010-05-06 00:08:46 +00005852 CloseHandle(handle);
5853 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005854}
5855#endif /* MS_WINDOWS */
5856
Guido van Rossumc0125471996-06-28 18:55:32 +00005857#ifdef HAVE_PLOCK
5858
5859#ifdef HAVE_SYS_LOCK_H
5860#include <sys/lock.h>
5861#endif
5862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005863PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005864"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005865Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005866
Barry Warsaw53699e91996-12-10 23:23:01 +00005867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005868posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005869{
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 int op;
5871 if (!PyArg_ParseTuple(args, "i:plock", &op))
5872 return NULL;
5873 if (plock(op) == -1)
5874 return posix_error();
5875 Py_INCREF(Py_None);
5876 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005877}
5878#endif
5879
Guido van Rossumb6775db1994-08-01 11:34:53 +00005880#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005881PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005882"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005883Set the current process's user id.");
5884
Barry Warsaw53699e91996-12-10 23:23:01 +00005885static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005886posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005887{
Victor Stinner8c62be82010-05-06 00:08:46 +00005888 long uid_arg;
5889 uid_t uid;
5890 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5891 return NULL;
5892 uid = uid_arg;
5893 if (uid != uid_arg) {
5894 PyErr_SetString(PyExc_OverflowError, "user id too big");
5895 return NULL;
5896 }
5897 if (setuid(uid) < 0)
5898 return posix_error();
5899 Py_INCREF(Py_None);
5900 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005901}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005902#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005903
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005904
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005905#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005906PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005907"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005908Set the current process's effective user id.");
5909
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005910static PyObject *
5911posix_seteuid (PyObject *self, PyObject *args)
5912{
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 long euid_arg;
5914 uid_t euid;
5915 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5916 return NULL;
5917 euid = euid_arg;
5918 if (euid != euid_arg) {
5919 PyErr_SetString(PyExc_OverflowError, "user id too big");
5920 return NULL;
5921 }
5922 if (seteuid(euid) < 0) {
5923 return posix_error();
5924 } else {
5925 Py_INCREF(Py_None);
5926 return Py_None;
5927 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005928}
5929#endif /* HAVE_SETEUID */
5930
5931#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005932PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005933"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005934Set the current process's effective group id.");
5935
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005936static PyObject *
5937posix_setegid (PyObject *self, PyObject *args)
5938{
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 long egid_arg;
5940 gid_t egid;
5941 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5942 return NULL;
5943 egid = egid_arg;
5944 if (egid != egid_arg) {
5945 PyErr_SetString(PyExc_OverflowError, "group id too big");
5946 return NULL;
5947 }
5948 if (setegid(egid) < 0) {
5949 return posix_error();
5950 } else {
5951 Py_INCREF(Py_None);
5952 return Py_None;
5953 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005954}
5955#endif /* HAVE_SETEGID */
5956
5957#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005958PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005959"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005960Set the current process's real and effective user ids.");
5961
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005962static PyObject *
5963posix_setreuid (PyObject *self, PyObject *args)
5964{
Victor Stinner8c62be82010-05-06 00:08:46 +00005965 long ruid_arg, euid_arg;
5966 uid_t ruid, euid;
5967 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5968 return NULL;
5969 if (ruid_arg == -1)
5970 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5971 else
5972 ruid = ruid_arg; /* otherwise, assign from our long */
5973 if (euid_arg == -1)
5974 euid = (uid_t)-1;
5975 else
5976 euid = euid_arg;
5977 if ((euid_arg != -1 && euid != euid_arg) ||
5978 (ruid_arg != -1 && ruid != ruid_arg)) {
5979 PyErr_SetString(PyExc_OverflowError, "user id too big");
5980 return NULL;
5981 }
5982 if (setreuid(ruid, euid) < 0) {
5983 return posix_error();
5984 } else {
5985 Py_INCREF(Py_None);
5986 return Py_None;
5987 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005988}
5989#endif /* HAVE_SETREUID */
5990
5991#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005992PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005993"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005994Set the current process's real and effective group ids.");
5995
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005996static PyObject *
5997posix_setregid (PyObject *self, PyObject *args)
5998{
Victor Stinner8c62be82010-05-06 00:08:46 +00005999 long rgid_arg, egid_arg;
6000 gid_t rgid, egid;
6001 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
6002 return NULL;
6003 if (rgid_arg == -1)
6004 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
6005 else
6006 rgid = rgid_arg; /* otherwise, assign from our long */
6007 if (egid_arg == -1)
6008 egid = (gid_t)-1;
6009 else
6010 egid = egid_arg;
6011 if ((egid_arg != -1 && egid != egid_arg) ||
6012 (rgid_arg != -1 && rgid != rgid_arg)) {
6013 PyErr_SetString(PyExc_OverflowError, "group id too big");
6014 return NULL;
6015 }
6016 if (setregid(rgid, egid) < 0) {
6017 return posix_error();
6018 } else {
6019 Py_INCREF(Py_None);
6020 return Py_None;
6021 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006022}
6023#endif /* HAVE_SETREGID */
6024
Guido van Rossumb6775db1994-08-01 11:34:53 +00006025#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006026PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006027"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006028Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006029
Barry Warsaw53699e91996-12-10 23:23:01 +00006030static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006031posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006032{
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 long gid_arg;
6034 gid_t gid;
6035 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
6036 return NULL;
6037 gid = gid_arg;
6038 if (gid != gid_arg) {
6039 PyErr_SetString(PyExc_OverflowError, "group id too big");
6040 return NULL;
6041 }
6042 if (setgid(gid) < 0)
6043 return posix_error();
6044 Py_INCREF(Py_None);
6045 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006046}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006047#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006048
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006049#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006050PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006051"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006052Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006053
6054static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006055posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006056{
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 int i, len;
6058 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006059
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 if (!PySequence_Check(groups)) {
6061 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6062 return NULL;
6063 }
6064 len = PySequence_Size(groups);
6065 if (len > MAX_GROUPS) {
6066 PyErr_SetString(PyExc_ValueError, "too many groups");
6067 return NULL;
6068 }
6069 for(i = 0; i < len; i++) {
6070 PyObject *elem;
6071 elem = PySequence_GetItem(groups, i);
6072 if (!elem)
6073 return NULL;
6074 if (!PyLong_Check(elem)) {
6075 PyErr_SetString(PyExc_TypeError,
6076 "groups must be integers");
6077 Py_DECREF(elem);
6078 return NULL;
6079 } else {
6080 unsigned long x = PyLong_AsUnsignedLong(elem);
6081 if (PyErr_Occurred()) {
6082 PyErr_SetString(PyExc_TypeError,
6083 "group id too big");
6084 Py_DECREF(elem);
6085 return NULL;
6086 }
6087 grouplist[i] = x;
6088 /* read back the value to see if it fitted in gid_t */
6089 if (grouplist[i] != x) {
6090 PyErr_SetString(PyExc_TypeError,
6091 "group id too big");
6092 Py_DECREF(elem);
6093 return NULL;
6094 }
6095 }
6096 Py_DECREF(elem);
6097 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006098
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 if (setgroups(len, grouplist) < 0)
6100 return posix_error();
6101 Py_INCREF(Py_None);
6102 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006103}
6104#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006105
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006106#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6107static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006108wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006109{
Victor Stinner8c62be82010-05-06 00:08:46 +00006110 PyObject *result;
6111 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006112 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006113
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 if (pid == -1)
6115 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006116
Victor Stinner8c62be82010-05-06 00:08:46 +00006117 if (struct_rusage == NULL) {
6118 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6119 if (m == NULL)
6120 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006121 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006122 Py_DECREF(m);
6123 if (struct_rusage == NULL)
6124 return NULL;
6125 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006126
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6128 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6129 if (!result)
6130 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006131
6132#ifndef doubletime
6133#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6134#endif
6135
Victor Stinner8c62be82010-05-06 00:08:46 +00006136 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006137 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006139 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006140#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006141 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6142 SET_INT(result, 2, ru->ru_maxrss);
6143 SET_INT(result, 3, ru->ru_ixrss);
6144 SET_INT(result, 4, ru->ru_idrss);
6145 SET_INT(result, 5, ru->ru_isrss);
6146 SET_INT(result, 6, ru->ru_minflt);
6147 SET_INT(result, 7, ru->ru_majflt);
6148 SET_INT(result, 8, ru->ru_nswap);
6149 SET_INT(result, 9, ru->ru_inblock);
6150 SET_INT(result, 10, ru->ru_oublock);
6151 SET_INT(result, 11, ru->ru_msgsnd);
6152 SET_INT(result, 12, ru->ru_msgrcv);
6153 SET_INT(result, 13, ru->ru_nsignals);
6154 SET_INT(result, 14, ru->ru_nvcsw);
6155 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006156#undef SET_INT
6157
Victor Stinner8c62be82010-05-06 00:08:46 +00006158 if (PyErr_Occurred()) {
6159 Py_DECREF(result);
6160 return NULL;
6161 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006162
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006164}
6165#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6166
6167#ifdef HAVE_WAIT3
6168PyDoc_STRVAR(posix_wait3__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006169"wait3(options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006170Wait for completion of a child process.");
6171
6172static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006173posix_wait3(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006174{
Victor Stinner8c62be82010-05-06 00:08:46 +00006175 pid_t pid;
6176 int options;
6177 struct rusage ru;
6178 WAIT_TYPE status;
6179 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006180
Victor Stinner4195b5c2012-02-08 23:03:19 +01006181 if (!PyArg_ParseTuple(args, "i:wait3", &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006183
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 Py_BEGIN_ALLOW_THREADS
6185 pid = wait3(&status, options, &ru);
6186 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006187
Victor Stinner4195b5c2012-02-08 23:03:19 +01006188 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006189}
6190#endif /* HAVE_WAIT3 */
6191
6192#ifdef HAVE_WAIT4
6193PyDoc_STRVAR(posix_wait4__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006194"wait4(pid, options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006195Wait for completion of a given child process.");
6196
6197static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006198posix_wait4(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006199{
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 pid_t pid;
6201 int options;
6202 struct rusage ru;
6203 WAIT_TYPE status;
6204 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006205
Victor Stinner4195b5c2012-02-08 23:03:19 +01006206 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006208
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 Py_BEGIN_ALLOW_THREADS
6210 pid = wait4(pid, &status, options, &ru);
6211 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006212
Victor Stinner4195b5c2012-02-08 23:03:19 +01006213 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006214}
6215#endif /* HAVE_WAIT4 */
6216
Ross Lagerwall7807c352011-03-17 20:20:30 +02006217#if defined(HAVE_WAITID) && !defined(__APPLE__)
6218PyDoc_STRVAR(posix_waitid__doc__,
6219"waitid(idtype, id, options) -> waitid_result\n\n\
6220Wait for the completion of one or more child processes.\n\n\
6221idtype can be P_PID, P_PGID or P_ALL.\n\
6222id specifies the pid to wait on.\n\
6223options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6224or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6225Returns either waitid_result or None if WNOHANG is specified and there are\n\
6226no children in a waitable state.");
6227
6228static PyObject *
6229posix_waitid(PyObject *self, PyObject *args)
6230{
6231 PyObject *result;
6232 idtype_t idtype;
6233 id_t id;
6234 int options, res;
6235 siginfo_t si;
6236 si.si_pid = 0;
6237 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6238 return NULL;
6239 Py_BEGIN_ALLOW_THREADS
6240 res = waitid(idtype, id, &si, options);
6241 Py_END_ALLOW_THREADS
6242 if (res == -1)
6243 return posix_error();
6244
6245 if (si.si_pid == 0)
6246 Py_RETURN_NONE;
6247
6248 result = PyStructSequence_New(&WaitidResultType);
6249 if (!result)
6250 return NULL;
6251
6252 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6253 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6254 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6255 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6256 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6257 if (PyErr_Occurred()) {
6258 Py_DECREF(result);
6259 return NULL;
6260 }
6261
6262 return result;
6263}
6264#endif
6265
Guido van Rossumb6775db1994-08-01 11:34:53 +00006266#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006267PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006268"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006269Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006270
Barry Warsaw53699e91996-12-10 23:23:01 +00006271static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006272posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006273{
Victor Stinner8c62be82010-05-06 00:08:46 +00006274 pid_t pid;
6275 int options;
6276 WAIT_TYPE status;
6277 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006278
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6280 return NULL;
6281 Py_BEGIN_ALLOW_THREADS
6282 pid = waitpid(pid, &status, options);
6283 Py_END_ALLOW_THREADS
6284 if (pid == -1)
6285 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006286
Victor Stinner8c62be82010-05-06 00:08:46 +00006287 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006288}
6289
Tim Petersab034fa2002-02-01 11:27:43 +00006290#elif defined(HAVE_CWAIT)
6291
6292/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006293PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006294"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006295"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006296
6297static PyObject *
6298posix_waitpid(PyObject *self, PyObject *args)
6299{
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 Py_intptr_t pid;
6301 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006302
Victor Stinner8c62be82010-05-06 00:08:46 +00006303 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6304 return NULL;
6305 Py_BEGIN_ALLOW_THREADS
6306 pid = _cwait(&status, pid, options);
6307 Py_END_ALLOW_THREADS
6308 if (pid == -1)
6309 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006310
Victor Stinner8c62be82010-05-06 00:08:46 +00006311 /* shift the status left a byte so this is more like the POSIX waitpid */
6312 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006313}
6314#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006315
Guido van Rossumad0ee831995-03-01 10:34:45 +00006316#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006317PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006318"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006319Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006320
Barry Warsaw53699e91996-12-10 23:23:01 +00006321static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006322posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006323{
Victor Stinner8c62be82010-05-06 00:08:46 +00006324 pid_t pid;
6325 WAIT_TYPE status;
6326 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006327
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 Py_BEGIN_ALLOW_THREADS
6329 pid = wait(&status);
6330 Py_END_ALLOW_THREADS
6331 if (pid == -1)
6332 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006333
Victor Stinner8c62be82010-05-06 00:08:46 +00006334 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006335}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006336#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006337
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006339PyDoc_STRVAR(posix_lstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006340"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006341Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006342
Barry Warsaw53699e91996-12-10 23:23:01 +00006343static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006344posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006345{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006346#ifdef HAVE_LSTAT
Victor Stinner4195b5c2012-02-08 23:03:19 +01006347 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006348#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006349#ifdef MS_WINDOWS
Victor Stinner4195b5c2012-02-08 23:03:19 +01006350 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006351 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006352#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01006353 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006354#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006355#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006356}
6357
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006358
Guido van Rossumb6775db1994-08-01 11:34:53 +00006359#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006360PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006361"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006362Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006363
Barry Warsaw53699e91996-12-10 23:23:01 +00006364static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006365posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006366{
Victor Stinner8c62be82010-05-06 00:08:46 +00006367 PyObject* v;
6368 char buf[MAXPATHLEN];
6369 PyObject *opath;
6370 char *path;
6371 int n;
6372 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006373
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 if (!PyArg_ParseTuple(args, "O&:readlink",
6375 PyUnicode_FSConverter, &opath))
6376 return NULL;
6377 path = PyBytes_AsString(opath);
6378 v = PySequence_GetItem(args, 0);
6379 if (v == NULL) {
6380 Py_DECREF(opath);
6381 return NULL;
6382 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006383
Victor Stinner8c62be82010-05-06 00:08:46 +00006384 if (PyUnicode_Check(v)) {
6385 arg_is_unicode = 1;
6386 }
6387 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006388
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 Py_BEGIN_ALLOW_THREADS
6390 n = readlink(path, buf, (int) sizeof buf);
6391 Py_END_ALLOW_THREADS
6392 if (n < 0)
6393 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006394
Victor Stinner8c62be82010-05-06 00:08:46 +00006395 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006396 if (arg_is_unicode)
6397 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6398 else
6399 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006400}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006401#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006402
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006403
Brian Curtin52173d42010-12-02 18:29:18 +00006404#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006405PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006406"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006407Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006408
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006409static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006410posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006411{
Victor Stinner8c62be82010-05-06 00:08:46 +00006412 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006413}
6414#endif /* HAVE_SYMLINK */
6415
Brian Curtind40e6f72010-07-08 21:39:08 +00006416#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6417
6418PyDoc_STRVAR(win_readlink__doc__,
6419"readlink(path) -> path\n\n\
6420Return a string representing the path to which the symbolic link points.");
6421
Brian Curtind40e6f72010-07-08 21:39:08 +00006422/* Windows readlink implementation */
6423static PyObject *
6424win_readlink(PyObject *self, PyObject *args)
6425{
6426 wchar_t *path;
6427 DWORD n_bytes_returned;
6428 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006429 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00006430 HANDLE reparse_point_handle;
6431
6432 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6433 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6434 wchar_t *print_name;
6435
6436 if (!PyArg_ParseTuple(args,
Victor Stinnereb5657a2011-09-30 01:44:27 +02006437 "U:readlink",
6438 &po))
6439 return NULL;
6440 path = PyUnicode_AsUnicode(po);
6441 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006442 return NULL;
6443
6444 /* First get a handle to the reparse point */
6445 Py_BEGIN_ALLOW_THREADS
6446 reparse_point_handle = CreateFileW(
6447 path,
6448 0,
6449 0,
6450 0,
6451 OPEN_EXISTING,
6452 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6453 0);
6454 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006455
Brian Curtind40e6f72010-07-08 21:39:08 +00006456 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006457 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006458
Brian Curtind40e6f72010-07-08 21:39:08 +00006459 Py_BEGIN_ALLOW_THREADS
6460 /* New call DeviceIoControl to read the reparse point */
6461 io_result = DeviceIoControl(
6462 reparse_point_handle,
6463 FSCTL_GET_REPARSE_POINT,
6464 0, 0, /* in buffer */
6465 target_buffer, sizeof(target_buffer),
6466 &n_bytes_returned,
6467 0 /* we're not using OVERLAPPED_IO */
6468 );
6469 CloseHandle(reparse_point_handle);
6470 Py_END_ALLOW_THREADS
6471
6472 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006473 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00006474
6475 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6476 {
6477 PyErr_SetString(PyExc_ValueError,
6478 "not a symbolic link");
6479 return NULL;
6480 }
Brian Curtin74e45612010-07-09 15:58:59 +00006481 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6482 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6483
6484 result = PyUnicode_FromWideChar(print_name,
6485 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006486 return result;
6487}
6488
6489#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6490
Brian Curtin52173d42010-12-02 18:29:18 +00006491#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006492
6493/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6494static int has_CreateSymbolicLinkW = 0;
6495static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6496static int
6497check_CreateSymbolicLinkW()
6498{
6499 HINSTANCE hKernel32;
6500 /* only recheck */
6501 if (has_CreateSymbolicLinkW)
6502 return has_CreateSymbolicLinkW;
Martin v. Löwis50590f12012-01-14 17:54:09 +01006503 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006504 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6505 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006506 if (Py_CreateSymbolicLinkW)
6507 has_CreateSymbolicLinkW = 1;
6508 return has_CreateSymbolicLinkW;
6509}
6510
6511PyDoc_STRVAR(win_symlink__doc__,
6512"symlink(src, dst, target_is_directory=False)\n\n\
6513Create a symbolic link pointing to src named dst.\n\
6514target_is_directory is required if the target is to be interpreted as\n\
6515a directory.\n\
6516This function requires Windows 6.0 or greater, and raises a\n\
6517NotImplementedError otherwise.");
6518
6519static PyObject *
6520win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6521{
6522 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006523 PyObject *osrc, *odest;
6524 PyObject *usrc = NULL, *udest = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006525 wchar_t *wsrc, *wdest;
Brian Curtind40e6f72010-07-08 21:39:08 +00006526 int target_is_directory = 0;
6527 DWORD res;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006528
Brian Curtind40e6f72010-07-08 21:39:08 +00006529 if (!check_CreateSymbolicLinkW())
6530 {
6531 /* raise NotImplementedError */
6532 return PyErr_Format(PyExc_NotImplementedError,
6533 "CreateSymbolicLinkW not found");
6534 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006535 if (!PyArg_ParseTupleAndKeywords(
6536 args, kwargs, "OO|i:symlink", kwlist,
6537 &osrc, &odest, &target_is_directory))
Brian Curtind40e6f72010-07-08 21:39:08 +00006538 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006539
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006540 usrc = win32_decode_filename(osrc);
6541 if (!usrc)
6542 return NULL;
6543 udest = win32_decode_filename(odest);
6544 if (!udest)
6545 goto error;
6546
Brian Curtin3b4499c2010-12-28 14:31:47 +00006547 if (win32_can_symlink == 0)
6548 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6549
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006550 wsrc = PyUnicode_AsUnicode(usrc);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006551 if (wsrc == NULL)
6552 goto error;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006553 wdest = PyUnicode_AsUnicode(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006554 if (wsrc == NULL)
6555 goto error;
6556
Brian Curtind40e6f72010-07-08 21:39:08 +00006557 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006558 res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
Brian Curtind40e6f72010-07-08 21:39:08 +00006559 Py_END_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006560
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006561 Py_DECREF(usrc);
6562 Py_DECREF(udest);
Brian Curtind40e6f72010-07-08 21:39:08 +00006563 if (!res)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006564 return win32_error_object("symlink", osrc);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006565
Brian Curtind40e6f72010-07-08 21:39:08 +00006566 Py_INCREF(Py_None);
6567 return Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006568
6569error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006570 Py_XDECREF(usrc);
6571 Py_XDECREF(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006572 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00006573}
Brian Curtin52173d42010-12-02 18:29:18 +00006574#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006575
6576#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006577#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6578static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006579system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006580{
6581 ULONG value = 0;
6582
6583 Py_BEGIN_ALLOW_THREADS
6584 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6585 Py_END_ALLOW_THREADS
6586
6587 return value;
6588}
6589
6590static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006591posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006592{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006593 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006594 return Py_BuildValue("ddddd",
6595 (double)0 /* t.tms_utime / HZ */,
6596 (double)0 /* t.tms_stime / HZ */,
6597 (double)0 /* t.tms_cutime / HZ */,
6598 (double)0 /* t.tms_cstime / HZ */,
6599 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006600}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006601#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006602#define NEED_TICKS_PER_SECOND
6603static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006605posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006606{
Victor Stinner8c62be82010-05-06 00:08:46 +00006607 struct tms t;
6608 clock_t c;
6609 errno = 0;
6610 c = times(&t);
6611 if (c == (clock_t) -1)
6612 return posix_error();
6613 return Py_BuildValue("ddddd",
6614 (double)t.tms_utime / ticks_per_second,
6615 (double)t.tms_stime / ticks_per_second,
6616 (double)t.tms_cutime / ticks_per_second,
6617 (double)t.tms_cstime / ticks_per_second,
6618 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006619}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006620#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006621#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006622
6623
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006624#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006625#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006626static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006627posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006628{
Victor Stinner8c62be82010-05-06 00:08:46 +00006629 FILETIME create, exit, kernel, user;
6630 HANDLE hProc;
6631 hProc = GetCurrentProcess();
6632 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6633 /* The fields of a FILETIME structure are the hi and lo part
6634 of a 64-bit value expressed in 100 nanosecond units.
6635 1e7 is one second in such units; 1e-7 the inverse.
6636 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6637 */
6638 return Py_BuildValue(
6639 "ddddd",
6640 (double)(user.dwHighDateTime*429.4967296 +
6641 user.dwLowDateTime*1e-7),
6642 (double)(kernel.dwHighDateTime*429.4967296 +
6643 kernel.dwLowDateTime*1e-7),
6644 (double)0,
6645 (double)0,
6646 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006647}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006648#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006649
6650#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006651PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006652"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006653Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006654#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006655
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006656
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006657#ifdef HAVE_GETSID
6658PyDoc_STRVAR(posix_getsid__doc__,
6659"getsid(pid) -> sid\n\n\
6660Call the system call getsid().");
6661
6662static PyObject *
6663posix_getsid(PyObject *self, PyObject *args)
6664{
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 pid_t pid;
6666 int sid;
6667 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6668 return NULL;
6669 sid = getsid(pid);
6670 if (sid < 0)
6671 return posix_error();
6672 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006673}
6674#endif /* HAVE_GETSID */
6675
6676
Guido van Rossumb6775db1994-08-01 11:34:53 +00006677#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006678PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006679"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006680Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006681
Barry Warsaw53699e91996-12-10 23:23:01 +00006682static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006683posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006684{
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 if (setsid() < 0)
6686 return posix_error();
6687 Py_INCREF(Py_None);
6688 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006689}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006690#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006691
Guido van Rossumb6775db1994-08-01 11:34:53 +00006692#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006693PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006694"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006695Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006696
Barry Warsaw53699e91996-12-10 23:23:01 +00006697static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006698posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006699{
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 pid_t pid;
6701 int pgrp;
6702 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6703 return NULL;
6704 if (setpgid(pid, pgrp) < 0)
6705 return posix_error();
6706 Py_INCREF(Py_None);
6707 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006708}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006709#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006711
Guido van Rossumb6775db1994-08-01 11:34:53 +00006712#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006713PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006714"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006715Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006716
Barry Warsaw53699e91996-12-10 23:23:01 +00006717static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006718posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006719{
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 int fd;
6721 pid_t pgid;
6722 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6723 return NULL;
6724 pgid = tcgetpgrp(fd);
6725 if (pgid < 0)
6726 return posix_error();
6727 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006728}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006729#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006730
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006731
Guido van Rossumb6775db1994-08-01 11:34:53 +00006732#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006733PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006734"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006735Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006736
Barry Warsaw53699e91996-12-10 23:23:01 +00006737static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006738posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006739{
Victor Stinner8c62be82010-05-06 00:08:46 +00006740 int fd;
6741 pid_t pgid;
6742 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6743 return NULL;
6744 if (tcsetpgrp(fd, pgid) < 0)
6745 return posix_error();
6746 Py_INCREF(Py_None);
6747 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006748}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006749#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006750
Guido van Rossum687dd131993-05-17 08:34:16 +00006751/* Functions acting on file descriptors */
6752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006753PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006754"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006755Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006756
Barry Warsaw53699e91996-12-10 23:23:01 +00006757static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006758posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006759{
Victor Stinner8c62be82010-05-06 00:08:46 +00006760 PyObject *ofile;
6761 char *file;
6762 int flag;
6763 int mode = 0777;
6764 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006765
6766#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006767 PyObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006768 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02006769 wchar_t *wpath = PyUnicode_AsUnicode(po);
6770 if (wpath == NULL)
6771 return NULL;
6772
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006774 fd = _wopen(wpath, flag, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00006775 Py_END_ALLOW_THREADS
6776 if (fd < 0)
6777 return posix_error();
6778 return PyLong_FromLong((long)fd);
6779 }
6780 /* Drop the argument parsing error as narrow strings
6781 are also valid. */
6782 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006783#endif
6784
Victor Stinner26de69d2011-06-17 15:15:38 +02006785 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006786 PyUnicode_FSConverter, &ofile,
6787 &flag, &mode))
6788 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006789#ifdef MS_WINDOWS
6790 if (win32_warn_bytes_api()) {
6791 Py_DECREF(ofile);
6792 return NULL;
6793 }
6794#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006795 file = PyBytes_AsString(ofile);
6796 Py_BEGIN_ALLOW_THREADS
6797 fd = open(file, flag, mode);
6798 Py_END_ALLOW_THREADS
6799 if (fd < 0)
6800 return posix_error_with_allocated_filename(ofile);
6801 Py_DECREF(ofile);
6802 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006803}
6804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006805
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006806PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006807"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006808Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006809
Barry Warsaw53699e91996-12-10 23:23:01 +00006810static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006811posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006812{
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 int fd, res;
6814 if (!PyArg_ParseTuple(args, "i:close", &fd))
6815 return NULL;
6816 if (!_PyVerify_fd(fd))
6817 return posix_error();
6818 Py_BEGIN_ALLOW_THREADS
6819 res = close(fd);
6820 Py_END_ALLOW_THREADS
6821 if (res < 0)
6822 return posix_error();
6823 Py_INCREF(Py_None);
6824 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006825}
6826
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006827
Victor Stinner8c62be82010-05-06 00:08:46 +00006828PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006829"closerange(fd_low, fd_high)\n\n\
6830Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6831
6832static PyObject *
6833posix_closerange(PyObject *self, PyObject *args)
6834{
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 int fd_from, fd_to, i;
6836 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6837 return NULL;
6838 Py_BEGIN_ALLOW_THREADS
6839 for (i = fd_from; i < fd_to; i++)
6840 if (_PyVerify_fd(i))
6841 close(i);
6842 Py_END_ALLOW_THREADS
6843 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006844}
6845
6846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006847PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006848"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006849Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006850
Barry Warsaw53699e91996-12-10 23:23:01 +00006851static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006852posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006853{
Victor Stinner8c62be82010-05-06 00:08:46 +00006854 int fd;
6855 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6856 return NULL;
6857 if (!_PyVerify_fd(fd))
6858 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 if (fd < 0)
6861 return posix_error();
6862 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006863}
6864
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006865
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006866PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006867"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006868Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006869
Barry Warsaw53699e91996-12-10 23:23:01 +00006870static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006871posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006872{
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 int fd, fd2, res;
6874 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6875 return NULL;
6876 if (!_PyVerify_fd_dup2(fd, fd2))
6877 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006878 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 if (res < 0)
6880 return posix_error();
6881 Py_INCREF(Py_None);
6882 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006883}
6884
Ross Lagerwall7807c352011-03-17 20:20:30 +02006885#ifdef HAVE_LOCKF
6886PyDoc_STRVAR(posix_lockf__doc__,
6887"lockf(fd, cmd, len)\n\n\
6888Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6889fd is an open file descriptor.\n\
6890cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6891F_TEST.\n\
6892len specifies the section of the file to lock.");
6893
6894static PyObject *
6895posix_lockf(PyObject *self, PyObject *args)
6896{
6897 int fd, cmd, res;
6898 off_t len;
6899 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6900 &fd, &cmd, _parse_off_t, &len))
6901 return NULL;
6902
6903 Py_BEGIN_ALLOW_THREADS
6904 res = lockf(fd, cmd, len);
6905 Py_END_ALLOW_THREADS
6906
6907 if (res < 0)
6908 return posix_error();
6909
6910 Py_RETURN_NONE;
6911}
6912#endif
6913
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006915PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006916"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01006917Set the current position of a file descriptor.\n\
6918Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006919
Barry Warsaw53699e91996-12-10 23:23:01 +00006920static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006921posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006922{
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006924#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006926#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006928#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006929 PyObject *posobj;
6930 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006932#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6934 switch (how) {
6935 case 0: how = SEEK_SET; break;
6936 case 1: how = SEEK_CUR; break;
6937 case 2: how = SEEK_END; break;
6938 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006939#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006940
Ross Lagerwall8e749672011-03-17 21:54:07 +02006941#if !defined(HAVE_LARGEFILE_SUPPORT)
6942 pos = PyLong_AsLong(posobj);
6943#else
6944 pos = PyLong_AsLongLong(posobj);
6945#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 if (PyErr_Occurred())
6947 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006948
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 if (!_PyVerify_fd(fd))
6950 return posix_error();
6951 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006952#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006953 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006954#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006956#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 Py_END_ALLOW_THREADS
6958 if (res < 0)
6959 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006960
6961#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006962 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006963#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006965#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006966}
6967
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006969PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006970"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006971Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006972
Barry Warsaw53699e91996-12-10 23:23:01 +00006973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006974posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006975{
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 int fd, size;
6977 Py_ssize_t n;
6978 PyObject *buffer;
6979 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6980 return NULL;
6981 if (size < 0) {
6982 errno = EINVAL;
6983 return posix_error();
6984 }
6985 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6986 if (buffer == NULL)
6987 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006988 if (!_PyVerify_fd(fd)) {
6989 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006991 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006992 Py_BEGIN_ALLOW_THREADS
6993 n = read(fd, PyBytes_AS_STRING(buffer), size);
6994 Py_END_ALLOW_THREADS
6995 if (n < 0) {
6996 Py_DECREF(buffer);
6997 return posix_error();
6998 }
6999 if (n != size)
7000 _PyBytes_Resize(&buffer, n);
7001 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007002}
7003
Ross Lagerwall7807c352011-03-17 20:20:30 +02007004#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7005 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007006static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007007iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7008{
7009 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007010 Py_ssize_t blen, total = 0;
7011
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007012 *iov = PyMem_New(struct iovec, cnt);
7013 if (*iov == NULL) {
7014 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007015 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007016 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007017
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007018 *buf = PyMem_New(Py_buffer, cnt);
7019 if (*buf == NULL) {
7020 PyMem_Del(*iov);
7021 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007022 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007023 }
7024
7025 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007026 PyObject *item = PySequence_GetItem(seq, i);
7027 if (item == NULL)
7028 goto fail;
7029 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7030 Py_DECREF(item);
7031 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007032 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007033 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007034 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007035 blen = (*buf)[i].len;
7036 (*iov)[i].iov_len = blen;
7037 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007038 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007039 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007040
7041fail:
7042 PyMem_Del(*iov);
7043 for (j = 0; j < i; j++) {
7044 PyBuffer_Release(&(*buf)[j]);
7045 }
7046 PyMem_Del(*buf);
7047 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007048}
7049
7050static void
7051iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7052{
7053 int i;
7054 PyMem_Del(iov);
7055 for (i = 0; i < cnt; i++) {
7056 PyBuffer_Release(&buf[i]);
7057 }
7058 PyMem_Del(buf);
7059}
7060#endif
7061
Ross Lagerwall7807c352011-03-17 20:20:30 +02007062#ifdef HAVE_READV
7063PyDoc_STRVAR(posix_readv__doc__,
7064"readv(fd, buffers) -> bytesread\n\n\
7065Read from a file descriptor into a number of writable buffers. buffers\n\
7066is an arbitrary sequence of writable buffers.\n\
7067Returns the total number of bytes read.");
7068
7069static PyObject *
7070posix_readv(PyObject *self, PyObject *args)
7071{
7072 int fd, cnt;
7073 Py_ssize_t n;
7074 PyObject *seq;
7075 struct iovec *iov;
7076 Py_buffer *buf;
7077
7078 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7079 return NULL;
7080 if (!PySequence_Check(seq)) {
7081 PyErr_SetString(PyExc_TypeError,
7082 "readv() arg 2 must be a sequence");
7083 return NULL;
7084 }
7085 cnt = PySequence_Size(seq);
7086
7087 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7088 return NULL;
7089
7090 Py_BEGIN_ALLOW_THREADS
7091 n = readv(fd, iov, cnt);
7092 Py_END_ALLOW_THREADS
7093
7094 iov_cleanup(iov, buf, cnt);
7095 return PyLong_FromSsize_t(n);
7096}
7097#endif
7098
7099#ifdef HAVE_PREAD
7100PyDoc_STRVAR(posix_pread__doc__,
7101"pread(fd, buffersize, offset) -> string\n\n\
7102Read from a file descriptor, fd, at a position of offset. It will read up\n\
7103to buffersize number of bytes. The file offset remains unchanged.");
7104
7105static PyObject *
7106posix_pread(PyObject *self, PyObject *args)
7107{
7108 int fd, size;
7109 off_t offset;
7110 Py_ssize_t n;
7111 PyObject *buffer;
7112 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7113 return NULL;
7114
7115 if (size < 0) {
7116 errno = EINVAL;
7117 return posix_error();
7118 }
7119 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7120 if (buffer == NULL)
7121 return NULL;
7122 if (!_PyVerify_fd(fd)) {
7123 Py_DECREF(buffer);
7124 return posix_error();
7125 }
7126 Py_BEGIN_ALLOW_THREADS
7127 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7128 Py_END_ALLOW_THREADS
7129 if (n < 0) {
7130 Py_DECREF(buffer);
7131 return posix_error();
7132 }
7133 if (n != size)
7134 _PyBytes_Resize(&buffer, n);
7135 return buffer;
7136}
7137#endif
7138
7139PyDoc_STRVAR(posix_write__doc__,
7140"write(fd, string) -> byteswritten\n\n\
7141Write a string to a file descriptor.");
7142
7143static PyObject *
7144posix_write(PyObject *self, PyObject *args)
7145{
7146 Py_buffer pbuf;
7147 int fd;
7148 Py_ssize_t size, len;
7149
7150 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7151 return NULL;
7152 if (!_PyVerify_fd(fd)) {
7153 PyBuffer_Release(&pbuf);
7154 return posix_error();
7155 }
7156 len = pbuf.len;
7157 Py_BEGIN_ALLOW_THREADS
7158#if defined(MS_WIN64) || defined(MS_WINDOWS)
7159 if (len > INT_MAX)
7160 len = INT_MAX;
7161 size = write(fd, pbuf.buf, (int)len);
7162#else
7163 size = write(fd, pbuf.buf, len);
7164#endif
7165 Py_END_ALLOW_THREADS
7166 PyBuffer_Release(&pbuf);
7167 if (size < 0)
7168 return posix_error();
7169 return PyLong_FromSsize_t(size);
7170}
7171
7172#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007173PyDoc_STRVAR(posix_sendfile__doc__,
7174"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7175sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7176 -> byteswritten\n\
7177Copy nbytes bytes from file descriptor in to file descriptor out.");
7178
7179static PyObject *
7180posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7181{
7182 int in, out;
7183 Py_ssize_t ret;
7184 off_t offset;
7185
7186#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7187#ifndef __APPLE__
7188 Py_ssize_t len;
7189#endif
7190 PyObject *headers = NULL, *trailers = NULL;
7191 Py_buffer *hbuf, *tbuf;
7192 off_t sbytes;
7193 struct sf_hdtr sf;
7194 int flags = 0;
7195 sf.headers = NULL;
7196 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007197 static char *keywords[] = {"out", "in",
7198 "offset", "count",
7199 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007200
7201#ifdef __APPLE__
7202 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007203 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007204#else
7205 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007206 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007207#endif
7208 &headers, &trailers, &flags))
7209 return NULL;
7210 if (headers != NULL) {
7211 if (!PySequence_Check(headers)) {
7212 PyErr_SetString(PyExc_TypeError,
7213 "sendfile() headers must be a sequence or None");
7214 return NULL;
7215 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007216 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007217 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007218 if (sf.hdr_cnt > 0 &&
7219 !(i = iov_setup(&(sf.headers), &hbuf,
7220 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007221 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007222#ifdef __APPLE__
7223 sbytes += i;
7224#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007225 }
7226 }
7227 if (trailers != NULL) {
7228 if (!PySequence_Check(trailers)) {
7229 PyErr_SetString(PyExc_TypeError,
7230 "sendfile() trailers must be a sequence or None");
7231 return NULL;
7232 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007233 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007234 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007235 if (sf.trl_cnt > 0 &&
7236 !(i = iov_setup(&(sf.trailers), &tbuf,
7237 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007238 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007239#ifdef __APPLE__
7240 sbytes += i;
7241#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007242 }
7243 }
7244
7245 Py_BEGIN_ALLOW_THREADS
7246#ifdef __APPLE__
7247 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7248#else
7249 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7250#endif
7251 Py_END_ALLOW_THREADS
7252
7253 if (sf.headers != NULL)
7254 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7255 if (sf.trailers != NULL)
7256 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7257
7258 if (ret < 0) {
7259 if ((errno == EAGAIN) || (errno == EBUSY)) {
7260 if (sbytes != 0) {
7261 // some data has been sent
7262 goto done;
7263 }
7264 else {
7265 // no data has been sent; upper application is supposed
7266 // to retry on EAGAIN or EBUSY
7267 return posix_error();
7268 }
7269 }
7270 return posix_error();
7271 }
7272 goto done;
7273
7274done:
7275 #if !defined(HAVE_LARGEFILE_SUPPORT)
7276 return Py_BuildValue("l", sbytes);
7277 #else
7278 return Py_BuildValue("L", sbytes);
7279 #endif
7280
7281#else
7282 Py_ssize_t count;
7283 PyObject *offobj;
7284 static char *keywords[] = {"out", "in",
7285 "offset", "count", NULL};
7286 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7287 keywords, &out, &in, &offobj, &count))
7288 return NULL;
7289#ifdef linux
7290 if (offobj == Py_None) {
7291 Py_BEGIN_ALLOW_THREADS
7292 ret = sendfile(out, in, NULL, count);
7293 Py_END_ALLOW_THREADS
7294 if (ret < 0)
7295 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007296 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007297 }
7298#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007299 if (!_parse_off_t(offobj, &offset))
7300 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007301 Py_BEGIN_ALLOW_THREADS
7302 ret = sendfile(out, in, &offset, count);
7303 Py_END_ALLOW_THREADS
7304 if (ret < 0)
7305 return posix_error();
7306 return Py_BuildValue("n", ret);
7307#endif
7308}
7309#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007311PyDoc_STRVAR(posix_fstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007312"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007313Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007314
Barry Warsaw53699e91996-12-10 23:23:01 +00007315static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007316posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007317{
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 int fd;
7319 STRUCT_STAT st;
7320 int res;
Victor Stinner4195b5c2012-02-08 23:03:19 +01007321 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007322 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007323#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 /* on OpenVMS we must ensure that all bytes are written to the file */
7325 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007326#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 if (!_PyVerify_fd(fd))
7328 return posix_error();
7329 Py_BEGIN_ALLOW_THREADS
7330 res = FSTAT(fd, &st);
7331 Py_END_ALLOW_THREADS
7332 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007333#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007334 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007335#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007337#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007338 }
Tim Peters5aa91602002-01-30 05:46:57 +00007339
Victor Stinner4195b5c2012-02-08 23:03:19 +01007340 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007341}
7342
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007343PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007344"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007345Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007346connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007347
7348static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007349posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007350{
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 int fd;
7352 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7353 return NULL;
7354 if (!_PyVerify_fd(fd))
7355 return PyBool_FromLong(0);
7356 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007357}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007358
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007359#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007360PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007361"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007362Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007363
Barry Warsaw53699e91996-12-10 23:23:01 +00007364static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007365posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007366{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007367#if defined(PYOS_OS2)
7368 HFILE read, write;
7369 APIRET rc;
7370
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007371 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007372 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007373 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007374
7375 return Py_BuildValue("(ii)", read, write);
7376#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007377#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 int fds[2];
7379 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 if (res != 0)
7382 return posix_error();
7383 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007384#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 HANDLE read, write;
7386 int read_fd, write_fd;
7387 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007388 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007389 if (!ok)
7390 return win32_error("CreatePipe", NULL);
7391 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7392 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7393 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007394#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007395#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007396}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007397#endif /* HAVE_PIPE */
7398
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007399#ifdef HAVE_PIPE2
7400PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007401"pipe2(flags) -> (read_end, write_end)\n\n\
7402Create a pipe with flags set atomically.\n\
7403flags can be constructed by ORing together one or more of these values:\n\
7404O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007405");
7406
7407static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007408posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007409{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007410 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007411 int fds[2];
7412 int res;
7413
Charles-François Natali368f34b2011-06-06 19:49:47 +02007414 flags = PyLong_AsLong(arg);
7415 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007416 return NULL;
7417
7418 res = pipe2(fds, flags);
7419 if (res != 0)
7420 return posix_error();
7421 return Py_BuildValue("(ii)", fds[0], fds[1]);
7422}
7423#endif /* HAVE_PIPE2 */
7424
Ross Lagerwall7807c352011-03-17 20:20:30 +02007425#ifdef HAVE_WRITEV
7426PyDoc_STRVAR(posix_writev__doc__,
7427"writev(fd, buffers) -> byteswritten\n\n\
7428Write the contents of buffers to a file descriptor, where buffers is an\n\
7429arbitrary sequence of buffers.\n\
7430Returns the total bytes written.");
7431
7432static PyObject *
7433posix_writev(PyObject *self, PyObject *args)
7434{
7435 int fd, cnt;
7436 Py_ssize_t res;
7437 PyObject *seq;
7438 struct iovec *iov;
7439 Py_buffer *buf;
7440 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7441 return NULL;
7442 if (!PySequence_Check(seq)) {
7443 PyErr_SetString(PyExc_TypeError,
7444 "writev() arg 2 must be a sequence");
7445 return NULL;
7446 }
7447 cnt = PySequence_Size(seq);
7448
7449 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7450 return NULL;
7451 }
7452
7453 Py_BEGIN_ALLOW_THREADS
7454 res = writev(fd, iov, cnt);
7455 Py_END_ALLOW_THREADS
7456
7457 iov_cleanup(iov, buf, cnt);
7458 return PyLong_FromSsize_t(res);
7459}
7460#endif
7461
7462#ifdef HAVE_PWRITE
7463PyDoc_STRVAR(posix_pwrite__doc__,
7464"pwrite(fd, string, offset) -> byteswritten\n\n\
7465Write string to a file descriptor, fd, from offset, leaving the file\n\
7466offset unchanged.");
7467
7468static PyObject *
7469posix_pwrite(PyObject *self, PyObject *args)
7470{
7471 Py_buffer pbuf;
7472 int fd;
7473 off_t offset;
7474 Py_ssize_t size;
7475
7476 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7477 return NULL;
7478
7479 if (!_PyVerify_fd(fd)) {
7480 PyBuffer_Release(&pbuf);
7481 return posix_error();
7482 }
7483 Py_BEGIN_ALLOW_THREADS
7484 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7485 Py_END_ALLOW_THREADS
7486 PyBuffer_Release(&pbuf);
7487 if (size < 0)
7488 return posix_error();
7489 return PyLong_FromSsize_t(size);
7490}
7491#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007492
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007493#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007494PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007495"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007496Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007497
Barry Warsaw53699e91996-12-10 23:23:01 +00007498static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007499posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007500{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007501 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007502 char *filename;
7503 int mode = 0666;
7504 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007505 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7506 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007507 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007508 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007509 Py_BEGIN_ALLOW_THREADS
7510 res = mkfifo(filename, mode);
7511 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007512 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007513 if (res < 0)
7514 return posix_error();
7515 Py_INCREF(Py_None);
7516 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007517}
7518#endif
7519
7520
Neal Norwitz11690112002-07-30 01:08:28 +00007521#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007522PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007523"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007524Create a filesystem node (file, device special file or named pipe)\n\
7525named filename. mode specifies both the permissions to use and the\n\
7526type of node to be created, being combined (bitwise OR) with one of\n\
7527S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007528device defines the newly created device special file (probably using\n\
7529os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007530
7531
7532static PyObject *
7533posix_mknod(PyObject *self, PyObject *args)
7534{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007535 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007536 char *filename;
7537 int mode = 0600;
7538 int device = 0;
7539 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007540 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7541 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007542 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007543 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 Py_BEGIN_ALLOW_THREADS
7545 res = mknod(filename, mode, device);
7546 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007547 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007548 if (res < 0)
7549 return posix_error();
7550 Py_INCREF(Py_None);
7551 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007552}
7553#endif
7554
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007555#ifdef HAVE_DEVICE_MACROS
7556PyDoc_STRVAR(posix_major__doc__,
7557"major(device) -> major number\n\
7558Extracts a device major number from a raw device number.");
7559
7560static PyObject *
7561posix_major(PyObject *self, PyObject *args)
7562{
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 int device;
7564 if (!PyArg_ParseTuple(args, "i:major", &device))
7565 return NULL;
7566 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007567}
7568
7569PyDoc_STRVAR(posix_minor__doc__,
7570"minor(device) -> minor number\n\
7571Extracts a device minor number from a raw device number.");
7572
7573static PyObject *
7574posix_minor(PyObject *self, PyObject *args)
7575{
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 int device;
7577 if (!PyArg_ParseTuple(args, "i:minor", &device))
7578 return NULL;
7579 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007580}
7581
7582PyDoc_STRVAR(posix_makedev__doc__,
7583"makedev(major, minor) -> device number\n\
7584Composes a raw device number from the major and minor device numbers.");
7585
7586static PyObject *
7587posix_makedev(PyObject *self, PyObject *args)
7588{
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 int major, minor;
7590 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7591 return NULL;
7592 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007593}
7594#endif /* device macros */
7595
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007596
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007597#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007598PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007599"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007600Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007601
Barry Warsaw53699e91996-12-10 23:23:01 +00007602static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007603posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007604{
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 int fd;
7606 off_t length;
7607 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007608
Ross Lagerwall7807c352011-03-17 20:20:30 +02007609 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007611
Victor Stinner8c62be82010-05-06 00:08:46 +00007612 Py_BEGIN_ALLOW_THREADS
7613 res = ftruncate(fd, length);
7614 Py_END_ALLOW_THREADS
7615 if (res < 0)
7616 return posix_error();
7617 Py_INCREF(Py_None);
7618 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007619}
7620#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007621
Ross Lagerwall7807c352011-03-17 20:20:30 +02007622#ifdef HAVE_TRUNCATE
7623PyDoc_STRVAR(posix_truncate__doc__,
7624"truncate(path, length)\n\n\
7625Truncate the file given by path to length bytes.");
7626
7627static PyObject *
7628posix_truncate(PyObject *self, PyObject *args)
7629{
7630 PyObject *opath;
7631 const char *path;
7632 off_t length;
7633 int res;
7634
7635 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7636 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7637 return NULL;
7638 path = PyBytes_AsString(opath);
7639
7640 Py_BEGIN_ALLOW_THREADS
7641 res = truncate(path, length);
7642 Py_END_ALLOW_THREADS
7643 Py_DECREF(opath);
7644 if (res < 0)
7645 return posix_error();
7646 Py_RETURN_NONE;
7647}
7648#endif
7649
7650#ifdef HAVE_POSIX_FALLOCATE
7651PyDoc_STRVAR(posix_posix_fallocate__doc__,
7652"posix_fallocate(fd, offset, len)\n\n\
7653Ensures that enough disk space is allocated for the file specified by fd\n\
7654starting from offset and continuing for len bytes.");
7655
7656static PyObject *
7657posix_posix_fallocate(PyObject *self, PyObject *args)
7658{
7659 off_t len, offset;
7660 int res, fd;
7661
7662 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7663 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7664 return NULL;
7665
7666 Py_BEGIN_ALLOW_THREADS
7667 res = posix_fallocate(fd, offset, len);
7668 Py_END_ALLOW_THREADS
7669 if (res != 0) {
7670 errno = res;
7671 return posix_error();
7672 }
7673 Py_RETURN_NONE;
7674}
7675#endif
7676
7677#ifdef HAVE_POSIX_FADVISE
7678PyDoc_STRVAR(posix_posix_fadvise__doc__,
7679"posix_fadvise(fd, offset, len, advice)\n\n\
7680Announces an intention to access data in a specific pattern thus allowing\n\
7681the kernel to make optimizations.\n\
7682The advice applies to the region of the file specified by fd starting at\n\
7683offset and continuing for len bytes.\n\
7684advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7685POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7686POSIX_FADV_DONTNEED.");
7687
7688static PyObject *
7689posix_posix_fadvise(PyObject *self, PyObject *args)
7690{
7691 off_t len, offset;
7692 int res, fd, advice;
7693
7694 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7695 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7696 return NULL;
7697
7698 Py_BEGIN_ALLOW_THREADS
7699 res = posix_fadvise(fd, offset, len, advice);
7700 Py_END_ALLOW_THREADS
7701 if (res != 0) {
7702 errno = res;
7703 return posix_error();
7704 }
7705 Py_RETURN_NONE;
7706}
7707#endif
7708
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007709#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007710PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007711"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007712Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007713
Fred Drake762e2061999-08-26 17:23:54 +00007714/* Save putenv() parameters as values here, so we can collect them when they
7715 * get re-set with another call for the same key. */
7716static PyObject *posix_putenv_garbage;
7717
Tim Peters5aa91602002-01-30 05:46:57 +00007718static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007719posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007720{
Victor Stinner84ae1182010-05-06 22:05:07 +00007721 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007722#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01007723 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007724 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01007725
Victor Stinner8c62be82010-05-06 00:08:46 +00007726 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007727 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01007728 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007730
Victor Stinner65170952011-11-22 22:16:17 +01007731 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00007732 if (newstr == NULL) {
7733 PyErr_NoMemory();
7734 goto error;
7735 }
Victor Stinner65170952011-11-22 22:16:17 +01007736 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
7737 PyErr_Format(PyExc_ValueError,
7738 "the environment variable is longer than %u characters",
7739 _MAX_ENV);
7740 goto error;
7741 }
7742
Victor Stinner8c62be82010-05-06 00:08:46 +00007743 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02007744 if (newenv == NULL)
7745 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007747 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007748 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007750#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007751 PyObject *os1, *os2;
7752 char *s1, *s2;
7753 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007754
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007755 if (!PyArg_ParseTuple(args,
7756 "O&O&:putenv",
7757 PyUnicode_FSConverter, &os1,
7758 PyUnicode_FSConverter, &os2))
7759 return NULL;
7760 s1 = PyBytes_AsString(os1);
7761 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00007762
Victor Stinner65170952011-11-22 22:16:17 +01007763 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01007764 if (newstr == NULL) {
7765 PyErr_NoMemory();
7766 goto error;
7767 }
7768
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007772 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007774#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007775
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 /* Install the first arg and newstr in posix_putenv_garbage;
7777 * this will cause previous value to be collected. This has to
7778 * happen after the real putenv() call because the old value
7779 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01007780 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 /* really not much we can do; just leak */
7782 PyErr_Clear();
7783 }
7784 else {
7785 Py_DECREF(newstr);
7786 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007787
Martin v. Löwis011e8422009-05-05 04:43:17 +00007788#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007789 Py_DECREF(os1);
7790 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007791#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007792 Py_RETURN_NONE;
7793
7794error:
7795#ifndef MS_WINDOWS
7796 Py_DECREF(os1);
7797 Py_DECREF(os2);
7798#endif
7799 Py_XDECREF(newstr);
7800 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00007801}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007802#endif /* putenv */
7803
Guido van Rossumc524d952001-10-19 01:31:59 +00007804#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007805PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007806"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007807Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007808
7809static PyObject *
7810posix_unsetenv(PyObject *self, PyObject *args)
7811{
Victor Stinner65170952011-11-22 22:16:17 +01007812 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01007813#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01007814 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01007815#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007816
7817 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00007818
Victor Stinner65170952011-11-22 22:16:17 +01007819 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00007820 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00007821
Victor Stinner984890f2011-11-24 13:53:38 +01007822#ifdef HAVE_BROKEN_UNSETENV
7823 unsetenv(PyBytes_AS_STRING(name));
7824#else
Victor Stinner65170952011-11-22 22:16:17 +01007825 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007826 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06007827 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01007828 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06007829 }
Victor Stinner984890f2011-11-24 13:53:38 +01007830#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007831
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 /* Remove the key from posix_putenv_garbage;
7833 * this will cause it to be collected. This has to
7834 * happen after the real unsetenv() call because the
7835 * old value was still accessible until then.
7836 */
Victor Stinner65170952011-11-22 22:16:17 +01007837 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 /* really not much we can do; just leak */
7839 PyErr_Clear();
7840 }
Victor Stinner65170952011-11-22 22:16:17 +01007841 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00007842 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007843}
7844#endif /* unsetenv */
7845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007846PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007847"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007848Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007849
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007850static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007851posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007852{
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 int code;
7854 char *message;
7855 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7856 return NULL;
7857 message = strerror(code);
7858 if (message == NULL) {
7859 PyErr_SetString(PyExc_ValueError,
7860 "strerror() argument out of range");
7861 return NULL;
7862 }
Victor Stinner1b579672011-12-17 05:47:23 +01007863 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007864}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007865
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007866
Guido van Rossumc9641791998-08-04 15:26:23 +00007867#ifdef HAVE_SYS_WAIT_H
7868
Fred Drake106c1a02002-04-23 15:58:02 +00007869#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007870PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007871"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007872Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007873
7874static PyObject *
7875posix_WCOREDUMP(PyObject *self, PyObject *args)
7876{
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 WAIT_TYPE status;
7878 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007879
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7881 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007882
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007884}
7885#endif /* WCOREDUMP */
7886
7887#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007888PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007889"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007890Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007891job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007892
7893static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007894posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007895{
Victor Stinner8c62be82010-05-06 00:08:46 +00007896 WAIT_TYPE status;
7897 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007898
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7900 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007901
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007903}
7904#endif /* WIFCONTINUED */
7905
Guido van Rossumc9641791998-08-04 15:26:23 +00007906#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007907PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007908"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007909Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007910
7911static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007912posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007913{
Victor Stinner8c62be82010-05-06 00:08:46 +00007914 WAIT_TYPE status;
7915 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007916
Victor Stinner8c62be82010-05-06 00:08:46 +00007917 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7918 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007919
Victor Stinner8c62be82010-05-06 00:08:46 +00007920 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007921}
7922#endif /* WIFSTOPPED */
7923
7924#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007925PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007926"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007927Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007928
7929static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007930posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007931{
Victor Stinner8c62be82010-05-06 00:08:46 +00007932 WAIT_TYPE status;
7933 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007934
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7936 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007937
Victor Stinner8c62be82010-05-06 00:08:46 +00007938 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007939}
7940#endif /* WIFSIGNALED */
7941
7942#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007943PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007944"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007945Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007946system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007947
7948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007949posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007950{
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 WAIT_TYPE status;
7952 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007953
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7955 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007956
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007958}
7959#endif /* WIFEXITED */
7960
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007961#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007962PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007963"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007964Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007965
7966static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007967posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007968{
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 WAIT_TYPE status;
7970 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007971
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7973 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007974
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007976}
7977#endif /* WEXITSTATUS */
7978
7979#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007980PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007981"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007982Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007983value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007984
7985static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007986posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007987{
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 WAIT_TYPE status;
7989 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007990
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7992 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007993
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007995}
7996#endif /* WTERMSIG */
7997
7998#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007999PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008000"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008001Return the signal that stopped the process that provided\n\
8002the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008003
8004static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008005posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008006{
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 WAIT_TYPE status;
8008 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008009
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8011 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008012
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008014}
8015#endif /* WSTOPSIG */
8016
8017#endif /* HAVE_SYS_WAIT_H */
8018
8019
Thomas Wouters477c8d52006-05-27 19:21:47 +00008020#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008021#ifdef _SCO_DS
8022/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8023 needed definitions in sys/statvfs.h */
8024#define _SVID3
8025#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008026#include <sys/statvfs.h>
8027
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008028static PyObject*
8029_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8031 if (v == NULL)
8032 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008033
8034#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008035 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8036 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8037 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8038 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8039 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8040 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8041 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8042 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8043 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8044 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008045#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008046 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8047 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8048 PyStructSequence_SET_ITEM(v, 2,
8049 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8050 PyStructSequence_SET_ITEM(v, 3,
8051 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8052 PyStructSequence_SET_ITEM(v, 4,
8053 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8054 PyStructSequence_SET_ITEM(v, 5,
8055 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8056 PyStructSequence_SET_ITEM(v, 6,
8057 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8058 PyStructSequence_SET_ITEM(v, 7,
8059 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8060 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8061 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008062#endif
8063
Victor Stinner8c62be82010-05-06 00:08:46 +00008064 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008065}
8066
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008067PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008068"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008069Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008070
8071static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008072posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008073{
Victor Stinner8c62be82010-05-06 00:08:46 +00008074 int fd, res;
8075 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008076
Victor Stinner8c62be82010-05-06 00:08:46 +00008077 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8078 return NULL;
8079 Py_BEGIN_ALLOW_THREADS
8080 res = fstatvfs(fd, &st);
8081 Py_END_ALLOW_THREADS
8082 if (res != 0)
8083 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008084
Victor Stinner8c62be82010-05-06 00:08:46 +00008085 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008086}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008087#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008088
8089
Thomas Wouters477c8d52006-05-27 19:21:47 +00008090#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008091#include <sys/statvfs.h>
8092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008093PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008094"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008095Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008096
8097static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008098posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008099{
Victor Stinner6fa67772011-09-20 04:04:33 +02008100 PyObject *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00008101 int res;
8102 struct statvfs st;
Victor Stinner6fa67772011-09-20 04:04:33 +02008103 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00008104 return NULL;
8105 Py_BEGIN_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008106 res = statvfs(PyBytes_AS_STRING(path), &st);
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 Py_END_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008108 if (res != 0) {
8109 posix_error_with_filename(PyBytes_AS_STRING(path));
8110 Py_DECREF(path);
8111 return NULL;
8112 }
8113 Py_DECREF(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008114
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008116}
8117#endif /* HAVE_STATVFS */
8118
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008119#ifdef MS_WINDOWS
8120PyDoc_STRVAR(win32__getdiskusage__doc__,
8121"_getdiskusage(path) -> (total, free)\n\n\
8122Return disk usage statistics about the given path as (total, free) tuple.");
8123
8124static PyObject *
8125win32__getdiskusage(PyObject *self, PyObject *args)
8126{
8127 BOOL retval;
8128 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008129 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008130
Victor Stinner6139c1b2011-11-09 22:14:14 +01008131 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008132 return NULL;
8133
8134 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008135 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008136 Py_END_ALLOW_THREADS
8137 if (retval == 0)
8138 return PyErr_SetFromWindowsErr(0);
8139
8140 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8141}
8142#endif
8143
8144
Fred Drakec9680921999-12-13 16:37:25 +00008145/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8146 * It maps strings representing configuration variable names to
8147 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008148 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008149 * rarely-used constants. There are three separate tables that use
8150 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008151 *
8152 * This code is always included, even if none of the interfaces that
8153 * need it are included. The #if hackery needed to avoid it would be
8154 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008155 */
8156struct constdef {
8157 char *name;
8158 long value;
8159};
8160
Fred Drake12c6e2d1999-12-14 21:25:03 +00008161static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008162conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008163 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008164{
Christian Heimes217cfd12007-12-02 14:31:20 +00008165 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008166 *valuep = PyLong_AS_LONG(arg);
8167 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008168 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008169 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008170 /* look up the value in the table using a binary search */
8171 size_t lo = 0;
8172 size_t mid;
8173 size_t hi = tablesize;
8174 int cmp;
8175 const char *confname;
8176 if (!PyUnicode_Check(arg)) {
8177 PyErr_SetString(PyExc_TypeError,
8178 "configuration names must be strings or integers");
8179 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008181 confname = _PyUnicode_AsString(arg);
8182 if (confname == NULL)
8183 return 0;
8184 while (lo < hi) {
8185 mid = (lo + hi) / 2;
8186 cmp = strcmp(confname, table[mid].name);
8187 if (cmp < 0)
8188 hi = mid;
8189 else if (cmp > 0)
8190 lo = mid + 1;
8191 else {
8192 *valuep = table[mid].value;
8193 return 1;
8194 }
8195 }
8196 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8197 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008198 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008199}
8200
8201
8202#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8203static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008204#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008206#endif
8207#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008208 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008209#endif
Fred Drakec9680921999-12-13 16:37:25 +00008210#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008211 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008212#endif
8213#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008215#endif
8216#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008218#endif
8219#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008221#endif
8222#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008224#endif
8225#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008227#endif
8228#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008230#endif
8231#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008233#endif
8234#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008236#endif
8237#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008239#endif
8240#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008242#endif
8243#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008245#endif
8246#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008248#endif
8249#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008251#endif
8252#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008253 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008254#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008255#ifdef _PC_ACL_ENABLED
8256 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8257#endif
8258#ifdef _PC_MIN_HOLE_SIZE
8259 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8260#endif
8261#ifdef _PC_ALLOC_SIZE_MIN
8262 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8263#endif
8264#ifdef _PC_REC_INCR_XFER_SIZE
8265 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8266#endif
8267#ifdef _PC_REC_MAX_XFER_SIZE
8268 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8269#endif
8270#ifdef _PC_REC_MIN_XFER_SIZE
8271 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8272#endif
8273#ifdef _PC_REC_XFER_ALIGN
8274 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8275#endif
8276#ifdef _PC_SYMLINK_MAX
8277 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8278#endif
8279#ifdef _PC_XATTR_ENABLED
8280 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8281#endif
8282#ifdef _PC_XATTR_EXISTS
8283 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8284#endif
8285#ifdef _PC_TIMESTAMP_RESOLUTION
8286 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8287#endif
Fred Drakec9680921999-12-13 16:37:25 +00008288};
8289
Fred Drakec9680921999-12-13 16:37:25 +00008290static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008291conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008292{
8293 return conv_confname(arg, valuep, posix_constants_pathconf,
8294 sizeof(posix_constants_pathconf)
8295 / sizeof(struct constdef));
8296}
8297#endif
8298
8299#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008300PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008301"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008302Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008303If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008304
8305static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008306posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008307{
8308 PyObject *result = NULL;
8309 int name, fd;
8310
Fred Drake12c6e2d1999-12-14 21:25:03 +00008311 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8312 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008313 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008314
Stefan Krah0e803b32010-11-26 16:16:47 +00008315 errno = 0;
8316 limit = fpathconf(fd, name);
8317 if (limit == -1 && errno != 0)
8318 posix_error();
8319 else
8320 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008321 }
8322 return result;
8323}
8324#endif
8325
8326
8327#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008328PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008329"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008330Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008331If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008332
8333static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008334posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008335{
8336 PyObject *result = NULL;
8337 int name;
8338 char *path;
8339
8340 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8341 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008343
Victor Stinner8c62be82010-05-06 00:08:46 +00008344 errno = 0;
8345 limit = pathconf(path, name);
8346 if (limit == -1 && errno != 0) {
8347 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008348 /* could be a path or name problem */
8349 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008350 else
Stefan Krah99439262010-11-26 12:58:05 +00008351 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 }
8353 else
8354 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008355 }
8356 return result;
8357}
8358#endif
8359
8360#ifdef HAVE_CONFSTR
8361static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008362#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008363 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008364#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008365#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008366 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008367#endif
8368#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008369 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008370#endif
Fred Draked86ed291999-12-15 15:34:33 +00008371#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008372 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008373#endif
8374#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008376#endif
8377#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008378 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008379#endif
8380#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008381 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008382#endif
Fred Drakec9680921999-12-13 16:37:25 +00008383#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008384 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008385#endif
8386#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008388#endif
8389#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008390 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008391#endif
8392#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008393 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008394#endif
8395#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008397#endif
8398#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008399 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008400#endif
8401#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008403#endif
8404#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008405 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008406#endif
Fred Draked86ed291999-12-15 15:34:33 +00008407#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008408 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008409#endif
Fred Drakec9680921999-12-13 16:37:25 +00008410#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008411 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008412#endif
Fred Draked86ed291999-12-15 15:34:33 +00008413#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008415#endif
8416#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008418#endif
8419#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008420 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008421#endif
8422#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008423 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008424#endif
Fred Drakec9680921999-12-13 16:37:25 +00008425#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008426 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008427#endif
8428#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008429 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008430#endif
8431#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008432 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008433#endif
8434#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008435 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008436#endif
8437#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008438 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008439#endif
8440#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008442#endif
8443#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008445#endif
8446#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008448#endif
8449#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008450 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008451#endif
8452#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008453 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008454#endif
8455#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008456 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008457#endif
8458#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008460#endif
8461#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008462 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008463#endif
8464#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008466#endif
8467#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008468 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008469#endif
8470#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008471 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008472#endif
Fred Draked86ed291999-12-15 15:34:33 +00008473#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008475#endif
8476#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008477 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008478#endif
8479#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008480 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008481#endif
8482#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008483 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008484#endif
8485#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008486 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008487#endif
8488#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008490#endif
8491#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008493#endif
8494#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008495 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008496#endif
8497#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008498 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008499#endif
8500#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008501 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008502#endif
8503#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008504 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008505#endif
8506#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008507 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008508#endif
8509#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008510 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008511#endif
Fred Drakec9680921999-12-13 16:37:25 +00008512};
8513
8514static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008515conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008516{
8517 return conv_confname(arg, valuep, posix_constants_confstr,
8518 sizeof(posix_constants_confstr)
8519 / sizeof(struct constdef));
8520}
8521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008522PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008523"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008524Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008525
8526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008527posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008528{
8529 PyObject *result = NULL;
8530 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008531 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008532 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008533
Victor Stinnercb043522010-09-10 23:49:04 +00008534 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8535 return NULL;
8536
8537 errno = 0;
8538 len = confstr(name, buffer, sizeof(buffer));
8539 if (len == 0) {
8540 if (errno) {
8541 posix_error();
8542 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008543 }
8544 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008545 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008546 }
8547 }
Victor Stinnercb043522010-09-10 23:49:04 +00008548
8549 if ((unsigned int)len >= sizeof(buffer)) {
8550 char *buf = PyMem_Malloc(len);
8551 if (buf == NULL)
8552 return PyErr_NoMemory();
8553 confstr(name, buf, len);
8554 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8555 PyMem_Free(buf);
8556 }
8557 else
8558 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008559 return result;
8560}
8561#endif
8562
8563
8564#ifdef HAVE_SYSCONF
8565static struct constdef posix_constants_sysconf[] = {
8566#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008567 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008568#endif
8569#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008570 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008571#endif
8572#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008573 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008574#endif
8575#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008576 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008577#endif
8578#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008579 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008580#endif
8581#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008582 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008583#endif
8584#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008585 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008586#endif
8587#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008588 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008589#endif
8590#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008591 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008592#endif
8593#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008594 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008595#endif
Fred Draked86ed291999-12-15 15:34:33 +00008596#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008597 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008598#endif
8599#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008600 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008601#endif
Fred Drakec9680921999-12-13 16:37:25 +00008602#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008603 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008604#endif
Fred Drakec9680921999-12-13 16:37:25 +00008605#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008606 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008607#endif
8608#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008609 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008610#endif
8611#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008612 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008613#endif
8614#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008615 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008616#endif
8617#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008618 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008619#endif
Fred Draked86ed291999-12-15 15:34:33 +00008620#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008621 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008622#endif
Fred Drakec9680921999-12-13 16:37:25 +00008623#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008625#endif
8626#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008627 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008628#endif
8629#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008630 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008631#endif
8632#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008633 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008634#endif
8635#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008636 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008637#endif
Fred Draked86ed291999-12-15 15:34:33 +00008638#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008640#endif
Fred Drakec9680921999-12-13 16:37:25 +00008641#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008643#endif
8644#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008645 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008646#endif
8647#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008649#endif
8650#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008651 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008652#endif
8653#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008654 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008655#endif
8656#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008657 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008658#endif
8659#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008660 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008661#endif
8662#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008663 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008664#endif
8665#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008666 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008667#endif
8668#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008670#endif
8671#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008672 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008673#endif
8674#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008675 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008676#endif
8677#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008678 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008679#endif
8680#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008681 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008682#endif
8683#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008684 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008685#endif
8686#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008687 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008688#endif
8689#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008690 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008691#endif
8692#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008693 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008694#endif
8695#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008696 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008697#endif
8698#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008699 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008700#endif
8701#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008702 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008703#endif
8704#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008705 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008706#endif
8707#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008708 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008709#endif
Fred Draked86ed291999-12-15 15:34:33 +00008710#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008711 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008712#endif
Fred Drakec9680921999-12-13 16:37:25 +00008713#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008714 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008715#endif
8716#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008717 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008718#endif
8719#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008720 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008721#endif
Fred Draked86ed291999-12-15 15:34:33 +00008722#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008723 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008724#endif
Fred Drakec9680921999-12-13 16:37:25 +00008725#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008726 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008727#endif
Fred Draked86ed291999-12-15 15:34:33 +00008728#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008729 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008730#endif
8731#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008732 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008733#endif
Fred Drakec9680921999-12-13 16:37:25 +00008734#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008735 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008736#endif
8737#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008738 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008739#endif
8740#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008741 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008742#endif
8743#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008744 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008745#endif
Fred Draked86ed291999-12-15 15:34:33 +00008746#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008747 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008748#endif
Fred Drakec9680921999-12-13 16:37:25 +00008749#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008750 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008751#endif
8752#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008753 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008754#endif
8755#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008757#endif
8758#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008760#endif
8761#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008762 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008763#endif
8764#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008766#endif
8767#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008768 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008769#endif
Fred Draked86ed291999-12-15 15:34:33 +00008770#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008772#endif
Fred Drakec9680921999-12-13 16:37:25 +00008773#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008775#endif
8776#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008777 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008778#endif
Fred Draked86ed291999-12-15 15:34:33 +00008779#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008780 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008781#endif
Fred Drakec9680921999-12-13 16:37:25 +00008782#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008783 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008784#endif
8785#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008786 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008787#endif
8788#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008789 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008790#endif
8791#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008793#endif
8794#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008796#endif
8797#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008798 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008799#endif
8800#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008802#endif
8803#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008805#endif
8806#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008808#endif
Fred Draked86ed291999-12-15 15:34:33 +00008809#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008811#endif
8812#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008814#endif
Fred Drakec9680921999-12-13 16:37:25 +00008815#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008817#endif
8818#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008820#endif
8821#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
8842#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008844#endif
8845#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008847#endif
8848#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
8851#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008852 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008853#endif
8854#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008855 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008856#endif
8857#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008859#endif
8860#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008861 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008862#endif
8863#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008864 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008865#endif
8866#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008868#endif
8869#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008871#endif
8872#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008874#endif
8875#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008876 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008877#endif
8878#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008879 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008880#endif
8881#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008882 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008883#endif
8884#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008886#endif
8887#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008889#endif
8890#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008892#endif
8893#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008895#endif
8896#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008898#endif
8899#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008901#endif
8902#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008904#endif
8905#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008906 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008907#endif
8908#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008910#endif
8911#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008913#endif
8914#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008915 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008916#endif
8917#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008918 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008919#endif
Fred Draked86ed291999-12-15 15:34:33 +00008920#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008921 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008922#endif
Fred Drakec9680921999-12-13 16:37:25 +00008923#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008924 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008925#endif
8926#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008928#endif
8929#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008930 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008931#endif
8932#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008933 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008934#endif
8935#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008936 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008937#endif
8938#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008939 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008940#endif
8941#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008942 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008943#endif
8944#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008945 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008946#endif
8947#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008948 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008949#endif
8950#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008951 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008952#endif
8953#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008954 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008955#endif
8956#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008957 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008958#endif
8959#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008960 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008961#endif
8962#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008963 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008964#endif
8965#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008966 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008967#endif
8968#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008969 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008970#endif
8971#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008972 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008973#endif
8974#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008975 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008976#endif
8977#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008978 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008979#endif
8980#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008981 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008982#endif
8983#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008984 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008985#endif
8986#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008987 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008988#endif
8989#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008990 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008991#endif
8992#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008993 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008994#endif
8995#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008996 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008997#endif
8998#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008999 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009000#endif
9001#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009002 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009003#endif
9004#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009005 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009006#endif
9007#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009008 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009009#endif
9010#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009011 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009012#endif
9013#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009014 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009015#endif
9016#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009017 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009018#endif
9019#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009020 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009021#endif
9022#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009023 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009024#endif
9025#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009026 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009027#endif
9028#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009029 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009030#endif
9031#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009032 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009033#endif
9034#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009035 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009036#endif
9037#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009038 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009039#endif
9040#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009041 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009042#endif
9043#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009044 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009045#endif
9046#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009047 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009048#endif
9049#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009050 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009051#endif
9052#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009053 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009054#endif
9055#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009056 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009057#endif
9058};
9059
9060static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009061conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009062{
9063 return conv_confname(arg, valuep, posix_constants_sysconf,
9064 sizeof(posix_constants_sysconf)
9065 / sizeof(struct constdef));
9066}
9067
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009068PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009069"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009070Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009071
9072static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009073posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009074{
9075 PyObject *result = NULL;
9076 int name;
9077
9078 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9079 int value;
9080
9081 errno = 0;
9082 value = sysconf(name);
9083 if (value == -1 && errno != 0)
9084 posix_error();
9085 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009086 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009087 }
9088 return result;
9089}
9090#endif
9091
9092
Fred Drakebec628d1999-12-15 18:31:10 +00009093/* This code is used to ensure that the tables of configuration value names
9094 * are in sorted order as required by conv_confname(), and also to build the
9095 * the exported dictionaries that are used to publish information about the
9096 * names available on the host platform.
9097 *
9098 * Sorting the table at runtime ensures that the table is properly ordered
9099 * when used, even for platforms we're not able to test on. It also makes
9100 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009101 */
Fred Drakebec628d1999-12-15 18:31:10 +00009102
9103static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009104cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009105{
9106 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009107 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009108 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009109 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009110
9111 return strcmp(c1->name, c2->name);
9112}
9113
9114static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009115setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009116 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009117{
Fred Drakebec628d1999-12-15 18:31:10 +00009118 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009119 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009120
9121 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9122 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009123 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009124 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009125
Barry Warsaw3155db32000-04-13 15:20:40 +00009126 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009127 PyObject *o = PyLong_FromLong(table[i].value);
9128 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9129 Py_XDECREF(o);
9130 Py_DECREF(d);
9131 return -1;
9132 }
9133 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009134 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009135 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009136}
9137
Fred Drakebec628d1999-12-15 18:31:10 +00009138/* Return -1 on failure, 0 on success. */
9139static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009140setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009141{
9142#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009143 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009144 sizeof(posix_constants_pathconf)
9145 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009146 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009147 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009148#endif
9149#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009150 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009151 sizeof(posix_constants_confstr)
9152 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009153 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009154 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009155#endif
9156#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009157 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009158 sizeof(posix_constants_sysconf)
9159 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009160 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009161 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009162#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009163 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009164}
Fred Draked86ed291999-12-15 15:34:33 +00009165
9166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009167PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009168"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009169Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009170in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009171
9172static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009173posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009174{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009175 abort();
9176 /*NOTREACHED*/
9177 Py_FatalError("abort() called from Python code didn't abort!");
9178 return NULL;
9179}
Fred Drakebec628d1999-12-15 18:31:10 +00009180
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009181#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009182PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009183"startfile(filepath [, operation]) - Start a file with its associated\n\
9184application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009185\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009186When \"operation\" is not specified or \"open\", this acts like\n\
9187double-clicking the file in Explorer, or giving the file name as an\n\
9188argument to the DOS \"start\" command: the file is opened with whatever\n\
9189application (if any) its extension is associated.\n\
9190When another \"operation\" is given, it specifies what should be done with\n\
9191the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009192\n\
9193startfile returns as soon as the associated application is launched.\n\
9194There is no option to wait for the application to close, and no way\n\
9195to retrieve the application's exit status.\n\
9196\n\
9197The filepath is relative to the current directory. If you want to use\n\
9198an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009199the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009200
9201static PyObject *
9202win32_startfile(PyObject *self, PyObject *args)
9203{
Victor Stinner8c62be82010-05-06 00:08:46 +00009204 PyObject *ofilepath;
9205 char *filepath;
9206 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009207 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009208 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009209
Victor Stinnereb5657a2011-09-30 01:44:27 +02009210 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 if (!PyArg_ParseTuple(args, "U|s:startfile",
9212 &unipath, &operation)) {
9213 PyErr_Clear();
9214 goto normal;
9215 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009216
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009218 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009219 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009220 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009221 PyErr_Clear();
9222 operation = NULL;
9223 goto normal;
9224 }
9225 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009226
Victor Stinnereb5657a2011-09-30 01:44:27 +02009227 wpath = PyUnicode_AsUnicode(unipath);
9228 if (wpath == NULL)
9229 goto normal;
9230 if (uoperation) {
9231 woperation = PyUnicode_AsUnicode(uoperation);
9232 if (woperation == NULL)
9233 goto normal;
9234 }
9235 else
9236 woperation = NULL;
9237
Victor Stinner8c62be82010-05-06 00:08:46 +00009238 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009239 rc = ShellExecuteW((HWND)0, woperation, wpath,
9240 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009241 Py_END_ALLOW_THREADS
9242
Victor Stinnereb5657a2011-09-30 01:44:27 +02009243 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009245 win32_error_object("startfile", unipath);
9246 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009247 }
9248 Py_INCREF(Py_None);
9249 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009250
9251normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009252 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9253 PyUnicode_FSConverter, &ofilepath,
9254 &operation))
9255 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009256 if (win32_warn_bytes_api()) {
9257 Py_DECREF(ofilepath);
9258 return NULL;
9259 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009260 filepath = PyBytes_AsString(ofilepath);
9261 Py_BEGIN_ALLOW_THREADS
9262 rc = ShellExecute((HWND)0, operation, filepath,
9263 NULL, NULL, SW_SHOWNORMAL);
9264 Py_END_ALLOW_THREADS
9265 if (rc <= (HINSTANCE)32) {
9266 PyObject *errval = win32_error("startfile", filepath);
9267 Py_DECREF(ofilepath);
9268 return errval;
9269 }
9270 Py_DECREF(ofilepath);
9271 Py_INCREF(Py_None);
9272 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009273}
9274#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009275
Martin v. Löwis438b5342002-12-27 10:16:42 +00009276#ifdef HAVE_GETLOADAVG
9277PyDoc_STRVAR(posix_getloadavg__doc__,
9278"getloadavg() -> (float, float, float)\n\n\
9279Return the number of processes in the system run queue averaged over\n\
9280the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9281was unobtainable");
9282
9283static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009284posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009285{
9286 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009287 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009288 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9289 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009290 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009291 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009292}
9293#endif
9294
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009295PyDoc_STRVAR(device_encoding__doc__,
9296"device_encoding(fd) -> str\n\n\
9297Return a string describing the encoding of the device\n\
9298if the output is a terminal; else return None.");
9299
9300static PyObject *
9301device_encoding(PyObject *self, PyObject *args)
9302{
Victor Stinner8c62be82010-05-06 00:08:46 +00009303 int fd;
Brett Cannonefb00c02012-02-29 18:31:31 -05009304
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9306 return NULL;
Brett Cannonefb00c02012-02-29 18:31:31 -05009307
9308 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009309}
9310
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009311#ifdef __VMS
9312/* Use openssl random routine */
9313#include <openssl/rand.h>
9314PyDoc_STRVAR(vms_urandom__doc__,
9315"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009316Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009317
9318static PyObject*
9319vms_urandom(PyObject *self, PyObject *args)
9320{
Victor Stinner8c62be82010-05-06 00:08:46 +00009321 int howMany;
9322 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009323
Victor Stinner8c62be82010-05-06 00:08:46 +00009324 /* Read arguments */
9325 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9326 return NULL;
9327 if (howMany < 0)
9328 return PyErr_Format(PyExc_ValueError,
9329 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009330
Victor Stinner8c62be82010-05-06 00:08:46 +00009331 /* Allocate bytes */
9332 result = PyBytes_FromStringAndSize(NULL, howMany);
9333 if (result != NULL) {
9334 /* Get random data */
9335 if (RAND_pseudo_bytes((unsigned char*)
9336 PyBytes_AS_STRING(result),
9337 howMany) < 0) {
9338 Py_DECREF(result);
9339 return PyErr_Format(PyExc_ValueError,
9340 "RAND_pseudo_bytes");
9341 }
9342 }
9343 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009344}
9345#endif
9346
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009347#ifdef HAVE_SETRESUID
9348PyDoc_STRVAR(posix_setresuid__doc__,
9349"setresuid(ruid, euid, suid)\n\n\
9350Set the current process's real, effective, and saved user ids.");
9351
9352static PyObject*
9353posix_setresuid (PyObject *self, PyObject *args)
9354{
Victor Stinner8c62be82010-05-06 00:08:46 +00009355 /* We assume uid_t is no larger than a long. */
9356 long ruid, euid, suid;
9357 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9358 return NULL;
9359 if (setresuid(ruid, euid, suid) < 0)
9360 return posix_error();
9361 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009362}
9363#endif
9364
9365#ifdef HAVE_SETRESGID
9366PyDoc_STRVAR(posix_setresgid__doc__,
9367"setresgid(rgid, egid, sgid)\n\n\
9368Set the current process's real, effective, and saved group ids.");
9369
9370static PyObject*
9371posix_setresgid (PyObject *self, PyObject *args)
9372{
Victor Stinner8c62be82010-05-06 00:08:46 +00009373 /* We assume uid_t is no larger than a long. */
9374 long rgid, egid, sgid;
9375 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9376 return NULL;
9377 if (setresgid(rgid, egid, sgid) < 0)
9378 return posix_error();
9379 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009380}
9381#endif
9382
9383#ifdef HAVE_GETRESUID
9384PyDoc_STRVAR(posix_getresuid__doc__,
9385"getresuid() -> (ruid, euid, suid)\n\n\
9386Get tuple of the current process's real, effective, and saved user ids.");
9387
9388static PyObject*
9389posix_getresuid (PyObject *self, PyObject *noargs)
9390{
Victor Stinner8c62be82010-05-06 00:08:46 +00009391 uid_t ruid, euid, suid;
9392 long l_ruid, l_euid, l_suid;
9393 if (getresuid(&ruid, &euid, &suid) < 0)
9394 return posix_error();
9395 /* Force the values into long's as we don't know the size of uid_t. */
9396 l_ruid = ruid;
9397 l_euid = euid;
9398 l_suid = suid;
9399 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009400}
9401#endif
9402
9403#ifdef HAVE_GETRESGID
9404PyDoc_STRVAR(posix_getresgid__doc__,
9405"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009406Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009407
9408static PyObject*
9409posix_getresgid (PyObject *self, PyObject *noargs)
9410{
Victor Stinner8c62be82010-05-06 00:08:46 +00009411 uid_t rgid, egid, sgid;
9412 long l_rgid, l_egid, l_sgid;
9413 if (getresgid(&rgid, &egid, &sgid) < 0)
9414 return posix_error();
9415 /* Force the values into long's as we don't know the size of uid_t. */
9416 l_rgid = rgid;
9417 l_egid = egid;
9418 l_sgid = sgid;
9419 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009420}
9421#endif
9422
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009423/* Posix *at family of functions:
9424 faccessat, fchmodat, fchownat, fstatat, futimesat,
9425 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9426 unlinkat, utimensat, mkfifoat */
9427
9428#ifdef HAVE_FACCESSAT
9429PyDoc_STRVAR(posix_faccessat__doc__,
9430"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9431Like access() but if path is relative, it is taken as relative to dirfd.\n\
9432flags is optional and can be constructed by ORing together zero or more\n\
9433of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9434If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9435is interpreted relative to the current working directory.");
9436
9437static PyObject *
9438posix_faccessat(PyObject *self, PyObject *args)
9439{
9440 PyObject *opath;
9441 char *path;
9442 int mode;
9443 int res;
9444 int dirfd, flags = 0;
9445 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9446 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9447 return NULL;
9448 path = PyBytes_AsString(opath);
9449 Py_BEGIN_ALLOW_THREADS
9450 res = faccessat(dirfd, path, mode, flags);
9451 Py_END_ALLOW_THREADS
9452 Py_DECREF(opath);
9453 return PyBool_FromLong(res == 0);
9454}
9455#endif
9456
9457#ifdef HAVE_FCHMODAT
9458PyDoc_STRVAR(posix_fchmodat__doc__,
9459"fchmodat(dirfd, path, mode, flags=0)\n\n\
9460Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9461flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9462If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9463is interpreted relative to the current working directory.");
9464
9465static PyObject *
9466posix_fchmodat(PyObject *self, PyObject *args)
9467{
9468 int dirfd, mode, res;
9469 int flags = 0;
9470 PyObject *opath;
9471 char *path;
9472
9473 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9474 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9475 return NULL;
9476
9477 path = PyBytes_AsString(opath);
9478
9479 Py_BEGIN_ALLOW_THREADS
9480 res = fchmodat(dirfd, path, mode, flags);
9481 Py_END_ALLOW_THREADS
9482 Py_DECREF(opath);
9483 if (res < 0)
9484 return posix_error();
9485 Py_RETURN_NONE;
9486}
9487#endif /* HAVE_FCHMODAT */
9488
9489#ifdef HAVE_FCHOWNAT
9490PyDoc_STRVAR(posix_fchownat__doc__,
9491"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9492Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9493flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9494If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9495is interpreted relative to the current working directory.");
9496
9497static PyObject *
9498posix_fchownat(PyObject *self, PyObject *args)
9499{
9500 PyObject *opath;
9501 int dirfd, res;
9502 long uid, gid;
9503 int flags = 0;
9504 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009505
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009506 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9507 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9508 return NULL;
9509
9510 path = PyBytes_AsString(opath);
9511
9512 Py_BEGIN_ALLOW_THREADS
9513 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9514 Py_END_ALLOW_THREADS
9515 Py_DECREF(opath);
9516 if (res < 0)
9517 return posix_error();
9518 Py_RETURN_NONE;
9519}
9520#endif /* HAVE_FCHOWNAT */
9521
9522#ifdef HAVE_FSTATAT
9523PyDoc_STRVAR(posix_fstatat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01009524"fstatat(dirfd, path, flags=0) -> stat result\n\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009525Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9526flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9527If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9528is interpreted relative to the current working directory.");
9529
9530static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01009531posix_fstatat(PyObject *self, PyObject *args)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009532{
9533 PyObject *opath;
9534 char *path;
9535 STRUCT_STAT st;
9536 int dirfd, res, flags = 0;
9537
Victor Stinner4195b5c2012-02-08 23:03:19 +01009538 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9539 &dirfd, PyUnicode_FSConverter, &opath, &flags))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009540 return NULL;
9541 path = PyBytes_AsString(opath);
9542
9543 Py_BEGIN_ALLOW_THREADS
9544 res = fstatat(dirfd, path, &st, flags);
9545 Py_END_ALLOW_THREADS
9546 Py_DECREF(opath);
9547 if (res != 0)
9548 return posix_error();
9549
Victor Stinner4195b5c2012-02-08 23:03:19 +01009550 return _pystat_fromstructstat(&st);
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009551}
9552#endif
9553
9554#ifdef HAVE_FUTIMESAT
9555PyDoc_STRVAR(posix_futimesat__doc__,
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009556"futimesat(dirfd, path[, (atime, mtime)])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009557Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9558If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9559is interpreted relative to the current working directory.");
9560
9561static PyObject *
9562posix_futimesat(PyObject *self, PyObject *args)
9563{
9564 PyObject *opath;
9565 char *path;
9566 int res, dirfd;
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009567 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009568 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009569 long ansec, mnsec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009570
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009571 if (!PyArg_ParseTuple(args, "iO&|O:futimesat",
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009572 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9573 return NULL;
9574 path = PyBytes_AsString(opath);
9575 if (arg == Py_None) {
9576 /* optional time values not given */
9577 Py_BEGIN_ALLOW_THREADS
9578 res = futimesat(dirfd, path, NULL);
9579 Py_END_ALLOW_THREADS
9580 }
9581 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9582 PyErr_SetString(PyExc_TypeError,
9583 "futimesat() arg 3 must be a tuple (atime, mtime)");
9584 Py_DECREF(opath);
9585 return NULL;
9586 }
9587 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +01009588 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0),
9589 &atime, &ansec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009590 Py_DECREF(opath);
9591 return NULL;
9592 }
Victor Stinner5d272cc2012-03-13 13:35:55 +01009593 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1),
9594 &mtime, &mnsec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009595 Py_DECREF(opath);
9596 return NULL;
9597 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009598
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009599 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009600 {
9601#ifdef HAVE_UTIMENSAT
9602 struct timespec buf[2];
9603 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009604 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009605 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009606 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009607 res = utimensat(dirfd, path, buf, 0);
9608#else
9609 struct timeval buf[2];
9610 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009611 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009612 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009613 buf[1].tv_usec = mnsec / 1000;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009614 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009615#endif
9616 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009617 Py_END_ALLOW_THREADS
9618 }
9619 Py_DECREF(opath);
9620 if (res < 0) {
9621 return posix_error();
9622 }
9623 Py_RETURN_NONE;
9624}
9625#endif
9626
9627#ifdef HAVE_LINKAT
9628PyDoc_STRVAR(posix_linkat__doc__,
9629"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9630Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9631and if dstpath is relative, it is taken as relative to dstfd.\n\
9632flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9633If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9634srcpath is interpreted relative to the current working directory. This\n\
9635also applies for dstpath.");
9636
9637static PyObject *
9638posix_linkat(PyObject *self, PyObject *args)
9639{
9640 PyObject *osrc, *odst;
9641 char *src, *dst;
9642 int res, srcfd, dstfd;
9643 int flags = 0;
9644
9645 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9646 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9647 return NULL;
9648 src = PyBytes_AsString(osrc);
9649 dst = PyBytes_AsString(odst);
9650 Py_BEGIN_ALLOW_THREADS
9651 res = linkat(srcfd, src, dstfd, dst, flags);
9652 Py_END_ALLOW_THREADS
9653 Py_DECREF(osrc);
9654 Py_DECREF(odst);
9655 if (res < 0)
9656 return posix_error();
9657 Py_RETURN_NONE;
9658}
9659#endif /* HAVE_LINKAT */
9660
9661#ifdef HAVE_MKDIRAT
9662PyDoc_STRVAR(posix_mkdirat__doc__,
9663"mkdirat(dirfd, path, mode=0o777)\n\n\
9664Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9665If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9666is interpreted relative to the current working directory.");
9667
9668static PyObject *
9669posix_mkdirat(PyObject *self, PyObject *args)
9670{
9671 int res, dirfd;
9672 PyObject *opath;
9673 char *path;
9674 int mode = 0777;
9675
9676 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9677 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9678 return NULL;
9679 path = PyBytes_AsString(opath);
9680 Py_BEGIN_ALLOW_THREADS
9681 res = mkdirat(dirfd, path, mode);
9682 Py_END_ALLOW_THREADS
9683 Py_DECREF(opath);
9684 if (res < 0)
9685 return posix_error();
9686 Py_RETURN_NONE;
9687}
9688#endif
9689
9690#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9691PyDoc_STRVAR(posix_mknodat__doc__,
9692"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9693Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9694If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9695is interpreted relative to the current working directory.");
9696
9697static PyObject *
9698posix_mknodat(PyObject *self, PyObject *args)
9699{
9700 PyObject *opath;
9701 char *filename;
9702 int mode = 0600;
9703 int device = 0;
9704 int res, dirfd;
9705 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9706 PyUnicode_FSConverter, &opath, &mode, &device))
9707 return NULL;
9708 filename = PyBytes_AS_STRING(opath);
9709 Py_BEGIN_ALLOW_THREADS
9710 res = mknodat(dirfd, filename, mode, device);
9711 Py_END_ALLOW_THREADS
9712 Py_DECREF(opath);
9713 if (res < 0)
9714 return posix_error();
9715 Py_RETURN_NONE;
9716}
9717#endif
9718
9719#ifdef HAVE_OPENAT
9720PyDoc_STRVAR(posix_openat__doc__,
9721"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9722Like open() but if path is relative, it is taken as relative to dirfd.\n\
9723If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9724is interpreted relative to the current working directory.");
9725
9726static PyObject *
9727posix_openat(PyObject *self, PyObject *args)
9728{
9729 PyObject *ofile;
9730 char *file;
9731 int flag, dirfd, fd;
9732 int mode = 0777;
9733
9734 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9735 &dirfd, PyUnicode_FSConverter, &ofile,
9736 &flag, &mode))
9737 return NULL;
9738 file = PyBytes_AsString(ofile);
9739 Py_BEGIN_ALLOW_THREADS
9740 fd = openat(dirfd, file, flag, mode);
9741 Py_END_ALLOW_THREADS
9742 Py_DECREF(ofile);
9743 if (fd < 0)
9744 return posix_error();
9745 return PyLong_FromLong((long)fd);
9746}
9747#endif
9748
9749#ifdef HAVE_READLINKAT
9750PyDoc_STRVAR(posix_readlinkat__doc__,
9751"readlinkat(dirfd, path) -> path\n\n\
9752Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9753If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9754is interpreted relative to the current working directory.");
9755
9756static PyObject *
9757posix_readlinkat(PyObject *self, PyObject *args)
9758{
9759 PyObject *v, *opath;
9760 char buf[MAXPATHLEN];
9761 char *path;
9762 int n, dirfd;
9763 int arg_is_unicode = 0;
9764
9765 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9766 &dirfd, PyUnicode_FSConverter, &opath))
9767 return NULL;
9768 path = PyBytes_AsString(opath);
9769 v = PySequence_GetItem(args, 1);
9770 if (v == NULL) {
9771 Py_DECREF(opath);
9772 return NULL;
9773 }
9774
9775 if (PyUnicode_Check(v)) {
9776 arg_is_unicode = 1;
9777 }
9778 Py_DECREF(v);
9779
9780 Py_BEGIN_ALLOW_THREADS
9781 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9782 Py_END_ALLOW_THREADS
9783 Py_DECREF(opath);
9784 if (n < 0)
9785 return posix_error();
9786
9787 if (arg_is_unicode)
9788 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9789 else
9790 return PyBytes_FromStringAndSize(buf, n);
9791}
9792#endif /* HAVE_READLINKAT */
9793
9794#ifdef HAVE_RENAMEAT
9795PyDoc_STRVAR(posix_renameat__doc__,
9796"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9797Like rename() but if oldpath is relative, it is taken as relative to\n\
9798olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9799If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9800oldpath is interpreted relative to the current working directory. This\n\
9801also applies for newpath.");
9802
9803static PyObject *
9804posix_renameat(PyObject *self, PyObject *args)
9805{
9806 int res;
9807 PyObject *opathold, *opathnew;
9808 char *opath, *npath;
9809 int oldfd, newfd;
9810
9811 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9812 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9813 return NULL;
9814 opath = PyBytes_AsString(opathold);
9815 npath = PyBytes_AsString(opathnew);
9816 Py_BEGIN_ALLOW_THREADS
9817 res = renameat(oldfd, opath, newfd, npath);
9818 Py_END_ALLOW_THREADS
9819 Py_DECREF(opathold);
9820 Py_DECREF(opathnew);
9821 if (res < 0)
9822 return posix_error();
9823 Py_RETURN_NONE;
9824}
9825#endif
9826
9827#if HAVE_SYMLINKAT
9828PyDoc_STRVAR(posix_symlinkat__doc__,
9829"symlinkat(src, dstfd, dst)\n\n\
9830Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9831If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9832is interpreted relative to the current working directory.");
9833
9834static PyObject *
9835posix_symlinkat(PyObject *self, PyObject *args)
9836{
9837 int res, dstfd;
9838 PyObject *osrc, *odst;
9839 char *src, *dst;
9840
9841 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9842 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9843 return NULL;
9844 src = PyBytes_AsString(osrc);
9845 dst = PyBytes_AsString(odst);
9846 Py_BEGIN_ALLOW_THREADS
9847 res = symlinkat(src, dstfd, dst);
9848 Py_END_ALLOW_THREADS
9849 Py_DECREF(osrc);
9850 Py_DECREF(odst);
9851 if (res < 0)
9852 return posix_error();
9853 Py_RETURN_NONE;
9854}
9855#endif /* HAVE_SYMLINKAT */
9856
9857#ifdef HAVE_UNLINKAT
9858PyDoc_STRVAR(posix_unlinkat__doc__,
9859"unlinkat(dirfd, path, flags=0)\n\n\
9860Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9861flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9862specified, unlinkat() behaves like rmdir().\n\
9863If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9864is interpreted relative to the current working directory.");
9865
9866static PyObject *
9867posix_unlinkat(PyObject *self, PyObject *args)
9868{
9869 int dirfd, res, flags = 0;
9870 PyObject *opath;
9871 char *path;
9872
9873 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9874 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9875 return NULL;
9876 path = PyBytes_AsString(opath);
9877 Py_BEGIN_ALLOW_THREADS
9878 res = unlinkat(dirfd, path, flags);
9879 Py_END_ALLOW_THREADS
9880 Py_DECREF(opath);
9881 if (res < 0)
9882 return posix_error();
9883 Py_RETURN_NONE;
9884}
9885#endif
9886
9887#ifdef HAVE_UTIMENSAT
9888PyDoc_STRVAR(posix_utimensat__doc__,
Brian Curtin569b4942011-11-07 16:09:20 -06009889"utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\
9890 mtime=(mtime_sec, mtime_nsec), flags=0])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009891utimensat(dirfd, path, None, None, flags)\n\n\
9892Updates the timestamps of a file with nanosecond precision. If path is\n\
9893relative, it is taken as relative to dirfd.\n\
Brian Curtin569b4942011-11-07 16:09:20 -06009894If atime and mtime are both None, which is the default, set atime and\n\
9895mtime to the current time.\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009896flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9897If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9898is interpreted relative to the current working directory.\n\
9899If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9900current time.\n\
9901If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9902
9903static PyObject *
Brian Curtin569b4942011-11-07 16:09:20 -06009904posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009905{
9906 PyObject *opath;
9907 char *path;
9908 int res, dirfd, flags = 0;
Brian Curtin569b4942011-11-07 16:09:20 -06009909 PyObject *atime = Py_None;
9910 PyObject *mtime = Py_None;
9911
9912 static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL};
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009913
9914 struct timespec buf[2];
9915
Brian Curtin569b4942011-11-07 16:09:20 -06009916 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist,
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009917 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9918 return NULL;
9919 path = PyBytes_AsString(opath);
9920 if (atime == Py_None && mtime == Py_None) {
9921 /* optional time values not given */
9922 Py_BEGIN_ALLOW_THREADS
9923 res = utimensat(dirfd, path, NULL, flags);
9924 Py_END_ALLOW_THREADS
9925 }
9926 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9927 PyErr_SetString(PyExc_TypeError,
9928 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9929 Py_DECREF(opath);
9930 return NULL;
9931 }
9932 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9933 PyErr_SetString(PyExc_TypeError,
9934 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9935 Py_DECREF(opath);
9936 return NULL;
9937 }
9938 else {
9939 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9940 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9941 Py_DECREF(opath);
9942 return NULL;
9943 }
9944 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9945 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9946 Py_DECREF(opath);
9947 return NULL;
9948 }
9949 Py_BEGIN_ALLOW_THREADS
9950 res = utimensat(dirfd, path, buf, flags);
9951 Py_END_ALLOW_THREADS
9952 }
9953 Py_DECREF(opath);
9954 if (res < 0) {
9955 return posix_error();
9956 }
9957 Py_RETURN_NONE;
9958}
9959#endif
9960
9961#ifdef HAVE_MKFIFOAT
9962PyDoc_STRVAR(posix_mkfifoat__doc__,
9963"mkfifoat(dirfd, path, mode=0o666)\n\n\
9964Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9965If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9966is interpreted relative to the current working directory.");
9967
9968static PyObject *
9969posix_mkfifoat(PyObject *self, PyObject *args)
9970{
9971 PyObject *opath;
9972 char *filename;
9973 int mode = 0666;
9974 int res, dirfd;
9975 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9976 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9977 return NULL;
9978 filename = PyBytes_AS_STRING(opath);
9979 Py_BEGIN_ALLOW_THREADS
9980 res = mkfifoat(dirfd, filename, mode);
9981 Py_END_ALLOW_THREADS
9982 Py_DECREF(opath);
9983 if (res < 0)
9984 return posix_error();
9985 Py_RETURN_NONE;
9986}
9987#endif
9988
Benjamin Peterson9428d532011-09-14 11:45:52 -04009989#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -04009990
9991static int
9992try_getxattr(const char *path, const char *name,
9993 ssize_t (*get)(const char *, const char *, void *, size_t),
9994 Py_ssize_t buf_size, PyObject **res)
9995{
9996 PyObject *value;
9997 Py_ssize_t len;
9998
9999 assert(buf_size <= XATTR_SIZE_MAX);
10000 value = PyBytes_FromStringAndSize(NULL, buf_size);
10001 if (!value)
10002 return 0;
10003 Py_BEGIN_ALLOW_THREADS;
10004 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10005 Py_END_ALLOW_THREADS;
10006 if (len < 0) {
10007 Py_DECREF(value);
10008 if (errno == ERANGE) {
10009 value = NULL;
10010 }
10011 else {
10012 posix_error();
10013 return 0;
10014 }
10015 }
10016 else if (len != buf_size) {
10017 /* Can only shrink. */
10018 _PyBytes_Resize(&value, len);
10019 }
10020 *res = value;
10021 return 1;
10022}
10023
10024static PyObject *
10025getxattr_common(const char *path, PyObject *name_obj,
10026 ssize_t (*get)(const char *, const char *, void *, size_t))
10027{
10028 PyObject *value;
10029 const char *name = PyBytes_AS_STRING(name_obj);
10030
10031 /* Try a small value first. */
10032 if (!try_getxattr(path, name, get, 128, &value))
10033 return NULL;
10034 if (value)
10035 return value;
10036 /* Now the maximum possible one. */
10037 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10038 return NULL;
10039 assert(value);
10040 return value;
10041}
10042
10043PyDoc_STRVAR(posix_getxattr__doc__,
10044"getxattr(path, attr) -> value\n\n\
10045Return the value of extended attribute *name* on *path*.");
10046
10047static PyObject *
10048posix_getxattr(PyObject *self, PyObject *args)
10049{
10050 PyObject *path, *res, *name;
10051
10052 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10053 PyUnicode_FSConverter, &name))
10054 return NULL;
10055 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10056 Py_DECREF(path);
10057 Py_DECREF(name);
10058 return res;
10059}
10060
10061PyDoc_STRVAR(posix_lgetxattr__doc__,
10062"lgetxattr(path, attr) -> value\n\n\
10063Like getxattr but don't follow symlinks.");
10064
10065static PyObject *
10066posix_lgetxattr(PyObject *self, PyObject *args)
10067{
10068 PyObject *path, *res, *name;
10069
10070 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10071 PyUnicode_FSConverter, &name))
10072 return NULL;
10073 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10074 Py_DECREF(path);
10075 Py_DECREF(name);
10076 return res;
10077}
10078
10079static ssize_t
10080wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10081{
10082 /* Hack to share code. */
10083 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10084}
10085
10086PyDoc_STRVAR(posix_fgetxattr__doc__,
10087"fgetxattr(fd, attr) -> value\n\n\
10088Like getxattr but operate on a fd instead of a path.");
10089
10090static PyObject *
10091posix_fgetxattr(PyObject *self, PyObject *args)
10092{
10093 PyObject *res, *name;
10094 int fd;
10095
10096 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10097 return NULL;
10098 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10099 Py_DECREF(name);
10100 return res;
10101}
10102
10103PyDoc_STRVAR(posix_setxattr__doc__,
10104"setxattr(path, attr, value, flags=0)\n\n\
10105Set extended attribute *attr* on *path* to *value*.");
10106
10107static PyObject *
10108posix_setxattr(PyObject *self, PyObject *args)
10109{
10110 PyObject *path, *name;
10111 Py_buffer data;
10112 int flags = 0, err;
10113
10114 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10115 &path, PyUnicode_FSConverter, &name, &data, &flags))
10116 return NULL;
10117 Py_BEGIN_ALLOW_THREADS;
10118 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10119 data.buf, data.len, flags);
10120 Py_END_ALLOW_THREADS;
10121 Py_DECREF(path);
10122 Py_DECREF(name);
10123 PyBuffer_Release(&data);
10124 if (err)
10125 return posix_error();
10126 Py_RETURN_NONE;
10127}
10128
10129PyDoc_STRVAR(posix_lsetxattr__doc__,
10130"lsetxattr(path, attr, value, flags=0)\n\n\
10131Like setxattr but don't follow symlinks.");
10132
10133static PyObject *
10134posix_lsetxattr(PyObject *self, PyObject *args)
10135{
10136 PyObject *path, *name;
10137 Py_buffer data;
10138 int flags = 0, err;
10139
10140 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10141 &path, PyUnicode_FSConverter, &name, &data, &flags))
10142 return NULL;
10143 Py_BEGIN_ALLOW_THREADS;
10144 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10145 data.buf, data.len, flags);
10146 Py_END_ALLOW_THREADS;
10147 Py_DECREF(path);
10148 Py_DECREF(name);
10149 PyBuffer_Release(&data);
10150 if (err)
10151 return posix_error();
10152 Py_RETURN_NONE;
10153}
10154
10155PyDoc_STRVAR(posix_fsetxattr__doc__,
10156"fsetxattr(fd, attr, value, flags=0)\n\n\
10157Like setxattr but operates on *fd* instead of a path.");
10158
10159static PyObject *
10160posix_fsetxattr(PyObject *self, PyObject *args)
10161{
10162 Py_buffer data;
10163 const char *name;
10164 int fd, flags = 0, err;
10165
10166 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10167 &name, &data, &flags))
10168 return NULL;
10169 Py_BEGIN_ALLOW_THREADS;
10170 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10171 Py_END_ALLOW_THREADS;
10172 Py_DECREF(name);
10173 PyBuffer_Release(&data);
10174 if (err)
10175 return posix_error();
10176 Py_RETURN_NONE;
10177}
10178
10179PyDoc_STRVAR(posix_removexattr__doc__,
10180"removexattr(path, attr)\n\n\
10181Remove extended attribute *attr* on *path*.");
10182
10183static PyObject *
10184posix_removexattr(PyObject *self, PyObject *args)
10185{
10186 PyObject *path, *name;
10187 int err;
10188
10189 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10190 PyUnicode_FSConverter, &name))
10191 return NULL;
10192 Py_BEGIN_ALLOW_THREADS;
10193 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10194 Py_END_ALLOW_THREADS;
10195 Py_DECREF(path);
10196 Py_DECREF(name);
10197 if (err)
10198 return posix_error();
10199 Py_RETURN_NONE;
10200}
10201
10202PyDoc_STRVAR(posix_lremovexattr__doc__,
10203"lremovexattr(path, attr)\n\n\
10204Like removexattr but don't follow symlinks.");
10205
10206static PyObject *
10207posix_lremovexattr(PyObject *self, PyObject *args)
10208{
10209 PyObject *path, *name;
10210 int err;
10211
10212 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10213 PyUnicode_FSConverter, &name))
10214 return NULL;
10215 Py_BEGIN_ALLOW_THREADS;
10216 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10217 Py_END_ALLOW_THREADS;
10218 Py_DECREF(path);
10219 Py_DECREF(name);
10220 if (err)
10221 return posix_error();
10222 Py_RETURN_NONE;
10223}
10224
10225PyDoc_STRVAR(posix_fremovexattr__doc__,
10226"fremovexattr(fd, attr)\n\n\
10227Like removexattr but operates on a file descriptor.");
10228
10229static PyObject *
10230posix_fremovexattr(PyObject *self, PyObject *args)
10231{
10232 PyObject *name;
10233 int fd, err;
10234
10235 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10236 PyUnicode_FSConverter, &name))
10237 return NULL;
10238 Py_BEGIN_ALLOW_THREADS;
10239 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10240 Py_END_ALLOW_THREADS;
10241 Py_DECREF(name);
10242 if (err)
10243 return posix_error();
10244 Py_RETURN_NONE;
10245}
10246
10247static Py_ssize_t
10248try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10249 Py_ssize_t buf_size, char **buf)
10250{
10251 Py_ssize_t len;
10252
10253 *buf = PyMem_MALLOC(buf_size);
10254 if (!*buf) {
10255 PyErr_NoMemory();
10256 return -1;
10257 }
10258 Py_BEGIN_ALLOW_THREADS;
10259 len = list(path, *buf, buf_size);
10260 Py_END_ALLOW_THREADS;
10261 if (len < 0) {
10262 PyMem_FREE(*buf);
10263 if (errno != ERANGE)
10264 posix_error();
10265 return -1;
10266 }
10267 return len;
10268}
10269
10270static PyObject *
10271listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10272{
10273 PyObject *res, *attr;
10274 Py_ssize_t len, err, start, i;
10275 char *buf;
10276
10277 len = try_listxattr(path, list, 256, &buf);
10278 if (len < 0) {
10279 if (PyErr_Occurred())
10280 return NULL;
10281 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10282 if (len < 0)
10283 return NULL;
10284 }
10285 res = PyList_New(0);
10286 if (!res) {
10287 PyMem_FREE(buf);
10288 return NULL;
10289 }
10290 for (start = i = 0; i < len; i++) {
10291 if (!buf[i]) {
10292 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10293 if (!attr) {
10294 Py_DECREF(res);
10295 PyMem_FREE(buf);
10296 return NULL;
10297 }
10298 err = PyList_Append(res, attr);
10299 Py_DECREF(attr);
10300 if (err) {
10301 Py_DECREF(res);
10302 PyMem_FREE(buf);
10303 return NULL;
10304 }
10305 start = i + 1;
10306 }
10307 }
10308 PyMem_FREE(buf);
10309 return res;
10310}
10311
10312PyDoc_STRVAR(posix_listxattr__doc__,
10313"listxattr(path)\n\n\
10314Return a list of extended attributes on *path*.");
10315
10316static PyObject *
10317posix_listxattr(PyObject *self, PyObject *args)
10318{
10319 PyObject *path, *res;
10320
10321 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10322 return NULL;
10323 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10324 Py_DECREF(path);
10325 return res;
10326}
10327
10328PyDoc_STRVAR(posix_llistxattr__doc__,
10329"llistxattr(path)\n\n\
10330Like listxattr but don't follow symlinks..");
10331
10332static PyObject *
10333posix_llistxattr(PyObject *self, PyObject *args)
10334{
10335 PyObject *path, *res;
10336
10337 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10338 return NULL;
10339 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10340 Py_DECREF(path);
10341 return res;
10342}
10343
10344static ssize_t
10345wrap_flistxattr(const char *path, char *buf, size_t len)
10346{
10347 /* Hack to share code. */
10348 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10349}
10350
10351PyDoc_STRVAR(posix_flistxattr__doc__,
10352"flistxattr(path)\n\n\
10353Like flistxattr but operates on a file descriptor.");
10354
10355static PyObject *
10356posix_flistxattr(PyObject *self, PyObject *args)
10357{
10358 long fd;
10359
10360 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10361 return NULL;
10362 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10363}
10364
Benjamin Peterson9428d532011-09-14 11:45:52 -040010365#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010366
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010367
Georg Brandl2fb477c2012-02-21 00:33:36 +010010368PyDoc_STRVAR(posix_urandom__doc__,
10369"urandom(n) -> str\n\n\
10370Return n random bytes suitable for cryptographic use.");
10371
10372static PyObject *
10373posix_urandom(PyObject *self, PyObject *args)
10374{
10375 Py_ssize_t size;
10376 PyObject *result;
10377 int ret;
10378
10379 /* Read arguments */
10380 if (!PyArg_ParseTuple(args, "n:urandom", &size))
10381 return NULL;
10382 if (size < 0)
10383 return PyErr_Format(PyExc_ValueError,
10384 "negative argument not allowed");
10385 result = PyBytes_FromStringAndSize(NULL, size);
10386 if (result == NULL)
10387 return NULL;
10388
10389 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
10390 PyBytes_GET_SIZE(result));
10391 if (ret == -1) {
10392 Py_DECREF(result);
10393 return NULL;
10394 }
10395 return result;
10396}
10397
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010398/* Terminal size querying */
10399
10400static PyTypeObject TerminalSizeType;
10401
10402PyDoc_STRVAR(TerminalSize_docstring,
10403 "A tuple of (columns, lines) for holding terminal window size");
10404
10405static PyStructSequence_Field TerminalSize_fields[] = {
10406 {"columns", "width of the terminal window in characters"},
10407 {"lines", "height of the terminal window in characters"},
10408 {NULL, NULL}
10409};
10410
10411static PyStructSequence_Desc TerminalSize_desc = {
10412 "os.terminal_size",
10413 TerminalSize_docstring,
10414 TerminalSize_fields,
10415 2,
10416};
10417
10418#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10419PyDoc_STRVAR(termsize__doc__,
10420 "Return the size of the terminal window as (columns, lines).\n" \
10421 "\n" \
10422 "The optional argument fd (default standard output) specifies\n" \
10423 "which file descriptor should be queried.\n" \
10424 "\n" \
10425 "If the file descriptor is not connected to a terminal, an OSError\n" \
10426 "is thrown.\n" \
10427 "\n" \
10428 "This function will only be defined if an implementation is\n" \
10429 "available for this system.\n" \
10430 "\n" \
10431 "shutil.get_terminal_size is the high-level function which should \n" \
10432 "normally be used, os.get_terminal_size is the low-level implementation.");
10433
10434static PyObject*
10435get_terminal_size(PyObject *self, PyObject *args)
10436{
10437 int columns, lines;
10438 PyObject *termsize;
10439
10440 int fd = fileno(stdout);
10441 /* Under some conditions stdout may not be connected and
10442 * fileno(stdout) may point to an invalid file descriptor. For example
10443 * GUI apps don't have valid standard streams by default.
10444 *
10445 * If this happens, and the optional fd argument is not present,
10446 * the ioctl below will fail returning EBADF. This is what we want.
10447 */
10448
10449 if (!PyArg_ParseTuple(args, "|i", &fd))
10450 return NULL;
10451
10452#ifdef TERMSIZE_USE_IOCTL
10453 {
10454 struct winsize w;
10455 if (ioctl(fd, TIOCGWINSZ, &w))
10456 return PyErr_SetFromErrno(PyExc_OSError);
10457 columns = w.ws_col;
10458 lines = w.ws_row;
10459 }
10460#endif /* TERMSIZE_USE_IOCTL */
10461
10462#ifdef TERMSIZE_USE_CONIO
10463 {
10464 DWORD nhandle;
10465 HANDLE handle;
10466 CONSOLE_SCREEN_BUFFER_INFO csbi;
10467 switch (fd) {
10468 case 0: nhandle = STD_INPUT_HANDLE;
10469 break;
10470 case 1: nhandle = STD_OUTPUT_HANDLE;
10471 break;
10472 case 2: nhandle = STD_ERROR_HANDLE;
10473 break;
10474 default:
10475 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10476 }
10477 handle = GetStdHandle(nhandle);
10478 if (handle == NULL)
10479 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10480 if (handle == INVALID_HANDLE_VALUE)
10481 return PyErr_SetFromWindowsErr(0);
10482
10483 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10484 return PyErr_SetFromWindowsErr(0);
10485
10486 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10487 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10488 }
10489#endif /* TERMSIZE_USE_CONIO */
10490
10491 termsize = PyStructSequence_New(&TerminalSizeType);
10492 if (termsize == NULL)
10493 return NULL;
10494 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10495 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10496 if (PyErr_Occurred()) {
10497 Py_DECREF(termsize);
10498 return NULL;
10499 }
10500 return termsize;
10501}
10502#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10503
10504
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010505static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010507#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010508 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010509#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010510 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010511#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010513#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010514 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010515#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010516 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010517#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010518#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010520#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010521#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010523#endif /* HAVE_LCHMOD */
10524#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010525 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010526#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010527#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010528 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010529#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010530#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010531 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010532#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010533#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010534 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010535#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010536#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010537 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010538#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010539#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010540 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10541 METH_NOARGS, posix_getcwd__doc__},
10542 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10543 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010544#endif
10545#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010546 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010547#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010549#ifdef HAVE_FDOPENDIR
Charles-François Natali77940902012-02-06 19:54:48 +010010550 {"flistdir", posix_flistdir, METH_VARARGS, posix_flistdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010551#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010552 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010553 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010554#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010555 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010556#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010557#ifdef HAVE_GETPRIORITY
10558 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10559#endif /* HAVE_GETPRIORITY */
10560#ifdef HAVE_SETPRIORITY
10561 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10562#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010563#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010564 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010565#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010566#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010567 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010568#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010569 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
Antoine Pitrouf3b2d882012-01-30 22:08:52 +010010570 {"replace", posix_replace, METH_VARARGS, posix_replace__doc__},
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010571 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
Victor Stinner4195b5c2012-02-08 23:03:19 +010010572 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010574#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010576#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010577#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010578 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010579 win_symlink__doc__},
10580#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010581#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010582 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010583#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010585#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010586 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010587#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010588 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10589 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10590 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010591#ifdef HAVE_FUTIMES
10592 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10593#endif
10594#ifdef HAVE_LUTIMES
10595 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10596#endif
10597#ifdef HAVE_FUTIMENS
10598 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10599#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010600#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010601 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010602#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010603 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010604#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10606 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010607#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010608#ifdef HAVE_FEXECVE
10609 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10610#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010611#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010612 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10613 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010614#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010615 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10616 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010617#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010618#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010619#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010621#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010622#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010623 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010624#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010625#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010626#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010627 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10628 {"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 +020010629#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010630#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010631 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010632#endif
10633#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010634 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010635#endif
10636#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010637 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010638#endif
10639#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010640 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010641#endif
10642#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010643 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010644#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010645 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010646#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010647 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10648 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10649#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010650#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010651#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010653#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010654#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010655 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010656#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010657#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010658 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010659#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010660#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010662#endif /* HAVE_GETEUID */
10663#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010665#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010666#ifdef HAVE_GETGROUPLIST
10667 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10668#endif
Fred Drakec9680921999-12-13 16:37:25 +000010669#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010671#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010672 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010673#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010675#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010676#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010678#endif /* HAVE_GETPPID */
10679#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010681#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010682#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010684#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010685#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010687#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010688#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010690#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010691#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010693#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010694#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10696 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010697 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010698#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010699#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010701#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010702#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010704#endif /* HAVE_SETEUID */
10705#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010707#endif /* HAVE_SETEGID */
10708#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010710#endif /* HAVE_SETREUID */
10711#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010712 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010713#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010714#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010715 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010716#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010717#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010718 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010719#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010720#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010721 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010722#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010723#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010724 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010725#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010726#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010727 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010728#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010729#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010730 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010731#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010732#ifdef HAVE_WAIT3
Victor Stinner4195b5c2012-02-08 23:03:19 +010010733 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010734#endif /* HAVE_WAIT3 */
10735#ifdef HAVE_WAIT4
Victor Stinner4195b5c2012-02-08 23:03:19 +010010736 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010737#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010738#if defined(HAVE_WAITID) && !defined(__APPLE__)
10739 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10740#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010741#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010742 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010743#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010744#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010745 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010746#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010747#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010748 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010749#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010750#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010751 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010752#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010753#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010754 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010755#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010756#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010757 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010758#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010759 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10760 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10761 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10762 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10763 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10764 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010765#ifdef HAVE_LOCKF
10766 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10767#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010768 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10769 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010770#ifdef HAVE_READV
10771 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10772#endif
10773#ifdef HAVE_PREAD
10774 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10775#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010777#ifdef HAVE_WRITEV
10778 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10779#endif
10780#ifdef HAVE_PWRITE
10781 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10782#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010783#ifdef HAVE_SENDFILE
10784 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10785 posix_sendfile__doc__},
10786#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010787 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010789#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010790 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010791#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010792#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010793 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010794#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010795#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010797#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010798#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010799 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010800#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010801#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010802 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10803 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10804 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010805#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010806#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010807 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010808#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010809#ifdef HAVE_TRUNCATE
10810 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10811#endif
10812#ifdef HAVE_POSIX_FALLOCATE
10813 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10814#endif
10815#ifdef HAVE_POSIX_FADVISE
10816 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10817#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010818#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010819 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010820#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010821#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010823#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010825#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010826 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010827#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010828#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010829 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010830#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010831#ifdef HAVE_SYNC
10832 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10833#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010834#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010835 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010836#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010837#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010838#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010839 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010840#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010841#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010842 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010843#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010844#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010845 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010846#endif /* WIFSTOPPED */
10847#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010848 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010849#endif /* WIFSIGNALED */
10850#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010852#endif /* WIFEXITED */
10853#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010855#endif /* WEXITSTATUS */
10856#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010858#endif /* WTERMSIG */
10859#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010861#endif /* WSTOPSIG */
10862#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010863#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010864 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010865#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010866#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010867 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010868#endif
Fred Drakec9680921999-12-13 16:37:25 +000010869#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010870 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010871#endif
10872#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010873 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010874#endif
10875#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010876 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010877#endif
10878#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010879 {"pathconf", posix_pathconf, METH_VARARGS, 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 Curtin62857742010-09-06 17:07:27 +000010885 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010886 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010887 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010888#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010889#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010890 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010891#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010010892 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010893#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010894 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010895#endif
10896#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010897 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010898#endif
10899#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010900 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010901#endif
10902#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010903 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010904#endif
10905
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010906/* posix *at family of functions */
10907#ifdef HAVE_FACCESSAT
10908 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10909#endif
10910#ifdef HAVE_FCHMODAT
10911 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10912#endif /* HAVE_FCHMODAT */
10913#ifdef HAVE_FCHOWNAT
10914 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10915#endif /* HAVE_FCHOWNAT */
10916#ifdef HAVE_FSTATAT
Victor Stinner4195b5c2012-02-08 23:03:19 +010010917 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010918#endif
10919#ifdef HAVE_FUTIMESAT
10920 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10921#endif
10922#ifdef HAVE_LINKAT
10923 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10924#endif /* HAVE_LINKAT */
10925#ifdef HAVE_MKDIRAT
10926 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10927#endif
10928#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10929 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10930#endif
10931#ifdef HAVE_OPENAT
10932 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10933#endif
10934#ifdef HAVE_READLINKAT
10935 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10936#endif /* HAVE_READLINKAT */
10937#ifdef HAVE_RENAMEAT
10938 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10939#endif
10940#if HAVE_SYMLINKAT
10941 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10942#endif /* HAVE_SYMLINKAT */
10943#ifdef HAVE_UNLINKAT
10944 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10945#endif
10946#ifdef HAVE_UTIMENSAT
Jesus Cead03a4912011-11-08 17:28:04 +010010947 {"utimensat", (PyCFunction)posix_utimensat,
10948 METH_VARARGS | METH_KEYWORDS,
Brian Curtin569b4942011-11-07 16:09:20 -060010949 posix_utimensat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010950#endif
10951#ifdef HAVE_MKFIFOAT
10952 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10953#endif
Benjamin Peterson9428d532011-09-14 11:45:52 -040010954#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010955 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
10956 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
10957 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
10958 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
10959 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
10960 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
10961 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
10962 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
10963 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
10964 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
10965 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
10966 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
10967#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010968#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10969 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
10970#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010972};
10973
10974
Barry Warsaw4a342091996-12-19 23:50:02 +000010975static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010976ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010977{
Victor Stinner8c62be82010-05-06 00:08:46 +000010978 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010979}
10980
Guido van Rossumd48f2521997-12-05 22:19:34 +000010981#if defined(PYOS_OS2)
10982/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010983static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010984{
10985 APIRET rc;
10986 ULONG values[QSV_MAX+1];
10987 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010988 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010989
10990 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010991 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010992 Py_END_ALLOW_THREADS
10993
10994 if (rc != NO_ERROR) {
10995 os2_error(rc);
10996 return -1;
10997 }
10998
Fred Drake4d1e64b2002-04-15 19:40:07 +000010999 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
11000 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
11001 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
11002 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
11003 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
11004 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
11005 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011006
11007 switch (values[QSV_VERSION_MINOR]) {
11008 case 0: ver = "2.00"; break;
11009 case 10: ver = "2.10"; break;
11010 case 11: ver = "2.11"; break;
11011 case 30: ver = "3.00"; break;
11012 case 40: ver = "4.00"; break;
11013 case 50: ver = "5.00"; break;
11014 default:
Tim Peters885d4572001-11-28 20:27:42 +000011015 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000011016 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000011017 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011018 ver = &tmp[0];
11019 }
11020
11021 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011022 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011023 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011024
11025 /* Add Indicator of Which Drive was Used to Boot the System */
11026 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
11027 tmp[1] = ':';
11028 tmp[2] = '\0';
11029
Fred Drake4d1e64b2002-04-15 19:40:07 +000011030 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011031}
11032#endif
11033
Brian Curtin52173d42010-12-02 18:29:18 +000011034#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011035static int
Brian Curtin52173d42010-12-02 18:29:18 +000011036enable_symlink()
11037{
11038 HANDLE tok;
11039 TOKEN_PRIVILEGES tok_priv;
11040 LUID luid;
11041 int meth_idx = 0;
11042
11043 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011044 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011045
11046 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011047 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011048
11049 tok_priv.PrivilegeCount = 1;
11050 tok_priv.Privileges[0].Luid = luid;
11051 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
11052
11053 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
11054 sizeof(TOKEN_PRIVILEGES),
11055 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011056 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011057
Brian Curtin3b4499c2010-12-28 14:31:47 +000011058 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
11059 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000011060}
11061#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
11062
Barry Warsaw4a342091996-12-19 23:50:02 +000011063static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011064all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000011065{
Guido van Rossum94f6f721999-01-06 18:42:14 +000011066#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011068#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011069#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011071#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011072#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011074#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011075#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011077#endif
Fred Drakec9680921999-12-13 16:37:25 +000011078#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000011080#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011081#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011083#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011084#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011086#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011087#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011089#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011090#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011092#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011093#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011095#endif
11096#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011098#endif
11099#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011101#endif
11102#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011104#endif
11105#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011107#endif
11108#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011110#endif
11111#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011113#endif
11114#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011116#endif
11117#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011119#endif
11120#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011121 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011122#endif
11123#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011124 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011125#endif
11126#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011127 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011128#endif
11129#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011131#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011132#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011133 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011134#endif
11135#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011136 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011137#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011138#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011140#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011141#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011142 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011143#endif
11144#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011145 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011146#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011147#ifdef PRIO_PROCESS
11148 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11149#endif
11150#ifdef PRIO_PGRP
11151 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11152#endif
11153#ifdef PRIO_USER
11154 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11155#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011156#ifdef O_CLOEXEC
11157 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11158#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011159/* posix - constants for *at functions */
11160#ifdef AT_SYMLINK_NOFOLLOW
11161 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11162#endif
11163#ifdef AT_EACCESS
11164 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11165#endif
11166#ifdef AT_FDCWD
11167 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11168#endif
11169#ifdef AT_REMOVEDIR
11170 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11171#endif
11172#ifdef AT_SYMLINK_FOLLOW
11173 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11174#endif
11175#ifdef UTIME_NOW
11176 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11177#endif
11178#ifdef UTIME_OMIT
11179 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11180#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011181
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011182
Tim Peters5aa91602002-01-30 05:46:57 +000011183/* MS Windows */
11184#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011185 /* Don't inherit in child processes. */
11186 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011187#endif
11188#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 /* Optimize for short life (keep in memory). */
11190 /* MS forgot to define this one with a non-underscore form too. */
11191 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011192#endif
11193#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011194 /* Automatically delete when last handle is closed. */
11195 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011196#endif
11197#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011198 /* Optimize for random access. */
11199 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011200#endif
11201#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 /* Optimize for sequential access. */
11203 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011204#endif
11205
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011206/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011207#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011208 /* Send a SIGIO signal whenever input or output
11209 becomes available on file descriptor */
11210 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011211#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011212#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011213 /* Direct disk access. */
11214 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011215#endif
11216#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011217 /* Must be a directory. */
11218 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011219#endif
11220#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011221 /* Do not follow links. */
11222 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011223#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011224#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 /* Do not update the access time. */
11226 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011227#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011228
Victor Stinner8c62be82010-05-06 00:08:46 +000011229 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011230#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011232#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011233#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011235#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011236#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011238#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011239#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011241#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011242#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011244#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011245#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011247#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011248#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011250#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011251#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011253#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011254#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011256#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011257#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011259#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011260#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011262#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011263#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011265#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011266#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011268#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011269#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011271#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011272#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011274#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011275#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011277#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011278#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011280#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011281
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011282 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011283#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011284 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011285#endif /* ST_RDONLY */
11286#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011287 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011288#endif /* ST_NOSUID */
11289
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011290 /* FreeBSD sendfile() constants */
11291#ifdef SF_NODISKIO
11292 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11293#endif
11294#ifdef SF_MNOWAIT
11295 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11296#endif
11297#ifdef SF_SYNC
11298 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11299#endif
11300
Ross Lagerwall7807c352011-03-17 20:20:30 +020011301 /* constants for posix_fadvise */
11302#ifdef POSIX_FADV_NORMAL
11303 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11304#endif
11305#ifdef POSIX_FADV_SEQUENTIAL
11306 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11307#endif
11308#ifdef POSIX_FADV_RANDOM
11309 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11310#endif
11311#ifdef POSIX_FADV_NOREUSE
11312 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11313#endif
11314#ifdef POSIX_FADV_WILLNEED
11315 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11316#endif
11317#ifdef POSIX_FADV_DONTNEED
11318 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11319#endif
11320
11321 /* constants for waitid */
11322#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11323 if (ins(d, "P_PID", (long)P_PID)) return -1;
11324 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11325 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11326#endif
11327#ifdef WEXITED
11328 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11329#endif
11330#ifdef WNOWAIT
11331 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11332#endif
11333#ifdef WSTOPPED
11334 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11335#endif
11336#ifdef CLD_EXITED
11337 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11338#endif
11339#ifdef CLD_DUMPED
11340 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11341#endif
11342#ifdef CLD_TRAPPED
11343 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11344#endif
11345#ifdef CLD_CONTINUED
11346 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11347#endif
11348
11349 /* constants for lockf */
11350#ifdef F_LOCK
11351 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11352#endif
11353#ifdef F_TLOCK
11354 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11355#endif
11356#ifdef F_ULOCK
11357 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11358#endif
11359#ifdef F_TEST
11360 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11361#endif
11362
11363 /* constants for futimens */
11364#ifdef UTIME_NOW
11365 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11366#endif
11367#ifdef UTIME_OMIT
11368 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11369#endif
11370
Guido van Rossum246bc171999-02-01 23:54:31 +000011371#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011372#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011373 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11374 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11375 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11376 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11377 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11378 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11379 if (ins(d, "P_PM", (long)P_PM)) return -1;
11380 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11381 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11382 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11383 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11384 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11385 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11386 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11387 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11388 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11389 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11390 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11391 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11392 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011393#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011394 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11395 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11396 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11397 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11398 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011399#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011400#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011401
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011402#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011403 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011404 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11405 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11406#ifdef SCHED_SPORADIC
11407 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11408#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011409#ifdef SCHED_BATCH
11410 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11411#endif
11412#ifdef SCHED_IDLE
11413 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11414#endif
11415#ifdef SCHED_RESET_ON_FORK
11416 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11417#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011418#ifdef SCHED_SYS
11419 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11420#endif
11421#ifdef SCHED_IA
11422 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11423#endif
11424#ifdef SCHED_FSS
11425 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11426#endif
11427#ifdef SCHED_FX
11428 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11429#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011430#endif
11431
Benjamin Peterson9428d532011-09-14 11:45:52 -040011432#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011433 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11434 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11435 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11436#endif
11437
Victor Stinner8b905bd2011-10-25 13:34:04 +020011438#ifdef RTLD_LAZY
11439 if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1;
11440#endif
11441#ifdef RTLD_NOW
11442 if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1;
11443#endif
11444#ifdef RTLD_GLOBAL
11445 if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1;
11446#endif
11447#ifdef RTLD_LOCAL
11448 if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1;
11449#endif
11450#ifdef RTLD_NODELETE
11451 if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1;
11452#endif
11453#ifdef RTLD_NOLOAD
11454 if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1;
11455#endif
11456#ifdef RTLD_DEEPBIND
11457 if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1;
11458#endif
11459
Guido van Rossumd48f2521997-12-05 22:19:34 +000011460#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011461 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011462#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011463 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011464}
11465
11466
Tim Peters5aa91602002-01-30 05:46:57 +000011467#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011468#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011469#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011470
11471#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011472#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011473#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011474
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011475#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011476#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011477#define MODNAME "posix"
11478#endif
11479
Martin v. Löwis1a214512008-06-11 05:26:20 +000011480static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011481 PyModuleDef_HEAD_INIT,
11482 MODNAME,
11483 posix__doc__,
11484 -1,
11485 posix_methods,
11486 NULL,
11487 NULL,
11488 NULL,
11489 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011490};
11491
11492
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011493PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011494INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011495{
Victor Stinner8c62be82010-05-06 00:08:46 +000011496 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011497
Brian Curtin52173d42010-12-02 18:29:18 +000011498#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011499 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011500#endif
11501
Victor Stinner8c62be82010-05-06 00:08:46 +000011502 m = PyModule_Create(&posixmodule);
11503 if (m == NULL)
11504 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011505
Victor Stinner8c62be82010-05-06 00:08:46 +000011506 /* Initialize environ dictionary */
11507 v = convertenviron();
11508 Py_XINCREF(v);
11509 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11510 return NULL;
11511 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011512
Victor Stinner8c62be82010-05-06 00:08:46 +000011513 if (all_ins(m))
11514 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011515
Victor Stinner8c62be82010-05-06 00:08:46 +000011516 if (setup_confname_tables(m))
11517 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011518
Victor Stinner8c62be82010-05-06 00:08:46 +000011519 Py_INCREF(PyExc_OSError);
11520 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011521
Benjamin Peterson2740af82011-08-02 17:41:34 -050011522#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011523 if (PyType_Ready(&cpu_set_type) < 0)
11524 return NULL;
11525 Py_INCREF(&cpu_set_type);
11526 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011527#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011528
Guido van Rossumb3d39562000-01-31 18:41:26 +000011529#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011530 if (posix_putenv_garbage == NULL)
11531 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011532#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011533
Victor Stinner8c62be82010-05-06 00:08:46 +000011534 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011535#if defined(HAVE_WAITID) && !defined(__APPLE__)
11536 waitid_result_desc.name = MODNAME ".waitid_result";
11537 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11538#endif
11539
Victor Stinner8c62be82010-05-06 00:08:46 +000011540 stat_result_desc.name = MODNAME ".stat_result";
11541 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11542 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11543 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11544 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11545 structseq_new = StatResultType.tp_new;
11546 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011547
Victor Stinner8c62be82010-05-06 00:08:46 +000011548 statvfs_result_desc.name = MODNAME ".statvfs_result";
11549 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011550#ifdef NEED_TICKS_PER_SECOND
11551# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011553# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011554 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011555# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011556 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011557# endif
11558#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011559
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011560#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011561 sched_param_desc.name = MODNAME ".sched_param";
11562 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11563 SchedParamType.tp_new = sched_param_new;
11564#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011565
11566 /* initialize TerminalSize_info */
11567 PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
11568 Py_INCREF(&TerminalSizeType);
Victor Stinner8c62be82010-05-06 00:08:46 +000011569 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011570#if defined(HAVE_WAITID) && !defined(__APPLE__)
11571 Py_INCREF((PyObject*) &WaitidResultType);
11572 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11573#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011574 Py_INCREF((PyObject*) &StatResultType);
11575 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11576 Py_INCREF((PyObject*) &StatVFSResultType);
11577 PyModule_AddObject(m, "statvfs_result",
11578 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011579
11580#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011581 Py_INCREF(&SchedParamType);
11582 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011583#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011584 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011585
11586#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011587 /*
11588 * Step 2 of weak-linking support on Mac OS X.
11589 *
11590 * The code below removes functions that are not available on the
11591 * currently active platform.
11592 *
11593 * This block allow one to use a python binary that was build on
11594 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
11595 * OSX 10.4.
11596 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011597#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011598 if (fstatvfs == NULL) {
11599 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11600 return NULL;
11601 }
11602 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011603#endif /* HAVE_FSTATVFS */
11604
11605#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011606 if (statvfs == NULL) {
11607 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11608 return NULL;
11609 }
11610 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011611#endif /* HAVE_STATVFS */
11612
11613# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011614 if (lchown == NULL) {
11615 if (PyObject_DelAttrString(m, "lchown") == -1) {
11616 return NULL;
11617 }
11618 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011619#endif /* HAVE_LCHOWN */
11620
11621
11622#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011623
11624 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
11625
Victor Stinner8c62be82010-05-06 00:08:46 +000011626 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011627
Guido van Rossumb6775db1994-08-01 11:34:53 +000011628}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011629
11630#ifdef __cplusplus
11631}
11632#endif