blob: 7b45154cf4fb4898533d7adf80225abf2fd7a7b8 [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 Peterson2dbda072012-03-16 10:12:55 -0500110#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500111#undef HAVE_SCHED_SETAFFINITY
112#endif
113
Benjamin Peterson9428d532011-09-14 11:45:52 -0400114#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
115#define USE_XATTRS
116#endif
117
118#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400119#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400120#endif
121
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000122#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
123#ifdef HAVE_SYS_SOCKET_H
124#include <sys/socket.h>
125#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000126#endif
127
Victor Stinner8b905bd2011-10-25 13:34:04 +0200128#ifdef HAVE_DLFCN_H
129#include <dlfcn.h>
130#endif
131
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100132#if defined(MS_WINDOWS)
133# define TERMSIZE_USE_CONIO
134#elif defined(HAVE_SYS_IOCTL_H)
135# include <sys/ioctl.h>
136# if defined(HAVE_TERMIOS_H)
137# include <termios.h>
138# endif
139# if defined(TIOCGWINSZ)
140# define TERMSIZE_USE_IOCTL
141# endif
142#endif /* MS_WINDOWS */
143
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000144/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000145/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000146#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000147#include <process.h>
148#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000149#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150#define HAVE_GETCWD 1
151#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#if defined(__OS2__)
154#define HAVE_EXECV 1
155#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000156#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000157#include <process.h>
158#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000159#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#define HAVE_EXECV 1
161#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162#define HAVE_OPENDIR 1
163#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000164#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#define HAVE_WAIT 1
166#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000168#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000169#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000170#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000171#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#define HAVE_EXECV 1
173#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000174#define HAVE_SYSTEM 1
175#define HAVE_CWAIT 1
176#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000177#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000178#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000179#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
180/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000181#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182/* Unix functions that the configure script doesn't check for */
183#define HAVE_EXECV 1
184#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000185#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000186#define HAVE_FORK1 1
187#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000188#define HAVE_GETCWD 1
189#define HAVE_GETEGID 1
190#define HAVE_GETEUID 1
191#define HAVE_GETGID 1
192#define HAVE_GETPPID 1
193#define HAVE_GETUID 1
194#define HAVE_KILL 1
195#define HAVE_OPENDIR 1
196#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000197#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000198#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000199#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000200#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000201#endif /* _MSC_VER */
202#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000203#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000204#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000205
Victor Stinnera2f7c002012-02-08 03:36:25 +0100206
207
208
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000211#if defined(__sgi)&&_COMPILER_VERSION>=700
212/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
213 (default) */
214extern char *ctermid_r(char *);
215#endif
216
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000217#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000218#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000219extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000220#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000221#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000222extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000223#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000226#endif
227#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000228extern int chdir(char *);
229extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000230#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000231extern int chdir(const char *);
232extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000233#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000234#ifdef __BORLANDC__
235extern int chmod(const char *, int);
236#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000237extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000238#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000239/*#ifdef HAVE_FCHMOD
240extern int fchmod(int, mode_t);
241#endif*/
242/*#ifdef HAVE_LCHMOD
243extern int lchmod(const char *, mode_t);
244#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000245extern int chown(const char *, uid_t, gid_t);
246extern char *getcwd(char *, int);
247extern char *strerror(int);
248extern int link(const char *, const char *);
249extern int rename(const char *, const char *);
250extern int stat(const char *, struct stat *);
251extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000253extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000254#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000256extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000257#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000259
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000260#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#ifdef HAVE_UTIME_H
263#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000264#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000266#ifdef HAVE_SYS_UTIME_H
267#include <sys/utime.h>
268#define HAVE_UTIME_H /* pretend we do for the rest of this file */
269#endif /* HAVE_SYS_UTIME_H */
270
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#ifdef HAVE_SYS_TIMES_H
272#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274
275#ifdef HAVE_SYS_PARAM_H
276#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000277#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278
279#ifdef HAVE_SYS_UTSNAME_H
280#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000281#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000282
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000283#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000285#define NAMLEN(dirent) strlen((dirent)->d_name)
286#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000287#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000288#include <direct.h>
289#define NAMLEN(dirent) strlen((dirent)->d_name)
290#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000292#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000293#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000294#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000296#endif
297#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000299#endif
300#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000302#endif
303#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000304
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000305#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308#endif
309#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000310#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000311#endif
312#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000313#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000314#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000315#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000316#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000317#endif
318#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000319#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000320#endif
321#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000322#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000323#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000325#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000326#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000327#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000328#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000329#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
330#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000331static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000332#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000333#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000334
Guido van Rossumd48f2521997-12-05 22:19:34 +0000335#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000336#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000337#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338
Tim Petersbc2e10e2002-03-03 23:17:02 +0000339#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340#if defined(PATH_MAX) && PATH_MAX > 1024
341#define MAXPATHLEN PATH_MAX
342#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000343#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000345#endif /* MAXPATHLEN */
346
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000347#ifdef UNION_WAIT
348/* Emulate some macros on systems that have a union instead of macros */
349
350#ifndef WIFEXITED
351#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
352#endif
353
354#ifndef WEXITSTATUS
355#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
356#endif
357
358#ifndef WTERMSIG
359#define WTERMSIG(u_wait) ((u_wait).w_termsig)
360#endif
361
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000362#define WAIT_TYPE union wait
363#define WAIT_STATUS_INT(s) (s.w_status)
364
365#else /* !UNION_WAIT */
366#define WAIT_TYPE int
367#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000368#endif /* UNION_WAIT */
369
Greg Wardb48bc172000-03-01 21:51:56 +0000370/* Don't use the "_r" form if we don't need it (also, won't have a
371 prototype for it, at least on Solaris -- maybe others as well?). */
372#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
373#define USE_CTERMID_R
374#endif
375
Fred Drake699f3522000-06-29 21:12:41 +0000376/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000377#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000378#undef FSTAT
379#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000380#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000381# define STAT win32_stat
382# define FSTAT win32_fstat
383# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000384#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000385# define STAT stat
386# define FSTAT fstat
387# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000388#endif
389
Tim Peters11b23062003-04-23 02:39:17 +0000390#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000391#include <sys/mkdev.h>
392#else
393#if defined(MAJOR_IN_SYSMACROS)
394#include <sys/sysmacros.h>
395#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000396#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
397#include <sys/mkdev.h>
398#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000399#endif
Fred Drake699f3522000-06-29 21:12:41 +0000400
Larry Hastingscfe6f2a2012-05-05 17:39:09 -0700401/*
402 * De-vararg'd PyArg_ParseTupleAndKeywords()
403 */
404typedef struct argument_path_s {
405 struct argument_path_s *next;
406 PyObject *object;
407 char *format;
408 wchar_t **wide;
409 char **narrow;
410 Py_ssize_t *length;
411} argument_path_t;
412
413typedef struct {
414 PyObject *args;
415 PyObject *kwargs;
416 char format[32];
417 char *format_trace;
418 char *keywords[8];
419 int keyword_count;
420 PyObject **varargs[16];
421 int vararg_count;
422 const char *function_name;
423 argument_path_t *path_head;
424} argument_parser_table_t;
425
426#define APT_DECLARE(apt, fname) \
427 argument_parser_table_t apt; \
428 memset(&apt, 0, sizeof(apt)); \
429 apt.function_name = fname; \
430 apt.args = args; \
431 apt.kwargs = kwargs; \
432 apt.format_trace = apt.format
433
434#define _APT_ARGUMENT_OBJECT(apt, name, variable, format) \
435 *apt.format_trace++ = (format); \
436 apt.keywords[apt.keyword_count++] = (name); \
437 apt.varargs[apt.vararg_count++] = (PyObject **)(variable); \
438
439#define APT_ARGUMENT_OBJECT(apt, name, variable) \
440 _APT_ARGUMENT_OBJECT(apt, name, variable, 'O')
441
442#define APT_ARGUMENT_OBJECT_WITH_CONVERTER(apt, name, variable, converter) \
443 apt.varargs[apt.vararg_count++] = (PyObject **)converter; \
444 APT_ARGUMENT_OBJECT(apt, name, variable); \
445 *apt.format_trace++ = '&'; \
446
447#define APT_ARGUMENT_UNICODE(apt, name, variable) \
448 _APT_ARGUMENT_OBJECT(apt, name, variable, 'U')
449
450#define APT_ARGUMENT_BYTES(apt, name, variable) \
451 _APT_ARGUMENT_OBJECT(apt, name, variable, 'y')
452
453#define APT_ARGUMENT_INT(apt, name, variable) \
454 _APT_ARGUMENT_OBJECT(apt, name, variable, 'i')
455
456static int
457bool_converter(PyObject *object, void *address) {
458 int *output = (int *)address;
459 int value = PyObject_IsTrue(object);
460 if (value == -1)
461 return 0;
462 *output = value;
463 return 1;
464}
465
466#define APT_ARGUMENT_BOOL(apt, name, variable) \
467 _APT_ARGUMENT_OBJECT(apt, name, variable, 'p')
468
469#if !MS_WINDOWS
470 #define _APT_DEFAULT_PATH_TYPE 'U'
471 #define _APT_PATH_BEFORE ;
472 #define _APT_PATH_AFTER ;
473#else
474 #define _APT_DEFAULT_PATH_TYPE 'O'
475 #define _APT_PATH_BEFORE apt.varargs[apt.vararg_count++] = (PyObject **)PyUnicode_FSConverter;
476 #define _APT_PATH_AFTER *apt.format_trace++ = '&';
477#endif
478
479#define APT_ARGUMENT_PATH(apt, name, w, n, l) \
480 { \
481 argument_path_t *_path_ = (argument_path_t *)alloca(sizeof(argument_path_t)); \
482 _APT_PATH_BEFORE; \
483 memset(_path_, 0, sizeof(*_path_)); \
484 _path_->wide = w; \
485 _path_->narrow = n; \
486 _path_->length = l; \
487 _path_->next = apt.path_head; \
488 apt.path_head = _path_; \
489 _APT_ARGUMENT_OBJECT(apt, name, &_path_->object, _APT_DEFAULT_PATH_TYPE); \
490 _APT_PATH_AFTER; \
491 } \
492
493#define APT_OPTIONAL(apt) \
494 *apt.format_trace++ = '|' \
495
496#define APT_KEYWORD_ONLY(apt) \
497 *apt.format_trace++ = '$' \
498
499
500static int apt_parse(argument_parser_table_t *apt) {
501 argument_path_t *trace;
502
503 *apt->format_trace++ = ':';
504 strcpy(apt->format_trace, apt->function_name);
505 apt->keywords[apt->keyword_count] = NULL;
506
507 for (;;) {
508 int result;
509
510 switch (apt->vararg_count) {
511 case 0:
512 PyErr_SetString(PyExc_RuntimeError, "suspiciously too few arguments for apt_parse");
513 return 0;
514
515 #define APT_PREAMBLE \
516 result = PyArg_ParseTupleAndKeywords(apt->args, apt->kwargs, apt->format, apt->keywords, \
517
518 #define APT_POSTSCRIPT \
519 ); \
520 break; \
521
522 case 1:
523 APT_PREAMBLE
524 apt->varargs[0]
525 APT_POSTSCRIPT
526 case 2:
527 APT_PREAMBLE
528 apt->varargs[0],
529 apt->varargs[1]
530 APT_POSTSCRIPT
531 case 3:
532 APT_PREAMBLE
533 apt->varargs[0],
534 apt->varargs[1],
535 apt->varargs[2]
536 APT_POSTSCRIPT
537 case 4:
538 APT_PREAMBLE
539 apt->varargs[0],
540 apt->varargs[1],
541 apt->varargs[2],
542 apt->varargs[3]
543 APT_POSTSCRIPT
544 case 5:
545 APT_PREAMBLE
546 apt->varargs[0],
547 apt->varargs[1],
548 apt->varargs[2],
549 apt->varargs[3],
550 apt->varargs[4]
551 APT_POSTSCRIPT
552 case 6:
553 APT_PREAMBLE
554 apt->varargs[0],
555 apt->varargs[1],
556 apt->varargs[2],
557 apt->varargs[3],
558 apt->varargs[4],
559 apt->varargs[5]
560 APT_POSTSCRIPT
561 case 7:
562 APT_PREAMBLE
563 apt->varargs[0],
564 apt->varargs[1],
565 apt->varargs[2],
566 apt->varargs[3],
567 apt->varargs[4],
568 apt->varargs[5],
569 apt->varargs[6]
570 APT_POSTSCRIPT
571 case 8:
572 APT_PREAMBLE
573 apt->varargs[0],
574 apt->varargs[1],
575 apt->varargs[2],
576 apt->varargs[3],
577 apt->varargs[4],
578 apt->varargs[5],
579 apt->varargs[6],
580 apt->varargs[7]
581 APT_POSTSCRIPT
582 default:
583 PyErr_SetString(PyExc_RuntimeError, "too many arguments for apt_parse");
584 return 0;
585 }
586
587 if (result) {
588 for (trace = apt->path_head; trace != NULL; trace = trace->next) {
589#if MS_WINDOWS
590 switch (*trace->format) {
591 case 'U':
592 *trace->wide = PyUnicode_AsUnicode(trace->object);
593 if (trace->length)
594 *trace->length = PyUnicode_GET_SIZE(trace->object);
595 break;
596 case 'y'
597 if (win32_warn_bytes_api())
598 return 0;
599 *trace->narrow = trace->object;
600 if (trace->length)
601 *trace->length = strlen((char *)trace->object);
602 break;
603 default:
604 PyErr_SetString(PyExc_RuntimeError, "unexpected format character in apt_parse");
605 return 0;
606 }
607#else
608 assert(*trace->format == 'O');
609 *trace->narrow = PyBytes_AsString(trace->object);
610 if (trace->length)
611 *trace->length = PyBytes_GET_SIZE(trace->object);
612#endif
613 }
614 return result;
615 }
616
617#if !MS_WINDOWS
618 break;
619#else
620 for (trace = apt->path_head; trace != NULL; trace = trace->next) {
621 /*
622 * try flipping paths between wide and narrow.
623 *
624 * each element always started with wide.
625 * so:
626 * * if we see a wide, flip it to narrow and stop.
627 * * if we see a narrow, flip it to wide and move on to the next field.
628 * * if we run out of fields, we have exhausted all possibilities. fail.
629 *
630 * (conceptually it helps to think of the fields as a binary number
631 * with as many digits as there are entries in the path list.
632 * wide is 0, narrow is 1. we keep incrementing the number
633 * until we wrap around to 0 again.)
634 */
635 if (*trace->format == 'U') {
636 *trace->format = 'y';
637 break;
638 }
639 if (*trace->format == 'y') {
640 *trace->format = 'U';
641 continue;
642 }
643 PyErr_SetString(PyExc_RuntimeError, "unexpected format character in apt_parse");
644 return 0;
645 }
646 if (!trace)
647 break;
648#endif
649 }
650
651 return 0;
652}
653
654static void apt_cleanup(argument_parser_table_t *apt) {
655#if !MS_WINDOWS
656 argument_path_t *trace;
657 for (trace = apt->path_head; trace != NULL; trace = trace->next) {
658 Py_XDECREF(trace->object);
659 }
660#endif
661}
662
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200663/* A helper used by a number of POSIX-only functions */
664#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000665static int
666_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000667{
668#if !defined(HAVE_LARGEFILE_SUPPORT)
669 *((off_t*)addr) = PyLong_AsLong(arg);
670#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000671 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000672#endif
673 if (PyErr_Occurred())
674 return 0;
675 return 1;
676}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200677#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000678
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000679#if defined _MSC_VER && _MSC_VER >= 1400
680/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
681 * valid and throw an assertion if it isn't.
682 * Normally, an invalid fd is likely to be a C program error and therefore
683 * an assertion can be useful, but it does contradict the POSIX standard
684 * which for write(2) states:
685 * "Otherwise, -1 shall be returned and errno set to indicate the error."
686 * "[EBADF] The fildes argument is not a valid file descriptor open for
687 * writing."
688 * Furthermore, python allows the user to enter any old integer
689 * as a fd and should merely raise a python exception on error.
690 * The Microsoft CRT doesn't provide an official way to check for the
691 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000692 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000693 * internal structures involved.
694 * The structures below must be updated for each version of visual studio
695 * according to the file internal.h in the CRT source, until MS comes
696 * up with a less hacky way to do this.
697 * (all of this is to avoid globally modifying the CRT behaviour using
698 * _set_invalid_parameter_handler() and _CrtSetReportMode())
699 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000700/* The actual size of the structure is determined at runtime.
701 * Only the first items must be present.
702 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000703typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000704 intptr_t osfhnd;
705 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000706} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000707
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000708extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000709#define IOINFO_L2E 5
710#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
711#define IOINFO_ARRAYS 64
712#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
713#define FOPEN 0x01
714#define _NO_CONSOLE_FILENO (intptr_t)-2
715
716/* This function emulates what the windows CRT does to validate file handles */
717int
718_PyVerify_fd(int fd)
719{
Victor Stinner8c62be82010-05-06 00:08:46 +0000720 const int i1 = fd >> IOINFO_L2E;
721 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000722
Antoine Pitrou22e41552010-08-15 18:07:50 +0000723 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000724
Victor Stinner8c62be82010-05-06 00:08:46 +0000725 /* Determine the actual size of the ioinfo structure,
726 * as used by the CRT loaded in memory
727 */
728 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
729 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
730 }
731 if (sizeof_ioinfo == 0) {
732 /* This should not happen... */
733 goto fail;
734 }
735
736 /* See that it isn't a special CLEAR fileno */
737 if (fd != _NO_CONSOLE_FILENO) {
738 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
739 * we check pointer validity and other info
740 */
741 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
742 /* finally, check that the file is open */
743 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
744 if (info->osfile & FOPEN) {
745 return 1;
746 }
747 }
748 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000749 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000750 errno = EBADF;
751 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000752}
753
754/* the special case of checking dup2. The target fd must be in a sensible range */
755static int
756_PyVerify_fd_dup2(int fd1, int fd2)
757{
Victor Stinner8c62be82010-05-06 00:08:46 +0000758 if (!_PyVerify_fd(fd1))
759 return 0;
760 if (fd2 == _NO_CONSOLE_FILENO)
761 return 0;
762 if ((unsigned)fd2 < _NHANDLE_)
763 return 1;
764 else
765 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000766}
767#else
768/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
769#define _PyVerify_fd_dup2(A, B) (1)
770#endif
771
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000772#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000773/* The following structure was copied from
774 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
775 include doesn't seem to be present in the Windows SDK (at least as included
776 with Visual Studio Express). */
777typedef struct _REPARSE_DATA_BUFFER {
778 ULONG ReparseTag;
779 USHORT ReparseDataLength;
780 USHORT Reserved;
781 union {
782 struct {
783 USHORT SubstituteNameOffset;
784 USHORT SubstituteNameLength;
785 USHORT PrintNameOffset;
786 USHORT PrintNameLength;
787 ULONG Flags;
788 WCHAR PathBuffer[1];
789 } SymbolicLinkReparseBuffer;
790
791 struct {
792 USHORT SubstituteNameOffset;
793 USHORT SubstituteNameLength;
794 USHORT PrintNameOffset;
795 USHORT PrintNameLength;
796 WCHAR PathBuffer[1];
797 } MountPointReparseBuffer;
798
799 struct {
800 UCHAR DataBuffer[1];
801 } GenericReparseBuffer;
802 };
803} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
804
805#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
806 GenericReparseBuffer)
807#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
808
809static int
Brian Curtind25aef52011-06-13 15:16:04 -0500810win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000811{
812 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
813 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
814 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000815
816 if (0 == DeviceIoControl(
817 reparse_point_handle,
818 FSCTL_GET_REPARSE_POINT,
819 NULL, 0, /* in buffer */
820 target_buffer, sizeof(target_buffer),
821 &n_bytes_returned,
822 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500823 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000824
825 if (reparse_tag)
826 *reparse_tag = rdb->ReparseTag;
827
Brian Curtind25aef52011-06-13 15:16:04 -0500828 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000829}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100830
831static int
832win32_warn_bytes_api()
833{
834 return PyErr_WarnEx(PyExc_DeprecationWarning,
835 "The Windows bytes API has been deprecated, "
836 "use Unicode filenames instead",
837 1);
838}
839
840static PyObject*
841win32_decode_filename(PyObject *obj)
842{
843 PyObject *unicode;
844 if (PyUnicode_Check(obj)) {
845 if (PyUnicode_READY(obj))
846 return NULL;
847 Py_INCREF(obj);
848 return obj;
849 }
850 if (!PyUnicode_FSDecoder(obj, &unicode))
851 return NULL;
852 if (win32_warn_bytes_api()) {
853 Py_DECREF(unicode);
854 return NULL;
855 }
856 return unicode;
857}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000858#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000859
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000861#ifdef WITH_NEXT_FRAMEWORK
862/* On Darwin/MacOSX a shared library or framework has no access to
863** environ directly, we must obtain it with _NSGetEnviron().
864*/
865#include <crt_externs.h>
866static char **environ;
867#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000869#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000870
Barry Warsaw53699e91996-12-10 23:23:01 +0000871static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000872convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873{
Victor Stinner8c62be82010-05-06 00:08:46 +0000874 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000875#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000876 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000877#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000878 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000879#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000880#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000881 APIRET rc;
882 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
883#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000884
Victor Stinner8c62be82010-05-06 00:08:46 +0000885 d = PyDict_New();
886 if (d == NULL)
887 return NULL;
888#ifdef WITH_NEXT_FRAMEWORK
889 if (environ == NULL)
890 environ = *_NSGetEnviron();
891#endif
892#ifdef MS_WINDOWS
893 /* _wenviron must be initialized in this way if the program is started
894 through main() instead of wmain(). */
895 _wgetenv(L"");
896 if (_wenviron == NULL)
897 return d;
898 /* This part ignores errors */
899 for (e = _wenviron; *e != NULL; e++) {
900 PyObject *k;
901 PyObject *v;
902 wchar_t *p = wcschr(*e, L'=');
903 if (p == NULL)
904 continue;
905 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
906 if (k == NULL) {
907 PyErr_Clear();
908 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000909 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000910 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
911 if (v == NULL) {
912 PyErr_Clear();
913 Py_DECREF(k);
914 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000915 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000916 if (PyDict_GetItem(d, k) == NULL) {
917 if (PyDict_SetItem(d, k, v) != 0)
918 PyErr_Clear();
919 }
920 Py_DECREF(k);
921 Py_DECREF(v);
922 }
923#else
924 if (environ == NULL)
925 return d;
926 /* This part ignores errors */
927 for (e = environ; *e != NULL; e++) {
928 PyObject *k;
929 PyObject *v;
930 char *p = strchr(*e, '=');
931 if (p == NULL)
932 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000933 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000934 if (k == NULL) {
935 PyErr_Clear();
936 continue;
937 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000938 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000939 if (v == NULL) {
940 PyErr_Clear();
941 Py_DECREF(k);
942 continue;
943 }
944 if (PyDict_GetItem(d, k) == NULL) {
945 if (PyDict_SetItem(d, k, v) != 0)
946 PyErr_Clear();
947 }
948 Py_DECREF(k);
949 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000950 }
951#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000952#if defined(PYOS_OS2)
953 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
954 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
955 PyObject *v = PyBytes_FromString(buffer);
956 PyDict_SetItemString(d, "BEGINLIBPATH", v);
957 Py_DECREF(v);
958 }
959 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
960 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
961 PyObject *v = PyBytes_FromString(buffer);
962 PyDict_SetItemString(d, "ENDLIBPATH", v);
963 Py_DECREF(v);
964 }
965#endif
966 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967}
968
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000969/* Set a POSIX-specific error from errno, and return NULL */
970
Barry Warsawd58d7641998-07-23 16:14:40 +0000971static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000972posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000973{
Victor Stinner8c62be82010-05-06 00:08:46 +0000974 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000975}
Barry Warsawd58d7641998-07-23 16:14:40 +0000976static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000977posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000978{
Victor Stinner8c62be82010-05-06 00:08:46 +0000979 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000980}
981
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000982
Mark Hammondef8b6542001-05-13 08:04:26 +0000983static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000984posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000985{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000986 PyObject *name_str, *rc;
987 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
988 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000989 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000990 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
991 name_str);
992 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000993 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000994}
995
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000996#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000997static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000998win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000999{
Victor Stinner8c62be82010-05-06 00:08:46 +00001000 /* XXX We should pass the function name along in the future.
1001 (winreg.c also wants to pass the function name.)
1002 This would however require an additional param to the
1003 Windows error object, which is non-trivial.
1004 */
1005 errno = GetLastError();
1006 if (filename)
1007 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1008 else
1009 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001010}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001011
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001012static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +02001013win32_error_unicode(char* function, wchar_t* filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001014{
Victor Stinner8c62be82010-05-06 00:08:46 +00001015 /* XXX - see win32_error for comments on 'function' */
1016 errno = GetLastError();
1017 if (filename)
1018 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
1019 else
1020 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001021}
1022
Victor Stinnereb5657a2011-09-30 01:44:27 +02001023static PyObject *
1024win32_error_object(char* function, PyObject* filename)
1025{
1026 /* XXX - see win32_error for comments on 'function' */
1027 errno = GetLastError();
1028 if (filename)
1029 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1030 PyExc_WindowsError,
1031 errno,
1032 filename);
1033 else
1034 return PyErr_SetFromWindowsErr(errno);
1035}
1036
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001037#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001038
Guido van Rossumd48f2521997-12-05 22:19:34 +00001039#if defined(PYOS_OS2)
1040/**********************************************************************
1041 * Helper Function to Trim and Format OS/2 Messages
1042 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +00001043static void
Guido van Rossumd48f2521997-12-05 22:19:34 +00001044os2_formatmsg(char *msgbuf, int msglen, char *reason)
1045{
1046 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
1047
1048 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
1049 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
1050
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001051 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +00001052 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
1053 }
1054
1055 /* Add Optional Reason Text */
1056 if (reason) {
1057 strcat(msgbuf, " : ");
1058 strcat(msgbuf, reason);
1059 }
1060}
1061
1062/**********************************************************************
1063 * Decode an OS/2 Operating System Error Code
1064 *
1065 * A convenience function to lookup an OS/2 error code and return a
1066 * text message we can use to raise a Python exception.
1067 *
1068 * Notes:
1069 * The messages for errors returned from the OS/2 kernel reside in
1070 * the file OSO001.MSG in the \OS2 directory hierarchy.
1071 *
1072 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +00001073static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +00001074os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
1075{
1076 APIRET rc;
1077 ULONG msglen;
1078
1079 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
1080 Py_BEGIN_ALLOW_THREADS
1081 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
1082 errorcode, "oso001.msg", &msglen);
1083 Py_END_ALLOW_THREADS
1084
1085 if (rc == NO_ERROR)
1086 os2_formatmsg(msgbuf, msglen, reason);
1087 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +00001088 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +00001089 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001090
1091 return msgbuf;
1092}
1093
1094/* Set an OS/2-specific error and return NULL. OS/2 kernel
1095 errors are not in a global variable e.g. 'errno' nor are
1096 they congruent with posix error numbers. */
1097
Victor Stinner8c62be82010-05-06 00:08:46 +00001098static PyObject *
1099os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001100{
1101 char text[1024];
1102 PyObject *v;
1103
1104 os2_strerror(text, sizeof(text), code, "");
1105
1106 v = Py_BuildValue("(is)", code, text);
1107 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +00001108 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001109 Py_DECREF(v);
1110 }
1111 return NULL; /* Signal to Python that an Exception is Pending */
1112}
1113
1114#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001115
1116/* POSIX generic methods */
1117
Barry Warsaw53699e91996-12-10 23:23:01 +00001118static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001119posix_fildes(PyObject *fdobj, int (*func)(int))
1120{
Victor Stinner8c62be82010-05-06 00:08:46 +00001121 int fd;
1122 int res;
1123 fd = PyObject_AsFileDescriptor(fdobj);
1124 if (fd < 0)
1125 return NULL;
1126 if (!_PyVerify_fd(fd))
1127 return posix_error();
1128 Py_BEGIN_ALLOW_THREADS
1129 res = (*func)(fd);
1130 Py_END_ALLOW_THREADS
1131 if (res < 0)
1132 return posix_error();
1133 Py_INCREF(Py_None);
1134 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001135}
Guido van Rossum21142a01999-01-08 21:05:37 +00001136
1137static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001139{
Victor Stinner8c62be82010-05-06 00:08:46 +00001140 PyObject *opath1 = NULL;
1141 char *path1;
1142 int res;
1143 if (!PyArg_ParseTuple(args, format,
1144 PyUnicode_FSConverter, &opath1))
1145 return NULL;
1146 path1 = PyBytes_AsString(opath1);
1147 Py_BEGIN_ALLOW_THREADS
1148 res = (*func)(path1);
1149 Py_END_ALLOW_THREADS
1150 if (res < 0)
1151 return posix_error_with_allocated_filename(opath1);
1152 Py_DECREF(opath1);
1153 Py_INCREF(Py_None);
1154 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001155}
1156
Barry Warsaw53699e91996-12-10 23:23:01 +00001157static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001158posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001159 char *format,
1160 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001161{
Victor Stinner8c62be82010-05-06 00:08:46 +00001162 PyObject *opath1 = NULL, *opath2 = NULL;
1163 char *path1, *path2;
1164 int res;
1165 if (!PyArg_ParseTuple(args, format,
1166 PyUnicode_FSConverter, &opath1,
1167 PyUnicode_FSConverter, &opath2)) {
1168 return NULL;
1169 }
1170 path1 = PyBytes_AsString(opath1);
1171 path2 = PyBytes_AsString(opath2);
1172 Py_BEGIN_ALLOW_THREADS
1173 res = (*func)(path1, path2);
1174 Py_END_ALLOW_THREADS
1175 Py_DECREF(opath1);
1176 Py_DECREF(opath2);
1177 if (res != 0)
1178 /* XXX how to report both path1 and path2??? */
1179 return posix_error();
1180 Py_INCREF(Py_None);
1181 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182}
1183
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001184#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001185static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +00001186win32_1str(PyObject* args, char* func,
1187 char* format, BOOL (__stdcall *funcA)(LPCSTR),
1188 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +00001189{
Victor Stinner8c62be82010-05-06 00:08:46 +00001190 PyObject *uni;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001191 const char *ansi;
Victor Stinner8c62be82010-05-06 00:08:46 +00001192 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001193
Victor Stinnereb5657a2011-09-30 01:44:27 +02001194 if (PyArg_ParseTuple(args, wformat, &uni))
1195 {
1196 wchar_t *wstr = PyUnicode_AsUnicode(uni);
1197 if (wstr == NULL)
1198 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001199 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02001200 result = funcW(wstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00001201 Py_END_ALLOW_THREADS
1202 if (!result)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001203 return win32_error_object(func, uni);
Victor Stinner8c62be82010-05-06 00:08:46 +00001204 Py_INCREF(Py_None);
1205 return Py_None;
1206 }
Victor Stinnereb5657a2011-09-30 01:44:27 +02001207 PyErr_Clear();
1208
Victor Stinner8c62be82010-05-06 00:08:46 +00001209 if (!PyArg_ParseTuple(args, format, &ansi))
1210 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001211 if (win32_warn_bytes_api())
1212 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001213 Py_BEGIN_ALLOW_THREADS
1214 result = funcA(ansi);
1215 Py_END_ALLOW_THREADS
1216 if (!result)
1217 return win32_error(func, ansi);
1218 Py_INCREF(Py_None);
1219 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001220
1221}
1222
1223/* This is a reimplementation of the C library's chdir function,
1224 but one that produces Win32 errors instead of DOS error codes.
1225 chdir is essentially a wrapper around SetCurrentDirectory; however,
1226 it also needs to set "magic" environment variables indicating
1227 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001228static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001229win32_chdir(LPCSTR path)
1230{
Victor Stinner8c62be82010-05-06 00:08:46 +00001231 char new_path[MAX_PATH+1];
1232 int result;
1233 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001234
Victor Stinner8c62be82010-05-06 00:08:46 +00001235 if(!SetCurrentDirectoryA(path))
1236 return FALSE;
1237 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
1238 if (!result)
1239 return FALSE;
1240 /* In the ANSI API, there should not be any paths longer
1241 than MAX_PATH. */
1242 assert(result <= MAX_PATH+1);
1243 if (strncmp(new_path, "\\\\", 2) == 0 ||
1244 strncmp(new_path, "//", 2) == 0)
1245 /* UNC path, nothing to do. */
1246 return TRUE;
1247 env[1] = new_path[0];
1248 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001249}
1250
1251/* The Unicode version differs from the ANSI version
1252 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001253static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001254win32_wchdir(LPCWSTR path)
1255{
Victor Stinner8c62be82010-05-06 00:08:46 +00001256 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
1257 int result;
1258 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001259
Victor Stinner8c62be82010-05-06 00:08:46 +00001260 if(!SetCurrentDirectoryW(path))
1261 return FALSE;
1262 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
1263 if (!result)
1264 return FALSE;
1265 if (result > MAX_PATH+1) {
1266 new_path = malloc(result * sizeof(wchar_t));
1267 if (!new_path) {
1268 SetLastError(ERROR_OUTOFMEMORY);
1269 return FALSE;
1270 }
1271 result = GetCurrentDirectoryW(result, new_path);
1272 if (!result) {
1273 free(new_path);
1274 return FALSE;
1275 }
1276 }
1277 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1278 wcsncmp(new_path, L"//", 2) == 0)
1279 /* UNC path, nothing to do. */
1280 return TRUE;
1281 env[1] = new_path[0];
1282 result = SetEnvironmentVariableW(env, new_path);
1283 if (new_path != _new_path)
1284 free(new_path);
1285 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001286}
1287#endif
1288
Martin v. Löwis14694662006-02-03 12:54:16 +00001289#ifdef MS_WINDOWS
1290/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1291 - time stamps are restricted to second resolution
1292 - file modification times suffer from forth-and-back conversions between
1293 UTC and local time
1294 Therefore, we implement our own stat, based on the Win32 API directly.
1295*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001296#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001297
1298struct win32_stat{
1299 int st_dev;
1300 __int64 st_ino;
1301 unsigned short st_mode;
1302 int st_nlink;
1303 int st_uid;
1304 int st_gid;
1305 int st_rdev;
1306 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001307 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001308 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001309 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001310 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001311 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001312 int st_ctime_nsec;
1313};
1314
1315static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1316
1317static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001318FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001319{
Victor Stinner8c62be82010-05-06 00:08:46 +00001320 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1321 /* Cannot simply cast and dereference in_ptr,
1322 since it might not be aligned properly */
1323 __int64 in;
1324 memcpy(&in, in_ptr, sizeof(in));
1325 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001326 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001327}
1328
Thomas Wouters477c8d52006-05-27 19:21:47 +00001329static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001330time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001331{
Victor Stinner8c62be82010-05-06 00:08:46 +00001332 /* XXX endianness */
1333 __int64 out;
1334 out = time_in + secs_between_epochs;
1335 out = out * 10000000 + nsec_in / 100;
1336 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001337}
1338
Martin v. Löwis14694662006-02-03 12:54:16 +00001339/* Below, we *know* that ugo+r is 0444 */
1340#if _S_IREAD != 0400
1341#error Unsupported C library
1342#endif
1343static int
1344attributes_to_mode(DWORD attr)
1345{
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 int m = 0;
1347 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1348 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1349 else
1350 m |= _S_IFREG;
1351 if (attr & FILE_ATTRIBUTE_READONLY)
1352 m |= 0444;
1353 else
1354 m |= 0666;
1355 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001356}
1357
1358static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001359attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001360{
Victor Stinner8c62be82010-05-06 00:08:46 +00001361 memset(result, 0, sizeof(*result));
1362 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1363 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1364 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1365 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1366 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001367 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001368 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001369 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1370 /* first clear the S_IFMT bits */
1371 result->st_mode ^= (result->st_mode & 0170000);
1372 /* now set the bits that make this a symlink */
1373 result->st_mode |= 0120000;
1374 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001375
Victor Stinner8c62be82010-05-06 00:08:46 +00001376 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001377}
1378
Guido van Rossumd8faa362007-04-27 19:54:29 +00001379static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001380attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001381{
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 HANDLE hFindFile;
1383 WIN32_FIND_DATAA FileData;
1384 hFindFile = FindFirstFileA(pszFile, &FileData);
1385 if (hFindFile == INVALID_HANDLE_VALUE)
1386 return FALSE;
1387 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001388 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001389 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001390 info->dwFileAttributes = FileData.dwFileAttributes;
1391 info->ftCreationTime = FileData.ftCreationTime;
1392 info->ftLastAccessTime = FileData.ftLastAccessTime;
1393 info->ftLastWriteTime = FileData.ftLastWriteTime;
1394 info->nFileSizeHigh = FileData.nFileSizeHigh;
1395 info->nFileSizeLow = FileData.nFileSizeLow;
1396/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001397 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1398 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001400}
1401
1402static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001403attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001404{
Victor Stinner8c62be82010-05-06 00:08:46 +00001405 HANDLE hFindFile;
1406 WIN32_FIND_DATAW FileData;
1407 hFindFile = FindFirstFileW(pszFile, &FileData);
1408 if (hFindFile == INVALID_HANDLE_VALUE)
1409 return FALSE;
1410 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001411 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001412 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001413 info->dwFileAttributes = FileData.dwFileAttributes;
1414 info->ftCreationTime = FileData.ftCreationTime;
1415 info->ftLastAccessTime = FileData.ftLastAccessTime;
1416 info->ftLastWriteTime = FileData.ftLastWriteTime;
1417 info->nFileSizeHigh = FileData.nFileSizeHigh;
1418 info->nFileSizeLow = FileData.nFileSizeLow;
1419/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001420 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1421 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001423}
1424
Brian Curtind25aef52011-06-13 15:16:04 -05001425/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1426static int has_GetFinalPathNameByHandle = 0;
Brian Curtind25aef52011-06-13 15:16:04 -05001427static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1428 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001429static int
Brian Curtind25aef52011-06-13 15:16:04 -05001430check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001431{
Brian Curtind25aef52011-06-13 15:16:04 -05001432 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001433 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1434 DWORD);
1435
Brian Curtind25aef52011-06-13 15:16:04 -05001436 /* only recheck */
1437 if (!has_GetFinalPathNameByHandle)
1438 {
Martin v. Löwis50590f12012-01-14 17:54:09 +01001439 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtind25aef52011-06-13 15:16:04 -05001440 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1441 "GetFinalPathNameByHandleA");
1442 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1443 "GetFinalPathNameByHandleW");
1444 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1445 Py_GetFinalPathNameByHandleW;
1446 }
1447 return has_GetFinalPathNameByHandle;
1448}
1449
1450static BOOL
1451get_target_path(HANDLE hdl, wchar_t **target_path)
1452{
1453 int buf_size, result_length;
1454 wchar_t *buf;
1455
1456 /* We have a good handle to the target, use it to determine
1457 the target path name (then we'll call lstat on it). */
1458 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1459 VOLUME_NAME_DOS);
1460 if(!buf_size)
1461 return FALSE;
1462
1463 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001464 if (!buf) {
1465 SetLastError(ERROR_OUTOFMEMORY);
1466 return FALSE;
1467 }
1468
Brian Curtind25aef52011-06-13 15:16:04 -05001469 result_length = Py_GetFinalPathNameByHandleW(hdl,
1470 buf, buf_size, VOLUME_NAME_DOS);
1471
1472 if(!result_length) {
1473 free(buf);
1474 return FALSE;
1475 }
1476
1477 if(!CloseHandle(hdl)) {
1478 free(buf);
1479 return FALSE;
1480 }
1481
1482 buf[result_length] = 0;
1483
1484 *target_path = buf;
1485 return TRUE;
1486}
1487
1488static int
1489win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1490 BOOL traverse);
1491static int
1492win32_xstat_impl(const char *path, struct win32_stat *result,
1493 BOOL traverse)
1494{
Victor Stinner26de69d2011-06-17 15:15:38 +02001495 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001496 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001497 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001498 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001499 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001500 const char *dot;
1501
Brian Curtind25aef52011-06-13 15:16:04 -05001502 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001503 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1504 traverse reparse point. */
1505 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001506 }
1507
Brian Curtinf5e76d02010-11-24 13:14:05 +00001508 hFile = CreateFileA(
1509 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001510 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001511 0, /* share mode */
1512 NULL, /* security attributes */
1513 OPEN_EXISTING,
1514 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001515 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1516 Because of this, calls like GetFinalPathNameByHandle will return
1517 the symlink path agin and not the actual final path. */
1518 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1519 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001520 NULL);
1521
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001522 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001523 /* Either the target doesn't exist, or we don't have access to
1524 get a handle to it. If the former, we need to return an error.
1525 If the latter, we can use attributes_from_dir. */
1526 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001527 return -1;
1528 /* Could not get attributes on open file. Fall back to
1529 reading the directory. */
1530 if (!attributes_from_dir(path, &info, &reparse_tag))
1531 /* Very strange. This should not fail now */
1532 return -1;
1533 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1534 if (traverse) {
1535 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001536 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001537 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001538 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001539 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001540 } else {
1541 if (!GetFileInformationByHandle(hFile, &info)) {
1542 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001543 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001544 }
1545 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001546 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1547 return -1;
1548
1549 /* Close the outer open file handle now that we're about to
1550 reopen it with different flags. */
1551 if (!CloseHandle(hFile))
1552 return -1;
1553
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001554 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001555 /* In order to call GetFinalPathNameByHandle we need to open
1556 the file without the reparse handling flag set. */
1557 hFile2 = CreateFileA(
1558 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1559 NULL, OPEN_EXISTING,
1560 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1561 NULL);
1562 if (hFile2 == INVALID_HANDLE_VALUE)
1563 return -1;
1564
1565 if (!get_target_path(hFile2, &target_path))
1566 return -1;
1567
1568 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001569 free(target_path);
1570 return code;
1571 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001572 } else
1573 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001574 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001575 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001576
1577 /* Set S_IEXEC if it is an .exe, .bat, ... */
1578 dot = strrchr(path, '.');
1579 if (dot) {
1580 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1581 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1582 result->st_mode |= 0111;
1583 }
1584 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001585}
1586
1587static int
Brian Curtind25aef52011-06-13 15:16:04 -05001588win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1589 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001590{
1591 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001592 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001593 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001594 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001595 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001596 const wchar_t *dot;
1597
Brian Curtind25aef52011-06-13 15:16:04 -05001598 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001599 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1600 traverse reparse point. */
1601 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001602 }
1603
Brian Curtinf5e76d02010-11-24 13:14:05 +00001604 hFile = CreateFileW(
1605 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001606 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001607 0, /* share mode */
1608 NULL, /* security attributes */
1609 OPEN_EXISTING,
1610 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001611 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1612 Because of this, calls like GetFinalPathNameByHandle will return
1613 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001614 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001615 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001616 NULL);
1617
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001618 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001619 /* Either the target doesn't exist, or we don't have access to
1620 get a handle to it. If the former, we need to return an error.
1621 If the latter, we can use attributes_from_dir. */
1622 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001623 return -1;
1624 /* Could not get attributes on open file. Fall back to
1625 reading the directory. */
1626 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1627 /* Very strange. This should not fail now */
1628 return -1;
1629 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1630 if (traverse) {
1631 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001632 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001633 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001634 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001635 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001636 } else {
1637 if (!GetFileInformationByHandle(hFile, &info)) {
1638 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001639 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001640 }
1641 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001642 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1643 return -1;
1644
1645 /* Close the outer open file handle now that we're about to
1646 reopen it with different flags. */
1647 if (!CloseHandle(hFile))
1648 return -1;
1649
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001650 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001651 /* In order to call GetFinalPathNameByHandle we need to open
1652 the file without the reparse handling flag set. */
1653 hFile2 = CreateFileW(
1654 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1655 NULL, OPEN_EXISTING,
1656 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1657 NULL);
1658 if (hFile2 == INVALID_HANDLE_VALUE)
1659 return -1;
1660
1661 if (!get_target_path(hFile2, &target_path))
1662 return -1;
1663
1664 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001665 free(target_path);
1666 return code;
1667 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001668 } else
1669 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001670 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001671 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001672
1673 /* Set S_IEXEC if it is an .exe, .bat, ... */
1674 dot = wcsrchr(path, '.');
1675 if (dot) {
1676 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1677 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1678 result->st_mode |= 0111;
1679 }
1680 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001681}
1682
1683static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001684win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001685{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001686 /* Protocol violation: we explicitly clear errno, instead of
1687 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001688 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001689 errno = 0;
1690 return code;
1691}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001692
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001693static int
1694win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1695{
1696 /* Protocol violation: we explicitly clear errno, instead of
1697 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001698 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001699 errno = 0;
1700 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001701}
Brian Curtind25aef52011-06-13 15:16:04 -05001702/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001703
1704 In Posix, stat automatically traverses symlinks and returns the stat
1705 structure for the target. In Windows, the equivalent GetFileAttributes by
1706 default does not traverse symlinks and instead returns attributes for
1707 the symlink.
1708
1709 Therefore, win32_lstat will get the attributes traditionally, and
1710 win32_stat will first explicitly resolve the symlink target and then will
1711 call win32_lstat on that result.
1712
Ezio Melotti4969f702011-03-15 05:59:46 +02001713 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001714
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001715static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001716win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001717{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001718 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001719}
1720
Victor Stinner8c62be82010-05-06 00:08:46 +00001721static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001722win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001723{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001724 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001725}
1726
1727static int
1728win32_stat(const char* path, struct win32_stat *result)
1729{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001730 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001731}
1732
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001733static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001734win32_stat_w(const wchar_t* path, struct win32_stat *result)
1735{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001736 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001737}
1738
1739static int
1740win32_fstat(int file_number, struct win32_stat *result)
1741{
Victor Stinner8c62be82010-05-06 00:08:46 +00001742 BY_HANDLE_FILE_INFORMATION info;
1743 HANDLE h;
1744 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001745
Victor Stinner8c62be82010-05-06 00:08:46 +00001746 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001747
Victor Stinner8c62be82010-05-06 00:08:46 +00001748 /* Protocol violation: we explicitly clear errno, instead of
1749 setting it to a POSIX error. Callers should use GetLastError. */
1750 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001751
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 if (h == INVALID_HANDLE_VALUE) {
1753 /* This is really a C library error (invalid file handle).
1754 We set the Win32 error to the closes one matching. */
1755 SetLastError(ERROR_INVALID_HANDLE);
1756 return -1;
1757 }
1758 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001759
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 type = GetFileType(h);
1761 if (type == FILE_TYPE_UNKNOWN) {
1762 DWORD error = GetLastError();
1763 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001764 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001765 }
1766 /* else: valid but unknown file */
1767 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001768
Victor Stinner8c62be82010-05-06 00:08:46 +00001769 if (type != FILE_TYPE_DISK) {
1770 if (type == FILE_TYPE_CHAR)
1771 result->st_mode = _S_IFCHR;
1772 else if (type == FILE_TYPE_PIPE)
1773 result->st_mode = _S_IFIFO;
1774 return 0;
1775 }
1776
1777 if (!GetFileInformationByHandle(h, &info)) {
1778 return -1;
1779 }
1780
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001781 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001782 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001783 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1784 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001785}
1786
1787#endif /* MS_WINDOWS */
1788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001790"stat_result: Result from stat or lstat.\n\n\
1791This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001792 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001793or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1794\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001795Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1796or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001797\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001798See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001799
1800static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 {"st_mode", "protection bits"},
1802 {"st_ino", "inode"},
1803 {"st_dev", "device"},
1804 {"st_nlink", "number of hard links"},
1805 {"st_uid", "user ID of owner"},
1806 {"st_gid", "group ID of owner"},
1807 {"st_size", "total size, in bytes"},
1808 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1809 {NULL, "integer time of last access"},
1810 {NULL, "integer time of last modification"},
1811 {NULL, "integer time of last change"},
1812 {"st_atime", "time of last access"},
1813 {"st_mtime", "time of last modification"},
1814 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001815 {"st_atime_ns", "time of last access in nanoseconds"},
1816 {"st_mtime_ns", "time of last modification in nanoseconds"},
1817 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001818#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001819 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001820#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001821#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001822 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001823#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001824#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001825 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001826#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001827#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001828 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001829#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001830#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001832#endif
1833#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001834 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001835#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001836 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001837};
1838
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001839#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001840#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001841#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001842#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001843#endif
1844
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001845#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001846#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1847#else
1848#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1849#endif
1850
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001851#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001852#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1853#else
1854#define ST_RDEV_IDX ST_BLOCKS_IDX
1855#endif
1856
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001857#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1858#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1859#else
1860#define ST_FLAGS_IDX ST_RDEV_IDX
1861#endif
1862
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001863#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001864#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001865#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001866#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001867#endif
1868
1869#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1870#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1871#else
1872#define ST_BIRTHTIME_IDX ST_GEN_IDX
1873#endif
1874
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001875static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001876 "stat_result", /* name */
1877 stat_result__doc__, /* doc */
1878 stat_result_fields,
1879 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001880};
1881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001882PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001883"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1884This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001885 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001886or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001887\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001889
1890static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001891 {"f_bsize", },
1892 {"f_frsize", },
1893 {"f_blocks", },
1894 {"f_bfree", },
1895 {"f_bavail", },
1896 {"f_files", },
1897 {"f_ffree", },
1898 {"f_favail", },
1899 {"f_flag", },
1900 {"f_namemax",},
1901 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001902};
1903
1904static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 "statvfs_result", /* name */
1906 statvfs_result__doc__, /* doc */
1907 statvfs_result_fields,
1908 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001909};
1910
Ross Lagerwall7807c352011-03-17 20:20:30 +02001911#if defined(HAVE_WAITID) && !defined(__APPLE__)
1912PyDoc_STRVAR(waitid_result__doc__,
1913"waitid_result: Result from waitid.\n\n\
1914This object may be accessed either as a tuple of\n\
1915 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1916or via the attributes si_pid, si_uid, and so on.\n\
1917\n\
1918See os.waitid for more information.");
1919
1920static PyStructSequence_Field waitid_result_fields[] = {
1921 {"si_pid", },
1922 {"si_uid", },
1923 {"si_signo", },
1924 {"si_status", },
1925 {"si_code", },
1926 {0}
1927};
1928
1929static PyStructSequence_Desc waitid_result_desc = {
1930 "waitid_result", /* name */
1931 waitid_result__doc__, /* doc */
1932 waitid_result_fields,
1933 5
1934};
1935static PyTypeObject WaitidResultType;
1936#endif
1937
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001938static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001939static PyTypeObject StatResultType;
1940static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001941#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001942static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001943#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001944static newfunc structseq_new;
1945
1946static PyObject *
1947statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1948{
Victor Stinner8c62be82010-05-06 00:08:46 +00001949 PyStructSequence *result;
1950 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001951
Victor Stinner8c62be82010-05-06 00:08:46 +00001952 result = (PyStructSequence*)structseq_new(type, args, kwds);
1953 if (!result)
1954 return NULL;
1955 /* If we have been initialized from a tuple,
1956 st_?time might be set to None. Initialize it
1957 from the int slots. */
1958 for (i = 7; i <= 9; i++) {
1959 if (result->ob_item[i+3] == Py_None) {
1960 Py_DECREF(Py_None);
1961 Py_INCREF(result->ob_item[i]);
1962 result->ob_item[i+3] = result->ob_item[i];
1963 }
1964 }
1965 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001966}
1967
1968
1969
1970/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001971static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001972
1973PyDoc_STRVAR(stat_float_times__doc__,
1974"stat_float_times([newval]) -> oldval\n\n\
1975Determine whether os.[lf]stat represents time stamps as float objects.\n\
1976If newval is True, future calls to stat() return floats, if it is False,\n\
1977future calls return ints. \n\
1978If newval is omitted, return the current setting.\n");
1979
1980static PyObject*
1981stat_float_times(PyObject* self, PyObject *args)
1982{
Victor Stinner8c62be82010-05-06 00:08:46 +00001983 int newval = -1;
1984 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1985 return NULL;
1986 if (newval == -1)
1987 /* Return old value */
1988 return PyBool_FromLong(_stat_float_times);
1989 _stat_float_times = newval;
1990 Py_INCREF(Py_None);
1991 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001992}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001993
Larry Hastings6fe20b32012-04-19 15:07:49 -07001994static PyObject *billion = NULL;
1995
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001996static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001997fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001998{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001999 PyObject *s = _PyLong_FromTime_t(sec);
2000 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2001 PyObject *s_in_ns = NULL;
2002 PyObject *ns_total = NULL;
2003 PyObject *float_s = NULL;
2004
2005 if (!(s && ns_fractional))
2006 goto exit;
2007
2008 s_in_ns = PyNumber_Multiply(s, billion);
2009 if (!s_in_ns)
2010 goto exit;
2011
2012 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2013 if (!ns_total)
2014 goto exit;
2015
Victor Stinner4195b5c2012-02-08 23:03:19 +01002016 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07002017 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2018 if (!float_s)
2019 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00002020 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07002021 else {
2022 float_s = s;
2023 Py_INCREF(float_s);
2024 }
2025
2026 PyStructSequence_SET_ITEM(v, index, s);
2027 PyStructSequence_SET_ITEM(v, index+3, float_s);
2028 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2029 s = NULL;
2030 float_s = NULL;
2031 ns_total = NULL;
2032exit:
2033 Py_XDECREF(s);
2034 Py_XDECREF(ns_fractional);
2035 Py_XDECREF(s_in_ns);
2036 Py_XDECREF(ns_total);
2037 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002038}
2039
Tim Peters5aa91602002-01-30 05:46:57 +00002040/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002041 (used by posix_stat() and posix_fstat()) */
2042static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002043_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002044{
Victor Stinner8c62be82010-05-06 00:08:46 +00002045 unsigned long ansec, mnsec, cnsec;
2046 PyObject *v = PyStructSequence_New(&StatResultType);
2047 if (v == NULL)
2048 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002049
Victor Stinner8c62be82010-05-06 00:08:46 +00002050 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00002051#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002052 PyStructSequence_SET_ITEM(v, 1,
2053 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002054#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002055 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002056#endif
2057#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002058 PyStructSequence_SET_ITEM(v, 2,
2059 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002060#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002061 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002062#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002063 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
2064 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
2065 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00002066#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002067 PyStructSequence_SET_ITEM(v, 6,
2068 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002069#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002070 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002071#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002072
Martin v. Löwis14694662006-02-03 12:54:16 +00002073#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002074 ansec = st->st_atim.tv_nsec;
2075 mnsec = st->st_mtim.tv_nsec;
2076 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002077#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002078 ansec = st->st_atimespec.tv_nsec;
2079 mnsec = st->st_mtimespec.tv_nsec;
2080 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002081#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002082 ansec = st->st_atime_nsec;
2083 mnsec = st->st_mtime_nsec;
2084 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002085#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002086 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002087#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002088 fill_time(v, 7, st->st_atime, ansec);
2089 fill_time(v, 8, st->st_mtime, mnsec);
2090 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002091
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002092#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002093 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2094 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002095#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002096#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2098 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002099#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002100#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002101 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2102 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002103#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002104#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002105 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2106 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002107#endif
2108#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002109 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002110 PyObject *val;
2111 unsigned long bsec,bnsec;
2112 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002113#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002114 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002115#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002116 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002117#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002118 if (_stat_float_times) {
2119 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2120 } else {
2121 val = PyLong_FromLong((long)bsec);
2122 }
2123 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2124 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002125 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002126#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002127#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002128 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2129 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002130#endif
Fred Drake699f3522000-06-29 21:12:41 +00002131
Victor Stinner8c62be82010-05-06 00:08:46 +00002132 if (PyErr_Occurred()) {
2133 Py_DECREF(v);
2134 return NULL;
2135 }
Fred Drake699f3522000-06-29 21:12:41 +00002136
Victor Stinner8c62be82010-05-06 00:08:46 +00002137 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002138}
2139
Barry Warsaw53699e91996-12-10 23:23:01 +00002140static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01002141posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00002142 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002143#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00002144 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002145#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002146 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002147#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002148 char *wformat,
Victor Stinnereb5657a2011-09-30 01:44:27 +02002149 int (*wstatfunc)(const wchar_t *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002150{
Victor Stinner8c62be82010-05-06 00:08:46 +00002151 STRUCT_STAT st;
2152 PyObject *opath;
2153 char *path;
2154 int res;
2155 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002156
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002157#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002158 PyObject *po;
Victor Stinner4195b5c2012-02-08 23:03:19 +01002159 if (PyArg_ParseTuple(args, wformat, &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002160 wchar_t *wpath = PyUnicode_AsUnicode(po);
2161 if (wpath == NULL)
2162 return NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00002163
Victor Stinner8c62be82010-05-06 00:08:46 +00002164 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002165 res = wstatfunc(wpath, &st);
2166 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00002167
Victor Stinner8c62be82010-05-06 00:08:46 +00002168 if (res != 0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02002169 return win32_error_object("stat", po);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002170 return _pystat_fromstructstat(&st);
Victor Stinner8c62be82010-05-06 00:08:46 +00002171 }
2172 /* Drop the argument parsing error as narrow strings
2173 are also valid. */
2174 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002175#endif
2176
Victor Stinner4195b5c2012-02-08 23:03:19 +01002177 if (!PyArg_ParseTuple(args, format,
2178 PyUnicode_FSConverter, &opath))
Victor Stinner8c62be82010-05-06 00:08:46 +00002179 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002180#ifdef MS_WINDOWS
2181 if (win32_warn_bytes_api()) {
2182 Py_DECREF(opath);
2183 return NULL;
2184 }
2185#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002186 path = PyBytes_AsString(opath);
2187 Py_BEGIN_ALLOW_THREADS
2188 res = (*statfunc)(path, &st);
2189 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00002190
Victor Stinner8c62be82010-05-06 00:08:46 +00002191 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00002192#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002193 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00002194#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002195 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00002196#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002197 }
2198 else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002199 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00002200
Victor Stinner8c62be82010-05-06 00:08:46 +00002201 Py_DECREF(opath);
2202 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002203}
2204
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002205/* POSIX methods */
2206
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002207#ifdef AT_FDCWD
2208#define DEFAULT_DIR_FD AT_FDCWD
2209#else
2210#define DEFAULT_DIR_FD (-100)
2211#endif
2212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00002214"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00002215Use the real uid/gid to test for access to a path. Note that most\n\
2216operations will use the effective uid/gid, therefore this routine can\n\
2217be used in a suid/sgid environment to test if the invoking user has the\n\
2218specified access to the path. The mode argument can be F_OK to test\n\
2219existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00002220
2221static PyObject *
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002222posix_access(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002223{
Victor Stinner8c62be82010-05-06 00:08:46 +00002224 int mode;
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002225 int dir_fd = DEFAULT_DIR_FD;
2226 wchar_t *wide_path;
2227 char *path;
2228 int follow_symlinks = 1;
2229 int effective_ids = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00002230
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002231#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002232 DWORD attr;
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002233#else
2234 int res;
2235#endif
2236
2237 APT_DECLARE(apt, "access");
2238 APT_ARGUMENT_PATH(apt, "path", &wide_path, &path, NULL);
2239 APT_ARGUMENT_INT(apt, "mode", &mode);
2240 APT_OPTIONAL(apt);
2241 APT_KEYWORD_ONLY(apt);
2242 APT_ARGUMENT_INT(apt, "dir_fd", &dir_fd);
2243 APT_ARGUMENT_BOOL(apt, "follow_symlinks", &follow_symlinks);
2244 APT_ARGUMENT_BOOL(apt, "effective_ids", &effective_ids);
2245
2246#define ACCESS_FAIL_IF_KEYWORD_USED \
2247 if (dir_fd != DEFAULT_DIR_FD) \
2248 PyErr_SetString(PyExc_NotImplementedError, "access: dir_fd unavailable on this platform"); \
2249 if (!follow_symlinks) \
2250 PyErr_SetString(PyExc_NotImplementedError, "access: follow_symlinks unavailable on this platform"); \
2251 if (effective_ids) \
2252 PyErr_SetString(PyExc_NotImplementedError, "access: effective_ids unavailable on this platform")
2253
2254 if (!apt_parse(&apt))
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 return NULL;
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002256 Py_RETURN_NONE;
2257
2258#ifdef MS_WINDOWS
2259 ACCESS_FAIL_IF_KEYWORD_USED;
2260
Victor Stinner8c62be82010-05-06 00:08:46 +00002261 Py_BEGIN_ALLOW_THREADS
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002262 if (wide != NULL)
2263 attr = GetFileAttributesW(wide_path);
2264 else
2265 attr = GetFileAttributesA(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002266 Py_END_ALLOW_THREADS
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002267
2268 apt_cleanup(&apt);
2269
Victor Stinner8c62be82010-05-06 00:08:46 +00002270 if (attr == 0xFFFFFFFF)
2271 /* File does not exist, or cannot read attributes */
2272 return PyBool_FromLong(0);
2273 /* Access is possible if either write access wasn't requested, or
2274 the file isn't read-only, or if it's a directory, as there are
2275 no read-only directories on Windows. */
2276 return PyBool_FromLong(!(mode & 2)
2277 || !(attr & FILE_ATTRIBUTE_READONLY)
2278 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00002279#else
Larry Hastingscfe6f2a2012-05-05 17:39:09 -07002280
2281 if ((dir_fd != DEFAULT_DIR_FD)
2282 || effective_ids || !follow_symlinks) {
2283#ifdef HAVE_FACCESSAT
2284 int flags = 0;
2285 if (!follow_symlinks)
2286 flags |= AT_SYMLINK_NOFOLLOW;
2287 if (effective_ids)
2288 flags |= AT_EACCESS;
2289 Py_BEGIN_ALLOW_THREADS
2290 res = faccessat(dir_fd, path, mode, flags);
2291 Py_END_ALLOW_THREADS
2292#else
2293 ACCESS_FAIL_IF_KEYWORD_USED;
2294#endif
2295 } else {
2296 Py_BEGIN_ALLOW_THREADS
2297 res = access(path, mode);
2298 Py_END_ALLOW_THREADS
2299 }
2300
2301 apt_cleanup(&apt);
Victor Stinner8c62be82010-05-06 00:08:46 +00002302 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002303#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002304}
2305
Guido van Rossumd371ff11999-01-25 16:12:23 +00002306#ifndef F_OK
2307#define F_OK 0
2308#endif
2309#ifndef R_OK
2310#define R_OK 4
2311#endif
2312#ifndef W_OK
2313#define W_OK 2
2314#endif
2315#ifndef X_OK
2316#define X_OK 1
2317#endif
2318
2319#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002320PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002321"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002322Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00002323
2324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002325posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002326{
Victor Stinner8c62be82010-05-06 00:08:46 +00002327 int id;
2328 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002329
Victor Stinner8c62be82010-05-06 00:08:46 +00002330 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
2331 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002332
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002333#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002334 /* file descriptor 0 only, the default input device (stdin) */
2335 if (id == 0) {
2336 ret = ttyname();
2337 }
2338 else {
2339 ret = NULL;
2340 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002341#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002342 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002343#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002344 if (ret == NULL)
2345 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002346 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002347}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002348#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002349
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002350#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002351PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002352"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002353Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002354
2355static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002356posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002357{
Victor Stinner8c62be82010-05-06 00:08:46 +00002358 char *ret;
2359 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002360
Greg Wardb48bc172000-03-01 21:51:56 +00002361#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002362 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002363#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002364 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002365#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 if (ret == NULL)
2367 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002368 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002369}
2370#endif
2371
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002372PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002373"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002374Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002375
Barry Warsaw53699e91996-12-10 23:23:01 +00002376static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002377posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002378{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002379#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002380 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002381#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002382 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00002383#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002384 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002385#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002386 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002387#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002388}
2389
Fred Drake4d1e64b2002-04-15 19:40:07 +00002390#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002391PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002392"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00002393Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002394opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002395
2396static PyObject *
2397posix_fchdir(PyObject *self, PyObject *fdobj)
2398{
Victor Stinner8c62be82010-05-06 00:08:46 +00002399 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002400}
2401#endif /* HAVE_FCHDIR */
2402
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002403
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002404PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002405"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002406Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002407
Barry Warsaw53699e91996-12-10 23:23:01 +00002408static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002409posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002410{
Victor Stinner8c62be82010-05-06 00:08:46 +00002411 PyObject *opath = NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002412 const char *path = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002413 int i;
2414 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002415#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002416 DWORD attr;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002417 PyObject *po;
Victor Stinner8c62be82010-05-06 00:08:46 +00002418 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002419 wchar_t *wpath = PyUnicode_AsUnicode(po);
2420 if (wpath == NULL)
2421 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002422 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02002423 attr = GetFileAttributesW(wpath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002424 if (attr != 0xFFFFFFFF) {
2425 if (i & _S_IWRITE)
2426 attr &= ~FILE_ATTRIBUTE_READONLY;
2427 else
2428 attr |= FILE_ATTRIBUTE_READONLY;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002429 res = SetFileAttributesW(wpath, attr);
Victor Stinner8c62be82010-05-06 00:08:46 +00002430 }
2431 else
2432 res = 0;
2433 Py_END_ALLOW_THREADS
2434 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02002435 return win32_error_object("chmod", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00002436 Py_INCREF(Py_None);
2437 return Py_None;
2438 }
2439 /* Drop the argument parsing error as narrow strings
2440 are also valid. */
2441 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002442
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002443 if (!PyArg_ParseTuple(args, "yi:chmod", &path, &i))
Victor Stinner8c62be82010-05-06 00:08:46 +00002444 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002445 if (win32_warn_bytes_api())
2446 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002447 Py_BEGIN_ALLOW_THREADS
2448 attr = GetFileAttributesA(path);
2449 if (attr != 0xFFFFFFFF) {
2450 if (i & _S_IWRITE)
2451 attr &= ~FILE_ATTRIBUTE_READONLY;
2452 else
2453 attr |= FILE_ATTRIBUTE_READONLY;
2454 res = SetFileAttributesA(path, attr);
2455 }
2456 else
2457 res = 0;
2458 Py_END_ALLOW_THREADS
2459 if (!res) {
2460 win32_error("chmod", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002461 return NULL;
2462 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002463 Py_INCREF(Py_None);
2464 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002465#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002466 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2467 &opath, &i))
2468 return NULL;
2469 path = PyBytes_AsString(opath);
2470 Py_BEGIN_ALLOW_THREADS
2471 res = chmod(path, i);
2472 Py_END_ALLOW_THREADS
2473 if (res < 0)
2474 return posix_error_with_allocated_filename(opath);
2475 Py_DECREF(opath);
2476 Py_INCREF(Py_None);
2477 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002478#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002479}
2480
Christian Heimes4e30a842007-11-30 22:12:06 +00002481#ifdef HAVE_FCHMOD
2482PyDoc_STRVAR(posix_fchmod__doc__,
2483"fchmod(fd, mode)\n\n\
2484Change the access permissions of the file given by file\n\
2485descriptor fd.");
2486
2487static PyObject *
2488posix_fchmod(PyObject *self, PyObject *args)
2489{
Victor Stinner8c62be82010-05-06 00:08:46 +00002490 int fd, mode, res;
2491 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2492 return NULL;
2493 Py_BEGIN_ALLOW_THREADS
2494 res = fchmod(fd, mode);
2495 Py_END_ALLOW_THREADS
2496 if (res < 0)
2497 return posix_error();
2498 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002499}
2500#endif /* HAVE_FCHMOD */
2501
2502#ifdef HAVE_LCHMOD
2503PyDoc_STRVAR(posix_lchmod__doc__,
2504"lchmod(path, mode)\n\n\
2505Change the access permissions of a file. If path is a symlink, this\n\
2506affects the link itself rather than the target.");
2507
2508static PyObject *
2509posix_lchmod(PyObject *self, PyObject *args)
2510{
Victor Stinner8c62be82010-05-06 00:08:46 +00002511 PyObject *opath;
2512 char *path;
2513 int i;
2514 int res;
2515 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2516 &opath, &i))
2517 return NULL;
2518 path = PyBytes_AsString(opath);
2519 Py_BEGIN_ALLOW_THREADS
2520 res = lchmod(path, i);
2521 Py_END_ALLOW_THREADS
2522 if (res < 0)
2523 return posix_error_with_allocated_filename(opath);
2524 Py_DECREF(opath);
2525 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002526}
2527#endif /* HAVE_LCHMOD */
2528
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002529
Thomas Wouterscf297e42007-02-23 15:07:44 +00002530#ifdef HAVE_CHFLAGS
2531PyDoc_STRVAR(posix_chflags__doc__,
2532"chflags(path, flags)\n\n\
2533Set file flags.");
2534
2535static PyObject *
2536posix_chflags(PyObject *self, PyObject *args)
2537{
Victor Stinner8c62be82010-05-06 00:08:46 +00002538 PyObject *opath;
2539 char *path;
2540 unsigned long flags;
2541 int res;
2542 if (!PyArg_ParseTuple(args, "O&k:chflags",
2543 PyUnicode_FSConverter, &opath, &flags))
2544 return NULL;
2545 path = PyBytes_AsString(opath);
2546 Py_BEGIN_ALLOW_THREADS
2547 res = chflags(path, flags);
2548 Py_END_ALLOW_THREADS
2549 if (res < 0)
2550 return posix_error_with_allocated_filename(opath);
2551 Py_DECREF(opath);
2552 Py_INCREF(Py_None);
2553 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002554}
2555#endif /* HAVE_CHFLAGS */
2556
2557#ifdef HAVE_LCHFLAGS
2558PyDoc_STRVAR(posix_lchflags__doc__,
2559"lchflags(path, flags)\n\n\
2560Set file flags.\n\
2561This function will not follow symbolic links.");
2562
2563static PyObject *
2564posix_lchflags(PyObject *self, PyObject *args)
2565{
Victor Stinner8c62be82010-05-06 00:08:46 +00002566 PyObject *opath;
2567 char *path;
2568 unsigned long flags;
2569 int res;
2570 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2571 PyUnicode_FSConverter, &opath, &flags))
2572 return NULL;
2573 path = PyBytes_AsString(opath);
2574 Py_BEGIN_ALLOW_THREADS
2575 res = lchflags(path, flags);
2576 Py_END_ALLOW_THREADS
2577 if (res < 0)
2578 return posix_error_with_allocated_filename(opath);
2579 Py_DECREF(opath);
2580 Py_INCREF(Py_None);
2581 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002582}
2583#endif /* HAVE_LCHFLAGS */
2584
Martin v. Löwis244edc82001-10-04 22:44:26 +00002585#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002586PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002587"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002588Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002589
2590static PyObject *
2591posix_chroot(PyObject *self, PyObject *args)
2592{
Victor Stinner8c62be82010-05-06 00:08:46 +00002593 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002594}
2595#endif
2596
Guido van Rossum21142a01999-01-08 21:05:37 +00002597#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002598PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002599"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002600force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002601
2602static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002603posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002604{
Stefan Krah0e803b32010-11-26 16:16:47 +00002605 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002606}
2607#endif /* HAVE_FSYNC */
2608
Ross Lagerwall7807c352011-03-17 20:20:30 +02002609#ifdef HAVE_SYNC
2610PyDoc_STRVAR(posix_sync__doc__,
2611"sync()\n\n\
2612Force write of everything to disk.");
2613
2614static PyObject *
2615posix_sync(PyObject *self, PyObject *noargs)
2616{
2617 Py_BEGIN_ALLOW_THREADS
2618 sync();
2619 Py_END_ALLOW_THREADS
2620 Py_RETURN_NONE;
2621}
2622#endif
2623
Guido van Rossum21142a01999-01-08 21:05:37 +00002624#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002625
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002626#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002627extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2628#endif
2629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002630PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002631"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002632force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002633 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002634
2635static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002636posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002637{
Stefan Krah0e803b32010-11-26 16:16:47 +00002638 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002639}
2640#endif /* HAVE_FDATASYNC */
2641
2642
Fredrik Lundh10723342000-07-10 16:38:09 +00002643#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002644PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002645"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002646Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002647
Barry Warsaw53699e91996-12-10 23:23:01 +00002648static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002649posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002650{
Victor Stinner8c62be82010-05-06 00:08:46 +00002651 PyObject *opath;
2652 char *path;
2653 long uid, gid;
2654 int res;
2655 if (!PyArg_ParseTuple(args, "O&ll:chown",
2656 PyUnicode_FSConverter, &opath,
2657 &uid, &gid))
2658 return NULL;
2659 path = PyBytes_AsString(opath);
2660 Py_BEGIN_ALLOW_THREADS
2661 res = chown(path, (uid_t) uid, (gid_t) gid);
2662 Py_END_ALLOW_THREADS
2663 if (res < 0)
2664 return posix_error_with_allocated_filename(opath);
2665 Py_DECREF(opath);
2666 Py_INCREF(Py_None);
2667 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002668}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002669#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002670
Christian Heimes4e30a842007-11-30 22:12:06 +00002671#ifdef HAVE_FCHOWN
2672PyDoc_STRVAR(posix_fchown__doc__,
2673"fchown(fd, uid, gid)\n\n\
2674Change the owner and group id of the file given by file descriptor\n\
2675fd to the numeric uid and gid.");
2676
2677static PyObject *
2678posix_fchown(PyObject *self, PyObject *args)
2679{
Victor Stinner8c62be82010-05-06 00:08:46 +00002680 int fd;
2681 long uid, gid;
2682 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002683 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002684 return NULL;
2685 Py_BEGIN_ALLOW_THREADS
2686 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2687 Py_END_ALLOW_THREADS
2688 if (res < 0)
2689 return posix_error();
2690 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002691}
2692#endif /* HAVE_FCHOWN */
2693
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002694#ifdef HAVE_LCHOWN
2695PyDoc_STRVAR(posix_lchown__doc__,
2696"lchown(path, uid, gid)\n\n\
2697Change the owner and group id of path to the numeric uid and gid.\n\
2698This function will not follow symbolic links.");
2699
2700static PyObject *
2701posix_lchown(PyObject *self, PyObject *args)
2702{
Victor Stinner8c62be82010-05-06 00:08:46 +00002703 PyObject *opath;
2704 char *path;
2705 long uid, gid;
2706 int res;
2707 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2708 PyUnicode_FSConverter, &opath,
2709 &uid, &gid))
2710 return NULL;
2711 path = PyBytes_AsString(opath);
2712 Py_BEGIN_ALLOW_THREADS
2713 res = lchown(path, (uid_t) uid, (gid_t) gid);
2714 Py_END_ALLOW_THREADS
2715 if (res < 0)
2716 return posix_error_with_allocated_filename(opath);
2717 Py_DECREF(opath);
2718 Py_INCREF(Py_None);
2719 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002720}
2721#endif /* HAVE_LCHOWN */
2722
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002723
Guido van Rossum36bc6801995-06-14 22:54:23 +00002724#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002725static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002726posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002727{
Victor Stinner8c62be82010-05-06 00:08:46 +00002728 char buf[1026];
2729 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002730
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002731#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002732 if (!use_bytes) {
2733 wchar_t wbuf[1026];
2734 wchar_t *wbuf2 = wbuf;
2735 PyObject *resobj;
2736 DWORD len;
2737 Py_BEGIN_ALLOW_THREADS
2738 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2739 /* If the buffer is large enough, len does not include the
2740 terminating \0. If the buffer is too small, len includes
2741 the space needed for the terminator. */
2742 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2743 wbuf2 = malloc(len * sizeof(wchar_t));
2744 if (wbuf2)
2745 len = GetCurrentDirectoryW(len, wbuf2);
2746 }
2747 Py_END_ALLOW_THREADS
2748 if (!wbuf2) {
2749 PyErr_NoMemory();
2750 return NULL;
2751 }
2752 if (!len) {
2753 if (wbuf2 != wbuf) free(wbuf2);
2754 return win32_error("getcwdu", NULL);
2755 }
2756 resobj = PyUnicode_FromWideChar(wbuf2, len);
2757 if (wbuf2 != wbuf) free(wbuf2);
2758 return resobj;
2759 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01002760
2761 if (win32_warn_bytes_api())
2762 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002763#endif
2764
Victor Stinner8c62be82010-05-06 00:08:46 +00002765 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002766#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002767 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002768#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002769 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002770#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002771 Py_END_ALLOW_THREADS
2772 if (res == NULL)
2773 return posix_error();
2774 if (use_bytes)
2775 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002776 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002777}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002778
2779PyDoc_STRVAR(posix_getcwd__doc__,
2780"getcwd() -> path\n\n\
2781Return a unicode string representing the current working directory.");
2782
2783static PyObject *
2784posix_getcwd_unicode(PyObject *self)
2785{
2786 return posix_getcwd(0);
2787}
2788
2789PyDoc_STRVAR(posix_getcwdb__doc__,
2790"getcwdb() -> path\n\n\
2791Return a bytes string representing the current working directory.");
2792
2793static PyObject *
2794posix_getcwd_bytes(PyObject *self)
2795{
2796 return posix_getcwd(1);
2797}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002798#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002799
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002800
Guido van Rossumb6775db1994-08-01 11:34:53 +00002801#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002802PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002803"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002804Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002805
Barry Warsaw53699e91996-12-10 23:23:01 +00002806static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002807posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002808{
Victor Stinner8c62be82010-05-06 00:08:46 +00002809 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002810}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002811#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002812
Brian Curtin1b9df392010-11-24 20:24:31 +00002813#ifdef MS_WINDOWS
2814PyDoc_STRVAR(win32_link__doc__,
2815"link(src, dst)\n\n\
2816Create a hard link to a file.");
2817
2818static PyObject *
2819win32_link(PyObject *self, PyObject *args)
2820{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002821 PyObject *src, *dst;
2822 BOOL ok;
Brian Curtin1b9df392010-11-24 20:24:31 +00002823
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002824 if (PyArg_ParseTuple(args, "UU:link", &src, &dst))
Victor Stinnereb5657a2011-09-30 01:44:27 +02002825 {
2826 wchar_t *wsrc, *wdst;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002827
2828 wsrc = PyUnicode_AsUnicode(src);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002829 if (wsrc == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002830 goto error;
2831 wdst = PyUnicode_AsUnicode(dst);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002832 if (wdst == NULL)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002833 goto error;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002834
Brian Curtinfc889c42010-11-28 23:59:46 +00002835 Py_BEGIN_ALLOW_THREADS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002836 ok = CreateHardLinkW(wdst, wsrc, NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002837 Py_END_ALLOW_THREADS
2838
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002839 if (!ok)
Brian Curtinfc889c42010-11-28 23:59:46 +00002840 return win32_error("link", NULL);
Brian Curtinfc889c42010-11-28 23:59:46 +00002841 Py_RETURN_NONE;
2842 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002843 else {
2844 PyErr_Clear();
2845 if (!PyArg_ParseTuple(args, "O&O&:link",
2846 PyUnicode_FSConverter, &src,
2847 PyUnicode_FSConverter, &dst))
2848 return NULL;
Brian Curtinfc889c42010-11-28 23:59:46 +00002849
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002850 if (win32_warn_bytes_api())
2851 goto error;
Brian Curtinfc889c42010-11-28 23:59:46 +00002852
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002853 Py_BEGIN_ALLOW_THREADS
2854 ok = CreateHardLinkA(PyBytes_AS_STRING(dst),
2855 PyBytes_AS_STRING(src),
2856 NULL);
2857 Py_END_ALLOW_THREADS
2858
2859 Py_XDECREF(src);
2860 Py_XDECREF(dst);
2861
2862 if (!ok)
2863 return win32_error("link", NULL);
2864 Py_RETURN_NONE;
2865
2866 error:
2867 Py_XDECREF(src);
2868 Py_XDECREF(dst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002869 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002870 }
Brian Curtin1b9df392010-11-24 20:24:31 +00002871}
2872#endif /* MS_WINDOWS */
2873
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002875PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002876"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002877Return a list containing the names of the entries in the directory.\n\
2878\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002879 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002880\n\
2881The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002882entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002883
Barry Warsaw53699e91996-12-10 23:23:01 +00002884static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002885posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002886{
Victor Stinner8c62be82010-05-06 00:08:46 +00002887 /* XXX Should redo this putting the (now four) versions of opendir
2888 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002889#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002890
Victor Stinner8c62be82010-05-06 00:08:46 +00002891 PyObject *d, *v;
2892 HANDLE hFindFile;
2893 BOOL result;
2894 WIN32_FIND_DATA FileData;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002895 const char *path;
2896 Py_ssize_t pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00002897 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2898 char *bufptr = namebuf;
2899 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002900
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002901 PyObject *po = NULL;
2902 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002903 WIN32_FIND_DATAW wFileData;
Victor Stinnereb5657a2011-09-30 01:44:27 +02002904 wchar_t *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002905
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002906 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002907 po_wchars = L".";
2908 len = 1;
2909 } else {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02002910 po_wchars = PyUnicode_AsUnicodeAndSize(po, &len);
Victor Stinnereb5657a2011-09-30 01:44:27 +02002911 if (po_wchars == NULL)
2912 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002913 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002914 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002915 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2916 if (!wnamebuf) {
2917 PyErr_NoMemory();
2918 return NULL;
2919 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002920 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002921 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02002922 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00002923 if (wch != L'/' && wch != L'\\' && wch != L':')
2924 wnamebuf[len++] = L'\\';
2925 wcscpy(wnamebuf + len, L"*.*");
2926 }
2927 if ((d = PyList_New(0)) == NULL) {
2928 free(wnamebuf);
2929 return NULL;
2930 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002931 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002932 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002933 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002934 if (hFindFile == INVALID_HANDLE_VALUE) {
2935 int error = GetLastError();
2936 if (error == ERROR_FILE_NOT_FOUND) {
2937 free(wnamebuf);
2938 return d;
2939 }
2940 Py_DECREF(d);
2941 win32_error_unicode("FindFirstFileW", wnamebuf);
2942 free(wnamebuf);
2943 return NULL;
2944 }
2945 do {
2946 /* Skip over . and .. */
2947 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2948 wcscmp(wFileData.cFileName, L"..") != 0) {
Victor Stinner9d3b93b2011-11-22 02:27:30 +01002949 v = PyUnicode_FromWideChar(wFileData.cFileName, wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00002950 if (v == NULL) {
2951 Py_DECREF(d);
2952 d = NULL;
2953 break;
2954 }
2955 if (PyList_Append(d, v) != 0) {
2956 Py_DECREF(v);
2957 Py_DECREF(d);
2958 d = NULL;
2959 break;
2960 }
2961 Py_DECREF(v);
2962 }
2963 Py_BEGIN_ALLOW_THREADS
2964 result = FindNextFileW(hFindFile, &wFileData);
2965 Py_END_ALLOW_THREADS
2966 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2967 it got to the end of the directory. */
2968 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2969 Py_DECREF(d);
2970 win32_error_unicode("FindNextFileW", wnamebuf);
2971 FindClose(hFindFile);
2972 free(wnamebuf);
2973 return NULL;
2974 }
2975 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002976
Victor Stinner8c62be82010-05-06 00:08:46 +00002977 if (FindClose(hFindFile) == FALSE) {
2978 Py_DECREF(d);
2979 win32_error_unicode("FindClose", wnamebuf);
2980 free(wnamebuf);
2981 return NULL;
2982 }
2983 free(wnamebuf);
2984 return d;
2985 }
2986 /* Drop the argument parsing error as narrow strings
2987 are also valid. */
2988 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002989
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002990 if (!PyArg_ParseTuple(args, "y#:listdir", &path, &pathlen))
Victor Stinner8c62be82010-05-06 00:08:46 +00002991 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002992 if (win32_warn_bytes_api())
2993 return NULL;
2994 if (pathlen+1 > MAX_PATH) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002995 PyErr_SetString(PyExc_ValueError, "path too long");
Victor Stinner8c62be82010-05-06 00:08:46 +00002996 return NULL;
2997 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01002998 strcpy(namebuf, path);
2999 len = pathlen;
Victor Stinner8c62be82010-05-06 00:08:46 +00003000 if (len > 0) {
3001 char ch = namebuf[len-1];
3002 if (ch != SEP && ch != ALTSEP && ch != ':')
3003 namebuf[len++] = '/';
3004 strcpy(namebuf + len, "*.*");
3005 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00003006
Victor Stinner8c62be82010-05-06 00:08:46 +00003007 if ((d = PyList_New(0)) == NULL)
3008 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003009
Antoine Pitroub73caab2010-08-09 23:39:31 +00003010 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003011 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003012 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003013 if (hFindFile == INVALID_HANDLE_VALUE) {
3014 int error = GetLastError();
3015 if (error == ERROR_FILE_NOT_FOUND)
3016 return d;
3017 Py_DECREF(d);
3018 return win32_error("FindFirstFile", namebuf);
3019 }
3020 do {
3021 /* Skip over . and .. */
3022 if (strcmp(FileData.cFileName, ".") != 0 &&
3023 strcmp(FileData.cFileName, "..") != 0) {
3024 v = PyBytes_FromString(FileData.cFileName);
3025 if (v == NULL) {
3026 Py_DECREF(d);
3027 d = NULL;
3028 break;
3029 }
3030 if (PyList_Append(d, v) != 0) {
3031 Py_DECREF(v);
3032 Py_DECREF(d);
3033 d = NULL;
3034 break;
3035 }
3036 Py_DECREF(v);
3037 }
3038 Py_BEGIN_ALLOW_THREADS
3039 result = FindNextFile(hFindFile, &FileData);
3040 Py_END_ALLOW_THREADS
3041 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3042 it got to the end of the directory. */
3043 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
3044 Py_DECREF(d);
3045 win32_error("FindNextFile", namebuf);
3046 FindClose(hFindFile);
3047 return NULL;
3048 }
3049 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003050
Victor Stinner8c62be82010-05-06 00:08:46 +00003051 if (FindClose(hFindFile) == FALSE) {
3052 Py_DECREF(d);
3053 return win32_error("FindClose", namebuf);
3054 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00003055
Victor Stinner8c62be82010-05-06 00:08:46 +00003056 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003057
Tim Peters0bb44a42000-09-15 07:44:49 +00003058#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003059
3060#ifndef MAX_PATH
3061#define MAX_PATH CCHMAXPATH
3062#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00003063 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003064 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00003065 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003066 PyObject *d, *v;
3067 char namebuf[MAX_PATH+5];
3068 HDIR hdir = 1;
3069 ULONG srchcnt = 1;
3070 FILEFINDBUF3 ep;
3071 APIRET rc;
3072
Victor Stinner8c62be82010-05-06 00:08:46 +00003073 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00003074 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003075 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00003076 name = PyBytes_AsString(oname);
3077 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003078 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003079 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00003080 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003081 return NULL;
3082 }
3083 strcpy(namebuf, name);
3084 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003085 if (*pt == ALTSEP)
3086 *pt = SEP;
3087 if (namebuf[len-1] != SEP)
3088 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003089 strcpy(namebuf + len, "*.*");
3090
Neal Norwitz6c913782007-10-14 03:23:09 +00003091 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00003092 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003093 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00003094 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003095
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003096 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
3097 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003098 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003099 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
3100 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
3101 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003102
3103 if (rc != NO_ERROR) {
3104 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003105 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003106 }
3107
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003108 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003109 do {
3110 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003111 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003112 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003113
3114 strcpy(namebuf, ep.achName);
3115
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003116 /* Leave Case of Name Alone -- In Native Form */
3117 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003118
Christian Heimes72b710a2008-05-26 13:28:38 +00003119 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003120 if (v == NULL) {
3121 Py_DECREF(d);
3122 d = NULL;
3123 break;
3124 }
3125 if (PyList_Append(d, v) != 0) {
3126 Py_DECREF(v);
3127 Py_DECREF(d);
3128 d = NULL;
3129 break;
3130 }
3131 Py_DECREF(v);
3132 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
3133 }
3134
Victor Stinnerdcb24032010-04-22 12:08:36 +00003135 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003136 return d;
3137#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003138 PyObject *oname;
3139 char *name;
3140 PyObject *d, *v;
3141 DIR *dirp;
3142 struct dirent *ep;
3143 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00003144
Victor Stinner8c62be82010-05-06 00:08:46 +00003145 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003146 /* v is never read, so it does not need to be initialized yet. */
3147 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 arg_is_unicode = 0;
3149 PyErr_Clear();
3150 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003151 oname = NULL;
3152 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00003154 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00003155 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003156 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003157 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003158 Py_BEGIN_ALLOW_THREADS
3159 dirp = opendir(name);
3160 Py_END_ALLOW_THREADS
3161 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003162 return posix_error_with_allocated_filename(oname);
3163 }
3164 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003165 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003166 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003167 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003168 Py_DECREF(oname);
3169 return NULL;
3170 }
3171 for (;;) {
3172 errno = 0;
3173 Py_BEGIN_ALLOW_THREADS
3174 ep = readdir(dirp);
3175 Py_END_ALLOW_THREADS
3176 if (ep == NULL) {
3177 if (errno == 0) {
3178 break;
3179 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003180 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003181 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003182 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003183 Py_DECREF(d);
3184 return posix_error_with_allocated_filename(oname);
3185 }
3186 }
3187 if (ep->d_name[0] == '.' &&
3188 (NAMLEN(ep) == 1 ||
3189 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3190 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00003191 if (arg_is_unicode)
3192 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3193 else
3194 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003195 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00003196 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00003197 break;
3198 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003199 if (PyList_Append(d, v) != 0) {
3200 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00003201 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 break;
3203 }
3204 Py_DECREF(v);
3205 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003206 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003207 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003208 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003210
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003212
Tim Peters0bb44a42000-09-15 07:44:49 +00003213#endif /* which OS */
3214} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003215
Antoine Pitrou8250e232011-02-25 23:41:16 +00003216#ifdef HAVE_FDOPENDIR
Charles-François Natali77940902012-02-06 19:54:48 +01003217PyDoc_STRVAR(posix_flistdir__doc__,
3218"flistdir(fd) -> list_of_strings\n\n\
Charles-François Natali76961fa2012-01-10 20:25:09 +01003219Like listdir(), but uses a file descriptor instead.");
Antoine Pitrou8250e232011-02-25 23:41:16 +00003220
3221static PyObject *
Charles-François Natali77940902012-02-06 19:54:48 +01003222posix_flistdir(PyObject *self, PyObject *args)
Antoine Pitrou8250e232011-02-25 23:41:16 +00003223{
3224 PyObject *d, *v;
3225 DIR *dirp;
3226 struct dirent *ep;
3227 int fd;
3228
3229 errno = 0;
Charles-François Natali77940902012-02-06 19:54:48 +01003230 if (!PyArg_ParseTuple(args, "i:flistdir", &fd))
Antoine Pitrou8250e232011-02-25 23:41:16 +00003231 return NULL;
Charles-François Natali76961fa2012-01-10 20:25:09 +01003232 /* closedir() closes the FD, so we duplicate it */
3233 fd = dup(fd);
3234 if (fd < 0)
3235 return posix_error();
Antoine Pitrou8250e232011-02-25 23:41:16 +00003236 Py_BEGIN_ALLOW_THREADS
3237 dirp = fdopendir(fd);
3238 Py_END_ALLOW_THREADS
3239 if (dirp == NULL) {
3240 close(fd);
3241 return posix_error();
3242 }
3243 if ((d = PyList_New(0)) == NULL) {
3244 Py_BEGIN_ALLOW_THREADS
3245 closedir(dirp);
3246 Py_END_ALLOW_THREADS
3247 return NULL;
3248 }
3249 for (;;) {
3250 errno = 0;
3251 Py_BEGIN_ALLOW_THREADS
3252 ep = readdir(dirp);
3253 Py_END_ALLOW_THREADS
3254 if (ep == NULL) {
3255 if (errno == 0) {
3256 break;
3257 } else {
3258 Py_BEGIN_ALLOW_THREADS
Charles-François Natalif2840a82012-01-08 20:30:47 +01003259 rewinddir(dirp);
Antoine Pitrou8250e232011-02-25 23:41:16 +00003260 closedir(dirp);
3261 Py_END_ALLOW_THREADS
3262 Py_DECREF(d);
3263 return posix_error();
3264 }
3265 }
3266 if (ep->d_name[0] == '.' &&
3267 (NAMLEN(ep) == 1 ||
3268 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3269 continue;
3270 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3271 if (v == NULL) {
3272 Py_CLEAR(d);
3273 break;
3274 }
3275 if (PyList_Append(d, v) != 0) {
3276 Py_DECREF(v);
3277 Py_CLEAR(d);
3278 break;
3279 }
3280 Py_DECREF(v);
3281 }
3282 Py_BEGIN_ALLOW_THREADS
Charles-François Natalif2840a82012-01-08 20:30:47 +01003283 rewinddir(dirp);
Antoine Pitrou8250e232011-02-25 23:41:16 +00003284 closedir(dirp);
3285 Py_END_ALLOW_THREADS
3286
3287 return d;
3288}
3289#endif
3290
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003291#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003292/* A helper function for abspath on win32 */
3293static PyObject *
3294posix__getfullpathname(PyObject *self, PyObject *args)
3295{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003296 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003297 char outbuf[MAX_PATH*2];
3298 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003299 PyObject *po;
3300
3301 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
3302 {
3303 wchar_t *wpath;
3304 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
3305 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 DWORD result;
3307 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003308
3309 wpath = PyUnicode_AsUnicode(po);
3310 if (wpath == NULL)
3311 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003312 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02003313 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02003315 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003316 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003317 if (!woutbufp)
3318 return PyErr_NoMemory();
3319 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
3320 }
3321 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003322 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00003323 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02003324 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003325 if (woutbufp != woutbuf)
3326 free(woutbufp);
3327 return v;
3328 }
3329 /* Drop the argument parsing error as narrow strings
3330 are also valid. */
3331 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02003332
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003333 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
3334 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00003335 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003336 if (win32_warn_bytes_api())
3337 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02003338 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003339 outbuf, &temp)) {
3340 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003341 return NULL;
3342 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003343 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
3344 return PyUnicode_Decode(outbuf, strlen(outbuf),
3345 Py_FileSystemDefaultEncoding, NULL);
3346 }
3347 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00003348} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00003349
Brian Curtind25aef52011-06-13 15:16:04 -05003350
Brian Curtinf5e76d02010-11-24 13:14:05 +00003351
Brian Curtind40e6f72010-07-08 21:39:08 +00003352/* A helper function for samepath on windows */
3353static PyObject *
3354posix__getfinalpathname(PyObject *self, PyObject *args)
3355{
3356 HANDLE hFile;
3357 int buf_size;
3358 wchar_t *target_path;
3359 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003360 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003361 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003362
Victor Stinnereb5657a2011-09-30 01:44:27 +02003363 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003364 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003365 path = PyUnicode_AsUnicode(po);
3366 if (path == NULL)
3367 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003368
3369 if(!check_GetFinalPathNameByHandle()) {
3370 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3371 NotImplementedError. */
3372 return PyErr_Format(PyExc_NotImplementedError,
3373 "GetFinalPathNameByHandle not available on this platform");
3374 }
3375
3376 hFile = CreateFileW(
3377 path,
3378 0, /* desired access */
3379 0, /* share mode */
3380 NULL, /* security attributes */
3381 OPEN_EXISTING,
3382 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3383 FILE_FLAG_BACKUP_SEMANTICS,
3384 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003385
Victor Stinnereb5657a2011-09-30 01:44:27 +02003386 if(hFile == INVALID_HANDLE_VALUE)
3387 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003388
3389 /* We have a good handle to the target, use it to determine the
3390 target path name. */
3391 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3392
3393 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003394 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003395
3396 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3397 if(!target_path)
3398 return PyErr_NoMemory();
3399
3400 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3401 buf_size, VOLUME_NAME_DOS);
3402 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003403 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003404
3405 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003406 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003407
3408 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003409 result = PyUnicode_FromWideChar(target_path, result_length);
Brian Curtind40e6f72010-07-08 21:39:08 +00003410 free(target_path);
3411 return result;
3412
3413} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003414
3415static PyObject *
3416posix__getfileinformation(PyObject *self, PyObject *args)
3417{
3418 HANDLE hFile;
3419 BY_HANDLE_FILE_INFORMATION info;
3420 int fd;
3421
3422 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3423 return NULL;
3424
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003425 if (!_PyVerify_fd(fd))
3426 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003427
3428 hFile = (HANDLE)_get_osfhandle(fd);
3429 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003430 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003431
3432 if (!GetFileInformationByHandle(hFile, &info))
3433 return win32_error("_getfileinformation", NULL);
3434
3435 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3436 info.nFileIndexHigh,
3437 info.nFileIndexLow);
3438}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003439
Brian Curtin95d028f2011-06-09 09:10:38 -05003440PyDoc_STRVAR(posix__isdir__doc__,
3441"Return true if the pathname refers to an existing directory.");
3442
Brian Curtin9c669cc2011-06-08 18:17:18 -05003443static PyObject *
3444posix__isdir(PyObject *self, PyObject *args)
3445{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003446 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003447 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003448 DWORD attributes;
3449
3450 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003451 wchar_t *wpath = PyUnicode_AsUnicode(po);
3452 if (wpath == NULL)
3453 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003454
3455 attributes = GetFileAttributesW(wpath);
3456 if (attributes == INVALID_FILE_ATTRIBUTES)
3457 Py_RETURN_FALSE;
3458 goto check;
3459 }
3460 /* Drop the argument parsing error as narrow strings
3461 are also valid. */
3462 PyErr_Clear();
3463
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003464 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003465 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003466 if (win32_warn_bytes_api())
3467 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003468 attributes = GetFileAttributesA(path);
3469 if (attributes == INVALID_FILE_ATTRIBUTES)
3470 Py_RETURN_FALSE;
3471
3472check:
3473 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3474 Py_RETURN_TRUE;
3475 else
3476 Py_RETURN_FALSE;
3477}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003478#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003479
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003480PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003481"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003482Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003483
Barry Warsaw53699e91996-12-10 23:23:01 +00003484static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003485posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003486{
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 int res;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003488 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003489 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003490
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003491#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003492 PyObject *po;
3493 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode))
3494 {
3495 wchar_t *wpath = PyUnicode_AsUnicode(po);
3496 if (wpath == NULL)
3497 return NULL;
3498
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02003500 res = CreateDirectoryW(wpath, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003501 Py_END_ALLOW_THREADS
3502 if (!res)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003503 return win32_error_object("mkdir", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 Py_INCREF(Py_None);
3505 return Py_None;
3506 }
3507 /* Drop the argument parsing error as narrow strings
3508 are also valid. */
3509 PyErr_Clear();
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003510 if (!PyArg_ParseTuple(args, "y|i:mkdir", &path, &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00003511 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003512 if (win32_warn_bytes_api())
3513 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003515 res = CreateDirectoryA(path, NULL);
3516 Py_END_ALLOW_THREADS
3517 if (!res) {
3518 win32_error("mkdir", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003519 return NULL;
3520 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003521 Py_INCREF(Py_None);
3522 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003523#else
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003524 PyObject *opath;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003525
Victor Stinner8c62be82010-05-06 00:08:46 +00003526 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3527 PyUnicode_FSConverter, &opath, &mode))
3528 return NULL;
3529 path = PyBytes_AsString(opath);
3530 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003531#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003532 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003533#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003534 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003535#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 Py_END_ALLOW_THREADS
3537 if (res < 0)
3538 return posix_error_with_allocated_filename(opath);
3539 Py_DECREF(opath);
3540 Py_INCREF(Py_None);
3541 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003542#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003543}
3544
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003545
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003546/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3547#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003548#include <sys/resource.h>
3549#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003550
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003551
3552#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003553PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003554"nice(inc) -> new_priority\n\n\
3555Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003556
Barry Warsaw53699e91996-12-10 23:23:01 +00003557static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003558posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003559{
Victor Stinner8c62be82010-05-06 00:08:46 +00003560 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003561
Victor Stinner8c62be82010-05-06 00:08:46 +00003562 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3563 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003564
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 /* There are two flavours of 'nice': one that returns the new
3566 priority (as required by almost all standards out there) and the
3567 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3568 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003569
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 If we are of the nice family that returns the new priority, we
3571 need to clear errno before the call, and check if errno is filled
3572 before calling posix_error() on a returnvalue of -1, because the
3573 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003574
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 errno = 0;
3576 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003577#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003578 if (value == 0)
3579 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003580#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003581 if (value == -1 && errno != 0)
3582 /* either nice() or getpriority() returned an error */
3583 return posix_error();
3584 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003585}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003586#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003587
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003588
3589#ifdef HAVE_GETPRIORITY
3590PyDoc_STRVAR(posix_getpriority__doc__,
3591"getpriority(which, who) -> current_priority\n\n\
3592Get program scheduling priority.");
3593
3594static PyObject *
3595posix_getpriority(PyObject *self, PyObject *args)
3596{
3597 int which, who, retval;
3598
3599 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3600 return NULL;
3601 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003602 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003603 if (errno != 0)
3604 return posix_error();
3605 return PyLong_FromLong((long)retval);
3606}
3607#endif /* HAVE_GETPRIORITY */
3608
3609
3610#ifdef HAVE_SETPRIORITY
3611PyDoc_STRVAR(posix_setpriority__doc__,
3612"setpriority(which, who, prio) -> None\n\n\
3613Set program scheduling priority.");
3614
3615static PyObject *
3616posix_setpriority(PyObject *self, PyObject *args)
3617{
3618 int which, who, prio, retval;
3619
3620 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3621 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003622 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003623 if (retval == -1)
3624 return posix_error();
3625 Py_RETURN_NONE;
3626}
3627#endif /* HAVE_SETPRIORITY */
3628
3629
Barry Warsaw53699e91996-12-10 23:23:01 +00003630static PyObject *
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003631internal_rename(PyObject *self, PyObject *args, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003632{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003633#ifdef MS_WINDOWS
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003634 PyObject *src, *dst;
Victor Stinner8c62be82010-05-06 00:08:46 +00003635 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003636 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
3637 if (PyArg_ParseTuple(args,
3638 is_replace ? "UU:replace" : "UU:rename",
3639 &src, &dst))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003640 {
3641 wchar_t *wsrc, *wdst;
3642
3643 wsrc = PyUnicode_AsUnicode(src);
3644 if (wsrc == NULL)
3645 return NULL;
3646 wdst = PyUnicode_AsUnicode(dst);
3647 if (wdst == NULL)
3648 return NULL;
3649 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003650 result = MoveFileExW(wsrc, wdst, flags);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003651 Py_END_ALLOW_THREADS
3652 if (!result)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003653 return win32_error(is_replace ? "replace" : "rename", NULL);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003654 Py_INCREF(Py_None);
3655 return Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003657 else {
3658 PyErr_Clear();
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003659 if (!PyArg_ParseTuple(args,
3660 is_replace ? "O&O&:replace" : "O&O&:rename",
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003661 PyUnicode_FSConverter, &src,
3662 PyUnicode_FSConverter, &dst))
3663 return NULL;
3664
3665 if (win32_warn_bytes_api())
3666 goto error;
3667
3668 Py_BEGIN_ALLOW_THREADS
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003669 result = MoveFileExA(PyBytes_AS_STRING(src),
3670 PyBytes_AS_STRING(dst), flags);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003671 Py_END_ALLOW_THREADS
3672
3673 Py_XDECREF(src);
3674 Py_XDECREF(dst);
3675
3676 if (!result)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003677 return win32_error(is_replace ? "replace" : "rename", NULL);
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003678 Py_INCREF(Py_None);
3679 return Py_None;
3680
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003681error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003682 Py_XDECREF(src);
3683 Py_XDECREF(dst);
Victor Stinner8c62be82010-05-06 00:08:46 +00003684 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003685 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003686#else
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003687 return posix_2str(args,
3688 is_replace ? "O&O&:replace" : "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003689#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003690}
3691
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003692PyDoc_STRVAR(posix_rename__doc__,
3693"rename(old, new)\n\n\
3694Rename a file or directory.");
3695
3696static PyObject *
3697posix_rename(PyObject *self, PyObject *args)
3698{
3699 return internal_rename(self, args, 0);
3700}
3701
3702PyDoc_STRVAR(posix_replace__doc__,
3703"replace(old, new)\n\n\
3704Rename a file or directory, overwriting the destination.");
3705
3706static PyObject *
3707posix_replace(PyObject *self, PyObject *args)
3708{
3709 return internal_rename(self, args, 1);
3710}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003712PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003713"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003714Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003715
Barry Warsaw53699e91996-12-10 23:23:01 +00003716static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003717posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003718{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003719#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003720 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003721#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003722 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003723#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003724}
3725
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003727PyDoc_STRVAR(posix_stat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01003728"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003729Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003730
Barry Warsaw53699e91996-12-10 23:23:01 +00003731static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01003732posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003733{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003734#ifdef MS_WINDOWS
Victor Stinner4195b5c2012-02-08 23:03:19 +01003735 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003736#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01003737 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003738#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003739}
3740
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003741
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003742#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003743PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003744"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003745Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003746
Barry Warsaw53699e91996-12-10 23:23:01 +00003747static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003748posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003749{
Victor Stinner8c62be82010-05-06 00:08:46 +00003750 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003751#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003752 wchar_t *command;
3753 if (!PyArg_ParseTuple(args, "u:system", &command))
3754 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003755
Victor Stinner8c62be82010-05-06 00:08:46 +00003756 Py_BEGIN_ALLOW_THREADS
3757 sts = _wsystem(command);
3758 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003759#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003760 PyObject *command_obj;
3761 char *command;
3762 if (!PyArg_ParseTuple(args, "O&:system",
3763 PyUnicode_FSConverter, &command_obj))
3764 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003765
Victor Stinner8c62be82010-05-06 00:08:46 +00003766 command = PyBytes_AsString(command_obj);
3767 Py_BEGIN_ALLOW_THREADS
3768 sts = system(command);
3769 Py_END_ALLOW_THREADS
3770 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003771#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003772 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003773}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003774#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003775
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003777PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003778"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003779Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003780
Barry Warsaw53699e91996-12-10 23:23:01 +00003781static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003782posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003783{
Victor Stinner8c62be82010-05-06 00:08:46 +00003784 int i;
3785 if (!PyArg_ParseTuple(args, "i:umask", &i))
3786 return NULL;
3787 i = (int)umask(i);
3788 if (i < 0)
3789 return posix_error();
3790 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003791}
3792
Brian Curtind40e6f72010-07-08 21:39:08 +00003793#ifdef MS_WINDOWS
3794
3795/* override the default DeleteFileW behavior so that directory
3796symlinks can be removed with this function, the same as with
3797Unix symlinks */
3798BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3799{
3800 WIN32_FILE_ATTRIBUTE_DATA info;
3801 WIN32_FIND_DATAW find_data;
3802 HANDLE find_data_handle;
3803 int is_directory = 0;
3804 int is_link = 0;
3805
3806 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3807 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003808
Brian Curtind40e6f72010-07-08 21:39:08 +00003809 /* Get WIN32_FIND_DATA structure for the path to determine if
3810 it is a symlink */
3811 if(is_directory &&
3812 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3813 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3814
3815 if(find_data_handle != INVALID_HANDLE_VALUE) {
3816 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3817 FindClose(find_data_handle);
3818 }
3819 }
3820 }
3821
3822 if (is_directory && is_link)
3823 return RemoveDirectoryW(lpFileName);
3824
3825 return DeleteFileW(lpFileName);
3826}
3827#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003829PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003830"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003831Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003833PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003834"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003835Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003836
Barry Warsaw53699e91996-12-10 23:23:01 +00003837static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003838posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003839{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003840#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003841 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3842 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003843#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003844 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003845#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003846}
3847
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003848
Guido van Rossumb6775db1994-08-01 11:34:53 +00003849#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003850PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003851"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003852Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003853
Barry Warsaw53699e91996-12-10 23:23:01 +00003854static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003855posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003856{
Victor Stinner8c62be82010-05-06 00:08:46 +00003857 struct utsname u;
3858 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003859
Victor Stinner8c62be82010-05-06 00:08:46 +00003860 Py_BEGIN_ALLOW_THREADS
3861 res = uname(&u);
3862 Py_END_ALLOW_THREADS
3863 if (res < 0)
3864 return posix_error();
3865 return Py_BuildValue("(sssss)",
3866 u.sysname,
3867 u.nodename,
3868 u.release,
3869 u.version,
3870 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003871}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003872#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003873
Larry Hastings9e3e70b2011-09-08 19:29:07 -07003874
Larry Hastings76ad59b2012-05-03 00:30:07 -07003875static int
3876split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
3877{
3878 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04003879 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07003880 divmod = PyNumber_Divmod(py_long, billion);
3881 if (!divmod)
3882 goto exit;
3883 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
3884 if ((*s == -1) && PyErr_Occurred())
3885 goto exit;
3886 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04003887 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07003888 goto exit;
3889
3890 result = 1;
3891exit:
3892 Py_XDECREF(divmod);
3893 return result;
3894}
3895
3896
3897typedef int (*parameter_converter_t)(PyObject *, void *);
3898
3899typedef struct {
3900 /* input only */
3901 char path_format;
3902 parameter_converter_t converter;
3903 char *function_name;
3904 char *first_argument_name;
3905 PyObject *args;
3906 PyObject *kwargs;
3907
3908 /* input/output */
3909 PyObject **path;
3910
3911 /* output only */
3912 int now;
3913 time_t atime_s;
3914 long atime_ns;
3915 time_t mtime_s;
3916 long mtime_ns;
3917} utime_arguments;
3918
3919#define DECLARE_UA(ua, fname) \
3920 utime_arguments ua; \
3921 memset(&ua, 0, sizeof(ua)); \
3922 ua.function_name = fname; \
3923 ua.args = args; \
3924 ua.kwargs = kwargs; \
3925 ua.first_argument_name = "path"; \
3926
3927/* UA_TO_FILETIME doesn't declare atime and mtime for you */
3928#define UA_TO_FILETIME(ua, atime, mtime) \
3929 time_t_to_FILE_TIME(ua.atime_s, ua.atime_ns, &atime); \
3930 time_t_to_FILE_TIME(ua.mtime_s, ua.mtime_ns, &mtime)
3931
3932/* the rest of these macros declare the output variable for you */
3933#define UA_TO_TIMESPEC(ua, ts) \
3934 struct timespec ts[2]; \
3935 ts[0].tv_sec = ua.atime_s; \
3936 ts[0].tv_nsec = ua.atime_ns; \
3937 ts[1].tv_sec = ua.mtime_s; \
3938 ts[1].tv_nsec = ua.mtime_ns
3939
3940#define UA_TO_TIMEVAL(ua, tv) \
3941 struct timeval tv[2]; \
3942 tv[0].tv_sec = ua.atime_s; \
3943 tv[0].tv_usec = ua.atime_ns / 1000; \
3944 tv[1].tv_sec = ua.mtime_s; \
3945 tv[1].tv_usec = ua.mtime_ns / 1000
3946
3947#define UA_TO_UTIMBUF(ua, u) \
3948 struct utimbuf u; \
3949 utimbuf.actime = ua.atime_s; \
3950 utimbuf.modtime = ua.mtime_s
3951
3952#define UA_TO_TIME_T(ua, timet) \
3953 time_t timet[2]; \
3954 timet[0] = ua.atime_s; \
3955 timet[1] = ua.mtime_s
3956
3957
3958/*
3959 * utime_read_time_arguments() processes arguments for the utime
3960 * family of functions.
Larry Hastings76ad59b2012-05-03 00:30:07 -07003961 */
Larry Hastingsb3336402012-05-04 02:31:57 -07003962
3963typedef enum {
3964 utime_success = 0,
3965 utime_parse_failure = 1,
3966 utime_times_and_ns_collision = 2,
3967 utime_times_conversion_failure = 3,
3968 utime_ns_conversion_failure = 4,
3969} utime_status;
3970
3971static utime_status
Larry Hastings76ad59b2012-05-03 00:30:07 -07003972utime_read_time_arguments(utime_arguments *ua)
3973{
3974 PyObject *times = NULL;
3975 PyObject *ns = NULL;
3976 char format[24];
3977 char *kwlist[4];
3978 char **kw = kwlist;
Larry Hastingsb3336402012-05-04 02:31:57 -07003979 utime_status return_value;
3980 int parse_result;
Larry Hastings76ad59b2012-05-03 00:30:07 -07003981
3982 *kw++ = ua->first_argument_name;
3983 *kw++ = "times";
3984 *kw++ = "ns";
3985 *kw = NULL;
3986
3987 sprintf(format, "%c%s|O$O:%s",
3988 ua->path_format,
3989 ua->converter ? "&" : "",
3990 ua->function_name);
3991
3992 if (ua->converter)
Larry Hastingsb3336402012-05-04 02:31:57 -07003993 parse_result = PyArg_ParseTupleAndKeywords(ua->args, ua->kwargs,
Larry Hastings76ad59b2012-05-03 00:30:07 -07003994 format, kwlist, ua->converter, ua->path, &times, &ns);
3995 else
Larry Hastingsb3336402012-05-04 02:31:57 -07003996 parse_result = PyArg_ParseTupleAndKeywords(ua->args, ua->kwargs,
Larry Hastings76ad59b2012-05-03 00:30:07 -07003997 format, kwlist, ua->path, &times, &ns);
3998
Larry Hastingsb3336402012-05-04 02:31:57 -07003999 if (!parse_result)
4000 return utime_parse_failure;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004001
4002 if (times && ns) {
4003 PyErr_Format(PyExc_RuntimeError,
4004 "%s: you may specify either 'times'"
4005 " or 'ns' but not both",
4006 ua->function_name);
Larry Hastingsb3336402012-05-04 02:31:57 -07004007 return_value = utime_times_and_ns_collision;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004008 goto fail;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004009 }
4010
4011 if (times && (times != Py_None)) {
4012 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
4013 PyErr_Format(PyExc_TypeError,
Stefan Krah6b03f2c2012-05-05 22:37:05 +02004014 "%s: 'times' must be either"
Benjamin Peterson9bd9d742012-05-04 01:42:41 -04004015 " a tuple of two ints or None",
Larry Hastings76ad59b2012-05-03 00:30:07 -07004016 ua->function_name);
Larry Hastingsb3336402012-05-04 02:31:57 -07004017 return_value = utime_times_conversion_failure;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004018 goto fail;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004019 }
4020 ua->now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004021 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
4022 &ua->atime_s, &ua->atime_ns) == -1 ||
4023 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Larry Hastingsb3336402012-05-04 02:31:57 -07004024 &ua->mtime_s, &ua->mtime_ns) == -1) {
4025 return_value = utime_times_conversion_failure;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004026 goto fail;
Larry Hastingsb3336402012-05-04 02:31:57 -07004027 }
4028 return utime_success;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004029 }
4030
4031 if (ns) {
4032 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
4033 PyErr_Format(PyExc_TypeError,
Benjamin Peterson9bd9d742012-05-04 01:42:41 -04004034 "%s: 'ns' must be a tuple of two ints",
Larry Hastings76ad59b2012-05-03 00:30:07 -07004035 ua->function_name);
Larry Hastingsb3336402012-05-04 02:31:57 -07004036 return_value = utime_ns_conversion_failure;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004037 goto fail;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004038 }
4039 ua->now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004040 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
4041 &ua->atime_s, &ua->atime_ns) ||
4042 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastingsb3336402012-05-04 02:31:57 -07004043 &ua->mtime_s, &ua->mtime_ns)) {
4044 return_value = utime_ns_conversion_failure;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004045 goto fail;
Larry Hastingsb3336402012-05-04 02:31:57 -07004046 }
4047 return utime_success;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004048 }
4049
4050 /* either times=None, or neither times nor ns was specified. use "now". */
4051 ua->now = 1;
Larry Hastingsb3336402012-05-04 02:31:57 -07004052 return utime_success;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004053
4054 fail:
4055 if (ua->converter)
Richard Oudkerkf072b452012-05-04 12:01:31 +01004056 Py_DECREF(*ua->path);
Larry Hastingsb3336402012-05-04 02:31:57 -07004057 return return_value;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004058}
4059
4060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004061PyDoc_STRVAR(posix_utime__doc__,
Larry Hastings76ad59b2012-05-03 00:30:07 -07004062"utime(path[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\
4063Set the access and modified time of the file.\n\
4064If the second argument ('times') is specified,\n\
4065 the values should be expressed as float seconds since the epoch.\n\
4066If the keyword argument 'ns' is specified,\n\
4067 the values should be expressed as integer nanoseconds since the epoch.\n\
4068If neither the second nor the 'ns' argument is specified,\n\
4069 utime uses the current time.\n\
4070Specifying both 'times' and 'ns' is an error.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004071
Barry Warsaw53699e91996-12-10 23:23:01 +00004072static PyObject *
Larry Hastings76ad59b2012-05-03 00:30:07 -07004073posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004074{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004075#ifdef MS_WINDOWS
Larry Hastings76ad59b2012-05-03 00:30:07 -07004076 PyObject *upath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004077 HANDLE hFile;
Victor Stinner8c62be82010-05-06 00:08:46 +00004078 PyObject *result = NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004079 FILETIME atime, mtime;
Thomas Wouters477c8d52006-05-27 19:21:47 +00004080
Larry Hastings76ad59b2012-05-03 00:30:07 -07004081 DECLARE_UA(ua, "utime");
4082
4083 ua.path_format = 'U';
4084 ua.path = &upath;
4085
Larry Hastingsb3336402012-05-04 02:31:57 -07004086 switch (utime_read_time_arguments(&ua)) {
4087 default:
4088 return NULL;
4089 case utime_success: {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004090 wchar_t *wpath = PyUnicode_AsUnicode(upath);
Victor Stinnereb5657a2011-09-30 01:44:27 +02004091 if (wpath == NULL)
4092 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004093 Py_BEGIN_ALLOW_THREADS
4094 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
4095 NULL, OPEN_EXISTING,
4096 FILE_FLAG_BACKUP_SEMANTICS, NULL);
4097 Py_END_ALLOW_THREADS
4098 if (hFile == INVALID_HANDLE_VALUE)
Larry Hastings76ad59b2012-05-03 00:30:07 -07004099 return win32_error_object("utime", upath);
Larry Hastingsb3336402012-05-04 02:31:57 -07004100 break;
Victor Stinnereb5657a2011-09-30 01:44:27 +02004101 }
Larry Hastingsb3336402012-05-04 02:31:57 -07004102 case utime_parse_failure: {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004103 const char *apath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 /* Drop the argument parsing error as narrow strings
4105 are also valid. */
4106 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00004107
Larry Hastings76ad59b2012-05-03 00:30:07 -07004108 ua.path_format = 'y';
4109 ua.path = (PyObject **)&apath;
Larry Hastingsb3336402012-05-04 02:31:57 -07004110 if (utime_read_time_arguments(&ua) != utime_success)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01004111 return NULL;
4112 if (win32_warn_bytes_api())
Victor Stinner8c62be82010-05-06 00:08:46 +00004113 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02004114
Victor Stinner8c62be82010-05-06 00:08:46 +00004115 Py_BEGIN_ALLOW_THREADS
4116 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
4117 NULL, OPEN_EXISTING,
4118 FILE_FLAG_BACKUP_SEMANTICS, NULL);
4119 Py_END_ALLOW_THREADS
4120 if (hFile == INVALID_HANDLE_VALUE) {
4121 win32_error("utime", apath);
Victor Stinner8c62be82010-05-06 00:08:46 +00004122 return NULL;
4123 }
Larry Hastingsb3336402012-05-04 02:31:57 -07004124 break;
4125 }
4126
Victor Stinner8c62be82010-05-06 00:08:46 +00004127 }
4128
Larry Hastings76ad59b2012-05-03 00:30:07 -07004129 if (ua.now) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004130 SYSTEMTIME now;
4131 GetSystemTime(&now);
4132 if (!SystemTimeToFileTime(&now, &mtime) ||
4133 !SystemTimeToFileTime(&now, &atime)) {
4134 win32_error("utime", NULL);
4135 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00004136 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004137 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004138 else {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004139 UA_TO_FILETIME(ua, atime, mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004140 }
4141 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4142 /* Avoid putting the file name into the error here,
4143 as that may confuse the user into believing that
4144 something is wrong with the file, when it also
4145 could be the time stamp that gives a problem. */
4146 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00004147 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00004148 }
4149 Py_INCREF(Py_None);
4150 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00004151done:
Victor Stinner8c62be82010-05-06 00:08:46 +00004152 CloseHandle(hFile);
4153 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004154#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00004155 PyObject *opath;
4156 char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00004157 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004158
Larry Hastings76ad59b2012-05-03 00:30:07 -07004159 DECLARE_UA(ua, "utime");
4160
4161 ua.path_format = 'O';
4162 ua.path = &opath;
4163 ua.converter = PyUnicode_FSConverter;
4164
Larry Hastingsb3336402012-05-04 02:31:57 -07004165 if (utime_read_time_arguments(&ua) != utime_success)
Victor Stinner8c62be82010-05-06 00:08:46 +00004166 return NULL;
4167 path = PyBytes_AsString(opath);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004168 if (ua.now) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004169 Py_BEGIN_ALLOW_THREADS
4170 res = utime(path, NULL);
4171 Py_END_ALLOW_THREADS
4172 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004173 else {
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004174 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004175#ifdef HAVE_UTIMENSAT
Larry Hastings76ad59b2012-05-03 00:30:07 -07004176 UA_TO_TIMESPEC(ua, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004177 res = utimensat(AT_FDCWD, path, buf, 0);
4178#elif defined(HAVE_UTIMES)
Larry Hastings76ad59b2012-05-03 00:30:07 -07004179 UA_TO_TIMEVAL(ua, buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 res = utimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004181#elif defined(HAVE_UTIME_H)
4182 /* XXX should define struct utimbuf instead, above */
Larry Hastings76ad59b2012-05-03 00:30:07 -07004183 UA_TO_UTIMBUF(ua, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004184 res = utime(path, &buf);
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00004185#else
Larry Hastings76ad59b2012-05-03 00:30:07 -07004186 UA_TO_TIME_T(ua, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004187 res = utime(path, buf);
4188#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004189 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00004190 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004191
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 if (res < 0) {
4193 return posix_error_with_allocated_filename(opath);
4194 }
4195 Py_DECREF(opath);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004196 Py_RETURN_NONE;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004197#undef UTIME_EXTRACT
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004198#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004199}
4200
Ross Lagerwall7807c352011-03-17 20:20:30 +02004201#ifdef HAVE_FUTIMES
4202PyDoc_STRVAR(posix_futimes__doc__,
Larry Hastings76ad59b2012-05-03 00:30:07 -07004203"futimes(fd[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02004204Set the access and modified time of the file specified by the file\n\
Larry Hastings76ad59b2012-05-03 00:30:07 -07004205descriptor fd. See utime for the semantics of the times and ns parameters.");
Ross Lagerwall7807c352011-03-17 20:20:30 +02004206
4207static PyObject *
Larry Hastings76ad59b2012-05-03 00:30:07 -07004208posix_futimes(PyObject *self, PyObject *args, PyObject *kwargs)
Ross Lagerwall7807c352011-03-17 20:20:30 +02004209{
4210 int res, fd;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004211
Larry Hastings76ad59b2012-05-03 00:30:07 -07004212 DECLARE_UA(ua, "futimes");
4213
4214 ua.path_format = 'i';
4215 ua.path = (PyObject **)&fd;
4216 ua.first_argument_name = "fd";
4217
Larry Hastingsb3336402012-05-04 02:31:57 -07004218 if (utime_read_time_arguments(&ua) != utime_success)
Ross Lagerwall7807c352011-03-17 20:20:30 +02004219 return NULL;
4220
Larry Hastings76ad59b2012-05-03 00:30:07 -07004221 if (ua.now) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02004222 Py_BEGIN_ALLOW_THREADS
4223 res = futimes(fd, NULL);
4224 Py_END_ALLOW_THREADS
4225 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004226 else {
Ross Lagerwall7807c352011-03-17 20:20:30 +02004227 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004228 {
4229#ifdef HAVE_FUTIMENS
Larry Hastings76ad59b2012-05-03 00:30:07 -07004230 UA_TO_TIMESPEC(ua, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004231 res = futimens(fd, buf);
4232#else
Larry Hastings76ad59b2012-05-03 00:30:07 -07004233 UA_TO_TIMEVAL(ua, buf);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004234 res = futimes(fd, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004235#endif
4236 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004237 Py_END_ALLOW_THREADS
4238 }
4239 if (res < 0)
4240 return posix_error();
4241 Py_RETURN_NONE;
4242}
4243#endif
4244
4245#ifdef HAVE_LUTIMES
4246PyDoc_STRVAR(posix_lutimes__doc__,
Larry Hastings76ad59b2012-05-03 00:30:07 -07004247"lutimes(path[, times=(atime, mtime), *, ns=(atime_ns, mtime_ns)])\n\
Ross Lagerwall7807c352011-03-17 20:20:30 +02004248Like utime(), but if path is a symbolic link, it is not dereferenced.");
4249
4250static PyObject *
Larry Hastings76ad59b2012-05-03 00:30:07 -07004251posix_lutimes(PyObject *self, PyObject *args, PyObject *kwargs)
Ross Lagerwall7807c352011-03-17 20:20:30 +02004252{
Brian Curtinc1b65d12011-11-07 14:18:54 -06004253 PyObject *opath;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004254 const char *path;
4255 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004256
Larry Hastings76ad59b2012-05-03 00:30:07 -07004257 DECLARE_UA(ua, "lutimes");
4258
4259 ua.path_format = 'O';
4260 ua.path = &opath;
4261 ua.converter = PyUnicode_FSConverter;
4262
Larry Hastingsb3336402012-05-04 02:31:57 -07004263 if (utime_read_time_arguments(&ua) != utime_success)
Ross Lagerwall7807c352011-03-17 20:20:30 +02004264 return NULL;
4265 path = PyBytes_AsString(opath);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004266
4267 if (ua.now) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02004268 /* optional time values not given */
4269 Py_BEGIN_ALLOW_THREADS
4270 res = lutimes(path, NULL);
4271 Py_END_ALLOW_THREADS
4272 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004273 else {
Ross Lagerwall7807c352011-03-17 20:20:30 +02004274 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004275 {
4276#ifdef HAVE_UTIMENSAT
Larry Hastings76ad59b2012-05-03 00:30:07 -07004277 UA_TO_TIMESPEC(ua, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004278 res = utimensat(AT_FDCWD, path, buf, AT_SYMLINK_NOFOLLOW);
4279#else
Larry Hastings76ad59b2012-05-03 00:30:07 -07004280 UA_TO_TIMEVAL(ua, buf);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004281 res = lutimes(path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004282#endif
4283 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004284 Py_END_ALLOW_THREADS
4285 }
4286 Py_DECREF(opath);
4287 if (res < 0)
4288 return posix_error();
4289 Py_RETURN_NONE;
4290}
4291#endif
4292
Guido van Rossum3b066191991-06-04 19:40:25 +00004293/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004294
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004295PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004296"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004297Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004298
Barry Warsaw53699e91996-12-10 23:23:01 +00004299static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004300posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004301{
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 int sts;
4303 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
4304 return NULL;
4305 _exit(sts);
4306 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004307}
4308
Martin v. Löwis114619e2002-10-07 06:44:21 +00004309#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4310static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00004311free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004312{
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 Py_ssize_t i;
4314 for (i = 0; i < count; i++)
4315 PyMem_Free(array[i]);
4316 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004317}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004318
Antoine Pitrou69f71142009-05-24 21:25:49 +00004319static
Martin v. Löwis011e8422009-05-05 04:43:17 +00004320int fsconvert_strdup(PyObject *o, char**out)
4321{
Victor Stinner8c62be82010-05-06 00:08:46 +00004322 PyObject *bytes;
4323 Py_ssize_t size;
4324 if (!PyUnicode_FSConverter(o, &bytes))
4325 return 0;
4326 size = PyBytes_GET_SIZE(bytes);
4327 *out = PyMem_Malloc(size+1);
4328 if (!*out)
4329 return 0;
4330 memcpy(*out, PyBytes_AsString(bytes), size+1);
4331 Py_DECREF(bytes);
4332 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004333}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004334#endif
4335
Ross Lagerwall7807c352011-03-17 20:20:30 +02004336#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00004337static char**
4338parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4339{
Victor Stinner8c62be82010-05-06 00:08:46 +00004340 char **envlist;
4341 Py_ssize_t i, pos, envc;
4342 PyObject *keys=NULL, *vals=NULL;
4343 PyObject *key, *val, *key2, *val2;
4344 char *p, *k, *v;
4345 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004346
Victor Stinner8c62be82010-05-06 00:08:46 +00004347 i = PyMapping_Size(env);
4348 if (i < 0)
4349 return NULL;
4350 envlist = PyMem_NEW(char *, i + 1);
4351 if (envlist == NULL) {
4352 PyErr_NoMemory();
4353 return NULL;
4354 }
4355 envc = 0;
4356 keys = PyMapping_Keys(env);
4357 vals = PyMapping_Values(env);
4358 if (!keys || !vals)
4359 goto error;
4360 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4361 PyErr_Format(PyExc_TypeError,
4362 "env.keys() or env.values() is not a list");
4363 goto error;
4364 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004365
Victor Stinner8c62be82010-05-06 00:08:46 +00004366 for (pos = 0; pos < i; pos++) {
4367 key = PyList_GetItem(keys, pos);
4368 val = PyList_GetItem(vals, pos);
4369 if (!key || !val)
4370 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004371
Victor Stinner8c62be82010-05-06 00:08:46 +00004372 if (PyUnicode_FSConverter(key, &key2) == 0)
4373 goto error;
4374 if (PyUnicode_FSConverter(val, &val2) == 0) {
4375 Py_DECREF(key2);
4376 goto error;
4377 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004378
4379#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
4381 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00004382#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004383 k = PyBytes_AsString(key2);
4384 v = PyBytes_AsString(val2);
4385 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004386
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 p = PyMem_NEW(char, len);
4388 if (p == NULL) {
4389 PyErr_NoMemory();
4390 Py_DECREF(key2);
4391 Py_DECREF(val2);
4392 goto error;
4393 }
4394 PyOS_snprintf(p, len, "%s=%s", k, v);
4395 envlist[envc++] = p;
4396 Py_DECREF(key2);
4397 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004398#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00004399 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004400#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 }
4402 Py_DECREF(vals);
4403 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004404
Victor Stinner8c62be82010-05-06 00:08:46 +00004405 envlist[envc] = 0;
4406 *envc_ptr = envc;
4407 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004408
4409error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004410 Py_XDECREF(keys);
4411 Py_XDECREF(vals);
4412 while (--envc >= 0)
4413 PyMem_DEL(envlist[envc]);
4414 PyMem_DEL(envlist);
4415 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004416}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004417
Ross Lagerwall7807c352011-03-17 20:20:30 +02004418static char**
4419parse_arglist(PyObject* argv, Py_ssize_t *argc)
4420{
4421 int i;
4422 char **argvlist = PyMem_NEW(char *, *argc+1);
4423 if (argvlist == NULL) {
4424 PyErr_NoMemory();
4425 return NULL;
4426 }
4427 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004428 PyObject* item = PySequence_ITEM(argv, i);
4429 if (item == NULL)
4430 goto fail;
4431 if (!fsconvert_strdup(item, &argvlist[i])) {
4432 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004433 goto fail;
4434 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004435 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004436 }
4437 argvlist[*argc] = NULL;
4438 return argvlist;
4439fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004440 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004441 free_string_array(argvlist, *argc);
4442 return NULL;
4443}
4444#endif
4445
4446#ifdef HAVE_EXECV
4447PyDoc_STRVAR(posix_execv__doc__,
4448"execv(path, args)\n\n\
4449Execute an executable path with arguments, replacing current process.\n\
4450\n\
4451 path: path of executable file\n\
4452 args: tuple or list of strings");
4453
4454static PyObject *
4455posix_execv(PyObject *self, PyObject *args)
4456{
4457 PyObject *opath;
4458 char *path;
4459 PyObject *argv;
4460 char **argvlist;
4461 Py_ssize_t argc;
4462
4463 /* execv has two arguments: (path, argv), where
4464 argv is a list or tuple of strings. */
4465
4466 if (!PyArg_ParseTuple(args, "O&O:execv",
4467 PyUnicode_FSConverter,
4468 &opath, &argv))
4469 return NULL;
4470 path = PyBytes_AsString(opath);
4471 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4472 PyErr_SetString(PyExc_TypeError,
4473 "execv() arg 2 must be a tuple or list");
4474 Py_DECREF(opath);
4475 return NULL;
4476 }
4477 argc = PySequence_Size(argv);
4478 if (argc < 1) {
4479 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4480 Py_DECREF(opath);
4481 return NULL;
4482 }
4483
4484 argvlist = parse_arglist(argv, &argc);
4485 if (argvlist == NULL) {
4486 Py_DECREF(opath);
4487 return NULL;
4488 }
4489
4490 execv(path, argvlist);
4491
4492 /* If we get here it's definitely an error */
4493
4494 free_string_array(argvlist, argc);
4495 Py_DECREF(opath);
4496 return posix_error();
4497}
4498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004499PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004500"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004501Execute a path with arguments and environment, replacing current process.\n\
4502\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004503 path: path of executable file\n\
4504 args: tuple or list of arguments\n\
4505 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004506
Barry Warsaw53699e91996-12-10 23:23:01 +00004507static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004508posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004509{
Victor Stinner8c62be82010-05-06 00:08:46 +00004510 PyObject *opath;
4511 char *path;
4512 PyObject *argv, *env;
4513 char **argvlist;
4514 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004515 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004516
Victor Stinner8c62be82010-05-06 00:08:46 +00004517 /* execve has three arguments: (path, argv, env), where
4518 argv is a list or tuple of strings and env is a dictionary
4519 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004520
Victor Stinner8c62be82010-05-06 00:08:46 +00004521 if (!PyArg_ParseTuple(args, "O&OO:execve",
4522 PyUnicode_FSConverter,
4523 &opath, &argv, &env))
4524 return NULL;
4525 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004526 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004527 PyErr_SetString(PyExc_TypeError,
4528 "execve() arg 2 must be a tuple or list");
4529 goto fail_0;
4530 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004531 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004532 if (!PyMapping_Check(env)) {
4533 PyErr_SetString(PyExc_TypeError,
4534 "execve() arg 3 must be a mapping object");
4535 goto fail_0;
4536 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004537
Ross Lagerwall7807c352011-03-17 20:20:30 +02004538 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004539 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004540 goto fail_0;
4541 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004542
Victor Stinner8c62be82010-05-06 00:08:46 +00004543 envlist = parse_envlist(env, &envc);
4544 if (envlist == NULL)
4545 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004546
Victor Stinner8c62be82010-05-06 00:08:46 +00004547 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004548
Victor Stinner8c62be82010-05-06 00:08:46 +00004549 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004550
Victor Stinner8c62be82010-05-06 00:08:46 +00004551 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004552
Victor Stinner8c62be82010-05-06 00:08:46 +00004553 while (--envc >= 0)
4554 PyMem_DEL(envlist[envc]);
4555 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004556 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004557 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004558 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004559 Py_DECREF(opath);
4560 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004561}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004562#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004563
Ross Lagerwall7807c352011-03-17 20:20:30 +02004564#ifdef HAVE_FEXECVE
4565PyDoc_STRVAR(posix_fexecve__doc__,
4566"fexecve(fd, args, env)\n\n\
4567Execute the program specified by a file descriptor with arguments and\n\
4568environment, replacing the current process.\n\
4569\n\
4570 fd: file descriptor of executable\n\
4571 args: tuple or list of arguments\n\
4572 env: dictionary of strings mapping to strings");
4573
4574static PyObject *
4575posix_fexecve(PyObject *self, PyObject *args)
4576{
4577 int fd;
4578 PyObject *argv, *env;
4579 char **argvlist;
4580 char **envlist;
4581 Py_ssize_t argc, envc;
4582
4583 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4584 &fd, &argv, &env))
4585 return NULL;
4586 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4587 PyErr_SetString(PyExc_TypeError,
4588 "fexecve() arg 2 must be a tuple or list");
4589 return NULL;
4590 }
4591 argc = PySequence_Size(argv);
4592 if (!PyMapping_Check(env)) {
4593 PyErr_SetString(PyExc_TypeError,
4594 "fexecve() arg 3 must be a mapping object");
4595 return NULL;
4596 }
4597
4598 argvlist = parse_arglist(argv, &argc);
4599 if (argvlist == NULL)
4600 return NULL;
4601
4602 envlist = parse_envlist(env, &envc);
4603 if (envlist == NULL)
4604 goto fail;
4605
4606 fexecve(fd, argvlist, envlist);
4607
4608 /* If we get here it's definitely an error */
4609
4610 (void) posix_error();
4611
4612 while (--envc >= 0)
4613 PyMem_DEL(envlist[envc]);
4614 PyMem_DEL(envlist);
4615 fail:
4616 free_string_array(argvlist, argc);
4617 return NULL;
4618}
4619#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004620
Guido van Rossuma1065681999-01-25 23:20:23 +00004621#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004622PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004623"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004624Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004625\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004626 mode: mode of process creation\n\
4627 path: path of executable file\n\
4628 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004629
4630static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004631posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004632{
Victor Stinner8c62be82010-05-06 00:08:46 +00004633 PyObject *opath;
4634 char *path;
4635 PyObject *argv;
4636 char **argvlist;
4637 int mode, i;
4638 Py_ssize_t argc;
4639 Py_intptr_t spawnval;
4640 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004641
Victor Stinner8c62be82010-05-06 00:08:46 +00004642 /* spawnv has three arguments: (mode, path, argv), where
4643 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004644
Victor Stinner8c62be82010-05-06 00:08:46 +00004645 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4646 PyUnicode_FSConverter,
4647 &opath, &argv))
4648 return NULL;
4649 path = PyBytes_AsString(opath);
4650 if (PyList_Check(argv)) {
4651 argc = PyList_Size(argv);
4652 getitem = PyList_GetItem;
4653 }
4654 else if (PyTuple_Check(argv)) {
4655 argc = PyTuple_Size(argv);
4656 getitem = PyTuple_GetItem;
4657 }
4658 else {
4659 PyErr_SetString(PyExc_TypeError,
4660 "spawnv() arg 2 must be a tuple or list");
4661 Py_DECREF(opath);
4662 return NULL;
4663 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004664
Victor Stinner8c62be82010-05-06 00:08:46 +00004665 argvlist = PyMem_NEW(char *, argc+1);
4666 if (argvlist == NULL) {
4667 Py_DECREF(opath);
4668 return PyErr_NoMemory();
4669 }
4670 for (i = 0; i < argc; i++) {
4671 if (!fsconvert_strdup((*getitem)(argv, i),
4672 &argvlist[i])) {
4673 free_string_array(argvlist, i);
4674 PyErr_SetString(
4675 PyExc_TypeError,
4676 "spawnv() arg 2 must contain only strings");
4677 Py_DECREF(opath);
4678 return NULL;
4679 }
4680 }
4681 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004682
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004683#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004684 Py_BEGIN_ALLOW_THREADS
4685 spawnval = spawnv(mode, path, argvlist);
4686 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004687#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004688 if (mode == _OLD_P_OVERLAY)
4689 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004690
Victor Stinner8c62be82010-05-06 00:08:46 +00004691 Py_BEGIN_ALLOW_THREADS
4692 spawnval = _spawnv(mode, path, argvlist);
4693 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004694#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004695
Victor Stinner8c62be82010-05-06 00:08:46 +00004696 free_string_array(argvlist, argc);
4697 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004698
Victor Stinner8c62be82010-05-06 00:08:46 +00004699 if (spawnval == -1)
4700 return posix_error();
4701 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004702#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004703 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004704#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004705 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004706#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004707}
4708
4709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004710PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004711"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004712Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004713\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004714 mode: mode of process creation\n\
4715 path: path of executable file\n\
4716 args: tuple or list of arguments\n\
4717 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004718
4719static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004720posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004721{
Victor Stinner8c62be82010-05-06 00:08:46 +00004722 PyObject *opath;
4723 char *path;
4724 PyObject *argv, *env;
4725 char **argvlist;
4726 char **envlist;
4727 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004728 int mode;
4729 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004730 Py_intptr_t spawnval;
4731 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4732 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004733
Victor Stinner8c62be82010-05-06 00:08:46 +00004734 /* spawnve has four arguments: (mode, path, argv, env), where
4735 argv is a list or tuple of strings and env is a dictionary
4736 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004737
Victor Stinner8c62be82010-05-06 00:08:46 +00004738 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4739 PyUnicode_FSConverter,
4740 &opath, &argv, &env))
4741 return NULL;
4742 path = PyBytes_AsString(opath);
4743 if (PyList_Check(argv)) {
4744 argc = PyList_Size(argv);
4745 getitem = PyList_GetItem;
4746 }
4747 else if (PyTuple_Check(argv)) {
4748 argc = PyTuple_Size(argv);
4749 getitem = PyTuple_GetItem;
4750 }
4751 else {
4752 PyErr_SetString(PyExc_TypeError,
4753 "spawnve() arg 2 must be a tuple or list");
4754 goto fail_0;
4755 }
4756 if (!PyMapping_Check(env)) {
4757 PyErr_SetString(PyExc_TypeError,
4758 "spawnve() arg 3 must be a mapping object");
4759 goto fail_0;
4760 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004761
Victor Stinner8c62be82010-05-06 00:08:46 +00004762 argvlist = PyMem_NEW(char *, argc+1);
4763 if (argvlist == NULL) {
4764 PyErr_NoMemory();
4765 goto fail_0;
4766 }
4767 for (i = 0; i < argc; i++) {
4768 if (!fsconvert_strdup((*getitem)(argv, i),
4769 &argvlist[i]))
4770 {
4771 lastarg = i;
4772 goto fail_1;
4773 }
4774 }
4775 lastarg = argc;
4776 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004777
Victor Stinner8c62be82010-05-06 00:08:46 +00004778 envlist = parse_envlist(env, &envc);
4779 if (envlist == NULL)
4780 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004781
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004782#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004783 Py_BEGIN_ALLOW_THREADS
4784 spawnval = spawnve(mode, path, argvlist, envlist);
4785 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004786#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004787 if (mode == _OLD_P_OVERLAY)
4788 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004789
Victor Stinner8c62be82010-05-06 00:08:46 +00004790 Py_BEGIN_ALLOW_THREADS
4791 spawnval = _spawnve(mode, path, argvlist, envlist);
4792 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004793#endif
Tim Peters25059d32001-12-07 20:35:43 +00004794
Victor Stinner8c62be82010-05-06 00:08:46 +00004795 if (spawnval == -1)
4796 (void) posix_error();
4797 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004798#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004799 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004800#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004801 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004802#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004803
Victor Stinner8c62be82010-05-06 00:08:46 +00004804 while (--envc >= 0)
4805 PyMem_DEL(envlist[envc]);
4806 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004807 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004808 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004809 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004810 Py_DECREF(opath);
4811 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004812}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004813
4814/* OS/2 supports spawnvp & spawnvpe natively */
4815#if defined(PYOS_OS2)
4816PyDoc_STRVAR(posix_spawnvp__doc__,
4817"spawnvp(mode, file, args)\n\n\
4818Execute the program 'file' in a new process, using the environment\n\
4819search path to find the file.\n\
4820\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004821 mode: mode of process creation\n\
4822 file: executable file name\n\
4823 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004824
4825static PyObject *
4826posix_spawnvp(PyObject *self, PyObject *args)
4827{
Victor Stinner8c62be82010-05-06 00:08:46 +00004828 PyObject *opath;
4829 char *path;
4830 PyObject *argv;
4831 char **argvlist;
4832 int mode, i, argc;
4833 Py_intptr_t spawnval;
4834 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004835
Victor Stinner8c62be82010-05-06 00:08:46 +00004836 /* spawnvp has three arguments: (mode, path, argv), where
4837 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004838
Victor Stinner8c62be82010-05-06 00:08:46 +00004839 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4840 PyUnicode_FSConverter,
4841 &opath, &argv))
4842 return NULL;
4843 path = PyBytes_AsString(opath);
4844 if (PyList_Check(argv)) {
4845 argc = PyList_Size(argv);
4846 getitem = PyList_GetItem;
4847 }
4848 else if (PyTuple_Check(argv)) {
4849 argc = PyTuple_Size(argv);
4850 getitem = PyTuple_GetItem;
4851 }
4852 else {
4853 PyErr_SetString(PyExc_TypeError,
4854 "spawnvp() arg 2 must be a tuple or list");
4855 Py_DECREF(opath);
4856 return NULL;
4857 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004858
Victor Stinner8c62be82010-05-06 00:08:46 +00004859 argvlist = PyMem_NEW(char *, argc+1);
4860 if (argvlist == NULL) {
4861 Py_DECREF(opath);
4862 return PyErr_NoMemory();
4863 }
4864 for (i = 0; i < argc; i++) {
4865 if (!fsconvert_strdup((*getitem)(argv, i),
4866 &argvlist[i])) {
4867 free_string_array(argvlist, i);
4868 PyErr_SetString(
4869 PyExc_TypeError,
4870 "spawnvp() arg 2 must contain only strings");
4871 Py_DECREF(opath);
4872 return NULL;
4873 }
4874 }
4875 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004876
Victor Stinner8c62be82010-05-06 00:08:46 +00004877 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004878#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004880#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004882#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004883 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004884
Victor Stinner8c62be82010-05-06 00:08:46 +00004885 free_string_array(argvlist, argc);
4886 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004887
Victor Stinner8c62be82010-05-06 00:08:46 +00004888 if (spawnval == -1)
4889 return posix_error();
4890 else
4891 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004892}
4893
4894
4895PyDoc_STRVAR(posix_spawnvpe__doc__,
4896"spawnvpe(mode, file, args, env)\n\n\
4897Execute the program 'file' in a new process, using the environment\n\
4898search path to find the file.\n\
4899\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004900 mode: mode of process creation\n\
4901 file: executable file name\n\
4902 args: tuple or list of arguments\n\
4903 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004904
4905static PyObject *
4906posix_spawnvpe(PyObject *self, PyObject *args)
4907{
Ross Lagerwalldcfde5a2011-11-04 07:09:14 +02004908 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00004909 char *path;
4910 PyObject *argv, *env;
4911 char **argvlist;
4912 char **envlist;
4913 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004914 int mode;
4915 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004916 Py_intptr_t spawnval;
4917 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4918 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004919
Victor Stinner8c62be82010-05-06 00:08:46 +00004920 /* spawnvpe has four arguments: (mode, path, argv, env), where
4921 argv is a list or tuple of strings and env is a dictionary
4922 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004923
Victor Stinner8c62be82010-05-06 00:08:46 +00004924 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4925 PyUnicode_FSConverter,
4926 &opath, &argv, &env))
4927 return NULL;
4928 path = PyBytes_AsString(opath);
4929 if (PyList_Check(argv)) {
4930 argc = PyList_Size(argv);
4931 getitem = PyList_GetItem;
4932 }
4933 else if (PyTuple_Check(argv)) {
4934 argc = PyTuple_Size(argv);
4935 getitem = PyTuple_GetItem;
4936 }
4937 else {
4938 PyErr_SetString(PyExc_TypeError,
4939 "spawnvpe() arg 2 must be a tuple or list");
4940 goto fail_0;
4941 }
4942 if (!PyMapping_Check(env)) {
4943 PyErr_SetString(PyExc_TypeError,
4944 "spawnvpe() arg 3 must be a mapping object");
4945 goto fail_0;
4946 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004947
Victor Stinner8c62be82010-05-06 00:08:46 +00004948 argvlist = PyMem_NEW(char *, argc+1);
4949 if (argvlist == NULL) {
4950 PyErr_NoMemory();
4951 goto fail_0;
4952 }
4953 for (i = 0; i < argc; i++) {
4954 if (!fsconvert_strdup((*getitem)(argv, i),
4955 &argvlist[i]))
4956 {
4957 lastarg = i;
4958 goto fail_1;
4959 }
4960 }
4961 lastarg = argc;
4962 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004963
Victor Stinner8c62be82010-05-06 00:08:46 +00004964 envlist = parse_envlist(env, &envc);
4965 if (envlist == NULL)
4966 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004967
Victor Stinner8c62be82010-05-06 00:08:46 +00004968 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004969#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004970 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004971#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004973#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004975
Victor Stinner8c62be82010-05-06 00:08:46 +00004976 if (spawnval == -1)
4977 (void) posix_error();
4978 else
4979 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004980
Victor Stinner8c62be82010-05-06 00:08:46 +00004981 while (--envc >= 0)
4982 PyMem_DEL(envlist[envc]);
4983 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004984 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004986 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004987 Py_DECREF(opath);
4988 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004989}
4990#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004991#endif /* HAVE_SPAWNV */
4992
4993
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004994#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004995PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004996"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004997Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4998\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004999Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005000
5001static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005002posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005003{
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 pid_t pid;
5005 int result = 0;
5006 _PyImport_AcquireLock();
5007 pid = fork1();
5008 if (pid == 0) {
5009 /* child: this clobbers and resets the import lock. */
5010 PyOS_AfterFork();
5011 } else {
5012 /* parent: release the import lock. */
5013 result = _PyImport_ReleaseLock();
5014 }
5015 if (pid == -1)
5016 return posix_error();
5017 if (result < 0) {
5018 /* Don't clobber the OSError if the fork failed. */
5019 PyErr_SetString(PyExc_RuntimeError,
5020 "not holding the import lock");
5021 return NULL;
5022 }
5023 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005024}
5025#endif
5026
5027
Guido van Rossumad0ee831995-03-01 10:34:45 +00005028#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005029PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005030"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005031Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005032Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005033
Barry Warsaw53699e91996-12-10 23:23:01 +00005034static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005035posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005036{
Victor Stinner8c62be82010-05-06 00:08:46 +00005037 pid_t pid;
5038 int result = 0;
5039 _PyImport_AcquireLock();
5040 pid = fork();
5041 if (pid == 0) {
5042 /* child: this clobbers and resets the import lock. */
5043 PyOS_AfterFork();
5044 } else {
5045 /* parent: release the import lock. */
5046 result = _PyImport_ReleaseLock();
5047 }
5048 if (pid == -1)
5049 return posix_error();
5050 if (result < 0) {
5051 /* Don't clobber the OSError if the fork failed. */
5052 PyErr_SetString(PyExc_RuntimeError,
5053 "not holding the import lock");
5054 return NULL;
5055 }
5056 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005057}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005058#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005059
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005060#ifdef HAVE_SCHED_H
5061
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005062#ifdef HAVE_SCHED_GET_PRIORITY_MAX
5063
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005064PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
5065"sched_get_priority_max(policy)\n\n\
5066Get the maximum scheduling priority for *policy*.");
5067
5068static PyObject *
5069posix_sched_get_priority_max(PyObject *self, PyObject *args)
5070{
5071 int policy, max;
5072
5073 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
5074 return NULL;
5075 max = sched_get_priority_max(policy);
5076 if (max < 0)
5077 return posix_error();
5078 return PyLong_FromLong(max);
5079}
5080
5081PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
5082"sched_get_priority_min(policy)\n\n\
5083Get the minimum scheduling priority for *policy*.");
5084
5085static PyObject *
5086posix_sched_get_priority_min(PyObject *self, PyObject *args)
5087{
5088 int policy, min;
5089
5090 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
5091 return NULL;
5092 min = sched_get_priority_min(policy);
5093 if (min < 0)
5094 return posix_error();
5095 return PyLong_FromLong(min);
5096}
5097
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005098#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5099
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005100#ifdef HAVE_SCHED_SETSCHEDULER
5101
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005102PyDoc_STRVAR(posix_sched_getscheduler__doc__,
5103"sched_getscheduler(pid)\n\n\
5104Get the scheduling policy for the process with a PID of *pid*.\n\
5105Passing a PID of 0 returns the scheduling policy for the calling process.");
5106
5107static PyObject *
5108posix_sched_getscheduler(PyObject *self, PyObject *args)
5109{
5110 pid_t pid;
5111 int policy;
5112
5113 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
5114 return NULL;
5115 policy = sched_getscheduler(pid);
5116 if (policy < 0)
5117 return posix_error();
5118 return PyLong_FromLong(policy);
5119}
5120
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005121#endif
5122
5123#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
5124
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005125static PyObject *
5126sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5127{
5128 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05005129 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005130
5131 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
5132 return NULL;
5133 res = PyStructSequence_New(type);
5134 if (!res)
5135 return NULL;
5136 Py_INCREF(priority);
5137 PyStructSequence_SET_ITEM(res, 0, priority);
5138 return res;
5139}
5140
5141PyDoc_STRVAR(sched_param__doc__,
5142"sched_param(sched_priority): A scheduling parameter.\n\n\
5143Current has only one field: sched_priority");
5144
5145static PyStructSequence_Field sched_param_fields[] = {
5146 {"sched_priority", "the scheduling priority"},
5147 {0}
5148};
5149
5150static PyStructSequence_Desc sched_param_desc = {
5151 "sched_param", /* name */
5152 sched_param__doc__, /* doc */
5153 sched_param_fields,
5154 1
5155};
5156
5157static int
5158convert_sched_param(PyObject *param, struct sched_param *res)
5159{
5160 long priority;
5161
5162 if (Py_TYPE(param) != &SchedParamType) {
5163 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5164 return 0;
5165 }
5166 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5167 if (priority == -1 && PyErr_Occurred())
5168 return 0;
5169 if (priority > INT_MAX || priority < INT_MIN) {
5170 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5171 return 0;
5172 }
5173 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5174 return 1;
5175}
5176
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005177#endif
5178
5179#ifdef HAVE_SCHED_SETSCHEDULER
5180
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005181PyDoc_STRVAR(posix_sched_setscheduler__doc__,
5182"sched_setscheduler(pid, policy, param)\n\n\
5183Set the scheduling policy, *policy*, for *pid*.\n\
5184If *pid* is 0, the calling process is changed.\n\
5185*param* is an instance of sched_param.");
5186
5187static PyObject *
5188posix_sched_setscheduler(PyObject *self, PyObject *args)
5189{
5190 pid_t pid;
5191 int policy;
5192 struct sched_param param;
5193
5194 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
5195 &pid, &policy, &convert_sched_param, &param))
5196 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02005197
5198 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005199 ** sched_setscheduler() returns 0 in Linux, but the previous
5200 ** scheduling policy under Solaris/Illumos, and others.
5201 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005202 */
5203 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005204 return posix_error();
5205 Py_RETURN_NONE;
5206}
5207
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005208#endif
5209
5210#ifdef HAVE_SCHED_SETPARAM
5211
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005212PyDoc_STRVAR(posix_sched_getparam__doc__,
5213"sched_getparam(pid) -> sched_param\n\n\
5214Returns scheduling parameters for the process with *pid* as an instance of the\n\
5215sched_param class. A PID of 0 means the calling process.");
5216
5217static PyObject *
5218posix_sched_getparam(PyObject *self, PyObject *args)
5219{
5220 pid_t pid;
5221 struct sched_param param;
5222 PyObject *res, *priority;
5223
5224 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
5225 return NULL;
5226 if (sched_getparam(pid, &param))
5227 return posix_error();
5228 res = PyStructSequence_New(&SchedParamType);
5229 if (!res)
5230 return NULL;
5231 priority = PyLong_FromLong(param.sched_priority);
5232 if (!priority) {
5233 Py_DECREF(res);
5234 return NULL;
5235 }
5236 PyStructSequence_SET_ITEM(res, 0, priority);
5237 return res;
5238}
5239
5240PyDoc_STRVAR(posix_sched_setparam__doc__,
5241"sched_setparam(pid, param)\n\n\
5242Set scheduling parameters for a process with PID *pid*.\n\
5243A PID of 0 means the calling process.");
5244
5245static PyObject *
5246posix_sched_setparam(PyObject *self, PyObject *args)
5247{
5248 pid_t pid;
5249 struct sched_param param;
5250
5251 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
5252 &pid, &convert_sched_param, &param))
5253 return NULL;
5254 if (sched_setparam(pid, &param))
5255 return posix_error();
5256 Py_RETURN_NONE;
5257}
5258
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005259#endif
5260
5261#ifdef HAVE_SCHED_RR_GET_INTERVAL
5262
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005263PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
5264"sched_rr_get_interval(pid) -> float\n\n\
5265Return the round-robin quantum for the process with PID *pid* in seconds.");
5266
5267static PyObject *
5268posix_sched_rr_get_interval(PyObject *self, PyObject *args)
5269{
5270 pid_t pid;
5271 struct timespec interval;
5272
5273 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
5274 return NULL;
5275 if (sched_rr_get_interval(pid, &interval))
5276 return posix_error();
5277 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
5278}
5279
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005280#endif
5281
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005282PyDoc_STRVAR(posix_sched_yield__doc__,
5283"sched_yield()\n\n\
5284Voluntarily relinquish the CPU.");
5285
5286static PyObject *
5287posix_sched_yield(PyObject *self, PyObject *noargs)
5288{
5289 if (sched_yield())
5290 return posix_error();
5291 Py_RETURN_NONE;
5292}
5293
Benjamin Peterson2740af82011-08-02 17:41:34 -05005294#ifdef HAVE_SCHED_SETAFFINITY
5295
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005296typedef struct {
5297 PyObject_HEAD;
5298 Py_ssize_t size;
5299 int ncpus;
5300 cpu_set_t *set;
5301} Py_cpu_set;
5302
5303static PyTypeObject cpu_set_type;
5304
5305static void
5306cpu_set_dealloc(Py_cpu_set *set)
5307{
5308 assert(set->set);
5309 CPU_FREE(set->set);
5310 Py_TYPE(set)->tp_free(set);
5311}
5312
5313static Py_cpu_set *
5314make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
5315{
5316 Py_cpu_set *set;
5317
5318 if (size < 0) {
5319 PyErr_SetString(PyExc_ValueError, "negative size");
5320 return NULL;
5321 }
5322 set = (Py_cpu_set *)type->tp_alloc(type, 0);
5323 if (!set)
5324 return NULL;
5325 set->ncpus = size;
5326 set->size = CPU_ALLOC_SIZE(size);
5327 set->set = CPU_ALLOC(size);
5328 if (!set->set) {
5329 type->tp_free(set);
5330 PyErr_NoMemory();
5331 return NULL;
5332 }
5333 CPU_ZERO_S(set->size, set->set);
5334 return set;
5335}
5336
5337static PyObject *
5338cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5339{
5340 int size;
5341
5342 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
5343 !PyArg_ParseTuple(args, "i:cpu_set", &size))
5344 return NULL;
5345 return (PyObject *)make_new_cpu_set(type, size);
5346}
5347
5348static PyObject *
5349cpu_set_repr(Py_cpu_set *set)
5350{
5351 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
Victor Stinner63941882011-09-29 00:42:28 +02005352}
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005353
5354static Py_ssize_t
5355cpu_set_len(Py_cpu_set *set)
5356{
5357 return set->ncpus;
5358}
5359
5360static int
5361_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
5362{
5363 int cpu;
5364 if (!PyArg_ParseTuple(args, requester, &cpu))
5365 return -1;
5366 if (cpu < 0) {
5367 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
5368 return -1;
5369 }
5370 if (cpu >= set->ncpus) {
5371 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
5372 return -1;
5373 }
5374 return cpu;
5375}
5376
5377PyDoc_STRVAR(cpu_set_set_doc,
5378"cpu_set.set(i)\n\n\
5379Add CPU *i* to the set.");
5380
5381static PyObject *
5382cpu_set_set(Py_cpu_set *set, PyObject *args)
5383{
5384 int cpu = _get_cpu(set, "i|set", args);
5385 if (cpu == -1)
5386 return NULL;
5387 CPU_SET_S(cpu, set->size, set->set);
5388 Py_RETURN_NONE;
5389}
5390
5391PyDoc_STRVAR(cpu_set_count_doc,
5392"cpu_set.count() -> int\n\n\
5393Return the number of CPUs active in the set.");
5394
5395static PyObject *
5396cpu_set_count(Py_cpu_set *set, PyObject *noargs)
5397{
5398 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
5399}
5400
5401PyDoc_STRVAR(cpu_set_clear_doc,
5402"cpu_set.clear(i)\n\n\
5403Remove CPU *i* from the set.");
5404
5405static PyObject *
5406cpu_set_clear(Py_cpu_set *set, PyObject *args)
5407{
5408 int cpu = _get_cpu(set, "i|clear", args);
5409 if (cpu == -1)
5410 return NULL;
5411 CPU_CLR_S(cpu, set->size, set->set);
5412 Py_RETURN_NONE;
5413}
5414
5415PyDoc_STRVAR(cpu_set_isset_doc,
5416"cpu_set.isset(i) -> bool\n\n\
5417Test if CPU *i* is in the set.");
5418
5419static PyObject *
5420cpu_set_isset(Py_cpu_set *set, PyObject *args)
5421{
5422 int cpu = _get_cpu(set, "i|isset", args);
5423 if (cpu == -1)
5424 return NULL;
5425 if (CPU_ISSET_S(cpu, set->size, set->set))
5426 Py_RETURN_TRUE;
5427 Py_RETURN_FALSE;
5428}
5429
5430PyDoc_STRVAR(cpu_set_zero_doc,
5431"cpu_set.zero()\n\n\
5432Clear the cpu_set.");
5433
5434static PyObject *
5435cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
5436{
5437 CPU_ZERO_S(set->size, set->set);
5438 Py_RETURN_NONE;
5439}
5440
5441static PyObject *
5442cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
5443{
5444 int eq;
5445
Brian Curtindfc80e32011-08-10 20:28:54 -05005446 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type)
5447 Py_RETURN_NOTIMPLEMENTED;
5448
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005449 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
5450 if ((op == Py_EQ) ? eq : !eq)
5451 Py_RETURN_TRUE;
5452 else
5453 Py_RETURN_FALSE;
5454}
5455
5456#define CPU_SET_BINOP(name, op) \
5457 static PyObject * \
5458 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
5459 if (res) { \
5460 Py_INCREF(res); \
5461 } \
5462 else { \
Benjamin Petersone870fe62011-08-02 17:44:26 -05005463 res = make_new_cpu_set(&cpu_set_type, left->ncpus); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005464 if (!res) \
5465 return NULL; \
5466 } \
Benjamin Peterson9b374bf2011-08-02 18:22:30 -05005467 if (Py_TYPE(right) != &cpu_set_type || left->ncpus != right->ncpus) { \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005468 Py_DECREF(res); \
Brian Curtindfc80e32011-08-10 20:28:54 -05005469 Py_RETURN_NOTIMPLEMENTED; \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005470 } \
Benjamin Peterson8f7bdd32011-08-02 18:34:30 -05005471 assert(left->size == right->size && right->size == res->size); \
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005472 op(res->size, res->set, left->set, right->set); \
5473 return (PyObject *)res; \
5474 } \
5475 static PyObject * \
5476 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
5477 return do_cpu_set_##name(left, right, NULL); \
5478 } \
5479 static PyObject * \
5480 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
5481 return do_cpu_set_##name(left, right, left); \
5482 } \
5483
5484CPU_SET_BINOP(and, CPU_AND_S)
5485CPU_SET_BINOP(or, CPU_OR_S)
5486CPU_SET_BINOP(xor, CPU_XOR_S)
5487#undef CPU_SET_BINOP
5488
5489PyDoc_STRVAR(cpu_set_doc,
5490"cpu_set(size)\n\n\
5491Create an empty mask of CPUs.");
5492
5493static PyNumberMethods cpu_set_as_number = {
5494 0, /*nb_add*/
5495 0, /*nb_subtract*/
5496 0, /*nb_multiply*/
5497 0, /*nb_remainder*/
5498 0, /*nb_divmod*/
5499 0, /*nb_power*/
5500 0, /*nb_negative*/
5501 0, /*nb_positive*/
5502 0, /*nb_absolute*/
5503 0, /*nb_bool*/
5504 0, /*nb_invert*/
5505 0, /*nb_lshift*/
5506 0, /*nb_rshift*/
5507 (binaryfunc)cpu_set_and, /*nb_and*/
5508 (binaryfunc)cpu_set_xor, /*nb_xor*/
5509 (binaryfunc)cpu_set_or, /*nb_or*/
5510 0, /*nb_int*/
5511 0, /*nb_reserved*/
5512 0, /*nb_float*/
5513 0, /*nb_inplace_add*/
5514 0, /*nb_inplace_subtract*/
5515 0, /*nb_inplace_multiply*/
5516 0, /*nb_inplace_remainder*/
5517 0, /*nb_inplace_power*/
5518 0, /*nb_inplace_lshift*/
5519 0, /*nb_inplace_rshift*/
5520 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
5521 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
5522 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
5523};
5524
5525static PySequenceMethods cpu_set_as_sequence = {
5526 (lenfunc)cpu_set_len, /* sq_length */
5527};
5528
5529static PyMethodDef cpu_set_methods[] = {
5530 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
5531 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
5532 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
5533 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
5534 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
5535 {NULL, NULL} /* sentinel */
5536};
5537
5538static PyTypeObject cpu_set_type = {
5539 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5540 "posix.cpu_set", /* tp_name */
5541 sizeof(Py_cpu_set), /* tp_basicsize */
5542 0, /* tp_itemsize */
5543 /* methods */
5544 (destructor)cpu_set_dealloc, /* tp_dealloc */
5545 0, /* tp_print */
5546 0, /* tp_getattr */
5547 0, /* tp_setattr */
5548 0, /* tp_reserved */
5549 (reprfunc)cpu_set_repr, /* tp_repr */
5550 &cpu_set_as_number, /* tp_as_number */
5551 &cpu_set_as_sequence, /* tp_as_sequence */
5552 0, /* tp_as_mapping */
5553 PyObject_HashNotImplemented, /* tp_hash */
5554 0, /* tp_call */
5555 0, /* tp_str */
5556 PyObject_GenericGetAttr, /* tp_getattro */
5557 0, /* tp_setattro */
5558 0, /* tp_as_buffer */
5559 Py_TPFLAGS_DEFAULT, /* tp_flags */
5560 cpu_set_doc, /* tp_doc */
5561 0, /* tp_traverse */
5562 0, /* tp_clear */
5563 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5564 0, /* tp_weaklistoffset */
5565 0, /* tp_iter */
5566 0, /* tp_iternext */
5567 cpu_set_methods, /* tp_methods */
5568 0, /* tp_members */
5569 0, /* tp_getset */
5570 0, /* tp_base */
5571 0, /* tp_dict */
5572 0, /* tp_descr_get */
5573 0, /* tp_descr_set */
5574 0, /* tp_dictoffset */
5575 0, /* tp_init */
5576 PyType_GenericAlloc, /* tp_alloc */
5577 cpu_set_new, /* tp_new */
5578 PyObject_Del, /* tp_free */
5579};
5580
5581PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5582"sched_setaffinity(pid, cpu_set)\n\n\
5583Set the affinity of the process with PID *pid* to *cpu_set*.");
5584
5585static PyObject *
5586posix_sched_setaffinity(PyObject *self, PyObject *args)
5587{
5588 pid_t pid;
5589 Py_cpu_set *cpu_set;
5590
Benjamin Petersona17a5d62011-08-09 16:49:13 -05005591 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!:sched_setaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005592 &pid, &cpu_set_type, &cpu_set))
5593 return NULL;
5594 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5595 return posix_error();
5596 Py_RETURN_NONE;
5597}
5598
5599PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5600"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5601Return the affinity of the process with PID *pid*.\n\
5602The returned cpu_set will be of size *ncpus*.");
5603
5604static PyObject *
5605posix_sched_getaffinity(PyObject *self, PyObject *args)
5606{
5607 pid_t pid;
5608 int ncpus;
5609 Py_cpu_set *res;
5610
Benjamin Peterson7ac92142011-08-03 08:54:26 -05005611 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:sched_getaffinity",
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005612 &pid, &ncpus))
5613 return NULL;
5614 res = make_new_cpu_set(&cpu_set_type, ncpus);
5615 if (!res)
5616 return NULL;
5617 if (sched_getaffinity(pid, res->size, res->set)) {
5618 Py_DECREF(res);
5619 return posix_error();
5620 }
5621 return (PyObject *)res;
5622}
5623
Benjamin Peterson2740af82011-08-02 17:41:34 -05005624#endif /* HAVE_SCHED_SETAFFINITY */
5625
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005626#endif /* HAVE_SCHED_H */
5627
Neal Norwitzb59798b2003-03-21 01:43:31 +00005628/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005629/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5630#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005631#define DEV_PTY_FILE "/dev/ptc"
5632#define HAVE_DEV_PTMX
5633#else
5634#define DEV_PTY_FILE "/dev/ptmx"
5635#endif
5636
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005637#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005638#ifdef HAVE_PTY_H
5639#include <pty.h>
5640#else
5641#ifdef HAVE_LIBUTIL_H
5642#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005643#else
5644#ifdef HAVE_UTIL_H
5645#include <util.h>
5646#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005647#endif /* HAVE_LIBUTIL_H */
5648#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005649#ifdef HAVE_STROPTS_H
5650#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005651#endif
5652#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005653
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005654#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005655PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005656"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005657Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005658
5659static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005660posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005661{
Victor Stinner8c62be82010-05-06 00:08:46 +00005662 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005663#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005664 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005665#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005666#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005667 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005668#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005669 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005670#endif
5671#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005672
Thomas Wouters70c21a12000-07-14 14:28:33 +00005673#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005674 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5675 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005676#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005677 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5678 if (slave_name == NULL)
5679 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005680
Victor Stinner8c62be82010-05-06 00:08:46 +00005681 slave_fd = open(slave_name, O_RDWR);
5682 if (slave_fd < 0)
5683 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005684#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005685 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5686 if (master_fd < 0)
5687 return posix_error();
5688 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5689 /* change permission of slave */
5690 if (grantpt(master_fd) < 0) {
5691 PyOS_setsig(SIGCHLD, sig_saved);
5692 return posix_error();
5693 }
5694 /* unlock slave */
5695 if (unlockpt(master_fd) < 0) {
5696 PyOS_setsig(SIGCHLD, sig_saved);
5697 return posix_error();
5698 }
5699 PyOS_setsig(SIGCHLD, sig_saved);
5700 slave_name = ptsname(master_fd); /* get name of slave */
5701 if (slave_name == NULL)
5702 return posix_error();
5703 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5704 if (slave_fd < 0)
5705 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005706#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005707 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5708 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005709#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005710 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005711#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005712#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005713#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005714
Victor Stinner8c62be82010-05-06 00:08:46 +00005715 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005716
Fred Drake8cef4cf2000-06-28 16:40:38 +00005717}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005718#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005719
5720#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005721PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005722"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005723Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5724Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005725To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005726
5727static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005728posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005729{
Victor Stinner8c62be82010-05-06 00:08:46 +00005730 int master_fd = -1, result = 0;
5731 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005732
Victor Stinner8c62be82010-05-06 00:08:46 +00005733 _PyImport_AcquireLock();
5734 pid = forkpty(&master_fd, NULL, NULL, NULL);
5735 if (pid == 0) {
5736 /* child: this clobbers and resets the import lock. */
5737 PyOS_AfterFork();
5738 } else {
5739 /* parent: release the import lock. */
5740 result = _PyImport_ReleaseLock();
5741 }
5742 if (pid == -1)
5743 return posix_error();
5744 if (result < 0) {
5745 /* Don't clobber the OSError if the fork failed. */
5746 PyErr_SetString(PyExc_RuntimeError,
5747 "not holding the import lock");
5748 return NULL;
5749 }
5750 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005751}
5752#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005753
Ross Lagerwall7807c352011-03-17 20:20:30 +02005754
Guido van Rossumad0ee831995-03-01 10:34:45 +00005755#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005756PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005757"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005758Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005759
Barry Warsaw53699e91996-12-10 23:23:01 +00005760static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005761posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005762{
Victor Stinner8c62be82010-05-06 00:08:46 +00005763 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005764}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005765#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005766
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005767
Guido van Rossumad0ee831995-03-01 10:34:45 +00005768#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005769PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005770"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005771Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005772
Barry Warsaw53699e91996-12-10 23:23:01 +00005773static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005774posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005775{
Victor Stinner8c62be82010-05-06 00:08:46 +00005776 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005777}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005778#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005779
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005780
Guido van Rossumad0ee831995-03-01 10:34:45 +00005781#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005782PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005783"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005784Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005785
Barry Warsaw53699e91996-12-10 23:23:01 +00005786static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005787posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005788{
Victor Stinner8c62be82010-05-06 00:08:46 +00005789 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005790}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005791#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005792
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005793
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005794PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005795"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005796Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005797
Barry Warsaw53699e91996-12-10 23:23:01 +00005798static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005799posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005800{
Victor Stinner8c62be82010-05-06 00:08:46 +00005801 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005802}
5803
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005804#ifdef HAVE_GETGROUPLIST
5805PyDoc_STRVAR(posix_getgrouplist__doc__,
5806"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5807Returns a list of groups to which a user belongs.\n\n\
5808 user: username to lookup\n\
5809 group: base group id of the user");
5810
5811static PyObject *
5812posix_getgrouplist(PyObject *self, PyObject *args)
5813{
5814#ifdef NGROUPS_MAX
5815#define MAX_GROUPS NGROUPS_MAX
5816#else
5817 /* defined to be 16 on Solaris7, so this should be a small number */
5818#define MAX_GROUPS 64
5819#endif
5820
5821 const char *user;
5822 int i, ngroups;
5823 PyObject *list;
5824#ifdef __APPLE__
5825 int *groups, basegid;
5826#else
5827 gid_t *groups, basegid;
5828#endif
5829 ngroups = MAX_GROUPS;
5830
5831 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5832 return NULL;
5833
5834#ifdef __APPLE__
5835 groups = PyMem_Malloc(ngroups * sizeof(int));
5836#else
5837 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5838#endif
5839 if (groups == NULL)
5840 return PyErr_NoMemory();
5841
5842 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5843 PyMem_Del(groups);
5844 return posix_error();
5845 }
5846
5847 list = PyList_New(ngroups);
5848 if (list == NULL) {
5849 PyMem_Del(groups);
5850 return NULL;
5851 }
5852
5853 for (i = 0; i < ngroups; i++) {
5854 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5855 if (o == NULL) {
5856 Py_DECREF(list);
5857 PyMem_Del(groups);
5858 return NULL;
5859 }
5860 PyList_SET_ITEM(list, i, o);
5861 }
5862
5863 PyMem_Del(groups);
5864
5865 return list;
5866}
5867#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005868
Fred Drakec9680921999-12-13 16:37:25 +00005869#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005870PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005871"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005873
5874static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005875posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005876{
5877 PyObject *result = NULL;
5878
Fred Drakec9680921999-12-13 16:37:25 +00005879#ifdef NGROUPS_MAX
5880#define MAX_GROUPS NGROUPS_MAX
5881#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005882 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005883#define MAX_GROUPS 64
5884#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005885 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005886
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005887 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005888 * This is a helper variable to store the intermediate result when
5889 * that happens.
5890 *
5891 * To keep the code readable the OSX behaviour is unconditional,
5892 * according to the POSIX spec this should be safe on all unix-y
5893 * systems.
5894 */
5895 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005896 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005897
Victor Stinner8c62be82010-05-06 00:08:46 +00005898 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005899 if (n < 0) {
5900 if (errno == EINVAL) {
5901 n = getgroups(0, NULL);
5902 if (n == -1) {
5903 return posix_error();
5904 }
5905 if (n == 0) {
5906 /* Avoid malloc(0) */
5907 alt_grouplist = grouplist;
5908 } else {
5909 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5910 if (alt_grouplist == NULL) {
5911 errno = EINVAL;
5912 return posix_error();
5913 }
5914 n = getgroups(n, alt_grouplist);
5915 if (n == -1) {
5916 PyMem_Free(alt_grouplist);
5917 return posix_error();
5918 }
5919 }
5920 } else {
5921 return posix_error();
5922 }
5923 }
5924 result = PyList_New(n);
5925 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 int i;
5927 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005928 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005930 Py_DECREF(result);
5931 result = NULL;
5932 break;
Fred Drakec9680921999-12-13 16:37:25 +00005933 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005935 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005936 }
5937
5938 if (alt_grouplist != grouplist) {
5939 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005941
Fred Drakec9680921999-12-13 16:37:25 +00005942 return result;
5943}
5944#endif
5945
Antoine Pitroub7572f02009-12-02 20:46:48 +00005946#ifdef HAVE_INITGROUPS
5947PyDoc_STRVAR(posix_initgroups__doc__,
5948"initgroups(username, gid) -> None\n\n\
5949Call the system initgroups() to initialize the group access list with all of\n\
5950the groups of which the specified username is a member, plus the specified\n\
5951group id.");
5952
5953static PyObject *
5954posix_initgroups(PyObject *self, PyObject *args)
5955{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005956 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005957 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005958 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005959 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005960
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005961 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5962 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005964 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005965
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005966 res = initgroups(username, (gid_t) gid);
5967 Py_DECREF(oname);
5968 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005969 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005970
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 Py_INCREF(Py_None);
5972 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005973}
5974#endif
5975
Martin v. Löwis606edc12002-06-13 21:09:11 +00005976#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005977PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005978"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005979Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005980
5981static PyObject *
5982posix_getpgid(PyObject *self, PyObject *args)
5983{
Victor Stinner8c62be82010-05-06 00:08:46 +00005984 pid_t pid, pgid;
5985 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5986 return NULL;
5987 pgid = getpgid(pid);
5988 if (pgid < 0)
5989 return posix_error();
5990 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005991}
5992#endif /* HAVE_GETPGID */
5993
5994
Guido van Rossumb6775db1994-08-01 11:34:53 +00005995#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005996PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005997"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005999
Barry Warsaw53699e91996-12-10 23:23:01 +00006000static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006001posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00006002{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006003#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006005#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006006 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006007#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006008}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006009#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006010
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006011
Guido van Rossumb6775db1994-08-01 11:34:53 +00006012#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006013PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006014"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00006015Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006016
Barry Warsaw53699e91996-12-10 23:23:01 +00006017static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006018posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006019{
Guido van Rossum64933891994-10-20 21:56:42 +00006020#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006022#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006023 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006024#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006025 return posix_error();
6026 Py_INCREF(Py_None);
6027 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006028}
6029
Guido van Rossumb6775db1994-08-01 11:34:53 +00006030#endif /* HAVE_SETPGRP */
6031
Guido van Rossumad0ee831995-03-01 10:34:45 +00006032#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006033
6034#ifdef MS_WINDOWS
6035#include <tlhelp32.h>
6036
6037static PyObject*
6038win32_getppid()
6039{
6040 HANDLE snapshot;
6041 pid_t mypid;
6042 PyObject* result = NULL;
6043 BOOL have_record;
6044 PROCESSENTRY32 pe;
6045
6046 mypid = getpid(); /* This function never fails */
6047
6048 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6049 if (snapshot == INVALID_HANDLE_VALUE)
6050 return PyErr_SetFromWindowsErr(GetLastError());
6051
6052 pe.dwSize = sizeof(pe);
6053 have_record = Process32First(snapshot, &pe);
6054 while (have_record) {
6055 if (mypid == (pid_t)pe.th32ProcessID) {
6056 /* We could cache the ulong value in a static variable. */
6057 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6058 break;
6059 }
6060
6061 have_record = Process32Next(snapshot, &pe);
6062 }
6063
6064 /* If our loop exits and our pid was not found (result will be NULL)
6065 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6066 * error anyway, so let's raise it. */
6067 if (!result)
6068 result = PyErr_SetFromWindowsErr(GetLastError());
6069
6070 CloseHandle(snapshot);
6071
6072 return result;
6073}
6074#endif /*MS_WINDOWS*/
6075
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006076PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006077"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006078Return the parent's process id. If the parent process has already exited,\n\
6079Windows machines will still return its id; others systems will return the id\n\
6080of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006081
Barry Warsaw53699e91996-12-10 23:23:01 +00006082static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006083posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006084{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006085#ifdef MS_WINDOWS
6086 return win32_getppid();
6087#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006089#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006090}
6091#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006092
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006093
Fred Drake12c6e2d1999-12-14 21:25:03 +00006094#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006095PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006096"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006097Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00006098
6099static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006100posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006101{
Victor Stinner8c62be82010-05-06 00:08:46 +00006102 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006103#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006104 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006105 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006106
6107 if (GetUserNameW(user_name, &num_chars)) {
6108 /* num_chars is the number of unicode chars plus null terminator */
6109 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006110 }
6111 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006112 result = PyErr_SetFromWindowsErr(GetLastError());
6113#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 char *name;
6115 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006116
Victor Stinner8c62be82010-05-06 00:08:46 +00006117 errno = 0;
6118 name = getlogin();
6119 if (name == NULL) {
6120 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006121 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006122 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006123 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006124 }
6125 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006126 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006127 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006128#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006129 return result;
6130}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006131#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006132
Guido van Rossumad0ee831995-03-01 10:34:45 +00006133#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006134PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006135"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006136Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006137
Barry Warsaw53699e91996-12-10 23:23:01 +00006138static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006139posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00006140{
Victor Stinner8c62be82010-05-06 00:08:46 +00006141 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006142}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006143#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00006144
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006145
Guido van Rossumad0ee831995-03-01 10:34:45 +00006146#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006147PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006148"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006149Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006150
Barry Warsaw53699e91996-12-10 23:23:01 +00006151static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006152posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006153{
Victor Stinner8c62be82010-05-06 00:08:46 +00006154 pid_t pid;
6155 int sig;
6156 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
6157 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00006158#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006159 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
6160 APIRET rc;
6161 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006162 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006163
6164 } else if (sig == XCPT_SIGNAL_KILLPROC) {
6165 APIRET rc;
6166 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006167 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006168
6169 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00006170 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006171#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006172 if (kill(pid, sig) == -1)
6173 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006174#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006175 Py_INCREF(Py_None);
6176 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00006177}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006178#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006179
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006180#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006181PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006182"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006183Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006184
6185static PyObject *
6186posix_killpg(PyObject *self, PyObject *args)
6187{
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 int sig;
6189 pid_t pgid;
6190 /* XXX some man pages make the `pgid` parameter an int, others
6191 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6192 take the same type. Moreover, pid_t is always at least as wide as
6193 int (else compilation of this module fails), which is safe. */
6194 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
6195 return NULL;
6196 if (killpg(pgid, sig) == -1)
6197 return posix_error();
6198 Py_INCREF(Py_None);
6199 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006200}
6201#endif
6202
Brian Curtineb24d742010-04-12 17:16:38 +00006203#ifdef MS_WINDOWS
6204PyDoc_STRVAR(win32_kill__doc__,
6205"kill(pid, sig)\n\n\
6206Kill a process with a signal.");
6207
6208static PyObject *
6209win32_kill(PyObject *self, PyObject *args)
6210{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006211 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00006212 DWORD pid, sig, err;
6213 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006214
Victor Stinner8c62be82010-05-06 00:08:46 +00006215 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
6216 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00006217
Victor Stinner8c62be82010-05-06 00:08:46 +00006218 /* Console processes which share a common console can be sent CTRL+C or
6219 CTRL+BREAK events, provided they handle said events. */
6220 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
6221 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
6222 err = GetLastError();
6223 PyErr_SetFromWindowsErr(err);
6224 }
6225 else
6226 Py_RETURN_NONE;
6227 }
Brian Curtineb24d742010-04-12 17:16:38 +00006228
Victor Stinner8c62be82010-05-06 00:08:46 +00006229 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6230 attempt to open and terminate the process. */
6231 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
6232 if (handle == NULL) {
6233 err = GetLastError();
6234 return PyErr_SetFromWindowsErr(err);
6235 }
Brian Curtineb24d742010-04-12 17:16:38 +00006236
Victor Stinner8c62be82010-05-06 00:08:46 +00006237 if (TerminateProcess(handle, sig) == 0) {
6238 err = GetLastError();
6239 result = PyErr_SetFromWindowsErr(err);
6240 } else {
6241 Py_INCREF(Py_None);
6242 result = Py_None;
6243 }
Brian Curtineb24d742010-04-12 17:16:38 +00006244
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 CloseHandle(handle);
6246 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006247}
6248#endif /* MS_WINDOWS */
6249
Guido van Rossumc0125471996-06-28 18:55:32 +00006250#ifdef HAVE_PLOCK
6251
6252#ifdef HAVE_SYS_LOCK_H
6253#include <sys/lock.h>
6254#endif
6255
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006256PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006257"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006258Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006259
Barry Warsaw53699e91996-12-10 23:23:01 +00006260static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006261posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00006262{
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 int op;
6264 if (!PyArg_ParseTuple(args, "i:plock", &op))
6265 return NULL;
6266 if (plock(op) == -1)
6267 return posix_error();
6268 Py_INCREF(Py_None);
6269 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00006270}
6271#endif
6272
Guido van Rossumb6775db1994-08-01 11:34:53 +00006273#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006275"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006276Set the current process's user id.");
6277
Barry Warsaw53699e91996-12-10 23:23:01 +00006278static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006279posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006280{
Victor Stinner8c62be82010-05-06 00:08:46 +00006281 long uid_arg;
6282 uid_t uid;
6283 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
6284 return NULL;
6285 uid = uid_arg;
6286 if (uid != uid_arg) {
6287 PyErr_SetString(PyExc_OverflowError, "user id too big");
6288 return NULL;
6289 }
6290 if (setuid(uid) < 0)
6291 return posix_error();
6292 Py_INCREF(Py_None);
6293 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006294}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006295#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006296
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006297
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006298#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006299PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006300"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006301Set the current process's effective user id.");
6302
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006303static PyObject *
6304posix_seteuid (PyObject *self, PyObject *args)
6305{
Victor Stinner8c62be82010-05-06 00:08:46 +00006306 long euid_arg;
6307 uid_t euid;
6308 if (!PyArg_ParseTuple(args, "l", &euid_arg))
6309 return NULL;
6310 euid = euid_arg;
6311 if (euid != euid_arg) {
6312 PyErr_SetString(PyExc_OverflowError, "user id too big");
6313 return NULL;
6314 }
6315 if (seteuid(euid) < 0) {
6316 return posix_error();
6317 } else {
6318 Py_INCREF(Py_None);
6319 return Py_None;
6320 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006321}
6322#endif /* HAVE_SETEUID */
6323
6324#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006325PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006326"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006327Set the current process's effective group id.");
6328
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006329static PyObject *
6330posix_setegid (PyObject *self, PyObject *args)
6331{
Victor Stinner8c62be82010-05-06 00:08:46 +00006332 long egid_arg;
6333 gid_t egid;
6334 if (!PyArg_ParseTuple(args, "l", &egid_arg))
6335 return NULL;
6336 egid = egid_arg;
6337 if (egid != egid_arg) {
6338 PyErr_SetString(PyExc_OverflowError, "group id too big");
6339 return NULL;
6340 }
6341 if (setegid(egid) < 0) {
6342 return posix_error();
6343 } else {
6344 Py_INCREF(Py_None);
6345 return Py_None;
6346 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006347}
6348#endif /* HAVE_SETEGID */
6349
6350#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006351PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006352"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006353Set the current process's real and effective user ids.");
6354
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006355static PyObject *
6356posix_setreuid (PyObject *self, PyObject *args)
6357{
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 long ruid_arg, euid_arg;
6359 uid_t ruid, euid;
6360 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
6361 return NULL;
6362 if (ruid_arg == -1)
6363 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
6364 else
6365 ruid = ruid_arg; /* otherwise, assign from our long */
6366 if (euid_arg == -1)
6367 euid = (uid_t)-1;
6368 else
6369 euid = euid_arg;
6370 if ((euid_arg != -1 && euid != euid_arg) ||
6371 (ruid_arg != -1 && ruid != ruid_arg)) {
6372 PyErr_SetString(PyExc_OverflowError, "user id too big");
6373 return NULL;
6374 }
6375 if (setreuid(ruid, euid) < 0) {
6376 return posix_error();
6377 } else {
6378 Py_INCREF(Py_None);
6379 return Py_None;
6380 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006381}
6382#endif /* HAVE_SETREUID */
6383
6384#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006385PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006386"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006387Set the current process's real and effective group ids.");
6388
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006389static PyObject *
6390posix_setregid (PyObject *self, PyObject *args)
6391{
Victor Stinner8c62be82010-05-06 00:08:46 +00006392 long rgid_arg, egid_arg;
6393 gid_t rgid, egid;
6394 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
6395 return NULL;
6396 if (rgid_arg == -1)
6397 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
6398 else
6399 rgid = rgid_arg; /* otherwise, assign from our long */
6400 if (egid_arg == -1)
6401 egid = (gid_t)-1;
6402 else
6403 egid = egid_arg;
6404 if ((egid_arg != -1 && egid != egid_arg) ||
6405 (rgid_arg != -1 && rgid != rgid_arg)) {
6406 PyErr_SetString(PyExc_OverflowError, "group id too big");
6407 return NULL;
6408 }
6409 if (setregid(rgid, egid) < 0) {
6410 return posix_error();
6411 } else {
6412 Py_INCREF(Py_None);
6413 return Py_None;
6414 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00006415}
6416#endif /* HAVE_SETREGID */
6417
Guido van Rossumb6775db1994-08-01 11:34:53 +00006418#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006419PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006420"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006421Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006422
Barry Warsaw53699e91996-12-10 23:23:01 +00006423static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006424posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006425{
Victor Stinner8c62be82010-05-06 00:08:46 +00006426 long gid_arg;
6427 gid_t gid;
6428 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
6429 return NULL;
6430 gid = gid_arg;
6431 if (gid != gid_arg) {
6432 PyErr_SetString(PyExc_OverflowError, "group id too big");
6433 return NULL;
6434 }
6435 if (setgid(gid) < 0)
6436 return posix_error();
6437 Py_INCREF(Py_None);
6438 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006439}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006440#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006441
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006442#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006443PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006444"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006445Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006446
6447static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006448posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006449{
Victor Stinner8c62be82010-05-06 00:08:46 +00006450 int i, len;
6451 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006452
Victor Stinner8c62be82010-05-06 00:08:46 +00006453 if (!PySequence_Check(groups)) {
6454 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6455 return NULL;
6456 }
6457 len = PySequence_Size(groups);
6458 if (len > MAX_GROUPS) {
6459 PyErr_SetString(PyExc_ValueError, "too many groups");
6460 return NULL;
6461 }
6462 for(i = 0; i < len; i++) {
6463 PyObject *elem;
6464 elem = PySequence_GetItem(groups, i);
6465 if (!elem)
6466 return NULL;
6467 if (!PyLong_Check(elem)) {
6468 PyErr_SetString(PyExc_TypeError,
6469 "groups must be integers");
6470 Py_DECREF(elem);
6471 return NULL;
6472 } else {
6473 unsigned long x = PyLong_AsUnsignedLong(elem);
6474 if (PyErr_Occurred()) {
6475 PyErr_SetString(PyExc_TypeError,
6476 "group id too big");
6477 Py_DECREF(elem);
6478 return NULL;
6479 }
6480 grouplist[i] = x;
6481 /* read back the value to see if it fitted in gid_t */
6482 if (grouplist[i] != x) {
6483 PyErr_SetString(PyExc_TypeError,
6484 "group id too big");
6485 Py_DECREF(elem);
6486 return NULL;
6487 }
6488 }
6489 Py_DECREF(elem);
6490 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006491
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 if (setgroups(len, grouplist) < 0)
6493 return posix_error();
6494 Py_INCREF(Py_None);
6495 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006496}
6497#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006498
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006499#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6500static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006501wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006502{
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 PyObject *result;
6504 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006505 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006506
Victor Stinner8c62be82010-05-06 00:08:46 +00006507 if (pid == -1)
6508 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006509
Victor Stinner8c62be82010-05-06 00:08:46 +00006510 if (struct_rusage == NULL) {
6511 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6512 if (m == NULL)
6513 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006514 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006515 Py_DECREF(m);
6516 if (struct_rusage == NULL)
6517 return NULL;
6518 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006519
Victor Stinner8c62be82010-05-06 00:08:46 +00006520 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6521 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6522 if (!result)
6523 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006524
6525#ifndef doubletime
6526#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6527#endif
6528
Victor Stinner8c62be82010-05-06 00:08:46 +00006529 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006530 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006531 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006532 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006533#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006534 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6535 SET_INT(result, 2, ru->ru_maxrss);
6536 SET_INT(result, 3, ru->ru_ixrss);
6537 SET_INT(result, 4, ru->ru_idrss);
6538 SET_INT(result, 5, ru->ru_isrss);
6539 SET_INT(result, 6, ru->ru_minflt);
6540 SET_INT(result, 7, ru->ru_majflt);
6541 SET_INT(result, 8, ru->ru_nswap);
6542 SET_INT(result, 9, ru->ru_inblock);
6543 SET_INT(result, 10, ru->ru_oublock);
6544 SET_INT(result, 11, ru->ru_msgsnd);
6545 SET_INT(result, 12, ru->ru_msgrcv);
6546 SET_INT(result, 13, ru->ru_nsignals);
6547 SET_INT(result, 14, ru->ru_nvcsw);
6548 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006549#undef SET_INT
6550
Victor Stinner8c62be82010-05-06 00:08:46 +00006551 if (PyErr_Occurred()) {
6552 Py_DECREF(result);
6553 return NULL;
6554 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006555
Victor Stinner8c62be82010-05-06 00:08:46 +00006556 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006557}
6558#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6559
6560#ifdef HAVE_WAIT3
6561PyDoc_STRVAR(posix_wait3__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006562"wait3(options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006563Wait for completion of a child process.");
6564
6565static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006566posix_wait3(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006567{
Victor Stinner8c62be82010-05-06 00:08:46 +00006568 pid_t pid;
6569 int options;
6570 struct rusage ru;
6571 WAIT_TYPE status;
6572 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006573
Victor Stinner4195b5c2012-02-08 23:03:19 +01006574 if (!PyArg_ParseTuple(args, "i:wait3", &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006575 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006576
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 Py_BEGIN_ALLOW_THREADS
6578 pid = wait3(&status, options, &ru);
6579 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006580
Victor Stinner4195b5c2012-02-08 23:03:19 +01006581 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006582}
6583#endif /* HAVE_WAIT3 */
6584
6585#ifdef HAVE_WAIT4
6586PyDoc_STRVAR(posix_wait4__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006587"wait4(pid, options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006588Wait for completion of a given child process.");
6589
6590static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006591posix_wait4(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006592{
Victor Stinner8c62be82010-05-06 00:08:46 +00006593 pid_t pid;
6594 int options;
6595 struct rusage ru;
6596 WAIT_TYPE status;
6597 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006598
Victor Stinner4195b5c2012-02-08 23:03:19 +01006599 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006600 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006601
Victor Stinner8c62be82010-05-06 00:08:46 +00006602 Py_BEGIN_ALLOW_THREADS
6603 pid = wait4(pid, &status, options, &ru);
6604 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006605
Victor Stinner4195b5c2012-02-08 23:03:19 +01006606 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006607}
6608#endif /* HAVE_WAIT4 */
6609
Ross Lagerwall7807c352011-03-17 20:20:30 +02006610#if defined(HAVE_WAITID) && !defined(__APPLE__)
6611PyDoc_STRVAR(posix_waitid__doc__,
6612"waitid(idtype, id, options) -> waitid_result\n\n\
6613Wait for the completion of one or more child processes.\n\n\
6614idtype can be P_PID, P_PGID or P_ALL.\n\
6615id specifies the pid to wait on.\n\
6616options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6617or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6618Returns either waitid_result or None if WNOHANG is specified and there are\n\
6619no children in a waitable state.");
6620
6621static PyObject *
6622posix_waitid(PyObject *self, PyObject *args)
6623{
6624 PyObject *result;
6625 idtype_t idtype;
6626 id_t id;
6627 int options, res;
6628 siginfo_t si;
6629 si.si_pid = 0;
6630 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6631 return NULL;
6632 Py_BEGIN_ALLOW_THREADS
6633 res = waitid(idtype, id, &si, options);
6634 Py_END_ALLOW_THREADS
6635 if (res == -1)
6636 return posix_error();
6637
6638 if (si.si_pid == 0)
6639 Py_RETURN_NONE;
6640
6641 result = PyStructSequence_New(&WaitidResultType);
6642 if (!result)
6643 return NULL;
6644
6645 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6646 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6647 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6648 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6649 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6650 if (PyErr_Occurred()) {
6651 Py_DECREF(result);
6652 return NULL;
6653 }
6654
6655 return result;
6656}
6657#endif
6658
Guido van Rossumb6775db1994-08-01 11:34:53 +00006659#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006660PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006661"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006662Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006663
Barry Warsaw53699e91996-12-10 23:23:01 +00006664static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006665posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006666{
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 pid_t pid;
6668 int options;
6669 WAIT_TYPE status;
6670 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006671
Victor Stinner8c62be82010-05-06 00:08:46 +00006672 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6673 return NULL;
6674 Py_BEGIN_ALLOW_THREADS
6675 pid = waitpid(pid, &status, options);
6676 Py_END_ALLOW_THREADS
6677 if (pid == -1)
6678 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006679
Victor Stinner8c62be82010-05-06 00:08:46 +00006680 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006681}
6682
Tim Petersab034fa2002-02-01 11:27:43 +00006683#elif defined(HAVE_CWAIT)
6684
6685/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006686PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006687"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006688"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006689
6690static PyObject *
6691posix_waitpid(PyObject *self, PyObject *args)
6692{
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 Py_intptr_t pid;
6694 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006695
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6697 return NULL;
6698 Py_BEGIN_ALLOW_THREADS
6699 pid = _cwait(&status, pid, options);
6700 Py_END_ALLOW_THREADS
6701 if (pid == -1)
6702 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006703
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 /* shift the status left a byte so this is more like the POSIX waitpid */
6705 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006706}
6707#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006708
Guido van Rossumad0ee831995-03-01 10:34:45 +00006709#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006710PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006711"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006712Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006713
Barry Warsaw53699e91996-12-10 23:23:01 +00006714static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006715posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006716{
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 pid_t pid;
6718 WAIT_TYPE status;
6719 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006720
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 Py_BEGIN_ALLOW_THREADS
6722 pid = wait(&status);
6723 Py_END_ALLOW_THREADS
6724 if (pid == -1)
6725 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006726
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006728}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006729#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006730
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006732PyDoc_STRVAR(posix_lstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006733"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006734Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006735
Barry Warsaw53699e91996-12-10 23:23:01 +00006736static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006737posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006738{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006739#ifdef HAVE_LSTAT
Victor Stinner4195b5c2012-02-08 23:03:19 +01006740 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006741#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006742#ifdef MS_WINDOWS
Victor Stinner4195b5c2012-02-08 23:03:19 +01006743 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006744 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006745#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01006746 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006747#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006748#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006749}
6750
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006751
Guido van Rossumb6775db1994-08-01 11:34:53 +00006752#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006753PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006754"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006755Return a string representing the path to which the symbolic link points.");
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_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006759{
Victor Stinner8c62be82010-05-06 00:08:46 +00006760 PyObject* v;
6761 char buf[MAXPATHLEN];
6762 PyObject *opath;
6763 char *path;
6764 int n;
6765 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006766
Victor Stinner8c62be82010-05-06 00:08:46 +00006767 if (!PyArg_ParseTuple(args, "O&:readlink",
6768 PyUnicode_FSConverter, &opath))
6769 return NULL;
6770 path = PyBytes_AsString(opath);
6771 v = PySequence_GetItem(args, 0);
6772 if (v == NULL) {
6773 Py_DECREF(opath);
6774 return NULL;
6775 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006776
Victor Stinner8c62be82010-05-06 00:08:46 +00006777 if (PyUnicode_Check(v)) {
6778 arg_is_unicode = 1;
6779 }
6780 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006781
Victor Stinner8c62be82010-05-06 00:08:46 +00006782 Py_BEGIN_ALLOW_THREADS
6783 n = readlink(path, buf, (int) sizeof buf);
6784 Py_END_ALLOW_THREADS
6785 if (n < 0)
6786 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006787
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006789 if (arg_is_unicode)
6790 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6791 else
6792 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006793}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006794#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006796
Brian Curtin52173d42010-12-02 18:29:18 +00006797#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006798PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006799"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006800Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006801
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006802static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006803posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006804{
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006806}
6807#endif /* HAVE_SYMLINK */
6808
Brian Curtind40e6f72010-07-08 21:39:08 +00006809#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6810
6811PyDoc_STRVAR(win_readlink__doc__,
6812"readlink(path) -> path\n\n\
6813Return a string representing the path to which the symbolic link points.");
6814
Brian Curtind40e6f72010-07-08 21:39:08 +00006815/* Windows readlink implementation */
6816static PyObject *
6817win_readlink(PyObject *self, PyObject *args)
6818{
6819 wchar_t *path;
6820 DWORD n_bytes_returned;
6821 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006822 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00006823 HANDLE reparse_point_handle;
6824
6825 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6826 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6827 wchar_t *print_name;
6828
6829 if (!PyArg_ParseTuple(args,
Victor Stinnereb5657a2011-09-30 01:44:27 +02006830 "U:readlink",
6831 &po))
6832 return NULL;
6833 path = PyUnicode_AsUnicode(po);
6834 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006835 return NULL;
6836
6837 /* First get a handle to the reparse point */
6838 Py_BEGIN_ALLOW_THREADS
6839 reparse_point_handle = CreateFileW(
6840 path,
6841 0,
6842 0,
6843 0,
6844 OPEN_EXISTING,
6845 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6846 0);
6847 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006848
Brian Curtind40e6f72010-07-08 21:39:08 +00006849 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006850 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006851
Brian Curtind40e6f72010-07-08 21:39:08 +00006852 Py_BEGIN_ALLOW_THREADS
6853 /* New call DeviceIoControl to read the reparse point */
6854 io_result = DeviceIoControl(
6855 reparse_point_handle,
6856 FSCTL_GET_REPARSE_POINT,
6857 0, 0, /* in buffer */
6858 target_buffer, sizeof(target_buffer),
6859 &n_bytes_returned,
6860 0 /* we're not using OVERLAPPED_IO */
6861 );
6862 CloseHandle(reparse_point_handle);
6863 Py_END_ALLOW_THREADS
6864
6865 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006866 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00006867
6868 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6869 {
6870 PyErr_SetString(PyExc_ValueError,
6871 "not a symbolic link");
6872 return NULL;
6873 }
Brian Curtin74e45612010-07-09 15:58:59 +00006874 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6875 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6876
6877 result = PyUnicode_FromWideChar(print_name,
6878 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006879 return result;
6880}
6881
6882#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6883
Brian Curtin52173d42010-12-02 18:29:18 +00006884#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006885
6886/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6887static int has_CreateSymbolicLinkW = 0;
6888static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6889static int
6890check_CreateSymbolicLinkW()
6891{
6892 HINSTANCE hKernel32;
6893 /* only recheck */
6894 if (has_CreateSymbolicLinkW)
6895 return has_CreateSymbolicLinkW;
Martin v. Löwis50590f12012-01-14 17:54:09 +01006896 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006897 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6898 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006899 if (Py_CreateSymbolicLinkW)
6900 has_CreateSymbolicLinkW = 1;
6901 return has_CreateSymbolicLinkW;
6902}
6903
6904PyDoc_STRVAR(win_symlink__doc__,
6905"symlink(src, dst, target_is_directory=False)\n\n\
6906Create a symbolic link pointing to src named dst.\n\
6907target_is_directory is required if the target is to be interpreted as\n\
6908a directory.\n\
6909This function requires Windows 6.0 or greater, and raises a\n\
6910NotImplementedError otherwise.");
6911
6912static PyObject *
6913win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6914{
6915 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006916 PyObject *osrc, *odest;
6917 PyObject *usrc = NULL, *udest = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006918 wchar_t *wsrc, *wdest;
Brian Curtind40e6f72010-07-08 21:39:08 +00006919 int target_is_directory = 0;
6920 DWORD res;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006921
Brian Curtind40e6f72010-07-08 21:39:08 +00006922 if (!check_CreateSymbolicLinkW())
6923 {
6924 /* raise NotImplementedError */
6925 return PyErr_Format(PyExc_NotImplementedError,
6926 "CreateSymbolicLinkW not found");
6927 }
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006928 if (!PyArg_ParseTupleAndKeywords(
6929 args, kwargs, "OO|i:symlink", kwlist,
6930 &osrc, &odest, &target_is_directory))
Brian Curtind40e6f72010-07-08 21:39:08 +00006931 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006932
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006933 usrc = win32_decode_filename(osrc);
6934 if (!usrc)
6935 return NULL;
6936 udest = win32_decode_filename(odest);
6937 if (!udest)
6938 goto error;
6939
Brian Curtin3b4499c2010-12-28 14:31:47 +00006940 if (win32_can_symlink == 0)
6941 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6942
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006943 wsrc = PyUnicode_AsUnicode(usrc);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006944 if (wsrc == NULL)
6945 goto error;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006946 wdest = PyUnicode_AsUnicode(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006947 if (wsrc == NULL)
6948 goto error;
6949
Brian Curtind40e6f72010-07-08 21:39:08 +00006950 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006951 res = Py_CreateSymbolicLinkW(wdest, wsrc, target_is_directory);
Brian Curtind40e6f72010-07-08 21:39:08 +00006952 Py_END_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02006953
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006954 Py_DECREF(usrc);
6955 Py_DECREF(udest);
Brian Curtind40e6f72010-07-08 21:39:08 +00006956 if (!res)
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006957 return win32_error_object("symlink", osrc);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006958
Brian Curtind40e6f72010-07-08 21:39:08 +00006959 Py_INCREF(Py_None);
6960 return Py_None;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006961
6962error:
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006963 Py_XDECREF(usrc);
6964 Py_XDECREF(udest);
Victor Stinnereb5657a2011-09-30 01:44:27 +02006965 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00006966}
Brian Curtin52173d42010-12-02 18:29:18 +00006967#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006968
6969#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006970#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6971static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006972system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006973{
6974 ULONG value = 0;
6975
6976 Py_BEGIN_ALLOW_THREADS
6977 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6978 Py_END_ALLOW_THREADS
6979
6980 return value;
6981}
6982
6983static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006984posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006985{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006986 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 return Py_BuildValue("ddddd",
6988 (double)0 /* t.tms_utime / HZ */,
6989 (double)0 /* t.tms_stime / HZ */,
6990 (double)0 /* t.tms_cutime / HZ */,
6991 (double)0 /* t.tms_cstime / HZ */,
6992 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006993}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006994#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006995#define NEED_TICKS_PER_SECOND
6996static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006997static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006998posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006999{
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 struct tms t;
7001 clock_t c;
7002 errno = 0;
7003 c = times(&t);
7004 if (c == (clock_t) -1)
7005 return posix_error();
7006 return Py_BuildValue("ddddd",
7007 (double)t.tms_utime / ticks_per_second,
7008 (double)t.tms_stime / ticks_per_second,
7009 (double)t.tms_cutime / ticks_per_second,
7010 (double)t.tms_cstime / ticks_per_second,
7011 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00007012}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007013#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007014#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007015
7016
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007017#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007018#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00007019static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007020posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007021{
Victor Stinner8c62be82010-05-06 00:08:46 +00007022 FILETIME create, exit, kernel, user;
7023 HANDLE hProc;
7024 hProc = GetCurrentProcess();
7025 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7026 /* The fields of a FILETIME structure are the hi and lo part
7027 of a 64-bit value expressed in 100 nanosecond units.
7028 1e7 is one second in such units; 1e-7 the inverse.
7029 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7030 */
7031 return Py_BuildValue(
7032 "ddddd",
7033 (double)(user.dwHighDateTime*429.4967296 +
7034 user.dwLowDateTime*1e-7),
7035 (double)(kernel.dwHighDateTime*429.4967296 +
7036 kernel.dwLowDateTime*1e-7),
7037 (double)0,
7038 (double)0,
7039 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007040}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007041#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007042
7043#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007044PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007045"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007046Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007047#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007049
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007050#ifdef HAVE_GETSID
7051PyDoc_STRVAR(posix_getsid__doc__,
7052"getsid(pid) -> sid\n\n\
7053Call the system call getsid().");
7054
7055static PyObject *
7056posix_getsid(PyObject *self, PyObject *args)
7057{
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 pid_t pid;
7059 int sid;
7060 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
7061 return NULL;
7062 sid = getsid(pid);
7063 if (sid < 0)
7064 return posix_error();
7065 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007066}
7067#endif /* HAVE_GETSID */
7068
7069
Guido van Rossumb6775db1994-08-01 11:34:53 +00007070#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007071PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007072"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007073Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007074
Barry Warsaw53699e91996-12-10 23:23:01 +00007075static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007076posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007077{
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 if (setsid() < 0)
7079 return posix_error();
7080 Py_INCREF(Py_None);
7081 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007082}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007083#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007084
Guido van Rossumb6775db1994-08-01 11:34:53 +00007085#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007086PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007087"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007088Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007089
Barry Warsaw53699e91996-12-10 23:23:01 +00007090static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007091posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007092{
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 pid_t pid;
7094 int pgrp;
7095 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
7096 return NULL;
7097 if (setpgid(pid, pgrp) < 0)
7098 return posix_error();
7099 Py_INCREF(Py_None);
7100 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007101}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007102#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007103
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007104
Guido van Rossumb6775db1994-08-01 11:34:53 +00007105#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007106PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007107"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007108Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007109
Barry Warsaw53699e91996-12-10 23:23:01 +00007110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007111posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007112{
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 int fd;
7114 pid_t pgid;
7115 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
7116 return NULL;
7117 pgid = tcgetpgrp(fd);
7118 if (pgid < 0)
7119 return posix_error();
7120 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007121}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007122#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007123
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007124
Guido van Rossumb6775db1994-08-01 11:34:53 +00007125#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007126PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007127"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007128Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007129
Barry Warsaw53699e91996-12-10 23:23:01 +00007130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007131posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007132{
Victor Stinner8c62be82010-05-06 00:08:46 +00007133 int fd;
7134 pid_t pgid;
7135 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
7136 return NULL;
7137 if (tcsetpgrp(fd, pgid) < 0)
7138 return posix_error();
7139 Py_INCREF(Py_None);
7140 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007141}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007142#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007143
Guido van Rossum687dd131993-05-17 08:34:16 +00007144/* Functions acting on file descriptors */
7145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007146PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007147"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007148Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007149
Barry Warsaw53699e91996-12-10 23:23:01 +00007150static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007151posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007152{
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 PyObject *ofile;
7154 char *file;
7155 int flag;
7156 int mode = 0777;
7157 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007158
7159#ifdef MS_WINDOWS
Victor Stinnereb5657a2011-09-30 01:44:27 +02007160 PyObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02007161 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02007162 wchar_t *wpath = PyUnicode_AsUnicode(po);
7163 if (wpath == NULL)
7164 return NULL;
7165
Victor Stinner8c62be82010-05-06 00:08:46 +00007166 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02007167 fd = _wopen(wpath, flag, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 Py_END_ALLOW_THREADS
7169 if (fd < 0)
7170 return posix_error();
7171 return PyLong_FromLong((long)fd);
7172 }
7173 /* Drop the argument parsing error as narrow strings
7174 are also valid. */
7175 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007176#endif
7177
Victor Stinner26de69d2011-06-17 15:15:38 +02007178 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 PyUnicode_FSConverter, &ofile,
7180 &flag, &mode))
7181 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01007182#ifdef MS_WINDOWS
7183 if (win32_warn_bytes_api()) {
7184 Py_DECREF(ofile);
7185 return NULL;
7186 }
7187#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007188 file = PyBytes_AsString(ofile);
7189 Py_BEGIN_ALLOW_THREADS
7190 fd = open(file, flag, mode);
7191 Py_END_ALLOW_THREADS
7192 if (fd < 0)
7193 return posix_error_with_allocated_filename(ofile);
7194 Py_DECREF(ofile);
7195 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007196}
7197
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007199PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007200"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007201Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007202
Barry Warsaw53699e91996-12-10 23:23:01 +00007203static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007204posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007205{
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 int fd, res;
7207 if (!PyArg_ParseTuple(args, "i:close", &fd))
7208 return NULL;
7209 if (!_PyVerify_fd(fd))
7210 return posix_error();
7211 Py_BEGIN_ALLOW_THREADS
7212 res = close(fd);
7213 Py_END_ALLOW_THREADS
7214 if (res < 0)
7215 return posix_error();
7216 Py_INCREF(Py_None);
7217 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007218}
7219
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007220
Victor Stinner8c62be82010-05-06 00:08:46 +00007221PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00007222"closerange(fd_low, fd_high)\n\n\
7223Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
7224
7225static PyObject *
7226posix_closerange(PyObject *self, PyObject *args)
7227{
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 int fd_from, fd_to, i;
7229 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
7230 return NULL;
7231 Py_BEGIN_ALLOW_THREADS
7232 for (i = fd_from; i < fd_to; i++)
7233 if (_PyVerify_fd(i))
7234 close(i);
7235 Py_END_ALLOW_THREADS
7236 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007237}
7238
7239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007240PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007241"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007242Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007243
Barry Warsaw53699e91996-12-10 23:23:01 +00007244static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007245posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007246{
Victor Stinner8c62be82010-05-06 00:08:46 +00007247 int fd;
7248 if (!PyArg_ParseTuple(args, "i:dup", &fd))
7249 return NULL;
7250 if (!_PyVerify_fd(fd))
7251 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 if (fd < 0)
7254 return posix_error();
7255 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007256}
7257
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007258
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007259PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00007260"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007261Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007262
Barry Warsaw53699e91996-12-10 23:23:01 +00007263static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007264posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007265{
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 int fd, fd2, res;
7267 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
7268 return NULL;
7269 if (!_PyVerify_fd_dup2(fd, fd2))
7270 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00007272 if (res < 0)
7273 return posix_error();
7274 Py_INCREF(Py_None);
7275 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007276}
7277
Ross Lagerwall7807c352011-03-17 20:20:30 +02007278#ifdef HAVE_LOCKF
7279PyDoc_STRVAR(posix_lockf__doc__,
7280"lockf(fd, cmd, len)\n\n\
7281Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
7282fd is an open file descriptor.\n\
7283cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
7284F_TEST.\n\
7285len specifies the section of the file to lock.");
7286
7287static PyObject *
7288posix_lockf(PyObject *self, PyObject *args)
7289{
7290 int fd, cmd, res;
7291 off_t len;
7292 if (!PyArg_ParseTuple(args, "iiO&:lockf",
7293 &fd, &cmd, _parse_off_t, &len))
7294 return NULL;
7295
7296 Py_BEGIN_ALLOW_THREADS
7297 res = lockf(fd, cmd, len);
7298 Py_END_ALLOW_THREADS
7299
7300 if (res < 0)
7301 return posix_error();
7302
7303 Py_RETURN_NONE;
7304}
7305#endif
7306
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007308PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007309"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01007310Set the current position of a file descriptor.\n\
7311Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007312
Barry Warsaw53699e91996-12-10 23:23:01 +00007313static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007314posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007315{
Victor Stinner8c62be82010-05-06 00:08:46 +00007316 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007317#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007319#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007321#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02007322 PyObject *posobj;
7323 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00007325#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7327 switch (how) {
7328 case 0: how = SEEK_SET; break;
7329 case 1: how = SEEK_CUR; break;
7330 case 2: how = SEEK_END; break;
7331 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007332#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007333
Ross Lagerwall8e749672011-03-17 21:54:07 +02007334#if !defined(HAVE_LARGEFILE_SUPPORT)
7335 pos = PyLong_AsLong(posobj);
7336#else
7337 pos = PyLong_AsLongLong(posobj);
7338#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007339 if (PyErr_Occurred())
7340 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007341
Victor Stinner8c62be82010-05-06 00:08:46 +00007342 if (!_PyVerify_fd(fd))
7343 return posix_error();
7344 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007345#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007346 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007347#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007348 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007349#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 Py_END_ALLOW_THREADS
7351 if (res < 0)
7352 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00007353
7354#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007356#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007358#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007359}
7360
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007361
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007362PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007363"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007364Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007365
Barry Warsaw53699e91996-12-10 23:23:01 +00007366static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007367posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007368{
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 int fd, size;
7370 Py_ssize_t n;
7371 PyObject *buffer;
7372 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7373 return NULL;
7374 if (size < 0) {
7375 errno = EINVAL;
7376 return posix_error();
7377 }
7378 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7379 if (buffer == NULL)
7380 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007381 if (!_PyVerify_fd(fd)) {
7382 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007384 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 Py_BEGIN_ALLOW_THREADS
7386 n = read(fd, PyBytes_AS_STRING(buffer), size);
7387 Py_END_ALLOW_THREADS
7388 if (n < 0) {
7389 Py_DECREF(buffer);
7390 return posix_error();
7391 }
7392 if (n != size)
7393 _PyBytes_Resize(&buffer, n);
7394 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007395}
7396
Ross Lagerwall7807c352011-03-17 20:20:30 +02007397#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7398 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007399static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007400iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7401{
7402 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007403 Py_ssize_t blen, total = 0;
7404
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007405 *iov = PyMem_New(struct iovec, cnt);
7406 if (*iov == NULL) {
7407 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007408 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007409 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007410
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007411 *buf = PyMem_New(Py_buffer, cnt);
7412 if (*buf == NULL) {
7413 PyMem_Del(*iov);
7414 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007415 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007416 }
7417
7418 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007419 PyObject *item = PySequence_GetItem(seq, i);
7420 if (item == NULL)
7421 goto fail;
7422 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7423 Py_DECREF(item);
7424 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007425 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007426 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007427 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007428 blen = (*buf)[i].len;
7429 (*iov)[i].iov_len = blen;
7430 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007431 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007432 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007433
7434fail:
7435 PyMem_Del(*iov);
7436 for (j = 0; j < i; j++) {
7437 PyBuffer_Release(&(*buf)[j]);
7438 }
7439 PyMem_Del(*buf);
7440 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007441}
7442
7443static void
7444iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7445{
7446 int i;
7447 PyMem_Del(iov);
7448 for (i = 0; i < cnt; i++) {
7449 PyBuffer_Release(&buf[i]);
7450 }
7451 PyMem_Del(buf);
7452}
7453#endif
7454
Ross Lagerwall7807c352011-03-17 20:20:30 +02007455#ifdef HAVE_READV
7456PyDoc_STRVAR(posix_readv__doc__,
7457"readv(fd, buffers) -> bytesread\n\n\
7458Read from a file descriptor into a number of writable buffers. buffers\n\
7459is an arbitrary sequence of writable buffers.\n\
7460Returns the total number of bytes read.");
7461
7462static PyObject *
7463posix_readv(PyObject *self, PyObject *args)
7464{
7465 int fd, cnt;
7466 Py_ssize_t n;
7467 PyObject *seq;
7468 struct iovec *iov;
7469 Py_buffer *buf;
7470
7471 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7472 return NULL;
7473 if (!PySequence_Check(seq)) {
7474 PyErr_SetString(PyExc_TypeError,
7475 "readv() arg 2 must be a sequence");
7476 return NULL;
7477 }
7478 cnt = PySequence_Size(seq);
7479
7480 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7481 return NULL;
7482
7483 Py_BEGIN_ALLOW_THREADS
7484 n = readv(fd, iov, cnt);
7485 Py_END_ALLOW_THREADS
7486
7487 iov_cleanup(iov, buf, cnt);
7488 return PyLong_FromSsize_t(n);
7489}
7490#endif
7491
7492#ifdef HAVE_PREAD
7493PyDoc_STRVAR(posix_pread__doc__,
7494"pread(fd, buffersize, offset) -> string\n\n\
7495Read from a file descriptor, fd, at a position of offset. It will read up\n\
7496to buffersize number of bytes. The file offset remains unchanged.");
7497
7498static PyObject *
7499posix_pread(PyObject *self, PyObject *args)
7500{
7501 int fd, size;
7502 off_t offset;
7503 Py_ssize_t n;
7504 PyObject *buffer;
7505 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7506 return NULL;
7507
7508 if (size < 0) {
7509 errno = EINVAL;
7510 return posix_error();
7511 }
7512 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7513 if (buffer == NULL)
7514 return NULL;
7515 if (!_PyVerify_fd(fd)) {
7516 Py_DECREF(buffer);
7517 return posix_error();
7518 }
7519 Py_BEGIN_ALLOW_THREADS
7520 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7521 Py_END_ALLOW_THREADS
7522 if (n < 0) {
7523 Py_DECREF(buffer);
7524 return posix_error();
7525 }
7526 if (n != size)
7527 _PyBytes_Resize(&buffer, n);
7528 return buffer;
7529}
7530#endif
7531
7532PyDoc_STRVAR(posix_write__doc__,
7533"write(fd, string) -> byteswritten\n\n\
7534Write a string to a file descriptor.");
7535
7536static PyObject *
7537posix_write(PyObject *self, PyObject *args)
7538{
7539 Py_buffer pbuf;
7540 int fd;
7541 Py_ssize_t size, len;
7542
7543 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7544 return NULL;
7545 if (!_PyVerify_fd(fd)) {
7546 PyBuffer_Release(&pbuf);
7547 return posix_error();
7548 }
7549 len = pbuf.len;
7550 Py_BEGIN_ALLOW_THREADS
7551#if defined(MS_WIN64) || defined(MS_WINDOWS)
7552 if (len > INT_MAX)
7553 len = INT_MAX;
7554 size = write(fd, pbuf.buf, (int)len);
7555#else
7556 size = write(fd, pbuf.buf, len);
7557#endif
7558 Py_END_ALLOW_THREADS
7559 PyBuffer_Release(&pbuf);
7560 if (size < 0)
7561 return posix_error();
7562 return PyLong_FromSsize_t(size);
7563}
7564
7565#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007566PyDoc_STRVAR(posix_sendfile__doc__,
7567"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7568sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7569 -> byteswritten\n\
7570Copy nbytes bytes from file descriptor in to file descriptor out.");
7571
7572static PyObject *
7573posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7574{
7575 int in, out;
7576 Py_ssize_t ret;
7577 off_t offset;
7578
7579#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7580#ifndef __APPLE__
7581 Py_ssize_t len;
7582#endif
7583 PyObject *headers = NULL, *trailers = NULL;
7584 Py_buffer *hbuf, *tbuf;
7585 off_t sbytes;
7586 struct sf_hdtr sf;
7587 int flags = 0;
7588 sf.headers = NULL;
7589 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007590 static char *keywords[] = {"out", "in",
7591 "offset", "count",
7592 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007593
7594#ifdef __APPLE__
7595 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007596 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007597#else
7598 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007599 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007600#endif
7601 &headers, &trailers, &flags))
7602 return NULL;
7603 if (headers != NULL) {
7604 if (!PySequence_Check(headers)) {
7605 PyErr_SetString(PyExc_TypeError,
7606 "sendfile() headers must be a sequence or None");
7607 return NULL;
7608 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007609 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007610 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007611 if (sf.hdr_cnt > 0 &&
7612 !(i = iov_setup(&(sf.headers), &hbuf,
7613 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007614 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007615#ifdef __APPLE__
7616 sbytes += i;
7617#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007618 }
7619 }
7620 if (trailers != NULL) {
7621 if (!PySequence_Check(trailers)) {
7622 PyErr_SetString(PyExc_TypeError,
7623 "sendfile() trailers must be a sequence or None");
7624 return NULL;
7625 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007626 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007627 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007628 if (sf.trl_cnt > 0 &&
7629 !(i = iov_setup(&(sf.trailers), &tbuf,
7630 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007631 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007632#ifdef __APPLE__
7633 sbytes += i;
7634#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007635 }
7636 }
7637
7638 Py_BEGIN_ALLOW_THREADS
7639#ifdef __APPLE__
7640 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7641#else
7642 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7643#endif
7644 Py_END_ALLOW_THREADS
7645
7646 if (sf.headers != NULL)
7647 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7648 if (sf.trailers != NULL)
7649 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7650
7651 if (ret < 0) {
7652 if ((errno == EAGAIN) || (errno == EBUSY)) {
7653 if (sbytes != 0) {
7654 // some data has been sent
7655 goto done;
7656 }
7657 else {
7658 // no data has been sent; upper application is supposed
7659 // to retry on EAGAIN or EBUSY
7660 return posix_error();
7661 }
7662 }
7663 return posix_error();
7664 }
7665 goto done;
7666
7667done:
7668 #if !defined(HAVE_LARGEFILE_SUPPORT)
7669 return Py_BuildValue("l", sbytes);
7670 #else
7671 return Py_BuildValue("L", sbytes);
7672 #endif
7673
7674#else
7675 Py_ssize_t count;
7676 PyObject *offobj;
7677 static char *keywords[] = {"out", "in",
7678 "offset", "count", NULL};
7679 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7680 keywords, &out, &in, &offobj, &count))
7681 return NULL;
7682#ifdef linux
7683 if (offobj == Py_None) {
7684 Py_BEGIN_ALLOW_THREADS
7685 ret = sendfile(out, in, NULL, count);
7686 Py_END_ALLOW_THREADS
7687 if (ret < 0)
7688 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007689 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007690 }
7691#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007692 if (!_parse_off_t(offobj, &offset))
7693 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007694 Py_BEGIN_ALLOW_THREADS
7695 ret = sendfile(out, in, &offset, count);
7696 Py_END_ALLOW_THREADS
7697 if (ret < 0)
7698 return posix_error();
7699 return Py_BuildValue("n", ret);
7700#endif
7701}
7702#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007703
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007704PyDoc_STRVAR(posix_fstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007705"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007706Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007707
Barry Warsaw53699e91996-12-10 23:23:01 +00007708static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007709posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007710{
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 int fd;
7712 STRUCT_STAT st;
7713 int res;
Victor Stinner4195b5c2012-02-08 23:03:19 +01007714 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007716#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 /* on OpenVMS we must ensure that all bytes are written to the file */
7718 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007719#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007720 if (!_PyVerify_fd(fd))
7721 return posix_error();
7722 Py_BEGIN_ALLOW_THREADS
7723 res = FSTAT(fd, &st);
7724 Py_END_ALLOW_THREADS
7725 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007726#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007728#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007730#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 }
Tim Peters5aa91602002-01-30 05:46:57 +00007732
Victor Stinner4195b5c2012-02-08 23:03:19 +01007733 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007734}
7735
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007736PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007737"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007738Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007739connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007740
7741static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007742posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007743{
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 int fd;
7745 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7746 return NULL;
7747 if (!_PyVerify_fd(fd))
7748 return PyBool_FromLong(0);
7749 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007750}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007751
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007752#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007753PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007754"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007755Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007756
Barry Warsaw53699e91996-12-10 23:23:01 +00007757static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007758posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007759{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007760#if defined(PYOS_OS2)
7761 HFILE read, write;
7762 APIRET rc;
7763
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007764 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007765 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007766 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007767
7768 return Py_BuildValue("(ii)", read, write);
7769#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007770#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 int fds[2];
7772 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 if (res != 0)
7775 return posix_error();
7776 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007777#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 HANDLE read, write;
7779 int read_fd, write_fd;
7780 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 if (!ok)
7783 return win32_error("CreatePipe", NULL);
7784 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7785 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7786 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007787#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007788#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007789}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007790#endif /* HAVE_PIPE */
7791
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007792#ifdef HAVE_PIPE2
7793PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007794"pipe2(flags) -> (read_end, write_end)\n\n\
7795Create a pipe with flags set atomically.\n\
7796flags can be constructed by ORing together one or more of these values:\n\
7797O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007798");
7799
7800static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007801posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007802{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007803 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007804 int fds[2];
7805 int res;
7806
Charles-François Natali368f34b2011-06-06 19:49:47 +02007807 flags = PyLong_AsLong(arg);
7808 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007809 return NULL;
7810
7811 res = pipe2(fds, flags);
7812 if (res != 0)
7813 return posix_error();
7814 return Py_BuildValue("(ii)", fds[0], fds[1]);
7815}
7816#endif /* HAVE_PIPE2 */
7817
Ross Lagerwall7807c352011-03-17 20:20:30 +02007818#ifdef HAVE_WRITEV
7819PyDoc_STRVAR(posix_writev__doc__,
7820"writev(fd, buffers) -> byteswritten\n\n\
7821Write the contents of buffers to a file descriptor, where buffers is an\n\
7822arbitrary sequence of buffers.\n\
7823Returns the total bytes written.");
7824
7825static PyObject *
7826posix_writev(PyObject *self, PyObject *args)
7827{
7828 int fd, cnt;
7829 Py_ssize_t res;
7830 PyObject *seq;
7831 struct iovec *iov;
7832 Py_buffer *buf;
7833 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7834 return NULL;
7835 if (!PySequence_Check(seq)) {
7836 PyErr_SetString(PyExc_TypeError,
7837 "writev() arg 2 must be a sequence");
7838 return NULL;
7839 }
7840 cnt = PySequence_Size(seq);
7841
7842 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7843 return NULL;
7844 }
7845
7846 Py_BEGIN_ALLOW_THREADS
7847 res = writev(fd, iov, cnt);
7848 Py_END_ALLOW_THREADS
7849
7850 iov_cleanup(iov, buf, cnt);
7851 return PyLong_FromSsize_t(res);
7852}
7853#endif
7854
7855#ifdef HAVE_PWRITE
7856PyDoc_STRVAR(posix_pwrite__doc__,
7857"pwrite(fd, string, offset) -> byteswritten\n\n\
7858Write string to a file descriptor, fd, from offset, leaving the file\n\
7859offset unchanged.");
7860
7861static PyObject *
7862posix_pwrite(PyObject *self, PyObject *args)
7863{
7864 Py_buffer pbuf;
7865 int fd;
7866 off_t offset;
7867 Py_ssize_t size;
7868
7869 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7870 return NULL;
7871
7872 if (!_PyVerify_fd(fd)) {
7873 PyBuffer_Release(&pbuf);
7874 return posix_error();
7875 }
7876 Py_BEGIN_ALLOW_THREADS
7877 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7878 Py_END_ALLOW_THREADS
7879 PyBuffer_Release(&pbuf);
7880 if (size < 0)
7881 return posix_error();
7882 return PyLong_FromSsize_t(size);
7883}
7884#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007885
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007886#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007887PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007888"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007889Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007890
Barry Warsaw53699e91996-12-10 23:23:01 +00007891static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007892posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007893{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007894 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 char *filename;
7896 int mode = 0666;
7897 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007898 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7899 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007900 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007901 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 Py_BEGIN_ALLOW_THREADS
7903 res = mkfifo(filename, mode);
7904 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007905 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 if (res < 0)
7907 return posix_error();
7908 Py_INCREF(Py_None);
7909 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007910}
7911#endif
7912
7913
Neal Norwitz11690112002-07-30 01:08:28 +00007914#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007915PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007916"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007917Create a filesystem node (file, device special file or named pipe)\n\
7918named filename. mode specifies both the permissions to use and the\n\
7919type of node to be created, being combined (bitwise OR) with one of\n\
7920S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007921device defines the newly created device special file (probably using\n\
7922os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007923
7924
7925static PyObject *
7926posix_mknod(PyObject *self, PyObject *args)
7927{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007928 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007929 char *filename;
7930 int mode = 0600;
7931 int device = 0;
7932 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007933 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7934 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007936 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 Py_BEGIN_ALLOW_THREADS
7938 res = mknod(filename, mode, device);
7939 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007940 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 if (res < 0)
7942 return posix_error();
7943 Py_INCREF(Py_None);
7944 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007945}
7946#endif
7947
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007948#ifdef HAVE_DEVICE_MACROS
7949PyDoc_STRVAR(posix_major__doc__,
7950"major(device) -> major number\n\
7951Extracts a device major number from a raw device number.");
7952
7953static PyObject *
7954posix_major(PyObject *self, PyObject *args)
7955{
Victor Stinner8c62be82010-05-06 00:08:46 +00007956 int device;
7957 if (!PyArg_ParseTuple(args, "i:major", &device))
7958 return NULL;
7959 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007960}
7961
7962PyDoc_STRVAR(posix_minor__doc__,
7963"minor(device) -> minor number\n\
7964Extracts a device minor number from a raw device number.");
7965
7966static PyObject *
7967posix_minor(PyObject *self, PyObject *args)
7968{
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 int device;
7970 if (!PyArg_ParseTuple(args, "i:minor", &device))
7971 return NULL;
7972 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007973}
7974
7975PyDoc_STRVAR(posix_makedev__doc__,
7976"makedev(major, minor) -> device number\n\
7977Composes a raw device number from the major and minor device numbers.");
7978
7979static PyObject *
7980posix_makedev(PyObject *self, PyObject *args)
7981{
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 int major, minor;
7983 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7984 return NULL;
7985 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007986}
7987#endif /* device macros */
7988
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007989
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007990#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007991PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007992"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007993Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007994
Barry Warsaw53699e91996-12-10 23:23:01 +00007995static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007996posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007997{
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 int fd;
7999 off_t length;
8000 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008001
Ross Lagerwall7807c352011-03-17 20:20:30 +02008002 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008004
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 Py_BEGIN_ALLOW_THREADS
8006 res = ftruncate(fd, length);
8007 Py_END_ALLOW_THREADS
8008 if (res < 0)
8009 return posix_error();
8010 Py_INCREF(Py_None);
8011 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008012}
8013#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00008014
Ross Lagerwall7807c352011-03-17 20:20:30 +02008015#ifdef HAVE_TRUNCATE
8016PyDoc_STRVAR(posix_truncate__doc__,
8017"truncate(path, length)\n\n\
8018Truncate the file given by path to length bytes.");
8019
8020static PyObject *
8021posix_truncate(PyObject *self, PyObject *args)
8022{
8023 PyObject *opath;
8024 const char *path;
8025 off_t length;
8026 int res;
8027
8028 if (!PyArg_ParseTuple(args, "O&O&:truncate",
8029 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
8030 return NULL;
8031 path = PyBytes_AsString(opath);
8032
8033 Py_BEGIN_ALLOW_THREADS
8034 res = truncate(path, length);
8035 Py_END_ALLOW_THREADS
8036 Py_DECREF(opath);
8037 if (res < 0)
8038 return posix_error();
8039 Py_RETURN_NONE;
8040}
8041#endif
8042
8043#ifdef HAVE_POSIX_FALLOCATE
8044PyDoc_STRVAR(posix_posix_fallocate__doc__,
8045"posix_fallocate(fd, offset, len)\n\n\
8046Ensures that enough disk space is allocated for the file specified by fd\n\
8047starting from offset and continuing for len bytes.");
8048
8049static PyObject *
8050posix_posix_fallocate(PyObject *self, PyObject *args)
8051{
8052 off_t len, offset;
8053 int res, fd;
8054
8055 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
8056 &fd, _parse_off_t, &offset, _parse_off_t, &len))
8057 return NULL;
8058
8059 Py_BEGIN_ALLOW_THREADS
8060 res = posix_fallocate(fd, offset, len);
8061 Py_END_ALLOW_THREADS
8062 if (res != 0) {
8063 errno = res;
8064 return posix_error();
8065 }
8066 Py_RETURN_NONE;
8067}
8068#endif
8069
8070#ifdef HAVE_POSIX_FADVISE
8071PyDoc_STRVAR(posix_posix_fadvise__doc__,
8072"posix_fadvise(fd, offset, len, advice)\n\n\
8073Announces an intention to access data in a specific pattern thus allowing\n\
8074the kernel to make optimizations.\n\
8075The advice applies to the region of the file specified by fd starting at\n\
8076offset and continuing for len bytes.\n\
8077advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
8078POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
8079POSIX_FADV_DONTNEED.");
8080
8081static PyObject *
8082posix_posix_fadvise(PyObject *self, PyObject *args)
8083{
8084 off_t len, offset;
8085 int res, fd, advice;
8086
8087 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
8088 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
8089 return NULL;
8090
8091 Py_BEGIN_ALLOW_THREADS
8092 res = posix_fadvise(fd, offset, len, advice);
8093 Py_END_ALLOW_THREADS
8094 if (res != 0) {
8095 errno = res;
8096 return posix_error();
8097 }
8098 Py_RETURN_NONE;
8099}
8100#endif
8101
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008102#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008103PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008104"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008105Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008106
Fred Drake762e2061999-08-26 17:23:54 +00008107/* Save putenv() parameters as values here, so we can collect them when they
8108 * get re-set with another call for the same key. */
8109static PyObject *posix_putenv_garbage;
8110
Tim Peters5aa91602002-01-30 05:46:57 +00008111static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008112posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008113{
Victor Stinner84ae1182010-05-06 22:05:07 +00008114 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008115#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01008116 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008117 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01008118
Victor Stinner8c62be82010-05-06 00:08:46 +00008119 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008120 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01008121 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00008122 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008123
Victor Stinner65170952011-11-22 22:16:17 +01008124 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00008125 if (newstr == NULL) {
8126 PyErr_NoMemory();
8127 goto error;
8128 }
Victor Stinner65170952011-11-22 22:16:17 +01008129 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
8130 PyErr_Format(PyExc_ValueError,
8131 "the environment variable is longer than %u characters",
8132 _MAX_ENV);
8133 goto error;
8134 }
8135
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02008137 if (newenv == NULL)
8138 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008139 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008140 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008141 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008143#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008144 PyObject *os1, *os2;
8145 char *s1, *s2;
8146 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008147
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008148 if (!PyArg_ParseTuple(args,
8149 "O&O&:putenv",
8150 PyUnicode_FSConverter, &os1,
8151 PyUnicode_FSConverter, &os2))
8152 return NULL;
8153 s1 = PyBytes_AsString(os1);
8154 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00008155
Victor Stinner65170952011-11-22 22:16:17 +01008156 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008157 if (newstr == NULL) {
8158 PyErr_NoMemory();
8159 goto error;
8160 }
8161
Victor Stinner8c62be82010-05-06 00:08:46 +00008162 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00008163 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008164 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008165 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008166 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008167#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008168
Victor Stinner8c62be82010-05-06 00:08:46 +00008169 /* Install the first arg and newstr in posix_putenv_garbage;
8170 * this will cause previous value to be collected. This has to
8171 * happen after the real putenv() call because the old value
8172 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01008173 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008174 /* really not much we can do; just leak */
8175 PyErr_Clear();
8176 }
8177 else {
8178 Py_DECREF(newstr);
8179 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00008180
Martin v. Löwis011e8422009-05-05 04:43:17 +00008181#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008182 Py_DECREF(os1);
8183 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00008184#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008185 Py_RETURN_NONE;
8186
8187error:
8188#ifndef MS_WINDOWS
8189 Py_DECREF(os1);
8190 Py_DECREF(os2);
8191#endif
8192 Py_XDECREF(newstr);
8193 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00008194}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008195#endif /* putenv */
8196
Guido van Rossumc524d952001-10-19 01:31:59 +00008197#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008198PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008199"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008200Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00008201
8202static PyObject *
8203posix_unsetenv(PyObject *self, PyObject *args)
8204{
Victor Stinner65170952011-11-22 22:16:17 +01008205 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01008206#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01008207 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01008208#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008209
8210 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00008211
Victor Stinner65170952011-11-22 22:16:17 +01008212 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00008213 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00008214
Victor Stinner984890f2011-11-24 13:53:38 +01008215#ifdef HAVE_BROKEN_UNSETENV
8216 unsetenv(PyBytes_AS_STRING(name));
8217#else
Victor Stinner65170952011-11-22 22:16:17 +01008218 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008219 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06008220 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01008221 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008222 }
Victor Stinner984890f2011-11-24 13:53:38 +01008223#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008224
Victor Stinner8c62be82010-05-06 00:08:46 +00008225 /* Remove the key from posix_putenv_garbage;
8226 * this will cause it to be collected. This has to
8227 * happen after the real unsetenv() call because the
8228 * old value was still accessible until then.
8229 */
Victor Stinner65170952011-11-22 22:16:17 +01008230 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008231 /* really not much we can do; just leak */
8232 PyErr_Clear();
8233 }
Victor Stinner65170952011-11-22 22:16:17 +01008234 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00008235 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00008236}
8237#endif /* unsetenv */
8238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008239PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008240"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008241Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008242
Guido van Rossumf68d8e52001-04-14 17:55:09 +00008243static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008244posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00008245{
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 int code;
8247 char *message;
8248 if (!PyArg_ParseTuple(args, "i:strerror", &code))
8249 return NULL;
8250 message = strerror(code);
8251 if (message == NULL) {
8252 PyErr_SetString(PyExc_ValueError,
8253 "strerror() argument out of range");
8254 return NULL;
8255 }
Victor Stinner1b579672011-12-17 05:47:23 +01008256 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008257}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008258
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008259
Guido van Rossumc9641791998-08-04 15:26:23 +00008260#ifdef HAVE_SYS_WAIT_H
8261
Fred Drake106c1a02002-04-23 15:58:02 +00008262#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008263PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008264"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008265Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00008266
8267static PyObject *
8268posix_WCOREDUMP(PyObject *self, PyObject *args)
8269{
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 WAIT_TYPE status;
8271 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008272
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
8274 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008275
Victor Stinner8c62be82010-05-06 00:08:46 +00008276 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008277}
8278#endif /* WCOREDUMP */
8279
8280#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008281PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008282"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00008283Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008284job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00008285
8286static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008287posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00008288{
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 WAIT_TYPE status;
8290 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008291
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
8293 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008294
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008296}
8297#endif /* WIFCONTINUED */
8298
Guido van Rossumc9641791998-08-04 15:26:23 +00008299#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008300PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008301"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008302Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008303
8304static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008305posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008306{
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 WAIT_TYPE status;
8308 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008309
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
8311 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008312
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008314}
8315#endif /* WIFSTOPPED */
8316
8317#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008318PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008319"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008320Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008321
8322static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008323posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008324{
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 WAIT_TYPE status;
8326 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008327
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
8329 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008330
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008332}
8333#endif /* WIFSIGNALED */
8334
8335#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008336PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008337"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008338Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008339system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008340
8341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008342posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008343{
Victor Stinner8c62be82010-05-06 00:08:46 +00008344 WAIT_TYPE status;
8345 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008346
Victor Stinner8c62be82010-05-06 00:08:46 +00008347 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
8348 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008349
Victor Stinner8c62be82010-05-06 00:08:46 +00008350 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008351}
8352#endif /* WIFEXITED */
8353
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00008354#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008355PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008356"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008357Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008358
8359static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008360posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008361{
Victor Stinner8c62be82010-05-06 00:08:46 +00008362 WAIT_TYPE status;
8363 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008364
Victor Stinner8c62be82010-05-06 00:08:46 +00008365 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
8366 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008367
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008369}
8370#endif /* WEXITSTATUS */
8371
8372#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008373PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008374"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008375Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008376value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008377
8378static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008379posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008380{
Victor Stinner8c62be82010-05-06 00:08:46 +00008381 WAIT_TYPE status;
8382 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008383
Victor Stinner8c62be82010-05-06 00:08:46 +00008384 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8385 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008386
Victor Stinner8c62be82010-05-06 00:08:46 +00008387 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008388}
8389#endif /* WTERMSIG */
8390
8391#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008392PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008393"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008394Return the signal that stopped the process that provided\n\
8395the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008396
8397static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008398posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008399{
Victor Stinner8c62be82010-05-06 00:08:46 +00008400 WAIT_TYPE status;
8401 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008402
Victor Stinner8c62be82010-05-06 00:08:46 +00008403 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8404 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008405
Victor Stinner8c62be82010-05-06 00:08:46 +00008406 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008407}
8408#endif /* WSTOPSIG */
8409
8410#endif /* HAVE_SYS_WAIT_H */
8411
8412
Thomas Wouters477c8d52006-05-27 19:21:47 +00008413#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008414#ifdef _SCO_DS
8415/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8416 needed definitions in sys/statvfs.h */
8417#define _SVID3
8418#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008419#include <sys/statvfs.h>
8420
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008421static PyObject*
8422_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008423 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8424 if (v == NULL)
8425 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008426
8427#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008428 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8429 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8430 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8431 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8432 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8433 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8434 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8435 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8436 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8437 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008438#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008439 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8440 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8441 PyStructSequence_SET_ITEM(v, 2,
8442 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8443 PyStructSequence_SET_ITEM(v, 3,
8444 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8445 PyStructSequence_SET_ITEM(v, 4,
8446 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8447 PyStructSequence_SET_ITEM(v, 5,
8448 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8449 PyStructSequence_SET_ITEM(v, 6,
8450 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8451 PyStructSequence_SET_ITEM(v, 7,
8452 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8453 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8454 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008455#endif
8456
Victor Stinner8c62be82010-05-06 00:08:46 +00008457 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008458}
8459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008460PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008461"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008462Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008463
8464static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008465posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008466{
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 int fd, res;
8468 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008469
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8471 return NULL;
8472 Py_BEGIN_ALLOW_THREADS
8473 res = fstatvfs(fd, &st);
8474 Py_END_ALLOW_THREADS
8475 if (res != 0)
8476 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008477
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008479}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008480#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008481
8482
Thomas Wouters477c8d52006-05-27 19:21:47 +00008483#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008484#include <sys/statvfs.h>
8485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008486PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008487"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008488Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008489
8490static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008491posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008492{
Victor Stinner6fa67772011-09-20 04:04:33 +02008493 PyObject *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00008494 int res;
8495 struct statvfs st;
Victor Stinner6fa67772011-09-20 04:04:33 +02008496 if (!PyArg_ParseTuple(args, "O&:statvfs", PyUnicode_FSConverter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00008497 return NULL;
8498 Py_BEGIN_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008499 res = statvfs(PyBytes_AS_STRING(path), &st);
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 Py_END_ALLOW_THREADS
Victor Stinner6fa67772011-09-20 04:04:33 +02008501 if (res != 0) {
8502 posix_error_with_filename(PyBytes_AS_STRING(path));
8503 Py_DECREF(path);
8504 return NULL;
8505 }
8506 Py_DECREF(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008507
Victor Stinner8c62be82010-05-06 00:08:46 +00008508 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008509}
8510#endif /* HAVE_STATVFS */
8511
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008512#ifdef MS_WINDOWS
8513PyDoc_STRVAR(win32__getdiskusage__doc__,
8514"_getdiskusage(path) -> (total, free)\n\n\
8515Return disk usage statistics about the given path as (total, free) tuple.");
8516
8517static PyObject *
8518win32__getdiskusage(PyObject *self, PyObject *args)
8519{
8520 BOOL retval;
8521 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008522 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008523
Victor Stinner6139c1b2011-11-09 22:14:14 +01008524 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008525 return NULL;
8526
8527 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008528 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008529 Py_END_ALLOW_THREADS
8530 if (retval == 0)
8531 return PyErr_SetFromWindowsErr(0);
8532
8533 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8534}
8535#endif
8536
8537
Fred Drakec9680921999-12-13 16:37:25 +00008538/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8539 * It maps strings representing configuration variable names to
8540 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008541 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008542 * rarely-used constants. There are three separate tables that use
8543 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008544 *
8545 * This code is always included, even if none of the interfaces that
8546 * need it are included. The #if hackery needed to avoid it would be
8547 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008548 */
8549struct constdef {
8550 char *name;
8551 long value;
8552};
8553
Fred Drake12c6e2d1999-12-14 21:25:03 +00008554static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008555conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008556 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008557{
Christian Heimes217cfd12007-12-02 14:31:20 +00008558 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008559 *valuep = PyLong_AS_LONG(arg);
8560 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008561 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008562 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008563 /* look up the value in the table using a binary search */
8564 size_t lo = 0;
8565 size_t mid;
8566 size_t hi = tablesize;
8567 int cmp;
8568 const char *confname;
8569 if (!PyUnicode_Check(arg)) {
8570 PyErr_SetString(PyExc_TypeError,
8571 "configuration names must be strings or integers");
8572 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008573 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008574 confname = _PyUnicode_AsString(arg);
8575 if (confname == NULL)
8576 return 0;
8577 while (lo < hi) {
8578 mid = (lo + hi) / 2;
8579 cmp = strcmp(confname, table[mid].name);
8580 if (cmp < 0)
8581 hi = mid;
8582 else if (cmp > 0)
8583 lo = mid + 1;
8584 else {
8585 *valuep = table[mid].value;
8586 return 1;
8587 }
8588 }
8589 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8590 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008591 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008592}
8593
8594
8595#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8596static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008597#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008598 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008599#endif
8600#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008601 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008602#endif
Fred Drakec9680921999-12-13 16:37:25 +00008603#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008604 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008605#endif
8606#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008607 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008608#endif
8609#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008610 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008611#endif
8612#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008613 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008614#endif
8615#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008616 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008617#endif
8618#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008619 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008620#endif
8621#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008622 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008623#endif
8624#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008625 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008626#endif
8627#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008628 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008629#endif
8630#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008631 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008632#endif
8633#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008634 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008635#endif
8636#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008637 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008638#endif
8639#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008640 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008641#endif
8642#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008643 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008644#endif
8645#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008646 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008647#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008648#ifdef _PC_ACL_ENABLED
8649 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8650#endif
8651#ifdef _PC_MIN_HOLE_SIZE
8652 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8653#endif
8654#ifdef _PC_ALLOC_SIZE_MIN
8655 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8656#endif
8657#ifdef _PC_REC_INCR_XFER_SIZE
8658 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8659#endif
8660#ifdef _PC_REC_MAX_XFER_SIZE
8661 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8662#endif
8663#ifdef _PC_REC_MIN_XFER_SIZE
8664 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8665#endif
8666#ifdef _PC_REC_XFER_ALIGN
8667 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8668#endif
8669#ifdef _PC_SYMLINK_MAX
8670 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8671#endif
8672#ifdef _PC_XATTR_ENABLED
8673 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8674#endif
8675#ifdef _PC_XATTR_EXISTS
8676 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8677#endif
8678#ifdef _PC_TIMESTAMP_RESOLUTION
8679 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8680#endif
Fred Drakec9680921999-12-13 16:37:25 +00008681};
8682
Fred Drakec9680921999-12-13 16:37:25 +00008683static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008684conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008685{
8686 return conv_confname(arg, valuep, posix_constants_pathconf,
8687 sizeof(posix_constants_pathconf)
8688 / sizeof(struct constdef));
8689}
8690#endif
8691
8692#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008693PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008694"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008695Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008696If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008697
8698static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008699posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008700{
8701 PyObject *result = NULL;
8702 int name, fd;
8703
Fred Drake12c6e2d1999-12-14 21:25:03 +00008704 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8705 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008706 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008707
Stefan Krah0e803b32010-11-26 16:16:47 +00008708 errno = 0;
8709 limit = fpathconf(fd, name);
8710 if (limit == -1 && errno != 0)
8711 posix_error();
8712 else
8713 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008714 }
8715 return result;
8716}
8717#endif
8718
8719
8720#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008721PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008722"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008723Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008724If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008725
8726static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008727posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008728{
8729 PyObject *result = NULL;
8730 int name;
8731 char *path;
8732
8733 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8734 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008735 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008736
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 errno = 0;
8738 limit = pathconf(path, name);
8739 if (limit == -1 && errno != 0) {
8740 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008741 /* could be a path or name problem */
8742 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008743 else
Stefan Krah99439262010-11-26 12:58:05 +00008744 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008745 }
8746 else
8747 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008748 }
8749 return result;
8750}
8751#endif
8752
8753#ifdef HAVE_CONFSTR
8754static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008755#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008756 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008757#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008758#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008759 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008760#endif
8761#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008762 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008763#endif
Fred Draked86ed291999-12-15 15:34:33 +00008764#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008765 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008766#endif
8767#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008768 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008769#endif
8770#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008772#endif
8773#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008774 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008775#endif
Fred Drakec9680921999-12-13 16:37:25 +00008776#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008777 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008778#endif
8779#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008780 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008781#endif
8782#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008783 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008784#endif
8785#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008786 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008787#endif
8788#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008789 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008790#endif
8791#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008793#endif
8794#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008796#endif
8797#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008798 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008799#endif
Fred Draked86ed291999-12-15 15:34:33 +00008800#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008802#endif
Fred Drakec9680921999-12-13 16:37:25 +00008803#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008805#endif
Fred Draked86ed291999-12-15 15:34:33 +00008806#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008808#endif
8809#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008811#endif
8812#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008814#endif
8815#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008817#endif
Fred Drakec9680921999-12-13 16:37:25 +00008818#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008820#endif
8821#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
8842#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008844#endif
8845#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008847#endif
8848#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
8851#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008852 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008853#endif
8854#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008855 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008856#endif
8857#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008859#endif
8860#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008861 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008862#endif
8863#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008864 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008865#endif
Fred Draked86ed291999-12-15 15:34:33 +00008866#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008868#endif
8869#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008871#endif
8872#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008874#endif
8875#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008876 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008877#endif
8878#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008879 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008880#endif
8881#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008882 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008883#endif
8884#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008886#endif
8887#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008889#endif
8890#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008892#endif
8893#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008895#endif
8896#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008898#endif
8899#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008901#endif
8902#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008904#endif
Fred Drakec9680921999-12-13 16:37:25 +00008905};
8906
8907static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008908conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008909{
8910 return conv_confname(arg, valuep, posix_constants_confstr,
8911 sizeof(posix_constants_confstr)
8912 / sizeof(struct constdef));
8913}
8914
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008915PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008916"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008917Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008918
8919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008920posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008921{
8922 PyObject *result = NULL;
8923 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008924 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008925 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008926
Victor Stinnercb043522010-09-10 23:49:04 +00008927 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8928 return NULL;
8929
8930 errno = 0;
8931 len = confstr(name, buffer, sizeof(buffer));
8932 if (len == 0) {
8933 if (errno) {
8934 posix_error();
8935 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008936 }
8937 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008938 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008939 }
8940 }
Victor Stinnercb043522010-09-10 23:49:04 +00008941
8942 if ((unsigned int)len >= sizeof(buffer)) {
8943 char *buf = PyMem_Malloc(len);
8944 if (buf == NULL)
8945 return PyErr_NoMemory();
8946 confstr(name, buf, len);
8947 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8948 PyMem_Free(buf);
8949 }
8950 else
8951 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008952 return result;
8953}
8954#endif
8955
8956
8957#ifdef HAVE_SYSCONF
8958static struct constdef posix_constants_sysconf[] = {
8959#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008960 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008961#endif
8962#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008963 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008964#endif
8965#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008966 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008967#endif
8968#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008969 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008970#endif
8971#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008972 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008973#endif
8974#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008975 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008976#endif
8977#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008978 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008979#endif
8980#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008981 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008982#endif
8983#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008984 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008985#endif
8986#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008987 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008988#endif
Fred Draked86ed291999-12-15 15:34:33 +00008989#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008990 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008991#endif
8992#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008993 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008994#endif
Fred Drakec9680921999-12-13 16:37:25 +00008995#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008996 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008997#endif
Fred Drakec9680921999-12-13 16:37:25 +00008998#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008999 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009000#endif
9001#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009002 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009003#endif
9004#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009005 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009006#endif
9007#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009008 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009009#endif
9010#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009011 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009012#endif
Fred Draked86ed291999-12-15 15:34:33 +00009013#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009014 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009015#endif
Fred Drakec9680921999-12-13 16:37:25 +00009016#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009017 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009018#endif
9019#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009020 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009021#endif
9022#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009023 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009024#endif
9025#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009026 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009027#endif
9028#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009029 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009030#endif
Fred Draked86ed291999-12-15 15:34:33 +00009031#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009032 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009033#endif
Fred Drakec9680921999-12-13 16:37:25 +00009034#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009035 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009036#endif
9037#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009038 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009039#endif
9040#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009041 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009042#endif
9043#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009044 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009045#endif
9046#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009047 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009048#endif
9049#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009050 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00009051#endif
9052#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009053 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009054#endif
9055#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009056 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009057#endif
9058#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009059 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009060#endif
9061#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009062 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009063#endif
9064#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009065 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009066#endif
9067#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009068 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009069#endif
9070#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009071 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009072#endif
9073#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009074 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009075#endif
9076#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009077 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009078#endif
9079#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009080 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009081#endif
9082#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009083 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00009084#endif
9085#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009086 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009087#endif
9088#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009089 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009090#endif
9091#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009092 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009093#endif
9094#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009095 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009096#endif
9097#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009098 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009099#endif
9100#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009101 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009102#endif
Fred Draked86ed291999-12-15 15:34:33 +00009103#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00009104 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00009105#endif
Fred Drakec9680921999-12-13 16:37:25 +00009106#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009107 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009108#endif
9109#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009110 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009111#endif
9112#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009113 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009114#endif
Fred Draked86ed291999-12-15 15:34:33 +00009115#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009116 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00009117#endif
Fred Drakec9680921999-12-13 16:37:25 +00009118#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00009119 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00009120#endif
Fred Draked86ed291999-12-15 15:34:33 +00009121#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009122 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00009123#endif
9124#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00009125 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00009126#endif
Fred Drakec9680921999-12-13 16:37:25 +00009127#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009128 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009129#endif
9130#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009131 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009132#endif
9133#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009134 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009135#endif
9136#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009137 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009138#endif
Fred Draked86ed291999-12-15 15:34:33 +00009139#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00009140 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00009141#endif
Fred Drakec9680921999-12-13 16:37:25 +00009142#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00009143 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00009144#endif
9145#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009146 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00009147#endif
9148#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009149 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009150#endif
9151#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009152 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00009153#endif
9154#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009155 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00009156#endif
9157#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00009158 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00009159#endif
9160#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00009161 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00009162#endif
Fred Draked86ed291999-12-15 15:34:33 +00009163#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00009164 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00009165#endif
Fred Drakec9680921999-12-13 16:37:25 +00009166#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009167 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009168#endif
9169#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009170 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009171#endif
Fred Draked86ed291999-12-15 15:34:33 +00009172#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009173 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009174#endif
Fred Drakec9680921999-12-13 16:37:25 +00009175#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009176 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009177#endif
9178#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009179 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009180#endif
9181#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009182 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009183#endif
9184#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009185 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009186#endif
9187#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009188 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009189#endif
9190#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009191 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009192#endif
9193#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009194 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009195#endif
9196#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009197 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00009198#endif
9199#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009200 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00009201#endif
Fred Draked86ed291999-12-15 15:34:33 +00009202#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009203 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00009204#endif
9205#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009206 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00009207#endif
Fred Drakec9680921999-12-13 16:37:25 +00009208#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00009209 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00009210#endif
9211#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009212 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009213#endif
9214#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009215 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009216#endif
9217#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009218 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009219#endif
9220#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009221 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009222#endif
9223#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009224 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009225#endif
9226#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00009227 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00009228#endif
9229#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00009230 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00009231#endif
9232#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009233 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00009234#endif
9235#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009236 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00009237#endif
9238#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00009239 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00009240#endif
9241#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009242 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00009243#endif
9244#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009245 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00009246#endif
9247#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00009248 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00009249#endif
9250#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00009251 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00009252#endif
9253#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00009254 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00009255#endif
9256#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00009257 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00009258#endif
9259#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009260 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009261#endif
9262#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009263 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009264#endif
9265#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00009266 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00009267#endif
9268#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009269 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009270#endif
9271#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009272 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009273#endif
9274#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00009275 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00009276#endif
9277#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009278 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009279#endif
9280#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009281 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009282#endif
9283#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009284 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00009285#endif
9286#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00009287 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00009288#endif
9289#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009290 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009291#endif
9292#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009294#endif
9295#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009296 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00009297#endif
9298#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009299 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009300#endif
9301#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009302 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009303#endif
9304#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009306#endif
9307#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009308 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009309#endif
9310#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009311 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009312#endif
Fred Draked86ed291999-12-15 15:34:33 +00009313#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00009314 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00009315#endif
Fred Drakec9680921999-12-13 16:37:25 +00009316#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00009318#endif
9319#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009320 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009321#endif
9322#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00009323 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00009324#endif
9325#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009326 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009327#endif
9328#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009329 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009330#endif
9331#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009332 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009333#endif
9334#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00009335 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00009336#endif
9337#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009338 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009339#endif
9340#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009341 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009342#endif
9343#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009344 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009345#endif
9346#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009347 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009348#endif
9349#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009350 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00009351#endif
9352#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009353 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00009354#endif
9355#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00009356 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00009357#endif
9358#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009359 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009360#endif
9361#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009363#endif
9364#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009366#endif
9367#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009368 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009369#endif
9370#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009371 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009372#endif
9373#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009374 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009375#endif
9376#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009378#endif
9379#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009381#endif
9382#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009384#endif
9385#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009386 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009387#endif
9388#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009389 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009390#endif
9391#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009392 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009393#endif
9394#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009396#endif
9397#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009398 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009399#endif
9400#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009401 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009402#endif
9403#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009405#endif
9406#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009407 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009408#endif
9409#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009410 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009411#endif
9412#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009413 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009414#endif
9415#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009416 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009417#endif
9418#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009419 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009420#endif
9421#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009422 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009423#endif
9424#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009425 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009426#endif
9427#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009428 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009429#endif
9430#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009431 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009432#endif
9433#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009434 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009435#endif
9436#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009437 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009438#endif
9439#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009441#endif
9442#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009443 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009444#endif
9445#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009447#endif
9448#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009449 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009450#endif
9451};
9452
9453static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009454conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009455{
9456 return conv_confname(arg, valuep, posix_constants_sysconf,
9457 sizeof(posix_constants_sysconf)
9458 / sizeof(struct constdef));
9459}
9460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009461PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009462"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009463Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009464
9465static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009466posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009467{
9468 PyObject *result = NULL;
9469 int name;
9470
9471 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
9472 int value;
9473
9474 errno = 0;
9475 value = sysconf(name);
9476 if (value == -1 && errno != 0)
9477 posix_error();
9478 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009479 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009480 }
9481 return result;
9482}
9483#endif
9484
9485
Fred Drakebec628d1999-12-15 18:31:10 +00009486/* This code is used to ensure that the tables of configuration value names
9487 * are in sorted order as required by conv_confname(), and also to build the
9488 * the exported dictionaries that are used to publish information about the
9489 * names available on the host platform.
9490 *
9491 * Sorting the table at runtime ensures that the table is properly ordered
9492 * when used, even for platforms we're not able to test on. It also makes
9493 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009494 */
Fred Drakebec628d1999-12-15 18:31:10 +00009495
9496static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009497cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009498{
9499 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009500 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009501 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009502 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009503
9504 return strcmp(c1->name, c2->name);
9505}
9506
9507static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009508setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009509 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009510{
Fred Drakebec628d1999-12-15 18:31:10 +00009511 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009512 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009513
9514 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9515 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009516 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009517 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009518
Barry Warsaw3155db32000-04-13 15:20:40 +00009519 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009520 PyObject *o = PyLong_FromLong(table[i].value);
9521 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9522 Py_XDECREF(o);
9523 Py_DECREF(d);
9524 return -1;
9525 }
9526 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009527 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009528 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009529}
9530
Fred Drakebec628d1999-12-15 18:31:10 +00009531/* Return -1 on failure, 0 on success. */
9532static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009533setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009534{
9535#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009536 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009537 sizeof(posix_constants_pathconf)
9538 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009539 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009540 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009541#endif
9542#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009543 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009544 sizeof(posix_constants_confstr)
9545 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009546 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009547 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009548#endif
9549#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009550 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009551 sizeof(posix_constants_sysconf)
9552 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009553 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009554 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009555#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009556 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009557}
Fred Draked86ed291999-12-15 15:34:33 +00009558
9559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009560PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009561"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009562Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009563in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009564
9565static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009566posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009567{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009568 abort();
9569 /*NOTREACHED*/
9570 Py_FatalError("abort() called from Python code didn't abort!");
9571 return NULL;
9572}
Fred Drakebec628d1999-12-15 18:31:10 +00009573
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009574#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009575PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009576"startfile(filepath [, operation]) - Start a file with its associated\n\
9577application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009578\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009579When \"operation\" is not specified or \"open\", this acts like\n\
9580double-clicking the file in Explorer, or giving the file name as an\n\
9581argument to the DOS \"start\" command: the file is opened with whatever\n\
9582application (if any) its extension is associated.\n\
9583When another \"operation\" is given, it specifies what should be done with\n\
9584the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009585\n\
9586startfile returns as soon as the associated application is launched.\n\
9587There is no option to wait for the application to close, and no way\n\
9588to retrieve the application's exit status.\n\
9589\n\
9590The filepath is relative to the current directory. If you want to use\n\
9591an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009592the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009593
9594static PyObject *
9595win32_startfile(PyObject *self, PyObject *args)
9596{
Victor Stinner8c62be82010-05-06 00:08:46 +00009597 PyObject *ofilepath;
9598 char *filepath;
9599 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009600 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009601 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009602
Victor Stinnereb5657a2011-09-30 01:44:27 +02009603 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009604 if (!PyArg_ParseTuple(args, "U|s:startfile",
9605 &unipath, &operation)) {
9606 PyErr_Clear();
9607 goto normal;
9608 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009609
Victor Stinner8c62be82010-05-06 00:08:46 +00009610 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009611 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009612 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009613 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009614 PyErr_Clear();
9615 operation = NULL;
9616 goto normal;
9617 }
9618 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009619
Victor Stinnereb5657a2011-09-30 01:44:27 +02009620 wpath = PyUnicode_AsUnicode(unipath);
9621 if (wpath == NULL)
9622 goto normal;
9623 if (uoperation) {
9624 woperation = PyUnicode_AsUnicode(uoperation);
9625 if (woperation == NULL)
9626 goto normal;
9627 }
9628 else
9629 woperation = NULL;
9630
Victor Stinner8c62be82010-05-06 00:08:46 +00009631 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009632 rc = ShellExecuteW((HWND)0, woperation, wpath,
9633 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009634 Py_END_ALLOW_THREADS
9635
Victor Stinnereb5657a2011-09-30 01:44:27 +02009636 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009637 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009638 win32_error_object("startfile", unipath);
9639 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009640 }
9641 Py_INCREF(Py_None);
9642 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009643
9644normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009645 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9646 PyUnicode_FSConverter, &ofilepath,
9647 &operation))
9648 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009649 if (win32_warn_bytes_api()) {
9650 Py_DECREF(ofilepath);
9651 return NULL;
9652 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009653 filepath = PyBytes_AsString(ofilepath);
9654 Py_BEGIN_ALLOW_THREADS
9655 rc = ShellExecute((HWND)0, operation, filepath,
9656 NULL, NULL, SW_SHOWNORMAL);
9657 Py_END_ALLOW_THREADS
9658 if (rc <= (HINSTANCE)32) {
9659 PyObject *errval = win32_error("startfile", filepath);
9660 Py_DECREF(ofilepath);
9661 return errval;
9662 }
9663 Py_DECREF(ofilepath);
9664 Py_INCREF(Py_None);
9665 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009666}
9667#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009668
Martin v. Löwis438b5342002-12-27 10:16:42 +00009669#ifdef HAVE_GETLOADAVG
9670PyDoc_STRVAR(posix_getloadavg__doc__,
9671"getloadavg() -> (float, float, float)\n\n\
9672Return the number of processes in the system run queue averaged over\n\
9673the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9674was unobtainable");
9675
9676static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009677posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009678{
9679 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009680 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009681 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9682 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009683 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009684 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009685}
9686#endif
9687
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009688PyDoc_STRVAR(device_encoding__doc__,
9689"device_encoding(fd) -> str\n\n\
9690Return a string describing the encoding of the device\n\
9691if the output is a terminal; else return None.");
9692
9693static PyObject *
9694device_encoding(PyObject *self, PyObject *args)
9695{
Victor Stinner8c62be82010-05-06 00:08:46 +00009696 int fd;
Brett Cannonefb00c02012-02-29 18:31:31 -05009697
Victor Stinner8c62be82010-05-06 00:08:46 +00009698 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9699 return NULL;
Brett Cannonefb00c02012-02-29 18:31:31 -05009700
9701 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009702}
9703
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009704#ifdef __VMS
9705/* Use openssl random routine */
9706#include <openssl/rand.h>
9707PyDoc_STRVAR(vms_urandom__doc__,
9708"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009709Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009710
9711static PyObject*
9712vms_urandom(PyObject *self, PyObject *args)
9713{
Victor Stinner8c62be82010-05-06 00:08:46 +00009714 int howMany;
9715 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009716
Victor Stinner8c62be82010-05-06 00:08:46 +00009717 /* Read arguments */
9718 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9719 return NULL;
9720 if (howMany < 0)
9721 return PyErr_Format(PyExc_ValueError,
9722 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009723
Victor Stinner8c62be82010-05-06 00:08:46 +00009724 /* Allocate bytes */
9725 result = PyBytes_FromStringAndSize(NULL, howMany);
9726 if (result != NULL) {
9727 /* Get random data */
9728 if (RAND_pseudo_bytes((unsigned char*)
9729 PyBytes_AS_STRING(result),
9730 howMany) < 0) {
9731 Py_DECREF(result);
9732 return PyErr_Format(PyExc_ValueError,
9733 "RAND_pseudo_bytes");
9734 }
9735 }
9736 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009737}
9738#endif
9739
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009740#ifdef HAVE_SETRESUID
9741PyDoc_STRVAR(posix_setresuid__doc__,
9742"setresuid(ruid, euid, suid)\n\n\
9743Set the current process's real, effective, and saved user ids.");
9744
9745static PyObject*
9746posix_setresuid (PyObject *self, PyObject *args)
9747{
Victor Stinner8c62be82010-05-06 00:08:46 +00009748 /* We assume uid_t is no larger than a long. */
9749 long ruid, euid, suid;
9750 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9751 return NULL;
9752 if (setresuid(ruid, euid, suid) < 0)
9753 return posix_error();
9754 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009755}
9756#endif
9757
9758#ifdef HAVE_SETRESGID
9759PyDoc_STRVAR(posix_setresgid__doc__,
9760"setresgid(rgid, egid, sgid)\n\n\
9761Set the current process's real, effective, and saved group ids.");
9762
9763static PyObject*
9764posix_setresgid (PyObject *self, PyObject *args)
9765{
Victor Stinner8c62be82010-05-06 00:08:46 +00009766 /* We assume uid_t is no larger than a long. */
9767 long rgid, egid, sgid;
9768 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9769 return NULL;
9770 if (setresgid(rgid, egid, sgid) < 0)
9771 return posix_error();
9772 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009773}
9774#endif
9775
9776#ifdef HAVE_GETRESUID
9777PyDoc_STRVAR(posix_getresuid__doc__,
9778"getresuid() -> (ruid, euid, suid)\n\n\
9779Get tuple of the current process's real, effective, and saved user ids.");
9780
9781static PyObject*
9782posix_getresuid (PyObject *self, PyObject *noargs)
9783{
Victor Stinner8c62be82010-05-06 00:08:46 +00009784 uid_t ruid, euid, suid;
9785 long l_ruid, l_euid, l_suid;
9786 if (getresuid(&ruid, &euid, &suid) < 0)
9787 return posix_error();
9788 /* Force the values into long's as we don't know the size of uid_t. */
9789 l_ruid = ruid;
9790 l_euid = euid;
9791 l_suid = suid;
9792 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009793}
9794#endif
9795
9796#ifdef HAVE_GETRESGID
9797PyDoc_STRVAR(posix_getresgid__doc__,
9798"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009799Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009800
9801static PyObject*
9802posix_getresgid (PyObject *self, PyObject *noargs)
9803{
Victor Stinner8c62be82010-05-06 00:08:46 +00009804 uid_t rgid, egid, sgid;
9805 long l_rgid, l_egid, l_sgid;
9806 if (getresgid(&rgid, &egid, &sgid) < 0)
9807 return posix_error();
9808 /* Force the values into long's as we don't know the size of uid_t. */
9809 l_rgid = rgid;
9810 l_egid = egid;
9811 l_sgid = sgid;
9812 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009813}
9814#endif
9815
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009816/* Posix *at family of functions:
9817 faccessat, fchmodat, fchownat, fstatat, futimesat,
9818 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9819 unlinkat, utimensat, mkfifoat */
9820
9821#ifdef HAVE_FACCESSAT
9822PyDoc_STRVAR(posix_faccessat__doc__,
9823"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9824Like access() but if path is relative, it is taken as relative to dirfd.\n\
9825flags is optional and can be constructed by ORing together zero or more\n\
9826of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9827If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9828is interpreted relative to the current working directory.");
9829
9830static PyObject *
9831posix_faccessat(PyObject *self, PyObject *args)
9832{
9833 PyObject *opath;
9834 char *path;
9835 int mode;
9836 int res;
9837 int dirfd, flags = 0;
9838 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9839 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9840 return NULL;
9841 path = PyBytes_AsString(opath);
9842 Py_BEGIN_ALLOW_THREADS
9843 res = faccessat(dirfd, path, mode, flags);
9844 Py_END_ALLOW_THREADS
9845 Py_DECREF(opath);
9846 return PyBool_FromLong(res == 0);
9847}
9848#endif
9849
9850#ifdef HAVE_FCHMODAT
9851PyDoc_STRVAR(posix_fchmodat__doc__,
9852"fchmodat(dirfd, path, mode, flags=0)\n\n\
9853Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9854flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9855If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9856is interpreted relative to the current working directory.");
9857
9858static PyObject *
9859posix_fchmodat(PyObject *self, PyObject *args)
9860{
9861 int dirfd, mode, res;
9862 int flags = 0;
9863 PyObject *opath;
9864 char *path;
9865
9866 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9867 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9868 return NULL;
9869
9870 path = PyBytes_AsString(opath);
9871
9872 Py_BEGIN_ALLOW_THREADS
9873 res = fchmodat(dirfd, path, mode, flags);
9874 Py_END_ALLOW_THREADS
9875 Py_DECREF(opath);
9876 if (res < 0)
9877 return posix_error();
9878 Py_RETURN_NONE;
9879}
9880#endif /* HAVE_FCHMODAT */
9881
9882#ifdef HAVE_FCHOWNAT
9883PyDoc_STRVAR(posix_fchownat__doc__,
9884"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9885Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9886flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9887If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9888is interpreted relative to the current working directory.");
9889
9890static PyObject *
9891posix_fchownat(PyObject *self, PyObject *args)
9892{
9893 PyObject *opath;
9894 int dirfd, res;
9895 long uid, gid;
9896 int flags = 0;
9897 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009898
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009899 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9900 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9901 return NULL;
9902
9903 path = PyBytes_AsString(opath);
9904
9905 Py_BEGIN_ALLOW_THREADS
9906 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9907 Py_END_ALLOW_THREADS
9908 Py_DECREF(opath);
9909 if (res < 0)
9910 return posix_error();
9911 Py_RETURN_NONE;
9912}
9913#endif /* HAVE_FCHOWNAT */
9914
9915#ifdef HAVE_FSTATAT
9916PyDoc_STRVAR(posix_fstatat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01009917"fstatat(dirfd, path, flags=0) -> stat result\n\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009918Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9919flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9920If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9921is interpreted relative to the current working directory.");
9922
9923static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01009924posix_fstatat(PyObject *self, PyObject *args)
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009925{
9926 PyObject *opath;
9927 char *path;
9928 STRUCT_STAT st;
9929 int dirfd, res, flags = 0;
9930
Victor Stinner4195b5c2012-02-08 23:03:19 +01009931 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9932 &dirfd, PyUnicode_FSConverter, &opath, &flags))
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009933 return NULL;
9934 path = PyBytes_AsString(opath);
9935
9936 Py_BEGIN_ALLOW_THREADS
9937 res = fstatat(dirfd, path, &st, flags);
9938 Py_END_ALLOW_THREADS
9939 Py_DECREF(opath);
9940 if (res != 0)
9941 return posix_error();
9942
Victor Stinner4195b5c2012-02-08 23:03:19 +01009943 return _pystat_fromstructstat(&st);
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009944}
9945#endif
9946
9947#ifdef HAVE_FUTIMESAT
9948PyDoc_STRVAR(posix_futimesat__doc__,
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009949"futimesat(dirfd, path[, (atime, mtime)])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009950Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9951If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9952is interpreted relative to the current working directory.");
9953
9954static PyObject *
9955posix_futimesat(PyObject *self, PyObject *args)
9956{
9957 PyObject *opath;
9958 char *path;
9959 int res, dirfd;
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009960 PyObject* arg = Py_None;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009961 time_t atime, mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009962 long ansec, mnsec;
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009963
Brian Curtin7ef53ef2011-11-07 14:38:24 -06009964 if (!PyArg_ParseTuple(args, "iO&|O:futimesat",
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009965 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9966 return NULL;
9967 path = PyBytes_AsString(opath);
9968 if (arg == Py_None) {
9969 /* optional time values not given */
9970 Py_BEGIN_ALLOW_THREADS
9971 res = futimesat(dirfd, path, NULL);
9972 Py_END_ALLOW_THREADS
9973 }
9974 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9975 PyErr_SetString(PyExc_TypeError,
9976 "futimesat() arg 3 must be a tuple (atime, mtime)");
9977 Py_DECREF(opath);
9978 return NULL;
9979 }
9980 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +01009981 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 0),
9982 &atime, &ansec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009983 Py_DECREF(opath);
9984 return NULL;
9985 }
Victor Stinner5d272cc2012-03-13 13:35:55 +01009986 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(arg, 1),
9987 &mtime, &mnsec) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009988 Py_DECREF(opath);
9989 return NULL;
9990 }
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009991
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009992 Py_BEGIN_ALLOW_THREADS
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009993 {
9994#ifdef HAVE_UTIMENSAT
9995 struct timespec buf[2];
9996 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009997 buf[0].tv_nsec = ansec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -07009998 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +01009999 buf[1].tv_nsec = mnsec;
Larry Hastings9e3e70b2011-09-08 19:29:07 -070010000 res = utimensat(dirfd, path, buf, 0);
10001#else
10002 struct timeval buf[2];
10003 buf[0].tv_sec = atime;
Victor Stinnera2f7c002012-02-08 03:36:25 +010010004 buf[0].tv_usec = ansec / 1000;
Larry Hastings9e3e70b2011-09-08 19:29:07 -070010005 buf[1].tv_sec = mtime;
Victor Stinnera2f7c002012-02-08 03:36:25 +010010006 buf[1].tv_usec = mnsec / 1000;
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010007 res = futimesat(dirfd, path, buf);
Larry Hastings9e3e70b2011-09-08 19:29:07 -070010008#endif
10009 }
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010010 Py_END_ALLOW_THREADS
10011 }
10012 Py_DECREF(opath);
10013 if (res < 0) {
10014 return posix_error();
10015 }
10016 Py_RETURN_NONE;
10017}
10018#endif
10019
10020#ifdef HAVE_LINKAT
10021PyDoc_STRVAR(posix_linkat__doc__,
10022"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
10023Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
10024and if dstpath is relative, it is taken as relative to dstfd.\n\
10025flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
10026If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
10027srcpath is interpreted relative to the current working directory. This\n\
10028also applies for dstpath.");
10029
10030static PyObject *
10031posix_linkat(PyObject *self, PyObject *args)
10032{
10033 PyObject *osrc, *odst;
10034 char *src, *dst;
10035 int res, srcfd, dstfd;
10036 int flags = 0;
10037
10038 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
10039 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
10040 return NULL;
10041 src = PyBytes_AsString(osrc);
10042 dst = PyBytes_AsString(odst);
10043 Py_BEGIN_ALLOW_THREADS
10044 res = linkat(srcfd, src, dstfd, dst, flags);
10045 Py_END_ALLOW_THREADS
10046 Py_DECREF(osrc);
10047 Py_DECREF(odst);
10048 if (res < 0)
10049 return posix_error();
10050 Py_RETURN_NONE;
10051}
10052#endif /* HAVE_LINKAT */
10053
10054#ifdef HAVE_MKDIRAT
10055PyDoc_STRVAR(posix_mkdirat__doc__,
10056"mkdirat(dirfd, path, mode=0o777)\n\n\
10057Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
10058If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10059is interpreted relative to the current working directory.");
10060
10061static PyObject *
10062posix_mkdirat(PyObject *self, PyObject *args)
10063{
10064 int res, dirfd;
10065 PyObject *opath;
10066 char *path;
10067 int mode = 0777;
10068
10069 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
10070 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10071 return NULL;
10072 path = PyBytes_AsString(opath);
10073 Py_BEGIN_ALLOW_THREADS
10074 res = mkdirat(dirfd, path, mode);
10075 Py_END_ALLOW_THREADS
10076 Py_DECREF(opath);
10077 if (res < 0)
10078 return posix_error();
10079 Py_RETURN_NONE;
10080}
10081#endif
10082
10083#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10084PyDoc_STRVAR(posix_mknodat__doc__,
10085"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
10086Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
10087If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10088is interpreted relative to the current working directory.");
10089
10090static PyObject *
10091posix_mknodat(PyObject *self, PyObject *args)
10092{
10093 PyObject *opath;
10094 char *filename;
10095 int mode = 0600;
10096 int device = 0;
10097 int res, dirfd;
10098 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
10099 PyUnicode_FSConverter, &opath, &mode, &device))
10100 return NULL;
10101 filename = PyBytes_AS_STRING(opath);
10102 Py_BEGIN_ALLOW_THREADS
10103 res = mknodat(dirfd, filename, mode, device);
10104 Py_END_ALLOW_THREADS
10105 Py_DECREF(opath);
10106 if (res < 0)
10107 return posix_error();
10108 Py_RETURN_NONE;
10109}
10110#endif
10111
10112#ifdef HAVE_OPENAT
10113PyDoc_STRVAR(posix_openat__doc__,
10114"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
10115Like open() but if path is relative, it is taken as relative to dirfd.\n\
10116If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10117is interpreted relative to the current working directory.");
10118
10119static PyObject *
10120posix_openat(PyObject *self, PyObject *args)
10121{
10122 PyObject *ofile;
10123 char *file;
10124 int flag, dirfd, fd;
10125 int mode = 0777;
10126
10127 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
10128 &dirfd, PyUnicode_FSConverter, &ofile,
10129 &flag, &mode))
10130 return NULL;
10131 file = PyBytes_AsString(ofile);
10132 Py_BEGIN_ALLOW_THREADS
10133 fd = openat(dirfd, file, flag, mode);
10134 Py_END_ALLOW_THREADS
10135 Py_DECREF(ofile);
10136 if (fd < 0)
10137 return posix_error();
10138 return PyLong_FromLong((long)fd);
10139}
10140#endif
10141
10142#ifdef HAVE_READLINKAT
10143PyDoc_STRVAR(posix_readlinkat__doc__,
10144"readlinkat(dirfd, path) -> path\n\n\
10145Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
10146If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10147is interpreted relative to the current working directory.");
10148
10149static PyObject *
10150posix_readlinkat(PyObject *self, PyObject *args)
10151{
10152 PyObject *v, *opath;
10153 char buf[MAXPATHLEN];
10154 char *path;
10155 int n, dirfd;
10156 int arg_is_unicode = 0;
10157
10158 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
10159 &dirfd, PyUnicode_FSConverter, &opath))
10160 return NULL;
10161 path = PyBytes_AsString(opath);
10162 v = PySequence_GetItem(args, 1);
10163 if (v == NULL) {
10164 Py_DECREF(opath);
10165 return NULL;
10166 }
10167
10168 if (PyUnicode_Check(v)) {
10169 arg_is_unicode = 1;
10170 }
10171 Py_DECREF(v);
10172
10173 Py_BEGIN_ALLOW_THREADS
10174 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
10175 Py_END_ALLOW_THREADS
10176 Py_DECREF(opath);
10177 if (n < 0)
10178 return posix_error();
10179
10180 if (arg_is_unicode)
10181 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
10182 else
10183 return PyBytes_FromStringAndSize(buf, n);
10184}
10185#endif /* HAVE_READLINKAT */
10186
10187#ifdef HAVE_RENAMEAT
10188PyDoc_STRVAR(posix_renameat__doc__,
10189"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
10190Like rename() but if oldpath is relative, it is taken as relative to\n\
10191olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
10192If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
10193oldpath is interpreted relative to the current working directory. This\n\
10194also applies for newpath.");
10195
10196static PyObject *
10197posix_renameat(PyObject *self, PyObject *args)
10198{
10199 int res;
10200 PyObject *opathold, *opathnew;
10201 char *opath, *npath;
10202 int oldfd, newfd;
10203
10204 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
10205 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
10206 return NULL;
10207 opath = PyBytes_AsString(opathold);
10208 npath = PyBytes_AsString(opathnew);
10209 Py_BEGIN_ALLOW_THREADS
10210 res = renameat(oldfd, opath, newfd, npath);
10211 Py_END_ALLOW_THREADS
10212 Py_DECREF(opathold);
10213 Py_DECREF(opathnew);
10214 if (res < 0)
10215 return posix_error();
10216 Py_RETURN_NONE;
10217}
10218#endif
10219
10220#if HAVE_SYMLINKAT
10221PyDoc_STRVAR(posix_symlinkat__doc__,
10222"symlinkat(src, dstfd, dst)\n\n\
10223Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
10224If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
10225is interpreted relative to the current working directory.");
10226
10227static PyObject *
10228posix_symlinkat(PyObject *self, PyObject *args)
10229{
10230 int res, dstfd;
10231 PyObject *osrc, *odst;
10232 char *src, *dst;
10233
10234 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
10235 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
10236 return NULL;
10237 src = PyBytes_AsString(osrc);
10238 dst = PyBytes_AsString(odst);
10239 Py_BEGIN_ALLOW_THREADS
10240 res = symlinkat(src, dstfd, dst);
10241 Py_END_ALLOW_THREADS
10242 Py_DECREF(osrc);
10243 Py_DECREF(odst);
10244 if (res < 0)
10245 return posix_error();
10246 Py_RETURN_NONE;
10247}
10248#endif /* HAVE_SYMLINKAT */
10249
10250#ifdef HAVE_UNLINKAT
10251PyDoc_STRVAR(posix_unlinkat__doc__,
10252"unlinkat(dirfd, path, flags=0)\n\n\
10253Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
10254flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
10255specified, unlinkat() behaves like rmdir().\n\
10256If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10257is interpreted relative to the current working directory.");
10258
10259static PyObject *
10260posix_unlinkat(PyObject *self, PyObject *args)
10261{
10262 int dirfd, res, flags = 0;
10263 PyObject *opath;
10264 char *path;
10265
10266 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
10267 &dirfd, PyUnicode_FSConverter, &opath, &flags))
10268 return NULL;
10269 path = PyBytes_AsString(opath);
10270 Py_BEGIN_ALLOW_THREADS
10271 res = unlinkat(dirfd, path, flags);
10272 Py_END_ALLOW_THREADS
10273 Py_DECREF(opath);
10274 if (res < 0)
10275 return posix_error();
10276 Py_RETURN_NONE;
10277}
10278#endif
10279
10280#ifdef HAVE_UTIMENSAT
10281PyDoc_STRVAR(posix_utimensat__doc__,
Brian Curtin569b4942011-11-07 16:09:20 -060010282"utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\
10283 mtime=(mtime_sec, mtime_nsec), flags=0])\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010284utimensat(dirfd, path, None, None, flags)\n\n\
10285Updates the timestamps of a file with nanosecond precision. If path is\n\
10286relative, it is taken as relative to dirfd.\n\
Brian Curtin569b4942011-11-07 16:09:20 -060010287If atime and mtime are both None, which is the default, set atime and\n\
10288mtime to the current time.\n\
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010289flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
10290If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10291is interpreted relative to the current working directory.\n\
10292If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
10293current time.\n\
10294If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
10295
10296static PyObject *
Brian Curtin569b4942011-11-07 16:09:20 -060010297posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010298{
10299 PyObject *opath;
10300 char *path;
10301 int res, dirfd, flags = 0;
Brian Curtin569b4942011-11-07 16:09:20 -060010302 PyObject *atime = Py_None;
10303 PyObject *mtime = Py_None;
10304
10305 static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL};
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010306
10307 struct timespec buf[2];
10308
Brian Curtin569b4942011-11-07 16:09:20 -060010309 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist,
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010310 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
10311 return NULL;
10312 path = PyBytes_AsString(opath);
10313 if (atime == Py_None && mtime == Py_None) {
10314 /* optional time values not given */
10315 Py_BEGIN_ALLOW_THREADS
10316 res = utimensat(dirfd, path, NULL, flags);
10317 Py_END_ALLOW_THREADS
10318 }
10319 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
10320 PyErr_SetString(PyExc_TypeError,
10321 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
10322 Py_DECREF(opath);
10323 return NULL;
10324 }
10325 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
10326 PyErr_SetString(PyExc_TypeError,
10327 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
10328 Py_DECREF(opath);
10329 return NULL;
10330 }
10331 else {
10332 if (!PyArg_ParseTuple(atime, "ll:utimensat",
10333 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
10334 Py_DECREF(opath);
10335 return NULL;
10336 }
10337 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
10338 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
10339 Py_DECREF(opath);
10340 return NULL;
10341 }
10342 Py_BEGIN_ALLOW_THREADS
10343 res = utimensat(dirfd, path, buf, flags);
10344 Py_END_ALLOW_THREADS
10345 }
10346 Py_DECREF(opath);
10347 if (res < 0) {
10348 return posix_error();
10349 }
10350 Py_RETURN_NONE;
10351}
10352#endif
10353
10354#ifdef HAVE_MKFIFOAT
10355PyDoc_STRVAR(posix_mkfifoat__doc__,
10356"mkfifoat(dirfd, path, mode=0o666)\n\n\
10357Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
10358If path is relative and dirfd is the special value AT_FDCWD, then path\n\
10359is interpreted relative to the current working directory.");
10360
10361static PyObject *
10362posix_mkfifoat(PyObject *self, PyObject *args)
10363{
10364 PyObject *opath;
10365 char *filename;
10366 int mode = 0666;
10367 int res, dirfd;
10368 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
10369 &dirfd, PyUnicode_FSConverter, &opath, &mode))
10370 return NULL;
10371 filename = PyBytes_AS_STRING(opath);
10372 Py_BEGIN_ALLOW_THREADS
10373 res = mkfifoat(dirfd, filename, mode);
10374 Py_END_ALLOW_THREADS
10375 Py_DECREF(opath);
10376 if (res < 0)
10377 return posix_error();
10378 Py_RETURN_NONE;
10379}
10380#endif
10381
Benjamin Peterson9428d532011-09-14 11:45:52 -040010382#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040010383
10384static int
10385try_getxattr(const char *path, const char *name,
10386 ssize_t (*get)(const char *, const char *, void *, size_t),
10387 Py_ssize_t buf_size, PyObject **res)
10388{
10389 PyObject *value;
10390 Py_ssize_t len;
10391
10392 assert(buf_size <= XATTR_SIZE_MAX);
10393 value = PyBytes_FromStringAndSize(NULL, buf_size);
10394 if (!value)
10395 return 0;
10396 Py_BEGIN_ALLOW_THREADS;
10397 len = get(path, name, PyBytes_AS_STRING(value), buf_size);
10398 Py_END_ALLOW_THREADS;
10399 if (len < 0) {
10400 Py_DECREF(value);
10401 if (errno == ERANGE) {
10402 value = NULL;
10403 }
10404 else {
10405 posix_error();
10406 return 0;
10407 }
10408 }
10409 else if (len != buf_size) {
10410 /* Can only shrink. */
10411 _PyBytes_Resize(&value, len);
10412 }
10413 *res = value;
10414 return 1;
10415}
10416
10417static PyObject *
10418getxattr_common(const char *path, PyObject *name_obj,
10419 ssize_t (*get)(const char *, const char *, void *, size_t))
10420{
10421 PyObject *value;
10422 const char *name = PyBytes_AS_STRING(name_obj);
10423
10424 /* Try a small value first. */
10425 if (!try_getxattr(path, name, get, 128, &value))
10426 return NULL;
10427 if (value)
10428 return value;
10429 /* Now the maximum possible one. */
10430 if (!try_getxattr(path, name, get, XATTR_SIZE_MAX, &value))
10431 return NULL;
10432 assert(value);
10433 return value;
10434}
10435
10436PyDoc_STRVAR(posix_getxattr__doc__,
10437"getxattr(path, attr) -> value\n\n\
10438Return the value of extended attribute *name* on *path*.");
10439
10440static PyObject *
10441posix_getxattr(PyObject *self, PyObject *args)
10442{
10443 PyObject *path, *res, *name;
10444
10445 if (!PyArg_ParseTuple(args, "O&O&:getxattr", PyUnicode_FSConverter, &path,
10446 PyUnicode_FSConverter, &name))
10447 return NULL;
10448 res = getxattr_common(PyBytes_AS_STRING(path), name, getxattr);
10449 Py_DECREF(path);
10450 Py_DECREF(name);
10451 return res;
10452}
10453
10454PyDoc_STRVAR(posix_lgetxattr__doc__,
10455"lgetxattr(path, attr) -> value\n\n\
10456Like getxattr but don't follow symlinks.");
10457
10458static PyObject *
10459posix_lgetxattr(PyObject *self, PyObject *args)
10460{
10461 PyObject *path, *res, *name;
10462
10463 if (!PyArg_ParseTuple(args, "O&O&:lgetxattr", PyUnicode_FSConverter, &path,
10464 PyUnicode_FSConverter, &name))
10465 return NULL;
10466 res = getxattr_common(PyBytes_AS_STRING(path), name, lgetxattr);
10467 Py_DECREF(path);
10468 Py_DECREF(name);
10469 return res;
10470}
10471
10472static ssize_t
10473wrap_fgetxattr(const char *path, const char *name, void *value, size_t size)
10474{
10475 /* Hack to share code. */
10476 return fgetxattr((int)(Py_uintptr_t)path, name, value, size);
10477}
10478
10479PyDoc_STRVAR(posix_fgetxattr__doc__,
10480"fgetxattr(fd, attr) -> value\n\n\
10481Like getxattr but operate on a fd instead of a path.");
10482
10483static PyObject *
10484posix_fgetxattr(PyObject *self, PyObject *args)
10485{
10486 PyObject *res, *name;
10487 int fd;
10488
10489 if (!PyArg_ParseTuple(args, "iO&:fgetxattr", &fd, PyUnicode_FSConverter, &name))
10490 return NULL;
10491 res = getxattr_common((const char *)(Py_uintptr_t)fd, name, wrap_fgetxattr);
10492 Py_DECREF(name);
10493 return res;
10494}
10495
10496PyDoc_STRVAR(posix_setxattr__doc__,
10497"setxattr(path, attr, value, flags=0)\n\n\
10498Set extended attribute *attr* on *path* to *value*.");
10499
10500static PyObject *
10501posix_setxattr(PyObject *self, PyObject *args)
10502{
10503 PyObject *path, *name;
10504 Py_buffer data;
10505 int flags = 0, err;
10506
10507 if (!PyArg_ParseTuple(args, "O&O&y*|i:setxattr", PyUnicode_FSConverter,
10508 &path, PyUnicode_FSConverter, &name, &data, &flags))
10509 return NULL;
10510 Py_BEGIN_ALLOW_THREADS;
10511 err = setxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10512 data.buf, data.len, flags);
10513 Py_END_ALLOW_THREADS;
10514 Py_DECREF(path);
10515 Py_DECREF(name);
10516 PyBuffer_Release(&data);
10517 if (err)
10518 return posix_error();
10519 Py_RETURN_NONE;
10520}
10521
10522PyDoc_STRVAR(posix_lsetxattr__doc__,
10523"lsetxattr(path, attr, value, flags=0)\n\n\
10524Like setxattr but don't follow symlinks.");
10525
10526static PyObject *
10527posix_lsetxattr(PyObject *self, PyObject *args)
10528{
10529 PyObject *path, *name;
10530 Py_buffer data;
10531 int flags = 0, err;
10532
10533 if (!PyArg_ParseTuple(args, "O&O&y*|i:lsetxattr", PyUnicode_FSConverter,
10534 &path, PyUnicode_FSConverter, &name, &data, &flags))
10535 return NULL;
10536 Py_BEGIN_ALLOW_THREADS;
10537 err = lsetxattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name),
10538 data.buf, data.len, flags);
10539 Py_END_ALLOW_THREADS;
10540 Py_DECREF(path);
10541 Py_DECREF(name);
10542 PyBuffer_Release(&data);
10543 if (err)
10544 return posix_error();
10545 Py_RETURN_NONE;
10546}
10547
10548PyDoc_STRVAR(posix_fsetxattr__doc__,
10549"fsetxattr(fd, attr, value, flags=0)\n\n\
10550Like setxattr but operates on *fd* instead of a path.");
10551
10552static PyObject *
10553posix_fsetxattr(PyObject *self, PyObject *args)
10554{
10555 Py_buffer data;
10556 const char *name;
10557 int fd, flags = 0, err;
10558
10559 if (!PyArg_ParseTuple(args, "iO&y*|i:fsetxattr", &fd, PyUnicode_FSConverter,
10560 &name, &data, &flags))
10561 return NULL;
10562 Py_BEGIN_ALLOW_THREADS;
10563 err = fsetxattr(fd, PyBytes_AS_STRING(name), data.buf, data.len, flags);
10564 Py_END_ALLOW_THREADS;
10565 Py_DECREF(name);
10566 PyBuffer_Release(&data);
10567 if (err)
10568 return posix_error();
10569 Py_RETURN_NONE;
10570}
10571
10572PyDoc_STRVAR(posix_removexattr__doc__,
10573"removexattr(path, attr)\n\n\
10574Remove extended attribute *attr* on *path*.");
10575
10576static PyObject *
10577posix_removexattr(PyObject *self, PyObject *args)
10578{
10579 PyObject *path, *name;
10580 int err;
10581
10582 if (!PyArg_ParseTuple(args, "O&O&:removexattr", PyUnicode_FSConverter, &path,
10583 PyUnicode_FSConverter, &name))
10584 return NULL;
10585 Py_BEGIN_ALLOW_THREADS;
10586 err = removexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10587 Py_END_ALLOW_THREADS;
10588 Py_DECREF(path);
10589 Py_DECREF(name);
10590 if (err)
10591 return posix_error();
10592 Py_RETURN_NONE;
10593}
10594
10595PyDoc_STRVAR(posix_lremovexattr__doc__,
10596"lremovexattr(path, attr)\n\n\
10597Like removexattr but don't follow symlinks.");
10598
10599static PyObject *
10600posix_lremovexattr(PyObject *self, PyObject *args)
10601{
10602 PyObject *path, *name;
10603 int err;
10604
10605 if (!PyArg_ParseTuple(args, "O&O&:lremovexattr", PyUnicode_FSConverter, &path,
10606 PyUnicode_FSConverter, &name))
10607 return NULL;
10608 Py_BEGIN_ALLOW_THREADS;
10609 err = lremovexattr(PyBytes_AS_STRING(path), PyBytes_AS_STRING(name));
10610 Py_END_ALLOW_THREADS;
10611 Py_DECREF(path);
10612 Py_DECREF(name);
10613 if (err)
10614 return posix_error();
10615 Py_RETURN_NONE;
10616}
10617
10618PyDoc_STRVAR(posix_fremovexattr__doc__,
10619"fremovexattr(fd, attr)\n\n\
10620Like removexattr but operates on a file descriptor.");
10621
10622static PyObject *
10623posix_fremovexattr(PyObject *self, PyObject *args)
10624{
10625 PyObject *name;
10626 int fd, err;
10627
10628 if (!PyArg_ParseTuple(args, "iO&:fremovexattr", &fd,
10629 PyUnicode_FSConverter, &name))
10630 return NULL;
10631 Py_BEGIN_ALLOW_THREADS;
10632 err = fremovexattr(fd, PyBytes_AS_STRING(name));
10633 Py_END_ALLOW_THREADS;
10634 Py_DECREF(name);
10635 if (err)
10636 return posix_error();
10637 Py_RETURN_NONE;
10638}
10639
10640static Py_ssize_t
10641try_listxattr(const char *path, ssize_t (*list)(const char *, char *, size_t),
10642 Py_ssize_t buf_size, char **buf)
10643{
10644 Py_ssize_t len;
10645
10646 *buf = PyMem_MALLOC(buf_size);
10647 if (!*buf) {
10648 PyErr_NoMemory();
10649 return -1;
10650 }
10651 Py_BEGIN_ALLOW_THREADS;
10652 len = list(path, *buf, buf_size);
10653 Py_END_ALLOW_THREADS;
10654 if (len < 0) {
10655 PyMem_FREE(*buf);
10656 if (errno != ERANGE)
10657 posix_error();
10658 return -1;
10659 }
10660 return len;
10661}
10662
10663static PyObject *
10664listxattr_common(const char *path, ssize_t (*list)(const char *, char *, size_t))
10665{
10666 PyObject *res, *attr;
10667 Py_ssize_t len, err, start, i;
10668 char *buf;
10669
10670 len = try_listxattr(path, list, 256, &buf);
10671 if (len < 0) {
10672 if (PyErr_Occurred())
10673 return NULL;
10674 len = try_listxattr(path, list, XATTR_LIST_MAX, &buf);
10675 if (len < 0)
10676 return NULL;
10677 }
10678 res = PyList_New(0);
10679 if (!res) {
10680 PyMem_FREE(buf);
10681 return NULL;
10682 }
10683 for (start = i = 0; i < len; i++) {
10684 if (!buf[i]) {
10685 attr = PyUnicode_DecodeFSDefaultAndSize(&buf[start], i - start);
10686 if (!attr) {
10687 Py_DECREF(res);
10688 PyMem_FREE(buf);
10689 return NULL;
10690 }
10691 err = PyList_Append(res, attr);
10692 Py_DECREF(attr);
10693 if (err) {
10694 Py_DECREF(res);
10695 PyMem_FREE(buf);
10696 return NULL;
10697 }
10698 start = i + 1;
10699 }
10700 }
10701 PyMem_FREE(buf);
10702 return res;
10703}
10704
10705PyDoc_STRVAR(posix_listxattr__doc__,
10706"listxattr(path)\n\n\
10707Return a list of extended attributes on *path*.");
10708
10709static PyObject *
10710posix_listxattr(PyObject *self, PyObject *args)
10711{
10712 PyObject *path, *res;
10713
10714 if (!PyArg_ParseTuple(args, "O&:listxattr", PyUnicode_FSConverter, &path))
10715 return NULL;
10716 res = listxattr_common(PyBytes_AS_STRING(path), listxattr);
10717 Py_DECREF(path);
10718 return res;
10719}
10720
10721PyDoc_STRVAR(posix_llistxattr__doc__,
10722"llistxattr(path)\n\n\
10723Like listxattr but don't follow symlinks..");
10724
10725static PyObject *
10726posix_llistxattr(PyObject *self, PyObject *args)
10727{
10728 PyObject *path, *res;
10729
10730 if (!PyArg_ParseTuple(args, "O&:llistxattr", PyUnicode_FSConverter, &path))
10731 return NULL;
10732 res = listxattr_common(PyBytes_AS_STRING(path), llistxattr);
10733 Py_DECREF(path);
10734 return res;
10735}
10736
10737static ssize_t
10738wrap_flistxattr(const char *path, char *buf, size_t len)
10739{
10740 /* Hack to share code. */
10741 return flistxattr((int)(Py_uintptr_t)path, buf, len);
10742}
10743
10744PyDoc_STRVAR(posix_flistxattr__doc__,
10745"flistxattr(path)\n\n\
10746Like flistxattr but operates on a file descriptor.");
10747
10748static PyObject *
10749posix_flistxattr(PyObject *self, PyObject *args)
10750{
10751 long fd;
10752
10753 if (!PyArg_ParseTuple(args, "i:flistxattr", &fd))
10754 return NULL;
10755 return listxattr_common((const char *)(Py_uintptr_t)fd, wrap_flistxattr);
10756}
10757
Benjamin Peterson9428d532011-09-14 11:45:52 -040010758#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010759
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010760
Georg Brandl2fb477c2012-02-21 00:33:36 +010010761PyDoc_STRVAR(posix_urandom__doc__,
10762"urandom(n) -> str\n\n\
10763Return n random bytes suitable for cryptographic use.");
10764
10765static PyObject *
10766posix_urandom(PyObject *self, PyObject *args)
10767{
10768 Py_ssize_t size;
10769 PyObject *result;
10770 int ret;
10771
10772 /* Read arguments */
10773 if (!PyArg_ParseTuple(args, "n:urandom", &size))
10774 return NULL;
10775 if (size < 0)
10776 return PyErr_Format(PyExc_ValueError,
10777 "negative argument not allowed");
10778 result = PyBytes_FromStringAndSize(NULL, size);
10779 if (result == NULL)
10780 return NULL;
10781
10782 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
10783 PyBytes_GET_SIZE(result));
10784 if (ret == -1) {
10785 Py_DECREF(result);
10786 return NULL;
10787 }
10788 return result;
10789}
10790
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010791/* Terminal size querying */
10792
10793static PyTypeObject TerminalSizeType;
10794
10795PyDoc_STRVAR(TerminalSize_docstring,
10796 "A tuple of (columns, lines) for holding terminal window size");
10797
10798static PyStructSequence_Field TerminalSize_fields[] = {
10799 {"columns", "width of the terminal window in characters"},
10800 {"lines", "height of the terminal window in characters"},
10801 {NULL, NULL}
10802};
10803
10804static PyStructSequence_Desc TerminalSize_desc = {
10805 "os.terminal_size",
10806 TerminalSize_docstring,
10807 TerminalSize_fields,
10808 2,
10809};
10810
10811#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10812PyDoc_STRVAR(termsize__doc__,
10813 "Return the size of the terminal window as (columns, lines).\n" \
10814 "\n" \
10815 "The optional argument fd (default standard output) specifies\n" \
10816 "which file descriptor should be queried.\n" \
10817 "\n" \
10818 "If the file descriptor is not connected to a terminal, an OSError\n" \
10819 "is thrown.\n" \
10820 "\n" \
10821 "This function will only be defined if an implementation is\n" \
10822 "available for this system.\n" \
10823 "\n" \
10824 "shutil.get_terminal_size is the high-level function which should \n" \
10825 "normally be used, os.get_terminal_size is the low-level implementation.");
10826
10827static PyObject*
10828get_terminal_size(PyObject *self, PyObject *args)
10829{
10830 int columns, lines;
10831 PyObject *termsize;
10832
10833 int fd = fileno(stdout);
10834 /* Under some conditions stdout may not be connected and
10835 * fileno(stdout) may point to an invalid file descriptor. For example
10836 * GUI apps don't have valid standard streams by default.
10837 *
10838 * If this happens, and the optional fd argument is not present,
10839 * the ioctl below will fail returning EBADF. This is what we want.
10840 */
10841
10842 if (!PyArg_ParseTuple(args, "|i", &fd))
10843 return NULL;
10844
10845#ifdef TERMSIZE_USE_IOCTL
10846 {
10847 struct winsize w;
10848 if (ioctl(fd, TIOCGWINSZ, &w))
10849 return PyErr_SetFromErrno(PyExc_OSError);
10850 columns = w.ws_col;
10851 lines = w.ws_row;
10852 }
10853#endif /* TERMSIZE_USE_IOCTL */
10854
10855#ifdef TERMSIZE_USE_CONIO
10856 {
10857 DWORD nhandle;
10858 HANDLE handle;
10859 CONSOLE_SCREEN_BUFFER_INFO csbi;
10860 switch (fd) {
10861 case 0: nhandle = STD_INPUT_HANDLE;
10862 break;
10863 case 1: nhandle = STD_OUTPUT_HANDLE;
10864 break;
10865 case 2: nhandle = STD_ERROR_HANDLE;
10866 break;
10867 default:
10868 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10869 }
10870 handle = GetStdHandle(nhandle);
10871 if (handle == NULL)
10872 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10873 if (handle == INVALID_HANDLE_VALUE)
10874 return PyErr_SetFromWindowsErr(0);
10875
10876 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10877 return PyErr_SetFromWindowsErr(0);
10878
10879 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10880 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10881 }
10882#endif /* TERMSIZE_USE_CONIO */
10883
10884 termsize = PyStructSequence_New(&TerminalSizeType);
10885 if (termsize == NULL)
10886 return NULL;
10887 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10888 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10889 if (PyErr_Occurred()) {
10890 Py_DECREF(termsize);
10891 return NULL;
10892 }
10893 return termsize;
10894}
10895#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10896
10897
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010898static PyMethodDef posix_methods[] = {
Larry Hastingscfe6f2a2012-05-05 17:39:09 -070010899 {"access", (PyCFunction)posix_access,
10900 METH_VARARGS | METH_KEYWORDS,
10901 posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010902#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010903 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010904#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010905 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010906#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010907 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010908#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +000010909 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010910#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010911 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010912#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010913#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010914 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010915#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010916#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010917 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010918#endif /* HAVE_LCHMOD */
10919#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010920 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010921#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010922#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010923 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010924#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010925#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010926 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010927#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010928#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010929 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010930#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010931#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010932 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010933#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010934#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10936 METH_NOARGS, posix_getcwd__doc__},
10937 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10938 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010939#endif
10940#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010941 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010942#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010944#ifdef HAVE_FDOPENDIR
Charles-François Natali77940902012-02-06 19:54:48 +010010945 {"flistdir", posix_flistdir, METH_VARARGS, posix_flistdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +000010946#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010947 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010948 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010949#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010950 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010951#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010952#ifdef HAVE_GETPRIORITY
10953 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10954#endif /* HAVE_GETPRIORITY */
10955#ifdef HAVE_SETPRIORITY
10956 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10957#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010958#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +000010959 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010960#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010961#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010962 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010963#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010964 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
Antoine Pitrouf3b2d882012-01-30 22:08:52 +010010965 {"replace", posix_replace, METH_VARARGS, posix_replace__doc__},
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +000010966 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
Victor Stinner4195b5c2012-02-08 23:03:19 +010010967 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010968 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +000010969#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010971#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010972#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010973 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010974 win_symlink__doc__},
10975#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010976#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010977 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010978#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010980#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010981 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010982#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010983 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10984 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
Larry Hastings76ad59b2012-05-03 00:30:07 -070010985 {"utime", (PyCFunction)posix_utime,
10986 METH_VARARGS | METH_KEYWORDS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010987#ifdef HAVE_FUTIMES
Larry Hastings76ad59b2012-05-03 00:30:07 -070010988 {"futimes", (PyCFunction)posix_futimes,
10989 METH_VARARGS | METH_KEYWORDS, posix_futimes__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010990#endif
10991#ifdef HAVE_LUTIMES
Larry Hastings76ad59b2012-05-03 00:30:07 -070010992 {"lutimes", (PyCFunction)posix_lutimes,
10993 METH_VARARGS | METH_KEYWORDS, posix_lutimes__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010994#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010995#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010996 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010997#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010999#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
11001 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000011002#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020011003#ifdef HAVE_FEXECVE
11004 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
11005#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000011006#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000011007 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
11008 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000011009#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011010 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
11011 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000011012#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000011013#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000011014#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000011015 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000011016#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011017#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011019#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011020#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020011021#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011022 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
11023 {"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 +020011024#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050011025#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011026 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050011027#endif
11028#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011029 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050011030#endif
11031#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011032 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050011033#endif
11034#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011035 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050011036#endif
11037#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011038 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050011039#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011040 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050011041#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011042 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
11043 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
11044#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020011045#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000011046#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000011047 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000011048#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000011049#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011050 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000011051#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000011052#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011053 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000011054#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011055#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011056 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000011057#endif /* HAVE_GETEUID */
11058#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011059 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011060#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020011061#ifdef HAVE_GETGROUPLIST
11062 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
11063#endif
Fred Drakec9680921999-12-13 16:37:25 +000011064#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000011065 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011066#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000011068#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011070#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000011071#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000011073#endif /* HAVE_GETPPID */
11074#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000011076#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000011077#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000011079#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000011080#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000011082#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000011083#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000011085#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000011086#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000011088#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000011089#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
11091 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000011092 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000011093#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000011094#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011095 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011096#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000011097#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011098 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000011099#endif /* HAVE_SETEUID */
11100#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011101 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000011102#endif /* HAVE_SETEGID */
11103#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011104 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000011105#endif /* HAVE_SETREUID */
11106#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011107 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000011108#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011109#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011110 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011111#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000011112#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000011113 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000011114#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000011115#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000011116 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000011117#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000011118#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011119 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000011120#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011121#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000011122 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011123#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000011124#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011125 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000011126#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011127#ifdef HAVE_WAIT3
Victor Stinner4195b5c2012-02-08 23:03:19 +010011128 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011129#endif /* HAVE_WAIT3 */
11130#ifdef HAVE_WAIT4
Victor Stinner4195b5c2012-02-08 23:03:19 +010011131 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011132#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020011133#if defined(HAVE_WAITID) && !defined(__APPLE__)
11134 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
11135#endif
Tim Petersab034fa2002-02-01 11:27:43 +000011136#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000011137 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011138#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000011139#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000011140 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000011141#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011142#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000011143 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011144#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011145#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011146 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011147#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011148#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000011149 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011150#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000011151#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000011152 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000011153#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000011154 {"open", posix_open, METH_VARARGS, posix_open__doc__},
11155 {"close", posix_close, METH_VARARGS, posix_close__doc__},
11156 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
11157 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
11158 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
11159 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020011160#ifdef HAVE_LOCKF
11161 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
11162#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011163 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
11164 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020011165#ifdef HAVE_READV
11166 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
11167#endif
11168#ifdef HAVE_PREAD
11169 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
11170#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011171 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020011172#ifdef HAVE_WRITEV
11173 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
11174#endif
11175#ifdef HAVE_PWRITE
11176 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
11177#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011178#ifdef HAVE_SENDFILE
11179 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
11180 posix_sendfile__doc__},
11181#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010011182 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000011183 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011184#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011185 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011186#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020011187#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020011188 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020011189#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011190#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000011191 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011192#endif
Neal Norwitz11690112002-07-30 01:08:28 +000011193#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000011194 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000011195#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000011196#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000011197 {"major", posix_major, METH_VARARGS, posix_major__doc__},
11198 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
11199 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000011200#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011201#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000011203#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020011204#ifdef HAVE_TRUNCATE
11205 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
11206#endif
11207#ifdef HAVE_POSIX_FALLOCATE
11208 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
11209#endif
11210#ifdef HAVE_POSIX_FADVISE
11211 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
11212#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000011213#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011214 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000011215#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000011216#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011217 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000011218#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000011220#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000011221 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000011222#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000011223#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011224 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000011225#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020011226#ifdef HAVE_SYNC
11227 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
11228#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000011229#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011230 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000011231#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000011232#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000011233#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000011235#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000011236#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000011238#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000011239#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011241#endif /* WIFSTOPPED */
11242#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011244#endif /* WIFSIGNALED */
11245#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011247#endif /* WIFEXITED */
11248#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011250#endif /* WEXITSTATUS */
11251#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011253#endif /* WTERMSIG */
11254#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000011256#endif /* WSTOPSIG */
11257#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011258#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000011259 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000011260#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000011261#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000011263#endif
Fred Drakec9680921999-12-13 16:37:25 +000011264#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000011265 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011266#endif
11267#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011268 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011269#endif
11270#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011271 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011272#endif
11273#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011274 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000011275#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011277#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000011278 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000011279 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000011280 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050011281 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011282 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000011283#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000011284#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000011286#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010011287 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011288#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011289 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011290#endif
11291#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011292 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011293#endif
11294#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000011295 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011296#endif
11297#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000011298 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011299#endif
11300
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011301/* posix *at family of functions */
11302#ifdef HAVE_FACCESSAT
11303 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
11304#endif
11305#ifdef HAVE_FCHMODAT
11306 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
11307#endif /* HAVE_FCHMODAT */
11308#ifdef HAVE_FCHOWNAT
11309 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
11310#endif /* HAVE_FCHOWNAT */
11311#ifdef HAVE_FSTATAT
Victor Stinner4195b5c2012-02-08 23:03:19 +010011312 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011313#endif
11314#ifdef HAVE_FUTIMESAT
11315 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
11316#endif
11317#ifdef HAVE_LINKAT
11318 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
11319#endif /* HAVE_LINKAT */
11320#ifdef HAVE_MKDIRAT
11321 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
11322#endif
11323#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
11324 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
11325#endif
11326#ifdef HAVE_OPENAT
11327 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
11328#endif
11329#ifdef HAVE_READLINKAT
11330 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
11331#endif /* HAVE_READLINKAT */
11332#ifdef HAVE_RENAMEAT
11333 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
11334#endif
11335#if HAVE_SYMLINKAT
11336 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
11337#endif /* HAVE_SYMLINKAT */
11338#ifdef HAVE_UNLINKAT
11339 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
11340#endif
11341#ifdef HAVE_UTIMENSAT
Jesus Cead03a4912011-11-08 17:28:04 +010011342 {"utimensat", (PyCFunction)posix_utimensat,
11343 METH_VARARGS | METH_KEYWORDS,
Brian Curtin569b4942011-11-07 16:09:20 -060011344 posix_utimensat__doc__},
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011345#endif
11346#ifdef HAVE_MKFIFOAT
11347 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
11348#endif
Benjamin Peterson9428d532011-09-14 11:45:52 -040011349#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011350 {"setxattr", posix_setxattr, METH_VARARGS, posix_setxattr__doc__},
11351 {"lsetxattr", posix_lsetxattr, METH_VARARGS, posix_lsetxattr__doc__},
11352 {"fsetxattr", posix_fsetxattr, METH_VARARGS, posix_fsetxattr__doc__},
11353 {"getxattr", posix_getxattr, METH_VARARGS, posix_getxattr__doc__},
11354 {"lgetxattr", posix_lgetxattr, METH_VARARGS, posix_lgetxattr__doc__},
11355 {"fgetxattr", posix_fgetxattr, METH_VARARGS, posix_fgetxattr__doc__},
11356 {"removexattr", posix_removexattr, METH_VARARGS, posix_removexattr__doc__},
11357 {"lremovexattr", posix_lremovexattr, METH_VARARGS, posix_lremovexattr__doc__},
11358 {"fremovexattr", posix_fremovexattr, METH_VARARGS, posix_fremovexattr__doc__},
11359 {"listxattr", posix_listxattr, METH_VARARGS, posix_listxattr__doc__},
11360 {"llistxattr", posix_llistxattr, METH_VARARGS, posix_llistxattr__doc__},
11361 {"flistxattr", posix_flistxattr, METH_VARARGS, posix_flistxattr__doc__},
11362#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011363#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
11364 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
11365#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011366 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011367};
11368
11369
Barry Warsaw4a342091996-12-19 23:50:02 +000011370static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011371ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000011372{
Victor Stinner8c62be82010-05-06 00:08:46 +000011373 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000011374}
11375
Guido van Rossumd48f2521997-12-05 22:19:34 +000011376#if defined(PYOS_OS2)
11377/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011378static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011379{
11380 APIRET rc;
11381 ULONG values[QSV_MAX+1];
11382 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000011383 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000011384
11385 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000011386 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011387 Py_END_ALLOW_THREADS
11388
11389 if (rc != NO_ERROR) {
11390 os2_error(rc);
11391 return -1;
11392 }
11393
Fred Drake4d1e64b2002-04-15 19:40:07 +000011394 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
11395 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
11396 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
11397 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
11398 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
11399 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
11400 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011401
11402 switch (values[QSV_VERSION_MINOR]) {
11403 case 0: ver = "2.00"; break;
11404 case 10: ver = "2.10"; break;
11405 case 11: ver = "2.11"; break;
11406 case 30: ver = "3.00"; break;
11407 case 40: ver = "4.00"; break;
11408 case 50: ver = "5.00"; break;
11409 default:
Tim Peters885d4572001-11-28 20:27:42 +000011410 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000011412 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011413 ver = &tmp[0];
11414 }
11415
11416 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000011417 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000011418 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011419
11420 /* Add Indicator of Which Drive was Used to Boot the System */
11421 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
11422 tmp[1] = ':';
11423 tmp[2] = '\0';
11424
Fred Drake4d1e64b2002-04-15 19:40:07 +000011425 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000011426}
11427#endif
11428
Brian Curtin52173d42010-12-02 18:29:18 +000011429#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011430static int
Brian Curtin52173d42010-12-02 18:29:18 +000011431enable_symlink()
11432{
11433 HANDLE tok;
11434 TOKEN_PRIVILEGES tok_priv;
11435 LUID luid;
11436 int meth_idx = 0;
11437
11438 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011439 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011440
11441 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011442 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011443
11444 tok_priv.PrivilegeCount = 1;
11445 tok_priv.Privileges[0].Luid = luid;
11446 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
11447
11448 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
11449 sizeof(TOKEN_PRIVILEGES),
11450 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000011451 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000011452
Brian Curtin3b4499c2010-12-28 14:31:47 +000011453 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
11454 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000011455}
11456#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
11457
Barry Warsaw4a342091996-12-19 23:50:02 +000011458static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011459all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000011460{
Guido van Rossum94f6f721999-01-06 18:42:14 +000011461#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011462 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011463#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011464#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011465 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011466#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011467#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011468 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011469#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011470#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011471 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011472#endif
Fred Drakec9680921999-12-13 16:37:25 +000011473#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011474 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000011475#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011476#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011477 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011478#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011479#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000011480 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011481#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011482#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000011483 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011484#endif
Fred Drake106c1a02002-04-23 15:58:02 +000011485#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000011486 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000011487#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000011488#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011489 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011490#endif
11491#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000011492 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011493#endif
11494#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000011495 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011496#endif
11497#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000011498 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011499#endif
11500#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011501 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011502#endif
11503#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000011504 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011505#endif
11506#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011507 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011508#endif
11509#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011510 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011511#endif
11512#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011513 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011514#endif
11515#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000011516 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011517#endif
11518#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011519 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011520#endif
11521#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000011522 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011523#endif
11524#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011525 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011526#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011527#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011528 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011529#endif
11530#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000011531 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011532#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011533#ifdef O_XATTR
11534 if (ins(d, "O_XATTR", (long)O_XATTR)) return -1;
11535#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011536#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011537 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011538#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011539#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011540 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011541#endif
11542#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011543 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011544#endif
Jesus Ceacf381202012-04-24 20:44:40 +020011545#ifdef O_EXEC
11546 if (ins(d, "O_EXEC", (long)O_EXEC)) return -1;
11547#endif
11548#ifdef O_SEARCH
11549 if (ins(d, "O_SEARCH", (long)O_SEARCH)) return -1;
11550#endif
11551#ifdef O_TTY_INIT
11552 if (ins(d, "O_TTY_INIT", (long)O_TTY_INIT)) return -1;
11553#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011554#ifdef PRIO_PROCESS
11555 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
11556#endif
11557#ifdef PRIO_PGRP
11558 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
11559#endif
11560#ifdef PRIO_USER
11561 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
11562#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011563#ifdef O_CLOEXEC
11564 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
11565#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011566#ifdef O_ACCMODE
11567 if (ins(d, "O_ACCMODE", (long)O_ACCMODE)) return -1;
11568#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000011569/* posix - constants for *at functions */
11570#ifdef AT_SYMLINK_NOFOLLOW
11571 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
11572#endif
11573#ifdef AT_EACCESS
11574 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
11575#endif
11576#ifdef AT_FDCWD
11577 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
11578#endif
11579#ifdef AT_REMOVEDIR
11580 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
11581#endif
11582#ifdef AT_SYMLINK_FOLLOW
11583 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
11584#endif
11585#ifdef UTIME_NOW
11586 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11587#endif
11588#ifdef UTIME_OMIT
11589 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11590#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011591
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011592
Tim Peters5aa91602002-01-30 05:46:57 +000011593/* MS Windows */
11594#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011595 /* Don't inherit in child processes. */
11596 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011597#endif
11598#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011599 /* Optimize for short life (keep in memory). */
11600 /* MS forgot to define this one with a non-underscore form too. */
11601 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011602#endif
11603#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011604 /* Automatically delete when last handle is closed. */
11605 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011606#endif
11607#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011608 /* Optimize for random access. */
11609 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011610#endif
11611#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011612 /* Optimize for sequential access. */
11613 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011614#endif
11615
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011616/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011617#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011618 /* Send a SIGIO signal whenever input or output
11619 becomes available on file descriptor */
11620 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011621#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011622#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011623 /* Direct disk access. */
11624 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011625#endif
11626#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011627 /* Must be a directory. */
11628 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011629#endif
11630#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011631 /* Do not follow links. */
11632 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011633#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011634#ifdef O_NOLINKS
11635 /* Fails if link count of the named file is greater than 1 */
11636 if (ins(d, "O_NOLINKS", (long)O_NOLINKS)) return -1;
11637#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011638#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011639 /* Do not update the access time. */
11640 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011641#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011642
Victor Stinner8c62be82010-05-06 00:08:46 +000011643 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011644#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000011645 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011646#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011647#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011648 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011649#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011650#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011651 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011652#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011653#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011654 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011655#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011656#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000011657 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011658#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011659#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000011660 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011661#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011662#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011663 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011664#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011665#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000011666 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011667#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011668#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011669 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011670#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011671#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000011672 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011673#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011674#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000011675 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011676#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011677#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000011678 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011679#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011680#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000011681 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011682#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011683#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000011684 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011685#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011686#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011687 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011688#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011689#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011690 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011691#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011692#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000011693 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011694#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011695
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011696 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011697#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011698 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011699#endif /* ST_RDONLY */
11700#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011701 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011702#endif /* ST_NOSUID */
11703
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011704 /* FreeBSD sendfile() constants */
11705#ifdef SF_NODISKIO
11706 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
11707#endif
11708#ifdef SF_MNOWAIT
11709 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
11710#endif
11711#ifdef SF_SYNC
11712 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
11713#endif
11714
Ross Lagerwall7807c352011-03-17 20:20:30 +020011715 /* constants for posix_fadvise */
11716#ifdef POSIX_FADV_NORMAL
11717 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
11718#endif
11719#ifdef POSIX_FADV_SEQUENTIAL
11720 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
11721#endif
11722#ifdef POSIX_FADV_RANDOM
11723 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
11724#endif
11725#ifdef POSIX_FADV_NOREUSE
11726 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
11727#endif
11728#ifdef POSIX_FADV_WILLNEED
11729 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
11730#endif
11731#ifdef POSIX_FADV_DONTNEED
11732 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
11733#endif
11734
11735 /* constants for waitid */
11736#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
11737 if (ins(d, "P_PID", (long)P_PID)) return -1;
11738 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
11739 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
11740#endif
11741#ifdef WEXITED
11742 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
11743#endif
11744#ifdef WNOWAIT
11745 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
11746#endif
11747#ifdef WSTOPPED
11748 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
11749#endif
11750#ifdef CLD_EXITED
11751 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
11752#endif
11753#ifdef CLD_DUMPED
11754 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
11755#endif
11756#ifdef CLD_TRAPPED
11757 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
11758#endif
11759#ifdef CLD_CONTINUED
11760 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
11761#endif
11762
11763 /* constants for lockf */
11764#ifdef F_LOCK
11765 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
11766#endif
11767#ifdef F_TLOCK
11768 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
11769#endif
11770#ifdef F_ULOCK
11771 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
11772#endif
11773#ifdef F_TEST
11774 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
11775#endif
11776
11777 /* constants for futimens */
11778#ifdef UTIME_NOW
11779 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
11780#endif
11781#ifdef UTIME_OMIT
11782 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
11783#endif
11784
Guido van Rossum246bc171999-02-01 23:54:31 +000011785#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011786#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000011787 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
11788 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
11789 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
11790 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
11791 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
11792 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
11793 if (ins(d, "P_PM", (long)P_PM)) return -1;
11794 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
11795 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
11796 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
11797 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
11798 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
11799 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
11800 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
11801 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
11802 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
11803 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
11804 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
11805 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
11806 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011807#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011808 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
11809 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
11810 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
11811 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
11812 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011813#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000011814#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000011815
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011816#ifdef HAVE_SCHED_H
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011817 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011818 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
11819 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
11820#ifdef SCHED_SPORADIC
11821 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
11822#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011823#ifdef SCHED_BATCH
11824 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
11825#endif
11826#ifdef SCHED_IDLE
11827 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
11828#endif
11829#ifdef SCHED_RESET_ON_FORK
11830 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
11831#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011832#ifdef SCHED_SYS
11833 if (ins(d, "SCHED_SYS", (long)SCHED_SYS)) return -1;
11834#endif
11835#ifdef SCHED_IA
11836 if (ins(d, "SCHED_IA", (long)SCHED_IA)) return -1;
11837#endif
11838#ifdef SCHED_FSS
11839 if (ins(d, "SCHED_FSS", (long)SCHED_FSS)) return -1;
11840#endif
11841#ifdef SCHED_FX
11842 if (ins(d, "SCHED_FX", (long)SCHED_FSS)) return -1;
11843#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011844#endif
11845
Benjamin Peterson9428d532011-09-14 11:45:52 -040011846#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -040011847 if (ins(d, "XATTR_CREATE", (long)XATTR_CREATE)) return -1;
11848 if (ins(d, "XATTR_REPLACE", (long)XATTR_REPLACE)) return -1;
11849 if (ins(d, "XATTR_SIZE_MAX", (long)XATTR_SIZE_MAX)) return -1;
11850#endif
11851
Victor Stinner8b905bd2011-10-25 13:34:04 +020011852#ifdef RTLD_LAZY
11853 if (PyModule_AddIntMacro(d, RTLD_LAZY)) return -1;
11854#endif
11855#ifdef RTLD_NOW
11856 if (PyModule_AddIntMacro(d, RTLD_NOW)) return -1;
11857#endif
11858#ifdef RTLD_GLOBAL
11859 if (PyModule_AddIntMacro(d, RTLD_GLOBAL)) return -1;
11860#endif
11861#ifdef RTLD_LOCAL
11862 if (PyModule_AddIntMacro(d, RTLD_LOCAL)) return -1;
11863#endif
11864#ifdef RTLD_NODELETE
11865 if (PyModule_AddIntMacro(d, RTLD_NODELETE)) return -1;
11866#endif
11867#ifdef RTLD_NOLOAD
11868 if (PyModule_AddIntMacro(d, RTLD_NOLOAD)) return -1;
11869#endif
11870#ifdef RTLD_DEEPBIND
11871 if (PyModule_AddIntMacro(d, RTLD_DEEPBIND)) return -1;
11872#endif
11873
Guido van Rossumd48f2521997-12-05 22:19:34 +000011874#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000011875 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000011876#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011877 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011878}
11879
11880
Tim Peters5aa91602002-01-30 05:46:57 +000011881#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011882#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011883#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011884
11885#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011886#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011887#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011888
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011889#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011890#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011891#define MODNAME "posix"
11892#endif
11893
Martin v. Löwis1a214512008-06-11 05:26:20 +000011894static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011895 PyModuleDef_HEAD_INIT,
11896 MODNAME,
11897 posix__doc__,
11898 -1,
11899 posix_methods,
11900 NULL,
11901 NULL,
11902 NULL,
11903 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011904};
11905
11906
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011907PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011908INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011909{
Victor Stinner8c62be82010-05-06 00:08:46 +000011910 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000011911
Brian Curtin52173d42010-12-02 18:29:18 +000011912#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011913 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011914#endif
11915
Victor Stinner8c62be82010-05-06 00:08:46 +000011916 m = PyModule_Create(&posixmodule);
11917 if (m == NULL)
11918 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011919
Victor Stinner8c62be82010-05-06 00:08:46 +000011920 /* Initialize environ dictionary */
11921 v = convertenviron();
11922 Py_XINCREF(v);
11923 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11924 return NULL;
11925 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011926
Victor Stinner8c62be82010-05-06 00:08:46 +000011927 if (all_ins(m))
11928 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011929
Victor Stinner8c62be82010-05-06 00:08:46 +000011930 if (setup_confname_tables(m))
11931 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011932
Victor Stinner8c62be82010-05-06 00:08:46 +000011933 Py_INCREF(PyExc_OSError);
11934 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011935
Benjamin Peterson2740af82011-08-02 17:41:34 -050011936#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011937 if (PyType_Ready(&cpu_set_type) < 0)
11938 return NULL;
11939 Py_INCREF(&cpu_set_type);
11940 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
Benjamin Peterson2740af82011-08-02 17:41:34 -050011941#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011942
Guido van Rossumb3d39562000-01-31 18:41:26 +000011943#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011944 if (posix_putenv_garbage == NULL)
11945 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011946#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011947
Victor Stinner8c62be82010-05-06 00:08:46 +000011948 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011949#if defined(HAVE_WAITID) && !defined(__APPLE__)
11950 waitid_result_desc.name = MODNAME ".waitid_result";
11951 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11952#endif
11953
Victor Stinner8c62be82010-05-06 00:08:46 +000011954 stat_result_desc.name = MODNAME ".stat_result";
11955 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11956 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11957 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11958 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11959 structseq_new = StatResultType.tp_new;
11960 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011961
Victor Stinner8c62be82010-05-06 00:08:46 +000011962 statvfs_result_desc.name = MODNAME ".statvfs_result";
11963 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011964#ifdef NEED_TICKS_PER_SECOND
11965# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011966 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011967# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011968 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011969# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011970 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011971# endif
11972#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011973
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011974#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011975 sched_param_desc.name = MODNAME ".sched_param";
11976 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11977 SchedParamType.tp_new = sched_param_new;
11978#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011979
11980 /* initialize TerminalSize_info */
11981 PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
11982 Py_INCREF(&TerminalSizeType);
Victor Stinner8c62be82010-05-06 00:08:46 +000011983 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011984#if defined(HAVE_WAITID) && !defined(__APPLE__)
11985 Py_INCREF((PyObject*) &WaitidResultType);
11986 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11987#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011988 Py_INCREF((PyObject*) &StatResultType);
11989 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11990 Py_INCREF((PyObject*) &StatVFSResultType);
11991 PyModule_AddObject(m, "statvfs_result",
11992 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011993
11994#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011995 Py_INCREF(&SchedParamType);
11996 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011997#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011998 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011999
12000#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000012001 /*
12002 * Step 2 of weak-linking support on Mac OS X.
12003 *
12004 * The code below removes functions that are not available on the
12005 * currently active platform.
12006 *
12007 * This block allow one to use a python binary that was build on
12008 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
12009 * OSX 10.4.
12010 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000012011#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000012012 if (fstatvfs == NULL) {
12013 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
12014 return NULL;
12015 }
12016 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000012017#endif /* HAVE_FSTATVFS */
12018
12019#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000012020 if (statvfs == NULL) {
12021 if (PyObject_DelAttrString(m, "statvfs") == -1) {
12022 return NULL;
12023 }
12024 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000012025#endif /* HAVE_STATVFS */
12026
12027# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000012028 if (lchown == NULL) {
12029 if (PyObject_DelAttrString(m, "lchown") == -1) {
12030 return NULL;
12031 }
12032 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000012033#endif /* HAVE_LCHOWN */
12034
12035
12036#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012037
12038 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
12039
Larry Hastings6fe20b32012-04-19 15:07:49 -070012040 billion = PyLong_FromLong(1000000000);
12041 if (!billion)
12042 return NULL;
12043
Victor Stinner8c62be82010-05-06 00:08:46 +000012044 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000012045
Guido van Rossumb6775db1994-08-01 11:34:53 +000012046}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012047
12048#ifdef __cplusplus
12049}
12050#endif