blob: cc908e2a13ac0a32c808fce3507c17e240546d2c [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
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#endif /* defined(__VMS) */
35
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000041"This module provides access to operating system functionality that is\n\
42standardized by the C Standard and the POSIX standard (a thinly\n\
43disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000046
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#if defined(PYOS_OS2)
48#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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000071#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000075#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef HAVE_FCNTL_H
79#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000080#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000081
Guido van Rossuma6535fd2001-10-18 19:44:10 +000082#ifdef HAVE_GRP_H
83#include <grp.h>
84#endif
85
Barry Warsaw5676bd12003-01-07 20:57:09 +000086#ifdef HAVE_SYSEXITS_H
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
Anthony Baxter8a560de2004-10-13 15:30:56 +000090#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000094#ifdef HAVE_LANGINFO_H
95#include <langinfo.h>
96#endif
97
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000098#ifdef HAVE_SYS_SENDFILE_H
99#include <sys/sendfile.h>
100#endif
101
102#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
103#ifdef HAVE_SYS_SOCKET_H
104#include <sys/socket.h>
105#endif
106
107#ifdef HAVE_SYS_UIO_H
108#include <sys/uio.h>
109#endif
110#endif
111
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000113/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000114#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000115#include <process.h>
116#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000117#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000118#define HAVE_GETCWD 1
119#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000120#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000121#if defined(__OS2__)
122#define HAVE_EXECV 1
123#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000124#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#include <process.h>
126#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000127#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000128#define HAVE_EXECV 1
129#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#define HAVE_OPENDIR 1
131#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000132#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000133#define HAVE_WAIT 1
134#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000136#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000137#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000138#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140#define HAVE_EXECV 1
141#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000142#define HAVE_SYSTEM 1
143#define HAVE_CWAIT 1
144#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000145#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000146#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000147#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
148/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000149#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000150/* Unix functions that the configure script doesn't check for */
151#define HAVE_EXECV 1
152#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000154#define HAVE_FORK1 1
155#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156#define HAVE_GETCWD 1
157#define HAVE_GETEGID 1
158#define HAVE_GETEUID 1
159#define HAVE_GETGID 1
160#define HAVE_GETPPID 1
161#define HAVE_GETUID 1
162#define HAVE_KILL 1
163#define HAVE_OPENDIR 1
164#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000165#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000168#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000169#endif /* _MSC_VER */
170#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000172#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000173
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000175
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000176#if defined(__sgi)&&_COMPILER_VERSION>=700
177/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
178 (default) */
179extern char *ctermid_r(char *);
180#endif
181
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000182#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000184extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000186#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000188#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#endif
192#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000193extern int chdir(char *);
194extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000195#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chdir(const char *);
197extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000198#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000199#ifdef __BORLANDC__
200extern int chmod(const char *, int);
201#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000202extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000203#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000204/*#ifdef HAVE_FCHMOD
205extern int fchmod(int, mode_t);
206#endif*/
207/*#ifdef HAVE_LCHMOD
208extern int lchmod(const char *, mode_t);
209#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int chown(const char *, uid_t, gid_t);
211extern char *getcwd(char *, int);
212extern char *strerror(int);
213extern int link(const char *, const char *);
214extern int rename(const char *, const char *);
215extern int stat(const char *, struct stat *);
216extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000219#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000221extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000222#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000224
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#ifdef HAVE_UTIME_H
228#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000229#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000231#ifdef HAVE_SYS_UTIME_H
232#include <sys/utime.h>
233#define HAVE_UTIME_H /* pretend we do for the rest of this file */
234#endif /* HAVE_SYS_UTIME_H */
235
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236#ifdef HAVE_SYS_TIMES_H
237#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239
240#ifdef HAVE_SYS_PARAM_H
241#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000242#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243
244#ifdef HAVE_SYS_UTSNAME_H
245#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000246#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000248#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#define NAMLEN(dirent) strlen((dirent)->d_name)
251#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000252#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000253#include <direct.h>
254#define NAMLEN(dirent) strlen((dirent)->d_name)
255#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000257#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000258#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000259#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000261#endif
262#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000264#endif
265#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000267#endif
268#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000270#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273#endif
274#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276#endif
277#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000280#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000281#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000282#endif
283#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000284#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000285#endif
286#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000287#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000288#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000289#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000290#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000292#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000293#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000294#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
295#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000296static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000297#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000298#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299
Guido van Rossumd48f2521997-12-05 22:19:34 +0000300#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000302#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303
Tim Petersbc2e10e2002-03-03 23:17:02 +0000304#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000305#if defined(PATH_MAX) && PATH_MAX > 1024
306#define MAXPATHLEN PATH_MAX
307#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000308#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000309#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000310#endif /* MAXPATHLEN */
311
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000312#ifdef UNION_WAIT
313/* Emulate some macros on systems that have a union instead of macros */
314
315#ifndef WIFEXITED
316#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
317#endif
318
319#ifndef WEXITSTATUS
320#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
321#endif
322
323#ifndef WTERMSIG
324#define WTERMSIG(u_wait) ((u_wait).w_termsig)
325#endif
326
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327#define WAIT_TYPE union wait
328#define WAIT_STATUS_INT(s) (s.w_status)
329
330#else /* !UNION_WAIT */
331#define WAIT_TYPE int
332#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000333#endif /* UNION_WAIT */
334
Greg Wardb48bc172000-03-01 21:51:56 +0000335/* Don't use the "_r" form if we don't need it (also, won't have a
336 prototype for it, at least on Solaris -- maybe others as well?). */
337#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
338#define USE_CTERMID_R
339#endif
340
Fred Drake699f3522000-06-29 21:12:41 +0000341/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000342#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000343#undef FSTAT
344#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000345#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000346# define STAT win32_stat
347# define FSTAT win32_fstat
348# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000349#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000350# define STAT stat
351# define FSTAT fstat
352# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000353#endif
354
Tim Peters11b23062003-04-23 02:39:17 +0000355#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000356#include <sys/mkdev.h>
357#else
358#if defined(MAJOR_IN_SYSMACROS)
359#include <sys/sysmacros.h>
360#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000361#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
362#include <sys/mkdev.h>
363#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000364#endif
Fred Drake699f3522000-06-29 21:12:41 +0000365
Georg Brandla391b112011-02-25 15:23:18 +0000366static int
367_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000368{
369#if !defined(HAVE_LARGEFILE_SUPPORT)
370 *((off_t*)addr) = PyLong_AsLong(arg);
371#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000372 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000373#endif
374 if (PyErr_Occurred())
375 return 0;
376 return 1;
377}
378
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000379#if defined _MSC_VER && _MSC_VER >= 1400
380/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
381 * valid and throw an assertion if it isn't.
382 * Normally, an invalid fd is likely to be a C program error and therefore
383 * an assertion can be useful, but it does contradict the POSIX standard
384 * which for write(2) states:
385 * "Otherwise, -1 shall be returned and errno set to indicate the error."
386 * "[EBADF] The fildes argument is not a valid file descriptor open for
387 * writing."
388 * Furthermore, python allows the user to enter any old integer
389 * as a fd and should merely raise a python exception on error.
390 * The Microsoft CRT doesn't provide an official way to check for the
391 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000392 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000393 * internal structures involved.
394 * The structures below must be updated for each version of visual studio
395 * according to the file internal.h in the CRT source, until MS comes
396 * up with a less hacky way to do this.
397 * (all of this is to avoid globally modifying the CRT behaviour using
398 * _set_invalid_parameter_handler() and _CrtSetReportMode())
399 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000400/* The actual size of the structure is determined at runtime.
401 * Only the first items must be present.
402 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000403typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000404 intptr_t osfhnd;
405 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000406} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000407
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000408extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000409#define IOINFO_L2E 5
410#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
411#define IOINFO_ARRAYS 64
412#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
413#define FOPEN 0x01
414#define _NO_CONSOLE_FILENO (intptr_t)-2
415
416/* This function emulates what the windows CRT does to validate file handles */
417int
418_PyVerify_fd(int fd)
419{
Victor Stinner8c62be82010-05-06 00:08:46 +0000420 const int i1 = fd >> IOINFO_L2E;
421 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422
Antoine Pitrou22e41552010-08-15 18:07:50 +0000423 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000424
Victor Stinner8c62be82010-05-06 00:08:46 +0000425 /* Determine the actual size of the ioinfo structure,
426 * as used by the CRT loaded in memory
427 */
428 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
429 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
430 }
431 if (sizeof_ioinfo == 0) {
432 /* This should not happen... */
433 goto fail;
434 }
435
436 /* See that it isn't a special CLEAR fileno */
437 if (fd != _NO_CONSOLE_FILENO) {
438 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
439 * we check pointer validity and other info
440 */
441 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
442 /* finally, check that the file is open */
443 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
444 if (info->osfile & FOPEN) {
445 return 1;
446 }
447 }
448 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000449 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000450 errno = EBADF;
451 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000452}
453
454/* the special case of checking dup2. The target fd must be in a sensible range */
455static int
456_PyVerify_fd_dup2(int fd1, int fd2)
457{
Victor Stinner8c62be82010-05-06 00:08:46 +0000458 if (!_PyVerify_fd(fd1))
459 return 0;
460 if (fd2 == _NO_CONSOLE_FILENO)
461 return 0;
462 if ((unsigned)fd2 < _NHANDLE_)
463 return 1;
464 else
465 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000466}
467#else
468/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
469#define _PyVerify_fd_dup2(A, B) (1)
470#endif
471
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000472#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000473/* The following structure was copied from
474 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
475 include doesn't seem to be present in the Windows SDK (at least as included
476 with Visual Studio Express). */
477typedef struct _REPARSE_DATA_BUFFER {
478 ULONG ReparseTag;
479 USHORT ReparseDataLength;
480 USHORT Reserved;
481 union {
482 struct {
483 USHORT SubstituteNameOffset;
484 USHORT SubstituteNameLength;
485 USHORT PrintNameOffset;
486 USHORT PrintNameLength;
487 ULONG Flags;
488 WCHAR PathBuffer[1];
489 } SymbolicLinkReparseBuffer;
490
491 struct {
492 USHORT SubstituteNameOffset;
493 USHORT SubstituteNameLength;
494 USHORT PrintNameOffset;
495 USHORT PrintNameLength;
496 WCHAR PathBuffer[1];
497 } MountPointReparseBuffer;
498
499 struct {
500 UCHAR DataBuffer[1];
501 } GenericReparseBuffer;
502 };
503} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
504
505#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
506 GenericReparseBuffer)
507#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
508
509static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000510win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000511{
512 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
513 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
514 DWORD n_bytes_returned;
515 const wchar_t *ptr;
516 wchar_t *buf;
517 size_t len;
518
519 if (0 == DeviceIoControl(
520 reparse_point_handle,
521 FSCTL_GET_REPARSE_POINT,
522 NULL, 0, /* in buffer */
523 target_buffer, sizeof(target_buffer),
524 &n_bytes_returned,
525 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000526 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000527
528 if (reparse_tag)
529 *reparse_tag = rdb->ReparseTag;
530
531 if (target_path) {
532 switch (rdb->ReparseTag) {
533 case IO_REPARSE_TAG_SYMLINK:
534 /* XXX: Maybe should use SubstituteName? */
535 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
536 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
537 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
538 break;
539 case IO_REPARSE_TAG_MOUNT_POINT:
540 ptr = rdb->MountPointReparseBuffer.PathBuffer +
541 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
542 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
543 break;
544 default:
545 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000546 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000547 }
548 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
549 if (!buf) {
550 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000551 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000552 }
553 wcsncpy(buf, ptr, len);
554 buf[len] = L'\0';
555 if (wcsncmp(buf, L"\\??\\", 4) == 0)
556 buf[1] = L'\\';
557 *target_path = buf;
558 }
559
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000560 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000561}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000562#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000563
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000565#ifdef WITH_NEXT_FRAMEWORK
566/* On Darwin/MacOSX a shared library or framework has no access to
567** environ directly, we must obtain it with _NSGetEnviron().
568*/
569#include <crt_externs.h>
570static char **environ;
571#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000573#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574
Barry Warsaw53699e91996-12-10 23:23:01 +0000575static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000576convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577{
Victor Stinner8c62be82010-05-06 00:08:46 +0000578 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000579#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000580 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000581#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000582 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000583#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000584#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000585 APIRET rc;
586 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
587#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000588
Victor Stinner8c62be82010-05-06 00:08:46 +0000589 d = PyDict_New();
590 if (d == NULL)
591 return NULL;
592#ifdef WITH_NEXT_FRAMEWORK
593 if (environ == NULL)
594 environ = *_NSGetEnviron();
595#endif
596#ifdef MS_WINDOWS
597 /* _wenviron must be initialized in this way if the program is started
598 through main() instead of wmain(). */
599 _wgetenv(L"");
600 if (_wenviron == NULL)
601 return d;
602 /* This part ignores errors */
603 for (e = _wenviron; *e != NULL; e++) {
604 PyObject *k;
605 PyObject *v;
606 wchar_t *p = wcschr(*e, L'=');
607 if (p == NULL)
608 continue;
609 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
610 if (k == NULL) {
611 PyErr_Clear();
612 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000613 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000614 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
615 if (v == NULL) {
616 PyErr_Clear();
617 Py_DECREF(k);
618 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000619 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000620 if (PyDict_GetItem(d, k) == NULL) {
621 if (PyDict_SetItem(d, k, v) != 0)
622 PyErr_Clear();
623 }
624 Py_DECREF(k);
625 Py_DECREF(v);
626 }
627#else
628 if (environ == NULL)
629 return d;
630 /* This part ignores errors */
631 for (e = environ; *e != NULL; e++) {
632 PyObject *k;
633 PyObject *v;
634 char *p = strchr(*e, '=');
635 if (p == NULL)
636 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000637 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000638 if (k == NULL) {
639 PyErr_Clear();
640 continue;
641 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000642 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000643 if (v == NULL) {
644 PyErr_Clear();
645 Py_DECREF(k);
646 continue;
647 }
648 if (PyDict_GetItem(d, k) == NULL) {
649 if (PyDict_SetItem(d, k, v) != 0)
650 PyErr_Clear();
651 }
652 Py_DECREF(k);
653 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000654 }
655#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000656#if defined(PYOS_OS2)
657 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
658 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
659 PyObject *v = PyBytes_FromString(buffer);
660 PyDict_SetItemString(d, "BEGINLIBPATH", v);
661 Py_DECREF(v);
662 }
663 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
664 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
665 PyObject *v = PyBytes_FromString(buffer);
666 PyDict_SetItemString(d, "ENDLIBPATH", v);
667 Py_DECREF(v);
668 }
669#endif
670 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671}
672
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673/* Set a POSIX-specific error from errno, and return NULL */
674
Barry Warsawd58d7641998-07-23 16:14:40 +0000675static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000676posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000677{
Victor Stinner8c62be82010-05-06 00:08:46 +0000678 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679}
Barry Warsawd58d7641998-07-23 16:14:40 +0000680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000681posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000682{
Victor Stinner8c62be82010-05-06 00:08:46 +0000683 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000684}
685
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686
Mark Hammondef8b6542001-05-13 08:04:26 +0000687static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000688posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000689{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000690 PyObject *name_str, *rc;
691 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
692 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000693 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000694 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
695 name_str);
696 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000697 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000698}
699
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000700#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000701static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000702win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000703{
Victor Stinner8c62be82010-05-06 00:08:46 +0000704 /* XXX We should pass the function name along in the future.
705 (winreg.c also wants to pass the function name.)
706 This would however require an additional param to the
707 Windows error object, which is non-trivial.
708 */
709 errno = GetLastError();
710 if (filename)
711 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
712 else
713 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000714}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000715
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716static PyObject *
717win32_error_unicode(char* function, Py_UNICODE* filename)
718{
Victor Stinner8c62be82010-05-06 00:08:46 +0000719 /* XXX - see win32_error for comments on 'function' */
720 errno = GetLastError();
721 if (filename)
722 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
723 else
724 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000725}
726
Thomas Wouters477c8d52006-05-27 19:21:47 +0000727static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000728convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729{
Victor Stinner8c62be82010-05-06 00:08:46 +0000730 if (PyUnicode_CheckExact(*param))
731 Py_INCREF(*param);
732 else if (PyUnicode_Check(*param))
733 /* For a Unicode subtype that's not a Unicode object,
734 return a true Unicode object with the same data. */
735 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
736 PyUnicode_GET_SIZE(*param));
737 else
738 *param = PyUnicode_FromEncodedObject(*param,
739 Py_FileSystemDefaultEncoding,
740 "strict");
741 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000742}
743
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000744#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745
Guido van Rossumd48f2521997-12-05 22:19:34 +0000746#if defined(PYOS_OS2)
747/**********************************************************************
748 * Helper Function to Trim and Format OS/2 Messages
749 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000750static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000751os2_formatmsg(char *msgbuf, int msglen, char *reason)
752{
753 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
754
755 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
756 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
757
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000758 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000759 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
760 }
761
762 /* Add Optional Reason Text */
763 if (reason) {
764 strcat(msgbuf, " : ");
765 strcat(msgbuf, reason);
766 }
767}
768
769/**********************************************************************
770 * Decode an OS/2 Operating System Error Code
771 *
772 * A convenience function to lookup an OS/2 error code and return a
773 * text message we can use to raise a Python exception.
774 *
775 * Notes:
776 * The messages for errors returned from the OS/2 kernel reside in
777 * the file OSO001.MSG in the \OS2 directory hierarchy.
778 *
779 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000780static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000781os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
782{
783 APIRET rc;
784 ULONG msglen;
785
786 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
787 Py_BEGIN_ALLOW_THREADS
788 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
789 errorcode, "oso001.msg", &msglen);
790 Py_END_ALLOW_THREADS
791
792 if (rc == NO_ERROR)
793 os2_formatmsg(msgbuf, msglen, reason);
794 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000795 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000796 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000797
798 return msgbuf;
799}
800
801/* Set an OS/2-specific error and return NULL. OS/2 kernel
802 errors are not in a global variable e.g. 'errno' nor are
803 they congruent with posix error numbers. */
804
Victor Stinner8c62be82010-05-06 00:08:46 +0000805static PyObject *
806os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000807{
808 char text[1024];
809 PyObject *v;
810
811 os2_strerror(text, sizeof(text), code, "");
812
813 v = Py_BuildValue("(is)", code, text);
814 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000815 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000816 Py_DECREF(v);
817 }
818 return NULL; /* Signal to Python that an Exception is Pending */
819}
820
821#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822
823/* POSIX generic methods */
824
Barry Warsaw53699e91996-12-10 23:23:01 +0000825static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000826posix_fildes(PyObject *fdobj, int (*func)(int))
827{
Victor Stinner8c62be82010-05-06 00:08:46 +0000828 int fd;
829 int res;
830 fd = PyObject_AsFileDescriptor(fdobj);
831 if (fd < 0)
832 return NULL;
833 if (!_PyVerify_fd(fd))
834 return posix_error();
835 Py_BEGIN_ALLOW_THREADS
836 res = (*func)(fd);
837 Py_END_ALLOW_THREADS
838 if (res < 0)
839 return posix_error();
840 Py_INCREF(Py_None);
841 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000842}
Guido van Rossum21142a01999-01-08 21:05:37 +0000843
844static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000845posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000846{
Victor Stinner8c62be82010-05-06 00:08:46 +0000847 PyObject *opath1 = NULL;
848 char *path1;
849 int res;
850 if (!PyArg_ParseTuple(args, format,
851 PyUnicode_FSConverter, &opath1))
852 return NULL;
853 path1 = PyBytes_AsString(opath1);
854 Py_BEGIN_ALLOW_THREADS
855 res = (*func)(path1);
856 Py_END_ALLOW_THREADS
857 if (res < 0)
858 return posix_error_with_allocated_filename(opath1);
859 Py_DECREF(opath1);
860 Py_INCREF(Py_None);
861 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862}
863
Barry Warsaw53699e91996-12-10 23:23:01 +0000864static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000865posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000866 char *format,
867 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868{
Victor Stinner8c62be82010-05-06 00:08:46 +0000869 PyObject *opath1 = NULL, *opath2 = NULL;
870 char *path1, *path2;
871 int res;
872 if (!PyArg_ParseTuple(args, format,
873 PyUnicode_FSConverter, &opath1,
874 PyUnicode_FSConverter, &opath2)) {
875 return NULL;
876 }
877 path1 = PyBytes_AsString(opath1);
878 path2 = PyBytes_AsString(opath2);
879 Py_BEGIN_ALLOW_THREADS
880 res = (*func)(path1, path2);
881 Py_END_ALLOW_THREADS
882 Py_DECREF(opath1);
883 Py_DECREF(opath2);
884 if (res != 0)
885 /* XXX how to report both path1 and path2??? */
886 return posix_error();
887 Py_INCREF(Py_None);
888 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889}
890
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000891#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000893win32_1str(PyObject* args, char* func,
894 char* format, BOOL (__stdcall *funcA)(LPCSTR),
895 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000896{
Victor Stinner8c62be82010-05-06 00:08:46 +0000897 PyObject *uni;
898 char *ansi;
899 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000900
Victor Stinner8c62be82010-05-06 00:08:46 +0000901 if (!PyArg_ParseTuple(args, wformat, &uni))
902 PyErr_Clear();
903 else {
904 Py_BEGIN_ALLOW_THREADS
905 result = funcW(PyUnicode_AsUnicode(uni));
906 Py_END_ALLOW_THREADS
907 if (!result)
908 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
909 Py_INCREF(Py_None);
910 return Py_None;
911 }
912 if (!PyArg_ParseTuple(args, format, &ansi))
913 return NULL;
914 Py_BEGIN_ALLOW_THREADS
915 result = funcA(ansi);
916 Py_END_ALLOW_THREADS
917 if (!result)
918 return win32_error(func, ansi);
919 Py_INCREF(Py_None);
920 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921
922}
923
924/* This is a reimplementation of the C library's chdir function,
925 but one that produces Win32 errors instead of DOS error codes.
926 chdir is essentially a wrapper around SetCurrentDirectory; however,
927 it also needs to set "magic" environment variables indicating
928 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000929static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930win32_chdir(LPCSTR path)
931{
Victor Stinner8c62be82010-05-06 00:08:46 +0000932 char new_path[MAX_PATH+1];
933 int result;
934 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935
Victor Stinner8c62be82010-05-06 00:08:46 +0000936 if(!SetCurrentDirectoryA(path))
937 return FALSE;
938 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
939 if (!result)
940 return FALSE;
941 /* In the ANSI API, there should not be any paths longer
942 than MAX_PATH. */
943 assert(result <= MAX_PATH+1);
944 if (strncmp(new_path, "\\\\", 2) == 0 ||
945 strncmp(new_path, "//", 2) == 0)
946 /* UNC path, nothing to do. */
947 return TRUE;
948 env[1] = new_path[0];
949 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950}
951
952/* The Unicode version differs from the ANSI version
953 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000954static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955win32_wchdir(LPCWSTR path)
956{
Victor Stinner8c62be82010-05-06 00:08:46 +0000957 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
958 int result;
959 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960
Victor Stinner8c62be82010-05-06 00:08:46 +0000961 if(!SetCurrentDirectoryW(path))
962 return FALSE;
963 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
964 if (!result)
965 return FALSE;
966 if (result > MAX_PATH+1) {
967 new_path = malloc(result * sizeof(wchar_t));
968 if (!new_path) {
969 SetLastError(ERROR_OUTOFMEMORY);
970 return FALSE;
971 }
972 result = GetCurrentDirectoryW(result, new_path);
973 if (!result) {
974 free(new_path);
975 return FALSE;
976 }
977 }
978 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
979 wcsncmp(new_path, L"//", 2) == 0)
980 /* UNC path, nothing to do. */
981 return TRUE;
982 env[1] = new_path[0];
983 result = SetEnvironmentVariableW(env, new_path);
984 if (new_path != _new_path)
985 free(new_path);
986 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000987}
988#endif
989
Martin v. Löwis14694662006-02-03 12:54:16 +0000990#ifdef MS_WINDOWS
991/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
992 - time stamps are restricted to second resolution
993 - file modification times suffer from forth-and-back conversions between
994 UTC and local time
995 Therefore, we implement our own stat, based on the Win32 API directly.
996*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000997#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000998
999struct win32_stat{
1000 int st_dev;
1001 __int64 st_ino;
1002 unsigned short st_mode;
1003 int st_nlink;
1004 int st_uid;
1005 int st_gid;
1006 int st_rdev;
1007 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001008 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001009 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001010 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001011 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001012 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001013 int st_ctime_nsec;
1014};
1015
1016static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1017
1018static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001019FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001020{
Victor Stinner8c62be82010-05-06 00:08:46 +00001021 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1022 /* Cannot simply cast and dereference in_ptr,
1023 since it might not be aligned properly */
1024 __int64 in;
1025 memcpy(&in, in_ptr, sizeof(in));
1026 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001027 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001028}
1029
Thomas Wouters477c8d52006-05-27 19:21:47 +00001030static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001031time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032{
Victor Stinner8c62be82010-05-06 00:08:46 +00001033 /* XXX endianness */
1034 __int64 out;
1035 out = time_in + secs_between_epochs;
1036 out = out * 10000000 + nsec_in / 100;
1037 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038}
1039
Martin v. Löwis14694662006-02-03 12:54:16 +00001040/* Below, we *know* that ugo+r is 0444 */
1041#if _S_IREAD != 0400
1042#error Unsupported C library
1043#endif
1044static int
1045attributes_to_mode(DWORD attr)
1046{
Victor Stinner8c62be82010-05-06 00:08:46 +00001047 int m = 0;
1048 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1049 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1050 else
1051 m |= _S_IFREG;
1052 if (attr & FILE_ATTRIBUTE_READONLY)
1053 m |= 0444;
1054 else
1055 m |= 0666;
1056 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001057}
1058
1059static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001060attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001061{
Victor Stinner8c62be82010-05-06 00:08:46 +00001062 memset(result, 0, sizeof(*result));
1063 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1064 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1065 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1066 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1067 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001068 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001069 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001070 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1071 /* first clear the S_IFMT bits */
1072 result->st_mode ^= (result->st_mode & 0170000);
1073 /* now set the bits that make this a symlink */
1074 result->st_mode |= 0120000;
1075 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001076
Victor Stinner8c62be82010-05-06 00:08:46 +00001077 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001078}
1079
Guido van Rossumd8faa362007-04-27 19:54:29 +00001080static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001081attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001082{
Victor Stinner8c62be82010-05-06 00:08:46 +00001083 HANDLE hFindFile;
1084 WIN32_FIND_DATAA FileData;
1085 hFindFile = FindFirstFileA(pszFile, &FileData);
1086 if (hFindFile == INVALID_HANDLE_VALUE)
1087 return FALSE;
1088 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001089 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001090 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001091 info->dwFileAttributes = FileData.dwFileAttributes;
1092 info->ftCreationTime = FileData.ftCreationTime;
1093 info->ftLastAccessTime = FileData.ftLastAccessTime;
1094 info->ftLastWriteTime = FileData.ftLastWriteTime;
1095 info->nFileSizeHigh = FileData.nFileSizeHigh;
1096 info->nFileSizeLow = FileData.nFileSizeLow;
1097/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001098 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1099 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001100 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001101}
1102
1103static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001104attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001105{
Victor Stinner8c62be82010-05-06 00:08:46 +00001106 HANDLE hFindFile;
1107 WIN32_FIND_DATAW FileData;
1108 hFindFile = FindFirstFileW(pszFile, &FileData);
1109 if (hFindFile == INVALID_HANDLE_VALUE)
1110 return FALSE;
1111 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001112 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001113 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001114 info->dwFileAttributes = FileData.dwFileAttributes;
1115 info->ftCreationTime = FileData.ftCreationTime;
1116 info->ftLastAccessTime = FileData.ftLastAccessTime;
1117 info->ftLastWriteTime = FileData.ftLastWriteTime;
1118 info->nFileSizeHigh = FileData.nFileSizeHigh;
1119 info->nFileSizeLow = FileData.nFileSizeLow;
1120/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001121 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1122 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001123 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001124}
1125
Brian Curtinf5e76d02010-11-24 13:14:05 +00001126#ifndef SYMLOOP_MAX
1127#define SYMLOOP_MAX ( 88 )
1128#endif
1129
1130static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001132
1133static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001134win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001135{
1136 int code;
1137 HANDLE hFile;
1138 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001139 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001140 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001141 const char *dot;
1142
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001143 if (depth > SYMLOOP_MAX) {
1144 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1145 return -1;
1146 }
1147
Brian Curtinf5e76d02010-11-24 13:14:05 +00001148 hFile = CreateFileA(
1149 path,
1150 0, /* desired access */
1151 0, /* share mode */
1152 NULL, /* security attributes */
1153 OPEN_EXISTING,
1154 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1155 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1156 NULL);
1157
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001158 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001159 /* Either the target doesn't exist, or we don't have access to
1160 get a handle to it. If the former, we need to return an error.
1161 If the latter, we can use attributes_from_dir. */
1162 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001163 return -1;
1164 /* Could not get attributes on open file. Fall back to
1165 reading the directory. */
1166 if (!attributes_from_dir(path, &info, &reparse_tag))
1167 /* Very strange. This should not fail now */
1168 return -1;
1169 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1170 if (traverse) {
1171 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001172 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001173 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001174 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001175 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001176 } else {
1177 if (!GetFileInformationByHandle(hFile, &info)) {
1178 CloseHandle(hFile);
1179 return -1;;
1180 }
1181 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1182 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1183 CloseHandle(hFile);
1184 if (code < 0)
1185 return code;
1186 if (traverse) {
1187 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1188 free(target_path);
1189 return code;
1190 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001191 } else
1192 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001193 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001194 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001195
1196 /* Set S_IEXEC if it is an .exe, .bat, ... */
1197 dot = strrchr(path, '.');
1198 if (dot) {
1199 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1200 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1201 result->st_mode |= 0111;
1202 }
1203 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001204}
1205
1206static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001208{
1209 int code;
1210 HANDLE hFile;
1211 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001212 ULONG reparse_tag = 0;
1213 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001214 const wchar_t *dot;
1215
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001216 if (depth > SYMLOOP_MAX) {
1217 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1218 return -1;
1219 }
1220
Brian Curtinf5e76d02010-11-24 13:14:05 +00001221 hFile = CreateFileW(
1222 path,
1223 0, /* desired access */
1224 0, /* share mode */
1225 NULL, /* security attributes */
1226 OPEN_EXISTING,
1227 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1228 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1229 NULL);
1230
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001231 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001232 /* Either the target doesn't exist, or we don't have access to
1233 get a handle to it. If the former, we need to return an error.
1234 If the latter, we can use attributes_from_dir. */
1235 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001236 return -1;
1237 /* Could not get attributes on open file. Fall back to
1238 reading the directory. */
1239 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1240 /* Very strange. This should not fail now */
1241 return -1;
1242 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1243 if (traverse) {
1244 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001245 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001246 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001247 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001249 } else {
1250 if (!GetFileInformationByHandle(hFile, &info)) {
1251 CloseHandle(hFile);
1252 return -1;;
1253 }
1254 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1255 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1256 CloseHandle(hFile);
1257 if (code < 0)
1258 return code;
1259 if (traverse) {
1260 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1261 free(target_path);
1262 return code;
1263 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001264 } else
1265 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001266 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001267 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268
1269 /* Set S_IEXEC if it is an .exe, .bat, ... */
1270 dot = wcsrchr(path, '.');
1271 if (dot) {
1272 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1273 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1274 result->st_mode |= 0111;
1275 }
1276 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001277}
1278
1279static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001280win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001281{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001282 /* Protocol violation: we explicitly clear errno, instead of
1283 setting it to a POSIX error. Callers should use GetLastError. */
1284 int code = win32_xstat_impl(path, result, traverse, 0);
1285 errno = 0;
1286 return code;
1287}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001288
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001289static int
1290win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1291{
1292 /* Protocol violation: we explicitly clear errno, instead of
1293 setting it to a POSIX error. Callers should use GetLastError. */
1294 int code = win32_xstat_impl_w(path, result, traverse, 0);
1295 errno = 0;
1296 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001297}
1298
Brian Curtind40e6f72010-07-08 21:39:08 +00001299/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1300 win32_stat_w
1301
1302 In Posix, stat automatically traverses symlinks and returns the stat
1303 structure for the target. In Windows, the equivalent GetFileAttributes by
1304 default does not traverse symlinks and instead returns attributes for
1305 the symlink.
1306
1307 Therefore, win32_lstat will get the attributes traditionally, and
1308 win32_stat will first explicitly resolve the symlink target and then will
1309 call win32_lstat on that result.
1310
Ezio Melotti4969f702011-03-15 05:59:46 +02001311 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001312
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001313static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001314win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001315{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001316 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001317}
1318
Victor Stinner8c62be82010-05-06 00:08:46 +00001319static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001320win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001321{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001322 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001323}
1324
1325static int
1326win32_stat(const char* path, struct win32_stat *result)
1327{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001328 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001329}
1330
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001331static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001332win32_stat_w(const wchar_t* path, struct win32_stat *result)
1333{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001334 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001335}
1336
1337static int
1338win32_fstat(int file_number, struct win32_stat *result)
1339{
Victor Stinner8c62be82010-05-06 00:08:46 +00001340 BY_HANDLE_FILE_INFORMATION info;
1341 HANDLE h;
1342 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001343
Victor Stinner8c62be82010-05-06 00:08:46 +00001344 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001345
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 /* Protocol violation: we explicitly clear errno, instead of
1347 setting it to a POSIX error. Callers should use GetLastError. */
1348 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001349
Victor Stinner8c62be82010-05-06 00:08:46 +00001350 if (h == INVALID_HANDLE_VALUE) {
1351 /* This is really a C library error (invalid file handle).
1352 We set the Win32 error to the closes one matching. */
1353 SetLastError(ERROR_INVALID_HANDLE);
1354 return -1;
1355 }
1356 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001357
Victor Stinner8c62be82010-05-06 00:08:46 +00001358 type = GetFileType(h);
1359 if (type == FILE_TYPE_UNKNOWN) {
1360 DWORD error = GetLastError();
1361 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001362 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001363 }
1364 /* else: valid but unknown file */
1365 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001366
Victor Stinner8c62be82010-05-06 00:08:46 +00001367 if (type != FILE_TYPE_DISK) {
1368 if (type == FILE_TYPE_CHAR)
1369 result->st_mode = _S_IFCHR;
1370 else if (type == FILE_TYPE_PIPE)
1371 result->st_mode = _S_IFIFO;
1372 return 0;
1373 }
1374
1375 if (!GetFileInformationByHandle(h, &info)) {
1376 return -1;
1377 }
1378
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001379 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001380 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001381 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1382 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001383}
1384
1385#endif /* MS_WINDOWS */
1386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001387PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001388"stat_result: Result from stat or lstat.\n\n\
1389This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001390 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001391or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1392\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001393Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1394or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001395\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001396See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001397
1398static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 {"st_mode", "protection bits"},
1400 {"st_ino", "inode"},
1401 {"st_dev", "device"},
1402 {"st_nlink", "number of hard links"},
1403 {"st_uid", "user ID of owner"},
1404 {"st_gid", "group ID of owner"},
1405 {"st_size", "total size, in bytes"},
1406 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1407 {NULL, "integer time of last access"},
1408 {NULL, "integer time of last modification"},
1409 {NULL, "integer time of last change"},
1410 {"st_atime", "time of last access"},
1411 {"st_mtime", "time of last modification"},
1412 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001413#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001414 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001415#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001416#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001417 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001418#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001419#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001421#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001422#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001423 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001424#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001425#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001426 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001427#endif
1428#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001429 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001430#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001431 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001432};
1433
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001434#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001435#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001436#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001437#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001438#endif
1439
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001440#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001441#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1442#else
1443#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1444#endif
1445
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001446#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001447#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1448#else
1449#define ST_RDEV_IDX ST_BLOCKS_IDX
1450#endif
1451
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001452#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1453#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1454#else
1455#define ST_FLAGS_IDX ST_RDEV_IDX
1456#endif
1457
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001458#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001459#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001460#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001461#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001462#endif
1463
1464#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1465#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1466#else
1467#define ST_BIRTHTIME_IDX ST_GEN_IDX
1468#endif
1469
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001470static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 "stat_result", /* name */
1472 stat_result__doc__, /* doc */
1473 stat_result_fields,
1474 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001475};
1476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001477PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001478"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1479This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001480 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001481or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001482\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001483See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001484
1485static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 {"f_bsize", },
1487 {"f_frsize", },
1488 {"f_blocks", },
1489 {"f_bfree", },
1490 {"f_bavail", },
1491 {"f_files", },
1492 {"f_ffree", },
1493 {"f_favail", },
1494 {"f_flag", },
1495 {"f_namemax",},
1496 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001497};
1498
1499static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001500 "statvfs_result", /* name */
1501 statvfs_result__doc__, /* doc */
1502 statvfs_result_fields,
1503 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001504};
1505
Ross Lagerwall7807c352011-03-17 20:20:30 +02001506#if defined(HAVE_WAITID) && !defined(__APPLE__)
1507PyDoc_STRVAR(waitid_result__doc__,
1508"waitid_result: Result from waitid.\n\n\
1509This object may be accessed either as a tuple of\n\
1510 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1511or via the attributes si_pid, si_uid, and so on.\n\
1512\n\
1513See os.waitid for more information.");
1514
1515static PyStructSequence_Field waitid_result_fields[] = {
1516 {"si_pid", },
1517 {"si_uid", },
1518 {"si_signo", },
1519 {"si_status", },
1520 {"si_code", },
1521 {0}
1522};
1523
1524static PyStructSequence_Desc waitid_result_desc = {
1525 "waitid_result", /* name */
1526 waitid_result__doc__, /* doc */
1527 waitid_result_fields,
1528 5
1529};
1530static PyTypeObject WaitidResultType;
1531#endif
1532
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001533static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001534static PyTypeObject StatResultType;
1535static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001536static newfunc structseq_new;
1537
1538static PyObject *
1539statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1540{
Victor Stinner8c62be82010-05-06 00:08:46 +00001541 PyStructSequence *result;
1542 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001543
Victor Stinner8c62be82010-05-06 00:08:46 +00001544 result = (PyStructSequence*)structseq_new(type, args, kwds);
1545 if (!result)
1546 return NULL;
1547 /* If we have been initialized from a tuple,
1548 st_?time might be set to None. Initialize it
1549 from the int slots. */
1550 for (i = 7; i <= 9; i++) {
1551 if (result->ob_item[i+3] == Py_None) {
1552 Py_DECREF(Py_None);
1553 Py_INCREF(result->ob_item[i]);
1554 result->ob_item[i+3] = result->ob_item[i];
1555 }
1556 }
1557 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001558}
1559
1560
1561
1562/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001563static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001564
1565PyDoc_STRVAR(stat_float_times__doc__,
1566"stat_float_times([newval]) -> oldval\n\n\
1567Determine whether os.[lf]stat represents time stamps as float objects.\n\
1568If newval is True, future calls to stat() return floats, if it is False,\n\
1569future calls return ints. \n\
1570If newval is omitted, return the current setting.\n");
1571
1572static PyObject*
1573stat_float_times(PyObject* self, PyObject *args)
1574{
Victor Stinner8c62be82010-05-06 00:08:46 +00001575 int newval = -1;
1576 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1577 return NULL;
1578 if (newval == -1)
1579 /* Return old value */
1580 return PyBool_FromLong(_stat_float_times);
1581 _stat_float_times = newval;
1582 Py_INCREF(Py_None);
1583 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001584}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001585
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001586static void
1587fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1588{
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001590#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001592#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001594#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 if (!ival)
1596 return;
1597 if (_stat_float_times) {
1598 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1599 } else {
1600 fval = ival;
1601 Py_INCREF(fval);
1602 }
1603 PyStructSequence_SET_ITEM(v, index, ival);
1604 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001605}
1606
Tim Peters5aa91602002-01-30 05:46:57 +00001607/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001608 (used by posix_stat() and posix_fstat()) */
1609static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001610_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001611{
Victor Stinner8c62be82010-05-06 00:08:46 +00001612 unsigned long ansec, mnsec, cnsec;
1613 PyObject *v = PyStructSequence_New(&StatResultType);
1614 if (v == NULL)
1615 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001616
Victor Stinner8c62be82010-05-06 00:08:46 +00001617 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001618#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001619 PyStructSequence_SET_ITEM(v, 1,
1620 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001621#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001623#endif
1624#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 PyStructSequence_SET_ITEM(v, 2,
1626 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001627#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001628 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001630 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1631 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1632 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001633#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001634 PyStructSequence_SET_ITEM(v, 6,
1635 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001636#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001637 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001638#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001639
Martin v. Löwis14694662006-02-03 12:54:16 +00001640#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001641 ansec = st->st_atim.tv_nsec;
1642 mnsec = st->st_mtim.tv_nsec;
1643 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001644#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001645 ansec = st->st_atimespec.tv_nsec;
1646 mnsec = st->st_mtimespec.tv_nsec;
1647 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001648#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001649 ansec = st->st_atime_nsec;
1650 mnsec = st->st_mtime_nsec;
1651 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001652#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001653 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001654#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 fill_time(v, 7, st->st_atime, ansec);
1656 fill_time(v, 8, st->st_mtime, mnsec);
1657 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001658
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001659#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1661 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001662#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001663#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1665 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001666#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001667#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1669 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001670#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001671#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1673 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001674#endif
1675#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001676 {
1677 PyObject *val;
1678 unsigned long bsec,bnsec;
1679 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001680#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001682#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001684#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 if (_stat_float_times) {
1686 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1687 } else {
1688 val = PyLong_FromLong((long)bsec);
1689 }
1690 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1691 val);
1692 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001693#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001694#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001695 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1696 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001697#endif
Fred Drake699f3522000-06-29 21:12:41 +00001698
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 if (PyErr_Occurred()) {
1700 Py_DECREF(v);
1701 return NULL;
1702 }
Fred Drake699f3522000-06-29 21:12:41 +00001703
Victor Stinner8c62be82010-05-06 00:08:46 +00001704 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001705}
1706
Barry Warsaw53699e91996-12-10 23:23:01 +00001707static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001708posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001710#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001712#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001714#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001715 char *wformat,
1716 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001717{
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 STRUCT_STAT st;
1719 PyObject *opath;
1720 char *path;
1721 int res;
1722 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001723
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001724#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001725 PyUnicodeObject *po;
1726 if (PyArg_ParseTuple(args, wformat, &po)) {
1727 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001728
Victor Stinner8c62be82010-05-06 00:08:46 +00001729 Py_BEGIN_ALLOW_THREADS
1730 /* PyUnicode_AS_UNICODE result OK without
1731 thread lock as it is a simple dereference. */
1732 res = wstatfunc(wpath, &st);
1733 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001734
Victor Stinner8c62be82010-05-06 00:08:46 +00001735 if (res != 0)
1736 return win32_error_unicode("stat", wpath);
1737 return _pystat_fromstructstat(&st);
1738 }
1739 /* Drop the argument parsing error as narrow strings
1740 are also valid. */
1741 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001742#endif
1743
Victor Stinner8c62be82010-05-06 00:08:46 +00001744 if (!PyArg_ParseTuple(args, format,
1745 PyUnicode_FSConverter, &opath))
1746 return NULL;
1747 path = PyBytes_AsString(opath);
1748 Py_BEGIN_ALLOW_THREADS
1749 res = (*statfunc)(path, &st);
1750 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001751
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001753#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001754 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001755#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001756 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001757#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 }
1759 else
1760 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001761
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 Py_DECREF(opath);
1763 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001764}
1765
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001766/* POSIX methods */
1767
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001768PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001769"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001770Use the real uid/gid to test for access to a path. Note that most\n\
1771operations will use the effective uid/gid, therefore this routine can\n\
1772be used in a suid/sgid environment to test if the invoking user has the\n\
1773specified access to the path. The mode argument can be F_OK to test\n\
1774existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001775
1776static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001777posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001778{
Victor Stinner8c62be82010-05-06 00:08:46 +00001779 PyObject *opath;
1780 char *path;
1781 int mode;
1782
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001783#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 DWORD attr;
1785 PyUnicodeObject *po;
1786 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1787 Py_BEGIN_ALLOW_THREADS
1788 /* PyUnicode_AS_UNICODE OK without thread lock as
1789 it is a simple dereference. */
1790 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1791 Py_END_ALLOW_THREADS
1792 goto finish;
1793 }
1794 /* Drop the argument parsing error as narrow strings
1795 are also valid. */
1796 PyErr_Clear();
1797 if (!PyArg_ParseTuple(args, "O&i:access",
1798 PyUnicode_FSConverter, &opath, &mode))
1799 return NULL;
1800 path = PyBytes_AsString(opath);
1801 Py_BEGIN_ALLOW_THREADS
1802 attr = GetFileAttributesA(path);
1803 Py_END_ALLOW_THREADS
1804 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001805finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 if (attr == 0xFFFFFFFF)
1807 /* File does not exist, or cannot read attributes */
1808 return PyBool_FromLong(0);
1809 /* Access is possible if either write access wasn't requested, or
1810 the file isn't read-only, or if it's a directory, as there are
1811 no read-only directories on Windows. */
1812 return PyBool_FromLong(!(mode & 2)
1813 || !(attr & FILE_ATTRIBUTE_READONLY)
1814 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001815#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 int res;
1817 if (!PyArg_ParseTuple(args, "O&i:access",
1818 PyUnicode_FSConverter, &opath, &mode))
1819 return NULL;
1820 path = PyBytes_AsString(opath);
1821 Py_BEGIN_ALLOW_THREADS
1822 res = access(path, mode);
1823 Py_END_ALLOW_THREADS
1824 Py_DECREF(opath);
1825 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001826#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001827}
1828
Guido van Rossumd371ff11999-01-25 16:12:23 +00001829#ifndef F_OK
1830#define F_OK 0
1831#endif
1832#ifndef R_OK
1833#define R_OK 4
1834#endif
1835#ifndef W_OK
1836#define W_OK 2
1837#endif
1838#ifndef X_OK
1839#define X_OK 1
1840#endif
1841
1842#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001843PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001844"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001846
1847static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001848posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001849{
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 int id;
1851 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001852
Victor Stinner8c62be82010-05-06 00:08:46 +00001853 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1854 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001855
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001856#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001857 /* file descriptor 0 only, the default input device (stdin) */
1858 if (id == 0) {
1859 ret = ttyname();
1860 }
1861 else {
1862 ret = NULL;
1863 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001864#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001866#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001867 if (ret == NULL)
1868 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001869 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001870}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001871#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001872
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001873#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001875"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001877
1878static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001879posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001880{
Victor Stinner8c62be82010-05-06 00:08:46 +00001881 char *ret;
1882 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001883
Greg Wardb48bc172000-03-01 21:51:56 +00001884#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001886#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001887 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001888#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001889 if (ret == NULL)
1890 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001891 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001892}
1893#endif
1894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001896"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001898
Barry Warsaw53699e91996-12-10 23:23:01 +00001899static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001900posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001901{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001902#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001903 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001904#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001906#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001907 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001908#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001909 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001910#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001911}
1912
Fred Drake4d1e64b2002-04-15 19:40:07 +00001913#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001915"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001916Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001918
1919static PyObject *
1920posix_fchdir(PyObject *self, PyObject *fdobj)
1921{
Victor Stinner8c62be82010-05-06 00:08:46 +00001922 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001923}
1924#endif /* HAVE_FCHDIR */
1925
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001926
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001927PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001928"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001929Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001930
Barry Warsaw53699e91996-12-10 23:23:01 +00001931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001932posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001933{
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 PyObject *opath = NULL;
1935 char *path = NULL;
1936 int i;
1937 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001938#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001939 DWORD attr;
1940 PyUnicodeObject *po;
1941 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1942 Py_BEGIN_ALLOW_THREADS
1943 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1944 if (attr != 0xFFFFFFFF) {
1945 if (i & _S_IWRITE)
1946 attr &= ~FILE_ATTRIBUTE_READONLY;
1947 else
1948 attr |= FILE_ATTRIBUTE_READONLY;
1949 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1950 }
1951 else
1952 res = 0;
1953 Py_END_ALLOW_THREADS
1954 if (!res)
1955 return win32_error_unicode("chmod",
1956 PyUnicode_AS_UNICODE(po));
1957 Py_INCREF(Py_None);
1958 return Py_None;
1959 }
1960 /* Drop the argument parsing error as narrow strings
1961 are also valid. */
1962 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001963
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1965 &opath, &i))
1966 return NULL;
1967 path = PyBytes_AsString(opath);
1968 Py_BEGIN_ALLOW_THREADS
1969 attr = GetFileAttributesA(path);
1970 if (attr != 0xFFFFFFFF) {
1971 if (i & _S_IWRITE)
1972 attr &= ~FILE_ATTRIBUTE_READONLY;
1973 else
1974 attr |= FILE_ATTRIBUTE_READONLY;
1975 res = SetFileAttributesA(path, attr);
1976 }
1977 else
1978 res = 0;
1979 Py_END_ALLOW_THREADS
1980 if (!res) {
1981 win32_error("chmod", path);
1982 Py_DECREF(opath);
1983 return NULL;
1984 }
1985 Py_DECREF(opath);
1986 Py_INCREF(Py_None);
1987 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001988#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001989 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1990 &opath, &i))
1991 return NULL;
1992 path = PyBytes_AsString(opath);
1993 Py_BEGIN_ALLOW_THREADS
1994 res = chmod(path, i);
1995 Py_END_ALLOW_THREADS
1996 if (res < 0)
1997 return posix_error_with_allocated_filename(opath);
1998 Py_DECREF(opath);
1999 Py_INCREF(Py_None);
2000 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002001#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002002}
2003
Christian Heimes4e30a842007-11-30 22:12:06 +00002004#ifdef HAVE_FCHMOD
2005PyDoc_STRVAR(posix_fchmod__doc__,
2006"fchmod(fd, mode)\n\n\
2007Change the access permissions of the file given by file\n\
2008descriptor fd.");
2009
2010static PyObject *
2011posix_fchmod(PyObject *self, PyObject *args)
2012{
Victor Stinner8c62be82010-05-06 00:08:46 +00002013 int fd, mode, res;
2014 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2015 return NULL;
2016 Py_BEGIN_ALLOW_THREADS
2017 res = fchmod(fd, mode);
2018 Py_END_ALLOW_THREADS
2019 if (res < 0)
2020 return posix_error();
2021 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002022}
2023#endif /* HAVE_FCHMOD */
2024
2025#ifdef HAVE_LCHMOD
2026PyDoc_STRVAR(posix_lchmod__doc__,
2027"lchmod(path, mode)\n\n\
2028Change the access permissions of a file. If path is a symlink, this\n\
2029affects the link itself rather than the target.");
2030
2031static PyObject *
2032posix_lchmod(PyObject *self, PyObject *args)
2033{
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 PyObject *opath;
2035 char *path;
2036 int i;
2037 int res;
2038 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2039 &opath, &i))
2040 return NULL;
2041 path = PyBytes_AsString(opath);
2042 Py_BEGIN_ALLOW_THREADS
2043 res = lchmod(path, i);
2044 Py_END_ALLOW_THREADS
2045 if (res < 0)
2046 return posix_error_with_allocated_filename(opath);
2047 Py_DECREF(opath);
2048 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002049}
2050#endif /* HAVE_LCHMOD */
2051
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002052
Thomas Wouterscf297e42007-02-23 15:07:44 +00002053#ifdef HAVE_CHFLAGS
2054PyDoc_STRVAR(posix_chflags__doc__,
2055"chflags(path, flags)\n\n\
2056Set file flags.");
2057
2058static PyObject *
2059posix_chflags(PyObject *self, PyObject *args)
2060{
Victor Stinner8c62be82010-05-06 00:08:46 +00002061 PyObject *opath;
2062 char *path;
2063 unsigned long flags;
2064 int res;
2065 if (!PyArg_ParseTuple(args, "O&k:chflags",
2066 PyUnicode_FSConverter, &opath, &flags))
2067 return NULL;
2068 path = PyBytes_AsString(opath);
2069 Py_BEGIN_ALLOW_THREADS
2070 res = chflags(path, flags);
2071 Py_END_ALLOW_THREADS
2072 if (res < 0)
2073 return posix_error_with_allocated_filename(opath);
2074 Py_DECREF(opath);
2075 Py_INCREF(Py_None);
2076 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002077}
2078#endif /* HAVE_CHFLAGS */
2079
2080#ifdef HAVE_LCHFLAGS
2081PyDoc_STRVAR(posix_lchflags__doc__,
2082"lchflags(path, flags)\n\n\
2083Set file flags.\n\
2084This function will not follow symbolic links.");
2085
2086static PyObject *
2087posix_lchflags(PyObject *self, PyObject *args)
2088{
Victor Stinner8c62be82010-05-06 00:08:46 +00002089 PyObject *opath;
2090 char *path;
2091 unsigned long flags;
2092 int res;
2093 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2094 PyUnicode_FSConverter, &opath, &flags))
2095 return NULL;
2096 path = PyBytes_AsString(opath);
2097 Py_BEGIN_ALLOW_THREADS
2098 res = lchflags(path, flags);
2099 Py_END_ALLOW_THREADS
2100 if (res < 0)
2101 return posix_error_with_allocated_filename(opath);
2102 Py_DECREF(opath);
2103 Py_INCREF(Py_None);
2104 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002105}
2106#endif /* HAVE_LCHFLAGS */
2107
Martin v. Löwis244edc82001-10-04 22:44:26 +00002108#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002109PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002110"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002111Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002112
2113static PyObject *
2114posix_chroot(PyObject *self, PyObject *args)
2115{
Victor Stinner8c62be82010-05-06 00:08:46 +00002116 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002117}
2118#endif
2119
Guido van Rossum21142a01999-01-08 21:05:37 +00002120#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002121PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002122"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002123force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002124
2125static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002126posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002127{
Stefan Krah0e803b32010-11-26 16:16:47 +00002128 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002129}
2130#endif /* HAVE_FSYNC */
2131
Ross Lagerwall7807c352011-03-17 20:20:30 +02002132#ifdef HAVE_SYNC
2133PyDoc_STRVAR(posix_sync__doc__,
2134"sync()\n\n\
2135Force write of everything to disk.");
2136
2137static PyObject *
2138posix_sync(PyObject *self, PyObject *noargs)
2139{
2140 Py_BEGIN_ALLOW_THREADS
2141 sync();
2142 Py_END_ALLOW_THREADS
2143 Py_RETURN_NONE;
2144}
2145#endif
2146
Guido van Rossum21142a01999-01-08 21:05:37 +00002147#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002148
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002149#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002150extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2151#endif
2152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002153PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002154"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002155force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002156 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002157
2158static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002159posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002160{
Stefan Krah0e803b32010-11-26 16:16:47 +00002161 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002162}
2163#endif /* HAVE_FDATASYNC */
2164
2165
Fredrik Lundh10723342000-07-10 16:38:09 +00002166#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002167PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002168"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002169Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002170
Barry Warsaw53699e91996-12-10 23:23:01 +00002171static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002172posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002173{
Victor Stinner8c62be82010-05-06 00:08:46 +00002174 PyObject *opath;
2175 char *path;
2176 long uid, gid;
2177 int res;
2178 if (!PyArg_ParseTuple(args, "O&ll:chown",
2179 PyUnicode_FSConverter, &opath,
2180 &uid, &gid))
2181 return NULL;
2182 path = PyBytes_AsString(opath);
2183 Py_BEGIN_ALLOW_THREADS
2184 res = chown(path, (uid_t) uid, (gid_t) gid);
2185 Py_END_ALLOW_THREADS
2186 if (res < 0)
2187 return posix_error_with_allocated_filename(opath);
2188 Py_DECREF(opath);
2189 Py_INCREF(Py_None);
2190 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002191}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002192#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002193
Christian Heimes4e30a842007-11-30 22:12:06 +00002194#ifdef HAVE_FCHOWN
2195PyDoc_STRVAR(posix_fchown__doc__,
2196"fchown(fd, uid, gid)\n\n\
2197Change the owner and group id of the file given by file descriptor\n\
2198fd to the numeric uid and gid.");
2199
2200static PyObject *
2201posix_fchown(PyObject *self, PyObject *args)
2202{
Victor Stinner8c62be82010-05-06 00:08:46 +00002203 int fd;
2204 long uid, gid;
2205 int res;
2206 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2207 return NULL;
2208 Py_BEGIN_ALLOW_THREADS
2209 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2210 Py_END_ALLOW_THREADS
2211 if (res < 0)
2212 return posix_error();
2213 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002214}
2215#endif /* HAVE_FCHOWN */
2216
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002217#ifdef HAVE_LCHOWN
2218PyDoc_STRVAR(posix_lchown__doc__,
2219"lchown(path, uid, gid)\n\n\
2220Change the owner and group id of path to the numeric uid and gid.\n\
2221This function will not follow symbolic links.");
2222
2223static PyObject *
2224posix_lchown(PyObject *self, PyObject *args)
2225{
Victor Stinner8c62be82010-05-06 00:08:46 +00002226 PyObject *opath;
2227 char *path;
2228 long uid, gid;
2229 int res;
2230 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2231 PyUnicode_FSConverter, &opath,
2232 &uid, &gid))
2233 return NULL;
2234 path = PyBytes_AsString(opath);
2235 Py_BEGIN_ALLOW_THREADS
2236 res = lchown(path, (uid_t) uid, (gid_t) gid);
2237 Py_END_ALLOW_THREADS
2238 if (res < 0)
2239 return posix_error_with_allocated_filename(opath);
2240 Py_DECREF(opath);
2241 Py_INCREF(Py_None);
2242 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002243}
2244#endif /* HAVE_LCHOWN */
2245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002246
Guido van Rossum36bc6801995-06-14 22:54:23 +00002247#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002248static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002249posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002250{
Victor Stinner8c62be82010-05-06 00:08:46 +00002251 char buf[1026];
2252 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002253
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002254#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 if (!use_bytes) {
2256 wchar_t wbuf[1026];
2257 wchar_t *wbuf2 = wbuf;
2258 PyObject *resobj;
2259 DWORD len;
2260 Py_BEGIN_ALLOW_THREADS
2261 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2262 /* If the buffer is large enough, len does not include the
2263 terminating \0. If the buffer is too small, len includes
2264 the space needed for the terminator. */
2265 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2266 wbuf2 = malloc(len * sizeof(wchar_t));
2267 if (wbuf2)
2268 len = GetCurrentDirectoryW(len, wbuf2);
2269 }
2270 Py_END_ALLOW_THREADS
2271 if (!wbuf2) {
2272 PyErr_NoMemory();
2273 return NULL;
2274 }
2275 if (!len) {
2276 if (wbuf2 != wbuf) free(wbuf2);
2277 return win32_error("getcwdu", NULL);
2278 }
2279 resobj = PyUnicode_FromWideChar(wbuf2, len);
2280 if (wbuf2 != wbuf) free(wbuf2);
2281 return resobj;
2282 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002283#endif
2284
Victor Stinner8c62be82010-05-06 00:08:46 +00002285 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002286#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002287 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002288#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002289 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002290#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002291 Py_END_ALLOW_THREADS
2292 if (res == NULL)
2293 return posix_error();
2294 if (use_bytes)
2295 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002296 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002297}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002298
2299PyDoc_STRVAR(posix_getcwd__doc__,
2300"getcwd() -> path\n\n\
2301Return a unicode string representing the current working directory.");
2302
2303static PyObject *
2304posix_getcwd_unicode(PyObject *self)
2305{
2306 return posix_getcwd(0);
2307}
2308
2309PyDoc_STRVAR(posix_getcwdb__doc__,
2310"getcwdb() -> path\n\n\
2311Return a bytes string representing the current working directory.");
2312
2313static PyObject *
2314posix_getcwd_bytes(PyObject *self)
2315{
2316 return posix_getcwd(1);
2317}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002319
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002320
Guido van Rossumb6775db1994-08-01 11:34:53 +00002321#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002322PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002323"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002324Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002325
Barry Warsaw53699e91996-12-10 23:23:01 +00002326static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002327posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002328{
Victor Stinner8c62be82010-05-06 00:08:46 +00002329 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002330}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002331#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002332
Brian Curtin1b9df392010-11-24 20:24:31 +00002333#ifdef MS_WINDOWS
2334PyDoc_STRVAR(win32_link__doc__,
2335"link(src, dst)\n\n\
2336Create a hard link to a file.");
2337
2338static PyObject *
2339win32_link(PyObject *self, PyObject *args)
2340{
2341 PyObject *osrc, *odst;
2342 char *src, *dst;
2343 BOOL rslt;
2344
Brian Curtinfc889c42010-11-28 23:59:46 +00002345 PyUnicodeObject *usrc, *udst;
2346 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2347 Py_BEGIN_ALLOW_THREADS
2348 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2349 PyUnicode_AS_UNICODE(usrc), NULL);
2350 Py_END_ALLOW_THREADS
2351
2352 if (rslt == 0)
2353 return win32_error("link", NULL);
2354
2355 Py_RETURN_NONE;
2356 }
2357
2358 /* Narrow strings also valid. */
2359 PyErr_Clear();
2360
Brian Curtin1b9df392010-11-24 20:24:31 +00002361 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2362 PyUnicode_FSConverter, &odst))
2363 return NULL;
2364
2365 src = PyBytes_AsString(osrc);
2366 dst = PyBytes_AsString(odst);
2367
2368 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002369 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002370 Py_END_ALLOW_THREADS
2371
Stefan Krah30b341f2010-11-27 11:44:18 +00002372 Py_DECREF(osrc);
2373 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002374 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002375 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002376
2377 Py_RETURN_NONE;
2378}
2379#endif /* MS_WINDOWS */
2380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002382PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002383"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002384Return a list containing the names of the entries in the directory.\n\
2385\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002386 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002387\n\
2388The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002389entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002390
Barry Warsaw53699e91996-12-10 23:23:01 +00002391static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002392posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002393{
Victor Stinner8c62be82010-05-06 00:08:46 +00002394 /* XXX Should redo this putting the (now four) versions of opendir
2395 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002396#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002397
Victor Stinner8c62be82010-05-06 00:08:46 +00002398 PyObject *d, *v;
2399 HANDLE hFindFile;
2400 BOOL result;
2401 WIN32_FIND_DATA FileData;
2402 PyObject *opath;
2403 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2404 char *bufptr = namebuf;
2405 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002406
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002407 PyObject *po = NULL;
2408 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002409 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002410 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002411
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002412 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002413 po_wchars = L".";
2414 len = 1;
2415 } else {
2416 po_wchars = PyUnicode_AS_UNICODE(po);
2417 len = PyUnicode_GET_SIZE(po);
2418 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002419 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002420 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2421 if (!wnamebuf) {
2422 PyErr_NoMemory();
2423 return NULL;
2424 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002425 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002426 if (len > 0) {
2427 Py_UNICODE wch = wnamebuf[len-1];
2428 if (wch != L'/' && wch != L'\\' && wch != L':')
2429 wnamebuf[len++] = L'\\';
2430 wcscpy(wnamebuf + len, L"*.*");
2431 }
2432 if ((d = PyList_New(0)) == NULL) {
2433 free(wnamebuf);
2434 return NULL;
2435 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002436 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002437 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002438 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002439 if (hFindFile == INVALID_HANDLE_VALUE) {
2440 int error = GetLastError();
2441 if (error == ERROR_FILE_NOT_FOUND) {
2442 free(wnamebuf);
2443 return d;
2444 }
2445 Py_DECREF(d);
2446 win32_error_unicode("FindFirstFileW", wnamebuf);
2447 free(wnamebuf);
2448 return NULL;
2449 }
2450 do {
2451 /* Skip over . and .. */
2452 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2453 wcscmp(wFileData.cFileName, L"..") != 0) {
2454 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2455 if (v == NULL) {
2456 Py_DECREF(d);
2457 d = NULL;
2458 break;
2459 }
2460 if (PyList_Append(d, v) != 0) {
2461 Py_DECREF(v);
2462 Py_DECREF(d);
2463 d = NULL;
2464 break;
2465 }
2466 Py_DECREF(v);
2467 }
2468 Py_BEGIN_ALLOW_THREADS
2469 result = FindNextFileW(hFindFile, &wFileData);
2470 Py_END_ALLOW_THREADS
2471 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2472 it got to the end of the directory. */
2473 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2474 Py_DECREF(d);
2475 win32_error_unicode("FindNextFileW", wnamebuf);
2476 FindClose(hFindFile);
2477 free(wnamebuf);
2478 return NULL;
2479 }
2480 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002481
Victor Stinner8c62be82010-05-06 00:08:46 +00002482 if (FindClose(hFindFile) == FALSE) {
2483 Py_DECREF(d);
2484 win32_error_unicode("FindClose", wnamebuf);
2485 free(wnamebuf);
2486 return NULL;
2487 }
2488 free(wnamebuf);
2489 return d;
2490 }
2491 /* Drop the argument parsing error as narrow strings
2492 are also valid. */
2493 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002494
Victor Stinner8c62be82010-05-06 00:08:46 +00002495 if (!PyArg_ParseTuple(args, "O&:listdir",
2496 PyUnicode_FSConverter, &opath))
2497 return NULL;
2498 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2499 PyErr_SetString(PyExc_ValueError, "path too long");
2500 Py_DECREF(opath);
2501 return NULL;
2502 }
2503 strcpy(namebuf, PyBytes_AsString(opath));
2504 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002505 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002506 if (len > 0) {
2507 char ch = namebuf[len-1];
2508 if (ch != SEP && ch != ALTSEP && ch != ':')
2509 namebuf[len++] = '/';
2510 strcpy(namebuf + len, "*.*");
2511 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002512
Victor Stinner8c62be82010-05-06 00:08:46 +00002513 if ((d = PyList_New(0)) == NULL)
2514 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002515
Antoine Pitroub73caab2010-08-09 23:39:31 +00002516 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002517 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002518 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002519 if (hFindFile == INVALID_HANDLE_VALUE) {
2520 int error = GetLastError();
2521 if (error == ERROR_FILE_NOT_FOUND)
2522 return d;
2523 Py_DECREF(d);
2524 return win32_error("FindFirstFile", namebuf);
2525 }
2526 do {
2527 /* Skip over . and .. */
2528 if (strcmp(FileData.cFileName, ".") != 0 &&
2529 strcmp(FileData.cFileName, "..") != 0) {
2530 v = PyBytes_FromString(FileData.cFileName);
2531 if (v == NULL) {
2532 Py_DECREF(d);
2533 d = NULL;
2534 break;
2535 }
2536 if (PyList_Append(d, v) != 0) {
2537 Py_DECREF(v);
2538 Py_DECREF(d);
2539 d = NULL;
2540 break;
2541 }
2542 Py_DECREF(v);
2543 }
2544 Py_BEGIN_ALLOW_THREADS
2545 result = FindNextFile(hFindFile, &FileData);
2546 Py_END_ALLOW_THREADS
2547 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2548 it got to the end of the directory. */
2549 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2550 Py_DECREF(d);
2551 win32_error("FindNextFile", namebuf);
2552 FindClose(hFindFile);
2553 return NULL;
2554 }
2555 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002556
Victor Stinner8c62be82010-05-06 00:08:46 +00002557 if (FindClose(hFindFile) == FALSE) {
2558 Py_DECREF(d);
2559 return win32_error("FindClose", namebuf);
2560 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002561
Victor Stinner8c62be82010-05-06 00:08:46 +00002562 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002563
Tim Peters0bb44a42000-09-15 07:44:49 +00002564#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002565
2566#ifndef MAX_PATH
2567#define MAX_PATH CCHMAXPATH
2568#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002569 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002570 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002571 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002572 PyObject *d, *v;
2573 char namebuf[MAX_PATH+5];
2574 HDIR hdir = 1;
2575 ULONG srchcnt = 1;
2576 FILEFINDBUF3 ep;
2577 APIRET rc;
2578
Victor Stinner8c62be82010-05-06 00:08:46 +00002579 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002580 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002581 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002582 name = PyBytes_AsString(oname);
2583 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002584 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002585 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002586 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002587 return NULL;
2588 }
2589 strcpy(namebuf, name);
2590 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002591 if (*pt == ALTSEP)
2592 *pt = SEP;
2593 if (namebuf[len-1] != SEP)
2594 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002595 strcpy(namebuf + len, "*.*");
2596
Neal Norwitz6c913782007-10-14 03:23:09 +00002597 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002598 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002599 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002600 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002601
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002602 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2603 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002604 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002605 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2606 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2607 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002608
2609 if (rc != NO_ERROR) {
2610 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002611 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002612 }
2613
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002614 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002615 do {
2616 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002617 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002618 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002619
2620 strcpy(namebuf, ep.achName);
2621
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002622 /* Leave Case of Name Alone -- In Native Form */
2623 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002624
Christian Heimes72b710a2008-05-26 13:28:38 +00002625 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002626 if (v == NULL) {
2627 Py_DECREF(d);
2628 d = NULL;
2629 break;
2630 }
2631 if (PyList_Append(d, v) != 0) {
2632 Py_DECREF(v);
2633 Py_DECREF(d);
2634 d = NULL;
2635 break;
2636 }
2637 Py_DECREF(v);
2638 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2639 }
2640
Victor Stinnerdcb24032010-04-22 12:08:36 +00002641 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002642 return d;
2643#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 PyObject *oname;
2645 char *name;
2646 PyObject *d, *v;
2647 DIR *dirp;
2648 struct dirent *ep;
2649 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002650
Victor Stinner8c62be82010-05-06 00:08:46 +00002651 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002652 /* v is never read, so it does not need to be initialized yet. */
2653 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002654 arg_is_unicode = 0;
2655 PyErr_Clear();
2656 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002657 oname = NULL;
2658 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002659 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002660 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002661 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002662 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002664 Py_BEGIN_ALLOW_THREADS
2665 dirp = opendir(name);
2666 Py_END_ALLOW_THREADS
2667 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002668 return posix_error_with_allocated_filename(oname);
2669 }
2670 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002671 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002672 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002673 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002674 Py_DECREF(oname);
2675 return NULL;
2676 }
2677 for (;;) {
2678 errno = 0;
2679 Py_BEGIN_ALLOW_THREADS
2680 ep = readdir(dirp);
2681 Py_END_ALLOW_THREADS
2682 if (ep == NULL) {
2683 if (errno == 0) {
2684 break;
2685 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002686 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002687 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002688 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002689 Py_DECREF(d);
2690 return posix_error_with_allocated_filename(oname);
2691 }
2692 }
2693 if (ep->d_name[0] == '.' &&
2694 (NAMLEN(ep) == 1 ||
2695 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2696 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002697 if (arg_is_unicode)
2698 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2699 else
2700 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002701 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002702 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002703 break;
2704 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002705 if (PyList_Append(d, v) != 0) {
2706 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002707 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002708 break;
2709 }
2710 Py_DECREF(v);
2711 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002712 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002713 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002714 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002715 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002716
Victor Stinner8c62be82010-05-06 00:08:46 +00002717 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002718
Tim Peters0bb44a42000-09-15 07:44:49 +00002719#endif /* which OS */
2720} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002721
Antoine Pitrou8250e232011-02-25 23:41:16 +00002722#ifdef HAVE_FDOPENDIR
2723PyDoc_STRVAR(posix_fdlistdir__doc__,
2724"fdlistdir(fd) -> list_of_strings\n\n\
2725Like listdir(), but uses a file descriptor instead.\n\
2726After succesful execution of this function, fd will be closed.");
2727
2728static PyObject *
2729posix_fdlistdir(PyObject *self, PyObject *args)
2730{
2731 PyObject *d, *v;
2732 DIR *dirp;
2733 struct dirent *ep;
2734 int fd;
2735
2736 errno = 0;
2737 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2738 return NULL;
2739 Py_BEGIN_ALLOW_THREADS
2740 dirp = fdopendir(fd);
2741 Py_END_ALLOW_THREADS
2742 if (dirp == NULL) {
2743 close(fd);
2744 return posix_error();
2745 }
2746 if ((d = PyList_New(0)) == NULL) {
2747 Py_BEGIN_ALLOW_THREADS
2748 closedir(dirp);
2749 Py_END_ALLOW_THREADS
2750 return NULL;
2751 }
2752 for (;;) {
2753 errno = 0;
2754 Py_BEGIN_ALLOW_THREADS
2755 ep = readdir(dirp);
2756 Py_END_ALLOW_THREADS
2757 if (ep == NULL) {
2758 if (errno == 0) {
2759 break;
2760 } else {
2761 Py_BEGIN_ALLOW_THREADS
2762 closedir(dirp);
2763 Py_END_ALLOW_THREADS
2764 Py_DECREF(d);
2765 return posix_error();
2766 }
2767 }
2768 if (ep->d_name[0] == '.' &&
2769 (NAMLEN(ep) == 1 ||
2770 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2771 continue;
2772 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2773 if (v == NULL) {
2774 Py_CLEAR(d);
2775 break;
2776 }
2777 if (PyList_Append(d, v) != 0) {
2778 Py_DECREF(v);
2779 Py_CLEAR(d);
2780 break;
2781 }
2782 Py_DECREF(v);
2783 }
2784 Py_BEGIN_ALLOW_THREADS
2785 closedir(dirp);
2786 Py_END_ALLOW_THREADS
2787
2788 return d;
2789}
2790#endif
2791
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002792#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002793/* A helper function for abspath on win32 */
2794static PyObject *
2795posix__getfullpathname(PyObject *self, PyObject *args)
2796{
Victor Stinner8c62be82010-05-06 00:08:46 +00002797 PyObject *opath;
2798 char *path;
2799 char outbuf[MAX_PATH*2];
2800 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002801#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002802 PyUnicodeObject *po;
2803 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2804 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2805 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2806 Py_UNICODE *wtemp;
2807 DWORD result;
2808 PyObject *v;
2809 result = GetFullPathNameW(wpath,
2810 sizeof(woutbuf)/sizeof(woutbuf[0]),
2811 woutbuf, &wtemp);
2812 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2813 woutbufp = malloc(result * sizeof(Py_UNICODE));
2814 if (!woutbufp)
2815 return PyErr_NoMemory();
2816 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2817 }
2818 if (result)
2819 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2820 else
2821 v = win32_error_unicode("GetFullPathNameW", wpath);
2822 if (woutbufp != woutbuf)
2823 free(woutbufp);
2824 return v;
2825 }
2826 /* Drop the argument parsing error as narrow strings
2827 are also valid. */
2828 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002829
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002830#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002831 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2832 PyUnicode_FSConverter, &opath))
2833 return NULL;
2834 path = PyBytes_AsString(opath);
2835 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2836 outbuf, &temp)) {
2837 win32_error("GetFullPathName", path);
2838 Py_DECREF(opath);
2839 return NULL;
2840 }
2841 Py_DECREF(opath);
2842 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2843 return PyUnicode_Decode(outbuf, strlen(outbuf),
2844 Py_FileSystemDefaultEncoding, NULL);
2845 }
2846 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002847} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002848
Brian Curtinf5e76d02010-11-24 13:14:05 +00002849/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2850static int has_GetFinalPathNameByHandle = 0;
2851static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2852 DWORD);
2853static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2854 DWORD);
2855static int
2856check_GetFinalPathNameByHandle()
2857{
2858 HINSTANCE hKernel32;
2859 /* only recheck */
2860 if (!has_GetFinalPathNameByHandle)
2861 {
2862 hKernel32 = GetModuleHandle("KERNEL32");
2863 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2864 "GetFinalPathNameByHandleA");
2865 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2866 "GetFinalPathNameByHandleW");
2867 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2868 Py_GetFinalPathNameByHandleW;
2869 }
2870 return has_GetFinalPathNameByHandle;
2871}
2872
Brian Curtind40e6f72010-07-08 21:39:08 +00002873/* A helper function for samepath on windows */
2874static PyObject *
2875posix__getfinalpathname(PyObject *self, PyObject *args)
2876{
2877 HANDLE hFile;
2878 int buf_size;
2879 wchar_t *target_path;
2880 int result_length;
2881 PyObject *result;
2882 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002883
Brian Curtin94622b02010-09-24 00:03:39 +00002884 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002885 return NULL;
2886 }
2887
2888 if(!check_GetFinalPathNameByHandle()) {
2889 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2890 NotImplementedError. */
2891 return PyErr_Format(PyExc_NotImplementedError,
2892 "GetFinalPathNameByHandle not available on this platform");
2893 }
2894
2895 hFile = CreateFileW(
2896 path,
2897 0, /* desired access */
2898 0, /* share mode */
2899 NULL, /* security attributes */
2900 OPEN_EXISTING,
2901 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2902 FILE_FLAG_BACKUP_SEMANTICS,
2903 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002904
Brian Curtind40e6f72010-07-08 21:39:08 +00002905 if(hFile == INVALID_HANDLE_VALUE) {
2906 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002907 return PyErr_Format(PyExc_RuntimeError,
2908 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002909 }
2910
2911 /* We have a good handle to the target, use it to determine the
2912 target path name. */
2913 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2914
2915 if(!buf_size)
2916 return win32_error_unicode("GetFinalPathNameByHandle", path);
2917
2918 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2919 if(!target_path)
2920 return PyErr_NoMemory();
2921
2922 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2923 buf_size, VOLUME_NAME_DOS);
2924 if(!result_length)
2925 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2926
2927 if(!CloseHandle(hFile))
2928 return win32_error_unicode("GetFinalPathNameByHandle", path);
2929
2930 target_path[result_length] = 0;
2931 result = PyUnicode_FromUnicode(target_path, result_length);
2932 free(target_path);
2933 return result;
2934
2935} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002936
2937static PyObject *
2938posix__getfileinformation(PyObject *self, PyObject *args)
2939{
2940 HANDLE hFile;
2941 BY_HANDLE_FILE_INFORMATION info;
2942 int fd;
2943
2944 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2945 return NULL;
2946
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002947 if (!_PyVerify_fd(fd))
2948 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002949
2950 hFile = (HANDLE)_get_osfhandle(fd);
2951 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002952 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002953
2954 if (!GetFileInformationByHandle(hFile, &info))
2955 return win32_error("_getfileinformation", NULL);
2956
2957 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2958 info.nFileIndexHigh,
2959 info.nFileIndexLow);
2960}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002961#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002963PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002964"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002965Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002966
Barry Warsaw53699e91996-12-10 23:23:01 +00002967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002968posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002969{
Victor Stinner8c62be82010-05-06 00:08:46 +00002970 int res;
2971 PyObject *opath;
2972 char *path;
2973 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002974
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002975#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002976 PyUnicodeObject *po;
2977 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2978 Py_BEGIN_ALLOW_THREADS
2979 /* PyUnicode_AS_UNICODE OK without thread lock as
2980 it is a simple dereference. */
2981 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2982 Py_END_ALLOW_THREADS
2983 if (!res)
2984 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2985 Py_INCREF(Py_None);
2986 return Py_None;
2987 }
2988 /* Drop the argument parsing error as narrow strings
2989 are also valid. */
2990 PyErr_Clear();
2991 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2992 PyUnicode_FSConverter, &opath, &mode))
2993 return NULL;
2994 path = PyBytes_AsString(opath);
2995 Py_BEGIN_ALLOW_THREADS
2996 /* PyUnicode_AS_UNICODE OK without thread lock as
2997 it is a simple dereference. */
2998 res = CreateDirectoryA(path, NULL);
2999 Py_END_ALLOW_THREADS
3000 if (!res) {
3001 win32_error("mkdir", path);
3002 Py_DECREF(opath);
3003 return NULL;
3004 }
3005 Py_DECREF(opath);
3006 Py_INCREF(Py_None);
3007 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003008#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003009
Victor Stinner8c62be82010-05-06 00:08:46 +00003010 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3011 PyUnicode_FSConverter, &opath, &mode))
3012 return NULL;
3013 path = PyBytes_AsString(opath);
3014 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003015#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003016 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003017#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003018 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003019#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003020 Py_END_ALLOW_THREADS
3021 if (res < 0)
3022 return posix_error_with_allocated_filename(opath);
3023 Py_DECREF(opath);
3024 Py_INCREF(Py_None);
3025 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003026#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003027}
3028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003029
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003030/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3031#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003032#include <sys/resource.h>
3033#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003034
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003035
3036#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003038"nice(inc) -> new_priority\n\n\
3039Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003040
Barry Warsaw53699e91996-12-10 23:23:01 +00003041static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003042posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003043{
Victor Stinner8c62be82010-05-06 00:08:46 +00003044 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003045
Victor Stinner8c62be82010-05-06 00:08:46 +00003046 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3047 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003048
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 /* There are two flavours of 'nice': one that returns the new
3050 priority (as required by almost all standards out there) and the
3051 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3052 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003053
Victor Stinner8c62be82010-05-06 00:08:46 +00003054 If we are of the nice family that returns the new priority, we
3055 need to clear errno before the call, and check if errno is filled
3056 before calling posix_error() on a returnvalue of -1, because the
3057 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003058
Victor Stinner8c62be82010-05-06 00:08:46 +00003059 errno = 0;
3060 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003061#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003062 if (value == 0)
3063 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003064#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003065 if (value == -1 && errno != 0)
3066 /* either nice() or getpriority() returned an error */
3067 return posix_error();
3068 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003069}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003070#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003071
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003072
3073#ifdef HAVE_GETPRIORITY
3074PyDoc_STRVAR(posix_getpriority__doc__,
3075"getpriority(which, who) -> current_priority\n\n\
3076Get program scheduling priority.");
3077
3078static PyObject *
3079posix_getpriority(PyObject *self, PyObject *args)
3080{
3081 int which, who, retval;
3082
3083 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3084 return NULL;
3085 errno = 0;
3086 Py_BEGIN_ALLOW_THREADS
3087 retval = getpriority(which, who);
3088 Py_END_ALLOW_THREADS
3089 if (errno != 0)
3090 return posix_error();
3091 return PyLong_FromLong((long)retval);
3092}
3093#endif /* HAVE_GETPRIORITY */
3094
3095
3096#ifdef HAVE_SETPRIORITY
3097PyDoc_STRVAR(posix_setpriority__doc__,
3098"setpriority(which, who, prio) -> None\n\n\
3099Set program scheduling priority.");
3100
3101static PyObject *
3102posix_setpriority(PyObject *self, PyObject *args)
3103{
3104 int which, who, prio, retval;
3105
3106 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3107 return NULL;
3108 Py_BEGIN_ALLOW_THREADS
3109 retval = setpriority(which, who, prio);
3110 Py_END_ALLOW_THREADS
3111 if (retval == -1)
3112 return posix_error();
3113 Py_RETURN_NONE;
3114}
3115#endif /* HAVE_SETPRIORITY */
3116
3117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003118PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003119"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003120Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003121
Barry Warsaw53699e91996-12-10 23:23:01 +00003122static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003123posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003124{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003125#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003126 PyObject *o1, *o2;
3127 char *p1, *p2;
3128 BOOL result;
3129 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3130 goto error;
3131 if (!convert_to_unicode(&o1))
3132 goto error;
3133 if (!convert_to_unicode(&o2)) {
3134 Py_DECREF(o1);
3135 goto error;
3136 }
3137 Py_BEGIN_ALLOW_THREADS
3138 result = MoveFileW(PyUnicode_AsUnicode(o1),
3139 PyUnicode_AsUnicode(o2));
3140 Py_END_ALLOW_THREADS
3141 Py_DECREF(o1);
3142 Py_DECREF(o2);
3143 if (!result)
3144 return win32_error("rename", NULL);
3145 Py_INCREF(Py_None);
3146 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003147error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 PyErr_Clear();
3149 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3150 return NULL;
3151 Py_BEGIN_ALLOW_THREADS
3152 result = MoveFileA(p1, p2);
3153 Py_END_ALLOW_THREADS
3154 if (!result)
3155 return win32_error("rename", NULL);
3156 Py_INCREF(Py_None);
3157 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003158#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003160#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003161}
3162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003164PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003165"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003166Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167
Barry Warsaw53699e91996-12-10 23:23:01 +00003168static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003169posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003170{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003171#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003172 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003173#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003174 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003175#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003176}
3177
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003179PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003180"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003181Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003182
Barry Warsaw53699e91996-12-10 23:23:01 +00003183static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003184posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003185{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003186#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003187 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003188#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003190#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003191}
3192
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003193
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003194#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003195PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003196"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003197Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198
Barry Warsaw53699e91996-12-10 23:23:01 +00003199static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003200posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003201{
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003203#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003204 wchar_t *command;
3205 if (!PyArg_ParseTuple(args, "u:system", &command))
3206 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003207
Victor Stinner8c62be82010-05-06 00:08:46 +00003208 Py_BEGIN_ALLOW_THREADS
3209 sts = _wsystem(command);
3210 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003211#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003212 PyObject *command_obj;
3213 char *command;
3214 if (!PyArg_ParseTuple(args, "O&:system",
3215 PyUnicode_FSConverter, &command_obj))
3216 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003217
Victor Stinner8c62be82010-05-06 00:08:46 +00003218 command = PyBytes_AsString(command_obj);
3219 Py_BEGIN_ALLOW_THREADS
3220 sts = system(command);
3221 Py_END_ALLOW_THREADS
3222 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003223#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003224 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003225}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003226#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003227
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003229PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003230"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003231Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003232
Barry Warsaw53699e91996-12-10 23:23:01 +00003233static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003234posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003235{
Victor Stinner8c62be82010-05-06 00:08:46 +00003236 int i;
3237 if (!PyArg_ParseTuple(args, "i:umask", &i))
3238 return NULL;
3239 i = (int)umask(i);
3240 if (i < 0)
3241 return posix_error();
3242 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003243}
3244
Brian Curtind40e6f72010-07-08 21:39:08 +00003245#ifdef MS_WINDOWS
3246
3247/* override the default DeleteFileW behavior so that directory
3248symlinks can be removed with this function, the same as with
3249Unix symlinks */
3250BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3251{
3252 WIN32_FILE_ATTRIBUTE_DATA info;
3253 WIN32_FIND_DATAW find_data;
3254 HANDLE find_data_handle;
3255 int is_directory = 0;
3256 int is_link = 0;
3257
3258 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3259 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003260
Brian Curtind40e6f72010-07-08 21:39:08 +00003261 /* Get WIN32_FIND_DATA structure for the path to determine if
3262 it is a symlink */
3263 if(is_directory &&
3264 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3265 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3266
3267 if(find_data_handle != INVALID_HANDLE_VALUE) {
3268 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3269 FindClose(find_data_handle);
3270 }
3271 }
3272 }
3273
3274 if (is_directory && is_link)
3275 return RemoveDirectoryW(lpFileName);
3276
3277 return DeleteFileW(lpFileName);
3278}
3279#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003281PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003282"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003283Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003284
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003285PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003286"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003287Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003288
Barry Warsaw53699e91996-12-10 23:23:01 +00003289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003290posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003291{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003292#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003293 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3294 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003295#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003297#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003298}
3299
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003300
Guido van Rossumb6775db1994-08-01 11:34:53 +00003301#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003302PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003303"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003304Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003305
Barry Warsaw53699e91996-12-10 23:23:01 +00003306static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003307posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003308{
Victor Stinner8c62be82010-05-06 00:08:46 +00003309 struct utsname u;
3310 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003311
Victor Stinner8c62be82010-05-06 00:08:46 +00003312 Py_BEGIN_ALLOW_THREADS
3313 res = uname(&u);
3314 Py_END_ALLOW_THREADS
3315 if (res < 0)
3316 return posix_error();
3317 return Py_BuildValue("(sssss)",
3318 u.sysname,
3319 u.nodename,
3320 u.release,
3321 u.version,
3322 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003323}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003324#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003325
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003326static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003327extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003328{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003329 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 if (PyFloat_Check(t)) {
3331 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003332 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 if (!intobj)
3334 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003335#if SIZEOF_TIME_T > SIZEOF_LONG
3336 intval = PyLong_AsUnsignedLongLongMask(intobj);
3337#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003338 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003339#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003340 Py_DECREF(intobj);
3341 if (intval == -1 && PyErr_Occurred())
3342 return -1;
3343 *sec = intval;
3344 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3345 if (*usec < 0)
3346 /* If rounding gave us a negative number,
3347 truncate. */
3348 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003349 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003351#if SIZEOF_TIME_T > SIZEOF_LONG
3352 intval = PyLong_AsUnsignedLongLongMask(t);
3353#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003354 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003355#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 if (intval == -1 && PyErr_Occurred())
3357 return -1;
3358 *sec = intval;
3359 *usec = 0;
3360 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003361}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003363PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003364"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003365utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003366Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003367second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003368
Barry Warsaw53699e91996-12-10 23:23:01 +00003369static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003370posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003371{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003372#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003373 PyObject *arg;
3374 PyUnicodeObject *obwpath;
3375 wchar_t *wpath = NULL;
3376 PyObject *oapath;
3377 char *apath;
3378 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003379 time_t atimesec, mtimesec;
3380 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003381 FILETIME atime, mtime;
3382 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003383
Victor Stinner8c62be82010-05-06 00:08:46 +00003384 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3385 wpath = PyUnicode_AS_UNICODE(obwpath);
3386 Py_BEGIN_ALLOW_THREADS
3387 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3388 NULL, OPEN_EXISTING,
3389 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3390 Py_END_ALLOW_THREADS
3391 if (hFile == INVALID_HANDLE_VALUE)
3392 return win32_error_unicode("utime", wpath);
3393 } else
3394 /* Drop the argument parsing error as narrow strings
3395 are also valid. */
3396 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003397
Victor Stinner8c62be82010-05-06 00:08:46 +00003398 if (!wpath) {
3399 if (!PyArg_ParseTuple(args, "O&O:utime",
3400 PyUnicode_FSConverter, &oapath, &arg))
3401 return NULL;
3402 apath = PyBytes_AsString(oapath);
3403 Py_BEGIN_ALLOW_THREADS
3404 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3405 NULL, OPEN_EXISTING,
3406 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3407 Py_END_ALLOW_THREADS
3408 if (hFile == INVALID_HANDLE_VALUE) {
3409 win32_error("utime", apath);
3410 Py_DECREF(oapath);
3411 return NULL;
3412 }
3413 Py_DECREF(oapath);
3414 }
3415
3416 if (arg == Py_None) {
3417 SYSTEMTIME now;
3418 GetSystemTime(&now);
3419 if (!SystemTimeToFileTime(&now, &mtime) ||
3420 !SystemTimeToFileTime(&now, &atime)) {
3421 win32_error("utime", NULL);
3422 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003423 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003424 }
3425 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3426 PyErr_SetString(PyExc_TypeError,
3427 "utime() arg 2 must be a tuple (atime, mtime)");
3428 goto done;
3429 }
3430 else {
3431 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3432 &atimesec, &ausec) == -1)
3433 goto done;
3434 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3435 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3436 &mtimesec, &musec) == -1)
3437 goto done;
3438 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3439 }
3440 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3441 /* Avoid putting the file name into the error here,
3442 as that may confuse the user into believing that
3443 something is wrong with the file, when it also
3444 could be the time stamp that gives a problem. */
3445 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003446 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 }
3448 Py_INCREF(Py_None);
3449 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003450done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003451 CloseHandle(hFile);
3452 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003453#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003454
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 PyObject *opath;
3456 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003457 time_t atime, mtime;
3458 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003459 int res;
3460 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003461
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003462#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003464#define ATIME buf[0].tv_sec
3465#define MTIME buf[1].tv_sec
3466#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003467/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003468 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003469#define ATIME buf.actime
3470#define MTIME buf.modtime
3471#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003472#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003473 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003474#define ATIME buf[0]
3475#define MTIME buf[1]
3476#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003477#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003478
Mark Hammond817c9292003-12-03 01:22:38 +00003479
Victor Stinner8c62be82010-05-06 00:08:46 +00003480 if (!PyArg_ParseTuple(args, "O&O:utime",
3481 PyUnicode_FSConverter, &opath, &arg))
3482 return NULL;
3483 path = PyBytes_AsString(opath);
3484 if (arg == Py_None) {
3485 /* optional time values not given */
3486 Py_BEGIN_ALLOW_THREADS
3487 res = utime(path, NULL);
3488 Py_END_ALLOW_THREADS
3489 }
3490 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3491 PyErr_SetString(PyExc_TypeError,
3492 "utime() arg 2 must be a tuple (atime, mtime)");
3493 Py_DECREF(opath);
3494 return NULL;
3495 }
3496 else {
3497 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3498 &atime, &ausec) == -1) {
3499 Py_DECREF(opath);
3500 return NULL;
3501 }
3502 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3503 &mtime, &musec) == -1) {
3504 Py_DECREF(opath);
3505 return NULL;
3506 }
3507 ATIME = atime;
3508 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003509#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003510 buf[0].tv_usec = ausec;
3511 buf[1].tv_usec = musec;
3512 Py_BEGIN_ALLOW_THREADS
3513 res = utimes(path, buf);
3514 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003515#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 Py_BEGIN_ALLOW_THREADS
3517 res = utime(path, UTIME_ARG);
3518 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003519#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003520 }
3521 if (res < 0) {
3522 return posix_error_with_allocated_filename(opath);
3523 }
3524 Py_DECREF(opath);
3525 Py_INCREF(Py_None);
3526 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003527#undef UTIME_ARG
3528#undef ATIME
3529#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003530#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003531}
3532
Ross Lagerwall7807c352011-03-17 20:20:30 +02003533#ifdef HAVE_FUTIMES
3534PyDoc_STRVAR(posix_futimes__doc__,
3535"futimes(fd, (atime, mtime))\n\
3536futimes(fd, None)\n\n\
3537Set the access and modified time of the file specified by the file\n\
3538descriptor fd to the given values. If the second form is used, set the\n\
3539access and modified times to the current time.");
3540
3541static PyObject *
3542posix_futimes(PyObject *self, PyObject *args)
3543{
3544 int res, fd;
3545 PyObject* arg;
3546 struct timeval buf[2];
3547 long ausec, musec;
3548
3549 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3550 return NULL;
3551
3552 if (arg == Py_None) {
3553 /* optional time values not given */
3554 Py_BEGIN_ALLOW_THREADS
3555 res = futimes(fd, NULL);
3556 Py_END_ALLOW_THREADS
3557 }
3558 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3559 PyErr_SetString(PyExc_TypeError,
3560 "futimes() arg 2 must be a tuple (atime, mtime)");
3561 return NULL;
3562 }
3563 else {
3564 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3565 &(buf[0].tv_sec), &ausec) == -1) {
3566 return NULL;
3567 }
3568 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3569 &(buf[1].tv_sec), &musec) == -1) {
3570 return NULL;
3571 }
3572 buf[0].tv_usec = ausec;
3573 buf[1].tv_usec = musec;
3574 Py_BEGIN_ALLOW_THREADS
3575 res = futimes(fd, buf);
3576 Py_END_ALLOW_THREADS
3577 }
3578 if (res < 0)
3579 return posix_error();
3580 Py_RETURN_NONE;
3581}
3582#endif
3583
3584#ifdef HAVE_LUTIMES
3585PyDoc_STRVAR(posix_lutimes__doc__,
3586"lutimes(path, (atime, mtime))\n\
3587lutimes(path, None)\n\n\
3588Like utime(), but if path is a symbolic link, it is not dereferenced.");
3589
3590static PyObject *
3591posix_lutimes(PyObject *self, PyObject *args)
3592{
3593 PyObject *opath, *arg;
3594 const char *path;
3595 int res;
3596 struct timeval buf[2];
3597 long ausec, musec;
3598
3599 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3600 PyUnicode_FSConverter, &opath, &arg))
3601 return NULL;
3602 path = PyBytes_AsString(opath);
3603 if (arg == Py_None) {
3604 /* optional time values not given */
3605 Py_BEGIN_ALLOW_THREADS
3606 res = lutimes(path, NULL);
3607 Py_END_ALLOW_THREADS
3608 }
3609 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3610 PyErr_SetString(PyExc_TypeError,
3611 "lutimes() arg 2 must be a tuple (atime, mtime)");
3612 Py_DECREF(opath);
3613 return NULL;
3614 }
3615 else {
3616 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3617 &(buf[0].tv_sec), &ausec) == -1) {
3618 Py_DECREF(opath);
3619 return NULL;
3620 }
3621 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3622 &(buf[1].tv_sec), &musec) == -1) {
3623 Py_DECREF(opath);
3624 return NULL;
3625 }
3626 buf[0].tv_usec = ausec;
3627 buf[1].tv_usec = musec;
3628 Py_BEGIN_ALLOW_THREADS
3629 res = lutimes(path, buf);
3630 Py_END_ALLOW_THREADS
3631 }
3632 Py_DECREF(opath);
3633 if (res < 0)
3634 return posix_error();
3635 Py_RETURN_NONE;
3636}
3637#endif
3638
3639#ifdef HAVE_FUTIMENS
3640PyDoc_STRVAR(posix_futimens__doc__,
3641"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3642futimens(fd, None, None)\n\n\
3643Updates the timestamps of a file specified by the file descriptor fd, with\n\
3644nanosecond precision.\n\
3645The second form sets atime and mtime to the current time.\n\
3646If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3647current time.\n\
3648If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3649
3650static PyObject *
3651posix_futimens(PyObject *self, PyObject *args)
3652{
3653 int res, fd;
3654 PyObject *atime, *mtime;
3655 struct timespec buf[2];
3656
3657 if (!PyArg_ParseTuple(args, "iOO:futimens",
3658 &fd, &atime, &mtime))
3659 return NULL;
3660 if (atime == Py_None && mtime == Py_None) {
3661 /* optional time values not given */
3662 Py_BEGIN_ALLOW_THREADS
3663 res = futimens(fd, NULL);
3664 Py_END_ALLOW_THREADS
3665 }
3666 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3667 PyErr_SetString(PyExc_TypeError,
3668 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3669 return NULL;
3670 }
3671 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3672 PyErr_SetString(PyExc_TypeError,
3673 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3674 return NULL;
3675 }
3676 else {
3677 if (!PyArg_ParseTuple(atime, "ll:futimens",
3678 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3679 return NULL;
3680 }
3681 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3682 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3683 return NULL;
3684 }
3685 Py_BEGIN_ALLOW_THREADS
3686 res = futimens(fd, buf);
3687 Py_END_ALLOW_THREADS
3688 }
3689 if (res < 0)
3690 return posix_error();
3691 Py_RETURN_NONE;
3692}
3693#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003694
Guido van Rossum3b066191991-06-04 19:40:25 +00003695/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003697PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003698"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003699Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003700
Barry Warsaw53699e91996-12-10 23:23:01 +00003701static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003702posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003703{
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 int sts;
3705 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3706 return NULL;
3707 _exit(sts);
3708 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003709}
3710
Martin v. Löwis114619e2002-10-07 06:44:21 +00003711#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3712static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003713free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003714{
Victor Stinner8c62be82010-05-06 00:08:46 +00003715 Py_ssize_t i;
3716 for (i = 0; i < count; i++)
3717 PyMem_Free(array[i]);
3718 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003719}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003720
Antoine Pitrou69f71142009-05-24 21:25:49 +00003721static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003722int fsconvert_strdup(PyObject *o, char**out)
3723{
Victor Stinner8c62be82010-05-06 00:08:46 +00003724 PyObject *bytes;
3725 Py_ssize_t size;
3726 if (!PyUnicode_FSConverter(o, &bytes))
3727 return 0;
3728 size = PyBytes_GET_SIZE(bytes);
3729 *out = PyMem_Malloc(size+1);
3730 if (!*out)
3731 return 0;
3732 memcpy(*out, PyBytes_AsString(bytes), size+1);
3733 Py_DECREF(bytes);
3734 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003735}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003736#endif
3737
Ross Lagerwall7807c352011-03-17 20:20:30 +02003738#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003739static char**
3740parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3741{
Victor Stinner8c62be82010-05-06 00:08:46 +00003742 char **envlist;
3743 Py_ssize_t i, pos, envc;
3744 PyObject *keys=NULL, *vals=NULL;
3745 PyObject *key, *val, *key2, *val2;
3746 char *p, *k, *v;
3747 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003748
Victor Stinner8c62be82010-05-06 00:08:46 +00003749 i = PyMapping_Size(env);
3750 if (i < 0)
3751 return NULL;
3752 envlist = PyMem_NEW(char *, i + 1);
3753 if (envlist == NULL) {
3754 PyErr_NoMemory();
3755 return NULL;
3756 }
3757 envc = 0;
3758 keys = PyMapping_Keys(env);
3759 vals = PyMapping_Values(env);
3760 if (!keys || !vals)
3761 goto error;
3762 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3763 PyErr_Format(PyExc_TypeError,
3764 "env.keys() or env.values() is not a list");
3765 goto error;
3766 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003767
Victor Stinner8c62be82010-05-06 00:08:46 +00003768 for (pos = 0; pos < i; pos++) {
3769 key = PyList_GetItem(keys, pos);
3770 val = PyList_GetItem(vals, pos);
3771 if (!key || !val)
3772 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003773
Victor Stinner8c62be82010-05-06 00:08:46 +00003774 if (PyUnicode_FSConverter(key, &key2) == 0)
3775 goto error;
3776 if (PyUnicode_FSConverter(val, &val2) == 0) {
3777 Py_DECREF(key2);
3778 goto error;
3779 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003780
3781#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003782 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3783 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003784#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003785 k = PyBytes_AsString(key2);
3786 v = PyBytes_AsString(val2);
3787 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003788
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 p = PyMem_NEW(char, len);
3790 if (p == NULL) {
3791 PyErr_NoMemory();
3792 Py_DECREF(key2);
3793 Py_DECREF(val2);
3794 goto error;
3795 }
3796 PyOS_snprintf(p, len, "%s=%s", k, v);
3797 envlist[envc++] = p;
3798 Py_DECREF(key2);
3799 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003800#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003801 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003802#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003803 }
3804 Py_DECREF(vals);
3805 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003806
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 envlist[envc] = 0;
3808 *envc_ptr = envc;
3809 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003810
3811error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003812 Py_XDECREF(keys);
3813 Py_XDECREF(vals);
3814 while (--envc >= 0)
3815 PyMem_DEL(envlist[envc]);
3816 PyMem_DEL(envlist);
3817 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003818}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003819
Ross Lagerwall7807c352011-03-17 20:20:30 +02003820static char**
3821parse_arglist(PyObject* argv, Py_ssize_t *argc)
3822{
3823 int i;
3824 char **argvlist = PyMem_NEW(char *, *argc+1);
3825 if (argvlist == NULL) {
3826 PyErr_NoMemory();
3827 return NULL;
3828 }
3829 for (i = 0; i < *argc; i++) {
3830 if (!fsconvert_strdup(PySequence_ITEM(argv, i),
3831 &argvlist[i]))
3832 {
3833 *argc = i;
3834 goto fail;
3835 }
3836 }
3837 argvlist[*argc] = NULL;
3838 return argvlist;
3839fail:
3840 free_string_array(argvlist, *argc);
3841 return NULL;
3842}
3843#endif
3844
3845#ifdef HAVE_EXECV
3846PyDoc_STRVAR(posix_execv__doc__,
3847"execv(path, args)\n\n\
3848Execute an executable path with arguments, replacing current process.\n\
3849\n\
3850 path: path of executable file\n\
3851 args: tuple or list of strings");
3852
3853static PyObject *
3854posix_execv(PyObject *self, PyObject *args)
3855{
3856 PyObject *opath;
3857 char *path;
3858 PyObject *argv;
3859 char **argvlist;
3860 Py_ssize_t argc;
3861
3862 /* execv has two arguments: (path, argv), where
3863 argv is a list or tuple of strings. */
3864
3865 if (!PyArg_ParseTuple(args, "O&O:execv",
3866 PyUnicode_FSConverter,
3867 &opath, &argv))
3868 return NULL;
3869 path = PyBytes_AsString(opath);
3870 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3871 PyErr_SetString(PyExc_TypeError,
3872 "execv() arg 2 must be a tuple or list");
3873 Py_DECREF(opath);
3874 return NULL;
3875 }
3876 argc = PySequence_Size(argv);
3877 if (argc < 1) {
3878 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3879 Py_DECREF(opath);
3880 return NULL;
3881 }
3882
3883 argvlist = parse_arglist(argv, &argc);
3884 if (argvlist == NULL) {
3885 Py_DECREF(opath);
3886 return NULL;
3887 }
3888
3889 execv(path, argvlist);
3890
3891 /* If we get here it's definitely an error */
3892
3893 free_string_array(argvlist, argc);
3894 Py_DECREF(opath);
3895 return posix_error();
3896}
3897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003898PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003899"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003900Execute a path with arguments and environment, replacing current process.\n\
3901\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003902 path: path of executable file\n\
3903 args: tuple or list of arguments\n\
3904 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003905
Barry Warsaw53699e91996-12-10 23:23:01 +00003906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003907posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003908{
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 PyObject *opath;
3910 char *path;
3911 PyObject *argv, *env;
3912 char **argvlist;
3913 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003914 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003915
Victor Stinner8c62be82010-05-06 00:08:46 +00003916 /* execve has three arguments: (path, argv, env), where
3917 argv is a list or tuple of strings and env is a dictionary
3918 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003919
Victor Stinner8c62be82010-05-06 00:08:46 +00003920 if (!PyArg_ParseTuple(args, "O&OO:execve",
3921 PyUnicode_FSConverter,
3922 &opath, &argv, &env))
3923 return NULL;
3924 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003925 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003926 PyErr_SetString(PyExc_TypeError,
3927 "execve() arg 2 must be a tuple or list");
3928 goto fail_0;
3929 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003930 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 if (!PyMapping_Check(env)) {
3932 PyErr_SetString(PyExc_TypeError,
3933 "execve() arg 3 must be a mapping object");
3934 goto fail_0;
3935 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003936
Ross Lagerwall7807c352011-03-17 20:20:30 +02003937 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00003938 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003939 goto fail_0;
3940 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003941
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 envlist = parse_envlist(env, &envc);
3943 if (envlist == NULL)
3944 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003945
Victor Stinner8c62be82010-05-06 00:08:46 +00003946 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003947
Victor Stinner8c62be82010-05-06 00:08:46 +00003948 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003949
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003951
Victor Stinner8c62be82010-05-06 00:08:46 +00003952 while (--envc >= 0)
3953 PyMem_DEL(envlist[envc]);
3954 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003955 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02003956 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003957 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003958 Py_DECREF(opath);
3959 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003960}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003961#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003962
Ross Lagerwall7807c352011-03-17 20:20:30 +02003963#ifdef HAVE_FEXECVE
3964PyDoc_STRVAR(posix_fexecve__doc__,
3965"fexecve(fd, args, env)\n\n\
3966Execute the program specified by a file descriptor with arguments and\n\
3967environment, replacing the current process.\n\
3968\n\
3969 fd: file descriptor of executable\n\
3970 args: tuple or list of arguments\n\
3971 env: dictionary of strings mapping to strings");
3972
3973static PyObject *
3974posix_fexecve(PyObject *self, PyObject *args)
3975{
3976 int fd;
3977 PyObject *argv, *env;
3978 char **argvlist;
3979 char **envlist;
3980 Py_ssize_t argc, envc;
3981
3982 if (!PyArg_ParseTuple(args, "iOO:fexecve",
3983 &fd, &argv, &env))
3984 return NULL;
3985 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3986 PyErr_SetString(PyExc_TypeError,
3987 "fexecve() arg 2 must be a tuple or list");
3988 return NULL;
3989 }
3990 argc = PySequence_Size(argv);
3991 if (!PyMapping_Check(env)) {
3992 PyErr_SetString(PyExc_TypeError,
3993 "fexecve() arg 3 must be a mapping object");
3994 return NULL;
3995 }
3996
3997 argvlist = parse_arglist(argv, &argc);
3998 if (argvlist == NULL)
3999 return NULL;
4000
4001 envlist = parse_envlist(env, &envc);
4002 if (envlist == NULL)
4003 goto fail;
4004
4005 fexecve(fd, argvlist, envlist);
4006
4007 /* If we get here it's definitely an error */
4008
4009 (void) posix_error();
4010
4011 while (--envc >= 0)
4012 PyMem_DEL(envlist[envc]);
4013 PyMem_DEL(envlist);
4014 fail:
4015 free_string_array(argvlist, argc);
4016 return NULL;
4017}
4018#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004019
Guido van Rossuma1065681999-01-25 23:20:23 +00004020#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004021PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004022"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004023Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004024\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004025 mode: mode of process creation\n\
4026 path: path of executable file\n\
4027 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004028
4029static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004030posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004031{
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 PyObject *opath;
4033 char *path;
4034 PyObject *argv;
4035 char **argvlist;
4036 int mode, i;
4037 Py_ssize_t argc;
4038 Py_intptr_t spawnval;
4039 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004040
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 /* spawnv has three arguments: (mode, path, argv), where
4042 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004043
Victor Stinner8c62be82010-05-06 00:08:46 +00004044 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4045 PyUnicode_FSConverter,
4046 &opath, &argv))
4047 return NULL;
4048 path = PyBytes_AsString(opath);
4049 if (PyList_Check(argv)) {
4050 argc = PyList_Size(argv);
4051 getitem = PyList_GetItem;
4052 }
4053 else if (PyTuple_Check(argv)) {
4054 argc = PyTuple_Size(argv);
4055 getitem = PyTuple_GetItem;
4056 }
4057 else {
4058 PyErr_SetString(PyExc_TypeError,
4059 "spawnv() arg 2 must be a tuple or list");
4060 Py_DECREF(opath);
4061 return NULL;
4062 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004063
Victor Stinner8c62be82010-05-06 00:08:46 +00004064 argvlist = PyMem_NEW(char *, argc+1);
4065 if (argvlist == NULL) {
4066 Py_DECREF(opath);
4067 return PyErr_NoMemory();
4068 }
4069 for (i = 0; i < argc; i++) {
4070 if (!fsconvert_strdup((*getitem)(argv, i),
4071 &argvlist[i])) {
4072 free_string_array(argvlist, i);
4073 PyErr_SetString(
4074 PyExc_TypeError,
4075 "spawnv() arg 2 must contain only strings");
4076 Py_DECREF(opath);
4077 return NULL;
4078 }
4079 }
4080 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004081
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004082#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004083 Py_BEGIN_ALLOW_THREADS
4084 spawnval = spawnv(mode, path, argvlist);
4085 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004086#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004087 if (mode == _OLD_P_OVERLAY)
4088 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004089
Victor Stinner8c62be82010-05-06 00:08:46 +00004090 Py_BEGIN_ALLOW_THREADS
4091 spawnval = _spawnv(mode, path, argvlist);
4092 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004093#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004094
Victor Stinner8c62be82010-05-06 00:08:46 +00004095 free_string_array(argvlist, argc);
4096 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004097
Victor Stinner8c62be82010-05-06 00:08:46 +00004098 if (spawnval == -1)
4099 return posix_error();
4100 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004101#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004102 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004103#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004105#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004106}
4107
4108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004109PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004110"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004111Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004112\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004113 mode: mode of process creation\n\
4114 path: path of executable file\n\
4115 args: tuple or list of arguments\n\
4116 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004117
4118static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004119posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004120{
Victor Stinner8c62be82010-05-06 00:08:46 +00004121 PyObject *opath;
4122 char *path;
4123 PyObject *argv, *env;
4124 char **argvlist;
4125 char **envlist;
4126 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004127 int mode;
4128 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004129 Py_intptr_t spawnval;
4130 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4131 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004132
Victor Stinner8c62be82010-05-06 00:08:46 +00004133 /* spawnve has four arguments: (mode, path, argv, env), where
4134 argv is a list or tuple of strings and env is a dictionary
4135 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004136
Victor Stinner8c62be82010-05-06 00:08:46 +00004137 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4138 PyUnicode_FSConverter,
4139 &opath, &argv, &env))
4140 return NULL;
4141 path = PyBytes_AsString(opath);
4142 if (PyList_Check(argv)) {
4143 argc = PyList_Size(argv);
4144 getitem = PyList_GetItem;
4145 }
4146 else if (PyTuple_Check(argv)) {
4147 argc = PyTuple_Size(argv);
4148 getitem = PyTuple_GetItem;
4149 }
4150 else {
4151 PyErr_SetString(PyExc_TypeError,
4152 "spawnve() arg 2 must be a tuple or list");
4153 goto fail_0;
4154 }
4155 if (!PyMapping_Check(env)) {
4156 PyErr_SetString(PyExc_TypeError,
4157 "spawnve() arg 3 must be a mapping object");
4158 goto fail_0;
4159 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004160
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 argvlist = PyMem_NEW(char *, argc+1);
4162 if (argvlist == NULL) {
4163 PyErr_NoMemory();
4164 goto fail_0;
4165 }
4166 for (i = 0; i < argc; i++) {
4167 if (!fsconvert_strdup((*getitem)(argv, i),
4168 &argvlist[i]))
4169 {
4170 lastarg = i;
4171 goto fail_1;
4172 }
4173 }
4174 lastarg = argc;
4175 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004176
Victor Stinner8c62be82010-05-06 00:08:46 +00004177 envlist = parse_envlist(env, &envc);
4178 if (envlist == NULL)
4179 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004180
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004181#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 Py_BEGIN_ALLOW_THREADS
4183 spawnval = spawnve(mode, path, argvlist, envlist);
4184 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004185#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004186 if (mode == _OLD_P_OVERLAY)
4187 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004188
Victor Stinner8c62be82010-05-06 00:08:46 +00004189 Py_BEGIN_ALLOW_THREADS
4190 spawnval = _spawnve(mode, path, argvlist, envlist);
4191 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004192#endif
Tim Peters25059d32001-12-07 20:35:43 +00004193
Victor Stinner8c62be82010-05-06 00:08:46 +00004194 if (spawnval == -1)
4195 (void) posix_error();
4196 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004197#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004198 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004199#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004200 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004201#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004202
Victor Stinner8c62be82010-05-06 00:08:46 +00004203 while (--envc >= 0)
4204 PyMem_DEL(envlist[envc]);
4205 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004206 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004207 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004208 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004209 Py_DECREF(opath);
4210 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004211}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004212
4213/* OS/2 supports spawnvp & spawnvpe natively */
4214#if defined(PYOS_OS2)
4215PyDoc_STRVAR(posix_spawnvp__doc__,
4216"spawnvp(mode, file, args)\n\n\
4217Execute the program 'file' in a new process, using the environment\n\
4218search path to find the file.\n\
4219\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004220 mode: mode of process creation\n\
4221 file: executable file name\n\
4222 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004223
4224static PyObject *
4225posix_spawnvp(PyObject *self, PyObject *args)
4226{
Victor Stinner8c62be82010-05-06 00:08:46 +00004227 PyObject *opath;
4228 char *path;
4229 PyObject *argv;
4230 char **argvlist;
4231 int mode, i, argc;
4232 Py_intptr_t spawnval;
4233 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004234
Victor Stinner8c62be82010-05-06 00:08:46 +00004235 /* spawnvp has three arguments: (mode, path, argv), where
4236 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004237
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4239 PyUnicode_FSConverter,
4240 &opath, &argv))
4241 return NULL;
4242 path = PyBytes_AsString(opath);
4243 if (PyList_Check(argv)) {
4244 argc = PyList_Size(argv);
4245 getitem = PyList_GetItem;
4246 }
4247 else if (PyTuple_Check(argv)) {
4248 argc = PyTuple_Size(argv);
4249 getitem = PyTuple_GetItem;
4250 }
4251 else {
4252 PyErr_SetString(PyExc_TypeError,
4253 "spawnvp() arg 2 must be a tuple or list");
4254 Py_DECREF(opath);
4255 return NULL;
4256 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004257
Victor Stinner8c62be82010-05-06 00:08:46 +00004258 argvlist = PyMem_NEW(char *, argc+1);
4259 if (argvlist == NULL) {
4260 Py_DECREF(opath);
4261 return PyErr_NoMemory();
4262 }
4263 for (i = 0; i < argc; i++) {
4264 if (!fsconvert_strdup((*getitem)(argv, i),
4265 &argvlist[i])) {
4266 free_string_array(argvlist, i);
4267 PyErr_SetString(
4268 PyExc_TypeError,
4269 "spawnvp() arg 2 must contain only strings");
4270 Py_DECREF(opath);
4271 return NULL;
4272 }
4273 }
4274 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004275
Victor Stinner8c62be82010-05-06 00:08:46 +00004276 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004277#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004278 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004279#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004280 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004281#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004283
Victor Stinner8c62be82010-05-06 00:08:46 +00004284 free_string_array(argvlist, argc);
4285 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004286
Victor Stinner8c62be82010-05-06 00:08:46 +00004287 if (spawnval == -1)
4288 return posix_error();
4289 else
4290 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004291}
4292
4293
4294PyDoc_STRVAR(posix_spawnvpe__doc__,
4295"spawnvpe(mode, file, args, env)\n\n\
4296Execute the program 'file' in a new process, using the environment\n\
4297search path to find the file.\n\
4298\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004299 mode: mode of process creation\n\
4300 file: executable file name\n\
4301 args: tuple or list of arguments\n\
4302 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004303
4304static PyObject *
4305posix_spawnvpe(PyObject *self, PyObject *args)
4306{
Victor Stinner8c62be82010-05-06 00:08:46 +00004307 PyObject *opath
4308 char *path;
4309 PyObject *argv, *env;
4310 char **argvlist;
4311 char **envlist;
4312 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004313 int mode;
4314 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004315 Py_intptr_t spawnval;
4316 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4317 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004318
Victor Stinner8c62be82010-05-06 00:08:46 +00004319 /* spawnvpe has four arguments: (mode, path, argv, env), where
4320 argv is a list or tuple of strings and env is a dictionary
4321 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004322
Victor Stinner8c62be82010-05-06 00:08:46 +00004323 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4324 PyUnicode_FSConverter,
4325 &opath, &argv, &env))
4326 return NULL;
4327 path = PyBytes_AsString(opath);
4328 if (PyList_Check(argv)) {
4329 argc = PyList_Size(argv);
4330 getitem = PyList_GetItem;
4331 }
4332 else if (PyTuple_Check(argv)) {
4333 argc = PyTuple_Size(argv);
4334 getitem = PyTuple_GetItem;
4335 }
4336 else {
4337 PyErr_SetString(PyExc_TypeError,
4338 "spawnvpe() arg 2 must be a tuple or list");
4339 goto fail_0;
4340 }
4341 if (!PyMapping_Check(env)) {
4342 PyErr_SetString(PyExc_TypeError,
4343 "spawnvpe() arg 3 must be a mapping object");
4344 goto fail_0;
4345 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004346
Victor Stinner8c62be82010-05-06 00:08:46 +00004347 argvlist = PyMem_NEW(char *, argc+1);
4348 if (argvlist == NULL) {
4349 PyErr_NoMemory();
4350 goto fail_0;
4351 }
4352 for (i = 0; i < argc; i++) {
4353 if (!fsconvert_strdup((*getitem)(argv, i),
4354 &argvlist[i]))
4355 {
4356 lastarg = i;
4357 goto fail_1;
4358 }
4359 }
4360 lastarg = argc;
4361 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004362
Victor Stinner8c62be82010-05-06 00:08:46 +00004363 envlist = parse_envlist(env, &envc);
4364 if (envlist == NULL)
4365 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004366
Victor Stinner8c62be82010-05-06 00:08:46 +00004367 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004368#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004370#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004372#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 if (spawnval == -1)
4376 (void) posix_error();
4377 else
4378 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004379
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 while (--envc >= 0)
4381 PyMem_DEL(envlist[envc]);
4382 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004383 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004384 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004385 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004386 Py_DECREF(opath);
4387 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004388}
4389#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004390#endif /* HAVE_SPAWNV */
4391
4392
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004393#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004394PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004395"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004396Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4397\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004398Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004399
4400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004401posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004402{
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 pid_t pid;
4404 int result = 0;
4405 _PyImport_AcquireLock();
4406 pid = fork1();
4407 if (pid == 0) {
4408 /* child: this clobbers and resets the import lock. */
4409 PyOS_AfterFork();
4410 } else {
4411 /* parent: release the import lock. */
4412 result = _PyImport_ReleaseLock();
4413 }
4414 if (pid == -1)
4415 return posix_error();
4416 if (result < 0) {
4417 /* Don't clobber the OSError if the fork failed. */
4418 PyErr_SetString(PyExc_RuntimeError,
4419 "not holding the import lock");
4420 return NULL;
4421 }
4422 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004423}
4424#endif
4425
4426
Guido van Rossumad0ee831995-03-01 10:34:45 +00004427#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004428PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004429"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004430Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004431Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004432
Barry Warsaw53699e91996-12-10 23:23:01 +00004433static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004434posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004435{
Victor Stinner8c62be82010-05-06 00:08:46 +00004436 pid_t pid;
4437 int result = 0;
4438 _PyImport_AcquireLock();
4439 pid = fork();
4440 if (pid == 0) {
4441 /* child: this clobbers and resets the import lock. */
4442 PyOS_AfterFork();
4443 } else {
4444 /* parent: release the import lock. */
4445 result = _PyImport_ReleaseLock();
4446 }
4447 if (pid == -1)
4448 return posix_error();
4449 if (result < 0) {
4450 /* Don't clobber the OSError if the fork failed. */
4451 PyErr_SetString(PyExc_RuntimeError,
4452 "not holding the import lock");
4453 return NULL;
4454 }
4455 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004456}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004457#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004458
Neal Norwitzb59798b2003-03-21 01:43:31 +00004459/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004460/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4461#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004462#define DEV_PTY_FILE "/dev/ptc"
4463#define HAVE_DEV_PTMX
4464#else
4465#define DEV_PTY_FILE "/dev/ptmx"
4466#endif
4467
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004468#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004469#ifdef HAVE_PTY_H
4470#include <pty.h>
4471#else
4472#ifdef HAVE_LIBUTIL_H
4473#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004474#else
4475#ifdef HAVE_UTIL_H
4476#include <util.h>
4477#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004478#endif /* HAVE_LIBUTIL_H */
4479#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004480#ifdef HAVE_STROPTS_H
4481#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004482#endif
4483#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004484
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004485#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004486PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004487"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004488Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004489
4490static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004491posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004492{
Victor Stinner8c62be82010-05-06 00:08:46 +00004493 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004494#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004495 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004496#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004497#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004498 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004499#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004500 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004501#endif
4502#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004503
Thomas Wouters70c21a12000-07-14 14:28:33 +00004504#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004505 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4506 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004507#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004508 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4509 if (slave_name == NULL)
4510 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004511
Victor Stinner8c62be82010-05-06 00:08:46 +00004512 slave_fd = open(slave_name, O_RDWR);
4513 if (slave_fd < 0)
4514 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004515#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004516 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4517 if (master_fd < 0)
4518 return posix_error();
4519 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4520 /* change permission of slave */
4521 if (grantpt(master_fd) < 0) {
4522 PyOS_setsig(SIGCHLD, sig_saved);
4523 return posix_error();
4524 }
4525 /* unlock slave */
4526 if (unlockpt(master_fd) < 0) {
4527 PyOS_setsig(SIGCHLD, sig_saved);
4528 return posix_error();
4529 }
4530 PyOS_setsig(SIGCHLD, sig_saved);
4531 slave_name = ptsname(master_fd); /* get name of slave */
4532 if (slave_name == NULL)
4533 return posix_error();
4534 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4535 if (slave_fd < 0)
4536 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004537#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004538 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4539 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004540#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004542#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004543#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004544#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004545
Victor Stinner8c62be82010-05-06 00:08:46 +00004546 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004547
Fred Drake8cef4cf2000-06-28 16:40:38 +00004548}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004549#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004550
4551#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004552PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004553"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004554Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4555Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004556To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004557
4558static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004559posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004560{
Victor Stinner8c62be82010-05-06 00:08:46 +00004561 int master_fd = -1, result = 0;
4562 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004563
Victor Stinner8c62be82010-05-06 00:08:46 +00004564 _PyImport_AcquireLock();
4565 pid = forkpty(&master_fd, NULL, NULL, NULL);
4566 if (pid == 0) {
4567 /* child: this clobbers and resets the import lock. */
4568 PyOS_AfterFork();
4569 } else {
4570 /* parent: release the import lock. */
4571 result = _PyImport_ReleaseLock();
4572 }
4573 if (pid == -1)
4574 return posix_error();
4575 if (result < 0) {
4576 /* Don't clobber the OSError if the fork failed. */
4577 PyErr_SetString(PyExc_RuntimeError,
4578 "not holding the import lock");
4579 return NULL;
4580 }
4581 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004582}
4583#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004584
Ross Lagerwall7807c352011-03-17 20:20:30 +02004585
Guido van Rossumad0ee831995-03-01 10:34:45 +00004586#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004587PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004588"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004589Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004590
Barry Warsaw53699e91996-12-10 23:23:01 +00004591static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004592posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004593{
Victor Stinner8c62be82010-05-06 00:08:46 +00004594 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004595}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004596#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004597
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004598
Guido van Rossumad0ee831995-03-01 10:34:45 +00004599#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004600PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004601"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004603
Barry Warsaw53699e91996-12-10 23:23:01 +00004604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004605posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004606{
Victor Stinner8c62be82010-05-06 00:08:46 +00004607 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004608}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004609#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004611
Guido van Rossumad0ee831995-03-01 10:34:45 +00004612#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004613PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004614"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004615Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004616
Barry Warsaw53699e91996-12-10 23:23:01 +00004617static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004618posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004619{
Victor Stinner8c62be82010-05-06 00:08:46 +00004620 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004621}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004622#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004623
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004625PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004626"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004628
Barry Warsaw53699e91996-12-10 23:23:01 +00004629static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004630posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004631{
Victor Stinner8c62be82010-05-06 00:08:46 +00004632 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004633}
4634
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004635
Fred Drakec9680921999-12-13 16:37:25 +00004636#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004637PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004638"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004640
4641static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004642posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004643{
4644 PyObject *result = NULL;
4645
Fred Drakec9680921999-12-13 16:37:25 +00004646#ifdef NGROUPS_MAX
4647#define MAX_GROUPS NGROUPS_MAX
4648#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004649 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004650#define MAX_GROUPS 64
4651#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004652 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004653
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004654 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004655 * This is a helper variable to store the intermediate result when
4656 * that happens.
4657 *
4658 * To keep the code readable the OSX behaviour is unconditional,
4659 * according to the POSIX spec this should be safe on all unix-y
4660 * systems.
4661 */
4662 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004663 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004664
Victor Stinner8c62be82010-05-06 00:08:46 +00004665 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004666 if (n < 0) {
4667 if (errno == EINVAL) {
4668 n = getgroups(0, NULL);
4669 if (n == -1) {
4670 return posix_error();
4671 }
4672 if (n == 0) {
4673 /* Avoid malloc(0) */
4674 alt_grouplist = grouplist;
4675 } else {
4676 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4677 if (alt_grouplist == NULL) {
4678 errno = EINVAL;
4679 return posix_error();
4680 }
4681 n = getgroups(n, alt_grouplist);
4682 if (n == -1) {
4683 PyMem_Free(alt_grouplist);
4684 return posix_error();
4685 }
4686 }
4687 } else {
4688 return posix_error();
4689 }
4690 }
4691 result = PyList_New(n);
4692 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004693 int i;
4694 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004695 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004696 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004697 Py_DECREF(result);
4698 result = NULL;
4699 break;
Fred Drakec9680921999-12-13 16:37:25 +00004700 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004701 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004702 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004703 }
4704
4705 if (alt_grouplist != grouplist) {
4706 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004707 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004708
Fred Drakec9680921999-12-13 16:37:25 +00004709 return result;
4710}
4711#endif
4712
Antoine Pitroub7572f02009-12-02 20:46:48 +00004713#ifdef HAVE_INITGROUPS
4714PyDoc_STRVAR(posix_initgroups__doc__,
4715"initgroups(username, gid) -> None\n\n\
4716Call the system initgroups() to initialize the group access list with all of\n\
4717the groups of which the specified username is a member, plus the specified\n\
4718group id.");
4719
4720static PyObject *
4721posix_initgroups(PyObject *self, PyObject *args)
4722{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004723 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004724 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004725 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004726 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004727
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004728 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4729 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004730 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004731 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004732
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004733 res = initgroups(username, (gid_t) gid);
4734 Py_DECREF(oname);
4735 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004736 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004737
Victor Stinner8c62be82010-05-06 00:08:46 +00004738 Py_INCREF(Py_None);
4739 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004740}
4741#endif
4742
Martin v. Löwis606edc12002-06-13 21:09:11 +00004743#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004744PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004745"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004746Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004747
4748static PyObject *
4749posix_getpgid(PyObject *self, PyObject *args)
4750{
Victor Stinner8c62be82010-05-06 00:08:46 +00004751 pid_t pid, pgid;
4752 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4753 return NULL;
4754 pgid = getpgid(pid);
4755 if (pgid < 0)
4756 return posix_error();
4757 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004758}
4759#endif /* HAVE_GETPGID */
4760
4761
Guido van Rossumb6775db1994-08-01 11:34:53 +00004762#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004763PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004764"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004765Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004766
Barry Warsaw53699e91996-12-10 23:23:01 +00004767static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004768posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004769{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004770#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004771 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004772#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004773 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004774#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004775}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004776#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004777
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004778
Guido van Rossumb6775db1994-08-01 11:34:53 +00004779#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004780PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004781"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004782Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004783
Barry Warsaw53699e91996-12-10 23:23:01 +00004784static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004785posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004786{
Guido van Rossum64933891994-10-20 21:56:42 +00004787#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004788 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004789#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004790 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004791#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004792 return posix_error();
4793 Py_INCREF(Py_None);
4794 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004795}
4796
Guido van Rossumb6775db1994-08-01 11:34:53 +00004797#endif /* HAVE_SETPGRP */
4798
Guido van Rossumad0ee831995-03-01 10:34:45 +00004799#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004800
4801#ifdef MS_WINDOWS
4802#include <tlhelp32.h>
4803
4804static PyObject*
4805win32_getppid()
4806{
4807 HANDLE snapshot;
4808 pid_t mypid;
4809 PyObject* result = NULL;
4810 BOOL have_record;
4811 PROCESSENTRY32 pe;
4812
4813 mypid = getpid(); /* This function never fails */
4814
4815 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4816 if (snapshot == INVALID_HANDLE_VALUE)
4817 return PyErr_SetFromWindowsErr(GetLastError());
4818
4819 pe.dwSize = sizeof(pe);
4820 have_record = Process32First(snapshot, &pe);
4821 while (have_record) {
4822 if (mypid == (pid_t)pe.th32ProcessID) {
4823 /* We could cache the ulong value in a static variable. */
4824 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4825 break;
4826 }
4827
4828 have_record = Process32Next(snapshot, &pe);
4829 }
4830
4831 /* If our loop exits and our pid was not found (result will be NULL)
4832 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4833 * error anyway, so let's raise it. */
4834 if (!result)
4835 result = PyErr_SetFromWindowsErr(GetLastError());
4836
4837 CloseHandle(snapshot);
4838
4839 return result;
4840}
4841#endif /*MS_WINDOWS*/
4842
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004843PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004844"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004845Return the parent's process id. If the parent process has already exited,\n\
4846Windows machines will still return its id; others systems will return the id\n\
4847of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004848
Barry Warsaw53699e91996-12-10 23:23:01 +00004849static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004850posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004851{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004852#ifdef MS_WINDOWS
4853 return win32_getppid();
4854#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004855 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004856#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004857}
4858#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004860
Fred Drake12c6e2d1999-12-14 21:25:03 +00004861#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004862PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004863"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004864Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004865
4866static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004867posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004868{
Victor Stinner8c62be82010-05-06 00:08:46 +00004869 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004870#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004871 wchar_t user_name[UNLEN + 1];
4872 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4873
4874 if (GetUserNameW(user_name, &num_chars)) {
4875 /* num_chars is the number of unicode chars plus null terminator */
4876 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004877 }
4878 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004879 result = PyErr_SetFromWindowsErr(GetLastError());
4880#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 char *name;
4882 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004883
Victor Stinner8c62be82010-05-06 00:08:46 +00004884 errno = 0;
4885 name = getlogin();
4886 if (name == NULL) {
4887 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004888 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004889 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004890 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004891 }
4892 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004893 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004895#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004896 return result;
4897}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004898#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004899
Guido van Rossumad0ee831995-03-01 10:34:45 +00004900#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004901PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004902"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004903Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004904
Barry Warsaw53699e91996-12-10 23:23:01 +00004905static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004906posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004907{
Victor Stinner8c62be82010-05-06 00:08:46 +00004908 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004909}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004910#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004911
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004912
Guido van Rossumad0ee831995-03-01 10:34:45 +00004913#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004914PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004915"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004916Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004917
Barry Warsaw53699e91996-12-10 23:23:01 +00004918static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004919posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004920{
Victor Stinner8c62be82010-05-06 00:08:46 +00004921 pid_t pid;
4922 int sig;
4923 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4924 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004925#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004926 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4927 APIRET rc;
4928 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004929 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004930
4931 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4932 APIRET rc;
4933 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004934 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004935
4936 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004937 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004938#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004939 if (kill(pid, sig) == -1)
4940 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004941#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 Py_INCREF(Py_None);
4943 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004944}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004945#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004946
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004947#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004948PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004949"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004950Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004951
4952static PyObject *
4953posix_killpg(PyObject *self, PyObject *args)
4954{
Victor Stinner8c62be82010-05-06 00:08:46 +00004955 int sig;
4956 pid_t pgid;
4957 /* XXX some man pages make the `pgid` parameter an int, others
4958 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4959 take the same type. Moreover, pid_t is always at least as wide as
4960 int (else compilation of this module fails), which is safe. */
4961 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4962 return NULL;
4963 if (killpg(pgid, sig) == -1)
4964 return posix_error();
4965 Py_INCREF(Py_None);
4966 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004967}
4968#endif
4969
Brian Curtineb24d742010-04-12 17:16:38 +00004970#ifdef MS_WINDOWS
4971PyDoc_STRVAR(win32_kill__doc__,
4972"kill(pid, sig)\n\n\
4973Kill a process with a signal.");
4974
4975static PyObject *
4976win32_kill(PyObject *self, PyObject *args)
4977{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004978 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004979 DWORD pid, sig, err;
4980 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004981
Victor Stinner8c62be82010-05-06 00:08:46 +00004982 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4983 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004984
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 /* Console processes which share a common console can be sent CTRL+C or
4986 CTRL+BREAK events, provided they handle said events. */
4987 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4988 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4989 err = GetLastError();
4990 PyErr_SetFromWindowsErr(err);
4991 }
4992 else
4993 Py_RETURN_NONE;
4994 }
Brian Curtineb24d742010-04-12 17:16:38 +00004995
Victor Stinner8c62be82010-05-06 00:08:46 +00004996 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4997 attempt to open and terminate the process. */
4998 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4999 if (handle == NULL) {
5000 err = GetLastError();
5001 return PyErr_SetFromWindowsErr(err);
5002 }
Brian Curtineb24d742010-04-12 17:16:38 +00005003
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 if (TerminateProcess(handle, sig) == 0) {
5005 err = GetLastError();
5006 result = PyErr_SetFromWindowsErr(err);
5007 } else {
5008 Py_INCREF(Py_None);
5009 result = Py_None;
5010 }
Brian Curtineb24d742010-04-12 17:16:38 +00005011
Victor Stinner8c62be82010-05-06 00:08:46 +00005012 CloseHandle(handle);
5013 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005014}
5015#endif /* MS_WINDOWS */
5016
Guido van Rossumc0125471996-06-28 18:55:32 +00005017#ifdef HAVE_PLOCK
5018
5019#ifdef HAVE_SYS_LOCK_H
5020#include <sys/lock.h>
5021#endif
5022
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005023PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005024"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005026
Barry Warsaw53699e91996-12-10 23:23:01 +00005027static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005028posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005029{
Victor Stinner8c62be82010-05-06 00:08:46 +00005030 int op;
5031 if (!PyArg_ParseTuple(args, "i:plock", &op))
5032 return NULL;
5033 if (plock(op) == -1)
5034 return posix_error();
5035 Py_INCREF(Py_None);
5036 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005037}
5038#endif
5039
Guido van Rossumb6775db1994-08-01 11:34:53 +00005040#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005041PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005042"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005043Set the current process's user id.");
5044
Barry Warsaw53699e91996-12-10 23:23:01 +00005045static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005046posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005047{
Victor Stinner8c62be82010-05-06 00:08:46 +00005048 long uid_arg;
5049 uid_t uid;
5050 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5051 return NULL;
5052 uid = uid_arg;
5053 if (uid != uid_arg) {
5054 PyErr_SetString(PyExc_OverflowError, "user id too big");
5055 return NULL;
5056 }
5057 if (setuid(uid) < 0)
5058 return posix_error();
5059 Py_INCREF(Py_None);
5060 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005061}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005062#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005063
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005064
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005065#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005066PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005067"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005068Set the current process's effective user id.");
5069
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005070static PyObject *
5071posix_seteuid (PyObject *self, PyObject *args)
5072{
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 long euid_arg;
5074 uid_t euid;
5075 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5076 return NULL;
5077 euid = euid_arg;
5078 if (euid != euid_arg) {
5079 PyErr_SetString(PyExc_OverflowError, "user id too big");
5080 return NULL;
5081 }
5082 if (seteuid(euid) < 0) {
5083 return posix_error();
5084 } else {
5085 Py_INCREF(Py_None);
5086 return Py_None;
5087 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005088}
5089#endif /* HAVE_SETEUID */
5090
5091#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005092PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005093"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005094Set the current process's effective group id.");
5095
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005096static PyObject *
5097posix_setegid (PyObject *self, PyObject *args)
5098{
Victor Stinner8c62be82010-05-06 00:08:46 +00005099 long egid_arg;
5100 gid_t egid;
5101 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5102 return NULL;
5103 egid = egid_arg;
5104 if (egid != egid_arg) {
5105 PyErr_SetString(PyExc_OverflowError, "group id too big");
5106 return NULL;
5107 }
5108 if (setegid(egid) < 0) {
5109 return posix_error();
5110 } else {
5111 Py_INCREF(Py_None);
5112 return Py_None;
5113 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005114}
5115#endif /* HAVE_SETEGID */
5116
5117#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005118PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005119"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005120Set the current process's real and effective user ids.");
5121
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005122static PyObject *
5123posix_setreuid (PyObject *self, PyObject *args)
5124{
Victor Stinner8c62be82010-05-06 00:08:46 +00005125 long ruid_arg, euid_arg;
5126 uid_t ruid, euid;
5127 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5128 return NULL;
5129 if (ruid_arg == -1)
5130 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5131 else
5132 ruid = ruid_arg; /* otherwise, assign from our long */
5133 if (euid_arg == -1)
5134 euid = (uid_t)-1;
5135 else
5136 euid = euid_arg;
5137 if ((euid_arg != -1 && euid != euid_arg) ||
5138 (ruid_arg != -1 && ruid != ruid_arg)) {
5139 PyErr_SetString(PyExc_OverflowError, "user id too big");
5140 return NULL;
5141 }
5142 if (setreuid(ruid, euid) < 0) {
5143 return posix_error();
5144 } else {
5145 Py_INCREF(Py_None);
5146 return Py_None;
5147 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005148}
5149#endif /* HAVE_SETREUID */
5150
5151#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005152PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005153"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005154Set the current process's real and effective group ids.");
5155
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005156static PyObject *
5157posix_setregid (PyObject *self, PyObject *args)
5158{
Victor Stinner8c62be82010-05-06 00:08:46 +00005159 long rgid_arg, egid_arg;
5160 gid_t rgid, egid;
5161 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5162 return NULL;
5163 if (rgid_arg == -1)
5164 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5165 else
5166 rgid = rgid_arg; /* otherwise, assign from our long */
5167 if (egid_arg == -1)
5168 egid = (gid_t)-1;
5169 else
5170 egid = egid_arg;
5171 if ((egid_arg != -1 && egid != egid_arg) ||
5172 (rgid_arg != -1 && rgid != rgid_arg)) {
5173 PyErr_SetString(PyExc_OverflowError, "group id too big");
5174 return NULL;
5175 }
5176 if (setregid(rgid, egid) < 0) {
5177 return posix_error();
5178 } else {
5179 Py_INCREF(Py_None);
5180 return Py_None;
5181 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005182}
5183#endif /* HAVE_SETREGID */
5184
Guido van Rossumb6775db1994-08-01 11:34:53 +00005185#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005186PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005187"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005188Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005189
Barry Warsaw53699e91996-12-10 23:23:01 +00005190static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005191posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005192{
Victor Stinner8c62be82010-05-06 00:08:46 +00005193 long gid_arg;
5194 gid_t gid;
5195 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5196 return NULL;
5197 gid = gid_arg;
5198 if (gid != gid_arg) {
5199 PyErr_SetString(PyExc_OverflowError, "group id too big");
5200 return NULL;
5201 }
5202 if (setgid(gid) < 0)
5203 return posix_error();
5204 Py_INCREF(Py_None);
5205 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005206}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005207#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005208
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005209#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005210PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005211"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005213
5214static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005215posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005216{
Victor Stinner8c62be82010-05-06 00:08:46 +00005217 int i, len;
5218 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005219
Victor Stinner8c62be82010-05-06 00:08:46 +00005220 if (!PySequence_Check(groups)) {
5221 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5222 return NULL;
5223 }
5224 len = PySequence_Size(groups);
5225 if (len > MAX_GROUPS) {
5226 PyErr_SetString(PyExc_ValueError, "too many groups");
5227 return NULL;
5228 }
5229 for(i = 0; i < len; i++) {
5230 PyObject *elem;
5231 elem = PySequence_GetItem(groups, i);
5232 if (!elem)
5233 return NULL;
5234 if (!PyLong_Check(elem)) {
5235 PyErr_SetString(PyExc_TypeError,
5236 "groups must be integers");
5237 Py_DECREF(elem);
5238 return NULL;
5239 } else {
5240 unsigned long x = PyLong_AsUnsignedLong(elem);
5241 if (PyErr_Occurred()) {
5242 PyErr_SetString(PyExc_TypeError,
5243 "group id too big");
5244 Py_DECREF(elem);
5245 return NULL;
5246 }
5247 grouplist[i] = x;
5248 /* read back the value to see if it fitted in gid_t */
5249 if (grouplist[i] != x) {
5250 PyErr_SetString(PyExc_TypeError,
5251 "group id too big");
5252 Py_DECREF(elem);
5253 return NULL;
5254 }
5255 }
5256 Py_DECREF(elem);
5257 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005258
Victor Stinner8c62be82010-05-06 00:08:46 +00005259 if (setgroups(len, grouplist) < 0)
5260 return posix_error();
5261 Py_INCREF(Py_None);
5262 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005263}
5264#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005265
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005266#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5267static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005268wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005269{
Victor Stinner8c62be82010-05-06 00:08:46 +00005270 PyObject *result;
5271 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005272
Victor Stinner8c62be82010-05-06 00:08:46 +00005273 if (pid == -1)
5274 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005275
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 if (struct_rusage == NULL) {
5277 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5278 if (m == NULL)
5279 return NULL;
5280 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5281 Py_DECREF(m);
5282 if (struct_rusage == NULL)
5283 return NULL;
5284 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005285
Victor Stinner8c62be82010-05-06 00:08:46 +00005286 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5287 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5288 if (!result)
5289 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005290
5291#ifndef doubletime
5292#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5293#endif
5294
Victor Stinner8c62be82010-05-06 00:08:46 +00005295 PyStructSequence_SET_ITEM(result, 0,
5296 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5297 PyStructSequence_SET_ITEM(result, 1,
5298 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005299#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005300 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5301 SET_INT(result, 2, ru->ru_maxrss);
5302 SET_INT(result, 3, ru->ru_ixrss);
5303 SET_INT(result, 4, ru->ru_idrss);
5304 SET_INT(result, 5, ru->ru_isrss);
5305 SET_INT(result, 6, ru->ru_minflt);
5306 SET_INT(result, 7, ru->ru_majflt);
5307 SET_INT(result, 8, ru->ru_nswap);
5308 SET_INT(result, 9, ru->ru_inblock);
5309 SET_INT(result, 10, ru->ru_oublock);
5310 SET_INT(result, 11, ru->ru_msgsnd);
5311 SET_INT(result, 12, ru->ru_msgrcv);
5312 SET_INT(result, 13, ru->ru_nsignals);
5313 SET_INT(result, 14, ru->ru_nvcsw);
5314 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005315#undef SET_INT
5316
Victor Stinner8c62be82010-05-06 00:08:46 +00005317 if (PyErr_Occurred()) {
5318 Py_DECREF(result);
5319 return NULL;
5320 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005321
Victor Stinner8c62be82010-05-06 00:08:46 +00005322 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005323}
5324#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5325
5326#ifdef HAVE_WAIT3
5327PyDoc_STRVAR(posix_wait3__doc__,
5328"wait3(options) -> (pid, status, rusage)\n\n\
5329Wait for completion of a child process.");
5330
5331static PyObject *
5332posix_wait3(PyObject *self, PyObject *args)
5333{
Victor Stinner8c62be82010-05-06 00:08:46 +00005334 pid_t pid;
5335 int options;
5336 struct rusage ru;
5337 WAIT_TYPE status;
5338 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005339
Victor Stinner8c62be82010-05-06 00:08:46 +00005340 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5341 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005342
Victor Stinner8c62be82010-05-06 00:08:46 +00005343 Py_BEGIN_ALLOW_THREADS
5344 pid = wait3(&status, options, &ru);
5345 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005346
Victor Stinner8c62be82010-05-06 00:08:46 +00005347 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005348}
5349#endif /* HAVE_WAIT3 */
5350
5351#ifdef HAVE_WAIT4
5352PyDoc_STRVAR(posix_wait4__doc__,
5353"wait4(pid, options) -> (pid, status, rusage)\n\n\
5354Wait for completion of a given child process.");
5355
5356static PyObject *
5357posix_wait4(PyObject *self, PyObject *args)
5358{
Victor Stinner8c62be82010-05-06 00:08:46 +00005359 pid_t pid;
5360 int options;
5361 struct rusage ru;
5362 WAIT_TYPE status;
5363 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005364
Victor Stinner8c62be82010-05-06 00:08:46 +00005365 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5366 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005367
Victor Stinner8c62be82010-05-06 00:08:46 +00005368 Py_BEGIN_ALLOW_THREADS
5369 pid = wait4(pid, &status, options, &ru);
5370 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005371
Victor Stinner8c62be82010-05-06 00:08:46 +00005372 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005373}
5374#endif /* HAVE_WAIT4 */
5375
Ross Lagerwall7807c352011-03-17 20:20:30 +02005376#if defined(HAVE_WAITID) && !defined(__APPLE__)
5377PyDoc_STRVAR(posix_waitid__doc__,
5378"waitid(idtype, id, options) -> waitid_result\n\n\
5379Wait for the completion of one or more child processes.\n\n\
5380idtype can be P_PID, P_PGID or P_ALL.\n\
5381id specifies the pid to wait on.\n\
5382options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
5383or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
5384Returns either waitid_result or None if WNOHANG is specified and there are\n\
5385no children in a waitable state.");
5386
5387static PyObject *
5388posix_waitid(PyObject *self, PyObject *args)
5389{
5390 PyObject *result;
5391 idtype_t idtype;
5392 id_t id;
5393 int options, res;
5394 siginfo_t si;
5395 si.si_pid = 0;
5396 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
5397 return NULL;
5398 Py_BEGIN_ALLOW_THREADS
5399 res = waitid(idtype, id, &si, options);
5400 Py_END_ALLOW_THREADS
5401 if (res == -1)
5402 return posix_error();
5403
5404 if (si.si_pid == 0)
5405 Py_RETURN_NONE;
5406
5407 result = PyStructSequence_New(&WaitidResultType);
5408 if (!result)
5409 return NULL;
5410
5411 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
5412 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
5413 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
5414 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
5415 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
5416 if (PyErr_Occurred()) {
5417 Py_DECREF(result);
5418 return NULL;
5419 }
5420
5421 return result;
5422}
5423#endif
5424
Guido van Rossumb6775db1994-08-01 11:34:53 +00005425#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005426PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005427"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005428Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005429
Barry Warsaw53699e91996-12-10 23:23:01 +00005430static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005431posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005432{
Victor Stinner8c62be82010-05-06 00:08:46 +00005433 pid_t pid;
5434 int options;
5435 WAIT_TYPE status;
5436 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005437
Victor Stinner8c62be82010-05-06 00:08:46 +00005438 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5439 return NULL;
5440 Py_BEGIN_ALLOW_THREADS
5441 pid = waitpid(pid, &status, options);
5442 Py_END_ALLOW_THREADS
5443 if (pid == -1)
5444 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005445
Victor Stinner8c62be82010-05-06 00:08:46 +00005446 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005447}
5448
Tim Petersab034fa2002-02-01 11:27:43 +00005449#elif defined(HAVE_CWAIT)
5450
5451/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005452PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005453"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005454"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005455
5456static PyObject *
5457posix_waitpid(PyObject *self, PyObject *args)
5458{
Victor Stinner8c62be82010-05-06 00:08:46 +00005459 Py_intptr_t pid;
5460 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005461
Victor Stinner8c62be82010-05-06 00:08:46 +00005462 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5463 return NULL;
5464 Py_BEGIN_ALLOW_THREADS
5465 pid = _cwait(&status, pid, options);
5466 Py_END_ALLOW_THREADS
5467 if (pid == -1)
5468 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005469
Victor Stinner8c62be82010-05-06 00:08:46 +00005470 /* shift the status left a byte so this is more like the POSIX waitpid */
5471 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005472}
5473#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005474
Guido van Rossumad0ee831995-03-01 10:34:45 +00005475#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005476PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005477"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005478Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005479
Barry Warsaw53699e91996-12-10 23:23:01 +00005480static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005481posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005482{
Victor Stinner8c62be82010-05-06 00:08:46 +00005483 pid_t pid;
5484 WAIT_TYPE status;
5485 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005486
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 Py_BEGIN_ALLOW_THREADS
5488 pid = wait(&status);
5489 Py_END_ALLOW_THREADS
5490 if (pid == -1)
5491 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005492
Victor Stinner8c62be82010-05-06 00:08:46 +00005493 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005494}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005495#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005496
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005498PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005499"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005500Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005501
Barry Warsaw53699e91996-12-10 23:23:01 +00005502static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005503posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005504{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005505#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005506 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005507#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005508#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005509 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5510 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005511#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005512 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005513#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005514#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005515}
5516
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005517
Guido van Rossumb6775db1994-08-01 11:34:53 +00005518#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005520"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005521Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005522
Barry Warsaw53699e91996-12-10 23:23:01 +00005523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005524posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005525{
Victor Stinner8c62be82010-05-06 00:08:46 +00005526 PyObject* v;
5527 char buf[MAXPATHLEN];
5528 PyObject *opath;
5529 char *path;
5530 int n;
5531 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005532
Victor Stinner8c62be82010-05-06 00:08:46 +00005533 if (!PyArg_ParseTuple(args, "O&:readlink",
5534 PyUnicode_FSConverter, &opath))
5535 return NULL;
5536 path = PyBytes_AsString(opath);
5537 v = PySequence_GetItem(args, 0);
5538 if (v == NULL) {
5539 Py_DECREF(opath);
5540 return NULL;
5541 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005542
Victor Stinner8c62be82010-05-06 00:08:46 +00005543 if (PyUnicode_Check(v)) {
5544 arg_is_unicode = 1;
5545 }
5546 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005547
Victor Stinner8c62be82010-05-06 00:08:46 +00005548 Py_BEGIN_ALLOW_THREADS
5549 n = readlink(path, buf, (int) sizeof buf);
5550 Py_END_ALLOW_THREADS
5551 if (n < 0)
5552 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005553
Victor Stinner8c62be82010-05-06 00:08:46 +00005554 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005555 if (arg_is_unicode)
5556 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5557 else
5558 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005559}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005560#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005561
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005562
Brian Curtin52173d42010-12-02 18:29:18 +00005563#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005565"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005566Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005567
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005569posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005570{
Victor Stinner8c62be82010-05-06 00:08:46 +00005571 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005572}
5573#endif /* HAVE_SYMLINK */
5574
Brian Curtind40e6f72010-07-08 21:39:08 +00005575#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5576
5577PyDoc_STRVAR(win_readlink__doc__,
5578"readlink(path) -> path\n\n\
5579Return a string representing the path to which the symbolic link points.");
5580
Brian Curtind40e6f72010-07-08 21:39:08 +00005581/* Windows readlink implementation */
5582static PyObject *
5583win_readlink(PyObject *self, PyObject *args)
5584{
5585 wchar_t *path;
5586 DWORD n_bytes_returned;
5587 DWORD io_result;
5588 PyObject *result;
5589 HANDLE reparse_point_handle;
5590
5591 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5592 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5593 wchar_t *print_name;
5594
5595 if (!PyArg_ParseTuple(args,
5596 "u:readlink",
5597 &path))
5598 return NULL;
5599
5600 /* First get a handle to the reparse point */
5601 Py_BEGIN_ALLOW_THREADS
5602 reparse_point_handle = CreateFileW(
5603 path,
5604 0,
5605 0,
5606 0,
5607 OPEN_EXISTING,
5608 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5609 0);
5610 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005611
Brian Curtind40e6f72010-07-08 21:39:08 +00005612 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5613 {
5614 return win32_error_unicode("readlink", path);
5615 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005616
Brian Curtind40e6f72010-07-08 21:39:08 +00005617 Py_BEGIN_ALLOW_THREADS
5618 /* New call DeviceIoControl to read the reparse point */
5619 io_result = DeviceIoControl(
5620 reparse_point_handle,
5621 FSCTL_GET_REPARSE_POINT,
5622 0, 0, /* in buffer */
5623 target_buffer, sizeof(target_buffer),
5624 &n_bytes_returned,
5625 0 /* we're not using OVERLAPPED_IO */
5626 );
5627 CloseHandle(reparse_point_handle);
5628 Py_END_ALLOW_THREADS
5629
5630 if (io_result==0)
5631 {
5632 return win32_error_unicode("readlink", path);
5633 }
5634
5635 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5636 {
5637 PyErr_SetString(PyExc_ValueError,
5638 "not a symbolic link");
5639 return NULL;
5640 }
Brian Curtin74e45612010-07-09 15:58:59 +00005641 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5642 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5643
5644 result = PyUnicode_FromWideChar(print_name,
5645 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005646 return result;
5647}
5648
5649#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5650
Brian Curtin52173d42010-12-02 18:29:18 +00005651#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005652
5653/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5654static int has_CreateSymbolicLinkW = 0;
5655static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5656static int
5657check_CreateSymbolicLinkW()
5658{
5659 HINSTANCE hKernel32;
5660 /* only recheck */
5661 if (has_CreateSymbolicLinkW)
5662 return has_CreateSymbolicLinkW;
5663 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005664 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5665 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005666 if (Py_CreateSymbolicLinkW)
5667 has_CreateSymbolicLinkW = 1;
5668 return has_CreateSymbolicLinkW;
5669}
5670
5671PyDoc_STRVAR(win_symlink__doc__,
5672"symlink(src, dst, target_is_directory=False)\n\n\
5673Create a symbolic link pointing to src named dst.\n\
5674target_is_directory is required if the target is to be interpreted as\n\
5675a directory.\n\
5676This function requires Windows 6.0 or greater, and raises a\n\
5677NotImplementedError otherwise.");
5678
5679static PyObject *
5680win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5681{
5682 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5683 PyObject *src, *dest;
5684 int target_is_directory = 0;
5685 DWORD res;
5686 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005687
Brian Curtind40e6f72010-07-08 21:39:08 +00005688 if (!check_CreateSymbolicLinkW())
5689 {
5690 /* raise NotImplementedError */
5691 return PyErr_Format(PyExc_NotImplementedError,
5692 "CreateSymbolicLinkW not found");
5693 }
5694 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5695 kwlist, &src, &dest, &target_is_directory))
5696 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005697
5698 if (win32_can_symlink == 0)
5699 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5700
Brian Curtind40e6f72010-07-08 21:39:08 +00005701 if (!convert_to_unicode(&src)) { return NULL; }
5702 if (!convert_to_unicode(&dest)) {
5703 Py_DECREF(src);
5704 return NULL;
5705 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005706
Brian Curtind40e6f72010-07-08 21:39:08 +00005707 /* if src is a directory, ensure target_is_directory==1 */
5708 if(
5709 GetFileAttributesExW(
5710 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5711 ))
5712 {
5713 target_is_directory = target_is_directory ||
5714 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5715 }
5716
5717 Py_BEGIN_ALLOW_THREADS
5718 res = Py_CreateSymbolicLinkW(
5719 PyUnicode_AsUnicode(dest),
5720 PyUnicode_AsUnicode(src),
5721 target_is_directory);
5722 Py_END_ALLOW_THREADS
5723 Py_DECREF(src);
5724 Py_DECREF(dest);
5725 if (!res)
5726 {
5727 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5728 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005729
Brian Curtind40e6f72010-07-08 21:39:08 +00005730 Py_INCREF(Py_None);
5731 return Py_None;
5732}
Brian Curtin52173d42010-12-02 18:29:18 +00005733#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005734
5735#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005736#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5737static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005738system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005739{
5740 ULONG value = 0;
5741
5742 Py_BEGIN_ALLOW_THREADS
5743 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5744 Py_END_ALLOW_THREADS
5745
5746 return value;
5747}
5748
5749static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005750posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005751{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005752 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 return Py_BuildValue("ddddd",
5754 (double)0 /* t.tms_utime / HZ */,
5755 (double)0 /* t.tms_stime / HZ */,
5756 (double)0 /* t.tms_cutime / HZ */,
5757 (double)0 /* t.tms_cstime / HZ */,
5758 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005759}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005760#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005761#define NEED_TICKS_PER_SECOND
5762static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005763static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005764posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005765{
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 struct tms t;
5767 clock_t c;
5768 errno = 0;
5769 c = times(&t);
5770 if (c == (clock_t) -1)
5771 return posix_error();
5772 return Py_BuildValue("ddddd",
5773 (double)t.tms_utime / ticks_per_second,
5774 (double)t.tms_stime / ticks_per_second,
5775 (double)t.tms_cutime / ticks_per_second,
5776 (double)t.tms_cstime / ticks_per_second,
5777 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005778}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005779#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005780#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005781
5782
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005783#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005784#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005785static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005786posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005787{
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 FILETIME create, exit, kernel, user;
5789 HANDLE hProc;
5790 hProc = GetCurrentProcess();
5791 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5792 /* The fields of a FILETIME structure are the hi and lo part
5793 of a 64-bit value expressed in 100 nanosecond units.
5794 1e7 is one second in such units; 1e-7 the inverse.
5795 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5796 */
5797 return Py_BuildValue(
5798 "ddddd",
5799 (double)(user.dwHighDateTime*429.4967296 +
5800 user.dwLowDateTime*1e-7),
5801 (double)(kernel.dwHighDateTime*429.4967296 +
5802 kernel.dwLowDateTime*1e-7),
5803 (double)0,
5804 (double)0,
5805 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005806}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005807#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005808
5809#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005810PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005811"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005812Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005813#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005814
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005815
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005816#ifdef HAVE_GETSID
5817PyDoc_STRVAR(posix_getsid__doc__,
5818"getsid(pid) -> sid\n\n\
5819Call the system call getsid().");
5820
5821static PyObject *
5822posix_getsid(PyObject *self, PyObject *args)
5823{
Victor Stinner8c62be82010-05-06 00:08:46 +00005824 pid_t pid;
5825 int sid;
5826 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5827 return NULL;
5828 sid = getsid(pid);
5829 if (sid < 0)
5830 return posix_error();
5831 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005832}
5833#endif /* HAVE_GETSID */
5834
5835
Guido van Rossumb6775db1994-08-01 11:34:53 +00005836#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005837PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005838"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005839Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005840
Barry Warsaw53699e91996-12-10 23:23:01 +00005841static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005842posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005843{
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 if (setsid() < 0)
5845 return posix_error();
5846 Py_INCREF(Py_None);
5847 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005848}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005849#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005850
Guido van Rossumb6775db1994-08-01 11:34:53 +00005851#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005852PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005853"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005854Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005855
Barry Warsaw53699e91996-12-10 23:23:01 +00005856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005857posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005858{
Victor Stinner8c62be82010-05-06 00:08:46 +00005859 pid_t pid;
5860 int pgrp;
5861 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5862 return NULL;
5863 if (setpgid(pid, pgrp) < 0)
5864 return posix_error();
5865 Py_INCREF(Py_None);
5866 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005867}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005868#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005870
Guido van Rossumb6775db1994-08-01 11:34:53 +00005871#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005872PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005873"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005874Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005875
Barry Warsaw53699e91996-12-10 23:23:01 +00005876static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005877posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005878{
Victor Stinner8c62be82010-05-06 00:08:46 +00005879 int fd;
5880 pid_t pgid;
5881 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5882 return NULL;
5883 pgid = tcgetpgrp(fd);
5884 if (pgid < 0)
5885 return posix_error();
5886 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005887}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005888#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005890
Guido van Rossumb6775db1994-08-01 11:34:53 +00005891#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005892PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005893"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005894Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005895
Barry Warsaw53699e91996-12-10 23:23:01 +00005896static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005897posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005898{
Victor Stinner8c62be82010-05-06 00:08:46 +00005899 int fd;
5900 pid_t pgid;
5901 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5902 return NULL;
5903 if (tcsetpgrp(fd, pgid) < 0)
5904 return posix_error();
5905 Py_INCREF(Py_None);
5906 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005907}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005908#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005909
Guido van Rossum687dd131993-05-17 08:34:16 +00005910/* Functions acting on file descriptors */
5911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005912PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005913"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005914Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005915
Barry Warsaw53699e91996-12-10 23:23:01 +00005916static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005917posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005918{
Victor Stinner8c62be82010-05-06 00:08:46 +00005919 PyObject *ofile;
5920 char *file;
5921 int flag;
5922 int mode = 0777;
5923 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005924
5925#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 PyUnicodeObject *po;
5927 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5928 Py_BEGIN_ALLOW_THREADS
5929 /* PyUnicode_AS_UNICODE OK without thread
5930 lock as it is a simple dereference. */
5931 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5932 Py_END_ALLOW_THREADS
5933 if (fd < 0)
5934 return posix_error();
5935 return PyLong_FromLong((long)fd);
5936 }
5937 /* Drop the argument parsing error as narrow strings
5938 are also valid. */
5939 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005940#endif
5941
Victor Stinner8c62be82010-05-06 00:08:46 +00005942 if (!PyArg_ParseTuple(args, "O&i|i",
5943 PyUnicode_FSConverter, &ofile,
5944 &flag, &mode))
5945 return NULL;
5946 file = PyBytes_AsString(ofile);
5947 Py_BEGIN_ALLOW_THREADS
5948 fd = open(file, flag, mode);
5949 Py_END_ALLOW_THREADS
5950 if (fd < 0)
5951 return posix_error_with_allocated_filename(ofile);
5952 Py_DECREF(ofile);
5953 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005954}
5955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005957PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005958"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005959Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005960
Barry Warsaw53699e91996-12-10 23:23:01 +00005961static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005962posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005963{
Victor Stinner8c62be82010-05-06 00:08:46 +00005964 int fd, res;
5965 if (!PyArg_ParseTuple(args, "i:close", &fd))
5966 return NULL;
5967 if (!_PyVerify_fd(fd))
5968 return posix_error();
5969 Py_BEGIN_ALLOW_THREADS
5970 res = close(fd);
5971 Py_END_ALLOW_THREADS
5972 if (res < 0)
5973 return posix_error();
5974 Py_INCREF(Py_None);
5975 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005976}
5977
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005978
Victor Stinner8c62be82010-05-06 00:08:46 +00005979PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005980"closerange(fd_low, fd_high)\n\n\
5981Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5982
5983static PyObject *
5984posix_closerange(PyObject *self, PyObject *args)
5985{
Victor Stinner8c62be82010-05-06 00:08:46 +00005986 int fd_from, fd_to, i;
5987 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5988 return NULL;
5989 Py_BEGIN_ALLOW_THREADS
5990 for (i = fd_from; i < fd_to; i++)
5991 if (_PyVerify_fd(i))
5992 close(i);
5993 Py_END_ALLOW_THREADS
5994 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005995}
5996
5997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005999"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006000Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006001
Barry Warsaw53699e91996-12-10 23:23:01 +00006002static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006003posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006004{
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 int fd;
6006 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6007 return NULL;
6008 if (!_PyVerify_fd(fd))
6009 return posix_error();
6010 Py_BEGIN_ALLOW_THREADS
6011 fd = dup(fd);
6012 Py_END_ALLOW_THREADS
6013 if (fd < 0)
6014 return posix_error();
6015 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006016}
6017
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006019PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006020"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006021Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006022
Barry Warsaw53699e91996-12-10 23:23:01 +00006023static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006024posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006025{
Victor Stinner8c62be82010-05-06 00:08:46 +00006026 int fd, fd2, res;
6027 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6028 return NULL;
6029 if (!_PyVerify_fd_dup2(fd, fd2))
6030 return posix_error();
6031 Py_BEGIN_ALLOW_THREADS
6032 res = dup2(fd, fd2);
6033 Py_END_ALLOW_THREADS
6034 if (res < 0)
6035 return posix_error();
6036 Py_INCREF(Py_None);
6037 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006038}
6039
Ross Lagerwall7807c352011-03-17 20:20:30 +02006040#ifdef HAVE_LOCKF
6041PyDoc_STRVAR(posix_lockf__doc__,
6042"lockf(fd, cmd, len)\n\n\
6043Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6044fd is an open file descriptor.\n\
6045cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6046F_TEST.\n\
6047len specifies the section of the file to lock.");
6048
6049static PyObject *
6050posix_lockf(PyObject *self, PyObject *args)
6051{
6052 int fd, cmd, res;
6053 off_t len;
6054 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6055 &fd, &cmd, _parse_off_t, &len))
6056 return NULL;
6057
6058 Py_BEGIN_ALLOW_THREADS
6059 res = lockf(fd, cmd, len);
6060 Py_END_ALLOW_THREADS
6061
6062 if (res < 0)
6063 return posix_error();
6064
6065 Py_RETURN_NONE;
6066}
6067#endif
6068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006069
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006070PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006071"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006072Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006073
Barry Warsaw53699e91996-12-10 23:23:01 +00006074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006075posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006076{
Victor Stinner8c62be82010-05-06 00:08:46 +00006077 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006078#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006079 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006080#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006081 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006082#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02006083 if (!PyArg_ParseTuple(args, "iO&i:lseek", &fd, _parse_off_t, &pos, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006084 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006085#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006086 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6087 switch (how) {
6088 case 0: how = SEEK_SET; break;
6089 case 1: how = SEEK_CUR; break;
6090 case 2: how = SEEK_END; break;
6091 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006092#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006093
Victor Stinner8c62be82010-05-06 00:08:46 +00006094 if (PyErr_Occurred())
6095 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006096
Victor Stinner8c62be82010-05-06 00:08:46 +00006097 if (!_PyVerify_fd(fd))
6098 return posix_error();
6099 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006100#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006101 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006102#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006103 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006104#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006105 Py_END_ALLOW_THREADS
6106 if (res < 0)
6107 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006108
6109#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006110 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006111#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006112 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006113#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006114}
6115
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006116
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006117PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006118"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006119Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006120
Barry Warsaw53699e91996-12-10 23:23:01 +00006121static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006122posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006123{
Victor Stinner8c62be82010-05-06 00:08:46 +00006124 int fd, size;
6125 Py_ssize_t n;
6126 PyObject *buffer;
6127 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6128 return NULL;
6129 if (size < 0) {
6130 errno = EINVAL;
6131 return posix_error();
6132 }
6133 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6134 if (buffer == NULL)
6135 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006136 if (!_PyVerify_fd(fd)) {
6137 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006139 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006140 Py_BEGIN_ALLOW_THREADS
6141 n = read(fd, PyBytes_AS_STRING(buffer), size);
6142 Py_END_ALLOW_THREADS
6143 if (n < 0) {
6144 Py_DECREF(buffer);
6145 return posix_error();
6146 }
6147 if (n != size)
6148 _PyBytes_Resize(&buffer, n);
6149 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006150}
6151
Ross Lagerwall7807c352011-03-17 20:20:30 +02006152#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6153 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006154static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006155iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6156{
6157 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006158 Py_ssize_t blen, total = 0;
6159
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006160 *iov = PyMem_New(struct iovec, cnt);
6161 if (*iov == NULL) {
6162 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006163 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006164 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006165
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006166 *buf = PyMem_New(Py_buffer, cnt);
6167 if (*buf == NULL) {
6168 PyMem_Del(*iov);
6169 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006170 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006171 }
6172
6173 for (i = 0; i < cnt; i++) {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006174 if (PyObject_GetBuffer(PySequence_GetItem(seq, i),
6175 &(*buf)[i], type) == -1) {
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006176 PyMem_Del(*iov);
6177 for (j = 0; j < i; j++) {
6178 PyBuffer_Release(&(*buf)[j]);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006179 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006180 PyMem_Del(*buf);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006181 total = 0;
6182 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006183 }
6184 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006185 blen = (*buf)[i].len;
6186 (*iov)[i].iov_len = blen;
6187 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006188 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006189 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006190}
6191
6192static void
6193iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6194{
6195 int i;
6196 PyMem_Del(iov);
6197 for (i = 0; i < cnt; i++) {
6198 PyBuffer_Release(&buf[i]);
6199 }
6200 PyMem_Del(buf);
6201}
6202#endif
6203
Ross Lagerwall7807c352011-03-17 20:20:30 +02006204#ifdef HAVE_READV
6205PyDoc_STRVAR(posix_readv__doc__,
6206"readv(fd, buffers) -> bytesread\n\n\
6207Read from a file descriptor into a number of writable buffers. buffers\n\
6208is an arbitrary sequence of writable buffers.\n\
6209Returns the total number of bytes read.");
6210
6211static PyObject *
6212posix_readv(PyObject *self, PyObject *args)
6213{
6214 int fd, cnt;
6215 Py_ssize_t n;
6216 PyObject *seq;
6217 struct iovec *iov;
6218 Py_buffer *buf;
6219
6220 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6221 return NULL;
6222 if (!PySequence_Check(seq)) {
6223 PyErr_SetString(PyExc_TypeError,
6224 "readv() arg 2 must be a sequence");
6225 return NULL;
6226 }
6227 cnt = PySequence_Size(seq);
6228
6229 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6230 return NULL;
6231
6232 Py_BEGIN_ALLOW_THREADS
6233 n = readv(fd, iov, cnt);
6234 Py_END_ALLOW_THREADS
6235
6236 iov_cleanup(iov, buf, cnt);
6237 return PyLong_FromSsize_t(n);
6238}
6239#endif
6240
6241#ifdef HAVE_PREAD
6242PyDoc_STRVAR(posix_pread__doc__,
6243"pread(fd, buffersize, offset) -> string\n\n\
6244Read from a file descriptor, fd, at a position of offset. It will read up\n\
6245to buffersize number of bytes. The file offset remains unchanged.");
6246
6247static PyObject *
6248posix_pread(PyObject *self, PyObject *args)
6249{
6250 int fd, size;
6251 off_t offset;
6252 Py_ssize_t n;
6253 PyObject *buffer;
6254 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6255 return NULL;
6256
6257 if (size < 0) {
6258 errno = EINVAL;
6259 return posix_error();
6260 }
6261 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6262 if (buffer == NULL)
6263 return NULL;
6264 if (!_PyVerify_fd(fd)) {
6265 Py_DECREF(buffer);
6266 return posix_error();
6267 }
6268 Py_BEGIN_ALLOW_THREADS
6269 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6270 Py_END_ALLOW_THREADS
6271 if (n < 0) {
6272 Py_DECREF(buffer);
6273 return posix_error();
6274 }
6275 if (n != size)
6276 _PyBytes_Resize(&buffer, n);
6277 return buffer;
6278}
6279#endif
6280
6281PyDoc_STRVAR(posix_write__doc__,
6282"write(fd, string) -> byteswritten\n\n\
6283Write a string to a file descriptor.");
6284
6285static PyObject *
6286posix_write(PyObject *self, PyObject *args)
6287{
6288 Py_buffer pbuf;
6289 int fd;
6290 Py_ssize_t size, len;
6291
6292 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
6293 return NULL;
6294 if (!_PyVerify_fd(fd)) {
6295 PyBuffer_Release(&pbuf);
6296 return posix_error();
6297 }
6298 len = pbuf.len;
6299 Py_BEGIN_ALLOW_THREADS
6300#if defined(MS_WIN64) || defined(MS_WINDOWS)
6301 if (len > INT_MAX)
6302 len = INT_MAX;
6303 size = write(fd, pbuf.buf, (int)len);
6304#else
6305 size = write(fd, pbuf.buf, len);
6306#endif
6307 Py_END_ALLOW_THREADS
6308 PyBuffer_Release(&pbuf);
6309 if (size < 0)
6310 return posix_error();
6311 return PyLong_FromSsize_t(size);
6312}
6313
6314#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006315PyDoc_STRVAR(posix_sendfile__doc__,
6316"sendfile(out, in, offset, nbytes) -> byteswritten\n\
6317sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
6318 -> byteswritten\n\
6319Copy nbytes bytes from file descriptor in to file descriptor out.");
6320
6321static PyObject *
6322posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
6323{
6324 int in, out;
6325 Py_ssize_t ret;
6326 off_t offset;
6327
6328#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
6329#ifndef __APPLE__
6330 Py_ssize_t len;
6331#endif
6332 PyObject *headers = NULL, *trailers = NULL;
6333 Py_buffer *hbuf, *tbuf;
6334 off_t sbytes;
6335 struct sf_hdtr sf;
6336 int flags = 0;
6337 sf.headers = NULL;
6338 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00006339 static char *keywords[] = {"out", "in",
6340 "offset", "count",
6341 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006342
6343#ifdef __APPLE__
6344 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006345 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006346#else
6347 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006348 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006349#endif
6350 &headers, &trailers, &flags))
6351 return NULL;
6352 if (headers != NULL) {
6353 if (!PySequence_Check(headers)) {
6354 PyErr_SetString(PyExc_TypeError,
6355 "sendfile() headers must be a sequence or None");
6356 return NULL;
6357 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006358 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006359 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006360 if (sf.hdr_cnt > 0 &&
6361 !(i = iov_setup(&(sf.headers), &hbuf,
6362 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006363 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006364#ifdef __APPLE__
6365 sbytes += i;
6366#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006367 }
6368 }
6369 if (trailers != NULL) {
6370 if (!PySequence_Check(trailers)) {
6371 PyErr_SetString(PyExc_TypeError,
6372 "sendfile() trailers must be a sequence or None");
6373 return NULL;
6374 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006375 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006376 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006377 if (sf.trl_cnt > 0 &&
6378 !(i = iov_setup(&(sf.trailers), &tbuf,
6379 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006380 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006381#ifdef __APPLE__
6382 sbytes += i;
6383#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006384 }
6385 }
6386
6387 Py_BEGIN_ALLOW_THREADS
6388#ifdef __APPLE__
6389 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
6390#else
6391 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
6392#endif
6393 Py_END_ALLOW_THREADS
6394
6395 if (sf.headers != NULL)
6396 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
6397 if (sf.trailers != NULL)
6398 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
6399
6400 if (ret < 0) {
6401 if ((errno == EAGAIN) || (errno == EBUSY)) {
6402 if (sbytes != 0) {
6403 // some data has been sent
6404 goto done;
6405 }
6406 else {
6407 // no data has been sent; upper application is supposed
6408 // to retry on EAGAIN or EBUSY
6409 return posix_error();
6410 }
6411 }
6412 return posix_error();
6413 }
6414 goto done;
6415
6416done:
6417 #if !defined(HAVE_LARGEFILE_SUPPORT)
6418 return Py_BuildValue("l", sbytes);
6419 #else
6420 return Py_BuildValue("L", sbytes);
6421 #endif
6422
6423#else
6424 Py_ssize_t count;
6425 PyObject *offobj;
6426 static char *keywords[] = {"out", "in",
6427 "offset", "count", NULL};
6428 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
6429 keywords, &out, &in, &offobj, &count))
6430 return NULL;
6431#ifdef linux
6432 if (offobj == Py_None) {
6433 Py_BEGIN_ALLOW_THREADS
6434 ret = sendfile(out, in, NULL, count);
6435 Py_END_ALLOW_THREADS
6436 if (ret < 0)
6437 return posix_error();
6438 Py_INCREF(Py_None);
6439 return Py_BuildValue("nO", ret, Py_None);
6440 }
6441#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00006442 if (!_parse_off_t(offobj, &offset))
6443 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006444 Py_BEGIN_ALLOW_THREADS
6445 ret = sendfile(out, in, &offset, count);
6446 Py_END_ALLOW_THREADS
6447 if (ret < 0)
6448 return posix_error();
6449 return Py_BuildValue("n", ret);
6450#endif
6451}
6452#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006454PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006455"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006456Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006457
Barry Warsaw53699e91996-12-10 23:23:01 +00006458static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006459posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006460{
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 int fd;
6462 STRUCT_STAT st;
6463 int res;
6464 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6465 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006466#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 /* on OpenVMS we must ensure that all bytes are written to the file */
6468 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006469#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 if (!_PyVerify_fd(fd))
6471 return posix_error();
6472 Py_BEGIN_ALLOW_THREADS
6473 res = FSTAT(fd, &st);
6474 Py_END_ALLOW_THREADS
6475 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006476#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006477 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006478#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006480#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 }
Tim Peters5aa91602002-01-30 05:46:57 +00006482
Victor Stinner8c62be82010-05-06 00:08:46 +00006483 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006484}
6485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006486PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006487"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006488Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006489connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006490
6491static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006492posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006493{
Victor Stinner8c62be82010-05-06 00:08:46 +00006494 int fd;
6495 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6496 return NULL;
6497 if (!_PyVerify_fd(fd))
6498 return PyBool_FromLong(0);
6499 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006500}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006501
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006502#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006503PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006504"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006505Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006506
Barry Warsaw53699e91996-12-10 23:23:01 +00006507static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006508posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006509{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006510#if defined(PYOS_OS2)
6511 HFILE read, write;
6512 APIRET rc;
6513
Victor Stinner8c62be82010-05-06 00:08:46 +00006514 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006515 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00006516 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006517 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006518 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006519
6520 return Py_BuildValue("(ii)", read, write);
6521#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006522#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006523 int fds[2];
6524 int res;
6525 Py_BEGIN_ALLOW_THREADS
6526 res = pipe(fds);
6527 Py_END_ALLOW_THREADS
6528 if (res != 0)
6529 return posix_error();
6530 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006531#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00006532 HANDLE read, write;
6533 int read_fd, write_fd;
6534 BOOL ok;
6535 Py_BEGIN_ALLOW_THREADS
6536 ok = CreatePipe(&read, &write, NULL, 0);
6537 Py_END_ALLOW_THREADS
6538 if (!ok)
6539 return win32_error("CreatePipe", NULL);
6540 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6541 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6542 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006543#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006544#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006545}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006546#endif /* HAVE_PIPE */
6547
Ross Lagerwall7807c352011-03-17 20:20:30 +02006548#ifdef HAVE_WRITEV
6549PyDoc_STRVAR(posix_writev__doc__,
6550"writev(fd, buffers) -> byteswritten\n\n\
6551Write the contents of buffers to a file descriptor, where buffers is an\n\
6552arbitrary sequence of buffers.\n\
6553Returns the total bytes written.");
6554
6555static PyObject *
6556posix_writev(PyObject *self, PyObject *args)
6557{
6558 int fd, cnt;
6559 Py_ssize_t res;
6560 PyObject *seq;
6561 struct iovec *iov;
6562 Py_buffer *buf;
6563 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
6564 return NULL;
6565 if (!PySequence_Check(seq)) {
6566 PyErr_SetString(PyExc_TypeError,
6567 "writev() arg 2 must be a sequence");
6568 return NULL;
6569 }
6570 cnt = PySequence_Size(seq);
6571
6572 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
6573 return NULL;
6574 }
6575
6576 Py_BEGIN_ALLOW_THREADS
6577 res = writev(fd, iov, cnt);
6578 Py_END_ALLOW_THREADS
6579
6580 iov_cleanup(iov, buf, cnt);
6581 return PyLong_FromSsize_t(res);
6582}
6583#endif
6584
6585#ifdef HAVE_PWRITE
6586PyDoc_STRVAR(posix_pwrite__doc__,
6587"pwrite(fd, string, offset) -> byteswritten\n\n\
6588Write string to a file descriptor, fd, from offset, leaving the file\n\
6589offset unchanged.");
6590
6591static PyObject *
6592posix_pwrite(PyObject *self, PyObject *args)
6593{
6594 Py_buffer pbuf;
6595 int fd;
6596 off_t offset;
6597 Py_ssize_t size;
6598
6599 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
6600 return NULL;
6601
6602 if (!_PyVerify_fd(fd)) {
6603 PyBuffer_Release(&pbuf);
6604 return posix_error();
6605 }
6606 Py_BEGIN_ALLOW_THREADS
6607 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
6608 Py_END_ALLOW_THREADS
6609 PyBuffer_Release(&pbuf);
6610 if (size < 0)
6611 return posix_error();
6612 return PyLong_FromSsize_t(size);
6613}
6614#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006615
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006616#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006617PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006618"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006619Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006620
Barry Warsaw53699e91996-12-10 23:23:01 +00006621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006622posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006623{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006624 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 char *filename;
6626 int mode = 0666;
6627 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006628 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6629 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006630 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006631 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006632 Py_BEGIN_ALLOW_THREADS
6633 res = mkfifo(filename, mode);
6634 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006635 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 if (res < 0)
6637 return posix_error();
6638 Py_INCREF(Py_None);
6639 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006640}
6641#endif
6642
6643
Neal Norwitz11690112002-07-30 01:08:28 +00006644#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006645PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006646"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006647Create a filesystem node (file, device special file or named pipe)\n\
6648named filename. mode specifies both the permissions to use and the\n\
6649type of node to be created, being combined (bitwise OR) with one of\n\
6650S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006651device defines the newly created device special file (probably using\n\
6652os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006653
6654
6655static PyObject *
6656posix_mknod(PyObject *self, PyObject *args)
6657{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006658 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006659 char *filename;
6660 int mode = 0600;
6661 int device = 0;
6662 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006663 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6664 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006665 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006666 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 Py_BEGIN_ALLOW_THREADS
6668 res = mknod(filename, mode, device);
6669 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006670 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006671 if (res < 0)
6672 return posix_error();
6673 Py_INCREF(Py_None);
6674 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006675}
6676#endif
6677
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006678#ifdef HAVE_DEVICE_MACROS
6679PyDoc_STRVAR(posix_major__doc__,
6680"major(device) -> major number\n\
6681Extracts a device major number from a raw device number.");
6682
6683static PyObject *
6684posix_major(PyObject *self, PyObject *args)
6685{
Victor Stinner8c62be82010-05-06 00:08:46 +00006686 int device;
6687 if (!PyArg_ParseTuple(args, "i:major", &device))
6688 return NULL;
6689 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006690}
6691
6692PyDoc_STRVAR(posix_minor__doc__,
6693"minor(device) -> minor number\n\
6694Extracts a device minor number from a raw device number.");
6695
6696static PyObject *
6697posix_minor(PyObject *self, PyObject *args)
6698{
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 int device;
6700 if (!PyArg_ParseTuple(args, "i:minor", &device))
6701 return NULL;
6702 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006703}
6704
6705PyDoc_STRVAR(posix_makedev__doc__,
6706"makedev(major, minor) -> device number\n\
6707Composes a raw device number from the major and minor device numbers.");
6708
6709static PyObject *
6710posix_makedev(PyObject *self, PyObject *args)
6711{
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 int major, minor;
6713 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6714 return NULL;
6715 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006716}
6717#endif /* device macros */
6718
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006719
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006720#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006721PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006722"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006723Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006724
Barry Warsaw53699e91996-12-10 23:23:01 +00006725static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006726posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006727{
Victor Stinner8c62be82010-05-06 00:08:46 +00006728 int fd;
6729 off_t length;
6730 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006731
Ross Lagerwall7807c352011-03-17 20:20:30 +02006732 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006734
Victor Stinner8c62be82010-05-06 00:08:46 +00006735 Py_BEGIN_ALLOW_THREADS
6736 res = ftruncate(fd, length);
6737 Py_END_ALLOW_THREADS
6738 if (res < 0)
6739 return posix_error();
6740 Py_INCREF(Py_None);
6741 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006742}
6743#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006744
Ross Lagerwall7807c352011-03-17 20:20:30 +02006745#ifdef HAVE_TRUNCATE
6746PyDoc_STRVAR(posix_truncate__doc__,
6747"truncate(path, length)\n\n\
6748Truncate the file given by path to length bytes.");
6749
6750static PyObject *
6751posix_truncate(PyObject *self, PyObject *args)
6752{
6753 PyObject *opath;
6754 const char *path;
6755 off_t length;
6756 int res;
6757
6758 if (!PyArg_ParseTuple(args, "O&O&:truncate",
6759 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
6760 return NULL;
6761 path = PyBytes_AsString(opath);
6762
6763 Py_BEGIN_ALLOW_THREADS
6764 res = truncate(path, length);
6765 Py_END_ALLOW_THREADS
6766 Py_DECREF(opath);
6767 if (res < 0)
6768 return posix_error();
6769 Py_RETURN_NONE;
6770}
6771#endif
6772
6773#ifdef HAVE_POSIX_FALLOCATE
6774PyDoc_STRVAR(posix_posix_fallocate__doc__,
6775"posix_fallocate(fd, offset, len)\n\n\
6776Ensures that enough disk space is allocated for the file specified by fd\n\
6777starting from offset and continuing for len bytes.");
6778
6779static PyObject *
6780posix_posix_fallocate(PyObject *self, PyObject *args)
6781{
6782 off_t len, offset;
6783 int res, fd;
6784
6785 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
6786 &fd, _parse_off_t, &offset, _parse_off_t, &len))
6787 return NULL;
6788
6789 Py_BEGIN_ALLOW_THREADS
6790 res = posix_fallocate(fd, offset, len);
6791 Py_END_ALLOW_THREADS
6792 if (res != 0) {
6793 errno = res;
6794 return posix_error();
6795 }
6796 Py_RETURN_NONE;
6797}
6798#endif
6799
6800#ifdef HAVE_POSIX_FADVISE
6801PyDoc_STRVAR(posix_posix_fadvise__doc__,
6802"posix_fadvise(fd, offset, len, advice)\n\n\
6803Announces an intention to access data in a specific pattern thus allowing\n\
6804the kernel to make optimizations.\n\
6805The advice applies to the region of the file specified by fd starting at\n\
6806offset and continuing for len bytes.\n\
6807advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
6808POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
6809POSIX_FADV_DONTNEED.");
6810
6811static PyObject *
6812posix_posix_fadvise(PyObject *self, PyObject *args)
6813{
6814 off_t len, offset;
6815 int res, fd, advice;
6816
6817 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
6818 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
6819 return NULL;
6820
6821 Py_BEGIN_ALLOW_THREADS
6822 res = posix_fadvise(fd, offset, len, advice);
6823 Py_END_ALLOW_THREADS
6824 if (res != 0) {
6825 errno = res;
6826 return posix_error();
6827 }
6828 Py_RETURN_NONE;
6829}
6830#endif
6831
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006832#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006833PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006834"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006835Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006836
Fred Drake762e2061999-08-26 17:23:54 +00006837/* Save putenv() parameters as values here, so we can collect them when they
6838 * get re-set with another call for the same key. */
6839static PyObject *posix_putenv_garbage;
6840
Tim Peters5aa91602002-01-30 05:46:57 +00006841static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006842posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006843{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006844#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006845 wchar_t *s1, *s2;
6846 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006847#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006848 PyObject *os1, *os2;
6849 char *s1, *s2;
6850 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006851#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006852 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006854
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006855#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 if (!PyArg_ParseTuple(args,
6857 "uu:putenv",
6858 &s1, &s2))
6859 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006860#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006861 if (!PyArg_ParseTuple(args,
6862 "O&O&:putenv",
6863 PyUnicode_FSConverter, &os1,
6864 PyUnicode_FSConverter, &os2))
6865 return NULL;
6866 s1 = PyBytes_AsString(os1);
6867 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006868#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006869
6870#if defined(PYOS_OS2)
6871 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6872 APIRET rc;
6873
Guido van Rossumd48f2521997-12-05 22:19:34 +00006874 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006875 if (rc != NO_ERROR) {
6876 os2_error(rc);
6877 goto error;
6878 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006879
6880 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6881 APIRET rc;
6882
Guido van Rossumd48f2521997-12-05 22:19:34 +00006883 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006884 if (rc != NO_ERROR) {
6885 os2_error(rc);
6886 goto error;
6887 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006888 } else {
6889#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006890 /* XXX This can leak memory -- not easy to fix :-( */
6891 /* len includes space for a trailing \0; the size arg to
6892 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006893#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 len = wcslen(s1) + wcslen(s2) + 2;
6895 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006896#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006897 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006899#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006900 if (newstr == NULL) {
6901 PyErr_NoMemory();
6902 goto error;
6903 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006904#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006905 newenv = PyUnicode_AsUnicode(newstr);
6906 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6907 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006908 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006909 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006911#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 newenv = PyBytes_AS_STRING(newstr);
6913 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6914 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006916 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006917 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006918#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006919
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 /* Install the first arg and newstr in posix_putenv_garbage;
6921 * this will cause previous value to be collected. This has to
6922 * happen after the real putenv() call because the old value
6923 * was still accessible until then. */
6924 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006925#ifdef MS_WINDOWS
6926 PyTuple_GET_ITEM(args, 0),
6927#else
6928 os1,
6929#endif
6930 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 /* really not much we can do; just leak */
6932 PyErr_Clear();
6933 }
6934 else {
6935 Py_DECREF(newstr);
6936 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006937
6938#if defined(PYOS_OS2)
6939 }
6940#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006941
Martin v. Löwis011e8422009-05-05 04:43:17 +00006942#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 Py_DECREF(os1);
6944 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006945#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006946 Py_RETURN_NONE;
6947
6948error:
6949#ifndef MS_WINDOWS
6950 Py_DECREF(os1);
6951 Py_DECREF(os2);
6952#endif
6953 Py_XDECREF(newstr);
6954 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006955}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006956#endif /* putenv */
6957
Guido van Rossumc524d952001-10-19 01:31:59 +00006958#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006959PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006960"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006961Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006962
6963static PyObject *
6964posix_unsetenv(PyObject *self, PyObject *args)
6965{
Victor Stinner84ae1182010-05-06 22:05:07 +00006966#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006968
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6970 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006971#else
6972 PyObject *os1;
6973 char *s1;
6974
6975 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6976 PyUnicode_FSConverter, &os1))
6977 return NULL;
6978 s1 = PyBytes_AsString(os1);
6979#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006980
Victor Stinner8c62be82010-05-06 00:08:46 +00006981 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006982
Victor Stinner8c62be82010-05-06 00:08:46 +00006983 /* Remove the key from posix_putenv_garbage;
6984 * this will cause it to be collected. This has to
6985 * happen after the real unsetenv() call because the
6986 * old value was still accessible until then.
6987 */
6988 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006989#ifdef MS_WINDOWS
6990 PyTuple_GET_ITEM(args, 0)
6991#else
6992 os1
6993#endif
6994 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 /* really not much we can do; just leak */
6996 PyErr_Clear();
6997 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006998
Victor Stinner84ae1182010-05-06 22:05:07 +00006999#ifndef MS_WINDOWS
7000 Py_DECREF(os1);
7001#endif
7002 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007003}
7004#endif /* unsetenv */
7005
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007006PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007007"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007008Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007009
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007010static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007011posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007012{
Victor Stinner8c62be82010-05-06 00:08:46 +00007013 int code;
7014 char *message;
7015 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7016 return NULL;
7017 message = strerror(code);
7018 if (message == NULL) {
7019 PyErr_SetString(PyExc_ValueError,
7020 "strerror() argument out of range");
7021 return NULL;
7022 }
7023 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007024}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007025
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007026
Guido van Rossumc9641791998-08-04 15:26:23 +00007027#ifdef HAVE_SYS_WAIT_H
7028
Fred Drake106c1a02002-04-23 15:58:02 +00007029#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007030PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007031"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007032Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007033
7034static PyObject *
7035posix_WCOREDUMP(PyObject *self, PyObject *args)
7036{
Victor Stinner8c62be82010-05-06 00:08:46 +00007037 WAIT_TYPE status;
7038 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007039
Victor Stinner8c62be82010-05-06 00:08:46 +00007040 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7041 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007042
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007044}
7045#endif /* WCOREDUMP */
7046
7047#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007048PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007049"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007050Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007051job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007052
7053static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007054posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007055{
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 WAIT_TYPE status;
7057 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007058
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7060 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007061
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007063}
7064#endif /* WIFCONTINUED */
7065
Guido van Rossumc9641791998-08-04 15:26:23 +00007066#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007067PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007068"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007069Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007070
7071static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007072posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007073{
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 WAIT_TYPE status;
7075 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007076
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7078 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007079
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007081}
7082#endif /* WIFSTOPPED */
7083
7084#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007085PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007086"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007087Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007088
7089static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007090posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007091{
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 WAIT_TYPE status;
7093 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007094
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7096 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007097
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007099}
7100#endif /* WIFSIGNALED */
7101
7102#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007103PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007104"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007105Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007106system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007107
7108static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007109posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007110{
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 WAIT_TYPE status;
7112 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007113
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7115 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007116
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007118}
7119#endif /* WIFEXITED */
7120
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007121#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007122PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007123"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007124Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007125
7126static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007127posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007128{
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 WAIT_TYPE status;
7130 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007131
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7133 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007134
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007136}
7137#endif /* WEXITSTATUS */
7138
7139#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007140PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007141"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007142Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007143value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007144
7145static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007146posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007147{
Victor Stinner8c62be82010-05-06 00:08:46 +00007148 WAIT_TYPE status;
7149 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007150
Victor Stinner8c62be82010-05-06 00:08:46 +00007151 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7152 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007153
Victor Stinner8c62be82010-05-06 00:08:46 +00007154 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007155}
7156#endif /* WTERMSIG */
7157
7158#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007159PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007160"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007161Return the signal that stopped the process that provided\n\
7162the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007163
7164static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007165posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007166{
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 WAIT_TYPE status;
7168 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007169
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7171 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007172
Victor Stinner8c62be82010-05-06 00:08:46 +00007173 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007174}
7175#endif /* WSTOPSIG */
7176
7177#endif /* HAVE_SYS_WAIT_H */
7178
7179
Thomas Wouters477c8d52006-05-27 19:21:47 +00007180#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007181#ifdef _SCO_DS
7182/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7183 needed definitions in sys/statvfs.h */
7184#define _SVID3
7185#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007186#include <sys/statvfs.h>
7187
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007188static PyObject*
7189_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7191 if (v == NULL)
7192 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007193
7194#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7196 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7197 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7198 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7199 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7200 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7201 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7202 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7203 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7204 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007205#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007206 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7207 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7208 PyStructSequence_SET_ITEM(v, 2,
7209 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7210 PyStructSequence_SET_ITEM(v, 3,
7211 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7212 PyStructSequence_SET_ITEM(v, 4,
7213 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7214 PyStructSequence_SET_ITEM(v, 5,
7215 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7216 PyStructSequence_SET_ITEM(v, 6,
7217 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7218 PyStructSequence_SET_ITEM(v, 7,
7219 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7220 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7221 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007222#endif
7223
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007225}
7226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007227PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007228"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007229Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007230
7231static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007232posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007233{
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 int fd, res;
7235 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007236
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7238 return NULL;
7239 Py_BEGIN_ALLOW_THREADS
7240 res = fstatvfs(fd, &st);
7241 Py_END_ALLOW_THREADS
7242 if (res != 0)
7243 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007244
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007246}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007247#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007248
7249
Thomas Wouters477c8d52006-05-27 19:21:47 +00007250#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007251#include <sys/statvfs.h>
7252
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007253PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007254"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007255Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007256
7257static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007258posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007259{
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 char *path;
7261 int res;
7262 struct statvfs st;
7263 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7264 return NULL;
7265 Py_BEGIN_ALLOW_THREADS
7266 res = statvfs(path, &st);
7267 Py_END_ALLOW_THREADS
7268 if (res != 0)
7269 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007270
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007272}
7273#endif /* HAVE_STATVFS */
7274
Fred Drakec9680921999-12-13 16:37:25 +00007275/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7276 * It maps strings representing configuration variable names to
7277 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007278 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007279 * rarely-used constants. There are three separate tables that use
7280 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007281 *
7282 * This code is always included, even if none of the interfaces that
7283 * need it are included. The #if hackery needed to avoid it would be
7284 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007285 */
7286struct constdef {
7287 char *name;
7288 long value;
7289};
7290
Fred Drake12c6e2d1999-12-14 21:25:03 +00007291static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007292conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00007293 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007294{
Christian Heimes217cfd12007-12-02 14:31:20 +00007295 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007296 *valuep = PyLong_AS_LONG(arg);
7297 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007298 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00007299 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00007300 /* look up the value in the table using a binary search */
7301 size_t lo = 0;
7302 size_t mid;
7303 size_t hi = tablesize;
7304 int cmp;
7305 const char *confname;
7306 if (!PyUnicode_Check(arg)) {
7307 PyErr_SetString(PyExc_TypeError,
7308 "configuration names must be strings or integers");
7309 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 }
Stefan Krah0e803b32010-11-26 16:16:47 +00007311 confname = _PyUnicode_AsString(arg);
7312 if (confname == NULL)
7313 return 0;
7314 while (lo < hi) {
7315 mid = (lo + hi) / 2;
7316 cmp = strcmp(confname, table[mid].name);
7317 if (cmp < 0)
7318 hi = mid;
7319 else if (cmp > 0)
7320 lo = mid + 1;
7321 else {
7322 *valuep = table[mid].value;
7323 return 1;
7324 }
7325 }
7326 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7327 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007328 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007329}
7330
7331
7332#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7333static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007334#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007335 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007336#endif
7337#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007338 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007339#endif
Fred Drakec9680921999-12-13 16:37:25 +00007340#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007341 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007342#endif
7343#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007345#endif
7346#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007348#endif
7349#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007351#endif
7352#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007353 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007354#endif
7355#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007357#endif
7358#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007359 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007360#endif
7361#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007363#endif
7364#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007366#endif
7367#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007369#endif
7370#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007372#endif
7373#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007375#endif
7376#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007377 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007378#endif
7379#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007381#endif
7382#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007384#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00007385#ifdef _PC_ACL_ENABLED
7386 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
7387#endif
7388#ifdef _PC_MIN_HOLE_SIZE
7389 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
7390#endif
7391#ifdef _PC_ALLOC_SIZE_MIN
7392 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
7393#endif
7394#ifdef _PC_REC_INCR_XFER_SIZE
7395 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
7396#endif
7397#ifdef _PC_REC_MAX_XFER_SIZE
7398 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
7399#endif
7400#ifdef _PC_REC_MIN_XFER_SIZE
7401 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
7402#endif
7403#ifdef _PC_REC_XFER_ALIGN
7404 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
7405#endif
7406#ifdef _PC_SYMLINK_MAX
7407 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
7408#endif
7409#ifdef _PC_XATTR_ENABLED
7410 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
7411#endif
7412#ifdef _PC_XATTR_EXISTS
7413 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
7414#endif
7415#ifdef _PC_TIMESTAMP_RESOLUTION
7416 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
7417#endif
Fred Drakec9680921999-12-13 16:37:25 +00007418};
7419
Fred Drakec9680921999-12-13 16:37:25 +00007420static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007421conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007422{
7423 return conv_confname(arg, valuep, posix_constants_pathconf,
7424 sizeof(posix_constants_pathconf)
7425 / sizeof(struct constdef));
7426}
7427#endif
7428
7429#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007430PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007431"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007432Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007433If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007434
7435static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007436posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007437{
7438 PyObject *result = NULL;
7439 int name, fd;
7440
Fred Drake12c6e2d1999-12-14 21:25:03 +00007441 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7442 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007443 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007444
Stefan Krah0e803b32010-11-26 16:16:47 +00007445 errno = 0;
7446 limit = fpathconf(fd, name);
7447 if (limit == -1 && errno != 0)
7448 posix_error();
7449 else
7450 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007451 }
7452 return result;
7453}
7454#endif
7455
7456
7457#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007458PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007459"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007460Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007461If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007462
7463static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007464posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007465{
7466 PyObject *result = NULL;
7467 int name;
7468 char *path;
7469
7470 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7471 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007473
Victor Stinner8c62be82010-05-06 00:08:46 +00007474 errno = 0;
7475 limit = pathconf(path, name);
7476 if (limit == -1 && errno != 0) {
7477 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00007478 /* could be a path or name problem */
7479 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007480 else
Stefan Krah99439262010-11-26 12:58:05 +00007481 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00007482 }
7483 else
7484 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007485 }
7486 return result;
7487}
7488#endif
7489
7490#ifdef HAVE_CONFSTR
7491static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007492#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00007493 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007494#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00007495#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007496 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007497#endif
7498#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007500#endif
Fred Draked86ed291999-12-15 15:34:33 +00007501#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007502 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007503#endif
7504#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007505 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007506#endif
7507#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007509#endif
7510#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007512#endif
Fred Drakec9680921999-12-13 16:37:25 +00007513#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007514 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007515#endif
7516#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007517 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007518#endif
7519#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007521#endif
7522#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007523 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007524#endif
7525#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007526 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007527#endif
7528#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007529 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007530#endif
7531#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007532 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007533#endif
7534#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007535 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007536#endif
Fred Draked86ed291999-12-15 15:34:33 +00007537#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00007538 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007539#endif
Fred Drakec9680921999-12-13 16:37:25 +00007540#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007542#endif
Fred Draked86ed291999-12-15 15:34:33 +00007543#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007544 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007545#endif
7546#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007547 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007548#endif
7549#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007551#endif
7552#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007553 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007554#endif
Fred Drakec9680921999-12-13 16:37:25 +00007555#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007557#endif
7558#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007559 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007560#endif
7561#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007562 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007563#endif
7564#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007566#endif
7567#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007568 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007569#endif
7570#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007572#endif
7573#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007574 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007575#endif
7576#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007578#endif
7579#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007580 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007581#endif
7582#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007583 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007584#endif
7585#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007586 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007587#endif
7588#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007590#endif
7591#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007592 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007593#endif
7594#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007596#endif
7597#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007599#endif
7600#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007602#endif
Fred Draked86ed291999-12-15 15:34:33 +00007603#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007604 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007605#endif
7606#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007608#endif
7609#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007611#endif
7612#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007614#endif
7615#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007617#endif
7618#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00007619 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007620#endif
7621#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007623#endif
7624#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007626#endif
7627#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007629#endif
7630#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007632#endif
7633#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007635#endif
7636#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007638#endif
7639#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007641#endif
Fred Drakec9680921999-12-13 16:37:25 +00007642};
7643
7644static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007645conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007646{
7647 return conv_confname(arg, valuep, posix_constants_confstr,
7648 sizeof(posix_constants_confstr)
7649 / sizeof(struct constdef));
7650}
7651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007652PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007653"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007654Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007655
7656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007657posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007658{
7659 PyObject *result = NULL;
7660 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00007661 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00007662 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007663
Victor Stinnercb043522010-09-10 23:49:04 +00007664 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
7665 return NULL;
7666
7667 errno = 0;
7668 len = confstr(name, buffer, sizeof(buffer));
7669 if (len == 0) {
7670 if (errno) {
7671 posix_error();
7672 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00007673 }
7674 else {
Victor Stinnercb043522010-09-10 23:49:04 +00007675 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00007676 }
7677 }
Victor Stinnercb043522010-09-10 23:49:04 +00007678
7679 if ((unsigned int)len >= sizeof(buffer)) {
7680 char *buf = PyMem_Malloc(len);
7681 if (buf == NULL)
7682 return PyErr_NoMemory();
7683 confstr(name, buf, len);
7684 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
7685 PyMem_Free(buf);
7686 }
7687 else
7688 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007689 return result;
7690}
7691#endif
7692
7693
7694#ifdef HAVE_SYSCONF
7695static struct constdef posix_constants_sysconf[] = {
7696#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007697 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007698#endif
7699#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007700 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007701#endif
7702#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007704#endif
7705#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007706 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007707#endif
7708#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007709 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007710#endif
7711#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007713#endif
7714#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007716#endif
7717#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007718 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007719#endif
7720#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007721 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007722#endif
7723#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007724 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007725#endif
Fred Draked86ed291999-12-15 15:34:33 +00007726#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007728#endif
7729#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007731#endif
Fred Drakec9680921999-12-13 16:37:25 +00007732#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007734#endif
Fred Drakec9680921999-12-13 16:37:25 +00007735#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007736 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007737#endif
7738#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007739 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007740#endif
7741#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007742 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007743#endif
7744#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007745 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007746#endif
7747#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007749#endif
Fred Draked86ed291999-12-15 15:34:33 +00007750#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007752#endif
Fred Drakec9680921999-12-13 16:37:25 +00007753#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007755#endif
7756#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007758#endif
7759#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007761#endif
7762#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007764#endif
7765#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007767#endif
Fred Draked86ed291999-12-15 15:34:33 +00007768#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007770#endif
Fred Drakec9680921999-12-13 16:37:25 +00007771#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007773#endif
7774#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007776#endif
7777#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007779#endif
7780#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007782#endif
7783#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007785#endif
7786#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007788#endif
7789#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007791#endif
7792#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007793 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007794#endif
7795#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007796 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007797#endif
7798#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007799 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007800#endif
7801#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007803#endif
7804#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007806#endif
7807#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007809#endif
7810#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007811 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007812#endif
7813#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007814 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007815#endif
7816#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007818#endif
7819#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007821#endif
7822#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007824#endif
7825#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007826 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007827#endif
7828#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007830#endif
7831#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007833#endif
7834#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007835 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007836#endif
7837#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007839#endif
Fred Draked86ed291999-12-15 15:34:33 +00007840#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007842#endif
Fred Drakec9680921999-12-13 16:37:25 +00007843#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007845#endif
7846#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007848#endif
7849#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007851#endif
Fred Draked86ed291999-12-15 15:34:33 +00007852#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007854#endif
Fred Drakec9680921999-12-13 16:37:25 +00007855#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007856 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007857#endif
Fred Draked86ed291999-12-15 15:34:33 +00007858#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007860#endif
7861#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007863#endif
Fred Drakec9680921999-12-13 16:37:25 +00007864#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007866#endif
7867#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007869#endif
7870#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007872#endif
7873#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007875#endif
Fred Draked86ed291999-12-15 15:34:33 +00007876#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007878#endif
Fred Drakec9680921999-12-13 16:37:25 +00007879#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007881#endif
7882#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007884#endif
7885#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007887#endif
7888#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007890#endif
7891#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007893#endif
7894#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007896#endif
7897#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007899#endif
Fred Draked86ed291999-12-15 15:34:33 +00007900#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007902#endif
Fred Drakec9680921999-12-13 16:37:25 +00007903#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007904 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007905#endif
7906#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007907 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007908#endif
Fred Draked86ed291999-12-15 15:34:33 +00007909#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007910 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007911#endif
Fred Drakec9680921999-12-13 16:37:25 +00007912#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007913 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007914#endif
7915#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007916 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007917#endif
7918#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007920#endif
7921#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007923#endif
7924#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007926#endif
7927#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007929#endif
7930#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007932#endif
7933#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007935#endif
7936#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007938#endif
Fred Draked86ed291999-12-15 15:34:33 +00007939#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007941#endif
7942#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007944#endif
Fred Drakec9680921999-12-13 16:37:25 +00007945#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007946 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007947#endif
7948#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007949 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007950#endif
7951#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007953#endif
7954#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007955 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007956#endif
7957#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007958 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007959#endif
7960#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007962#endif
7963#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007964 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007965#endif
7966#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007967 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007968#endif
7969#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007970 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007971#endif
7972#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007973 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007974#endif
7975#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007976 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007977#endif
7978#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007979 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007980#endif
7981#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007983#endif
7984#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007985 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007986#endif
7987#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007989#endif
7990#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007992#endif
7993#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007995#endif
7996#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007998#endif
7999#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008001#endif
8002#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
8005#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008006 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008007#endif
8008#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008009 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008010#endif
8011#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008013#endif
8014#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008016#endif
8017#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008018 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008019#endif
8020#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008022#endif
8023#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008025#endif
8026#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008028#endif
8029#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008031#endif
8032#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008034#endif
8035#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008037#endif
8038#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
Fred Draked86ed291999-12-15 15:34:33 +00008050#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008052#endif
Fred Drakec9680921999-12-13 16:37:25 +00008053#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008054 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008055#endif
8056#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008058#endif
8059#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008061#endif
8062#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008064#endif
8065#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008067#endif
8068#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008070#endif
8071#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008073#endif
8074#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008075 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008076#endif
8077#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008079#endif
8080#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
8083#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008093 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
8095#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008096 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008097#endif
8098#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008099 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008100#endif
8101#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008103#endif
8104#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008105 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008106#endif
8107#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008108 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008109#endif
8110#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008111 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008114 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008117 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
8131#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008133#endif
8134#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008136#endif
8137#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008139#endif
8140#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008142#endif
8143#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008145#endif
8146#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008148#endif
8149#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008151#endif
8152#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008153 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008154#endif
8155#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008156 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008157#endif
8158#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008160#endif
8161#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008162 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008163#endif
8164#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008165 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008166#endif
8167#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008169#endif
8170#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008171 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008172#endif
8173#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008174 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008175#endif
8176#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008178#endif
8179#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008181#endif
8182#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008184#endif
8185#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008186 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008187#endif
8188};
8189
8190static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008191conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008192{
8193 return conv_confname(arg, valuep, posix_constants_sysconf,
8194 sizeof(posix_constants_sysconf)
8195 / sizeof(struct constdef));
8196}
8197
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008198PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008199"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008200Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008201
8202static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008203posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008204{
8205 PyObject *result = NULL;
8206 int name;
8207
8208 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8209 int value;
8210
8211 errno = 0;
8212 value = sysconf(name);
8213 if (value == -1 && errno != 0)
8214 posix_error();
8215 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008216 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008217 }
8218 return result;
8219}
8220#endif
8221
8222
Fred Drakebec628d1999-12-15 18:31:10 +00008223/* This code is used to ensure that the tables of configuration value names
8224 * are in sorted order as required by conv_confname(), and also to build the
8225 * the exported dictionaries that are used to publish information about the
8226 * names available on the host platform.
8227 *
8228 * Sorting the table at runtime ensures that the table is properly ordered
8229 * when used, even for platforms we're not able to test on. It also makes
8230 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008231 */
Fred Drakebec628d1999-12-15 18:31:10 +00008232
8233static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008234cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008235{
8236 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008237 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008238 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008239 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008240
8241 return strcmp(c1->name, c2->name);
8242}
8243
8244static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008245setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008247{
Fred Drakebec628d1999-12-15 18:31:10 +00008248 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008249 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008250
8251 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8252 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008253 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008255
Barry Warsaw3155db32000-04-13 15:20:40 +00008256 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008257 PyObject *o = PyLong_FromLong(table[i].value);
8258 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8259 Py_XDECREF(o);
8260 Py_DECREF(d);
8261 return -1;
8262 }
8263 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008264 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008265 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008266}
8267
Fred Drakebec628d1999-12-15 18:31:10 +00008268/* Return -1 on failure, 0 on success. */
8269static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008270setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008271{
8272#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008273 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008274 sizeof(posix_constants_pathconf)
8275 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008276 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008277 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008278#endif
8279#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008280 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008281 sizeof(posix_constants_confstr)
8282 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008283 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008284 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008285#endif
8286#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008287 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008288 sizeof(posix_constants_sysconf)
8289 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008290 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008291 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008292#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008293 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008294}
Fred Draked86ed291999-12-15 15:34:33 +00008295
8296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008297PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008298"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008299Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008300in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008301
8302static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008303posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008304{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008305 abort();
8306 /*NOTREACHED*/
8307 Py_FatalError("abort() called from Python code didn't abort!");
8308 return NULL;
8309}
Fred Drakebec628d1999-12-15 18:31:10 +00008310
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008311#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008312PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008313"startfile(filepath [, operation]) - Start a file with its associated\n\
8314application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008315\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008316When \"operation\" is not specified or \"open\", this acts like\n\
8317double-clicking the file in Explorer, or giving the file name as an\n\
8318argument to the DOS \"start\" command: the file is opened with whatever\n\
8319application (if any) its extension is associated.\n\
8320When another \"operation\" is given, it specifies what should be done with\n\
8321the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008322\n\
8323startfile returns as soon as the associated application is launched.\n\
8324There is no option to wait for the application to close, and no way\n\
8325to retrieve the application's exit status.\n\
8326\n\
8327The filepath is relative to the current directory. If you want to use\n\
8328an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008329the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008330
8331static PyObject *
8332win32_startfile(PyObject *self, PyObject *args)
8333{
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 PyObject *ofilepath;
8335 char *filepath;
8336 char *operation = NULL;
8337 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00008338
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 PyObject *unipath, *woperation = NULL;
8340 if (!PyArg_ParseTuple(args, "U|s:startfile",
8341 &unipath, &operation)) {
8342 PyErr_Clear();
8343 goto normal;
8344 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008345
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 if (operation) {
8347 woperation = PyUnicode_DecodeASCII(operation,
8348 strlen(operation), NULL);
8349 if (!woperation) {
8350 PyErr_Clear();
8351 operation = NULL;
8352 goto normal;
8353 }
8354 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008355
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 Py_BEGIN_ALLOW_THREADS
8357 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8358 PyUnicode_AS_UNICODE(unipath),
8359 NULL, NULL, SW_SHOWNORMAL);
8360 Py_END_ALLOW_THREADS
8361
8362 Py_XDECREF(woperation);
8363 if (rc <= (HINSTANCE)32) {
8364 PyObject *errval = win32_error_unicode("startfile",
8365 PyUnicode_AS_UNICODE(unipath));
8366 return errval;
8367 }
8368 Py_INCREF(Py_None);
8369 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008370
8371normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00008372 if (!PyArg_ParseTuple(args, "O&|s:startfile",
8373 PyUnicode_FSConverter, &ofilepath,
8374 &operation))
8375 return NULL;
8376 filepath = PyBytes_AsString(ofilepath);
8377 Py_BEGIN_ALLOW_THREADS
8378 rc = ShellExecute((HWND)0, operation, filepath,
8379 NULL, NULL, SW_SHOWNORMAL);
8380 Py_END_ALLOW_THREADS
8381 if (rc <= (HINSTANCE)32) {
8382 PyObject *errval = win32_error("startfile", filepath);
8383 Py_DECREF(ofilepath);
8384 return errval;
8385 }
8386 Py_DECREF(ofilepath);
8387 Py_INCREF(Py_None);
8388 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008389}
8390#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008391
Martin v. Löwis438b5342002-12-27 10:16:42 +00008392#ifdef HAVE_GETLOADAVG
8393PyDoc_STRVAR(posix_getloadavg__doc__,
8394"getloadavg() -> (float, float, float)\n\n\
8395Return the number of processes in the system run queue averaged over\n\
8396the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8397was unobtainable");
8398
8399static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008400posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008401{
8402 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008403 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008404 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8405 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008406 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00008407 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008408}
8409#endif
8410
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008411#ifdef MS_WINDOWS
8412
8413PyDoc_STRVAR(win32_urandom__doc__,
8414"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008415Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008416
8417typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8418 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8419 DWORD dwFlags );
8420typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8421 BYTE *pbBuffer );
8422
8423static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00008424/* This handle is never explicitly released. Instead, the operating
8425 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008426static HCRYPTPROV hCryptProv = 0;
8427
Tim Peters4ad82172004-08-30 17:02:04 +00008428static PyObject*
8429win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008430{
Victor Stinner8c62be82010-05-06 00:08:46 +00008431 int howMany;
8432 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008433
Victor Stinner8c62be82010-05-06 00:08:46 +00008434 /* Read arguments */
8435 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8436 return NULL;
8437 if (howMany < 0)
8438 return PyErr_Format(PyExc_ValueError,
8439 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008440
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 if (hCryptProv == 0) {
8442 HINSTANCE hAdvAPI32 = NULL;
8443 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008444
Victor Stinner8c62be82010-05-06 00:08:46 +00008445 /* Obtain handle to the DLL containing CryptoAPI
8446 This should not fail */
8447 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8448 if(hAdvAPI32 == NULL)
8449 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008450
Victor Stinner8c62be82010-05-06 00:08:46 +00008451 /* Obtain pointers to the CryptoAPI functions
8452 This will fail on some early versions of Win95 */
8453 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8454 hAdvAPI32,
8455 "CryptAcquireContextA");
8456 if (pCryptAcquireContext == NULL)
8457 return PyErr_Format(PyExc_NotImplementedError,
8458 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008459
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8461 hAdvAPI32, "CryptGenRandom");
8462 if (pCryptGenRandom == NULL)
8463 return PyErr_Format(PyExc_NotImplementedError,
8464 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008465
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 /* Acquire context */
8467 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8468 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8469 return win32_error("CryptAcquireContext", NULL);
8470 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008471
Victor Stinner8c62be82010-05-06 00:08:46 +00008472 /* Allocate bytes */
8473 result = PyBytes_FromStringAndSize(NULL, howMany);
8474 if (result != NULL) {
8475 /* Get random data */
8476 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
8477 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8478 PyBytes_AS_STRING(result))) {
8479 Py_DECREF(result);
8480 return win32_error("CryptGenRandom", NULL);
8481 }
8482 }
8483 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008484}
8485#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008486
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008487PyDoc_STRVAR(device_encoding__doc__,
8488"device_encoding(fd) -> str\n\n\
8489Return a string describing the encoding of the device\n\
8490if the output is a terminal; else return None.");
8491
8492static PyObject *
8493device_encoding(PyObject *self, PyObject *args)
8494{
Victor Stinner8c62be82010-05-06 00:08:46 +00008495 int fd;
8496 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
8497 return NULL;
8498 if (!_PyVerify_fd(fd) || !isatty(fd)) {
8499 Py_INCREF(Py_None);
8500 return Py_None;
8501 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008502#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 if (fd == 0) {
8504 char buf[100];
8505 sprintf(buf, "cp%d", GetConsoleCP());
8506 return PyUnicode_FromString(buf);
8507 }
8508 if (fd == 1 || fd == 2) {
8509 char buf[100];
8510 sprintf(buf, "cp%d", GetConsoleOutputCP());
8511 return PyUnicode_FromString(buf);
8512 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008513#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00008514 {
8515 char *codeset = nl_langinfo(CODESET);
8516 if (codeset != NULL && codeset[0] != 0)
8517 return PyUnicode_FromString(codeset);
8518 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008519#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008520 Py_INCREF(Py_None);
8521 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008522}
8523
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008524#ifdef __VMS
8525/* Use openssl random routine */
8526#include <openssl/rand.h>
8527PyDoc_STRVAR(vms_urandom__doc__,
8528"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008529Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008530
8531static PyObject*
8532vms_urandom(PyObject *self, PyObject *args)
8533{
Victor Stinner8c62be82010-05-06 00:08:46 +00008534 int howMany;
8535 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008536
Victor Stinner8c62be82010-05-06 00:08:46 +00008537 /* Read arguments */
8538 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8539 return NULL;
8540 if (howMany < 0)
8541 return PyErr_Format(PyExc_ValueError,
8542 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008543
Victor Stinner8c62be82010-05-06 00:08:46 +00008544 /* Allocate bytes */
8545 result = PyBytes_FromStringAndSize(NULL, howMany);
8546 if (result != NULL) {
8547 /* Get random data */
8548 if (RAND_pseudo_bytes((unsigned char*)
8549 PyBytes_AS_STRING(result),
8550 howMany) < 0) {
8551 Py_DECREF(result);
8552 return PyErr_Format(PyExc_ValueError,
8553 "RAND_pseudo_bytes");
8554 }
8555 }
8556 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008557}
8558#endif
8559
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008560#ifdef HAVE_SETRESUID
8561PyDoc_STRVAR(posix_setresuid__doc__,
8562"setresuid(ruid, euid, suid)\n\n\
8563Set the current process's real, effective, and saved user ids.");
8564
8565static PyObject*
8566posix_setresuid (PyObject *self, PyObject *args)
8567{
Victor Stinner8c62be82010-05-06 00:08:46 +00008568 /* We assume uid_t is no larger than a long. */
8569 long ruid, euid, suid;
8570 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8571 return NULL;
8572 if (setresuid(ruid, euid, suid) < 0)
8573 return posix_error();
8574 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008575}
8576#endif
8577
8578#ifdef HAVE_SETRESGID
8579PyDoc_STRVAR(posix_setresgid__doc__,
8580"setresgid(rgid, egid, sgid)\n\n\
8581Set the current process's real, effective, and saved group ids.");
8582
8583static PyObject*
8584posix_setresgid (PyObject *self, PyObject *args)
8585{
Victor Stinner8c62be82010-05-06 00:08:46 +00008586 /* We assume uid_t is no larger than a long. */
8587 long rgid, egid, sgid;
8588 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8589 return NULL;
8590 if (setresgid(rgid, egid, sgid) < 0)
8591 return posix_error();
8592 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008593}
8594#endif
8595
8596#ifdef HAVE_GETRESUID
8597PyDoc_STRVAR(posix_getresuid__doc__,
8598"getresuid() -> (ruid, euid, suid)\n\n\
8599Get tuple of the current process's real, effective, and saved user ids.");
8600
8601static PyObject*
8602posix_getresuid (PyObject *self, PyObject *noargs)
8603{
Victor Stinner8c62be82010-05-06 00:08:46 +00008604 uid_t ruid, euid, suid;
8605 long l_ruid, l_euid, l_suid;
8606 if (getresuid(&ruid, &euid, &suid) < 0)
8607 return posix_error();
8608 /* Force the values into long's as we don't know the size of uid_t. */
8609 l_ruid = ruid;
8610 l_euid = euid;
8611 l_suid = suid;
8612 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008613}
8614#endif
8615
8616#ifdef HAVE_GETRESGID
8617PyDoc_STRVAR(posix_getresgid__doc__,
8618"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00008619Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008620
8621static PyObject*
8622posix_getresgid (PyObject *self, PyObject *noargs)
8623{
Victor Stinner8c62be82010-05-06 00:08:46 +00008624 uid_t rgid, egid, sgid;
8625 long l_rgid, l_egid, l_sgid;
8626 if (getresgid(&rgid, &egid, &sgid) < 0)
8627 return posix_error();
8628 /* Force the values into long's as we don't know the size of uid_t. */
8629 l_rgid = rgid;
8630 l_egid = egid;
8631 l_sgid = sgid;
8632 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008633}
8634#endif
8635
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008636/* Posix *at family of functions:
8637 faccessat, fchmodat, fchownat, fstatat, futimesat,
8638 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
8639 unlinkat, utimensat, mkfifoat */
8640
8641#ifdef HAVE_FACCESSAT
8642PyDoc_STRVAR(posix_faccessat__doc__,
8643"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
8644Like access() but if path is relative, it is taken as relative to dirfd.\n\
8645flags is optional and can be constructed by ORing together zero or more\n\
8646of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
8647If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8648is interpreted relative to the current working directory.");
8649
8650static PyObject *
8651posix_faccessat(PyObject *self, PyObject *args)
8652{
8653 PyObject *opath;
8654 char *path;
8655 int mode;
8656 int res;
8657 int dirfd, flags = 0;
8658 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
8659 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8660 return NULL;
8661 path = PyBytes_AsString(opath);
8662 Py_BEGIN_ALLOW_THREADS
8663 res = faccessat(dirfd, path, mode, flags);
8664 Py_END_ALLOW_THREADS
8665 Py_DECREF(opath);
8666 return PyBool_FromLong(res == 0);
8667}
8668#endif
8669
8670#ifdef HAVE_FCHMODAT
8671PyDoc_STRVAR(posix_fchmodat__doc__,
8672"fchmodat(dirfd, path, mode, flags=0)\n\n\
8673Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
8674flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8675If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8676is interpreted relative to the current working directory.");
8677
8678static PyObject *
8679posix_fchmodat(PyObject *self, PyObject *args)
8680{
8681 int dirfd, mode, res;
8682 int flags = 0;
8683 PyObject *opath;
8684 char *path;
8685
8686 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
8687 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8688 return NULL;
8689
8690 path = PyBytes_AsString(opath);
8691
8692 Py_BEGIN_ALLOW_THREADS
8693 res = fchmodat(dirfd, path, mode, flags);
8694 Py_END_ALLOW_THREADS
8695 Py_DECREF(opath);
8696 if (res < 0)
8697 return posix_error();
8698 Py_RETURN_NONE;
8699}
8700#endif /* HAVE_FCHMODAT */
8701
8702#ifdef HAVE_FCHOWNAT
8703PyDoc_STRVAR(posix_fchownat__doc__,
8704"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
8705Like chown() but if path is relative, it is taken as relative to dirfd.\n\
8706flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8707If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8708is interpreted relative to the current working directory.");
8709
8710static PyObject *
8711posix_fchownat(PyObject *self, PyObject *args)
8712{
8713 PyObject *opath;
8714 int dirfd, res;
8715 long uid, gid;
8716 int flags = 0;
8717 char *path;
8718
8719 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
8720 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
8721 return NULL;
8722
8723 path = PyBytes_AsString(opath);
8724
8725 Py_BEGIN_ALLOW_THREADS
8726 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
8727 Py_END_ALLOW_THREADS
8728 Py_DECREF(opath);
8729 if (res < 0)
8730 return posix_error();
8731 Py_RETURN_NONE;
8732}
8733#endif /* HAVE_FCHOWNAT */
8734
8735#ifdef HAVE_FSTATAT
8736PyDoc_STRVAR(posix_fstatat__doc__,
8737"fstatat(dirfd, path, flags=0) -> stat result\n\n\
8738Like stat() but if path is relative, it is taken as relative to dirfd.\n\
8739flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8740If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8741is interpreted relative to the current working directory.");
8742
8743static PyObject *
8744posix_fstatat(PyObject *self, PyObject *args)
8745{
8746 PyObject *opath;
8747 char *path;
8748 STRUCT_STAT st;
8749 int dirfd, res, flags = 0;
8750
8751 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
8752 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8753 return NULL;
8754 path = PyBytes_AsString(opath);
8755
8756 Py_BEGIN_ALLOW_THREADS
8757 res = fstatat(dirfd, path, &st, flags);
8758 Py_END_ALLOW_THREADS
8759 Py_DECREF(opath);
8760 if (res != 0)
8761 return posix_error();
8762
8763 return _pystat_fromstructstat(&st);
8764}
8765#endif
8766
8767#ifdef HAVE_FUTIMESAT
8768PyDoc_STRVAR(posix_futimesat__doc__,
8769"futimesat(dirfd, path, (atime, mtime))\n\
8770futimesat(dirfd, path, None)\n\n\
8771Like utime() but if path is relative, it is taken as relative to dirfd.\n\
8772If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8773is interpreted relative to the current working directory.");
8774
8775static PyObject *
8776posix_futimesat(PyObject *self, PyObject *args)
8777{
8778 PyObject *opath;
8779 char *path;
8780 int res, dirfd;
8781 PyObject* arg;
8782
8783 struct timeval buf[2];
8784
8785 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
8786 &dirfd, PyUnicode_FSConverter, &opath, &arg))
8787 return NULL;
8788 path = PyBytes_AsString(opath);
8789 if (arg == Py_None) {
8790 /* optional time values not given */
8791 Py_BEGIN_ALLOW_THREADS
8792 res = futimesat(dirfd, path, NULL);
8793 Py_END_ALLOW_THREADS
8794 }
8795 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
8796 PyErr_SetString(PyExc_TypeError,
8797 "futimesat() arg 3 must be a tuple (atime, mtime)");
8798 Py_DECREF(opath);
8799 return NULL;
8800 }
8801 else {
8802 if (extract_time(PyTuple_GET_ITEM(arg, 0),
8803 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
8804 Py_DECREF(opath);
8805 return NULL;
8806 }
8807 if (extract_time(PyTuple_GET_ITEM(arg, 1),
8808 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
8809 Py_DECREF(opath);
8810 return NULL;
8811 }
8812 Py_BEGIN_ALLOW_THREADS
8813 res = futimesat(dirfd, path, buf);
8814 Py_END_ALLOW_THREADS
8815 }
8816 Py_DECREF(opath);
8817 if (res < 0) {
8818 return posix_error();
8819 }
8820 Py_RETURN_NONE;
8821}
8822#endif
8823
8824#ifdef HAVE_LINKAT
8825PyDoc_STRVAR(posix_linkat__doc__,
8826"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
8827Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
8828and if dstpath is relative, it is taken as relative to dstfd.\n\
8829flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
8830If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
8831srcpath is interpreted relative to the current working directory. This\n\
8832also applies for dstpath.");
8833
8834static PyObject *
8835posix_linkat(PyObject *self, PyObject *args)
8836{
8837 PyObject *osrc, *odst;
8838 char *src, *dst;
8839 int res, srcfd, dstfd;
8840 int flags = 0;
8841
8842 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
8843 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
8844 return NULL;
8845 src = PyBytes_AsString(osrc);
8846 dst = PyBytes_AsString(odst);
8847 Py_BEGIN_ALLOW_THREADS
8848 res = linkat(srcfd, src, dstfd, dst, flags);
8849 Py_END_ALLOW_THREADS
8850 Py_DECREF(osrc);
8851 Py_DECREF(odst);
8852 if (res < 0)
8853 return posix_error();
8854 Py_RETURN_NONE;
8855}
8856#endif /* HAVE_LINKAT */
8857
8858#ifdef HAVE_MKDIRAT
8859PyDoc_STRVAR(posix_mkdirat__doc__,
8860"mkdirat(dirfd, path, mode=0o777)\n\n\
8861Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
8862If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8863is interpreted relative to the current working directory.");
8864
8865static PyObject *
8866posix_mkdirat(PyObject *self, PyObject *args)
8867{
8868 int res, dirfd;
8869 PyObject *opath;
8870 char *path;
8871 int mode = 0777;
8872
8873 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
8874 &dirfd, PyUnicode_FSConverter, &opath, &mode))
8875 return NULL;
8876 path = PyBytes_AsString(opath);
8877 Py_BEGIN_ALLOW_THREADS
8878 res = mkdirat(dirfd, path, mode);
8879 Py_END_ALLOW_THREADS
8880 Py_DECREF(opath);
8881 if (res < 0)
8882 return posix_error();
8883 Py_RETURN_NONE;
8884}
8885#endif
8886
8887#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
8888PyDoc_STRVAR(posix_mknodat__doc__,
8889"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
8890Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
8891If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8892is interpreted relative to the current working directory.");
8893
8894static PyObject *
8895posix_mknodat(PyObject *self, PyObject *args)
8896{
8897 PyObject *opath;
8898 char *filename;
8899 int mode = 0600;
8900 int device = 0;
8901 int res, dirfd;
8902 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
8903 PyUnicode_FSConverter, &opath, &mode, &device))
8904 return NULL;
8905 filename = PyBytes_AS_STRING(opath);
8906 Py_BEGIN_ALLOW_THREADS
8907 res = mknodat(dirfd, filename, mode, device);
8908 Py_END_ALLOW_THREADS
8909 Py_DECREF(opath);
8910 if (res < 0)
8911 return posix_error();
8912 Py_RETURN_NONE;
8913}
8914#endif
8915
8916#ifdef HAVE_OPENAT
8917PyDoc_STRVAR(posix_openat__doc__,
8918"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
8919Like open() but if path is relative, it is taken as relative to dirfd.\n\
8920If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8921is interpreted relative to the current working directory.");
8922
8923static PyObject *
8924posix_openat(PyObject *self, PyObject *args)
8925{
8926 PyObject *ofile;
8927 char *file;
8928 int flag, dirfd, fd;
8929 int mode = 0777;
8930
8931 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
8932 &dirfd, PyUnicode_FSConverter, &ofile,
8933 &flag, &mode))
8934 return NULL;
8935 file = PyBytes_AsString(ofile);
8936 Py_BEGIN_ALLOW_THREADS
8937 fd = openat(dirfd, file, flag, mode);
8938 Py_END_ALLOW_THREADS
8939 Py_DECREF(ofile);
8940 if (fd < 0)
8941 return posix_error();
8942 return PyLong_FromLong((long)fd);
8943}
8944#endif
8945
8946#ifdef HAVE_READLINKAT
8947PyDoc_STRVAR(posix_readlinkat__doc__,
8948"readlinkat(dirfd, path) -> path\n\n\
8949Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
8950If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8951is interpreted relative to the current working directory.");
8952
8953static PyObject *
8954posix_readlinkat(PyObject *self, PyObject *args)
8955{
8956 PyObject *v, *opath;
8957 char buf[MAXPATHLEN];
8958 char *path;
8959 int n, dirfd;
8960 int arg_is_unicode = 0;
8961
8962 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
8963 &dirfd, PyUnicode_FSConverter, &opath))
8964 return NULL;
8965 path = PyBytes_AsString(opath);
8966 v = PySequence_GetItem(args, 1);
8967 if (v == NULL) {
8968 Py_DECREF(opath);
8969 return NULL;
8970 }
8971
8972 if (PyUnicode_Check(v)) {
8973 arg_is_unicode = 1;
8974 }
8975 Py_DECREF(v);
8976
8977 Py_BEGIN_ALLOW_THREADS
8978 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
8979 Py_END_ALLOW_THREADS
8980 Py_DECREF(opath);
8981 if (n < 0)
8982 return posix_error();
8983
8984 if (arg_is_unicode)
8985 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
8986 else
8987 return PyBytes_FromStringAndSize(buf, n);
8988}
8989#endif /* HAVE_READLINKAT */
8990
8991#ifdef HAVE_RENAMEAT
8992PyDoc_STRVAR(posix_renameat__doc__,
8993"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
8994Like rename() but if oldpath is relative, it is taken as relative to\n\
8995olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
8996If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
8997oldpath is interpreted relative to the current working directory. This\n\
8998also applies for newpath.");
8999
9000static PyObject *
9001posix_renameat(PyObject *self, PyObject *args)
9002{
9003 int res;
9004 PyObject *opathold, *opathnew;
9005 char *opath, *npath;
9006 int oldfd, newfd;
9007
9008 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9009 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9010 return NULL;
9011 opath = PyBytes_AsString(opathold);
9012 npath = PyBytes_AsString(opathnew);
9013 Py_BEGIN_ALLOW_THREADS
9014 res = renameat(oldfd, opath, newfd, npath);
9015 Py_END_ALLOW_THREADS
9016 Py_DECREF(opathold);
9017 Py_DECREF(opathnew);
9018 if (res < 0)
9019 return posix_error();
9020 Py_RETURN_NONE;
9021}
9022#endif
9023
9024#if HAVE_SYMLINKAT
9025PyDoc_STRVAR(posix_symlinkat__doc__,
9026"symlinkat(src, dstfd, dst)\n\n\
9027Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9028If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9029is interpreted relative to the current working directory.");
9030
9031static PyObject *
9032posix_symlinkat(PyObject *self, PyObject *args)
9033{
9034 int res, dstfd;
9035 PyObject *osrc, *odst;
9036 char *src, *dst;
9037
9038 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9039 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9040 return NULL;
9041 src = PyBytes_AsString(osrc);
9042 dst = PyBytes_AsString(odst);
9043 Py_BEGIN_ALLOW_THREADS
9044 res = symlinkat(src, dstfd, dst);
9045 Py_END_ALLOW_THREADS
9046 Py_DECREF(osrc);
9047 Py_DECREF(odst);
9048 if (res < 0)
9049 return posix_error();
9050 Py_RETURN_NONE;
9051}
9052#endif /* HAVE_SYMLINKAT */
9053
9054#ifdef HAVE_UNLINKAT
9055PyDoc_STRVAR(posix_unlinkat__doc__,
9056"unlinkat(dirfd, path, flags=0)\n\n\
9057Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9058flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9059specified, unlinkat() behaves like rmdir().\n\
9060If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9061is interpreted relative to the current working directory.");
9062
9063static PyObject *
9064posix_unlinkat(PyObject *self, PyObject *args)
9065{
9066 int dirfd, res, flags = 0;
9067 PyObject *opath;
9068 char *path;
9069
9070 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9071 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9072 return NULL;
9073 path = PyBytes_AsString(opath);
9074 Py_BEGIN_ALLOW_THREADS
9075 res = unlinkat(dirfd, path, flags);
9076 Py_END_ALLOW_THREADS
9077 Py_DECREF(opath);
9078 if (res < 0)
9079 return posix_error();
9080 Py_RETURN_NONE;
9081}
9082#endif
9083
9084#ifdef HAVE_UTIMENSAT
9085PyDoc_STRVAR(posix_utimensat__doc__,
9086"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9087 (mtime_sec, mtime_nsec), flags)\n\
9088utimensat(dirfd, path, None, None, flags)\n\n\
9089Updates the timestamps of a file with nanosecond precision. If path is\n\
9090relative, it is taken as relative to dirfd.\n\
9091The second form sets atime and mtime to the current time.\n\
9092flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9093If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9094is interpreted relative to the current working directory.\n\
9095If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9096current time.\n\
9097If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9098
9099static PyObject *
9100posix_utimensat(PyObject *self, PyObject *args)
9101{
9102 PyObject *opath;
9103 char *path;
9104 int res, dirfd, flags = 0;
9105 PyObject *atime, *mtime;
9106
9107 struct timespec buf[2];
9108
9109 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9110 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9111 return NULL;
9112 path = PyBytes_AsString(opath);
9113 if (atime == Py_None && mtime == Py_None) {
9114 /* optional time values not given */
9115 Py_BEGIN_ALLOW_THREADS
9116 res = utimensat(dirfd, path, NULL, flags);
9117 Py_END_ALLOW_THREADS
9118 }
9119 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9120 PyErr_SetString(PyExc_TypeError,
9121 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9122 Py_DECREF(opath);
9123 return NULL;
9124 }
9125 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9126 PyErr_SetString(PyExc_TypeError,
9127 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9128 Py_DECREF(opath);
9129 return NULL;
9130 }
9131 else {
9132 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9133 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9134 Py_DECREF(opath);
9135 return NULL;
9136 }
9137 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9138 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9139 Py_DECREF(opath);
9140 return NULL;
9141 }
9142 Py_BEGIN_ALLOW_THREADS
9143 res = utimensat(dirfd, path, buf, flags);
9144 Py_END_ALLOW_THREADS
9145 }
9146 Py_DECREF(opath);
9147 if (res < 0) {
9148 return posix_error();
9149 }
9150 Py_RETURN_NONE;
9151}
9152#endif
9153
9154#ifdef HAVE_MKFIFOAT
9155PyDoc_STRVAR(posix_mkfifoat__doc__,
9156"mkfifoat(dirfd, path, mode=0o666)\n\n\
9157Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9158If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9159is interpreted relative to the current working directory.");
9160
9161static PyObject *
9162posix_mkfifoat(PyObject *self, PyObject *args)
9163{
9164 PyObject *opath;
9165 char *filename;
9166 int mode = 0666;
9167 int res, dirfd;
9168 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9169 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9170 return NULL;
9171 filename = PyBytes_AS_STRING(opath);
9172 Py_BEGIN_ALLOW_THREADS
9173 res = mkfifoat(dirfd, filename, mode);
9174 Py_END_ALLOW_THREADS
9175 Py_DECREF(opath);
9176 if (res < 0)
9177 return posix_error();
9178 Py_RETURN_NONE;
9179}
9180#endif
9181
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009182static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009183 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009184#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009185 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009186#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009187 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009188#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009189 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009190#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009191 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009192#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009193 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009194#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009195#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009196 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009197#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009198#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009199 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009200#endif /* HAVE_LCHMOD */
9201#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009202 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009203#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009204#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009205 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009206#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009207#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009208 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009209#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009210#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009212#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009213#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009214 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009215#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009216#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9218 METH_NOARGS, posix_getcwd__doc__},
9219 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9220 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009221#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009222#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009223 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009224#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009225 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009226#ifdef HAVE_FDOPENDIR
9227 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9228#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009229 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
9230 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009231#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00009232 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009233#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009234#ifdef HAVE_GETPRIORITY
9235 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
9236#endif /* HAVE_GETPRIORITY */
9237#ifdef HAVE_SETPRIORITY
9238 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
9239#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009240#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009241 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009242#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00009243#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009244 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00009245#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009246 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
9247 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
9248 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00009249 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00009250#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00009251 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009252#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00009253#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009254 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00009255 win_symlink__doc__},
9256#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009257#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00009258 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009259#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009260 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009261#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009262 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009263#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00009264 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
9265 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
9266 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009267#ifdef HAVE_FUTIMES
9268 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
9269#endif
9270#ifdef HAVE_LUTIMES
9271 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
9272#endif
9273#ifdef HAVE_FUTIMENS
9274 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
9275#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009276#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00009277 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009278#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00009279 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009280#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00009281 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
9282 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009283#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009284#ifdef HAVE_FEXECVE
9285 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
9286#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00009287#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00009288 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
9289 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009290#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00009291 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
9292 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009293#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00009294#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009295#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00009296 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009297#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009298#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00009299 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009300#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009301#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00009302 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009303#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00009304#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00009306#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009307#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009308 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009309#endif /* HAVE_GETEGID */
9310#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009311 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009312#endif /* HAVE_GETEUID */
9313#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009314 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009315#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00009316#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009318#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009319 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009320#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009321 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009322#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009323#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009324 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009325#endif /* HAVE_GETPPID */
9326#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009327 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009328#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00009329#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009330 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00009331#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00009332#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00009333 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009334#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009335#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00009336 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009337#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00009338#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00009340#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00009341#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009342 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
9343 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00009344 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00009345#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009346#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009347 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009348#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009349#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009350 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009351#endif /* HAVE_SETEUID */
9352#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009353 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009354#endif /* HAVE_SETEGID */
9355#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009356 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009357#endif /* HAVE_SETREUID */
9358#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009359 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009360#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009361#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009363#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009364#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009366#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00009367#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009368 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00009369#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00009370#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009371 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00009372#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009373#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009374 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009375#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009376#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009378#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009379#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009381#endif /* HAVE_WAIT3 */
9382#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009384#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009385#if defined(HAVE_WAITID) && !defined(__APPLE__)
9386 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
9387#endif
Tim Petersab034fa2002-02-01 11:27:43 +00009388#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009389 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009390#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009391#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009392 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009393#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009394#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009396#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009397#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009398 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009399#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009400#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009401 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009402#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009403#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009405#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00009406 {"open", posix_open, METH_VARARGS, posix_open__doc__},
9407 {"close", posix_close, METH_VARARGS, posix_close__doc__},
9408 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
9409 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
9410 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
9411 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009412#ifdef HAVE_LOCKF
9413 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
9414#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009415 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
9416 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009417#ifdef HAVE_READV
9418 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
9419#endif
9420#ifdef HAVE_PREAD
9421 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
9422#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009423 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009424#ifdef HAVE_WRITEV
9425 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
9426#endif
9427#ifdef HAVE_PWRITE
9428 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
9429#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009430#ifdef HAVE_SENDFILE
9431 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
9432 posix_sendfile__doc__},
9433#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009434 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
9435 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009436#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009437 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009438#endif
9439#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009441#endif
Neal Norwitz11690112002-07-30 01:08:28 +00009442#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00009443 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00009444#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009445#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 {"major", posix_major, METH_VARARGS, posix_major__doc__},
9447 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
9448 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009449#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009450#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00009451 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009452#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009453#ifdef HAVE_TRUNCATE
9454 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
9455#endif
9456#ifdef HAVE_POSIX_FALLOCATE
9457 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
9458#endif
9459#ifdef HAVE_POSIX_FADVISE
9460 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
9461#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009462#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009463 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009464#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009465#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009466 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00009467#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009468 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009469#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00009470 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009471#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009472#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009473 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009474#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009475#ifdef HAVE_SYNC
9476 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
9477#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009478#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009479 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009480#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00009481#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009482#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00009483 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00009484#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009485#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009486 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009487#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00009488#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00009489 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009490#endif /* WIFSTOPPED */
9491#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00009492 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009493#endif /* WIFSIGNALED */
9494#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00009495 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009496#endif /* WIFEXITED */
9497#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00009498 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009499#endif /* WEXITSTATUS */
9500#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009501 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009502#endif /* WTERMSIG */
9503#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009504 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009505#endif /* WSTOPSIG */
9506#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009507#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009508 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009509#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00009510#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009511 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009512#endif
Fred Drakec9680921999-12-13 16:37:25 +00009513#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00009514 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009515#endif
9516#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009517 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009518#endif
9519#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009520 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009521#endif
9522#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009523 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009524#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009525 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009526#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009527 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00009528 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00009529 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00009530#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009531#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009533#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009534 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009535 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009536 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009537 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009539 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009540#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009541 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009542#endif
9543#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009544 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009545#endif
9546#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009547 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009548#endif
9549#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009551#endif
9552
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009553/* posix *at family of functions */
9554#ifdef HAVE_FACCESSAT
9555 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
9556#endif
9557#ifdef HAVE_FCHMODAT
9558 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
9559#endif /* HAVE_FCHMODAT */
9560#ifdef HAVE_FCHOWNAT
9561 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
9562#endif /* HAVE_FCHOWNAT */
9563#ifdef HAVE_FSTATAT
9564 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
9565#endif
9566#ifdef HAVE_FUTIMESAT
9567 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
9568#endif
9569#ifdef HAVE_LINKAT
9570 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
9571#endif /* HAVE_LINKAT */
9572#ifdef HAVE_MKDIRAT
9573 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
9574#endif
9575#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9576 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
9577#endif
9578#ifdef HAVE_OPENAT
9579 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
9580#endif
9581#ifdef HAVE_READLINKAT
9582 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
9583#endif /* HAVE_READLINKAT */
9584#ifdef HAVE_RENAMEAT
9585 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
9586#endif
9587#if HAVE_SYMLINKAT
9588 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
9589#endif /* HAVE_SYMLINKAT */
9590#ifdef HAVE_UNLINKAT
9591 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
9592#endif
9593#ifdef HAVE_UTIMENSAT
9594 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
9595#endif
9596#ifdef HAVE_MKFIFOAT
9597 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
9598#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009599 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009600};
9601
9602
Barry Warsaw4a342091996-12-19 23:50:02 +00009603static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009604ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009605{
Victor Stinner8c62be82010-05-06 00:08:46 +00009606 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009607}
9608
Guido van Rossumd48f2521997-12-05 22:19:34 +00009609#if defined(PYOS_OS2)
9610/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009611static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009612{
9613 APIRET rc;
9614 ULONG values[QSV_MAX+1];
9615 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009616 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009617
9618 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009619 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009620 Py_END_ALLOW_THREADS
9621
9622 if (rc != NO_ERROR) {
9623 os2_error(rc);
9624 return -1;
9625 }
9626
Fred Drake4d1e64b2002-04-15 19:40:07 +00009627 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9628 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9629 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9630 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9631 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9632 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9633 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009634
9635 switch (values[QSV_VERSION_MINOR]) {
9636 case 0: ver = "2.00"; break;
9637 case 10: ver = "2.10"; break;
9638 case 11: ver = "2.11"; break;
9639 case 30: ver = "3.00"; break;
9640 case 40: ver = "4.00"; break;
9641 case 50: ver = "5.00"; break;
9642 default:
Tim Peters885d4572001-11-28 20:27:42 +00009643 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00009644 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009645 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009646 ver = &tmp[0];
9647 }
9648
9649 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009650 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009651 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009652
9653 /* Add Indicator of Which Drive was Used to Boot the System */
9654 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9655 tmp[1] = ':';
9656 tmp[2] = '\0';
9657
Fred Drake4d1e64b2002-04-15 19:40:07 +00009658 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009659}
9660#endif
9661
Brian Curtin52173d42010-12-02 18:29:18 +00009662#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009663static int
Brian Curtin52173d42010-12-02 18:29:18 +00009664enable_symlink()
9665{
9666 HANDLE tok;
9667 TOKEN_PRIVILEGES tok_priv;
9668 LUID luid;
9669 int meth_idx = 0;
9670
9671 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009672 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009673
9674 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009675 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009676
9677 tok_priv.PrivilegeCount = 1;
9678 tok_priv.Privileges[0].Luid = luid;
9679 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
9680
9681 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
9682 sizeof(TOKEN_PRIVILEGES),
9683 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009684 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009685
Brian Curtin3b4499c2010-12-28 14:31:47 +00009686 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
9687 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00009688}
9689#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
9690
Barry Warsaw4a342091996-12-19 23:50:02 +00009691static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009692all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009693{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009694#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009695 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009696#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009697#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009698 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009699#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009700#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009701 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009702#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009703#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009704 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009705#endif
Fred Drakec9680921999-12-13 16:37:25 +00009706#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009707 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009708#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009709#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009710 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009711#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009712#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009713 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009714#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009715#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00009716 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009717#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009718#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00009719 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009720#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009721#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009722 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009723#endif
9724#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009725 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009726#endif
9727#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00009728 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009729#endif
9730#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00009731 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009732#endif
9733#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009734 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009735#endif
9736#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00009737 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009738#endif
9739#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009740 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009741#endif
9742#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009743 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009744#endif
9745#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009746 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009747#endif
9748#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009749 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009750#endif
9751#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009752 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009753#endif
9754#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00009755 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009756#endif
9757#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009758 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009759#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009760#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009761 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009762#endif
9763#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00009764 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009765#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009766#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009767 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009768#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009769#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009770 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009771#endif
9772#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009773 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009774#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009775#ifdef PRIO_PROCESS
9776 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
9777#endif
9778#ifdef PRIO_PGRP
9779 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
9780#endif
9781#ifdef PRIO_USER
9782 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
9783#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009784/* posix - constants for *at functions */
9785#ifdef AT_SYMLINK_NOFOLLOW
9786 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
9787#endif
9788#ifdef AT_EACCESS
9789 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
9790#endif
9791#ifdef AT_FDCWD
9792 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
9793#endif
9794#ifdef AT_REMOVEDIR
9795 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
9796#endif
9797#ifdef AT_SYMLINK_FOLLOW
9798 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
9799#endif
9800#ifdef UTIME_NOW
9801 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9802#endif
9803#ifdef UTIME_OMIT
9804 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
9805#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009806
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009807
Tim Peters5aa91602002-01-30 05:46:57 +00009808/* MS Windows */
9809#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009810 /* Don't inherit in child processes. */
9811 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009812#endif
9813#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00009814 /* Optimize for short life (keep in memory). */
9815 /* MS forgot to define this one with a non-underscore form too. */
9816 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009817#endif
9818#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009819 /* Automatically delete when last handle is closed. */
9820 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009821#endif
9822#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00009823 /* Optimize for random access. */
9824 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009825#endif
9826#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009827 /* Optimize for sequential access. */
9828 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009829#endif
9830
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009831/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009832#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009833 /* Send a SIGIO signal whenever input or output
9834 becomes available on file descriptor */
9835 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009836#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009837#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009838 /* Direct disk access. */
9839 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009840#endif
9841#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00009842 /* Must be a directory. */
9843 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009844#endif
9845#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00009846 /* Do not follow links. */
9847 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009848#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009849#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009850 /* Do not update the access time. */
9851 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009852#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009853
Victor Stinner8c62be82010-05-06 00:08:46 +00009854 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009855#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009856 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009857#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009858#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009859 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009860#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009861#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009862 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009863#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009864#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009865 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009866#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009867#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00009868 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009869#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009870#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00009871 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009872#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009873#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009874 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009875#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009876#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00009877 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009878#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009879#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009880 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009881#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009882#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009883 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009884#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009885#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009886 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009887#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009888#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00009889 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009890#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009891#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00009892 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009893#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009894#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00009895 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009896#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009897#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009898 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009899#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009900#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009901 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009902#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009903#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00009904 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00009905#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009906
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009907 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009908#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009909 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009910#endif /* ST_RDONLY */
9911#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00009912 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00009913#endif /* ST_NOSUID */
9914
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009915 /* FreeBSD sendfile() constants */
9916#ifdef SF_NODISKIO
9917 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
9918#endif
9919#ifdef SF_MNOWAIT
9920 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
9921#endif
9922#ifdef SF_SYNC
9923 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
9924#endif
9925
Ross Lagerwall7807c352011-03-17 20:20:30 +02009926 /* constants for posix_fadvise */
9927#ifdef POSIX_FADV_NORMAL
9928 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
9929#endif
9930#ifdef POSIX_FADV_SEQUENTIAL
9931 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
9932#endif
9933#ifdef POSIX_FADV_RANDOM
9934 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
9935#endif
9936#ifdef POSIX_FADV_NOREUSE
9937 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
9938#endif
9939#ifdef POSIX_FADV_WILLNEED
9940 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
9941#endif
9942#ifdef POSIX_FADV_DONTNEED
9943 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
9944#endif
9945
9946 /* constants for waitid */
9947#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
9948 if (ins(d, "P_PID", (long)P_PID)) return -1;
9949 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
9950 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
9951#endif
9952#ifdef WEXITED
9953 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
9954#endif
9955#ifdef WNOWAIT
9956 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
9957#endif
9958#ifdef WSTOPPED
9959 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
9960#endif
9961#ifdef CLD_EXITED
9962 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
9963#endif
9964#ifdef CLD_DUMPED
9965 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
9966#endif
9967#ifdef CLD_TRAPPED
9968 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
9969#endif
9970#ifdef CLD_CONTINUED
9971 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
9972#endif
9973
9974 /* constants for lockf */
9975#ifdef F_LOCK
9976 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
9977#endif
9978#ifdef F_TLOCK
9979 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
9980#endif
9981#ifdef F_ULOCK
9982 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
9983#endif
9984#ifdef F_TEST
9985 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
9986#endif
9987
9988 /* constants for futimens */
9989#ifdef UTIME_NOW
9990 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9991#endif
9992#ifdef UTIME_OMIT
9993 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
9994#endif
9995
Guido van Rossum246bc171999-02-01 23:54:31 +00009996#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009997#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00009998 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
9999 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10000 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10001 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10002 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10003 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10004 if (ins(d, "P_PM", (long)P_PM)) return -1;
10005 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10006 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10007 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10008 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10009 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10010 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10011 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10012 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10013 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10014 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10015 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10016 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10017 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010018#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010019 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10020 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10021 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10022 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10023 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010024#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010025#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010026
Guido van Rossumd48f2521997-12-05 22:19:34 +000010027#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010028 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010029#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010030 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010031}
10032
10033
Tim Peters5aa91602002-01-30 05:46:57 +000010034#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010035#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010036#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010037
10038#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010039#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010040#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010041
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010042#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010043#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010044#define MODNAME "posix"
10045#endif
10046
Martin v. Löwis1a214512008-06-11 05:26:20 +000010047static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010048 PyModuleDef_HEAD_INIT,
10049 MODNAME,
10050 posix__doc__,
10051 -1,
10052 posix_methods,
10053 NULL,
10054 NULL,
10055 NULL,
10056 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010057};
10058
10059
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010060PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010061INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010062{
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010064
Brian Curtin52173d42010-12-02 18:29:18 +000010065#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010066 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010067#endif
10068
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 m = PyModule_Create(&posixmodule);
10070 if (m == NULL)
10071 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010072
Victor Stinner8c62be82010-05-06 00:08:46 +000010073 /* Initialize environ dictionary */
10074 v = convertenviron();
10075 Py_XINCREF(v);
10076 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10077 return NULL;
10078 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010079
Victor Stinner8c62be82010-05-06 00:08:46 +000010080 if (all_ins(m))
10081 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010082
Victor Stinner8c62be82010-05-06 00:08:46 +000010083 if (setup_confname_tables(m))
10084 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010085
Victor Stinner8c62be82010-05-06 00:08:46 +000010086 Py_INCREF(PyExc_OSError);
10087 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010088
Guido van Rossumb3d39562000-01-31 18:41:26 +000010089#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010090 if (posix_putenv_garbage == NULL)
10091 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010092#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010093
Victor Stinner8c62be82010-05-06 00:08:46 +000010094 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010095#if defined(HAVE_WAITID) && !defined(__APPLE__)
10096 waitid_result_desc.name = MODNAME ".waitid_result";
10097 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10098#endif
10099
Victor Stinner8c62be82010-05-06 00:08:46 +000010100 stat_result_desc.name = MODNAME ".stat_result";
10101 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10102 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10103 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10104 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10105 structseq_new = StatResultType.tp_new;
10106 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010107
Victor Stinner8c62be82010-05-06 00:08:46 +000010108 statvfs_result_desc.name = MODNAME ".statvfs_result";
10109 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010110#ifdef NEED_TICKS_PER_SECOND
10111# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010112 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010113# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010114 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010115# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010116 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010117# endif
10118#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010119 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010120#if defined(HAVE_WAITID) && !defined(__APPLE__)
10121 Py_INCREF((PyObject*) &WaitidResultType);
10122 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10123#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010124 Py_INCREF((PyObject*) &StatResultType);
10125 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10126 Py_INCREF((PyObject*) &StatVFSResultType);
10127 PyModule_AddObject(m, "statvfs_result",
10128 (PyObject*) &StatVFSResultType);
10129 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010130
10131#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010132 /*
10133 * Step 2 of weak-linking support on Mac OS X.
10134 *
10135 * The code below removes functions that are not available on the
10136 * currently active platform.
10137 *
10138 * This block allow one to use a python binary that was build on
10139 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10140 * OSX 10.4.
10141 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010142#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010143 if (fstatvfs == NULL) {
10144 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10145 return NULL;
10146 }
10147 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010148#endif /* HAVE_FSTATVFS */
10149
10150#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010151 if (statvfs == NULL) {
10152 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10153 return NULL;
10154 }
10155 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010156#endif /* HAVE_STATVFS */
10157
10158# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010159 if (lchown == NULL) {
10160 if (PyObject_DelAttrString(m, "lchown") == -1) {
10161 return NULL;
10162 }
10163 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010164#endif /* HAVE_LCHOWN */
10165
10166
10167#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000010168 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010169
Guido van Rossumb6775db1994-08-01 11:34:53 +000010170}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010171
10172#ifdef __cplusplus
10173}
10174#endif