blob: ba80f5742d995fcddaf04ec86a90dcd0c72d1f0c [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)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020033# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020049#error "PEP 11: OS/2 is now unsupported, code will be removed in Python 3.4"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000050#define INCL_DOS
51#define INCL_DOSERRORS
52#define INCL_DOSPROCESS
53#define INCL_NOPMAPI
54#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000055#if defined(PYCC_GCC)
56#include <ctype.h>
57#include <io.h>
58#include <stdio.h>
59#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000060#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000061#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000062#endif
63
Ross Lagerwall4d076da2011-03-18 06:56:53 +020064#ifdef HAVE_SYS_UIO_H
65#include <sys/uio.h>
66#endif
67
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000069#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070#endif /* HAVE_SYS_TYPES_H */
71
72#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000073#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000075
Guido van Rossum36bc6801995-06-14 22:54:23 +000076#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000077#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000078#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000079
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000081#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000082#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000083
Guido van Rossumb6775db1994-08-01 11:34:53 +000084#ifdef HAVE_FCNTL_H
85#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000086#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000087
Guido van Rossuma6535fd2001-10-18 19:44:10 +000088#ifdef HAVE_GRP_H
89#include <grp.h>
90#endif
91
Barry Warsaw5676bd12003-01-07 20:57:09 +000092#ifdef HAVE_SYSEXITS_H
93#include <sysexits.h>
94#endif /* HAVE_SYSEXITS_H */
95
Anthony Baxter8a560de2004-10-13 15:30:56 +000096#ifdef HAVE_SYS_LOADAVG_H
97#include <sys/loadavg.h>
98#endif
99
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000100#ifdef HAVE_LANGINFO_H
101#include <langinfo.h>
102#endif
103
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000104#ifdef HAVE_SYS_SENDFILE_H
105#include <sys/sendfile.h>
106#endif
107
108#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
109#ifdef HAVE_SYS_SOCKET_H
110#include <sys/socket.h>
111#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000112#endif
113
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000115/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000116#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000117#include <process.h>
118#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000119#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120#define HAVE_GETCWD 1
121#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000122#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000123#if defined(__OS2__)
124#define HAVE_EXECV 1
125#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000126#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#include <process.h>
128#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000129#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000130#define HAVE_EXECV 1
131#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#define HAVE_OPENDIR 1
133#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000134#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135#define HAVE_WAIT 1
136#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000137#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000138#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000139#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000140#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000141#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_EXECV 1
143#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000144#define HAVE_SYSTEM 1
145#define HAVE_CWAIT 1
146#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000147#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000148#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000149#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
150/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152/* Unix functions that the configure script doesn't check for */
153#define HAVE_EXECV 1
154#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000156#define HAVE_FORK1 1
157#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000158#define HAVE_GETCWD 1
159#define HAVE_GETEGID 1
160#define HAVE_GETEUID 1
161#define HAVE_GETGID 1
162#define HAVE_GETPPID 1
163#define HAVE_GETUID 1
164#define HAVE_KILL 1
165#define HAVE_OPENDIR 1
166#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000167#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000168#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000169#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000170#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000171#endif /* _MSC_VER */
172#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000173#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000174#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000175
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000177
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000178#if defined(__sgi)&&_COMPILER_VERSION>=700
179/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
180 (default) */
181extern char *ctermid_r(char *);
182#endif
183
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000184#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000185#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000186extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000187#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000188#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000189extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000191extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000193#endif
194#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chdir(char *);
196extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000197#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000198extern int chdir(const char *);
199extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000200#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000201#ifdef __BORLANDC__
202extern int chmod(const char *, int);
203#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000205#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000206/*#ifdef HAVE_FCHMOD
207extern int fchmod(int, mode_t);
208#endif*/
209/*#ifdef HAVE_LCHMOD
210extern int lchmod(const char *, mode_t);
211#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int chown(const char *, uid_t, gid_t);
213extern char *getcwd(char *, int);
214extern char *strerror(int);
215extern int link(const char *, const char *);
216extern int rename(const char *, const char *);
217extern int stat(const char *, struct stat *);
218extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000220extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000221#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000223extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000226
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000227#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#ifdef HAVE_UTIME_H
230#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000233#ifdef HAVE_SYS_UTIME_H
234#include <sys/utime.h>
235#define HAVE_UTIME_H /* pretend we do for the rest of this file */
236#endif /* HAVE_SYS_UTIME_H */
237
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238#ifdef HAVE_SYS_TIMES_H
239#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000240#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241
242#ifdef HAVE_SYS_PARAM_H
243#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000244#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245
246#ifdef HAVE_SYS_UTSNAME_H
247#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000248#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#define NAMLEN(dirent) strlen((dirent)->d_name)
253#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000254#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#include <direct.h>
256#define NAMLEN(dirent) strlen((dirent)->d_name)
257#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000259#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000260#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000261#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000263#endif
264#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000266#endif
267#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000269#endif
270#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000272#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000273#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275#endif
276#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000278#endif
279#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000281#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000282#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000283#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000284#endif
285#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000286#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000287#endif
288#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000289#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000290#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000291#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000292#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000294#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000295#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000296#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
297#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000298static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000299#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000300#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301
Guido van Rossumd48f2521997-12-05 22:19:34 +0000302#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000304#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305
Tim Petersbc2e10e2002-03-03 23:17:02 +0000306#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000307#if defined(PATH_MAX) && PATH_MAX > 1024
308#define MAXPATHLEN PATH_MAX
309#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000310#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000311#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#endif /* MAXPATHLEN */
313
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000314#ifdef UNION_WAIT
315/* Emulate some macros on systems that have a union instead of macros */
316
317#ifndef WIFEXITED
318#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
319#endif
320
321#ifndef WEXITSTATUS
322#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
323#endif
324
325#ifndef WTERMSIG
326#define WTERMSIG(u_wait) ((u_wait).w_termsig)
327#endif
328
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000329#define WAIT_TYPE union wait
330#define WAIT_STATUS_INT(s) (s.w_status)
331
332#else /* !UNION_WAIT */
333#define WAIT_TYPE int
334#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000335#endif /* UNION_WAIT */
336
Greg Wardb48bc172000-03-01 21:51:56 +0000337/* Don't use the "_r" form if we don't need it (also, won't have a
338 prototype for it, at least on Solaris -- maybe others as well?). */
339#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
340#define USE_CTERMID_R
341#endif
342
Fred Drake699f3522000-06-29 21:12:41 +0000343/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000344#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000345#undef FSTAT
346#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000347#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000348# define STAT win32_stat
349# define FSTAT win32_fstat
350# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000351#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000352# define STAT stat
353# define FSTAT fstat
354# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000355#endif
356
Tim Peters11b23062003-04-23 02:39:17 +0000357#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000358#include <sys/mkdev.h>
359#else
360#if defined(MAJOR_IN_SYSMACROS)
361#include <sys/sysmacros.h>
362#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000363#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
364#include <sys/mkdev.h>
365#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000366#endif
Fred Drake699f3522000-06-29 21:12:41 +0000367
Georg Brandla391b112011-02-25 15:23:18 +0000368static int
369_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000370{
371#if !defined(HAVE_LARGEFILE_SUPPORT)
372 *((off_t*)addr) = PyLong_AsLong(arg);
373#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000374 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000375#endif
376 if (PyErr_Occurred())
377 return 0;
378 return 1;
379}
380
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000381#if defined _MSC_VER && _MSC_VER >= 1400
382/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
383 * valid and throw an assertion if it isn't.
384 * Normally, an invalid fd is likely to be a C program error and therefore
385 * an assertion can be useful, but it does contradict the POSIX standard
386 * which for write(2) states:
387 * "Otherwise, -1 shall be returned and errno set to indicate the error."
388 * "[EBADF] The fildes argument is not a valid file descriptor open for
389 * writing."
390 * Furthermore, python allows the user to enter any old integer
391 * as a fd and should merely raise a python exception on error.
392 * The Microsoft CRT doesn't provide an official way to check for the
393 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000394 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000395 * internal structures involved.
396 * The structures below must be updated for each version of visual studio
397 * according to the file internal.h in the CRT source, until MS comes
398 * up with a less hacky way to do this.
399 * (all of this is to avoid globally modifying the CRT behaviour using
400 * _set_invalid_parameter_handler() and _CrtSetReportMode())
401 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000402/* The actual size of the structure is determined at runtime.
403 * Only the first items must be present.
404 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000405typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000406 intptr_t osfhnd;
407 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000408} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000409
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000410extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000411#define IOINFO_L2E 5
412#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
413#define IOINFO_ARRAYS 64
414#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
415#define FOPEN 0x01
416#define _NO_CONSOLE_FILENO (intptr_t)-2
417
418/* This function emulates what the windows CRT does to validate file handles */
419int
420_PyVerify_fd(int fd)
421{
Victor Stinner8c62be82010-05-06 00:08:46 +0000422 const int i1 = fd >> IOINFO_L2E;
423 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000424
Antoine Pitrou22e41552010-08-15 18:07:50 +0000425 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000426
Victor Stinner8c62be82010-05-06 00:08:46 +0000427 /* Determine the actual size of the ioinfo structure,
428 * as used by the CRT loaded in memory
429 */
430 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
431 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
432 }
433 if (sizeof_ioinfo == 0) {
434 /* This should not happen... */
435 goto fail;
436 }
437
438 /* See that it isn't a special CLEAR fileno */
439 if (fd != _NO_CONSOLE_FILENO) {
440 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
441 * we check pointer validity and other info
442 */
443 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
444 /* finally, check that the file is open */
445 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
446 if (info->osfile & FOPEN) {
447 return 1;
448 }
449 }
450 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000451 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000452 errno = EBADF;
453 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000454}
455
456/* the special case of checking dup2. The target fd must be in a sensible range */
457static int
458_PyVerify_fd_dup2(int fd1, int fd2)
459{
Victor Stinner8c62be82010-05-06 00:08:46 +0000460 if (!_PyVerify_fd(fd1))
461 return 0;
462 if (fd2 == _NO_CONSOLE_FILENO)
463 return 0;
464 if ((unsigned)fd2 < _NHANDLE_)
465 return 1;
466 else
467 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000468}
469#else
470/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
471#define _PyVerify_fd_dup2(A, B) (1)
472#endif
473
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000474#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000475/* The following structure was copied from
476 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
477 include doesn't seem to be present in the Windows SDK (at least as included
478 with Visual Studio Express). */
479typedef struct _REPARSE_DATA_BUFFER {
480 ULONG ReparseTag;
481 USHORT ReparseDataLength;
482 USHORT Reserved;
483 union {
484 struct {
485 USHORT SubstituteNameOffset;
486 USHORT SubstituteNameLength;
487 USHORT PrintNameOffset;
488 USHORT PrintNameLength;
489 ULONG Flags;
490 WCHAR PathBuffer[1];
491 } SymbolicLinkReparseBuffer;
492
493 struct {
494 USHORT SubstituteNameOffset;
495 USHORT SubstituteNameLength;
496 USHORT PrintNameOffset;
497 USHORT PrintNameLength;
498 WCHAR PathBuffer[1];
499 } MountPointReparseBuffer;
500
501 struct {
502 UCHAR DataBuffer[1];
503 } GenericReparseBuffer;
504 };
505} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
506
507#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
508 GenericReparseBuffer)
509#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
510
511static int
Brian Curtind25aef52011-06-13 15:16:04 -0500512win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000513{
514 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
515 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
516 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000517
518 if (0 == DeviceIoControl(
519 reparse_point_handle,
520 FSCTL_GET_REPARSE_POINT,
521 NULL, 0, /* in buffer */
522 target_buffer, sizeof(target_buffer),
523 &n_bytes_returned,
524 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500525 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000526
527 if (reparse_tag)
528 *reparse_tag = rdb->ReparseTag;
529
Brian Curtind25aef52011-06-13 15:16:04 -0500530 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000531}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000532#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000533
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000535#ifdef WITH_NEXT_FRAMEWORK
536/* On Darwin/MacOSX a shared library or framework has no access to
537** environ directly, we must obtain it with _NSGetEnviron().
538*/
539#include <crt_externs.h>
540static char **environ;
541#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000543#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544
Barry Warsaw53699e91996-12-10 23:23:01 +0000545static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000546convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547{
Victor Stinner8c62be82010-05-06 00:08:46 +0000548 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000549#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000550 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000551#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000553#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000554#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000555 APIRET rc;
556 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
557#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000558
Victor Stinner8c62be82010-05-06 00:08:46 +0000559 d = PyDict_New();
560 if (d == NULL)
561 return NULL;
562#ifdef WITH_NEXT_FRAMEWORK
563 if (environ == NULL)
564 environ = *_NSGetEnviron();
565#endif
566#ifdef MS_WINDOWS
567 /* _wenviron must be initialized in this way if the program is started
568 through main() instead of wmain(). */
569 _wgetenv(L"");
570 if (_wenviron == NULL)
571 return d;
572 /* This part ignores errors */
573 for (e = _wenviron; *e != NULL; e++) {
574 PyObject *k;
575 PyObject *v;
576 wchar_t *p = wcschr(*e, L'=');
577 if (p == NULL)
578 continue;
579 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
580 if (k == NULL) {
581 PyErr_Clear();
582 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000583 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000584 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
585 if (v == NULL) {
586 PyErr_Clear();
587 Py_DECREF(k);
588 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000589 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000590 if (PyDict_GetItem(d, k) == NULL) {
591 if (PyDict_SetItem(d, k, v) != 0)
592 PyErr_Clear();
593 }
594 Py_DECREF(k);
595 Py_DECREF(v);
596 }
597#else
598 if (environ == NULL)
599 return d;
600 /* This part ignores errors */
601 for (e = environ; *e != NULL; e++) {
602 PyObject *k;
603 PyObject *v;
604 char *p = strchr(*e, '=');
605 if (p == NULL)
606 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000607 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000608 if (k == NULL) {
609 PyErr_Clear();
610 continue;
611 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000612 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000613 if (v == NULL) {
614 PyErr_Clear();
615 Py_DECREF(k);
616 continue;
617 }
618 if (PyDict_GetItem(d, k) == NULL) {
619 if (PyDict_SetItem(d, k, v) != 0)
620 PyErr_Clear();
621 }
622 Py_DECREF(k);
623 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000624 }
625#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000626#if defined(PYOS_OS2)
627 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
628 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
629 PyObject *v = PyBytes_FromString(buffer);
630 PyDict_SetItemString(d, "BEGINLIBPATH", v);
631 Py_DECREF(v);
632 }
633 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
634 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
635 PyObject *v = PyBytes_FromString(buffer);
636 PyDict_SetItemString(d, "ENDLIBPATH", v);
637 Py_DECREF(v);
638 }
639#endif
640 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641}
642
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643/* Set a POSIX-specific error from errno, and return NULL */
644
Barry Warsawd58d7641998-07-23 16:14:40 +0000645static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000646posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000647{
Victor Stinner8c62be82010-05-06 00:08:46 +0000648 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649}
Barry Warsawd58d7641998-07-23 16:14:40 +0000650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000651posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000652{
Victor Stinner8c62be82010-05-06 00:08:46 +0000653 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000654}
655
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000656
Mark Hammondef8b6542001-05-13 08:04:26 +0000657static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000658posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000659{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000660 PyObject *name_str, *rc;
661 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
662 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000663 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000664 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
665 name_str);
666 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000667 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000668}
669
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000670#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000671static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000672win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000673{
Victor Stinner8c62be82010-05-06 00:08:46 +0000674 /* XXX We should pass the function name along in the future.
675 (winreg.c also wants to pass the function name.)
676 This would however require an additional param to the
677 Windows error object, which is non-trivial.
678 */
679 errno = GetLastError();
680 if (filename)
681 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
682 else
683 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000684}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000685
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686static PyObject *
687win32_error_unicode(char* function, Py_UNICODE* filename)
688{
Victor Stinner8c62be82010-05-06 00:08:46 +0000689 /* XXX - see win32_error for comments on 'function' */
690 errno = GetLastError();
691 if (filename)
692 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
693 else
694 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000695}
696
Thomas Wouters477c8d52006-05-27 19:21:47 +0000697static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000698convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699{
Victor Stinner8c62be82010-05-06 00:08:46 +0000700 if (PyUnicode_CheckExact(*param))
701 Py_INCREF(*param);
702 else if (PyUnicode_Check(*param))
703 /* For a Unicode subtype that's not a Unicode object,
704 return a true Unicode object with the same data. */
705 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
706 PyUnicode_GET_SIZE(*param));
707 else
708 *param = PyUnicode_FromEncodedObject(*param,
709 Py_FileSystemDefaultEncoding,
710 "strict");
711 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000712}
713
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000714#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715
Guido van Rossumd48f2521997-12-05 22:19:34 +0000716#if defined(PYOS_OS2)
717/**********************************************************************
718 * Helper Function to Trim and Format OS/2 Messages
719 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000720static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000721os2_formatmsg(char *msgbuf, int msglen, char *reason)
722{
723 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
724
725 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
726 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
727
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000728 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000729 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
730 }
731
732 /* Add Optional Reason Text */
733 if (reason) {
734 strcat(msgbuf, " : ");
735 strcat(msgbuf, reason);
736 }
737}
738
739/**********************************************************************
740 * Decode an OS/2 Operating System Error Code
741 *
742 * A convenience function to lookup an OS/2 error code and return a
743 * text message we can use to raise a Python exception.
744 *
745 * Notes:
746 * The messages for errors returned from the OS/2 kernel reside in
747 * the file OSO001.MSG in the \OS2 directory hierarchy.
748 *
749 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000750static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000751os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
752{
753 APIRET rc;
754 ULONG msglen;
755
756 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
757 Py_BEGIN_ALLOW_THREADS
758 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
759 errorcode, "oso001.msg", &msglen);
760 Py_END_ALLOW_THREADS
761
762 if (rc == NO_ERROR)
763 os2_formatmsg(msgbuf, msglen, reason);
764 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000765 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000766 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000767
768 return msgbuf;
769}
770
771/* Set an OS/2-specific error and return NULL. OS/2 kernel
772 errors are not in a global variable e.g. 'errno' nor are
773 they congruent with posix error numbers. */
774
Victor Stinner8c62be82010-05-06 00:08:46 +0000775static PyObject *
776os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000777{
778 char text[1024];
779 PyObject *v;
780
781 os2_strerror(text, sizeof(text), code, "");
782
783 v = Py_BuildValue("(is)", code, text);
784 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000785 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000786 Py_DECREF(v);
787 }
788 return NULL; /* Signal to Python that an Exception is Pending */
789}
790
791#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792
793/* POSIX generic methods */
794
Barry Warsaw53699e91996-12-10 23:23:01 +0000795static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000796posix_fildes(PyObject *fdobj, int (*func)(int))
797{
Victor Stinner8c62be82010-05-06 00:08:46 +0000798 int fd;
799 int res;
800 fd = PyObject_AsFileDescriptor(fdobj);
801 if (fd < 0)
802 return NULL;
803 if (!_PyVerify_fd(fd))
804 return posix_error();
805 Py_BEGIN_ALLOW_THREADS
806 res = (*func)(fd);
807 Py_END_ALLOW_THREADS
808 if (res < 0)
809 return posix_error();
810 Py_INCREF(Py_None);
811 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000812}
Guido van Rossum21142a01999-01-08 21:05:37 +0000813
814static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000815posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816{
Victor Stinner8c62be82010-05-06 00:08:46 +0000817 PyObject *opath1 = NULL;
818 char *path1;
819 int res;
820 if (!PyArg_ParseTuple(args, format,
821 PyUnicode_FSConverter, &opath1))
822 return NULL;
823 path1 = PyBytes_AsString(opath1);
824 Py_BEGIN_ALLOW_THREADS
825 res = (*func)(path1);
826 Py_END_ALLOW_THREADS
827 if (res < 0)
828 return posix_error_with_allocated_filename(opath1);
829 Py_DECREF(opath1);
830 Py_INCREF(Py_None);
831 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832}
833
Barry Warsaw53699e91996-12-10 23:23:01 +0000834static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000835posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000836 char *format,
837 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838{
Victor Stinner8c62be82010-05-06 00:08:46 +0000839 PyObject *opath1 = NULL, *opath2 = NULL;
840 char *path1, *path2;
841 int res;
842 if (!PyArg_ParseTuple(args, format,
843 PyUnicode_FSConverter, &opath1,
844 PyUnicode_FSConverter, &opath2)) {
845 return NULL;
846 }
847 path1 = PyBytes_AsString(opath1);
848 path2 = PyBytes_AsString(opath2);
849 Py_BEGIN_ALLOW_THREADS
850 res = (*func)(path1, path2);
851 Py_END_ALLOW_THREADS
852 Py_DECREF(opath1);
853 Py_DECREF(opath2);
854 if (res != 0)
855 /* XXX how to report both path1 and path2??? */
856 return posix_error();
857 Py_INCREF(Py_None);
858 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859}
860
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000861#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000863win32_1str(PyObject* args, char* func,
864 char* format, BOOL (__stdcall *funcA)(LPCSTR),
865 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866{
Victor Stinner8c62be82010-05-06 00:08:46 +0000867 PyObject *uni;
868 char *ansi;
869 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000870
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 if (!PyArg_ParseTuple(args, wformat, &uni))
872 PyErr_Clear();
873 else {
874 Py_BEGIN_ALLOW_THREADS
875 result = funcW(PyUnicode_AsUnicode(uni));
876 Py_END_ALLOW_THREADS
877 if (!result)
878 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
879 Py_INCREF(Py_None);
880 return Py_None;
881 }
882 if (!PyArg_ParseTuple(args, format, &ansi))
883 return NULL;
884 Py_BEGIN_ALLOW_THREADS
885 result = funcA(ansi);
886 Py_END_ALLOW_THREADS
887 if (!result)
888 return win32_error(func, ansi);
889 Py_INCREF(Py_None);
890 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000891
892}
893
894/* This is a reimplementation of the C library's chdir function,
895 but one that produces Win32 errors instead of DOS error codes.
896 chdir is essentially a wrapper around SetCurrentDirectory; however,
897 it also needs to set "magic" environment variables indicating
898 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000899static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000900win32_chdir(LPCSTR path)
901{
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 char new_path[MAX_PATH+1];
903 int result;
904 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 if(!SetCurrentDirectoryA(path))
907 return FALSE;
908 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
909 if (!result)
910 return FALSE;
911 /* In the ANSI API, there should not be any paths longer
912 than MAX_PATH. */
913 assert(result <= MAX_PATH+1);
914 if (strncmp(new_path, "\\\\", 2) == 0 ||
915 strncmp(new_path, "//", 2) == 0)
916 /* UNC path, nothing to do. */
917 return TRUE;
918 env[1] = new_path[0];
919 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920}
921
922/* The Unicode version differs from the ANSI version
923 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000924static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000925win32_wchdir(LPCWSTR path)
926{
Victor Stinner8c62be82010-05-06 00:08:46 +0000927 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
928 int result;
929 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000930
Victor Stinner8c62be82010-05-06 00:08:46 +0000931 if(!SetCurrentDirectoryW(path))
932 return FALSE;
933 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
934 if (!result)
935 return FALSE;
936 if (result > MAX_PATH+1) {
937 new_path = malloc(result * sizeof(wchar_t));
938 if (!new_path) {
939 SetLastError(ERROR_OUTOFMEMORY);
940 return FALSE;
941 }
942 result = GetCurrentDirectoryW(result, new_path);
943 if (!result) {
944 free(new_path);
945 return FALSE;
946 }
947 }
948 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
949 wcsncmp(new_path, L"//", 2) == 0)
950 /* UNC path, nothing to do. */
951 return TRUE;
952 env[1] = new_path[0];
953 result = SetEnvironmentVariableW(env, new_path);
954 if (new_path != _new_path)
955 free(new_path);
956 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957}
958#endif
959
Martin v. Löwis14694662006-02-03 12:54:16 +0000960#ifdef MS_WINDOWS
961/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
962 - time stamps are restricted to second resolution
963 - file modification times suffer from forth-and-back conversions between
964 UTC and local time
965 Therefore, we implement our own stat, based on the Win32 API directly.
966*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000967#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000968
969struct win32_stat{
970 int st_dev;
971 __int64 st_ino;
972 unsigned short st_mode;
973 int st_nlink;
974 int st_uid;
975 int st_gid;
976 int st_rdev;
977 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000978 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000979 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000980 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000981 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000982 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000983 int st_ctime_nsec;
984};
985
986static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
987
988static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000989FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000990{
Victor Stinner8c62be82010-05-06 00:08:46 +0000991 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
992 /* Cannot simply cast and dereference in_ptr,
993 since it might not be aligned properly */
994 __int64 in;
995 memcpy(&in, in_ptr, sizeof(in));
996 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000997 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +0000998}
999
Thomas Wouters477c8d52006-05-27 19:21:47 +00001000static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001001time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001002{
Victor Stinner8c62be82010-05-06 00:08:46 +00001003 /* XXX endianness */
1004 __int64 out;
1005 out = time_in + secs_between_epochs;
1006 out = out * 10000000 + nsec_in / 100;
1007 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001008}
1009
Martin v. Löwis14694662006-02-03 12:54:16 +00001010/* Below, we *know* that ugo+r is 0444 */
1011#if _S_IREAD != 0400
1012#error Unsupported C library
1013#endif
1014static int
1015attributes_to_mode(DWORD attr)
1016{
Victor Stinner8c62be82010-05-06 00:08:46 +00001017 int m = 0;
1018 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1019 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1020 else
1021 m |= _S_IFREG;
1022 if (attr & FILE_ATTRIBUTE_READONLY)
1023 m |= 0444;
1024 else
1025 m |= 0666;
1026 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001027}
1028
1029static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001030attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001031{
Victor Stinner8c62be82010-05-06 00:08:46 +00001032 memset(result, 0, sizeof(*result));
1033 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1034 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1035 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1036 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1037 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001038 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001039 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001040 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1041 /* first clear the S_IFMT bits */
1042 result->st_mode ^= (result->st_mode & 0170000);
1043 /* now set the bits that make this a symlink */
1044 result->st_mode |= 0120000;
1045 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001046
Victor Stinner8c62be82010-05-06 00:08:46 +00001047 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001048}
1049
Guido van Rossumd8faa362007-04-27 19:54:29 +00001050static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001051attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001052{
Victor Stinner8c62be82010-05-06 00:08:46 +00001053 HANDLE hFindFile;
1054 WIN32_FIND_DATAA FileData;
1055 hFindFile = FindFirstFileA(pszFile, &FileData);
1056 if (hFindFile == INVALID_HANDLE_VALUE)
1057 return FALSE;
1058 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001059 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001060 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001061 info->dwFileAttributes = FileData.dwFileAttributes;
1062 info->ftCreationTime = FileData.ftCreationTime;
1063 info->ftLastAccessTime = FileData.ftLastAccessTime;
1064 info->ftLastWriteTime = FileData.ftLastWriteTime;
1065 info->nFileSizeHigh = FileData.nFileSizeHigh;
1066 info->nFileSizeLow = FileData.nFileSizeLow;
1067/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001068 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1069 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001070 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001071}
1072
1073static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001074attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001075{
Victor Stinner8c62be82010-05-06 00:08:46 +00001076 HANDLE hFindFile;
1077 WIN32_FIND_DATAW FileData;
1078 hFindFile = FindFirstFileW(pszFile, &FileData);
1079 if (hFindFile == INVALID_HANDLE_VALUE)
1080 return FALSE;
1081 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001082 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001083 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001084 info->dwFileAttributes = FileData.dwFileAttributes;
1085 info->ftCreationTime = FileData.ftCreationTime;
1086 info->ftLastAccessTime = FileData.ftLastAccessTime;
1087 info->ftLastWriteTime = FileData.ftLastWriteTime;
1088 info->nFileSizeHigh = FileData.nFileSizeHigh;
1089 info->nFileSizeLow = FileData.nFileSizeLow;
1090/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001091 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1092 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001093 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001094}
1095
Brian Curtind25aef52011-06-13 15:16:04 -05001096/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1097static int has_GetFinalPathNameByHandle = 0;
1098static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1099 DWORD);
1100static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1101 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001102static int
Brian Curtind25aef52011-06-13 15:16:04 -05001103check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001104{
Brian Curtind25aef52011-06-13 15:16:04 -05001105 HINSTANCE hKernel32;
1106 /* only recheck */
1107 if (!has_GetFinalPathNameByHandle)
1108 {
1109 hKernel32 = GetModuleHandle("KERNEL32");
1110 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1111 "GetFinalPathNameByHandleA");
1112 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1113 "GetFinalPathNameByHandleW");
1114 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1115 Py_GetFinalPathNameByHandleW;
1116 }
1117 return has_GetFinalPathNameByHandle;
1118}
1119
1120static BOOL
1121get_target_path(HANDLE hdl, wchar_t **target_path)
1122{
1123 int buf_size, result_length;
1124 wchar_t *buf;
1125
1126 /* We have a good handle to the target, use it to determine
1127 the target path name (then we'll call lstat on it). */
1128 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1129 VOLUME_NAME_DOS);
1130 if(!buf_size)
1131 return FALSE;
1132
1133 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001134 if (!buf) {
1135 SetLastError(ERROR_OUTOFMEMORY);
1136 return FALSE;
1137 }
1138
Brian Curtind25aef52011-06-13 15:16:04 -05001139 result_length = Py_GetFinalPathNameByHandleW(hdl,
1140 buf, buf_size, VOLUME_NAME_DOS);
1141
1142 if(!result_length) {
1143 free(buf);
1144 return FALSE;
1145 }
1146
1147 if(!CloseHandle(hdl)) {
1148 free(buf);
1149 return FALSE;
1150 }
1151
1152 buf[result_length] = 0;
1153
1154 *target_path = buf;
1155 return TRUE;
1156}
1157
1158static int
1159win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1160 BOOL traverse);
1161static int
1162win32_xstat_impl(const char *path, struct win32_stat *result,
1163 BOOL traverse)
1164{
Victor Stinner26de69d2011-06-17 15:15:38 +02001165 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001166 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001167 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001168 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001169 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001170 const char *dot;
1171
Brian Curtind25aef52011-06-13 15:16:04 -05001172 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001173 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1174 traverse reparse point. */
1175 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001176 }
1177
Brian Curtinf5e76d02010-11-24 13:14:05 +00001178 hFile = CreateFileA(
1179 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001180 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001181 0, /* share mode */
1182 NULL, /* security attributes */
1183 OPEN_EXISTING,
1184 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001185 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1186 Because of this, calls like GetFinalPathNameByHandle will return
1187 the symlink path agin and not the actual final path. */
1188 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1189 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001190 NULL);
1191
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001192 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001193 /* Either the target doesn't exist, or we don't have access to
1194 get a handle to it. If the former, we need to return an error.
1195 If the latter, we can use attributes_from_dir. */
1196 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001197 return -1;
1198 /* Could not get attributes on open file. Fall back to
1199 reading the directory. */
1200 if (!attributes_from_dir(path, &info, &reparse_tag))
1201 /* Very strange. This should not fail now */
1202 return -1;
1203 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1204 if (traverse) {
1205 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001206 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001208 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001209 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001210 } else {
1211 if (!GetFileInformationByHandle(hFile, &info)) {
1212 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001213 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001214 }
1215 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001216 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1217 return -1;
1218
1219 /* Close the outer open file handle now that we're about to
1220 reopen it with different flags. */
1221 if (!CloseHandle(hFile))
1222 return -1;
1223
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001224 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001225 /* In order to call GetFinalPathNameByHandle we need to open
1226 the file without the reparse handling flag set. */
1227 hFile2 = CreateFileA(
1228 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1229 NULL, OPEN_EXISTING,
1230 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1231 NULL);
1232 if (hFile2 == INVALID_HANDLE_VALUE)
1233 return -1;
1234
1235 if (!get_target_path(hFile2, &target_path))
1236 return -1;
1237
1238 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001239 free(target_path);
1240 return code;
1241 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001242 } else
1243 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001244 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001245 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001246
1247 /* Set S_IEXEC if it is an .exe, .bat, ... */
1248 dot = strrchr(path, '.');
1249 if (dot) {
1250 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1251 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1252 result->st_mode |= 0111;
1253 }
1254 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001255}
1256
1257static int
Brian Curtind25aef52011-06-13 15:16:04 -05001258win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1259 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001260{
1261 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001262 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001263 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001264 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001265 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001266 const wchar_t *dot;
1267
Brian Curtind25aef52011-06-13 15:16:04 -05001268 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001269 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1270 traverse reparse point. */
1271 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001272 }
1273
Brian Curtinf5e76d02010-11-24 13:14:05 +00001274 hFile = CreateFileW(
1275 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001276 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001277 0, /* share mode */
1278 NULL, /* security attributes */
1279 OPEN_EXISTING,
1280 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001281 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1282 Because of this, calls like GetFinalPathNameByHandle will return
1283 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001284 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001285 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001286 NULL);
1287
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001288 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001289 /* Either the target doesn't exist, or we don't have access to
1290 get a handle to it. If the former, we need to return an error.
1291 If the latter, we can use attributes_from_dir. */
1292 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001293 return -1;
1294 /* Could not get attributes on open file. Fall back to
1295 reading the directory. */
1296 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1297 /* Very strange. This should not fail now */
1298 return -1;
1299 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1300 if (traverse) {
1301 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001302 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001303 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001304 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001305 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001306 } else {
1307 if (!GetFileInformationByHandle(hFile, &info)) {
1308 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001309 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001310 }
1311 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001312 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1313 return -1;
1314
1315 /* Close the outer open file handle now that we're about to
1316 reopen it with different flags. */
1317 if (!CloseHandle(hFile))
1318 return -1;
1319
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001320 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001321 /* In order to call GetFinalPathNameByHandle we need to open
1322 the file without the reparse handling flag set. */
1323 hFile2 = CreateFileW(
1324 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1325 NULL, OPEN_EXISTING,
1326 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1327 NULL);
1328 if (hFile2 == INVALID_HANDLE_VALUE)
1329 return -1;
1330
1331 if (!get_target_path(hFile2, &target_path))
1332 return -1;
1333
1334 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001335 free(target_path);
1336 return code;
1337 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001338 } else
1339 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001340 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001341 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001342
1343 /* Set S_IEXEC if it is an .exe, .bat, ... */
1344 dot = wcsrchr(path, '.');
1345 if (dot) {
1346 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1347 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1348 result->st_mode |= 0111;
1349 }
1350 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001351}
1352
1353static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001354win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001355{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001356 /* Protocol violation: we explicitly clear errno, instead of
1357 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001358 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001359 errno = 0;
1360 return code;
1361}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001362
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001363static int
1364win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1365{
1366 /* Protocol violation: we explicitly clear errno, instead of
1367 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001368 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001369 errno = 0;
1370 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001371}
Brian Curtind25aef52011-06-13 15:16:04 -05001372/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001373
1374 In Posix, stat automatically traverses symlinks and returns the stat
1375 structure for the target. In Windows, the equivalent GetFileAttributes by
1376 default does not traverse symlinks and instead returns attributes for
1377 the symlink.
1378
1379 Therefore, win32_lstat will get the attributes traditionally, and
1380 win32_stat will first explicitly resolve the symlink target and then will
1381 call win32_lstat on that result.
1382
Ezio Melotti4969f702011-03-15 05:59:46 +02001383 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001384
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001385static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001386win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001387{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001388 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001389}
1390
Victor Stinner8c62be82010-05-06 00:08:46 +00001391static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001392win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001393{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001395}
1396
1397static int
1398win32_stat(const char* path, struct win32_stat *result)
1399{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001400 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001401}
1402
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001403static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001404win32_stat_w(const wchar_t* path, struct win32_stat *result)
1405{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001406 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001407}
1408
1409static int
1410win32_fstat(int file_number, struct win32_stat *result)
1411{
Victor Stinner8c62be82010-05-06 00:08:46 +00001412 BY_HANDLE_FILE_INFORMATION info;
1413 HANDLE h;
1414 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001415
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001417
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 /* Protocol violation: we explicitly clear errno, instead of
1419 setting it to a POSIX error. Callers should use GetLastError. */
1420 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 if (h == INVALID_HANDLE_VALUE) {
1423 /* This is really a C library error (invalid file handle).
1424 We set the Win32 error to the closes one matching. */
1425 SetLastError(ERROR_INVALID_HANDLE);
1426 return -1;
1427 }
1428 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001429
Victor Stinner8c62be82010-05-06 00:08:46 +00001430 type = GetFileType(h);
1431 if (type == FILE_TYPE_UNKNOWN) {
1432 DWORD error = GetLastError();
1433 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001434 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001435 }
1436 /* else: valid but unknown file */
1437 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001438
Victor Stinner8c62be82010-05-06 00:08:46 +00001439 if (type != FILE_TYPE_DISK) {
1440 if (type == FILE_TYPE_CHAR)
1441 result->st_mode = _S_IFCHR;
1442 else if (type == FILE_TYPE_PIPE)
1443 result->st_mode = _S_IFIFO;
1444 return 0;
1445 }
1446
1447 if (!GetFileInformationByHandle(h, &info)) {
1448 return -1;
1449 }
1450
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001451 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001452 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001453 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1454 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001455}
1456
1457#endif /* MS_WINDOWS */
1458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001459PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001460"stat_result: Result from stat or lstat.\n\n\
1461This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001462 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001463or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1464\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001465Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1466or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001467\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001468See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001469
1470static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 {"st_mode", "protection bits"},
1472 {"st_ino", "inode"},
1473 {"st_dev", "device"},
1474 {"st_nlink", "number of hard links"},
1475 {"st_uid", "user ID of owner"},
1476 {"st_gid", "group ID of owner"},
1477 {"st_size", "total size, in bytes"},
1478 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1479 {NULL, "integer time of last access"},
1480 {NULL, "integer time of last modification"},
1481 {NULL, "integer time of last change"},
1482 {"st_atime", "time of last access"},
1483 {"st_mtime", "time of last modification"},
1484 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001485#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001486 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001487#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001488#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001489 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001490#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001491#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001493#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001494#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001495 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001496#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001497#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001498 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001499#endif
1500#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001501 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001502#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001503 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001504};
1505
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001506#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001507#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001509#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001510#endif
1511
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001512#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001513#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1514#else
1515#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1516#endif
1517
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001518#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001519#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1520#else
1521#define ST_RDEV_IDX ST_BLOCKS_IDX
1522#endif
1523
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001524#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1525#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1526#else
1527#define ST_FLAGS_IDX ST_RDEV_IDX
1528#endif
1529
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001530#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001531#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001532#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001533#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001534#endif
1535
1536#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1537#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1538#else
1539#define ST_BIRTHTIME_IDX ST_GEN_IDX
1540#endif
1541
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001542static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001543 "stat_result", /* name */
1544 stat_result__doc__, /* doc */
1545 stat_result_fields,
1546 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001547};
1548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001549PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001550"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1551This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001552 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001553or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001554\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001555See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001556
1557static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001558 {"f_bsize", },
1559 {"f_frsize", },
1560 {"f_blocks", },
1561 {"f_bfree", },
1562 {"f_bavail", },
1563 {"f_files", },
1564 {"f_ffree", },
1565 {"f_favail", },
1566 {"f_flag", },
1567 {"f_namemax",},
1568 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001569};
1570
1571static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 "statvfs_result", /* name */
1573 statvfs_result__doc__, /* doc */
1574 statvfs_result_fields,
1575 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001576};
1577
Ross Lagerwall7807c352011-03-17 20:20:30 +02001578#if defined(HAVE_WAITID) && !defined(__APPLE__)
1579PyDoc_STRVAR(waitid_result__doc__,
1580"waitid_result: Result from waitid.\n\n\
1581This object may be accessed either as a tuple of\n\
1582 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1583or via the attributes si_pid, si_uid, and so on.\n\
1584\n\
1585See os.waitid for more information.");
1586
1587static PyStructSequence_Field waitid_result_fields[] = {
1588 {"si_pid", },
1589 {"si_uid", },
1590 {"si_signo", },
1591 {"si_status", },
1592 {"si_code", },
1593 {0}
1594};
1595
1596static PyStructSequence_Desc waitid_result_desc = {
1597 "waitid_result", /* name */
1598 waitid_result__doc__, /* doc */
1599 waitid_result_fields,
1600 5
1601};
1602static PyTypeObject WaitidResultType;
1603#endif
1604
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001605static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001606static PyTypeObject StatResultType;
1607static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001608static newfunc structseq_new;
1609
1610static PyObject *
1611statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1612{
Victor Stinner8c62be82010-05-06 00:08:46 +00001613 PyStructSequence *result;
1614 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001615
Victor Stinner8c62be82010-05-06 00:08:46 +00001616 result = (PyStructSequence*)structseq_new(type, args, kwds);
1617 if (!result)
1618 return NULL;
1619 /* If we have been initialized from a tuple,
1620 st_?time might be set to None. Initialize it
1621 from the int slots. */
1622 for (i = 7; i <= 9; i++) {
1623 if (result->ob_item[i+3] == Py_None) {
1624 Py_DECREF(Py_None);
1625 Py_INCREF(result->ob_item[i]);
1626 result->ob_item[i+3] = result->ob_item[i];
1627 }
1628 }
1629 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001630}
1631
1632
1633
1634/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001635static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001636
1637PyDoc_STRVAR(stat_float_times__doc__,
1638"stat_float_times([newval]) -> oldval\n\n\
1639Determine whether os.[lf]stat represents time stamps as float objects.\n\
1640If newval is True, future calls to stat() return floats, if it is False,\n\
1641future calls return ints. \n\
1642If newval is omitted, return the current setting.\n");
1643
1644static PyObject*
1645stat_float_times(PyObject* self, PyObject *args)
1646{
Victor Stinner8c62be82010-05-06 00:08:46 +00001647 int newval = -1;
1648 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1649 return NULL;
1650 if (newval == -1)
1651 /* Return old value */
1652 return PyBool_FromLong(_stat_float_times);
1653 _stat_float_times = newval;
1654 Py_INCREF(Py_None);
1655 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001656}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001657
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001658static void
1659fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1660{
Victor Stinner8c62be82010-05-06 00:08:46 +00001661 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001662#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001663 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001664#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001665 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001666#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001667 if (!ival)
1668 return;
1669 if (_stat_float_times) {
1670 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1671 } else {
1672 fval = ival;
1673 Py_INCREF(fval);
1674 }
1675 PyStructSequence_SET_ITEM(v, index, ival);
1676 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001677}
1678
Tim Peters5aa91602002-01-30 05:46:57 +00001679/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001680 (used by posix_stat() and posix_fstat()) */
1681static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001682_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001683{
Victor Stinner8c62be82010-05-06 00:08:46 +00001684 unsigned long ansec, mnsec, cnsec;
1685 PyObject *v = PyStructSequence_New(&StatResultType);
1686 if (v == NULL)
1687 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001688
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001690#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001691 PyStructSequence_SET_ITEM(v, 1,
1692 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001693#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001695#endif
1696#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001697 PyStructSequence_SET_ITEM(v, 2,
1698 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001699#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001701#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1703 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1704 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001705#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 PyStructSequence_SET_ITEM(v, 6,
1707 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001708#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001710#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001711
Martin v. Löwis14694662006-02-03 12:54:16 +00001712#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 ansec = st->st_atim.tv_nsec;
1714 mnsec = st->st_mtim.tv_nsec;
1715 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001716#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001717 ansec = st->st_atimespec.tv_nsec;
1718 mnsec = st->st_mtimespec.tv_nsec;
1719 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001720#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001721 ansec = st->st_atime_nsec;
1722 mnsec = st->st_mtime_nsec;
1723 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001724#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001725 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001726#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001727 fill_time(v, 7, st->st_atime, ansec);
1728 fill_time(v, 8, st->st_mtime, mnsec);
1729 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001730
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001731#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1733 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001734#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001735#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1737 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001738#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001739#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001740 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1741 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001742#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001743#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001744 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1745 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001746#endif
1747#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001748 {
1749 PyObject *val;
1750 unsigned long bsec,bnsec;
1751 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001752#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001754#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001755 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001756#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001757 if (_stat_float_times) {
1758 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1759 } else {
1760 val = PyLong_FromLong((long)bsec);
1761 }
1762 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1763 val);
1764 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001765#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001766#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001767 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1768 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001769#endif
Fred Drake699f3522000-06-29 21:12:41 +00001770
Victor Stinner8c62be82010-05-06 00:08:46 +00001771 if (PyErr_Occurred()) {
1772 Py_DECREF(v);
1773 return NULL;
1774 }
Fred Drake699f3522000-06-29 21:12:41 +00001775
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001777}
1778
Barry Warsaw53699e91996-12-10 23:23:01 +00001779static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001780posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001781 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001782#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001783 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001784#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001785 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001786#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001787 char *wformat,
1788 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001789{
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 STRUCT_STAT st;
1791 PyObject *opath;
1792 char *path;
1793 int res;
1794 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001795
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001796#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001797 PyUnicodeObject *po;
1798 if (PyArg_ParseTuple(args, wformat, &po)) {
1799 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001800
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 Py_BEGIN_ALLOW_THREADS
1802 /* PyUnicode_AS_UNICODE result OK without
1803 thread lock as it is a simple dereference. */
1804 res = wstatfunc(wpath, &st);
1805 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001806
Victor Stinner8c62be82010-05-06 00:08:46 +00001807 if (res != 0)
1808 return win32_error_unicode("stat", wpath);
1809 return _pystat_fromstructstat(&st);
1810 }
1811 /* Drop the argument parsing error as narrow strings
1812 are also valid. */
1813 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001814#endif
1815
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 if (!PyArg_ParseTuple(args, format,
1817 PyUnicode_FSConverter, &opath))
1818 return NULL;
1819 path = PyBytes_AsString(opath);
1820 Py_BEGIN_ALLOW_THREADS
1821 res = (*statfunc)(path, &st);
1822 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001823
Victor Stinner8c62be82010-05-06 00:08:46 +00001824 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001825#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001826 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001827#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001828 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001829#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001830 }
1831 else
1832 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001833
Victor Stinner8c62be82010-05-06 00:08:46 +00001834 Py_DECREF(opath);
1835 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001836}
1837
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001838/* POSIX methods */
1839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001840PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001841"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001842Use the real uid/gid to test for access to a path. Note that most\n\
1843operations will use the effective uid/gid, therefore this routine can\n\
1844be used in a suid/sgid environment to test if the invoking user has the\n\
1845specified access to the path. The mode argument can be F_OK to test\n\
1846existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001847
1848static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001849posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001850{
Victor Stinner8c62be82010-05-06 00:08:46 +00001851 PyObject *opath;
1852 char *path;
1853 int mode;
1854
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001855#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001856 DWORD attr;
1857 PyUnicodeObject *po;
1858 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1859 Py_BEGIN_ALLOW_THREADS
1860 /* PyUnicode_AS_UNICODE OK without thread lock as
1861 it is a simple dereference. */
1862 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1863 Py_END_ALLOW_THREADS
1864 goto finish;
1865 }
1866 /* Drop the argument parsing error as narrow strings
1867 are also valid. */
1868 PyErr_Clear();
1869 if (!PyArg_ParseTuple(args, "O&i:access",
1870 PyUnicode_FSConverter, &opath, &mode))
1871 return NULL;
1872 path = PyBytes_AsString(opath);
1873 Py_BEGIN_ALLOW_THREADS
1874 attr = GetFileAttributesA(path);
1875 Py_END_ALLOW_THREADS
1876 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001877finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001878 if (attr == 0xFFFFFFFF)
1879 /* File does not exist, or cannot read attributes */
1880 return PyBool_FromLong(0);
1881 /* Access is possible if either write access wasn't requested, or
1882 the file isn't read-only, or if it's a directory, as there are
1883 no read-only directories on Windows. */
1884 return PyBool_FromLong(!(mode & 2)
1885 || !(attr & FILE_ATTRIBUTE_READONLY)
1886 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001887#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001888 int res;
1889 if (!PyArg_ParseTuple(args, "O&i:access",
1890 PyUnicode_FSConverter, &opath, &mode))
1891 return NULL;
1892 path = PyBytes_AsString(opath);
1893 Py_BEGIN_ALLOW_THREADS
1894 res = access(path, mode);
1895 Py_END_ALLOW_THREADS
1896 Py_DECREF(opath);
1897 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001898#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001899}
1900
Guido van Rossumd371ff11999-01-25 16:12:23 +00001901#ifndef F_OK
1902#define F_OK 0
1903#endif
1904#ifndef R_OK
1905#define R_OK 4
1906#endif
1907#ifndef W_OK
1908#define W_OK 2
1909#endif
1910#ifndef X_OK
1911#define X_OK 1
1912#endif
1913
1914#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001915PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001916"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001917Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001918
1919static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001920posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001921{
Victor Stinner8c62be82010-05-06 00:08:46 +00001922 int id;
1923 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001924
Victor Stinner8c62be82010-05-06 00:08:46 +00001925 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1926 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001927
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001928#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001929 /* file descriptor 0 only, the default input device (stdin) */
1930 if (id == 0) {
1931 ret = ttyname();
1932 }
1933 else {
1934 ret = NULL;
1935 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001936#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001937 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001938#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001939 if (ret == NULL)
1940 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001941 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001942}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001943#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001944
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001945#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001946PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001947"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001948Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001949
1950static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001951posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001952{
Victor Stinner8c62be82010-05-06 00:08:46 +00001953 char *ret;
1954 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001955
Greg Wardb48bc172000-03-01 21:51:56 +00001956#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001958#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001959 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001960#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001961 if (ret == NULL)
1962 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001963 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001964}
1965#endif
1966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001967PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001968"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001970
Barry Warsaw53699e91996-12-10 23:23:01 +00001971static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001972posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001973{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001974#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001975 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001976#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001977 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001978#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001979 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001980#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001981 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001982#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001983}
1984
Fred Drake4d1e64b2002-04-15 19:40:07 +00001985#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001986PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001987"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001988Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001989opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001990
1991static PyObject *
1992posix_fchdir(PyObject *self, PyObject *fdobj)
1993{
Victor Stinner8c62be82010-05-06 00:08:46 +00001994 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001995}
1996#endif /* HAVE_FCHDIR */
1997
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001999PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002000"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002001Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002002
Barry Warsaw53699e91996-12-10 23:23:01 +00002003static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002004posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002005{
Victor Stinner8c62be82010-05-06 00:08:46 +00002006 PyObject *opath = NULL;
2007 char *path = NULL;
2008 int i;
2009 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002010#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002011 DWORD attr;
2012 PyUnicodeObject *po;
2013 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2014 Py_BEGIN_ALLOW_THREADS
2015 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2016 if (attr != 0xFFFFFFFF) {
2017 if (i & _S_IWRITE)
2018 attr &= ~FILE_ATTRIBUTE_READONLY;
2019 else
2020 attr |= FILE_ATTRIBUTE_READONLY;
2021 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2022 }
2023 else
2024 res = 0;
2025 Py_END_ALLOW_THREADS
2026 if (!res)
2027 return win32_error_unicode("chmod",
2028 PyUnicode_AS_UNICODE(po));
2029 Py_INCREF(Py_None);
2030 return Py_None;
2031 }
2032 /* Drop the argument parsing error as narrow strings
2033 are also valid. */
2034 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002035
Victor Stinner8c62be82010-05-06 00:08:46 +00002036 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2037 &opath, &i))
2038 return NULL;
2039 path = PyBytes_AsString(opath);
2040 Py_BEGIN_ALLOW_THREADS
2041 attr = GetFileAttributesA(path);
2042 if (attr != 0xFFFFFFFF) {
2043 if (i & _S_IWRITE)
2044 attr &= ~FILE_ATTRIBUTE_READONLY;
2045 else
2046 attr |= FILE_ATTRIBUTE_READONLY;
2047 res = SetFileAttributesA(path, attr);
2048 }
2049 else
2050 res = 0;
2051 Py_END_ALLOW_THREADS
2052 if (!res) {
2053 win32_error("chmod", path);
2054 Py_DECREF(opath);
2055 return NULL;
2056 }
2057 Py_DECREF(opath);
2058 Py_INCREF(Py_None);
2059 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002060#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002061 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2062 &opath, &i))
2063 return NULL;
2064 path = PyBytes_AsString(opath);
2065 Py_BEGIN_ALLOW_THREADS
2066 res = chmod(path, i);
2067 Py_END_ALLOW_THREADS
2068 if (res < 0)
2069 return posix_error_with_allocated_filename(opath);
2070 Py_DECREF(opath);
2071 Py_INCREF(Py_None);
2072 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002073#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002074}
2075
Christian Heimes4e30a842007-11-30 22:12:06 +00002076#ifdef HAVE_FCHMOD
2077PyDoc_STRVAR(posix_fchmod__doc__,
2078"fchmod(fd, mode)\n\n\
2079Change the access permissions of the file given by file\n\
2080descriptor fd.");
2081
2082static PyObject *
2083posix_fchmod(PyObject *self, PyObject *args)
2084{
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 int fd, mode, res;
2086 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2087 return NULL;
2088 Py_BEGIN_ALLOW_THREADS
2089 res = fchmod(fd, mode);
2090 Py_END_ALLOW_THREADS
2091 if (res < 0)
2092 return posix_error();
2093 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002094}
2095#endif /* HAVE_FCHMOD */
2096
2097#ifdef HAVE_LCHMOD
2098PyDoc_STRVAR(posix_lchmod__doc__,
2099"lchmod(path, mode)\n\n\
2100Change the access permissions of a file. If path is a symlink, this\n\
2101affects the link itself rather than the target.");
2102
2103static PyObject *
2104posix_lchmod(PyObject *self, PyObject *args)
2105{
Victor Stinner8c62be82010-05-06 00:08:46 +00002106 PyObject *opath;
2107 char *path;
2108 int i;
2109 int res;
2110 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2111 &opath, &i))
2112 return NULL;
2113 path = PyBytes_AsString(opath);
2114 Py_BEGIN_ALLOW_THREADS
2115 res = lchmod(path, i);
2116 Py_END_ALLOW_THREADS
2117 if (res < 0)
2118 return posix_error_with_allocated_filename(opath);
2119 Py_DECREF(opath);
2120 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002121}
2122#endif /* HAVE_LCHMOD */
2123
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002124
Thomas Wouterscf297e42007-02-23 15:07:44 +00002125#ifdef HAVE_CHFLAGS
2126PyDoc_STRVAR(posix_chflags__doc__,
2127"chflags(path, flags)\n\n\
2128Set file flags.");
2129
2130static PyObject *
2131posix_chflags(PyObject *self, PyObject *args)
2132{
Victor Stinner8c62be82010-05-06 00:08:46 +00002133 PyObject *opath;
2134 char *path;
2135 unsigned long flags;
2136 int res;
2137 if (!PyArg_ParseTuple(args, "O&k:chflags",
2138 PyUnicode_FSConverter, &opath, &flags))
2139 return NULL;
2140 path = PyBytes_AsString(opath);
2141 Py_BEGIN_ALLOW_THREADS
2142 res = chflags(path, flags);
2143 Py_END_ALLOW_THREADS
2144 if (res < 0)
2145 return posix_error_with_allocated_filename(opath);
2146 Py_DECREF(opath);
2147 Py_INCREF(Py_None);
2148 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002149}
2150#endif /* HAVE_CHFLAGS */
2151
2152#ifdef HAVE_LCHFLAGS
2153PyDoc_STRVAR(posix_lchflags__doc__,
2154"lchflags(path, flags)\n\n\
2155Set file flags.\n\
2156This function will not follow symbolic links.");
2157
2158static PyObject *
2159posix_lchflags(PyObject *self, PyObject *args)
2160{
Victor Stinner8c62be82010-05-06 00:08:46 +00002161 PyObject *opath;
2162 char *path;
2163 unsigned long flags;
2164 int res;
2165 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2166 PyUnicode_FSConverter, &opath, &flags))
2167 return NULL;
2168 path = PyBytes_AsString(opath);
2169 Py_BEGIN_ALLOW_THREADS
2170 res = lchflags(path, flags);
2171 Py_END_ALLOW_THREADS
2172 if (res < 0)
2173 return posix_error_with_allocated_filename(opath);
2174 Py_DECREF(opath);
2175 Py_INCREF(Py_None);
2176 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002177}
2178#endif /* HAVE_LCHFLAGS */
2179
Martin v. Löwis244edc82001-10-04 22:44:26 +00002180#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002181PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002182"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002183Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002184
2185static PyObject *
2186posix_chroot(PyObject *self, PyObject *args)
2187{
Victor Stinner8c62be82010-05-06 00:08:46 +00002188 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002189}
2190#endif
2191
Guido van Rossum21142a01999-01-08 21:05:37 +00002192#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002193PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002194"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002195force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002196
2197static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002198posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002199{
Stefan Krah0e803b32010-11-26 16:16:47 +00002200 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002201}
2202#endif /* HAVE_FSYNC */
2203
Ross Lagerwall7807c352011-03-17 20:20:30 +02002204#ifdef HAVE_SYNC
2205PyDoc_STRVAR(posix_sync__doc__,
2206"sync()\n\n\
2207Force write of everything to disk.");
2208
2209static PyObject *
2210posix_sync(PyObject *self, PyObject *noargs)
2211{
2212 Py_BEGIN_ALLOW_THREADS
2213 sync();
2214 Py_END_ALLOW_THREADS
2215 Py_RETURN_NONE;
2216}
2217#endif
2218
Guido van Rossum21142a01999-01-08 21:05:37 +00002219#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002220
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002221#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002222extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2223#endif
2224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002225PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002226"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002227force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002228 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002229
2230static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002231posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002232{
Stefan Krah0e803b32010-11-26 16:16:47 +00002233 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002234}
2235#endif /* HAVE_FDATASYNC */
2236
2237
Fredrik Lundh10723342000-07-10 16:38:09 +00002238#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002239PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002240"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002241Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002242
Barry Warsaw53699e91996-12-10 23:23:01 +00002243static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002244posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002245{
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 PyObject *opath;
2247 char *path;
2248 long uid, gid;
2249 int res;
2250 if (!PyArg_ParseTuple(args, "O&ll:chown",
2251 PyUnicode_FSConverter, &opath,
2252 &uid, &gid))
2253 return NULL;
2254 path = PyBytes_AsString(opath);
2255 Py_BEGIN_ALLOW_THREADS
2256 res = chown(path, (uid_t) uid, (gid_t) gid);
2257 Py_END_ALLOW_THREADS
2258 if (res < 0)
2259 return posix_error_with_allocated_filename(opath);
2260 Py_DECREF(opath);
2261 Py_INCREF(Py_None);
2262 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002263}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002264#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002265
Christian Heimes4e30a842007-11-30 22:12:06 +00002266#ifdef HAVE_FCHOWN
2267PyDoc_STRVAR(posix_fchown__doc__,
2268"fchown(fd, uid, gid)\n\n\
2269Change the owner and group id of the file given by file descriptor\n\
2270fd to the numeric uid and gid.");
2271
2272static PyObject *
2273posix_fchown(PyObject *self, PyObject *args)
2274{
Victor Stinner8c62be82010-05-06 00:08:46 +00002275 int fd;
2276 long uid, gid;
2277 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002278 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002279 return NULL;
2280 Py_BEGIN_ALLOW_THREADS
2281 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2282 Py_END_ALLOW_THREADS
2283 if (res < 0)
2284 return posix_error();
2285 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002286}
2287#endif /* HAVE_FCHOWN */
2288
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002289#ifdef HAVE_LCHOWN
2290PyDoc_STRVAR(posix_lchown__doc__,
2291"lchown(path, uid, gid)\n\n\
2292Change the owner and group id of path to the numeric uid and gid.\n\
2293This function will not follow symbolic links.");
2294
2295static PyObject *
2296posix_lchown(PyObject *self, PyObject *args)
2297{
Victor Stinner8c62be82010-05-06 00:08:46 +00002298 PyObject *opath;
2299 char *path;
2300 long uid, gid;
2301 int res;
2302 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2303 PyUnicode_FSConverter, &opath,
2304 &uid, &gid))
2305 return NULL;
2306 path = PyBytes_AsString(opath);
2307 Py_BEGIN_ALLOW_THREADS
2308 res = lchown(path, (uid_t) uid, (gid_t) gid);
2309 Py_END_ALLOW_THREADS
2310 if (res < 0)
2311 return posix_error_with_allocated_filename(opath);
2312 Py_DECREF(opath);
2313 Py_INCREF(Py_None);
2314 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002315}
2316#endif /* HAVE_LCHOWN */
2317
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002318
Guido van Rossum36bc6801995-06-14 22:54:23 +00002319#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002320static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002321posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002322{
Victor Stinner8c62be82010-05-06 00:08:46 +00002323 char buf[1026];
2324 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002325
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002326#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002327 if (!use_bytes) {
2328 wchar_t wbuf[1026];
2329 wchar_t *wbuf2 = wbuf;
2330 PyObject *resobj;
2331 DWORD len;
2332 Py_BEGIN_ALLOW_THREADS
2333 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2334 /* If the buffer is large enough, len does not include the
2335 terminating \0. If the buffer is too small, len includes
2336 the space needed for the terminator. */
2337 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2338 wbuf2 = malloc(len * sizeof(wchar_t));
2339 if (wbuf2)
2340 len = GetCurrentDirectoryW(len, wbuf2);
2341 }
2342 Py_END_ALLOW_THREADS
2343 if (!wbuf2) {
2344 PyErr_NoMemory();
2345 return NULL;
2346 }
2347 if (!len) {
2348 if (wbuf2 != wbuf) free(wbuf2);
2349 return win32_error("getcwdu", NULL);
2350 }
2351 resobj = PyUnicode_FromWideChar(wbuf2, len);
2352 if (wbuf2 != wbuf) free(wbuf2);
2353 return resobj;
2354 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002355#endif
2356
Victor Stinner8c62be82010-05-06 00:08:46 +00002357 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002358#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002359 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002360#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002361 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002362#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002363 Py_END_ALLOW_THREADS
2364 if (res == NULL)
2365 return posix_error();
2366 if (use_bytes)
2367 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002368 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002369}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002370
2371PyDoc_STRVAR(posix_getcwd__doc__,
2372"getcwd() -> path\n\n\
2373Return a unicode string representing the current working directory.");
2374
2375static PyObject *
2376posix_getcwd_unicode(PyObject *self)
2377{
2378 return posix_getcwd(0);
2379}
2380
2381PyDoc_STRVAR(posix_getcwdb__doc__,
2382"getcwdb() -> path\n\n\
2383Return a bytes string representing the current working directory.");
2384
2385static PyObject *
2386posix_getcwd_bytes(PyObject *self)
2387{
2388 return posix_getcwd(1);
2389}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002390#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002392
Guido van Rossumb6775db1994-08-01 11:34:53 +00002393#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002394PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002395"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002396Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002397
Barry Warsaw53699e91996-12-10 23:23:01 +00002398static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002399posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002400{
Victor Stinner8c62be82010-05-06 00:08:46 +00002401 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002402}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002403#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002404
Brian Curtin1b9df392010-11-24 20:24:31 +00002405#ifdef MS_WINDOWS
2406PyDoc_STRVAR(win32_link__doc__,
2407"link(src, dst)\n\n\
2408Create a hard link to a file.");
2409
2410static PyObject *
2411win32_link(PyObject *self, PyObject *args)
2412{
2413 PyObject *osrc, *odst;
2414 char *src, *dst;
2415 BOOL rslt;
2416
Brian Curtinfc889c42010-11-28 23:59:46 +00002417 PyUnicodeObject *usrc, *udst;
2418 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2419 Py_BEGIN_ALLOW_THREADS
2420 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2421 PyUnicode_AS_UNICODE(usrc), NULL);
2422 Py_END_ALLOW_THREADS
2423
2424 if (rslt == 0)
2425 return win32_error("link", NULL);
2426
2427 Py_RETURN_NONE;
2428 }
2429
2430 /* Narrow strings also valid. */
2431 PyErr_Clear();
2432
Brian Curtin1b9df392010-11-24 20:24:31 +00002433 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2434 PyUnicode_FSConverter, &odst))
2435 return NULL;
2436
2437 src = PyBytes_AsString(osrc);
2438 dst = PyBytes_AsString(odst);
2439
2440 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002441 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002442 Py_END_ALLOW_THREADS
2443
Stefan Krah30b341f2010-11-27 11:44:18 +00002444 Py_DECREF(osrc);
2445 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002446 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002447 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002448
2449 Py_RETURN_NONE;
2450}
2451#endif /* MS_WINDOWS */
2452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002454PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002455"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002456Return a list containing the names of the entries in the directory.\n\
2457\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002458 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002459\n\
2460The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002461entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002462
Barry Warsaw53699e91996-12-10 23:23:01 +00002463static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002464posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002465{
Victor Stinner8c62be82010-05-06 00:08:46 +00002466 /* XXX Should redo this putting the (now four) versions of opendir
2467 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002468#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002469
Victor Stinner8c62be82010-05-06 00:08:46 +00002470 PyObject *d, *v;
2471 HANDLE hFindFile;
2472 BOOL result;
2473 WIN32_FIND_DATA FileData;
2474 PyObject *opath;
2475 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2476 char *bufptr = namebuf;
2477 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002478
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002479 PyObject *po = NULL;
2480 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002481 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002482 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002483
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002484 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002485 po_wchars = L".";
2486 len = 1;
2487 } else {
2488 po_wchars = PyUnicode_AS_UNICODE(po);
2489 len = PyUnicode_GET_SIZE(po);
2490 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002491 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002492 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2493 if (!wnamebuf) {
2494 PyErr_NoMemory();
2495 return NULL;
2496 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002497 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002498 if (len > 0) {
2499 Py_UNICODE wch = wnamebuf[len-1];
2500 if (wch != L'/' && wch != L'\\' && wch != L':')
2501 wnamebuf[len++] = L'\\';
2502 wcscpy(wnamebuf + len, L"*.*");
2503 }
2504 if ((d = PyList_New(0)) == NULL) {
2505 free(wnamebuf);
2506 return NULL;
2507 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002508 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002509 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002510 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002511 if (hFindFile == INVALID_HANDLE_VALUE) {
2512 int error = GetLastError();
2513 if (error == ERROR_FILE_NOT_FOUND) {
2514 free(wnamebuf);
2515 return d;
2516 }
2517 Py_DECREF(d);
2518 win32_error_unicode("FindFirstFileW", wnamebuf);
2519 free(wnamebuf);
2520 return NULL;
2521 }
2522 do {
2523 /* Skip over . and .. */
2524 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2525 wcscmp(wFileData.cFileName, L"..") != 0) {
2526 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2527 if (v == NULL) {
2528 Py_DECREF(d);
2529 d = NULL;
2530 break;
2531 }
2532 if (PyList_Append(d, v) != 0) {
2533 Py_DECREF(v);
2534 Py_DECREF(d);
2535 d = NULL;
2536 break;
2537 }
2538 Py_DECREF(v);
2539 }
2540 Py_BEGIN_ALLOW_THREADS
2541 result = FindNextFileW(hFindFile, &wFileData);
2542 Py_END_ALLOW_THREADS
2543 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2544 it got to the end of the directory. */
2545 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2546 Py_DECREF(d);
2547 win32_error_unicode("FindNextFileW", wnamebuf);
2548 FindClose(hFindFile);
2549 free(wnamebuf);
2550 return NULL;
2551 }
2552 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002553
Victor Stinner8c62be82010-05-06 00:08:46 +00002554 if (FindClose(hFindFile) == FALSE) {
2555 Py_DECREF(d);
2556 win32_error_unicode("FindClose", wnamebuf);
2557 free(wnamebuf);
2558 return NULL;
2559 }
2560 free(wnamebuf);
2561 return d;
2562 }
2563 /* Drop the argument parsing error as narrow strings
2564 are also valid. */
2565 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002566
Victor Stinner8c62be82010-05-06 00:08:46 +00002567 if (!PyArg_ParseTuple(args, "O&:listdir",
2568 PyUnicode_FSConverter, &opath))
2569 return NULL;
2570 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2571 PyErr_SetString(PyExc_ValueError, "path too long");
2572 Py_DECREF(opath);
2573 return NULL;
2574 }
2575 strcpy(namebuf, PyBytes_AsString(opath));
2576 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002577 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002578 if (len > 0) {
2579 char ch = namebuf[len-1];
2580 if (ch != SEP && ch != ALTSEP && ch != ':')
2581 namebuf[len++] = '/';
2582 strcpy(namebuf + len, "*.*");
2583 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002584
Victor Stinner8c62be82010-05-06 00:08:46 +00002585 if ((d = PyList_New(0)) == NULL)
2586 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002587
Antoine Pitroub73caab2010-08-09 23:39:31 +00002588 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002589 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002590 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002591 if (hFindFile == INVALID_HANDLE_VALUE) {
2592 int error = GetLastError();
2593 if (error == ERROR_FILE_NOT_FOUND)
2594 return d;
2595 Py_DECREF(d);
2596 return win32_error("FindFirstFile", namebuf);
2597 }
2598 do {
2599 /* Skip over . and .. */
2600 if (strcmp(FileData.cFileName, ".") != 0 &&
2601 strcmp(FileData.cFileName, "..") != 0) {
2602 v = PyBytes_FromString(FileData.cFileName);
2603 if (v == NULL) {
2604 Py_DECREF(d);
2605 d = NULL;
2606 break;
2607 }
2608 if (PyList_Append(d, v) != 0) {
2609 Py_DECREF(v);
2610 Py_DECREF(d);
2611 d = NULL;
2612 break;
2613 }
2614 Py_DECREF(v);
2615 }
2616 Py_BEGIN_ALLOW_THREADS
2617 result = FindNextFile(hFindFile, &FileData);
2618 Py_END_ALLOW_THREADS
2619 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2620 it got to the end of the directory. */
2621 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2622 Py_DECREF(d);
2623 win32_error("FindNextFile", namebuf);
2624 FindClose(hFindFile);
2625 return NULL;
2626 }
2627 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002628
Victor Stinner8c62be82010-05-06 00:08:46 +00002629 if (FindClose(hFindFile) == FALSE) {
2630 Py_DECREF(d);
2631 return win32_error("FindClose", namebuf);
2632 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002633
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002635
Tim Peters0bb44a42000-09-15 07:44:49 +00002636#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002637
2638#ifndef MAX_PATH
2639#define MAX_PATH CCHMAXPATH
2640#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002641 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002642 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002643 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002644 PyObject *d, *v;
2645 char namebuf[MAX_PATH+5];
2646 HDIR hdir = 1;
2647 ULONG srchcnt = 1;
2648 FILEFINDBUF3 ep;
2649 APIRET rc;
2650
Victor Stinner8c62be82010-05-06 00:08:46 +00002651 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002652 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002653 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002654 name = PyBytes_AsString(oname);
2655 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002656 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002657 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002658 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002659 return NULL;
2660 }
2661 strcpy(namebuf, name);
2662 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002663 if (*pt == ALTSEP)
2664 *pt = SEP;
2665 if (namebuf[len-1] != SEP)
2666 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002667 strcpy(namebuf + len, "*.*");
2668
Neal Norwitz6c913782007-10-14 03:23:09 +00002669 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002670 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002671 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002672 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002673
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002674 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2675 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002676 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002677 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2678 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2679 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002680
2681 if (rc != NO_ERROR) {
2682 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002683 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002684 }
2685
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002686 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002687 do {
2688 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002689 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002690 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002691
2692 strcpy(namebuf, ep.achName);
2693
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002694 /* Leave Case of Name Alone -- In Native Form */
2695 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002696
Christian Heimes72b710a2008-05-26 13:28:38 +00002697 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002698 if (v == NULL) {
2699 Py_DECREF(d);
2700 d = NULL;
2701 break;
2702 }
2703 if (PyList_Append(d, v) != 0) {
2704 Py_DECREF(v);
2705 Py_DECREF(d);
2706 d = NULL;
2707 break;
2708 }
2709 Py_DECREF(v);
2710 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2711 }
2712
Victor Stinnerdcb24032010-04-22 12:08:36 +00002713 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002714 return d;
2715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002716 PyObject *oname;
2717 char *name;
2718 PyObject *d, *v;
2719 DIR *dirp;
2720 struct dirent *ep;
2721 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002722
Victor Stinner8c62be82010-05-06 00:08:46 +00002723 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002724 /* v is never read, so it does not need to be initialized yet. */
2725 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002726 arg_is_unicode = 0;
2727 PyErr_Clear();
2728 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002729 oname = NULL;
2730 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002731 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002732 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002733 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002734 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002735 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002736 Py_BEGIN_ALLOW_THREADS
2737 dirp = opendir(name);
2738 Py_END_ALLOW_THREADS
2739 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002740 return posix_error_with_allocated_filename(oname);
2741 }
2742 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002743 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002744 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002745 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002746 Py_DECREF(oname);
2747 return NULL;
2748 }
2749 for (;;) {
2750 errno = 0;
2751 Py_BEGIN_ALLOW_THREADS
2752 ep = readdir(dirp);
2753 Py_END_ALLOW_THREADS
2754 if (ep == NULL) {
2755 if (errno == 0) {
2756 break;
2757 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002758 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002759 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002760 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002761 Py_DECREF(d);
2762 return posix_error_with_allocated_filename(oname);
2763 }
2764 }
2765 if (ep->d_name[0] == '.' &&
2766 (NAMLEN(ep) == 1 ||
2767 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2768 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002769 if (arg_is_unicode)
2770 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2771 else
2772 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002773 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002774 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002775 break;
2776 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002777 if (PyList_Append(d, v) != 0) {
2778 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002779 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002780 break;
2781 }
2782 Py_DECREF(v);
2783 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002784 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002785 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002786 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002787 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002788
Victor Stinner8c62be82010-05-06 00:08:46 +00002789 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002790
Tim Peters0bb44a42000-09-15 07:44:49 +00002791#endif /* which OS */
2792} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002793
Antoine Pitrou8250e232011-02-25 23:41:16 +00002794#ifdef HAVE_FDOPENDIR
2795PyDoc_STRVAR(posix_fdlistdir__doc__,
2796"fdlistdir(fd) -> list_of_strings\n\n\
2797Like listdir(), but uses a file descriptor instead.\n\
2798After succesful execution of this function, fd will be closed.");
2799
2800static PyObject *
2801posix_fdlistdir(PyObject *self, PyObject *args)
2802{
2803 PyObject *d, *v;
2804 DIR *dirp;
2805 struct dirent *ep;
2806 int fd;
2807
2808 errno = 0;
2809 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2810 return NULL;
2811 Py_BEGIN_ALLOW_THREADS
2812 dirp = fdopendir(fd);
2813 Py_END_ALLOW_THREADS
2814 if (dirp == NULL) {
2815 close(fd);
2816 return posix_error();
2817 }
2818 if ((d = PyList_New(0)) == NULL) {
2819 Py_BEGIN_ALLOW_THREADS
2820 closedir(dirp);
2821 Py_END_ALLOW_THREADS
2822 return NULL;
2823 }
2824 for (;;) {
2825 errno = 0;
2826 Py_BEGIN_ALLOW_THREADS
2827 ep = readdir(dirp);
2828 Py_END_ALLOW_THREADS
2829 if (ep == NULL) {
2830 if (errno == 0) {
2831 break;
2832 } else {
2833 Py_BEGIN_ALLOW_THREADS
2834 closedir(dirp);
2835 Py_END_ALLOW_THREADS
2836 Py_DECREF(d);
2837 return posix_error();
2838 }
2839 }
2840 if (ep->d_name[0] == '.' &&
2841 (NAMLEN(ep) == 1 ||
2842 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2843 continue;
2844 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2845 if (v == NULL) {
2846 Py_CLEAR(d);
2847 break;
2848 }
2849 if (PyList_Append(d, v) != 0) {
2850 Py_DECREF(v);
2851 Py_CLEAR(d);
2852 break;
2853 }
2854 Py_DECREF(v);
2855 }
2856 Py_BEGIN_ALLOW_THREADS
2857 closedir(dirp);
2858 Py_END_ALLOW_THREADS
2859
2860 return d;
2861}
2862#endif
2863
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002864#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002865/* A helper function for abspath on win32 */
2866static PyObject *
2867posix__getfullpathname(PyObject *self, PyObject *args)
2868{
Victor Stinner8c62be82010-05-06 00:08:46 +00002869 PyObject *opath;
2870 char *path;
2871 char outbuf[MAX_PATH*2];
2872 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002873#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002874 PyUnicodeObject *po;
2875 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2876 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2877 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2878 Py_UNICODE *wtemp;
2879 DWORD result;
2880 PyObject *v;
2881 result = GetFullPathNameW(wpath,
2882 sizeof(woutbuf)/sizeof(woutbuf[0]),
2883 woutbuf, &wtemp);
2884 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2885 woutbufp = malloc(result * sizeof(Py_UNICODE));
2886 if (!woutbufp)
2887 return PyErr_NoMemory();
2888 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2889 }
2890 if (result)
2891 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2892 else
2893 v = win32_error_unicode("GetFullPathNameW", wpath);
2894 if (woutbufp != woutbuf)
2895 free(woutbufp);
2896 return v;
2897 }
2898 /* Drop the argument parsing error as narrow strings
2899 are also valid. */
2900 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002901
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002902#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002903 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2904 PyUnicode_FSConverter, &opath))
2905 return NULL;
2906 path = PyBytes_AsString(opath);
2907 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2908 outbuf, &temp)) {
2909 win32_error("GetFullPathName", path);
2910 Py_DECREF(opath);
2911 return NULL;
2912 }
2913 Py_DECREF(opath);
2914 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2915 return PyUnicode_Decode(outbuf, strlen(outbuf),
2916 Py_FileSystemDefaultEncoding, NULL);
2917 }
2918 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002919} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002920
Brian Curtind25aef52011-06-13 15:16:04 -05002921
Brian Curtinf5e76d02010-11-24 13:14:05 +00002922
Brian Curtind40e6f72010-07-08 21:39:08 +00002923/* A helper function for samepath on windows */
2924static PyObject *
2925posix__getfinalpathname(PyObject *self, PyObject *args)
2926{
2927 HANDLE hFile;
2928 int buf_size;
2929 wchar_t *target_path;
2930 int result_length;
2931 PyObject *result;
2932 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002933
Brian Curtin94622b02010-09-24 00:03:39 +00002934 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002935 return NULL;
2936 }
2937
2938 if(!check_GetFinalPathNameByHandle()) {
2939 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2940 NotImplementedError. */
2941 return PyErr_Format(PyExc_NotImplementedError,
2942 "GetFinalPathNameByHandle not available on this platform");
2943 }
2944
2945 hFile = CreateFileW(
2946 path,
2947 0, /* desired access */
2948 0, /* share mode */
2949 NULL, /* security attributes */
2950 OPEN_EXISTING,
2951 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2952 FILE_FLAG_BACKUP_SEMANTICS,
2953 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002954
Brian Curtind40e6f72010-07-08 21:39:08 +00002955 if(hFile == INVALID_HANDLE_VALUE) {
2956 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002957 return PyErr_Format(PyExc_RuntimeError,
2958 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002959 }
2960
2961 /* We have a good handle to the target, use it to determine the
2962 target path name. */
2963 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2964
2965 if(!buf_size)
2966 return win32_error_unicode("GetFinalPathNameByHandle", path);
2967
2968 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2969 if(!target_path)
2970 return PyErr_NoMemory();
2971
2972 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2973 buf_size, VOLUME_NAME_DOS);
2974 if(!result_length)
2975 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2976
2977 if(!CloseHandle(hFile))
2978 return win32_error_unicode("GetFinalPathNameByHandle", path);
2979
2980 target_path[result_length] = 0;
2981 result = PyUnicode_FromUnicode(target_path, result_length);
2982 free(target_path);
2983 return result;
2984
2985} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002986
2987static PyObject *
2988posix__getfileinformation(PyObject *self, PyObject *args)
2989{
2990 HANDLE hFile;
2991 BY_HANDLE_FILE_INFORMATION info;
2992 int fd;
2993
2994 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2995 return NULL;
2996
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002997 if (!_PyVerify_fd(fd))
2998 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002999
3000 hFile = (HANDLE)_get_osfhandle(fd);
3001 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003002 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003003
3004 if (!GetFileInformationByHandle(hFile, &info))
3005 return win32_error("_getfileinformation", NULL);
3006
3007 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3008 info.nFileIndexHigh,
3009 info.nFileIndexLow);
3010}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003011
Brian Curtin95d028f2011-06-09 09:10:38 -05003012PyDoc_STRVAR(posix__isdir__doc__,
3013"Return true if the pathname refers to an existing directory.");
3014
Brian Curtin9c669cc2011-06-08 18:17:18 -05003015static PyObject *
3016posix__isdir(PyObject *self, PyObject *args)
3017{
3018 PyObject *opath;
3019 char *path;
3020 PyUnicodeObject *po;
3021 DWORD attributes;
3022
3023 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3024 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3025
3026 attributes = GetFileAttributesW(wpath);
3027 if (attributes == INVALID_FILE_ATTRIBUTES)
3028 Py_RETURN_FALSE;
3029 goto check;
3030 }
3031 /* Drop the argument parsing error as narrow strings
3032 are also valid. */
3033 PyErr_Clear();
3034
3035 if (!PyArg_ParseTuple(args, "O&:_isdir",
3036 PyUnicode_FSConverter, &opath))
3037 return NULL;
3038
3039 path = PyBytes_AsString(opath);
3040 attributes = GetFileAttributesA(path);
3041 if (attributes == INVALID_FILE_ATTRIBUTES)
3042 Py_RETURN_FALSE;
3043
3044check:
3045 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3046 Py_RETURN_TRUE;
3047 else
3048 Py_RETURN_FALSE;
3049}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003050#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003051
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003052PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003053"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003054Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003055
Barry Warsaw53699e91996-12-10 23:23:01 +00003056static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003057posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003058{
Victor Stinner8c62be82010-05-06 00:08:46 +00003059 int res;
3060 PyObject *opath;
3061 char *path;
3062 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003063
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003064#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003065 PyUnicodeObject *po;
3066 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3067 Py_BEGIN_ALLOW_THREADS
3068 /* PyUnicode_AS_UNICODE OK without thread lock as
3069 it is a simple dereference. */
3070 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3071 Py_END_ALLOW_THREADS
3072 if (!res)
3073 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3074 Py_INCREF(Py_None);
3075 return Py_None;
3076 }
3077 /* Drop the argument parsing error as narrow strings
3078 are also valid. */
3079 PyErr_Clear();
3080 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3081 PyUnicode_FSConverter, &opath, &mode))
3082 return NULL;
3083 path = PyBytes_AsString(opath);
3084 Py_BEGIN_ALLOW_THREADS
3085 /* PyUnicode_AS_UNICODE OK without thread lock as
3086 it is a simple dereference. */
3087 res = CreateDirectoryA(path, NULL);
3088 Py_END_ALLOW_THREADS
3089 if (!res) {
3090 win32_error("mkdir", path);
3091 Py_DECREF(opath);
3092 return NULL;
3093 }
3094 Py_DECREF(opath);
3095 Py_INCREF(Py_None);
3096 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003097#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003098
Victor Stinner8c62be82010-05-06 00:08:46 +00003099 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3100 PyUnicode_FSConverter, &opath, &mode))
3101 return NULL;
3102 path = PyBytes_AsString(opath);
3103 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003104#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003105 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003106#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003107 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003108#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003109 Py_END_ALLOW_THREADS
3110 if (res < 0)
3111 return posix_error_with_allocated_filename(opath);
3112 Py_DECREF(opath);
3113 Py_INCREF(Py_None);
3114 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003115#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003116}
3117
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003118
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003119/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3120#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003121#include <sys/resource.h>
3122#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003124
3125#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003126PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003127"nice(inc) -> new_priority\n\n\
3128Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003129
Barry Warsaw53699e91996-12-10 23:23:01 +00003130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003131posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003132{
Victor Stinner8c62be82010-05-06 00:08:46 +00003133 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003134
Victor Stinner8c62be82010-05-06 00:08:46 +00003135 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3136 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003137
Victor Stinner8c62be82010-05-06 00:08:46 +00003138 /* There are two flavours of 'nice': one that returns the new
3139 priority (as required by almost all standards out there) and the
3140 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3141 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003142
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 If we are of the nice family that returns the new priority, we
3144 need to clear errno before the call, and check if errno is filled
3145 before calling posix_error() on a returnvalue of -1, because the
3146 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003147
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 errno = 0;
3149 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003150#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003151 if (value == 0)
3152 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003153#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003154 if (value == -1 && errno != 0)
3155 /* either nice() or getpriority() returned an error */
3156 return posix_error();
3157 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003158}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003159#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003160
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003161
3162#ifdef HAVE_GETPRIORITY
3163PyDoc_STRVAR(posix_getpriority__doc__,
3164"getpriority(which, who) -> current_priority\n\n\
3165Get program scheduling priority.");
3166
3167static PyObject *
3168posix_getpriority(PyObject *self, PyObject *args)
3169{
3170 int which, who, retval;
3171
3172 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3173 return NULL;
3174 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003175 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003176 if (errno != 0)
3177 return posix_error();
3178 return PyLong_FromLong((long)retval);
3179}
3180#endif /* HAVE_GETPRIORITY */
3181
3182
3183#ifdef HAVE_SETPRIORITY
3184PyDoc_STRVAR(posix_setpriority__doc__,
3185"setpriority(which, who, prio) -> None\n\n\
3186Set program scheduling priority.");
3187
3188static PyObject *
3189posix_setpriority(PyObject *self, PyObject *args)
3190{
3191 int which, who, prio, retval;
3192
3193 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3194 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003195 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003196 if (retval == -1)
3197 return posix_error();
3198 Py_RETURN_NONE;
3199}
3200#endif /* HAVE_SETPRIORITY */
3201
3202
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003203PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003204"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003205Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003206
Barry Warsaw53699e91996-12-10 23:23:01 +00003207static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003208posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003209{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003210#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 PyObject *o1, *o2;
3212 char *p1, *p2;
3213 BOOL result;
3214 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3215 goto error;
3216 if (!convert_to_unicode(&o1))
3217 goto error;
3218 if (!convert_to_unicode(&o2)) {
3219 Py_DECREF(o1);
3220 goto error;
3221 }
3222 Py_BEGIN_ALLOW_THREADS
3223 result = MoveFileW(PyUnicode_AsUnicode(o1),
3224 PyUnicode_AsUnicode(o2));
3225 Py_END_ALLOW_THREADS
3226 Py_DECREF(o1);
3227 Py_DECREF(o2);
3228 if (!result)
3229 return win32_error("rename", NULL);
3230 Py_INCREF(Py_None);
3231 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003232error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003233 PyErr_Clear();
3234 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3235 return NULL;
3236 Py_BEGIN_ALLOW_THREADS
3237 result = MoveFileA(p1, p2);
3238 Py_END_ALLOW_THREADS
3239 if (!result)
3240 return win32_error("rename", NULL);
3241 Py_INCREF(Py_None);
3242 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003243#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003244 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003245#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003246}
3247
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003248
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003249PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003250"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003251Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003252
Barry Warsaw53699e91996-12-10 23:23:01 +00003253static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003254posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003255{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003256#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003257 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003258#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003259 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003260#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003261}
3262
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003264PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003265"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003266Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003267
Barry Warsaw53699e91996-12-10 23:23:01 +00003268static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003269posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003270{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003271#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003272 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003273#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003274 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003275#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003276}
3277
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003278
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003279#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003280PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003281"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003282Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003283
Barry Warsaw53699e91996-12-10 23:23:01 +00003284static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003285posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003286{
Victor Stinner8c62be82010-05-06 00:08:46 +00003287 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003288#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003289 wchar_t *command;
3290 if (!PyArg_ParseTuple(args, "u:system", &command))
3291 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003292
Victor Stinner8c62be82010-05-06 00:08:46 +00003293 Py_BEGIN_ALLOW_THREADS
3294 sts = _wsystem(command);
3295 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003296#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003297 PyObject *command_obj;
3298 char *command;
3299 if (!PyArg_ParseTuple(args, "O&:system",
3300 PyUnicode_FSConverter, &command_obj))
3301 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003302
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 command = PyBytes_AsString(command_obj);
3304 Py_BEGIN_ALLOW_THREADS
3305 sts = system(command);
3306 Py_END_ALLOW_THREADS
3307 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003308#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003309 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003310}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003311#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003312
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003314PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003315"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003316Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003317
Barry Warsaw53699e91996-12-10 23:23:01 +00003318static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003319posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003320{
Victor Stinner8c62be82010-05-06 00:08:46 +00003321 int i;
3322 if (!PyArg_ParseTuple(args, "i:umask", &i))
3323 return NULL;
3324 i = (int)umask(i);
3325 if (i < 0)
3326 return posix_error();
3327 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003328}
3329
Brian Curtind40e6f72010-07-08 21:39:08 +00003330#ifdef MS_WINDOWS
3331
3332/* override the default DeleteFileW behavior so that directory
3333symlinks can be removed with this function, the same as with
3334Unix symlinks */
3335BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3336{
3337 WIN32_FILE_ATTRIBUTE_DATA info;
3338 WIN32_FIND_DATAW find_data;
3339 HANDLE find_data_handle;
3340 int is_directory = 0;
3341 int is_link = 0;
3342
3343 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3344 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003345
Brian Curtind40e6f72010-07-08 21:39:08 +00003346 /* Get WIN32_FIND_DATA structure for the path to determine if
3347 it is a symlink */
3348 if(is_directory &&
3349 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3350 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3351
3352 if(find_data_handle != INVALID_HANDLE_VALUE) {
3353 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3354 FindClose(find_data_handle);
3355 }
3356 }
3357 }
3358
3359 if (is_directory && is_link)
3360 return RemoveDirectoryW(lpFileName);
3361
3362 return DeleteFileW(lpFileName);
3363}
3364#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003366PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003367"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003368Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003369
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003370PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003371"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003372Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003373
Barry Warsaw53699e91996-12-10 23:23:01 +00003374static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003375posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003376{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003377#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003378 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3379 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003380#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003381 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003382#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003383}
3384
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003385
Guido van Rossumb6775db1994-08-01 11:34:53 +00003386#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003387PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003388"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003389Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003390
Barry Warsaw53699e91996-12-10 23:23:01 +00003391static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003392posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003393{
Victor Stinner8c62be82010-05-06 00:08:46 +00003394 struct utsname u;
3395 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003396
Victor Stinner8c62be82010-05-06 00:08:46 +00003397 Py_BEGIN_ALLOW_THREADS
3398 res = uname(&u);
3399 Py_END_ALLOW_THREADS
3400 if (res < 0)
3401 return posix_error();
3402 return Py_BuildValue("(sssss)",
3403 u.sysname,
3404 u.nodename,
3405 u.release,
3406 u.version,
3407 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003408}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003409#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003410
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003411static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003412extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003413{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003414 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003415 if (PyFloat_Check(t)) {
3416 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003417 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003418 if (!intobj)
3419 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003420#if SIZEOF_TIME_T > SIZEOF_LONG
3421 intval = PyLong_AsUnsignedLongLongMask(intobj);
3422#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003423 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003424#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003425 Py_DECREF(intobj);
3426 if (intval == -1 && PyErr_Occurred())
3427 return -1;
3428 *sec = intval;
3429 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3430 if (*usec < 0)
3431 /* If rounding gave us a negative number,
3432 truncate. */
3433 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003434 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003435 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003436#if SIZEOF_TIME_T > SIZEOF_LONG
3437 intval = PyLong_AsUnsignedLongLongMask(t);
3438#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003439 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003440#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003441 if (intval == -1 && PyErr_Occurred())
3442 return -1;
3443 *sec = intval;
3444 *usec = 0;
3445 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003446}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003448PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003449"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003450utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003451Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003452second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003453
Barry Warsaw53699e91996-12-10 23:23:01 +00003454static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003455posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003456{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003457#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003458 PyObject *arg;
3459 PyUnicodeObject *obwpath;
3460 wchar_t *wpath = NULL;
3461 PyObject *oapath;
3462 char *apath;
3463 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003464 time_t atimesec, mtimesec;
3465 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003466 FILETIME atime, mtime;
3467 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003468
Victor Stinner8c62be82010-05-06 00:08:46 +00003469 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3470 wpath = PyUnicode_AS_UNICODE(obwpath);
3471 Py_BEGIN_ALLOW_THREADS
3472 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3473 NULL, OPEN_EXISTING,
3474 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3475 Py_END_ALLOW_THREADS
3476 if (hFile == INVALID_HANDLE_VALUE)
3477 return win32_error_unicode("utime", wpath);
3478 } else
3479 /* Drop the argument parsing error as narrow strings
3480 are also valid. */
3481 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003482
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 if (!wpath) {
3484 if (!PyArg_ParseTuple(args, "O&O:utime",
3485 PyUnicode_FSConverter, &oapath, &arg))
3486 return NULL;
3487 apath = PyBytes_AsString(oapath);
3488 Py_BEGIN_ALLOW_THREADS
3489 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3490 NULL, OPEN_EXISTING,
3491 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3492 Py_END_ALLOW_THREADS
3493 if (hFile == INVALID_HANDLE_VALUE) {
3494 win32_error("utime", apath);
3495 Py_DECREF(oapath);
3496 return NULL;
3497 }
3498 Py_DECREF(oapath);
3499 }
3500
3501 if (arg == Py_None) {
3502 SYSTEMTIME now;
3503 GetSystemTime(&now);
3504 if (!SystemTimeToFileTime(&now, &mtime) ||
3505 !SystemTimeToFileTime(&now, &atime)) {
3506 win32_error("utime", NULL);
3507 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003508 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003509 }
3510 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3511 PyErr_SetString(PyExc_TypeError,
3512 "utime() arg 2 must be a tuple (atime, mtime)");
3513 goto done;
3514 }
3515 else {
3516 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3517 &atimesec, &ausec) == -1)
3518 goto done;
3519 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3520 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3521 &mtimesec, &musec) == -1)
3522 goto done;
3523 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3524 }
3525 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3526 /* Avoid putting the file name into the error here,
3527 as that may confuse the user into believing that
3528 something is wrong with the file, when it also
3529 could be the time stamp that gives a problem. */
3530 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003531 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003532 }
3533 Py_INCREF(Py_None);
3534 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003535done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 CloseHandle(hFile);
3537 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003538#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003539
Victor Stinner8c62be82010-05-06 00:08:46 +00003540 PyObject *opath;
3541 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003542 time_t atime, mtime;
3543 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003544 int res;
3545 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003546
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003547#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003548 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003549#define ATIME buf[0].tv_sec
3550#define MTIME buf[1].tv_sec
3551#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003552/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003554#define ATIME buf.actime
3555#define MTIME buf.modtime
3556#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003557#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003559#define ATIME buf[0]
3560#define MTIME buf[1]
3561#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003562#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003563
Mark Hammond817c9292003-12-03 01:22:38 +00003564
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 if (!PyArg_ParseTuple(args, "O&O:utime",
3566 PyUnicode_FSConverter, &opath, &arg))
3567 return NULL;
3568 path = PyBytes_AsString(opath);
3569 if (arg == Py_None) {
3570 /* optional time values not given */
3571 Py_BEGIN_ALLOW_THREADS
3572 res = utime(path, NULL);
3573 Py_END_ALLOW_THREADS
3574 }
3575 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3576 PyErr_SetString(PyExc_TypeError,
3577 "utime() arg 2 must be a tuple (atime, mtime)");
3578 Py_DECREF(opath);
3579 return NULL;
3580 }
3581 else {
3582 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3583 &atime, &ausec) == -1) {
3584 Py_DECREF(opath);
3585 return NULL;
3586 }
3587 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3588 &mtime, &musec) == -1) {
3589 Py_DECREF(opath);
3590 return NULL;
3591 }
3592 ATIME = atime;
3593 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003594#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003595 buf[0].tv_usec = ausec;
3596 buf[1].tv_usec = musec;
3597 Py_BEGIN_ALLOW_THREADS
3598 res = utimes(path, buf);
3599 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003600#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 Py_BEGIN_ALLOW_THREADS
3602 res = utime(path, UTIME_ARG);
3603 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003604#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 }
3606 if (res < 0) {
3607 return posix_error_with_allocated_filename(opath);
3608 }
3609 Py_DECREF(opath);
3610 Py_INCREF(Py_None);
3611 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003612#undef UTIME_ARG
3613#undef ATIME
3614#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003615#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003616}
3617
Ross Lagerwall7807c352011-03-17 20:20:30 +02003618#ifdef HAVE_FUTIMES
3619PyDoc_STRVAR(posix_futimes__doc__,
3620"futimes(fd, (atime, mtime))\n\
3621futimes(fd, None)\n\n\
3622Set the access and modified time of the file specified by the file\n\
3623descriptor fd to the given values. If the second form is used, set the\n\
3624access and modified times to the current time.");
3625
3626static PyObject *
3627posix_futimes(PyObject *self, PyObject *args)
3628{
3629 int res, fd;
3630 PyObject* arg;
3631 struct timeval buf[2];
3632 long ausec, musec;
3633
3634 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3635 return NULL;
3636
3637 if (arg == Py_None) {
3638 /* optional time values not given */
3639 Py_BEGIN_ALLOW_THREADS
3640 res = futimes(fd, NULL);
3641 Py_END_ALLOW_THREADS
3642 }
3643 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3644 PyErr_SetString(PyExc_TypeError,
3645 "futimes() arg 2 must be a tuple (atime, mtime)");
3646 return NULL;
3647 }
3648 else {
3649 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3650 &(buf[0].tv_sec), &ausec) == -1) {
3651 return NULL;
3652 }
3653 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3654 &(buf[1].tv_sec), &musec) == -1) {
3655 return NULL;
3656 }
3657 buf[0].tv_usec = ausec;
3658 buf[1].tv_usec = musec;
3659 Py_BEGIN_ALLOW_THREADS
3660 res = futimes(fd, buf);
3661 Py_END_ALLOW_THREADS
3662 }
3663 if (res < 0)
3664 return posix_error();
3665 Py_RETURN_NONE;
3666}
3667#endif
3668
3669#ifdef HAVE_LUTIMES
3670PyDoc_STRVAR(posix_lutimes__doc__,
3671"lutimes(path, (atime, mtime))\n\
3672lutimes(path, None)\n\n\
3673Like utime(), but if path is a symbolic link, it is not dereferenced.");
3674
3675static PyObject *
3676posix_lutimes(PyObject *self, PyObject *args)
3677{
3678 PyObject *opath, *arg;
3679 const char *path;
3680 int res;
3681 struct timeval buf[2];
3682 long ausec, musec;
3683
3684 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3685 PyUnicode_FSConverter, &opath, &arg))
3686 return NULL;
3687 path = PyBytes_AsString(opath);
3688 if (arg == Py_None) {
3689 /* optional time values not given */
3690 Py_BEGIN_ALLOW_THREADS
3691 res = lutimes(path, NULL);
3692 Py_END_ALLOW_THREADS
3693 }
3694 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3695 PyErr_SetString(PyExc_TypeError,
3696 "lutimes() arg 2 must be a tuple (atime, mtime)");
3697 Py_DECREF(opath);
3698 return NULL;
3699 }
3700 else {
3701 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3702 &(buf[0].tv_sec), &ausec) == -1) {
3703 Py_DECREF(opath);
3704 return NULL;
3705 }
3706 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3707 &(buf[1].tv_sec), &musec) == -1) {
3708 Py_DECREF(opath);
3709 return NULL;
3710 }
3711 buf[0].tv_usec = ausec;
3712 buf[1].tv_usec = musec;
3713 Py_BEGIN_ALLOW_THREADS
3714 res = lutimes(path, buf);
3715 Py_END_ALLOW_THREADS
3716 }
3717 Py_DECREF(opath);
3718 if (res < 0)
3719 return posix_error();
3720 Py_RETURN_NONE;
3721}
3722#endif
3723
3724#ifdef HAVE_FUTIMENS
3725PyDoc_STRVAR(posix_futimens__doc__,
3726"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3727futimens(fd, None, None)\n\n\
3728Updates the timestamps of a file specified by the file descriptor fd, with\n\
3729nanosecond precision.\n\
3730The second form sets atime and mtime to the current time.\n\
3731If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3732current time.\n\
3733If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3734
3735static PyObject *
3736posix_futimens(PyObject *self, PyObject *args)
3737{
3738 int res, fd;
3739 PyObject *atime, *mtime;
3740 struct timespec buf[2];
3741
3742 if (!PyArg_ParseTuple(args, "iOO:futimens",
3743 &fd, &atime, &mtime))
3744 return NULL;
3745 if (atime == Py_None && mtime == Py_None) {
3746 /* optional time values not given */
3747 Py_BEGIN_ALLOW_THREADS
3748 res = futimens(fd, NULL);
3749 Py_END_ALLOW_THREADS
3750 }
3751 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3752 PyErr_SetString(PyExc_TypeError,
3753 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3754 return NULL;
3755 }
3756 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3757 PyErr_SetString(PyExc_TypeError,
3758 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3759 return NULL;
3760 }
3761 else {
3762 if (!PyArg_ParseTuple(atime, "ll:futimens",
3763 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3764 return NULL;
3765 }
3766 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3767 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3768 return NULL;
3769 }
3770 Py_BEGIN_ALLOW_THREADS
3771 res = futimens(fd, buf);
3772 Py_END_ALLOW_THREADS
3773 }
3774 if (res < 0)
3775 return posix_error();
3776 Py_RETURN_NONE;
3777}
3778#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003779
Guido van Rossum3b066191991-06-04 19:40:25 +00003780/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003781
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003782PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003783"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003784Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003785
Barry Warsaw53699e91996-12-10 23:23:01 +00003786static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003787posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003788{
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 int sts;
3790 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3791 return NULL;
3792 _exit(sts);
3793 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003794}
3795
Martin v. Löwis114619e2002-10-07 06:44:21 +00003796#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3797static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003798free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003799{
Victor Stinner8c62be82010-05-06 00:08:46 +00003800 Py_ssize_t i;
3801 for (i = 0; i < count; i++)
3802 PyMem_Free(array[i]);
3803 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003804}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003805
Antoine Pitrou69f71142009-05-24 21:25:49 +00003806static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003807int fsconvert_strdup(PyObject *o, char**out)
3808{
Victor Stinner8c62be82010-05-06 00:08:46 +00003809 PyObject *bytes;
3810 Py_ssize_t size;
3811 if (!PyUnicode_FSConverter(o, &bytes))
3812 return 0;
3813 size = PyBytes_GET_SIZE(bytes);
3814 *out = PyMem_Malloc(size+1);
3815 if (!*out)
3816 return 0;
3817 memcpy(*out, PyBytes_AsString(bytes), size+1);
3818 Py_DECREF(bytes);
3819 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003820}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003821#endif
3822
Ross Lagerwall7807c352011-03-17 20:20:30 +02003823#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003824static char**
3825parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3826{
Victor Stinner8c62be82010-05-06 00:08:46 +00003827 char **envlist;
3828 Py_ssize_t i, pos, envc;
3829 PyObject *keys=NULL, *vals=NULL;
3830 PyObject *key, *val, *key2, *val2;
3831 char *p, *k, *v;
3832 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003833
Victor Stinner8c62be82010-05-06 00:08:46 +00003834 i = PyMapping_Size(env);
3835 if (i < 0)
3836 return NULL;
3837 envlist = PyMem_NEW(char *, i + 1);
3838 if (envlist == NULL) {
3839 PyErr_NoMemory();
3840 return NULL;
3841 }
3842 envc = 0;
3843 keys = PyMapping_Keys(env);
3844 vals = PyMapping_Values(env);
3845 if (!keys || !vals)
3846 goto error;
3847 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3848 PyErr_Format(PyExc_TypeError,
3849 "env.keys() or env.values() is not a list");
3850 goto error;
3851 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003852
Victor Stinner8c62be82010-05-06 00:08:46 +00003853 for (pos = 0; pos < i; pos++) {
3854 key = PyList_GetItem(keys, pos);
3855 val = PyList_GetItem(vals, pos);
3856 if (!key || !val)
3857 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003858
Victor Stinner8c62be82010-05-06 00:08:46 +00003859 if (PyUnicode_FSConverter(key, &key2) == 0)
3860 goto error;
3861 if (PyUnicode_FSConverter(val, &val2) == 0) {
3862 Py_DECREF(key2);
3863 goto error;
3864 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003865
3866#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003867 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3868 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003869#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003870 k = PyBytes_AsString(key2);
3871 v = PyBytes_AsString(val2);
3872 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003873
Victor Stinner8c62be82010-05-06 00:08:46 +00003874 p = PyMem_NEW(char, len);
3875 if (p == NULL) {
3876 PyErr_NoMemory();
3877 Py_DECREF(key2);
3878 Py_DECREF(val2);
3879 goto error;
3880 }
3881 PyOS_snprintf(p, len, "%s=%s", k, v);
3882 envlist[envc++] = p;
3883 Py_DECREF(key2);
3884 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003885#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003886 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003887#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 }
3889 Py_DECREF(vals);
3890 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003891
Victor Stinner8c62be82010-05-06 00:08:46 +00003892 envlist[envc] = 0;
3893 *envc_ptr = envc;
3894 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003895
3896error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 Py_XDECREF(keys);
3898 Py_XDECREF(vals);
3899 while (--envc >= 0)
3900 PyMem_DEL(envlist[envc]);
3901 PyMem_DEL(envlist);
3902 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003903}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003904
Ross Lagerwall7807c352011-03-17 20:20:30 +02003905static char**
3906parse_arglist(PyObject* argv, Py_ssize_t *argc)
3907{
3908 int i;
3909 char **argvlist = PyMem_NEW(char *, *argc+1);
3910 if (argvlist == NULL) {
3911 PyErr_NoMemory();
3912 return NULL;
3913 }
3914 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003915 PyObject* item = PySequence_ITEM(argv, i);
3916 if (item == NULL)
3917 goto fail;
3918 if (!fsconvert_strdup(item, &argvlist[i])) {
3919 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003920 goto fail;
3921 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003922 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003923 }
3924 argvlist[*argc] = NULL;
3925 return argvlist;
3926fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003927 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003928 free_string_array(argvlist, *argc);
3929 return NULL;
3930}
3931#endif
3932
3933#ifdef HAVE_EXECV
3934PyDoc_STRVAR(posix_execv__doc__,
3935"execv(path, args)\n\n\
3936Execute an executable path with arguments, replacing current process.\n\
3937\n\
3938 path: path of executable file\n\
3939 args: tuple or list of strings");
3940
3941static PyObject *
3942posix_execv(PyObject *self, PyObject *args)
3943{
3944 PyObject *opath;
3945 char *path;
3946 PyObject *argv;
3947 char **argvlist;
3948 Py_ssize_t argc;
3949
3950 /* execv has two arguments: (path, argv), where
3951 argv is a list or tuple of strings. */
3952
3953 if (!PyArg_ParseTuple(args, "O&O:execv",
3954 PyUnicode_FSConverter,
3955 &opath, &argv))
3956 return NULL;
3957 path = PyBytes_AsString(opath);
3958 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3959 PyErr_SetString(PyExc_TypeError,
3960 "execv() arg 2 must be a tuple or list");
3961 Py_DECREF(opath);
3962 return NULL;
3963 }
3964 argc = PySequence_Size(argv);
3965 if (argc < 1) {
3966 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3967 Py_DECREF(opath);
3968 return NULL;
3969 }
3970
3971 argvlist = parse_arglist(argv, &argc);
3972 if (argvlist == NULL) {
3973 Py_DECREF(opath);
3974 return NULL;
3975 }
3976
3977 execv(path, argvlist);
3978
3979 /* If we get here it's definitely an error */
3980
3981 free_string_array(argvlist, argc);
3982 Py_DECREF(opath);
3983 return posix_error();
3984}
3985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003986PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003987"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003988Execute a path with arguments and environment, replacing current process.\n\
3989\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003990 path: path of executable file\n\
3991 args: tuple or list of arguments\n\
3992 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003993
Barry Warsaw53699e91996-12-10 23:23:01 +00003994static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003995posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003996{
Victor Stinner8c62be82010-05-06 00:08:46 +00003997 PyObject *opath;
3998 char *path;
3999 PyObject *argv, *env;
4000 char **argvlist;
4001 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004002 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004003
Victor Stinner8c62be82010-05-06 00:08:46 +00004004 /* execve has three arguments: (path, argv, env), where
4005 argv is a list or tuple of strings and env is a dictionary
4006 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004007
Victor Stinner8c62be82010-05-06 00:08:46 +00004008 if (!PyArg_ParseTuple(args, "O&OO:execve",
4009 PyUnicode_FSConverter,
4010 &opath, &argv, &env))
4011 return NULL;
4012 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004013 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004014 PyErr_SetString(PyExc_TypeError,
4015 "execve() arg 2 must be a tuple or list");
4016 goto fail_0;
4017 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004018 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004019 if (!PyMapping_Check(env)) {
4020 PyErr_SetString(PyExc_TypeError,
4021 "execve() arg 3 must be a mapping object");
4022 goto fail_0;
4023 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004024
Ross Lagerwall7807c352011-03-17 20:20:30 +02004025 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004026 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004027 goto fail_0;
4028 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004029
Victor Stinner8c62be82010-05-06 00:08:46 +00004030 envlist = parse_envlist(env, &envc);
4031 if (envlist == NULL)
4032 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004033
Victor Stinner8c62be82010-05-06 00:08:46 +00004034 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004035
Victor Stinner8c62be82010-05-06 00:08:46 +00004036 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004037
Victor Stinner8c62be82010-05-06 00:08:46 +00004038 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004039
Victor Stinner8c62be82010-05-06 00:08:46 +00004040 while (--envc >= 0)
4041 PyMem_DEL(envlist[envc]);
4042 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004043 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004044 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004045 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004046 Py_DECREF(opath);
4047 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004048}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004049#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004050
Ross Lagerwall7807c352011-03-17 20:20:30 +02004051#ifdef HAVE_FEXECVE
4052PyDoc_STRVAR(posix_fexecve__doc__,
4053"fexecve(fd, args, env)\n\n\
4054Execute the program specified by a file descriptor with arguments and\n\
4055environment, replacing the current process.\n\
4056\n\
4057 fd: file descriptor of executable\n\
4058 args: tuple or list of arguments\n\
4059 env: dictionary of strings mapping to strings");
4060
4061static PyObject *
4062posix_fexecve(PyObject *self, PyObject *args)
4063{
4064 int fd;
4065 PyObject *argv, *env;
4066 char **argvlist;
4067 char **envlist;
4068 Py_ssize_t argc, envc;
4069
4070 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4071 &fd, &argv, &env))
4072 return NULL;
4073 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4074 PyErr_SetString(PyExc_TypeError,
4075 "fexecve() arg 2 must be a tuple or list");
4076 return NULL;
4077 }
4078 argc = PySequence_Size(argv);
4079 if (!PyMapping_Check(env)) {
4080 PyErr_SetString(PyExc_TypeError,
4081 "fexecve() arg 3 must be a mapping object");
4082 return NULL;
4083 }
4084
4085 argvlist = parse_arglist(argv, &argc);
4086 if (argvlist == NULL)
4087 return NULL;
4088
4089 envlist = parse_envlist(env, &envc);
4090 if (envlist == NULL)
4091 goto fail;
4092
4093 fexecve(fd, argvlist, envlist);
4094
4095 /* If we get here it's definitely an error */
4096
4097 (void) posix_error();
4098
4099 while (--envc >= 0)
4100 PyMem_DEL(envlist[envc]);
4101 PyMem_DEL(envlist);
4102 fail:
4103 free_string_array(argvlist, argc);
4104 return NULL;
4105}
4106#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004107
Guido van Rossuma1065681999-01-25 23:20:23 +00004108#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004109PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004110"spawnv(mode, path, args)\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 strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004116
4117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004118posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004119{
Victor Stinner8c62be82010-05-06 00:08:46 +00004120 PyObject *opath;
4121 char *path;
4122 PyObject *argv;
4123 char **argvlist;
4124 int mode, i;
4125 Py_ssize_t argc;
4126 Py_intptr_t spawnval;
4127 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004128
Victor Stinner8c62be82010-05-06 00:08:46 +00004129 /* spawnv has three arguments: (mode, path, argv), where
4130 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004131
Victor Stinner8c62be82010-05-06 00:08:46 +00004132 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4133 PyUnicode_FSConverter,
4134 &opath, &argv))
4135 return NULL;
4136 path = PyBytes_AsString(opath);
4137 if (PyList_Check(argv)) {
4138 argc = PyList_Size(argv);
4139 getitem = PyList_GetItem;
4140 }
4141 else if (PyTuple_Check(argv)) {
4142 argc = PyTuple_Size(argv);
4143 getitem = PyTuple_GetItem;
4144 }
4145 else {
4146 PyErr_SetString(PyExc_TypeError,
4147 "spawnv() arg 2 must be a tuple or list");
4148 Py_DECREF(opath);
4149 return NULL;
4150 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004151
Victor Stinner8c62be82010-05-06 00:08:46 +00004152 argvlist = PyMem_NEW(char *, argc+1);
4153 if (argvlist == NULL) {
4154 Py_DECREF(opath);
4155 return PyErr_NoMemory();
4156 }
4157 for (i = 0; i < argc; i++) {
4158 if (!fsconvert_strdup((*getitem)(argv, i),
4159 &argvlist[i])) {
4160 free_string_array(argvlist, i);
4161 PyErr_SetString(
4162 PyExc_TypeError,
4163 "spawnv() arg 2 must contain only strings");
4164 Py_DECREF(opath);
4165 return NULL;
4166 }
4167 }
4168 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004169
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004170#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004171 Py_BEGIN_ALLOW_THREADS
4172 spawnval = spawnv(mode, path, argvlist);
4173 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004174#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004175 if (mode == _OLD_P_OVERLAY)
4176 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004177
Victor Stinner8c62be82010-05-06 00:08:46 +00004178 Py_BEGIN_ALLOW_THREADS
4179 spawnval = _spawnv(mode, path, argvlist);
4180 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004181#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004182
Victor Stinner8c62be82010-05-06 00:08:46 +00004183 free_string_array(argvlist, argc);
4184 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004185
Victor Stinner8c62be82010-05-06 00:08:46 +00004186 if (spawnval == -1)
4187 return posix_error();
4188 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004189#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004190 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004191#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004193#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004194}
4195
4196
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004197PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004198"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004199Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004200\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004201 mode: mode of process creation\n\
4202 path: path of executable file\n\
4203 args: tuple or list of arguments\n\
4204 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004205
4206static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004207posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004208{
Victor Stinner8c62be82010-05-06 00:08:46 +00004209 PyObject *opath;
4210 char *path;
4211 PyObject *argv, *env;
4212 char **argvlist;
4213 char **envlist;
4214 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004215 int mode;
4216 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004217 Py_intptr_t spawnval;
4218 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4219 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004220
Victor Stinner8c62be82010-05-06 00:08:46 +00004221 /* spawnve has four arguments: (mode, path, argv, env), where
4222 argv is a list or tuple of strings and env is a dictionary
4223 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004224
Victor Stinner8c62be82010-05-06 00:08:46 +00004225 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4226 PyUnicode_FSConverter,
4227 &opath, &argv, &env))
4228 return NULL;
4229 path = PyBytes_AsString(opath);
4230 if (PyList_Check(argv)) {
4231 argc = PyList_Size(argv);
4232 getitem = PyList_GetItem;
4233 }
4234 else if (PyTuple_Check(argv)) {
4235 argc = PyTuple_Size(argv);
4236 getitem = PyTuple_GetItem;
4237 }
4238 else {
4239 PyErr_SetString(PyExc_TypeError,
4240 "spawnve() arg 2 must be a tuple or list");
4241 goto fail_0;
4242 }
4243 if (!PyMapping_Check(env)) {
4244 PyErr_SetString(PyExc_TypeError,
4245 "spawnve() arg 3 must be a mapping object");
4246 goto fail_0;
4247 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004248
Victor Stinner8c62be82010-05-06 00:08:46 +00004249 argvlist = PyMem_NEW(char *, argc+1);
4250 if (argvlist == NULL) {
4251 PyErr_NoMemory();
4252 goto fail_0;
4253 }
4254 for (i = 0; i < argc; i++) {
4255 if (!fsconvert_strdup((*getitem)(argv, i),
4256 &argvlist[i]))
4257 {
4258 lastarg = i;
4259 goto fail_1;
4260 }
4261 }
4262 lastarg = argc;
4263 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004264
Victor Stinner8c62be82010-05-06 00:08:46 +00004265 envlist = parse_envlist(env, &envc);
4266 if (envlist == NULL)
4267 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004268
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004269#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004270 Py_BEGIN_ALLOW_THREADS
4271 spawnval = spawnve(mode, path, argvlist, envlist);
4272 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004273#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004274 if (mode == _OLD_P_OVERLAY)
4275 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004276
Victor Stinner8c62be82010-05-06 00:08:46 +00004277 Py_BEGIN_ALLOW_THREADS
4278 spawnval = _spawnve(mode, path, argvlist, envlist);
4279 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004280#endif
Tim Peters25059d32001-12-07 20:35:43 +00004281
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 if (spawnval == -1)
4283 (void) posix_error();
4284 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004285#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004286 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004287#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004288 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004289#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004290
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 while (--envc >= 0)
4292 PyMem_DEL(envlist[envc]);
4293 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004294 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004295 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004296 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004297 Py_DECREF(opath);
4298 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004299}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004300
4301/* OS/2 supports spawnvp & spawnvpe natively */
4302#if defined(PYOS_OS2)
4303PyDoc_STRVAR(posix_spawnvp__doc__,
4304"spawnvp(mode, file, args)\n\n\
4305Execute the program 'file' in a new process, using the environment\n\
4306search path to find the file.\n\
4307\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004308 mode: mode of process creation\n\
4309 file: executable file name\n\
4310 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004311
4312static PyObject *
4313posix_spawnvp(PyObject *self, PyObject *args)
4314{
Victor Stinner8c62be82010-05-06 00:08:46 +00004315 PyObject *opath;
4316 char *path;
4317 PyObject *argv;
4318 char **argvlist;
4319 int mode, i, argc;
4320 Py_intptr_t spawnval;
4321 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004322
Victor Stinner8c62be82010-05-06 00:08:46 +00004323 /* spawnvp has three arguments: (mode, path, argv), where
4324 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004325
Victor Stinner8c62be82010-05-06 00:08:46 +00004326 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4327 PyUnicode_FSConverter,
4328 &opath, &argv))
4329 return NULL;
4330 path = PyBytes_AsString(opath);
4331 if (PyList_Check(argv)) {
4332 argc = PyList_Size(argv);
4333 getitem = PyList_GetItem;
4334 }
4335 else if (PyTuple_Check(argv)) {
4336 argc = PyTuple_Size(argv);
4337 getitem = PyTuple_GetItem;
4338 }
4339 else {
4340 PyErr_SetString(PyExc_TypeError,
4341 "spawnvp() arg 2 must be a tuple or list");
4342 Py_DECREF(opath);
4343 return NULL;
4344 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004345
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 argvlist = PyMem_NEW(char *, argc+1);
4347 if (argvlist == NULL) {
4348 Py_DECREF(opath);
4349 return PyErr_NoMemory();
4350 }
4351 for (i = 0; i < argc; i++) {
4352 if (!fsconvert_strdup((*getitem)(argv, i),
4353 &argvlist[i])) {
4354 free_string_array(argvlist, i);
4355 PyErr_SetString(
4356 PyExc_TypeError,
4357 "spawnvp() arg 2 must contain only strings");
4358 Py_DECREF(opath);
4359 return NULL;
4360 }
4361 }
4362 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004363
Victor Stinner8c62be82010-05-06 00:08:46 +00004364 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004365#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004366 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004367#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004368 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004369#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004370 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004371
Victor Stinner8c62be82010-05-06 00:08:46 +00004372 free_string_array(argvlist, argc);
4373 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 if (spawnval == -1)
4376 return posix_error();
4377 else
4378 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004379}
4380
4381
4382PyDoc_STRVAR(posix_spawnvpe__doc__,
4383"spawnvpe(mode, file, args, env)\n\n\
4384Execute the program 'file' in a new process, using the environment\n\
4385search path to find the file.\n\
4386\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 mode: mode of process creation\n\
4388 file: executable file name\n\
4389 args: tuple or list of arguments\n\
4390 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004391
4392static PyObject *
4393posix_spawnvpe(PyObject *self, PyObject *args)
4394{
Victor Stinner8c62be82010-05-06 00:08:46 +00004395 PyObject *opath
4396 char *path;
4397 PyObject *argv, *env;
4398 char **argvlist;
4399 char **envlist;
4400 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004401 int mode;
4402 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 Py_intptr_t spawnval;
4404 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4405 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004406
Victor Stinner8c62be82010-05-06 00:08:46 +00004407 /* spawnvpe has four arguments: (mode, path, argv, env), where
4408 argv is a list or tuple of strings and env is a dictionary
4409 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004410
Victor Stinner8c62be82010-05-06 00:08:46 +00004411 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4412 PyUnicode_FSConverter,
4413 &opath, &argv, &env))
4414 return NULL;
4415 path = PyBytes_AsString(opath);
4416 if (PyList_Check(argv)) {
4417 argc = PyList_Size(argv);
4418 getitem = PyList_GetItem;
4419 }
4420 else if (PyTuple_Check(argv)) {
4421 argc = PyTuple_Size(argv);
4422 getitem = PyTuple_GetItem;
4423 }
4424 else {
4425 PyErr_SetString(PyExc_TypeError,
4426 "spawnvpe() arg 2 must be a tuple or list");
4427 goto fail_0;
4428 }
4429 if (!PyMapping_Check(env)) {
4430 PyErr_SetString(PyExc_TypeError,
4431 "spawnvpe() arg 3 must be a mapping object");
4432 goto fail_0;
4433 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004434
Victor Stinner8c62be82010-05-06 00:08:46 +00004435 argvlist = PyMem_NEW(char *, argc+1);
4436 if (argvlist == NULL) {
4437 PyErr_NoMemory();
4438 goto fail_0;
4439 }
4440 for (i = 0; i < argc; i++) {
4441 if (!fsconvert_strdup((*getitem)(argv, i),
4442 &argvlist[i]))
4443 {
4444 lastarg = i;
4445 goto fail_1;
4446 }
4447 }
4448 lastarg = argc;
4449 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004450
Victor Stinner8c62be82010-05-06 00:08:46 +00004451 envlist = parse_envlist(env, &envc);
4452 if (envlist == NULL)
4453 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004454
Victor Stinner8c62be82010-05-06 00:08:46 +00004455 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004456#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004457 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004458#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004459 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004460#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004461 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004462
Victor Stinner8c62be82010-05-06 00:08:46 +00004463 if (spawnval == -1)
4464 (void) posix_error();
4465 else
4466 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 while (--envc >= 0)
4469 PyMem_DEL(envlist[envc]);
4470 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004471 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004472 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004473 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004474 Py_DECREF(opath);
4475 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004476}
4477#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004478#endif /* HAVE_SPAWNV */
4479
4480
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004481#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004482PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004483"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004484Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4485\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004486Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004487
4488static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004489posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004490{
Victor Stinner8c62be82010-05-06 00:08:46 +00004491 pid_t pid;
4492 int result = 0;
4493 _PyImport_AcquireLock();
4494 pid = fork1();
4495 if (pid == 0) {
4496 /* child: this clobbers and resets the import lock. */
4497 PyOS_AfterFork();
4498 } else {
4499 /* parent: release the import lock. */
4500 result = _PyImport_ReleaseLock();
4501 }
4502 if (pid == -1)
4503 return posix_error();
4504 if (result < 0) {
4505 /* Don't clobber the OSError if the fork failed. */
4506 PyErr_SetString(PyExc_RuntimeError,
4507 "not holding the import lock");
4508 return NULL;
4509 }
4510 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004511}
4512#endif
4513
4514
Guido van Rossumad0ee831995-03-01 10:34:45 +00004515#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004516PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004517"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004518Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004519Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004520
Barry Warsaw53699e91996-12-10 23:23:01 +00004521static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004522posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004523{
Victor Stinner8c62be82010-05-06 00:08:46 +00004524 pid_t pid;
4525 int result = 0;
4526 _PyImport_AcquireLock();
4527 pid = fork();
4528 if (pid == 0) {
4529 /* child: this clobbers and resets the import lock. */
4530 PyOS_AfterFork();
4531 } else {
4532 /* parent: release the import lock. */
4533 result = _PyImport_ReleaseLock();
4534 }
4535 if (pid == -1)
4536 return posix_error();
4537 if (result < 0) {
4538 /* Don't clobber the OSError if the fork failed. */
4539 PyErr_SetString(PyExc_RuntimeError,
4540 "not holding the import lock");
4541 return NULL;
4542 }
4543 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004544}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004545#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004546
Neal Norwitzb59798b2003-03-21 01:43:31 +00004547/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004548/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4549#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004550#define DEV_PTY_FILE "/dev/ptc"
4551#define HAVE_DEV_PTMX
4552#else
4553#define DEV_PTY_FILE "/dev/ptmx"
4554#endif
4555
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004556#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004557#ifdef HAVE_PTY_H
4558#include <pty.h>
4559#else
4560#ifdef HAVE_LIBUTIL_H
4561#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004562#else
4563#ifdef HAVE_UTIL_H
4564#include <util.h>
4565#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004566#endif /* HAVE_LIBUTIL_H */
4567#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004568#ifdef HAVE_STROPTS_H
4569#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004570#endif
4571#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004572
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004573#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004574PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004575"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004576Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004577
4578static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004579posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004580{
Victor Stinner8c62be82010-05-06 00:08:46 +00004581 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004582#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004583 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004584#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004585#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004586 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004587#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004588 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004589#endif
4590#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004591
Thomas Wouters70c21a12000-07-14 14:28:33 +00004592#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004593 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4594 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004595#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004596 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4597 if (slave_name == NULL)
4598 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004599
Victor Stinner8c62be82010-05-06 00:08:46 +00004600 slave_fd = open(slave_name, O_RDWR);
4601 if (slave_fd < 0)
4602 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004603#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4605 if (master_fd < 0)
4606 return posix_error();
4607 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4608 /* change permission of slave */
4609 if (grantpt(master_fd) < 0) {
4610 PyOS_setsig(SIGCHLD, sig_saved);
4611 return posix_error();
4612 }
4613 /* unlock slave */
4614 if (unlockpt(master_fd) < 0) {
4615 PyOS_setsig(SIGCHLD, sig_saved);
4616 return posix_error();
4617 }
4618 PyOS_setsig(SIGCHLD, sig_saved);
4619 slave_name = ptsname(master_fd); /* get name of slave */
4620 if (slave_name == NULL)
4621 return posix_error();
4622 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4623 if (slave_fd < 0)
4624 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004625#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004626 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4627 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004628#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004629 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004630#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004631#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004632#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004633
Victor Stinner8c62be82010-05-06 00:08:46 +00004634 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004635
Fred Drake8cef4cf2000-06-28 16:40:38 +00004636}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004637#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004638
4639#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004640PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004641"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004642Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4643Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004644To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004645
4646static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004647posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004648{
Victor Stinner8c62be82010-05-06 00:08:46 +00004649 int master_fd = -1, result = 0;
4650 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004651
Victor Stinner8c62be82010-05-06 00:08:46 +00004652 _PyImport_AcquireLock();
4653 pid = forkpty(&master_fd, NULL, NULL, NULL);
4654 if (pid == 0) {
4655 /* child: this clobbers and resets the import lock. */
4656 PyOS_AfterFork();
4657 } else {
4658 /* parent: release the import lock. */
4659 result = _PyImport_ReleaseLock();
4660 }
4661 if (pid == -1)
4662 return posix_error();
4663 if (result < 0) {
4664 /* Don't clobber the OSError if the fork failed. */
4665 PyErr_SetString(PyExc_RuntimeError,
4666 "not holding the import lock");
4667 return NULL;
4668 }
4669 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004670}
4671#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004672
Ross Lagerwall7807c352011-03-17 20:20:30 +02004673
Guido van Rossumad0ee831995-03-01 10:34:45 +00004674#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004675PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004676"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004677Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004678
Barry Warsaw53699e91996-12-10 23:23:01 +00004679static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004680posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004681{
Victor Stinner8c62be82010-05-06 00:08:46 +00004682 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004683}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004684#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004685
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004686
Guido van Rossumad0ee831995-03-01 10:34:45 +00004687#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004688PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004689"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004690Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004691
Barry Warsaw53699e91996-12-10 23:23:01 +00004692static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004693posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004694{
Victor Stinner8c62be82010-05-06 00:08:46 +00004695 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004696}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004697#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004698
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004699
Guido van Rossumad0ee831995-03-01 10:34:45 +00004700#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004701PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004702"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004703Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004704
Barry Warsaw53699e91996-12-10 23:23:01 +00004705static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004706posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004707{
Victor Stinner8c62be82010-05-06 00:08:46 +00004708 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004709}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004710#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004711
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004712
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004713PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004714"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004715Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004716
Barry Warsaw53699e91996-12-10 23:23:01 +00004717static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004718posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004719{
Victor Stinner8c62be82010-05-06 00:08:46 +00004720 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004721}
4722
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02004723#ifdef HAVE_GETGROUPLIST
4724PyDoc_STRVAR(posix_getgrouplist__doc__,
4725"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
4726Returns a list of groups to which a user belongs.\n\n\
4727 user: username to lookup\n\
4728 group: base group id of the user");
4729
4730static PyObject *
4731posix_getgrouplist(PyObject *self, PyObject *args)
4732{
4733#ifdef NGROUPS_MAX
4734#define MAX_GROUPS NGROUPS_MAX
4735#else
4736 /* defined to be 16 on Solaris7, so this should be a small number */
4737#define MAX_GROUPS 64
4738#endif
4739
4740 const char *user;
4741 int i, ngroups;
4742 PyObject *list;
4743#ifdef __APPLE__
4744 int *groups, basegid;
4745#else
4746 gid_t *groups, basegid;
4747#endif
4748 ngroups = MAX_GROUPS;
4749
4750 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
4751 return NULL;
4752
4753#ifdef __APPLE__
4754 groups = PyMem_Malloc(ngroups * sizeof(int));
4755#else
4756 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
4757#endif
4758 if (groups == NULL)
4759 return PyErr_NoMemory();
4760
4761 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
4762 PyMem_Del(groups);
4763 return posix_error();
4764 }
4765
4766 list = PyList_New(ngroups);
4767 if (list == NULL) {
4768 PyMem_Del(groups);
4769 return NULL;
4770 }
4771
4772 for (i = 0; i < ngroups; i++) {
4773 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
4774 if (o == NULL) {
4775 Py_DECREF(list);
4776 PyMem_Del(groups);
4777 return NULL;
4778 }
4779 PyList_SET_ITEM(list, i, o);
4780 }
4781
4782 PyMem_Del(groups);
4783
4784 return list;
4785}
4786#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004787
Fred Drakec9680921999-12-13 16:37:25 +00004788#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004789PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004790"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004791Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004792
4793static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004794posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004795{
4796 PyObject *result = NULL;
4797
Fred Drakec9680921999-12-13 16:37:25 +00004798#ifdef NGROUPS_MAX
4799#define MAX_GROUPS NGROUPS_MAX
4800#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004801 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004802#define MAX_GROUPS 64
4803#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004804 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004805
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004806 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004807 * This is a helper variable to store the intermediate result when
4808 * that happens.
4809 *
4810 * To keep the code readable the OSX behaviour is unconditional,
4811 * according to the POSIX spec this should be safe on all unix-y
4812 * systems.
4813 */
4814 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004815 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004816
Victor Stinner8c62be82010-05-06 00:08:46 +00004817 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004818 if (n < 0) {
4819 if (errno == EINVAL) {
4820 n = getgroups(0, NULL);
4821 if (n == -1) {
4822 return posix_error();
4823 }
4824 if (n == 0) {
4825 /* Avoid malloc(0) */
4826 alt_grouplist = grouplist;
4827 } else {
4828 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4829 if (alt_grouplist == NULL) {
4830 errno = EINVAL;
4831 return posix_error();
4832 }
4833 n = getgroups(n, alt_grouplist);
4834 if (n == -1) {
4835 PyMem_Free(alt_grouplist);
4836 return posix_error();
4837 }
4838 }
4839 } else {
4840 return posix_error();
4841 }
4842 }
4843 result = PyList_New(n);
4844 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004845 int i;
4846 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004847 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004848 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004849 Py_DECREF(result);
4850 result = NULL;
4851 break;
Fred Drakec9680921999-12-13 16:37:25 +00004852 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004853 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004854 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004855 }
4856
4857 if (alt_grouplist != grouplist) {
4858 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004859 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004860
Fred Drakec9680921999-12-13 16:37:25 +00004861 return result;
4862}
4863#endif
4864
Antoine Pitroub7572f02009-12-02 20:46:48 +00004865#ifdef HAVE_INITGROUPS
4866PyDoc_STRVAR(posix_initgroups__doc__,
4867"initgroups(username, gid) -> None\n\n\
4868Call the system initgroups() to initialize the group access list with all of\n\
4869the groups of which the specified username is a member, plus the specified\n\
4870group id.");
4871
4872static PyObject *
4873posix_initgroups(PyObject *self, PyObject *args)
4874{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004875 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004876 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004877 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004878 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004879
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004880 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4881 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004882 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004883 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004884
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004885 res = initgroups(username, (gid_t) gid);
4886 Py_DECREF(oname);
4887 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004888 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004889
Victor Stinner8c62be82010-05-06 00:08:46 +00004890 Py_INCREF(Py_None);
4891 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004892}
4893#endif
4894
Martin v. Löwis606edc12002-06-13 21:09:11 +00004895#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004896PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004897"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004898Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004899
4900static PyObject *
4901posix_getpgid(PyObject *self, PyObject *args)
4902{
Victor Stinner8c62be82010-05-06 00:08:46 +00004903 pid_t pid, pgid;
4904 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4905 return NULL;
4906 pgid = getpgid(pid);
4907 if (pgid < 0)
4908 return posix_error();
4909 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004910}
4911#endif /* HAVE_GETPGID */
4912
4913
Guido van Rossumb6775db1994-08-01 11:34:53 +00004914#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004915PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004916"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004917Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004918
Barry Warsaw53699e91996-12-10 23:23:01 +00004919static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004920posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004921{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004922#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004923 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004924#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004925 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004926#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004927}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004928#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004929
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004930
Guido van Rossumb6775db1994-08-01 11:34:53 +00004931#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004932PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004933"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004934Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004935
Barry Warsaw53699e91996-12-10 23:23:01 +00004936static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004937posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004938{
Guido van Rossum64933891994-10-20 21:56:42 +00004939#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004940 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004941#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004943#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004944 return posix_error();
4945 Py_INCREF(Py_None);
4946 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004947}
4948
Guido van Rossumb6775db1994-08-01 11:34:53 +00004949#endif /* HAVE_SETPGRP */
4950
Guido van Rossumad0ee831995-03-01 10:34:45 +00004951#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004952
4953#ifdef MS_WINDOWS
4954#include <tlhelp32.h>
4955
4956static PyObject*
4957win32_getppid()
4958{
4959 HANDLE snapshot;
4960 pid_t mypid;
4961 PyObject* result = NULL;
4962 BOOL have_record;
4963 PROCESSENTRY32 pe;
4964
4965 mypid = getpid(); /* This function never fails */
4966
4967 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4968 if (snapshot == INVALID_HANDLE_VALUE)
4969 return PyErr_SetFromWindowsErr(GetLastError());
4970
4971 pe.dwSize = sizeof(pe);
4972 have_record = Process32First(snapshot, &pe);
4973 while (have_record) {
4974 if (mypid == (pid_t)pe.th32ProcessID) {
4975 /* We could cache the ulong value in a static variable. */
4976 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4977 break;
4978 }
4979
4980 have_record = Process32Next(snapshot, &pe);
4981 }
4982
4983 /* If our loop exits and our pid was not found (result will be NULL)
4984 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4985 * error anyway, so let's raise it. */
4986 if (!result)
4987 result = PyErr_SetFromWindowsErr(GetLastError());
4988
4989 CloseHandle(snapshot);
4990
4991 return result;
4992}
4993#endif /*MS_WINDOWS*/
4994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004995PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004996"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004997Return the parent's process id. If the parent process has already exited,\n\
4998Windows machines will still return its id; others systems will return the id\n\
4999of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005000
Barry Warsaw53699e91996-12-10 23:23:01 +00005001static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005002posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005003{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005004#ifdef MS_WINDOWS
5005 return win32_getppid();
5006#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005007 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005008#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005009}
5010#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005012
Fred Drake12c6e2d1999-12-14 21:25:03 +00005013#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005014PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005015"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005016Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005017
5018static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005019posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005020{
Victor Stinner8c62be82010-05-06 00:08:46 +00005021 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005022#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005023 wchar_t user_name[UNLEN + 1];
5024 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5025
5026 if (GetUserNameW(user_name, &num_chars)) {
5027 /* num_chars is the number of unicode chars plus null terminator */
5028 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005029 }
5030 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005031 result = PyErr_SetFromWindowsErr(GetLastError());
5032#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005033 char *name;
5034 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005035
Victor Stinner8c62be82010-05-06 00:08:46 +00005036 errno = 0;
5037 name = getlogin();
5038 if (name == NULL) {
5039 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005040 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005041 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005042 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005043 }
5044 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005045 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005046 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005047#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005048 return result;
5049}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005050#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005051
Guido van Rossumad0ee831995-03-01 10:34:45 +00005052#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005053PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005054"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005055Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005056
Barry Warsaw53699e91996-12-10 23:23:01 +00005057static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005058posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005059{
Victor Stinner8c62be82010-05-06 00:08:46 +00005060 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005061}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005062#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005063
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005064
Guido van Rossumad0ee831995-03-01 10:34:45 +00005065#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005066PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005067"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005068Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005069
Barry Warsaw53699e91996-12-10 23:23:01 +00005070static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005071posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005072{
Victor Stinner8c62be82010-05-06 00:08:46 +00005073 pid_t pid;
5074 int sig;
5075 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5076 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005077#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005078 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5079 APIRET rc;
5080 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005081 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005082
5083 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5084 APIRET rc;
5085 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005086 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005087
5088 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005089 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005090#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005091 if (kill(pid, sig) == -1)
5092 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005093#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 Py_INCREF(Py_None);
5095 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005096}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005097#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005098
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005099#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005100PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005101"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005102Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005103
5104static PyObject *
5105posix_killpg(PyObject *self, PyObject *args)
5106{
Victor Stinner8c62be82010-05-06 00:08:46 +00005107 int sig;
5108 pid_t pgid;
5109 /* XXX some man pages make the `pgid` parameter an int, others
5110 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5111 take the same type. Moreover, pid_t is always at least as wide as
5112 int (else compilation of this module fails), which is safe. */
5113 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5114 return NULL;
5115 if (killpg(pgid, sig) == -1)
5116 return posix_error();
5117 Py_INCREF(Py_None);
5118 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005119}
5120#endif
5121
Brian Curtineb24d742010-04-12 17:16:38 +00005122#ifdef MS_WINDOWS
5123PyDoc_STRVAR(win32_kill__doc__,
5124"kill(pid, sig)\n\n\
5125Kill a process with a signal.");
5126
5127static PyObject *
5128win32_kill(PyObject *self, PyObject *args)
5129{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005130 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005131 DWORD pid, sig, err;
5132 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005133
Victor Stinner8c62be82010-05-06 00:08:46 +00005134 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5135 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005136
Victor Stinner8c62be82010-05-06 00:08:46 +00005137 /* Console processes which share a common console can be sent CTRL+C or
5138 CTRL+BREAK events, provided they handle said events. */
5139 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5140 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5141 err = GetLastError();
5142 PyErr_SetFromWindowsErr(err);
5143 }
5144 else
5145 Py_RETURN_NONE;
5146 }
Brian Curtineb24d742010-04-12 17:16:38 +00005147
Victor Stinner8c62be82010-05-06 00:08:46 +00005148 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5149 attempt to open and terminate the process. */
5150 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5151 if (handle == NULL) {
5152 err = GetLastError();
5153 return PyErr_SetFromWindowsErr(err);
5154 }
Brian Curtineb24d742010-04-12 17:16:38 +00005155
Victor Stinner8c62be82010-05-06 00:08:46 +00005156 if (TerminateProcess(handle, sig) == 0) {
5157 err = GetLastError();
5158 result = PyErr_SetFromWindowsErr(err);
5159 } else {
5160 Py_INCREF(Py_None);
5161 result = Py_None;
5162 }
Brian Curtineb24d742010-04-12 17:16:38 +00005163
Victor Stinner8c62be82010-05-06 00:08:46 +00005164 CloseHandle(handle);
5165 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005166}
5167#endif /* MS_WINDOWS */
5168
Guido van Rossumc0125471996-06-28 18:55:32 +00005169#ifdef HAVE_PLOCK
5170
5171#ifdef HAVE_SYS_LOCK_H
5172#include <sys/lock.h>
5173#endif
5174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005175PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005176"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005177Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005178
Barry Warsaw53699e91996-12-10 23:23:01 +00005179static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005180posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005181{
Victor Stinner8c62be82010-05-06 00:08:46 +00005182 int op;
5183 if (!PyArg_ParseTuple(args, "i:plock", &op))
5184 return NULL;
5185 if (plock(op) == -1)
5186 return posix_error();
5187 Py_INCREF(Py_None);
5188 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005189}
5190#endif
5191
Guido van Rossumb6775db1994-08-01 11:34:53 +00005192#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005193PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005194"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005195Set the current process's user id.");
5196
Barry Warsaw53699e91996-12-10 23:23:01 +00005197static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005198posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005199{
Victor Stinner8c62be82010-05-06 00:08:46 +00005200 long uid_arg;
5201 uid_t uid;
5202 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5203 return NULL;
5204 uid = uid_arg;
5205 if (uid != uid_arg) {
5206 PyErr_SetString(PyExc_OverflowError, "user id too big");
5207 return NULL;
5208 }
5209 if (setuid(uid) < 0)
5210 return posix_error();
5211 Py_INCREF(Py_None);
5212 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005213}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005214#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005215
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005216
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005217#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005218PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005219"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005220Set the current process's effective user id.");
5221
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005222static PyObject *
5223posix_seteuid (PyObject *self, PyObject *args)
5224{
Victor Stinner8c62be82010-05-06 00:08:46 +00005225 long euid_arg;
5226 uid_t euid;
5227 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5228 return NULL;
5229 euid = euid_arg;
5230 if (euid != euid_arg) {
5231 PyErr_SetString(PyExc_OverflowError, "user id too big");
5232 return NULL;
5233 }
5234 if (seteuid(euid) < 0) {
5235 return posix_error();
5236 } else {
5237 Py_INCREF(Py_None);
5238 return Py_None;
5239 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005240}
5241#endif /* HAVE_SETEUID */
5242
5243#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005244PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005245"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005246Set the current process's effective group id.");
5247
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005248static PyObject *
5249posix_setegid (PyObject *self, PyObject *args)
5250{
Victor Stinner8c62be82010-05-06 00:08:46 +00005251 long egid_arg;
5252 gid_t egid;
5253 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5254 return NULL;
5255 egid = egid_arg;
5256 if (egid != egid_arg) {
5257 PyErr_SetString(PyExc_OverflowError, "group id too big");
5258 return NULL;
5259 }
5260 if (setegid(egid) < 0) {
5261 return posix_error();
5262 } else {
5263 Py_INCREF(Py_None);
5264 return Py_None;
5265 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005266}
5267#endif /* HAVE_SETEGID */
5268
5269#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005270PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005271"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272Set the current process's real and effective user ids.");
5273
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005274static PyObject *
5275posix_setreuid (PyObject *self, PyObject *args)
5276{
Victor Stinner8c62be82010-05-06 00:08:46 +00005277 long ruid_arg, euid_arg;
5278 uid_t ruid, euid;
5279 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5280 return NULL;
5281 if (ruid_arg == -1)
5282 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5283 else
5284 ruid = ruid_arg; /* otherwise, assign from our long */
5285 if (euid_arg == -1)
5286 euid = (uid_t)-1;
5287 else
5288 euid = euid_arg;
5289 if ((euid_arg != -1 && euid != euid_arg) ||
5290 (ruid_arg != -1 && ruid != ruid_arg)) {
5291 PyErr_SetString(PyExc_OverflowError, "user id too big");
5292 return NULL;
5293 }
5294 if (setreuid(ruid, euid) < 0) {
5295 return posix_error();
5296 } else {
5297 Py_INCREF(Py_None);
5298 return Py_None;
5299 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005300}
5301#endif /* HAVE_SETREUID */
5302
5303#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005304PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005305"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005306Set the current process's real and effective group ids.");
5307
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005308static PyObject *
5309posix_setregid (PyObject *self, PyObject *args)
5310{
Victor Stinner8c62be82010-05-06 00:08:46 +00005311 long rgid_arg, egid_arg;
5312 gid_t rgid, egid;
5313 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5314 return NULL;
5315 if (rgid_arg == -1)
5316 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5317 else
5318 rgid = rgid_arg; /* otherwise, assign from our long */
5319 if (egid_arg == -1)
5320 egid = (gid_t)-1;
5321 else
5322 egid = egid_arg;
5323 if ((egid_arg != -1 && egid != egid_arg) ||
5324 (rgid_arg != -1 && rgid != rgid_arg)) {
5325 PyErr_SetString(PyExc_OverflowError, "group id too big");
5326 return NULL;
5327 }
5328 if (setregid(rgid, egid) < 0) {
5329 return posix_error();
5330 } else {
5331 Py_INCREF(Py_None);
5332 return Py_None;
5333 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005334}
5335#endif /* HAVE_SETREGID */
5336
Guido van Rossumb6775db1994-08-01 11:34:53 +00005337#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005338PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005339"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005340Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005341
Barry Warsaw53699e91996-12-10 23:23:01 +00005342static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005343posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005344{
Victor Stinner8c62be82010-05-06 00:08:46 +00005345 long gid_arg;
5346 gid_t gid;
5347 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5348 return NULL;
5349 gid = gid_arg;
5350 if (gid != gid_arg) {
5351 PyErr_SetString(PyExc_OverflowError, "group id too big");
5352 return NULL;
5353 }
5354 if (setgid(gid) < 0)
5355 return posix_error();
5356 Py_INCREF(Py_None);
5357 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005358}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005359#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005360
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005361#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005362PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005363"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005364Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005365
5366static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005367posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005368{
Victor Stinner8c62be82010-05-06 00:08:46 +00005369 int i, len;
5370 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005371
Victor Stinner8c62be82010-05-06 00:08:46 +00005372 if (!PySequence_Check(groups)) {
5373 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5374 return NULL;
5375 }
5376 len = PySequence_Size(groups);
5377 if (len > MAX_GROUPS) {
5378 PyErr_SetString(PyExc_ValueError, "too many groups");
5379 return NULL;
5380 }
5381 for(i = 0; i < len; i++) {
5382 PyObject *elem;
5383 elem = PySequence_GetItem(groups, i);
5384 if (!elem)
5385 return NULL;
5386 if (!PyLong_Check(elem)) {
5387 PyErr_SetString(PyExc_TypeError,
5388 "groups must be integers");
5389 Py_DECREF(elem);
5390 return NULL;
5391 } else {
5392 unsigned long x = PyLong_AsUnsignedLong(elem);
5393 if (PyErr_Occurred()) {
5394 PyErr_SetString(PyExc_TypeError,
5395 "group id too big");
5396 Py_DECREF(elem);
5397 return NULL;
5398 }
5399 grouplist[i] = x;
5400 /* read back the value to see if it fitted in gid_t */
5401 if (grouplist[i] != x) {
5402 PyErr_SetString(PyExc_TypeError,
5403 "group id too big");
5404 Py_DECREF(elem);
5405 return NULL;
5406 }
5407 }
5408 Py_DECREF(elem);
5409 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005410
Victor Stinner8c62be82010-05-06 00:08:46 +00005411 if (setgroups(len, grouplist) < 0)
5412 return posix_error();
5413 Py_INCREF(Py_None);
5414 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005415}
5416#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005417
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005418#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5419static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005420wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005421{
Victor Stinner8c62be82010-05-06 00:08:46 +00005422 PyObject *result;
5423 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005424
Victor Stinner8c62be82010-05-06 00:08:46 +00005425 if (pid == -1)
5426 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005427
Victor Stinner8c62be82010-05-06 00:08:46 +00005428 if (struct_rusage == NULL) {
5429 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5430 if (m == NULL)
5431 return NULL;
5432 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5433 Py_DECREF(m);
5434 if (struct_rusage == NULL)
5435 return NULL;
5436 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005437
Victor Stinner8c62be82010-05-06 00:08:46 +00005438 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5439 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5440 if (!result)
5441 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005442
5443#ifndef doubletime
5444#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5445#endif
5446
Victor Stinner8c62be82010-05-06 00:08:46 +00005447 PyStructSequence_SET_ITEM(result, 0,
5448 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5449 PyStructSequence_SET_ITEM(result, 1,
5450 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005451#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005452 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5453 SET_INT(result, 2, ru->ru_maxrss);
5454 SET_INT(result, 3, ru->ru_ixrss);
5455 SET_INT(result, 4, ru->ru_idrss);
5456 SET_INT(result, 5, ru->ru_isrss);
5457 SET_INT(result, 6, ru->ru_minflt);
5458 SET_INT(result, 7, ru->ru_majflt);
5459 SET_INT(result, 8, ru->ru_nswap);
5460 SET_INT(result, 9, ru->ru_inblock);
5461 SET_INT(result, 10, ru->ru_oublock);
5462 SET_INT(result, 11, ru->ru_msgsnd);
5463 SET_INT(result, 12, ru->ru_msgrcv);
5464 SET_INT(result, 13, ru->ru_nsignals);
5465 SET_INT(result, 14, ru->ru_nvcsw);
5466 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005467#undef SET_INT
5468
Victor Stinner8c62be82010-05-06 00:08:46 +00005469 if (PyErr_Occurred()) {
5470 Py_DECREF(result);
5471 return NULL;
5472 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005473
Victor Stinner8c62be82010-05-06 00:08:46 +00005474 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005475}
5476#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5477
5478#ifdef HAVE_WAIT3
5479PyDoc_STRVAR(posix_wait3__doc__,
5480"wait3(options) -> (pid, status, rusage)\n\n\
5481Wait for completion of a child process.");
5482
5483static PyObject *
5484posix_wait3(PyObject *self, PyObject *args)
5485{
Victor Stinner8c62be82010-05-06 00:08:46 +00005486 pid_t pid;
5487 int options;
5488 struct rusage ru;
5489 WAIT_TYPE status;
5490 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005491
Victor Stinner8c62be82010-05-06 00:08:46 +00005492 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5493 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005494
Victor Stinner8c62be82010-05-06 00:08:46 +00005495 Py_BEGIN_ALLOW_THREADS
5496 pid = wait3(&status, options, &ru);
5497 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005498
Victor Stinner8c62be82010-05-06 00:08:46 +00005499 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005500}
5501#endif /* HAVE_WAIT3 */
5502
5503#ifdef HAVE_WAIT4
5504PyDoc_STRVAR(posix_wait4__doc__,
5505"wait4(pid, options) -> (pid, status, rusage)\n\n\
5506Wait for completion of a given child process.");
5507
5508static PyObject *
5509posix_wait4(PyObject *self, PyObject *args)
5510{
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 pid_t pid;
5512 int options;
5513 struct rusage ru;
5514 WAIT_TYPE status;
5515 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005516
Victor Stinner8c62be82010-05-06 00:08:46 +00005517 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5518 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005519
Victor Stinner8c62be82010-05-06 00:08:46 +00005520 Py_BEGIN_ALLOW_THREADS
5521 pid = wait4(pid, &status, options, &ru);
5522 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005523
Victor Stinner8c62be82010-05-06 00:08:46 +00005524 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005525}
5526#endif /* HAVE_WAIT4 */
5527
Ross Lagerwall7807c352011-03-17 20:20:30 +02005528#if defined(HAVE_WAITID) && !defined(__APPLE__)
5529PyDoc_STRVAR(posix_waitid__doc__,
5530"waitid(idtype, id, options) -> waitid_result\n\n\
5531Wait for the completion of one or more child processes.\n\n\
5532idtype can be P_PID, P_PGID or P_ALL.\n\
5533id specifies the pid to wait on.\n\
5534options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
5535or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
5536Returns either waitid_result or None if WNOHANG is specified and there are\n\
5537no children in a waitable state.");
5538
5539static PyObject *
5540posix_waitid(PyObject *self, PyObject *args)
5541{
5542 PyObject *result;
5543 idtype_t idtype;
5544 id_t id;
5545 int options, res;
5546 siginfo_t si;
5547 si.si_pid = 0;
5548 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
5549 return NULL;
5550 Py_BEGIN_ALLOW_THREADS
5551 res = waitid(idtype, id, &si, options);
5552 Py_END_ALLOW_THREADS
5553 if (res == -1)
5554 return posix_error();
5555
5556 if (si.si_pid == 0)
5557 Py_RETURN_NONE;
5558
5559 result = PyStructSequence_New(&WaitidResultType);
5560 if (!result)
5561 return NULL;
5562
5563 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
5564 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
5565 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
5566 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
5567 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
5568 if (PyErr_Occurred()) {
5569 Py_DECREF(result);
5570 return NULL;
5571 }
5572
5573 return result;
5574}
5575#endif
5576
Guido van Rossumb6775db1994-08-01 11:34:53 +00005577#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005579"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005581
Barry Warsaw53699e91996-12-10 23:23:01 +00005582static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005583posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005584{
Victor Stinner8c62be82010-05-06 00:08:46 +00005585 pid_t pid;
5586 int options;
5587 WAIT_TYPE status;
5588 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005589
Victor Stinner8c62be82010-05-06 00:08:46 +00005590 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5591 return NULL;
5592 Py_BEGIN_ALLOW_THREADS
5593 pid = waitpid(pid, &status, options);
5594 Py_END_ALLOW_THREADS
5595 if (pid == -1)
5596 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005597
Victor Stinner8c62be82010-05-06 00:08:46 +00005598 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005599}
5600
Tim Petersab034fa2002-02-01 11:27:43 +00005601#elif defined(HAVE_CWAIT)
5602
5603/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005604PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005605"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005606"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005607
5608static PyObject *
5609posix_waitpid(PyObject *self, PyObject *args)
5610{
Victor Stinner8c62be82010-05-06 00:08:46 +00005611 Py_intptr_t pid;
5612 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005613
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5615 return NULL;
5616 Py_BEGIN_ALLOW_THREADS
5617 pid = _cwait(&status, pid, options);
5618 Py_END_ALLOW_THREADS
5619 if (pid == -1)
5620 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005621
Victor Stinner8c62be82010-05-06 00:08:46 +00005622 /* shift the status left a byte so this is more like the POSIX waitpid */
5623 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005624}
5625#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005626
Guido van Rossumad0ee831995-03-01 10:34:45 +00005627#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005628PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005629"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005630Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005631
Barry Warsaw53699e91996-12-10 23:23:01 +00005632static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005633posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005634{
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 pid_t pid;
5636 WAIT_TYPE status;
5637 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005638
Victor Stinner8c62be82010-05-06 00:08:46 +00005639 Py_BEGIN_ALLOW_THREADS
5640 pid = wait(&status);
5641 Py_END_ALLOW_THREADS
5642 if (pid == -1)
5643 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005644
Victor Stinner8c62be82010-05-06 00:08:46 +00005645 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005646}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005647#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005650PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005651"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005652Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005653
Barry Warsaw53699e91996-12-10 23:23:01 +00005654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005655posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005656{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005657#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005658 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005659#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005660#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05005661 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00005662 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005663#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005664 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005665#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005666#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005667}
5668
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005669
Guido van Rossumb6775db1994-08-01 11:34:53 +00005670#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005671PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005672"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005673Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005674
Barry Warsaw53699e91996-12-10 23:23:01 +00005675static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005676posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005677{
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 PyObject* v;
5679 char buf[MAXPATHLEN];
5680 PyObject *opath;
5681 char *path;
5682 int n;
5683 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005684
Victor Stinner8c62be82010-05-06 00:08:46 +00005685 if (!PyArg_ParseTuple(args, "O&:readlink",
5686 PyUnicode_FSConverter, &opath))
5687 return NULL;
5688 path = PyBytes_AsString(opath);
5689 v = PySequence_GetItem(args, 0);
5690 if (v == NULL) {
5691 Py_DECREF(opath);
5692 return NULL;
5693 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005694
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 if (PyUnicode_Check(v)) {
5696 arg_is_unicode = 1;
5697 }
5698 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005699
Victor Stinner8c62be82010-05-06 00:08:46 +00005700 Py_BEGIN_ALLOW_THREADS
5701 n = readlink(path, buf, (int) sizeof buf);
5702 Py_END_ALLOW_THREADS
5703 if (n < 0)
5704 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005705
Victor Stinner8c62be82010-05-06 00:08:46 +00005706 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005707 if (arg_is_unicode)
5708 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5709 else
5710 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005711}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005712#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005713
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005714
Brian Curtin52173d42010-12-02 18:29:18 +00005715#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005716PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005717"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005718Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005719
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005721posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005722{
Victor Stinner8c62be82010-05-06 00:08:46 +00005723 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005724}
5725#endif /* HAVE_SYMLINK */
5726
Brian Curtind40e6f72010-07-08 21:39:08 +00005727#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5728
5729PyDoc_STRVAR(win_readlink__doc__,
5730"readlink(path) -> path\n\n\
5731Return a string representing the path to which the symbolic link points.");
5732
Brian Curtind40e6f72010-07-08 21:39:08 +00005733/* Windows readlink implementation */
5734static PyObject *
5735win_readlink(PyObject *self, PyObject *args)
5736{
5737 wchar_t *path;
5738 DWORD n_bytes_returned;
5739 DWORD io_result;
5740 PyObject *result;
5741 HANDLE reparse_point_handle;
5742
5743 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5744 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5745 wchar_t *print_name;
5746
5747 if (!PyArg_ParseTuple(args,
5748 "u:readlink",
5749 &path))
5750 return NULL;
5751
5752 /* First get a handle to the reparse point */
5753 Py_BEGIN_ALLOW_THREADS
5754 reparse_point_handle = CreateFileW(
5755 path,
5756 0,
5757 0,
5758 0,
5759 OPEN_EXISTING,
5760 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5761 0);
5762 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005763
Brian Curtind40e6f72010-07-08 21:39:08 +00005764 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5765 {
5766 return win32_error_unicode("readlink", path);
5767 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005768
Brian Curtind40e6f72010-07-08 21:39:08 +00005769 Py_BEGIN_ALLOW_THREADS
5770 /* New call DeviceIoControl to read the reparse point */
5771 io_result = DeviceIoControl(
5772 reparse_point_handle,
5773 FSCTL_GET_REPARSE_POINT,
5774 0, 0, /* in buffer */
5775 target_buffer, sizeof(target_buffer),
5776 &n_bytes_returned,
5777 0 /* we're not using OVERLAPPED_IO */
5778 );
5779 CloseHandle(reparse_point_handle);
5780 Py_END_ALLOW_THREADS
5781
5782 if (io_result==0)
5783 {
5784 return win32_error_unicode("readlink", path);
5785 }
5786
5787 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5788 {
5789 PyErr_SetString(PyExc_ValueError,
5790 "not a symbolic link");
5791 return NULL;
5792 }
Brian Curtin74e45612010-07-09 15:58:59 +00005793 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5794 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5795
5796 result = PyUnicode_FromWideChar(print_name,
5797 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005798 return result;
5799}
5800
5801#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5802
Brian Curtin52173d42010-12-02 18:29:18 +00005803#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005804
5805/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5806static int has_CreateSymbolicLinkW = 0;
5807static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5808static int
5809check_CreateSymbolicLinkW()
5810{
5811 HINSTANCE hKernel32;
5812 /* only recheck */
5813 if (has_CreateSymbolicLinkW)
5814 return has_CreateSymbolicLinkW;
5815 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005816 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5817 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005818 if (Py_CreateSymbolicLinkW)
5819 has_CreateSymbolicLinkW = 1;
5820 return has_CreateSymbolicLinkW;
5821}
5822
5823PyDoc_STRVAR(win_symlink__doc__,
5824"symlink(src, dst, target_is_directory=False)\n\n\
5825Create a symbolic link pointing to src named dst.\n\
5826target_is_directory is required if the target is to be interpreted as\n\
5827a directory.\n\
5828This function requires Windows 6.0 or greater, and raises a\n\
5829NotImplementedError otherwise.");
5830
5831static PyObject *
5832win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5833{
5834 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5835 PyObject *src, *dest;
5836 int target_is_directory = 0;
5837 DWORD res;
5838 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005839
Brian Curtind40e6f72010-07-08 21:39:08 +00005840 if (!check_CreateSymbolicLinkW())
5841 {
5842 /* raise NotImplementedError */
5843 return PyErr_Format(PyExc_NotImplementedError,
5844 "CreateSymbolicLinkW not found");
5845 }
5846 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5847 kwlist, &src, &dest, &target_is_directory))
5848 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005849
5850 if (win32_can_symlink == 0)
5851 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5852
Brian Curtind40e6f72010-07-08 21:39:08 +00005853 if (!convert_to_unicode(&src)) { return NULL; }
5854 if (!convert_to_unicode(&dest)) {
5855 Py_DECREF(src);
5856 return NULL;
5857 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005858
Brian Curtind40e6f72010-07-08 21:39:08 +00005859 /* if src is a directory, ensure target_is_directory==1 */
5860 if(
5861 GetFileAttributesExW(
5862 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5863 ))
5864 {
5865 target_is_directory = target_is_directory ||
5866 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5867 }
5868
5869 Py_BEGIN_ALLOW_THREADS
5870 res = Py_CreateSymbolicLinkW(
5871 PyUnicode_AsUnicode(dest),
5872 PyUnicode_AsUnicode(src),
5873 target_is_directory);
5874 Py_END_ALLOW_THREADS
5875 Py_DECREF(src);
5876 Py_DECREF(dest);
5877 if (!res)
5878 {
5879 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5880 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005881
Brian Curtind40e6f72010-07-08 21:39:08 +00005882 Py_INCREF(Py_None);
5883 return Py_None;
5884}
Brian Curtin52173d42010-12-02 18:29:18 +00005885#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005886
5887#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005888#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5889static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005890system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005891{
5892 ULONG value = 0;
5893
5894 Py_BEGIN_ALLOW_THREADS
5895 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5896 Py_END_ALLOW_THREADS
5897
5898 return value;
5899}
5900
5901static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005902posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005903{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005904 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005905 return Py_BuildValue("ddddd",
5906 (double)0 /* t.tms_utime / HZ */,
5907 (double)0 /* t.tms_stime / HZ */,
5908 (double)0 /* t.tms_cutime / HZ */,
5909 (double)0 /* t.tms_cstime / HZ */,
5910 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005911}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005912#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005913#define NEED_TICKS_PER_SECOND
5914static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005915static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005916posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005917{
Victor Stinner8c62be82010-05-06 00:08:46 +00005918 struct tms t;
5919 clock_t c;
5920 errno = 0;
5921 c = times(&t);
5922 if (c == (clock_t) -1)
5923 return posix_error();
5924 return Py_BuildValue("ddddd",
5925 (double)t.tms_utime / ticks_per_second,
5926 (double)t.tms_stime / ticks_per_second,
5927 (double)t.tms_cutime / ticks_per_second,
5928 (double)t.tms_cstime / ticks_per_second,
5929 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005930}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005931#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005932#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005933
5934
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005935#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005936#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005937static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005938posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005939{
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 FILETIME create, exit, kernel, user;
5941 HANDLE hProc;
5942 hProc = GetCurrentProcess();
5943 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5944 /* The fields of a FILETIME structure are the hi and lo part
5945 of a 64-bit value expressed in 100 nanosecond units.
5946 1e7 is one second in such units; 1e-7 the inverse.
5947 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5948 */
5949 return Py_BuildValue(
5950 "ddddd",
5951 (double)(user.dwHighDateTime*429.4967296 +
5952 user.dwLowDateTime*1e-7),
5953 (double)(kernel.dwHighDateTime*429.4967296 +
5954 kernel.dwLowDateTime*1e-7),
5955 (double)0,
5956 (double)0,
5957 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005958}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005959#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005960
5961#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005962PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005963"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005964Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005965#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005966
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005967
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005968#ifdef HAVE_GETSID
5969PyDoc_STRVAR(posix_getsid__doc__,
5970"getsid(pid) -> sid\n\n\
5971Call the system call getsid().");
5972
5973static PyObject *
5974posix_getsid(PyObject *self, PyObject *args)
5975{
Victor Stinner8c62be82010-05-06 00:08:46 +00005976 pid_t pid;
5977 int sid;
5978 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5979 return NULL;
5980 sid = getsid(pid);
5981 if (sid < 0)
5982 return posix_error();
5983 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005984}
5985#endif /* HAVE_GETSID */
5986
5987
Guido van Rossumb6775db1994-08-01 11:34:53 +00005988#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005989PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005990"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005991Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005992
Barry Warsaw53699e91996-12-10 23:23:01 +00005993static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005994posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005995{
Victor Stinner8c62be82010-05-06 00:08:46 +00005996 if (setsid() < 0)
5997 return posix_error();
5998 Py_INCREF(Py_None);
5999 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006000}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006001#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006002
Guido van Rossumb6775db1994-08-01 11:34:53 +00006003#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006004PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006005"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006006Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006007
Barry Warsaw53699e91996-12-10 23:23:01 +00006008static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006009posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006010{
Victor Stinner8c62be82010-05-06 00:08:46 +00006011 pid_t pid;
6012 int pgrp;
6013 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6014 return NULL;
6015 if (setpgid(pid, pgrp) < 0)
6016 return posix_error();
6017 Py_INCREF(Py_None);
6018 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006019}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006020#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006021
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006022
Guido van Rossumb6775db1994-08-01 11:34:53 +00006023#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006024PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006025"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006026Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006027
Barry Warsaw53699e91996-12-10 23:23:01 +00006028static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006029posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006030{
Victor Stinner8c62be82010-05-06 00:08:46 +00006031 int fd;
6032 pid_t pgid;
6033 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6034 return NULL;
6035 pgid = tcgetpgrp(fd);
6036 if (pgid < 0)
6037 return posix_error();
6038 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006039}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006040#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006041
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006042
Guido van Rossumb6775db1994-08-01 11:34:53 +00006043#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006044PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006045"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006046Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006047
Barry Warsaw53699e91996-12-10 23:23:01 +00006048static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006049posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006050{
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 int fd;
6052 pid_t pgid;
6053 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6054 return NULL;
6055 if (tcsetpgrp(fd, pgid) < 0)
6056 return posix_error();
6057 Py_INCREF(Py_None);
6058 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006059}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006060#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006061
Guido van Rossum687dd131993-05-17 08:34:16 +00006062/* Functions acting on file descriptors */
6063
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006064PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006065"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006066Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006067
Barry Warsaw53699e91996-12-10 23:23:01 +00006068static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006069posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006070{
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 PyObject *ofile;
6072 char *file;
6073 int flag;
6074 int mode = 0777;
6075 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006076
6077#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006078 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006079 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 Py_BEGIN_ALLOW_THREADS
6081 /* PyUnicode_AS_UNICODE OK without thread
6082 lock as it is a simple dereference. */
6083 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6084 Py_END_ALLOW_THREADS
6085 if (fd < 0)
6086 return posix_error();
6087 return PyLong_FromLong((long)fd);
6088 }
6089 /* Drop the argument parsing error as narrow strings
6090 are also valid. */
6091 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006092#endif
6093
Victor Stinner26de69d2011-06-17 15:15:38 +02006094 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006095 PyUnicode_FSConverter, &ofile,
6096 &flag, &mode))
6097 return NULL;
6098 file = PyBytes_AsString(ofile);
6099 Py_BEGIN_ALLOW_THREADS
6100 fd = open(file, flag, mode);
6101 Py_END_ALLOW_THREADS
6102 if (fd < 0)
6103 return posix_error_with_allocated_filename(ofile);
6104 Py_DECREF(ofile);
6105 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006106}
6107
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006109PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006110"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006111Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006112
Barry Warsaw53699e91996-12-10 23:23:01 +00006113static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006114posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006115{
Victor Stinner8c62be82010-05-06 00:08:46 +00006116 int fd, res;
6117 if (!PyArg_ParseTuple(args, "i:close", &fd))
6118 return NULL;
6119 if (!_PyVerify_fd(fd))
6120 return posix_error();
6121 Py_BEGIN_ALLOW_THREADS
6122 res = close(fd);
6123 Py_END_ALLOW_THREADS
6124 if (res < 0)
6125 return posix_error();
6126 Py_INCREF(Py_None);
6127 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006128}
6129
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006130
Victor Stinner8c62be82010-05-06 00:08:46 +00006131PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006132"closerange(fd_low, fd_high)\n\n\
6133Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6134
6135static PyObject *
6136posix_closerange(PyObject *self, PyObject *args)
6137{
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 int fd_from, fd_to, i;
6139 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6140 return NULL;
6141 Py_BEGIN_ALLOW_THREADS
6142 for (i = fd_from; i < fd_to; i++)
6143 if (_PyVerify_fd(i))
6144 close(i);
6145 Py_END_ALLOW_THREADS
6146 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006147}
6148
6149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006150PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006151"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006152Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006153
Barry Warsaw53699e91996-12-10 23:23:01 +00006154static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006155posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006156{
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 int fd;
6158 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6159 return NULL;
6160 if (!_PyVerify_fd(fd))
6161 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006162 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 if (fd < 0)
6164 return posix_error();
6165 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006166}
6167
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006169PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006170"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006172
Barry Warsaw53699e91996-12-10 23:23:01 +00006173static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006174posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006175{
Victor Stinner8c62be82010-05-06 00:08:46 +00006176 int fd, fd2, res;
6177 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6178 return NULL;
6179 if (!_PyVerify_fd_dup2(fd, fd2))
6180 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 if (res < 0)
6183 return posix_error();
6184 Py_INCREF(Py_None);
6185 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006186}
6187
Ross Lagerwall7807c352011-03-17 20:20:30 +02006188#ifdef HAVE_LOCKF
6189PyDoc_STRVAR(posix_lockf__doc__,
6190"lockf(fd, cmd, len)\n\n\
6191Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6192fd is an open file descriptor.\n\
6193cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6194F_TEST.\n\
6195len specifies the section of the file to lock.");
6196
6197static PyObject *
6198posix_lockf(PyObject *self, PyObject *args)
6199{
6200 int fd, cmd, res;
6201 off_t len;
6202 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6203 &fd, &cmd, _parse_off_t, &len))
6204 return NULL;
6205
6206 Py_BEGIN_ALLOW_THREADS
6207 res = lockf(fd, cmd, len);
6208 Py_END_ALLOW_THREADS
6209
6210 if (res < 0)
6211 return posix_error();
6212
6213 Py_RETURN_NONE;
6214}
6215#endif
6216
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006218PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006219"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006220Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006221
Barry Warsaw53699e91996-12-10 23:23:01 +00006222static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006223posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006224{
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006226#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006228#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006229 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006230#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006231 PyObject *posobj;
6232 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006233 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006234#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006235 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6236 switch (how) {
6237 case 0: how = SEEK_SET; break;
6238 case 1: how = SEEK_CUR; break;
6239 case 2: how = SEEK_END; break;
6240 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006241#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006242
Ross Lagerwall8e749672011-03-17 21:54:07 +02006243#if !defined(HAVE_LARGEFILE_SUPPORT)
6244 pos = PyLong_AsLong(posobj);
6245#else
6246 pos = PyLong_AsLongLong(posobj);
6247#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006248 if (PyErr_Occurred())
6249 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006250
Victor Stinner8c62be82010-05-06 00:08:46 +00006251 if (!_PyVerify_fd(fd))
6252 return posix_error();
6253 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006254#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006255 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006256#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006257 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006258#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006259 Py_END_ALLOW_THREADS
6260 if (res < 0)
6261 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006262
6263#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006265#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006267#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006268}
6269
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006271PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006272"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006273Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006274
Barry Warsaw53699e91996-12-10 23:23:01 +00006275static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006276posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006277{
Victor Stinner8c62be82010-05-06 00:08:46 +00006278 int fd, size;
6279 Py_ssize_t n;
6280 PyObject *buffer;
6281 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6282 return NULL;
6283 if (size < 0) {
6284 errno = EINVAL;
6285 return posix_error();
6286 }
6287 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6288 if (buffer == NULL)
6289 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006290 if (!_PyVerify_fd(fd)) {
6291 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006293 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 Py_BEGIN_ALLOW_THREADS
6295 n = read(fd, PyBytes_AS_STRING(buffer), size);
6296 Py_END_ALLOW_THREADS
6297 if (n < 0) {
6298 Py_DECREF(buffer);
6299 return posix_error();
6300 }
6301 if (n != size)
6302 _PyBytes_Resize(&buffer, n);
6303 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006304}
6305
Ross Lagerwall7807c352011-03-17 20:20:30 +02006306#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6307 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006308static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006309iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6310{
6311 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006312 Py_ssize_t blen, total = 0;
6313
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006314 *iov = PyMem_New(struct iovec, cnt);
6315 if (*iov == NULL) {
6316 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006317 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006318 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006319
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006320 *buf = PyMem_New(Py_buffer, cnt);
6321 if (*buf == NULL) {
6322 PyMem_Del(*iov);
6323 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006324 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006325 }
6326
6327 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006328 PyObject *item = PySequence_GetItem(seq, i);
6329 if (item == NULL)
6330 goto fail;
6331 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6332 Py_DECREF(item);
6333 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006334 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006335 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006336 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006337 blen = (*buf)[i].len;
6338 (*iov)[i].iov_len = blen;
6339 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006340 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006341 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006342
6343fail:
6344 PyMem_Del(*iov);
6345 for (j = 0; j < i; j++) {
6346 PyBuffer_Release(&(*buf)[j]);
6347 }
6348 PyMem_Del(*buf);
6349 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006350}
6351
6352static void
6353iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6354{
6355 int i;
6356 PyMem_Del(iov);
6357 for (i = 0; i < cnt; i++) {
6358 PyBuffer_Release(&buf[i]);
6359 }
6360 PyMem_Del(buf);
6361}
6362#endif
6363
Ross Lagerwall7807c352011-03-17 20:20:30 +02006364#ifdef HAVE_READV
6365PyDoc_STRVAR(posix_readv__doc__,
6366"readv(fd, buffers) -> bytesread\n\n\
6367Read from a file descriptor into a number of writable buffers. buffers\n\
6368is an arbitrary sequence of writable buffers.\n\
6369Returns the total number of bytes read.");
6370
6371static PyObject *
6372posix_readv(PyObject *self, PyObject *args)
6373{
6374 int fd, cnt;
6375 Py_ssize_t n;
6376 PyObject *seq;
6377 struct iovec *iov;
6378 Py_buffer *buf;
6379
6380 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6381 return NULL;
6382 if (!PySequence_Check(seq)) {
6383 PyErr_SetString(PyExc_TypeError,
6384 "readv() arg 2 must be a sequence");
6385 return NULL;
6386 }
6387 cnt = PySequence_Size(seq);
6388
6389 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6390 return NULL;
6391
6392 Py_BEGIN_ALLOW_THREADS
6393 n = readv(fd, iov, cnt);
6394 Py_END_ALLOW_THREADS
6395
6396 iov_cleanup(iov, buf, cnt);
6397 return PyLong_FromSsize_t(n);
6398}
6399#endif
6400
6401#ifdef HAVE_PREAD
6402PyDoc_STRVAR(posix_pread__doc__,
6403"pread(fd, buffersize, offset) -> string\n\n\
6404Read from a file descriptor, fd, at a position of offset. It will read up\n\
6405to buffersize number of bytes. The file offset remains unchanged.");
6406
6407static PyObject *
6408posix_pread(PyObject *self, PyObject *args)
6409{
6410 int fd, size;
6411 off_t offset;
6412 Py_ssize_t n;
6413 PyObject *buffer;
6414 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6415 return NULL;
6416
6417 if (size < 0) {
6418 errno = EINVAL;
6419 return posix_error();
6420 }
6421 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6422 if (buffer == NULL)
6423 return NULL;
6424 if (!_PyVerify_fd(fd)) {
6425 Py_DECREF(buffer);
6426 return posix_error();
6427 }
6428 Py_BEGIN_ALLOW_THREADS
6429 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6430 Py_END_ALLOW_THREADS
6431 if (n < 0) {
6432 Py_DECREF(buffer);
6433 return posix_error();
6434 }
6435 if (n != size)
6436 _PyBytes_Resize(&buffer, n);
6437 return buffer;
6438}
6439#endif
6440
6441PyDoc_STRVAR(posix_write__doc__,
6442"write(fd, string) -> byteswritten\n\n\
6443Write a string to a file descriptor.");
6444
6445static PyObject *
6446posix_write(PyObject *self, PyObject *args)
6447{
6448 Py_buffer pbuf;
6449 int fd;
6450 Py_ssize_t size, len;
6451
6452 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
6453 return NULL;
6454 if (!_PyVerify_fd(fd)) {
6455 PyBuffer_Release(&pbuf);
6456 return posix_error();
6457 }
6458 len = pbuf.len;
6459 Py_BEGIN_ALLOW_THREADS
6460#if defined(MS_WIN64) || defined(MS_WINDOWS)
6461 if (len > INT_MAX)
6462 len = INT_MAX;
6463 size = write(fd, pbuf.buf, (int)len);
6464#else
6465 size = write(fd, pbuf.buf, len);
6466#endif
6467 Py_END_ALLOW_THREADS
6468 PyBuffer_Release(&pbuf);
6469 if (size < 0)
6470 return posix_error();
6471 return PyLong_FromSsize_t(size);
6472}
6473
6474#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006475PyDoc_STRVAR(posix_sendfile__doc__,
6476"sendfile(out, in, offset, nbytes) -> byteswritten\n\
6477sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
6478 -> byteswritten\n\
6479Copy nbytes bytes from file descriptor in to file descriptor out.");
6480
6481static PyObject *
6482posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
6483{
6484 int in, out;
6485 Py_ssize_t ret;
6486 off_t offset;
6487
6488#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
6489#ifndef __APPLE__
6490 Py_ssize_t len;
6491#endif
6492 PyObject *headers = NULL, *trailers = NULL;
6493 Py_buffer *hbuf, *tbuf;
6494 off_t sbytes;
6495 struct sf_hdtr sf;
6496 int flags = 0;
6497 sf.headers = NULL;
6498 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00006499 static char *keywords[] = {"out", "in",
6500 "offset", "count",
6501 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006502
6503#ifdef __APPLE__
6504 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006505 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006506#else
6507 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006508 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006509#endif
6510 &headers, &trailers, &flags))
6511 return NULL;
6512 if (headers != NULL) {
6513 if (!PySequence_Check(headers)) {
6514 PyErr_SetString(PyExc_TypeError,
6515 "sendfile() headers must be a sequence or None");
6516 return NULL;
6517 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006518 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006519 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006520 if (sf.hdr_cnt > 0 &&
6521 !(i = iov_setup(&(sf.headers), &hbuf,
6522 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006523 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006524#ifdef __APPLE__
6525 sbytes += i;
6526#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006527 }
6528 }
6529 if (trailers != NULL) {
6530 if (!PySequence_Check(trailers)) {
6531 PyErr_SetString(PyExc_TypeError,
6532 "sendfile() trailers must be a sequence or None");
6533 return NULL;
6534 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006535 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006536 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006537 if (sf.trl_cnt > 0 &&
6538 !(i = iov_setup(&(sf.trailers), &tbuf,
6539 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006540 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006541#ifdef __APPLE__
6542 sbytes += i;
6543#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006544 }
6545 }
6546
6547 Py_BEGIN_ALLOW_THREADS
6548#ifdef __APPLE__
6549 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
6550#else
6551 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
6552#endif
6553 Py_END_ALLOW_THREADS
6554
6555 if (sf.headers != NULL)
6556 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
6557 if (sf.trailers != NULL)
6558 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
6559
6560 if (ret < 0) {
6561 if ((errno == EAGAIN) || (errno == EBUSY)) {
6562 if (sbytes != 0) {
6563 // some data has been sent
6564 goto done;
6565 }
6566 else {
6567 // no data has been sent; upper application is supposed
6568 // to retry on EAGAIN or EBUSY
6569 return posix_error();
6570 }
6571 }
6572 return posix_error();
6573 }
6574 goto done;
6575
6576done:
6577 #if !defined(HAVE_LARGEFILE_SUPPORT)
6578 return Py_BuildValue("l", sbytes);
6579 #else
6580 return Py_BuildValue("L", sbytes);
6581 #endif
6582
6583#else
6584 Py_ssize_t count;
6585 PyObject *offobj;
6586 static char *keywords[] = {"out", "in",
6587 "offset", "count", NULL};
6588 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
6589 keywords, &out, &in, &offobj, &count))
6590 return NULL;
6591#ifdef linux
6592 if (offobj == Py_None) {
6593 Py_BEGIN_ALLOW_THREADS
6594 ret = sendfile(out, in, NULL, count);
6595 Py_END_ALLOW_THREADS
6596 if (ret < 0)
6597 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02006598 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006599 }
6600#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00006601 if (!_parse_off_t(offobj, &offset))
6602 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006603 Py_BEGIN_ALLOW_THREADS
6604 ret = sendfile(out, in, &offset, count);
6605 Py_END_ALLOW_THREADS
6606 if (ret < 0)
6607 return posix_error();
6608 return Py_BuildValue("n", ret);
6609#endif
6610}
6611#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006613PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006614"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006615Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006616
Barry Warsaw53699e91996-12-10 23:23:01 +00006617static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006618posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006619{
Victor Stinner8c62be82010-05-06 00:08:46 +00006620 int fd;
6621 STRUCT_STAT st;
6622 int res;
6623 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6624 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006625#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00006626 /* on OpenVMS we must ensure that all bytes are written to the file */
6627 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006629 if (!_PyVerify_fd(fd))
6630 return posix_error();
6631 Py_BEGIN_ALLOW_THREADS
6632 res = FSTAT(fd, &st);
6633 Py_END_ALLOW_THREADS
6634 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006635#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006637#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006638 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006639#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 }
Tim Peters5aa91602002-01-30 05:46:57 +00006641
Victor Stinner8c62be82010-05-06 00:08:46 +00006642 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006643}
6644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006645PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006646"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006647Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006648connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006649
6650static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006651posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006652{
Victor Stinner8c62be82010-05-06 00:08:46 +00006653 int fd;
6654 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6655 return NULL;
6656 if (!_PyVerify_fd(fd))
6657 return PyBool_FromLong(0);
6658 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006659}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006660
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006661#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006662PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006663"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006664Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006665
Barry Warsaw53699e91996-12-10 23:23:01 +00006666static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006667posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006668{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006669#if defined(PYOS_OS2)
6670 HFILE read, write;
6671 APIRET rc;
6672
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006673 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006674 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006675 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006676
6677 return Py_BuildValue("(ii)", read, write);
6678#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006679#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006680 int fds[2];
6681 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 if (res != 0)
6684 return posix_error();
6685 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006686#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00006687 HANDLE read, write;
6688 int read_fd, write_fd;
6689 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00006690 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 if (!ok)
6692 return win32_error("CreatePipe", NULL);
6693 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6694 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6695 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006696#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006697#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006698}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006699#endif /* HAVE_PIPE */
6700
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006701#ifdef HAVE_PIPE2
6702PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02006703"pipe2(flags) -> (read_end, write_end)\n\n\
6704Create a pipe with flags set atomically.\n\
6705flags can be constructed by ORing together one or more of these values:\n\
6706O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006707");
6708
6709static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02006710posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006711{
Charles-François Natali368f34b2011-06-06 19:49:47 +02006712 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006713 int fds[2];
6714 int res;
6715
Charles-François Natali368f34b2011-06-06 19:49:47 +02006716 flags = PyLong_AsLong(arg);
6717 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006718 return NULL;
6719
6720 res = pipe2(fds, flags);
6721 if (res != 0)
6722 return posix_error();
6723 return Py_BuildValue("(ii)", fds[0], fds[1]);
6724}
6725#endif /* HAVE_PIPE2 */
6726
Ross Lagerwall7807c352011-03-17 20:20:30 +02006727#ifdef HAVE_WRITEV
6728PyDoc_STRVAR(posix_writev__doc__,
6729"writev(fd, buffers) -> byteswritten\n\n\
6730Write the contents of buffers to a file descriptor, where buffers is an\n\
6731arbitrary sequence of buffers.\n\
6732Returns the total bytes written.");
6733
6734static PyObject *
6735posix_writev(PyObject *self, PyObject *args)
6736{
6737 int fd, cnt;
6738 Py_ssize_t res;
6739 PyObject *seq;
6740 struct iovec *iov;
6741 Py_buffer *buf;
6742 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
6743 return NULL;
6744 if (!PySequence_Check(seq)) {
6745 PyErr_SetString(PyExc_TypeError,
6746 "writev() arg 2 must be a sequence");
6747 return NULL;
6748 }
6749 cnt = PySequence_Size(seq);
6750
6751 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
6752 return NULL;
6753 }
6754
6755 Py_BEGIN_ALLOW_THREADS
6756 res = writev(fd, iov, cnt);
6757 Py_END_ALLOW_THREADS
6758
6759 iov_cleanup(iov, buf, cnt);
6760 return PyLong_FromSsize_t(res);
6761}
6762#endif
6763
6764#ifdef HAVE_PWRITE
6765PyDoc_STRVAR(posix_pwrite__doc__,
6766"pwrite(fd, string, offset) -> byteswritten\n\n\
6767Write string to a file descriptor, fd, from offset, leaving the file\n\
6768offset unchanged.");
6769
6770static PyObject *
6771posix_pwrite(PyObject *self, PyObject *args)
6772{
6773 Py_buffer pbuf;
6774 int fd;
6775 off_t offset;
6776 Py_ssize_t size;
6777
6778 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
6779 return NULL;
6780
6781 if (!_PyVerify_fd(fd)) {
6782 PyBuffer_Release(&pbuf);
6783 return posix_error();
6784 }
6785 Py_BEGIN_ALLOW_THREADS
6786 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
6787 Py_END_ALLOW_THREADS
6788 PyBuffer_Release(&pbuf);
6789 if (size < 0)
6790 return posix_error();
6791 return PyLong_FromSsize_t(size);
6792}
6793#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006794
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006795#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006796PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006797"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006798Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006799
Barry Warsaw53699e91996-12-10 23:23:01 +00006800static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006801posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006802{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006803 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 char *filename;
6805 int mode = 0666;
6806 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006807 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6808 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006810 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 Py_BEGIN_ALLOW_THREADS
6812 res = mkfifo(filename, mode);
6813 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006814 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006815 if (res < 0)
6816 return posix_error();
6817 Py_INCREF(Py_None);
6818 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006819}
6820#endif
6821
6822
Neal Norwitz11690112002-07-30 01:08:28 +00006823#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006824PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006825"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006826Create a filesystem node (file, device special file or named pipe)\n\
6827named filename. mode specifies both the permissions to use and the\n\
6828type of node to be created, being combined (bitwise OR) with one of\n\
6829S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006830device defines the newly created device special file (probably using\n\
6831os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006832
6833
6834static PyObject *
6835posix_mknod(PyObject *self, PyObject *args)
6836{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006837 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 char *filename;
6839 int mode = 0600;
6840 int device = 0;
6841 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006842 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6843 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006845 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 Py_BEGIN_ALLOW_THREADS
6847 res = mknod(filename, mode, device);
6848 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006849 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 if (res < 0)
6851 return posix_error();
6852 Py_INCREF(Py_None);
6853 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006854}
6855#endif
6856
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006857#ifdef HAVE_DEVICE_MACROS
6858PyDoc_STRVAR(posix_major__doc__,
6859"major(device) -> major number\n\
6860Extracts a device major number from a raw device number.");
6861
6862static PyObject *
6863posix_major(PyObject *self, PyObject *args)
6864{
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 int device;
6866 if (!PyArg_ParseTuple(args, "i:major", &device))
6867 return NULL;
6868 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006869}
6870
6871PyDoc_STRVAR(posix_minor__doc__,
6872"minor(device) -> minor number\n\
6873Extracts a device minor number from a raw device number.");
6874
6875static PyObject *
6876posix_minor(PyObject *self, PyObject *args)
6877{
Victor Stinner8c62be82010-05-06 00:08:46 +00006878 int device;
6879 if (!PyArg_ParseTuple(args, "i:minor", &device))
6880 return NULL;
6881 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006882}
6883
6884PyDoc_STRVAR(posix_makedev__doc__,
6885"makedev(major, minor) -> device number\n\
6886Composes a raw device number from the major and minor device numbers.");
6887
6888static PyObject *
6889posix_makedev(PyObject *self, PyObject *args)
6890{
Victor Stinner8c62be82010-05-06 00:08:46 +00006891 int major, minor;
6892 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6893 return NULL;
6894 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006895}
6896#endif /* device macros */
6897
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006898
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006899#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006900PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006901"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006902Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006903
Barry Warsaw53699e91996-12-10 23:23:01 +00006904static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006905posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006906{
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 int fd;
6908 off_t length;
6909 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006910
Ross Lagerwall7807c352011-03-17 20:20:30 +02006911 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006913
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 Py_BEGIN_ALLOW_THREADS
6915 res = ftruncate(fd, length);
6916 Py_END_ALLOW_THREADS
6917 if (res < 0)
6918 return posix_error();
6919 Py_INCREF(Py_None);
6920 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006921}
6922#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006923
Ross Lagerwall7807c352011-03-17 20:20:30 +02006924#ifdef HAVE_TRUNCATE
6925PyDoc_STRVAR(posix_truncate__doc__,
6926"truncate(path, length)\n\n\
6927Truncate the file given by path to length bytes.");
6928
6929static PyObject *
6930posix_truncate(PyObject *self, PyObject *args)
6931{
6932 PyObject *opath;
6933 const char *path;
6934 off_t length;
6935 int res;
6936
6937 if (!PyArg_ParseTuple(args, "O&O&:truncate",
6938 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
6939 return NULL;
6940 path = PyBytes_AsString(opath);
6941
6942 Py_BEGIN_ALLOW_THREADS
6943 res = truncate(path, length);
6944 Py_END_ALLOW_THREADS
6945 Py_DECREF(opath);
6946 if (res < 0)
6947 return posix_error();
6948 Py_RETURN_NONE;
6949}
6950#endif
6951
6952#ifdef HAVE_POSIX_FALLOCATE
6953PyDoc_STRVAR(posix_posix_fallocate__doc__,
6954"posix_fallocate(fd, offset, len)\n\n\
6955Ensures that enough disk space is allocated for the file specified by fd\n\
6956starting from offset and continuing for len bytes.");
6957
6958static PyObject *
6959posix_posix_fallocate(PyObject *self, PyObject *args)
6960{
6961 off_t len, offset;
6962 int res, fd;
6963
6964 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
6965 &fd, _parse_off_t, &offset, _parse_off_t, &len))
6966 return NULL;
6967
6968 Py_BEGIN_ALLOW_THREADS
6969 res = posix_fallocate(fd, offset, len);
6970 Py_END_ALLOW_THREADS
6971 if (res != 0) {
6972 errno = res;
6973 return posix_error();
6974 }
6975 Py_RETURN_NONE;
6976}
6977#endif
6978
6979#ifdef HAVE_POSIX_FADVISE
6980PyDoc_STRVAR(posix_posix_fadvise__doc__,
6981"posix_fadvise(fd, offset, len, advice)\n\n\
6982Announces an intention to access data in a specific pattern thus allowing\n\
6983the kernel to make optimizations.\n\
6984The advice applies to the region of the file specified by fd starting at\n\
6985offset and continuing for len bytes.\n\
6986advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
6987POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
6988POSIX_FADV_DONTNEED.");
6989
6990static PyObject *
6991posix_posix_fadvise(PyObject *self, PyObject *args)
6992{
6993 off_t len, offset;
6994 int res, fd, advice;
6995
6996 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
6997 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
6998 return NULL;
6999
7000 Py_BEGIN_ALLOW_THREADS
7001 res = posix_fadvise(fd, offset, len, advice);
7002 Py_END_ALLOW_THREADS
7003 if (res != 0) {
7004 errno = res;
7005 return posix_error();
7006 }
7007 Py_RETURN_NONE;
7008}
7009#endif
7010
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007011#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007012PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007013"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007014Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007015
Fred Drake762e2061999-08-26 17:23:54 +00007016/* Save putenv() parameters as values here, so we can collect them when they
7017 * get re-set with another call for the same key. */
7018static PyObject *posix_putenv_garbage;
7019
Tim Peters5aa91602002-01-30 05:46:57 +00007020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007021posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007022{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007023#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 wchar_t *s1, *s2;
7025 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007026#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 PyObject *os1, *os2;
7028 char *s1, *s2;
7029 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007030#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007031 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007033
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007034#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007035 if (!PyArg_ParseTuple(args,
7036 "uu:putenv",
7037 &s1, &s2))
7038 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007039#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007040 if (!PyArg_ParseTuple(args,
7041 "O&O&:putenv",
7042 PyUnicode_FSConverter, &os1,
7043 PyUnicode_FSConverter, &os2))
7044 return NULL;
7045 s1 = PyBytes_AsString(os1);
7046 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007047#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007048
7049#if defined(PYOS_OS2)
7050 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7051 APIRET rc;
7052
Guido van Rossumd48f2521997-12-05 22:19:34 +00007053 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007054 if (rc != NO_ERROR) {
7055 os2_error(rc);
7056 goto error;
7057 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007058
7059 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7060 APIRET rc;
7061
Guido van Rossumd48f2521997-12-05 22:19:34 +00007062 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007063 if (rc != NO_ERROR) {
7064 os2_error(rc);
7065 goto error;
7066 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007067 } else {
7068#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 /* XXX This can leak memory -- not easy to fix :-( */
7070 /* len includes space for a trailing \0; the size arg to
7071 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007072#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007073 len = wcslen(s1) + wcslen(s2) + 2;
7074 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007075#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007076 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007078#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007079 if (newstr == NULL) {
7080 PyErr_NoMemory();
7081 goto error;
7082 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007083#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 newenv = PyUnicode_AsUnicode(newstr);
7085 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7086 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007088 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007090#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007091 newenv = PyBytes_AS_STRING(newstr);
7092 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7093 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007095 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007097#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007098
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 /* Install the first arg and newstr in posix_putenv_garbage;
7100 * this will cause previous value to be collected. This has to
7101 * happen after the real putenv() call because the old value
7102 * was still accessible until then. */
7103 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007104#ifdef MS_WINDOWS
7105 PyTuple_GET_ITEM(args, 0),
7106#else
7107 os1,
7108#endif
7109 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 /* really not much we can do; just leak */
7111 PyErr_Clear();
7112 }
7113 else {
7114 Py_DECREF(newstr);
7115 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007116
7117#if defined(PYOS_OS2)
7118 }
7119#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007120
Martin v. Löwis011e8422009-05-05 04:43:17 +00007121#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 Py_DECREF(os1);
7123 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007124#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007125 Py_RETURN_NONE;
7126
7127error:
7128#ifndef MS_WINDOWS
7129 Py_DECREF(os1);
7130 Py_DECREF(os2);
7131#endif
7132 Py_XDECREF(newstr);
7133 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007134}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007135#endif /* putenv */
7136
Guido van Rossumc524d952001-10-19 01:31:59 +00007137#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007138PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007139"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007140Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007141
7142static PyObject *
7143posix_unsetenv(PyObject *self, PyObject *args)
7144{
Victor Stinner84ae1182010-05-06 22:05:07 +00007145#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007147
Victor Stinner8c62be82010-05-06 00:08:46 +00007148 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7149 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007150#else
7151 PyObject *os1;
7152 char *s1;
7153
7154 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7155 PyUnicode_FSConverter, &os1))
7156 return NULL;
7157 s1 = PyBytes_AsString(os1);
7158#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007159
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007161
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 /* Remove the key from posix_putenv_garbage;
7163 * this will cause it to be collected. This has to
7164 * happen after the real unsetenv() call because the
7165 * old value was still accessible until then.
7166 */
7167 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007168#ifdef MS_WINDOWS
7169 PyTuple_GET_ITEM(args, 0)
7170#else
7171 os1
7172#endif
7173 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 /* really not much we can do; just leak */
7175 PyErr_Clear();
7176 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007177
Victor Stinner84ae1182010-05-06 22:05:07 +00007178#ifndef MS_WINDOWS
7179 Py_DECREF(os1);
7180#endif
7181 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007182}
7183#endif /* unsetenv */
7184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007185PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007186"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007187Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007188
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007189static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007190posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007191{
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 int code;
7193 char *message;
7194 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7195 return NULL;
7196 message = strerror(code);
7197 if (message == NULL) {
7198 PyErr_SetString(PyExc_ValueError,
7199 "strerror() argument out of range");
7200 return NULL;
7201 }
7202 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007203}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007204
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007205
Guido van Rossumc9641791998-08-04 15:26:23 +00007206#ifdef HAVE_SYS_WAIT_H
7207
Fred Drake106c1a02002-04-23 15:58:02 +00007208#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007209PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007210"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007211Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007212
7213static PyObject *
7214posix_WCOREDUMP(PyObject *self, PyObject *args)
7215{
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 WAIT_TYPE status;
7217 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007218
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7220 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007221
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007223}
7224#endif /* WCOREDUMP */
7225
7226#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007227PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007228"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007229Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007230job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007231
7232static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007233posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007234{
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 WAIT_TYPE status;
7236 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007237
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7239 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007240
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007242}
7243#endif /* WIFCONTINUED */
7244
Guido van Rossumc9641791998-08-04 15:26:23 +00007245#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007246PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007247"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007248Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007249
7250static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007251posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007252{
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 WAIT_TYPE status;
7254 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007255
Victor Stinner8c62be82010-05-06 00:08:46 +00007256 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7257 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007258
Victor Stinner8c62be82010-05-06 00:08:46 +00007259 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007260}
7261#endif /* WIFSTOPPED */
7262
7263#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007264PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007265"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007266Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007267
7268static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007269posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007270{
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 WAIT_TYPE status;
7272 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007273
Victor Stinner8c62be82010-05-06 00:08:46 +00007274 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7275 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007276
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007278}
7279#endif /* WIFSIGNALED */
7280
7281#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007282PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007283"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007284Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007285system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007286
7287static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007288posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007289{
Victor Stinner8c62be82010-05-06 00:08:46 +00007290 WAIT_TYPE status;
7291 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007292
Victor Stinner8c62be82010-05-06 00:08:46 +00007293 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7294 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007295
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007297}
7298#endif /* WIFEXITED */
7299
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007300#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007301PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007302"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007303Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007304
7305static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007306posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007307{
Victor Stinner8c62be82010-05-06 00:08:46 +00007308 WAIT_TYPE status;
7309 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007310
Victor Stinner8c62be82010-05-06 00:08:46 +00007311 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7312 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007313
Victor Stinner8c62be82010-05-06 00:08:46 +00007314 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007315}
7316#endif /* WEXITSTATUS */
7317
7318#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007319PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007320"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007321Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007322value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007323
7324static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007325posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007326{
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 WAIT_TYPE status;
7328 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007329
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7331 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007332
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007334}
7335#endif /* WTERMSIG */
7336
7337#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007338PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007339"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007340Return the signal that stopped the process that provided\n\
7341the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007342
7343static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007344posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007345{
Victor Stinner8c62be82010-05-06 00:08:46 +00007346 WAIT_TYPE status;
7347 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007348
Victor Stinner8c62be82010-05-06 00:08:46 +00007349 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7350 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007351
Victor Stinner8c62be82010-05-06 00:08:46 +00007352 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007353}
7354#endif /* WSTOPSIG */
7355
7356#endif /* HAVE_SYS_WAIT_H */
7357
7358
Thomas Wouters477c8d52006-05-27 19:21:47 +00007359#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007360#ifdef _SCO_DS
7361/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7362 needed definitions in sys/statvfs.h */
7363#define _SVID3
7364#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007365#include <sys/statvfs.h>
7366
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007367static PyObject*
7368_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7370 if (v == NULL)
7371 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007372
7373#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007374 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7375 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7376 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7377 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7378 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7379 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7380 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7381 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7382 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7383 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007384#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7386 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7387 PyStructSequence_SET_ITEM(v, 2,
7388 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7389 PyStructSequence_SET_ITEM(v, 3,
7390 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7391 PyStructSequence_SET_ITEM(v, 4,
7392 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7393 PyStructSequence_SET_ITEM(v, 5,
7394 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7395 PyStructSequence_SET_ITEM(v, 6,
7396 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7397 PyStructSequence_SET_ITEM(v, 7,
7398 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7399 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7400 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007401#endif
7402
Victor Stinner8c62be82010-05-06 00:08:46 +00007403 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007404}
7405
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007406PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007407"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007408Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007409
7410static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007411posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007412{
Victor Stinner8c62be82010-05-06 00:08:46 +00007413 int fd, res;
7414 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007415
Victor Stinner8c62be82010-05-06 00:08:46 +00007416 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7417 return NULL;
7418 Py_BEGIN_ALLOW_THREADS
7419 res = fstatvfs(fd, &st);
7420 Py_END_ALLOW_THREADS
7421 if (res != 0)
7422 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007423
Victor Stinner8c62be82010-05-06 00:08:46 +00007424 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007425}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007426#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007427
7428
Thomas Wouters477c8d52006-05-27 19:21:47 +00007429#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007430#include <sys/statvfs.h>
7431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007432PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007433"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007434Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007435
7436static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007437posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007438{
Victor Stinner8c62be82010-05-06 00:08:46 +00007439 char *path;
7440 int res;
7441 struct statvfs st;
7442 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7443 return NULL;
7444 Py_BEGIN_ALLOW_THREADS
7445 res = statvfs(path, &st);
7446 Py_END_ALLOW_THREADS
7447 if (res != 0)
7448 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007449
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007451}
7452#endif /* HAVE_STATVFS */
7453
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02007454#ifdef MS_WINDOWS
7455PyDoc_STRVAR(win32__getdiskusage__doc__,
7456"_getdiskusage(path) -> (total, free)\n\n\
7457Return disk usage statistics about the given path as (total, free) tuple.");
7458
7459static PyObject *
7460win32__getdiskusage(PyObject *self, PyObject *args)
7461{
7462 BOOL retval;
7463 ULARGE_INTEGER _, total, free;
7464 LPCTSTR path;
7465
7466 if (! PyArg_ParseTuple(args, "s", &path))
7467 return NULL;
7468
7469 Py_BEGIN_ALLOW_THREADS
7470 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
7471 Py_END_ALLOW_THREADS
7472 if (retval == 0)
7473 return PyErr_SetFromWindowsErr(0);
7474
7475 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
7476}
7477#endif
7478
7479
Fred Drakec9680921999-12-13 16:37:25 +00007480/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7481 * It maps strings representing configuration variable names to
7482 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007483 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007484 * rarely-used constants. There are three separate tables that use
7485 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007486 *
7487 * This code is always included, even if none of the interfaces that
7488 * need it are included. The #if hackery needed to avoid it would be
7489 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007490 */
7491struct constdef {
7492 char *name;
7493 long value;
7494};
7495
Fred Drake12c6e2d1999-12-14 21:25:03 +00007496static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007497conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00007498 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007499{
Christian Heimes217cfd12007-12-02 14:31:20 +00007500 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007501 *valuep = PyLong_AS_LONG(arg);
7502 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007503 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00007504 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00007505 /* look up the value in the table using a binary search */
7506 size_t lo = 0;
7507 size_t mid;
7508 size_t hi = tablesize;
7509 int cmp;
7510 const char *confname;
7511 if (!PyUnicode_Check(arg)) {
7512 PyErr_SetString(PyExc_TypeError,
7513 "configuration names must be strings or integers");
7514 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007515 }
Stefan Krah0e803b32010-11-26 16:16:47 +00007516 confname = _PyUnicode_AsString(arg);
7517 if (confname == NULL)
7518 return 0;
7519 while (lo < hi) {
7520 mid = (lo + hi) / 2;
7521 cmp = strcmp(confname, table[mid].name);
7522 if (cmp < 0)
7523 hi = mid;
7524 else if (cmp > 0)
7525 lo = mid + 1;
7526 else {
7527 *valuep = table[mid].value;
7528 return 1;
7529 }
7530 }
7531 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7532 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007533 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007534}
7535
7536
7537#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7538static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007539#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007541#endif
7542#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007543 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007544#endif
Fred Drakec9680921999-12-13 16:37:25 +00007545#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007546 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007547#endif
7548#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007550#endif
7551#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007553#endif
7554#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00007555 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007556#endif
7557#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007558 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007559#endif
7560#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00007561 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007562#endif
7563#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007564 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007565#endif
7566#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007568#endif
7569#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007571#endif
7572#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007574#endif
7575#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007577#endif
7578#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007579 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007580#endif
7581#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007582 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007583#endif
7584#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007585 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007586#endif
7587#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007588 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007589#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00007590#ifdef _PC_ACL_ENABLED
7591 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
7592#endif
7593#ifdef _PC_MIN_HOLE_SIZE
7594 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
7595#endif
7596#ifdef _PC_ALLOC_SIZE_MIN
7597 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
7598#endif
7599#ifdef _PC_REC_INCR_XFER_SIZE
7600 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
7601#endif
7602#ifdef _PC_REC_MAX_XFER_SIZE
7603 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
7604#endif
7605#ifdef _PC_REC_MIN_XFER_SIZE
7606 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
7607#endif
7608#ifdef _PC_REC_XFER_ALIGN
7609 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
7610#endif
7611#ifdef _PC_SYMLINK_MAX
7612 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
7613#endif
7614#ifdef _PC_XATTR_ENABLED
7615 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
7616#endif
7617#ifdef _PC_XATTR_EXISTS
7618 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
7619#endif
7620#ifdef _PC_TIMESTAMP_RESOLUTION
7621 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
7622#endif
Fred Drakec9680921999-12-13 16:37:25 +00007623};
7624
Fred Drakec9680921999-12-13 16:37:25 +00007625static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007626conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007627{
7628 return conv_confname(arg, valuep, posix_constants_pathconf,
7629 sizeof(posix_constants_pathconf)
7630 / sizeof(struct constdef));
7631}
7632#endif
7633
7634#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007635PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007636"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007637Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007638If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007639
7640static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007641posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007642{
7643 PyObject *result = NULL;
7644 int name, fd;
7645
Fred Drake12c6e2d1999-12-14 21:25:03 +00007646 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7647 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007648 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007649
Stefan Krah0e803b32010-11-26 16:16:47 +00007650 errno = 0;
7651 limit = fpathconf(fd, name);
7652 if (limit == -1 && errno != 0)
7653 posix_error();
7654 else
7655 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007656 }
7657 return result;
7658}
7659#endif
7660
7661
7662#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007663PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007664"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007665Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007666If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007667
7668static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007669posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007670{
7671 PyObject *result = NULL;
7672 int name;
7673 char *path;
7674
7675 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7676 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007677 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007678
Victor Stinner8c62be82010-05-06 00:08:46 +00007679 errno = 0;
7680 limit = pathconf(path, name);
7681 if (limit == -1 && errno != 0) {
7682 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00007683 /* could be a path or name problem */
7684 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007685 else
Stefan Krah99439262010-11-26 12:58:05 +00007686 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 }
7688 else
7689 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007690 }
7691 return result;
7692}
7693#endif
7694
7695#ifdef HAVE_CONFSTR
7696static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007697#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007699#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00007700#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007701 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007702#endif
7703#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007704 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007705#endif
Fred Draked86ed291999-12-15 15:34:33 +00007706#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007707 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007708#endif
7709#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007710 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007711#endif
7712#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007713 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007714#endif
7715#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007716 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007717#endif
Fred Drakec9680921999-12-13 16:37:25 +00007718#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007720#endif
7721#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007723#endif
7724#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007725 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007726#endif
7727#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007728 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007729#endif
7730#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007732#endif
7733#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007735#endif
7736#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007737 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007738#endif
7739#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007740 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007741#endif
Fred Draked86ed291999-12-15 15:34:33 +00007742#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00007743 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007744#endif
Fred Drakec9680921999-12-13 16:37:25 +00007745#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007747#endif
Fred Draked86ed291999-12-15 15:34:33 +00007748#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007750#endif
7751#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007752 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007753#endif
7754#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007755 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007756#endif
7757#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007758 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007759#endif
Fred Drakec9680921999-12-13 16:37:25 +00007760#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007761 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007762#endif
7763#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007764 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007765#endif
7766#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007768#endif
7769#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007771#endif
7772#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007774#endif
7775#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007777#endif
7778#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007780#endif
7781#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007783#endif
7784#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007785 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007786#endif
7787#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007789#endif
7790#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007791 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007792#endif
7793#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007795#endif
7796#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007798#endif
7799#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007801#endif
7802#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007804#endif
7805#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007807#endif
Fred Draked86ed291999-12-15 15:34:33 +00007808#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007810#endif
7811#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007813#endif
7814#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007816#endif
7817#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007819#endif
7820#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007821 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007822#endif
7823#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00007824 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007825#endif
7826#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007828#endif
7829#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00007830 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007831#endif
7832#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007833 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007834#endif
7835#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007837#endif
7838#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007840#endif
7841#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007843#endif
7844#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007846#endif
Fred Drakec9680921999-12-13 16:37:25 +00007847};
7848
7849static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007850conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007851{
7852 return conv_confname(arg, valuep, posix_constants_confstr,
7853 sizeof(posix_constants_confstr)
7854 / sizeof(struct constdef));
7855}
7856
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007857PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007858"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007859Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007860
7861static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007862posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007863{
7864 PyObject *result = NULL;
7865 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00007866 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00007867 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007868
Victor Stinnercb043522010-09-10 23:49:04 +00007869 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
7870 return NULL;
7871
7872 errno = 0;
7873 len = confstr(name, buffer, sizeof(buffer));
7874 if (len == 0) {
7875 if (errno) {
7876 posix_error();
7877 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00007878 }
7879 else {
Victor Stinnercb043522010-09-10 23:49:04 +00007880 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00007881 }
7882 }
Victor Stinnercb043522010-09-10 23:49:04 +00007883
7884 if ((unsigned int)len >= sizeof(buffer)) {
7885 char *buf = PyMem_Malloc(len);
7886 if (buf == NULL)
7887 return PyErr_NoMemory();
7888 confstr(name, buf, len);
7889 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
7890 PyMem_Free(buf);
7891 }
7892 else
7893 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007894 return result;
7895}
7896#endif
7897
7898
7899#ifdef HAVE_SYSCONF
7900static struct constdef posix_constants_sysconf[] = {
7901#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007902 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007903#endif
7904#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007906#endif
7907#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007908 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007909#endif
7910#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007911 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007912#endif
7913#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007914 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007915#endif
7916#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007917 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007918#endif
7919#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007920 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007921#endif
7922#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007923 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007924#endif
7925#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007926 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007927#endif
7928#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007929 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007930#endif
Fred Draked86ed291999-12-15 15:34:33 +00007931#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007932 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007933#endif
7934#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007936#endif
Fred Drakec9680921999-12-13 16:37:25 +00007937#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007938 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007939#endif
Fred Drakec9680921999-12-13 16:37:25 +00007940#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007941 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007942#endif
7943#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007944 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007945#endif
7946#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007947 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007948#endif
7949#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007950 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007951#endif
7952#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007953 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007954#endif
Fred Draked86ed291999-12-15 15:34:33 +00007955#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007956 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007957#endif
Fred Drakec9680921999-12-13 16:37:25 +00007958#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007959 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007960#endif
7961#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007962 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007963#endif
7964#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007966#endif
7967#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007968 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007969#endif
7970#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007971 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007972#endif
Fred Draked86ed291999-12-15 15:34:33 +00007973#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007975#endif
Fred Drakec9680921999-12-13 16:37:25 +00007976#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007978#endif
7979#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007981#endif
7982#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007984#endif
7985#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007986 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007987#endif
7988#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007990#endif
7991#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007992 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007993#endif
7994#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007996#endif
7997#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007999#endif
8000#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008002#endif
8003#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008005#endif
8006#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008008#endif
8009#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008011#endif
8012#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008014#endif
8015#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008017#endif
8018#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008019 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008020#endif
8021#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008023#endif
8024#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008025 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008026#endif
8027#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008028 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008029#endif
8030#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008031 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008032#endif
8033#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008034 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008035#endif
8036#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008037 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008038#endif
8039#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008041#endif
8042#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008044#endif
Fred Draked86ed291999-12-15 15:34:33 +00008045#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008046 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008047#endif
Fred Drakec9680921999-12-13 16:37:25 +00008048#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008049 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008050#endif
8051#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008052 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008053#endif
8054#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008055 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008056#endif
Fred Draked86ed291999-12-15 15:34:33 +00008057#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008058 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008059#endif
Fred Drakec9680921999-12-13 16:37:25 +00008060#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008062#endif
Fred Draked86ed291999-12-15 15:34:33 +00008063#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008064 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008065#endif
8066#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008067 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008068#endif
Fred Drakec9680921999-12-13 16:37:25 +00008069#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008070 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008071#endif
8072#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008073 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008074#endif
8075#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008077#endif
8078#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008080#endif
Fred Draked86ed291999-12-15 15:34:33 +00008081#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008082 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008083#endif
Fred Drakec9680921999-12-13 16:37:25 +00008084#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008085 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008086#endif
8087#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008088 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008089#endif
8090#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008091 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008092#endif
8093#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008094 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008095#endif
8096#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008098#endif
8099#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008101#endif
8102#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008104#endif
Fred Draked86ed291999-12-15 15:34:33 +00008105#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008107#endif
Fred Drakec9680921999-12-13 16:37:25 +00008108#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008110#endif
8111#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008113#endif
Fred Draked86ed291999-12-15 15:34:33 +00008114#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008116#endif
Fred Drakec9680921999-12-13 16:37:25 +00008117#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008119#endif
8120#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008121 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008122#endif
8123#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008124 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008125#endif
8126#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008127 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008128#endif
8129#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008131#endif
8132#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008134#endif
8135#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008137#endif
8138#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008139 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008140#endif
8141#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008143#endif
Fred Draked86ed291999-12-15 15:34:33 +00008144#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008145 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008146#endif
8147#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008148 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008149#endif
Fred Drakec9680921999-12-13 16:37:25 +00008150#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008151 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008152#endif
8153#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008154 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008155#endif
8156#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008157 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008158#endif
8159#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008160 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008161#endif
8162#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008163 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008164#endif
8165#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008166 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008167#endif
8168#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008169 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008170#endif
8171#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008172 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008173#endif
8174#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008175 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008176#endif
8177#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008178 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008179#endif
8180#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008181 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008182#endif
8183#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008184 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008185#endif
8186#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008187 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008188#endif
8189#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008190 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008191#endif
8192#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008193 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008194#endif
8195#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008197#endif
8198#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008199 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008200#endif
8201#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008202 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008203#endif
8204#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008206#endif
8207#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008208 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008209#endif
8210#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008211 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008212#endif
8213#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008215#endif
8216#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008218#endif
8219#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008221#endif
8222#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008224#endif
8225#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008227#endif
8228#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008230#endif
8231#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008233#endif
8234#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008236#endif
8237#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008239#endif
8240#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008242#endif
8243#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008245#endif
8246#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008248#endif
8249#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008251#endif
8252#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008253 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008254#endif
Fred Draked86ed291999-12-15 15:34:33 +00008255#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008256 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008257#endif
Fred Drakec9680921999-12-13 16:37:25 +00008258#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008260#endif
8261#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008263#endif
8264#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008266#endif
8267#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008269#endif
8270#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008272#endif
8273#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008275#endif
8276#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008278#endif
8279#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008281#endif
8282#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008284#endif
8285#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008287#endif
8288#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008290#endif
8291#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008293#endif
8294#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008296#endif
8297#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008298 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008299#endif
8300#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008301 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008302#endif
8303#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008305#endif
8306#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008308#endif
8309#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008311#endif
8312#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008314#endif
8315#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008316 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008317#endif
8318#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008319 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008320#endif
8321#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008323#endif
8324#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008326#endif
8327#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008329#endif
8330#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008332#endif
8333#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008335#endif
8336#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008338#endif
8339#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008341#endif
8342#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008343 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008344#endif
8345#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008347#endif
8348#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008349 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008350#endif
8351#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008353#endif
8354#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008355 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008356#endif
8357#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008358 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008359#endif
8360#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008361 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008362#endif
8363#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008364 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008365#endif
8366#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008367 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008368#endif
8369#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008371#endif
8372#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008373 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008374#endif
8375#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008376 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008377#endif
8378#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008379 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008380#endif
8381#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008382 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008383#endif
8384#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008386#endif
8387#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008389#endif
8390#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008391 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008392#endif
8393};
8394
8395static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008396conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008397{
8398 return conv_confname(arg, valuep, posix_constants_sysconf,
8399 sizeof(posix_constants_sysconf)
8400 / sizeof(struct constdef));
8401}
8402
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008403PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008404"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008405Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008406
8407static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008408posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008409{
8410 PyObject *result = NULL;
8411 int name;
8412
8413 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8414 int value;
8415
8416 errno = 0;
8417 value = sysconf(name);
8418 if (value == -1 && errno != 0)
8419 posix_error();
8420 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008421 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008422 }
8423 return result;
8424}
8425#endif
8426
8427
Fred Drakebec628d1999-12-15 18:31:10 +00008428/* This code is used to ensure that the tables of configuration value names
8429 * are in sorted order as required by conv_confname(), and also to build the
8430 * the exported dictionaries that are used to publish information about the
8431 * names available on the host platform.
8432 *
8433 * Sorting the table at runtime ensures that the table is properly ordered
8434 * when used, even for platforms we're not able to test on. It also makes
8435 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008436 */
Fred Drakebec628d1999-12-15 18:31:10 +00008437
8438static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008439cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008440{
8441 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008442 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008443 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008445
8446 return strcmp(c1->name, c2->name);
8447}
8448
8449static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008450setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00008451 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008452{
Fred Drakebec628d1999-12-15 18:31:10 +00008453 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008454 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008455
8456 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8457 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008458 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008460
Barry Warsaw3155db32000-04-13 15:20:40 +00008461 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008462 PyObject *o = PyLong_FromLong(table[i].value);
8463 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8464 Py_XDECREF(o);
8465 Py_DECREF(d);
8466 return -1;
8467 }
8468 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008469 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008470 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008471}
8472
Fred Drakebec628d1999-12-15 18:31:10 +00008473/* Return -1 on failure, 0 on success. */
8474static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008475setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008476{
8477#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008478 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008479 sizeof(posix_constants_pathconf)
8480 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008481 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008482 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008483#endif
8484#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008485 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008486 sizeof(posix_constants_confstr)
8487 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008488 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008489 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008490#endif
8491#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008492 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008493 sizeof(posix_constants_sysconf)
8494 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008495 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008496 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008497#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008498 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008499}
Fred Draked86ed291999-12-15 15:34:33 +00008500
8501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008502PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008503"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008504Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008505in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008506
8507static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008508posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008509{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008510 abort();
8511 /*NOTREACHED*/
8512 Py_FatalError("abort() called from Python code didn't abort!");
8513 return NULL;
8514}
Fred Drakebec628d1999-12-15 18:31:10 +00008515
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008516#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008517PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008518"startfile(filepath [, operation]) - Start a file with its associated\n\
8519application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008520\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008521When \"operation\" is not specified or \"open\", this acts like\n\
8522double-clicking the file in Explorer, or giving the file name as an\n\
8523argument to the DOS \"start\" command: the file is opened with whatever\n\
8524application (if any) its extension is associated.\n\
8525When another \"operation\" is given, it specifies what should be done with\n\
8526the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008527\n\
8528startfile returns as soon as the associated application is launched.\n\
8529There is no option to wait for the application to close, and no way\n\
8530to retrieve the application's exit status.\n\
8531\n\
8532The filepath is relative to the current directory. If you want to use\n\
8533an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008534the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008535
8536static PyObject *
8537win32_startfile(PyObject *self, PyObject *args)
8538{
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 PyObject *ofilepath;
8540 char *filepath;
8541 char *operation = NULL;
8542 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00008543
Victor Stinner8c62be82010-05-06 00:08:46 +00008544 PyObject *unipath, *woperation = NULL;
8545 if (!PyArg_ParseTuple(args, "U|s:startfile",
8546 &unipath, &operation)) {
8547 PyErr_Clear();
8548 goto normal;
8549 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008550
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 if (operation) {
8552 woperation = PyUnicode_DecodeASCII(operation,
8553 strlen(operation), NULL);
8554 if (!woperation) {
8555 PyErr_Clear();
8556 operation = NULL;
8557 goto normal;
8558 }
8559 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008560
Victor Stinner8c62be82010-05-06 00:08:46 +00008561 Py_BEGIN_ALLOW_THREADS
8562 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8563 PyUnicode_AS_UNICODE(unipath),
8564 NULL, NULL, SW_SHOWNORMAL);
8565 Py_END_ALLOW_THREADS
8566
8567 Py_XDECREF(woperation);
8568 if (rc <= (HINSTANCE)32) {
8569 PyObject *errval = win32_error_unicode("startfile",
8570 PyUnicode_AS_UNICODE(unipath));
8571 return errval;
8572 }
8573 Py_INCREF(Py_None);
8574 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008575
8576normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00008577 if (!PyArg_ParseTuple(args, "O&|s:startfile",
8578 PyUnicode_FSConverter, &ofilepath,
8579 &operation))
8580 return NULL;
8581 filepath = PyBytes_AsString(ofilepath);
8582 Py_BEGIN_ALLOW_THREADS
8583 rc = ShellExecute((HWND)0, operation, filepath,
8584 NULL, NULL, SW_SHOWNORMAL);
8585 Py_END_ALLOW_THREADS
8586 if (rc <= (HINSTANCE)32) {
8587 PyObject *errval = win32_error("startfile", filepath);
8588 Py_DECREF(ofilepath);
8589 return errval;
8590 }
8591 Py_DECREF(ofilepath);
8592 Py_INCREF(Py_None);
8593 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008594}
8595#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008596
Martin v. Löwis438b5342002-12-27 10:16:42 +00008597#ifdef HAVE_GETLOADAVG
8598PyDoc_STRVAR(posix_getloadavg__doc__,
8599"getloadavg() -> (float, float, float)\n\n\
8600Return the number of processes in the system run queue averaged over\n\
8601the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8602was unobtainable");
8603
8604static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008605posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008606{
8607 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008608 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008609 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8610 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008611 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00008612 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008613}
8614#endif
8615
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008616#ifdef MS_WINDOWS
8617
8618PyDoc_STRVAR(win32_urandom__doc__,
8619"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008620Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008621
8622typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8623 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8624 DWORD dwFlags );
8625typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8626 BYTE *pbBuffer );
8627
8628static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00008629/* This handle is never explicitly released. Instead, the operating
8630 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008631static HCRYPTPROV hCryptProv = 0;
8632
Tim Peters4ad82172004-08-30 17:02:04 +00008633static PyObject*
8634win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008635{
Victor Stinner8c62be82010-05-06 00:08:46 +00008636 int howMany;
8637 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008638
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 /* Read arguments */
8640 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8641 return NULL;
8642 if (howMany < 0)
8643 return PyErr_Format(PyExc_ValueError,
8644 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008645
Victor Stinner8c62be82010-05-06 00:08:46 +00008646 if (hCryptProv == 0) {
8647 HINSTANCE hAdvAPI32 = NULL;
8648 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008649
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 /* Obtain handle to the DLL containing CryptoAPI
8651 This should not fail */
8652 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8653 if(hAdvAPI32 == NULL)
8654 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008655
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 /* Obtain pointers to the CryptoAPI functions
8657 This will fail on some early versions of Win95 */
8658 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8659 hAdvAPI32,
8660 "CryptAcquireContextA");
8661 if (pCryptAcquireContext == NULL)
8662 return PyErr_Format(PyExc_NotImplementedError,
8663 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008664
Victor Stinner8c62be82010-05-06 00:08:46 +00008665 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8666 hAdvAPI32, "CryptGenRandom");
8667 if (pCryptGenRandom == NULL)
8668 return PyErr_Format(PyExc_NotImplementedError,
8669 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008670
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 /* Acquire context */
8672 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8673 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8674 return win32_error("CryptAcquireContext", NULL);
8675 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008676
Victor Stinner8c62be82010-05-06 00:08:46 +00008677 /* Allocate bytes */
8678 result = PyBytes_FromStringAndSize(NULL, howMany);
8679 if (result != NULL) {
8680 /* Get random data */
8681 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
8682 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8683 PyBytes_AS_STRING(result))) {
8684 Py_DECREF(result);
8685 return win32_error("CryptGenRandom", NULL);
8686 }
8687 }
8688 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008689}
8690#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008691
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008692PyDoc_STRVAR(device_encoding__doc__,
8693"device_encoding(fd) -> str\n\n\
8694Return a string describing the encoding of the device\n\
8695if the output is a terminal; else return None.");
8696
8697static PyObject *
8698device_encoding(PyObject *self, PyObject *args)
8699{
Victor Stinner8c62be82010-05-06 00:08:46 +00008700 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02008701#if defined(MS_WINDOWS) || defined(MS_WIN64)
8702 UINT cp;
8703#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
8705 return NULL;
8706 if (!_PyVerify_fd(fd) || !isatty(fd)) {
8707 Py_INCREF(Py_None);
8708 return Py_None;
8709 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008710#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02008711 if (fd == 0)
8712 cp = GetConsoleCP();
8713 else if (fd == 1 || fd == 2)
8714 cp = GetConsoleOutputCP();
8715 else
8716 cp = 0;
8717 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
8718 has no console */
8719 if (cp != 0)
8720 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008721#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {
8723 char *codeset = nl_langinfo(CODESET);
8724 if (codeset != NULL && codeset[0] != 0)
8725 return PyUnicode_FromString(codeset);
8726 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008727#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 Py_INCREF(Py_None);
8729 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008730}
8731
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008732#ifdef __VMS
8733/* Use openssl random routine */
8734#include <openssl/rand.h>
8735PyDoc_STRVAR(vms_urandom__doc__,
8736"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008737Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008738
8739static PyObject*
8740vms_urandom(PyObject *self, PyObject *args)
8741{
Victor Stinner8c62be82010-05-06 00:08:46 +00008742 int howMany;
8743 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008744
Victor Stinner8c62be82010-05-06 00:08:46 +00008745 /* Read arguments */
8746 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8747 return NULL;
8748 if (howMany < 0)
8749 return PyErr_Format(PyExc_ValueError,
8750 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008751
Victor Stinner8c62be82010-05-06 00:08:46 +00008752 /* Allocate bytes */
8753 result = PyBytes_FromStringAndSize(NULL, howMany);
8754 if (result != NULL) {
8755 /* Get random data */
8756 if (RAND_pseudo_bytes((unsigned char*)
8757 PyBytes_AS_STRING(result),
8758 howMany) < 0) {
8759 Py_DECREF(result);
8760 return PyErr_Format(PyExc_ValueError,
8761 "RAND_pseudo_bytes");
8762 }
8763 }
8764 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008765}
8766#endif
8767
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008768#ifdef HAVE_SETRESUID
8769PyDoc_STRVAR(posix_setresuid__doc__,
8770"setresuid(ruid, euid, suid)\n\n\
8771Set the current process's real, effective, and saved user ids.");
8772
8773static PyObject*
8774posix_setresuid (PyObject *self, PyObject *args)
8775{
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 /* We assume uid_t is no larger than a long. */
8777 long ruid, euid, suid;
8778 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8779 return NULL;
8780 if (setresuid(ruid, euid, suid) < 0)
8781 return posix_error();
8782 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008783}
8784#endif
8785
8786#ifdef HAVE_SETRESGID
8787PyDoc_STRVAR(posix_setresgid__doc__,
8788"setresgid(rgid, egid, sgid)\n\n\
8789Set the current process's real, effective, and saved group ids.");
8790
8791static PyObject*
8792posix_setresgid (PyObject *self, PyObject *args)
8793{
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 /* We assume uid_t is no larger than a long. */
8795 long rgid, egid, sgid;
8796 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8797 return NULL;
8798 if (setresgid(rgid, egid, sgid) < 0)
8799 return posix_error();
8800 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008801}
8802#endif
8803
8804#ifdef HAVE_GETRESUID
8805PyDoc_STRVAR(posix_getresuid__doc__,
8806"getresuid() -> (ruid, euid, suid)\n\n\
8807Get tuple of the current process's real, effective, and saved user ids.");
8808
8809static PyObject*
8810posix_getresuid (PyObject *self, PyObject *noargs)
8811{
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 uid_t ruid, euid, suid;
8813 long l_ruid, l_euid, l_suid;
8814 if (getresuid(&ruid, &euid, &suid) < 0)
8815 return posix_error();
8816 /* Force the values into long's as we don't know the size of uid_t. */
8817 l_ruid = ruid;
8818 l_euid = euid;
8819 l_suid = suid;
8820 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008821}
8822#endif
8823
8824#ifdef HAVE_GETRESGID
8825PyDoc_STRVAR(posix_getresgid__doc__,
8826"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00008827Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008828
8829static PyObject*
8830posix_getresgid (PyObject *self, PyObject *noargs)
8831{
Victor Stinner8c62be82010-05-06 00:08:46 +00008832 uid_t rgid, egid, sgid;
8833 long l_rgid, l_egid, l_sgid;
8834 if (getresgid(&rgid, &egid, &sgid) < 0)
8835 return posix_error();
8836 /* Force the values into long's as we don't know the size of uid_t. */
8837 l_rgid = rgid;
8838 l_egid = egid;
8839 l_sgid = sgid;
8840 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008841}
8842#endif
8843
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008844/* Posix *at family of functions:
8845 faccessat, fchmodat, fchownat, fstatat, futimesat,
8846 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
8847 unlinkat, utimensat, mkfifoat */
8848
8849#ifdef HAVE_FACCESSAT
8850PyDoc_STRVAR(posix_faccessat__doc__,
8851"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
8852Like access() but if path is relative, it is taken as relative to dirfd.\n\
8853flags is optional and can be constructed by ORing together zero or more\n\
8854of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
8855If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8856is interpreted relative to the current working directory.");
8857
8858static PyObject *
8859posix_faccessat(PyObject *self, PyObject *args)
8860{
8861 PyObject *opath;
8862 char *path;
8863 int mode;
8864 int res;
8865 int dirfd, flags = 0;
8866 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
8867 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8868 return NULL;
8869 path = PyBytes_AsString(opath);
8870 Py_BEGIN_ALLOW_THREADS
8871 res = faccessat(dirfd, path, mode, flags);
8872 Py_END_ALLOW_THREADS
8873 Py_DECREF(opath);
8874 return PyBool_FromLong(res == 0);
8875}
8876#endif
8877
8878#ifdef HAVE_FCHMODAT
8879PyDoc_STRVAR(posix_fchmodat__doc__,
8880"fchmodat(dirfd, path, mode, flags=0)\n\n\
8881Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
8882flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8883If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8884is interpreted relative to the current working directory.");
8885
8886static PyObject *
8887posix_fchmodat(PyObject *self, PyObject *args)
8888{
8889 int dirfd, mode, res;
8890 int flags = 0;
8891 PyObject *opath;
8892 char *path;
8893
8894 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
8895 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8896 return NULL;
8897
8898 path = PyBytes_AsString(opath);
8899
8900 Py_BEGIN_ALLOW_THREADS
8901 res = fchmodat(dirfd, path, mode, flags);
8902 Py_END_ALLOW_THREADS
8903 Py_DECREF(opath);
8904 if (res < 0)
8905 return posix_error();
8906 Py_RETURN_NONE;
8907}
8908#endif /* HAVE_FCHMODAT */
8909
8910#ifdef HAVE_FCHOWNAT
8911PyDoc_STRVAR(posix_fchownat__doc__,
8912"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
8913Like chown() but if path is relative, it is taken as relative to dirfd.\n\
8914flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8915If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8916is interpreted relative to the current working directory.");
8917
8918static PyObject *
8919posix_fchownat(PyObject *self, PyObject *args)
8920{
8921 PyObject *opath;
8922 int dirfd, res;
8923 long uid, gid;
8924 int flags = 0;
8925 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008926
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008927 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
8928 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
8929 return NULL;
8930
8931 path = PyBytes_AsString(opath);
8932
8933 Py_BEGIN_ALLOW_THREADS
8934 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
8935 Py_END_ALLOW_THREADS
8936 Py_DECREF(opath);
8937 if (res < 0)
8938 return posix_error();
8939 Py_RETURN_NONE;
8940}
8941#endif /* HAVE_FCHOWNAT */
8942
8943#ifdef HAVE_FSTATAT
8944PyDoc_STRVAR(posix_fstatat__doc__,
8945"fstatat(dirfd, path, flags=0) -> stat result\n\n\
8946Like stat() but if path is relative, it is taken as relative to dirfd.\n\
8947flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8948If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8949is interpreted relative to the current working directory.");
8950
8951static PyObject *
8952posix_fstatat(PyObject *self, PyObject *args)
8953{
8954 PyObject *opath;
8955 char *path;
8956 STRUCT_STAT st;
8957 int dirfd, res, flags = 0;
8958
8959 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
8960 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8961 return NULL;
8962 path = PyBytes_AsString(opath);
8963
8964 Py_BEGIN_ALLOW_THREADS
8965 res = fstatat(dirfd, path, &st, flags);
8966 Py_END_ALLOW_THREADS
8967 Py_DECREF(opath);
8968 if (res != 0)
8969 return posix_error();
8970
8971 return _pystat_fromstructstat(&st);
8972}
8973#endif
8974
8975#ifdef HAVE_FUTIMESAT
8976PyDoc_STRVAR(posix_futimesat__doc__,
8977"futimesat(dirfd, path, (atime, mtime))\n\
8978futimesat(dirfd, path, None)\n\n\
8979Like utime() but if path is relative, it is taken as relative to dirfd.\n\
8980If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8981is interpreted relative to the current working directory.");
8982
8983static PyObject *
8984posix_futimesat(PyObject *self, PyObject *args)
8985{
8986 PyObject *opath;
8987 char *path;
8988 int res, dirfd;
8989 PyObject* arg;
8990
8991 struct timeval buf[2];
8992
8993 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
8994 &dirfd, PyUnicode_FSConverter, &opath, &arg))
8995 return NULL;
8996 path = PyBytes_AsString(opath);
8997 if (arg == Py_None) {
8998 /* optional time values not given */
8999 Py_BEGIN_ALLOW_THREADS
9000 res = futimesat(dirfd, path, NULL);
9001 Py_END_ALLOW_THREADS
9002 }
9003 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9004 PyErr_SetString(PyExc_TypeError,
9005 "futimesat() arg 3 must be a tuple (atime, mtime)");
9006 Py_DECREF(opath);
9007 return NULL;
9008 }
9009 else {
9010 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009011 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009012 Py_DECREF(opath);
9013 return NULL;
9014 }
9015 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009016 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009017 Py_DECREF(opath);
9018 return NULL;
9019 }
9020 Py_BEGIN_ALLOW_THREADS
9021 res = futimesat(dirfd, path, buf);
9022 Py_END_ALLOW_THREADS
9023 }
9024 Py_DECREF(opath);
9025 if (res < 0) {
9026 return posix_error();
9027 }
9028 Py_RETURN_NONE;
9029}
9030#endif
9031
9032#ifdef HAVE_LINKAT
9033PyDoc_STRVAR(posix_linkat__doc__,
9034"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9035Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9036and if dstpath is relative, it is taken as relative to dstfd.\n\
9037flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9038If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9039srcpath is interpreted relative to the current working directory. This\n\
9040also applies for dstpath.");
9041
9042static PyObject *
9043posix_linkat(PyObject *self, PyObject *args)
9044{
9045 PyObject *osrc, *odst;
9046 char *src, *dst;
9047 int res, srcfd, dstfd;
9048 int flags = 0;
9049
9050 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9051 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9052 return NULL;
9053 src = PyBytes_AsString(osrc);
9054 dst = PyBytes_AsString(odst);
9055 Py_BEGIN_ALLOW_THREADS
9056 res = linkat(srcfd, src, dstfd, dst, flags);
9057 Py_END_ALLOW_THREADS
9058 Py_DECREF(osrc);
9059 Py_DECREF(odst);
9060 if (res < 0)
9061 return posix_error();
9062 Py_RETURN_NONE;
9063}
9064#endif /* HAVE_LINKAT */
9065
9066#ifdef HAVE_MKDIRAT
9067PyDoc_STRVAR(posix_mkdirat__doc__,
9068"mkdirat(dirfd, path, mode=0o777)\n\n\
9069Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9070If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9071is interpreted relative to the current working directory.");
9072
9073static PyObject *
9074posix_mkdirat(PyObject *self, PyObject *args)
9075{
9076 int res, dirfd;
9077 PyObject *opath;
9078 char *path;
9079 int mode = 0777;
9080
9081 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9082 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9083 return NULL;
9084 path = PyBytes_AsString(opath);
9085 Py_BEGIN_ALLOW_THREADS
9086 res = mkdirat(dirfd, path, mode);
9087 Py_END_ALLOW_THREADS
9088 Py_DECREF(opath);
9089 if (res < 0)
9090 return posix_error();
9091 Py_RETURN_NONE;
9092}
9093#endif
9094
9095#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9096PyDoc_STRVAR(posix_mknodat__doc__,
9097"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9098Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9099If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9100is interpreted relative to the current working directory.");
9101
9102static PyObject *
9103posix_mknodat(PyObject *self, PyObject *args)
9104{
9105 PyObject *opath;
9106 char *filename;
9107 int mode = 0600;
9108 int device = 0;
9109 int res, dirfd;
9110 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9111 PyUnicode_FSConverter, &opath, &mode, &device))
9112 return NULL;
9113 filename = PyBytes_AS_STRING(opath);
9114 Py_BEGIN_ALLOW_THREADS
9115 res = mknodat(dirfd, filename, mode, device);
9116 Py_END_ALLOW_THREADS
9117 Py_DECREF(opath);
9118 if (res < 0)
9119 return posix_error();
9120 Py_RETURN_NONE;
9121}
9122#endif
9123
9124#ifdef HAVE_OPENAT
9125PyDoc_STRVAR(posix_openat__doc__,
9126"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9127Like open() but if path is relative, it is taken as relative to dirfd.\n\
9128If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9129is interpreted relative to the current working directory.");
9130
9131static PyObject *
9132posix_openat(PyObject *self, PyObject *args)
9133{
9134 PyObject *ofile;
9135 char *file;
9136 int flag, dirfd, fd;
9137 int mode = 0777;
9138
9139 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9140 &dirfd, PyUnicode_FSConverter, &ofile,
9141 &flag, &mode))
9142 return NULL;
9143 file = PyBytes_AsString(ofile);
9144 Py_BEGIN_ALLOW_THREADS
9145 fd = openat(dirfd, file, flag, mode);
9146 Py_END_ALLOW_THREADS
9147 Py_DECREF(ofile);
9148 if (fd < 0)
9149 return posix_error();
9150 return PyLong_FromLong((long)fd);
9151}
9152#endif
9153
9154#ifdef HAVE_READLINKAT
9155PyDoc_STRVAR(posix_readlinkat__doc__,
9156"readlinkat(dirfd, path) -> path\n\n\
9157Like readlink() 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_readlinkat(PyObject *self, PyObject *args)
9163{
9164 PyObject *v, *opath;
9165 char buf[MAXPATHLEN];
9166 char *path;
9167 int n, dirfd;
9168 int arg_is_unicode = 0;
9169
9170 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9171 &dirfd, PyUnicode_FSConverter, &opath))
9172 return NULL;
9173 path = PyBytes_AsString(opath);
9174 v = PySequence_GetItem(args, 1);
9175 if (v == NULL) {
9176 Py_DECREF(opath);
9177 return NULL;
9178 }
9179
9180 if (PyUnicode_Check(v)) {
9181 arg_is_unicode = 1;
9182 }
9183 Py_DECREF(v);
9184
9185 Py_BEGIN_ALLOW_THREADS
9186 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9187 Py_END_ALLOW_THREADS
9188 Py_DECREF(opath);
9189 if (n < 0)
9190 return posix_error();
9191
9192 if (arg_is_unicode)
9193 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9194 else
9195 return PyBytes_FromStringAndSize(buf, n);
9196}
9197#endif /* HAVE_READLINKAT */
9198
9199#ifdef HAVE_RENAMEAT
9200PyDoc_STRVAR(posix_renameat__doc__,
9201"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9202Like rename() but if oldpath is relative, it is taken as relative to\n\
9203olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9204If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9205oldpath is interpreted relative to the current working directory. This\n\
9206also applies for newpath.");
9207
9208static PyObject *
9209posix_renameat(PyObject *self, PyObject *args)
9210{
9211 int res;
9212 PyObject *opathold, *opathnew;
9213 char *opath, *npath;
9214 int oldfd, newfd;
9215
9216 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9217 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9218 return NULL;
9219 opath = PyBytes_AsString(opathold);
9220 npath = PyBytes_AsString(opathnew);
9221 Py_BEGIN_ALLOW_THREADS
9222 res = renameat(oldfd, opath, newfd, npath);
9223 Py_END_ALLOW_THREADS
9224 Py_DECREF(opathold);
9225 Py_DECREF(opathnew);
9226 if (res < 0)
9227 return posix_error();
9228 Py_RETURN_NONE;
9229}
9230#endif
9231
9232#if HAVE_SYMLINKAT
9233PyDoc_STRVAR(posix_symlinkat__doc__,
9234"symlinkat(src, dstfd, dst)\n\n\
9235Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9236If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9237is interpreted relative to the current working directory.");
9238
9239static PyObject *
9240posix_symlinkat(PyObject *self, PyObject *args)
9241{
9242 int res, dstfd;
9243 PyObject *osrc, *odst;
9244 char *src, *dst;
9245
9246 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9247 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9248 return NULL;
9249 src = PyBytes_AsString(osrc);
9250 dst = PyBytes_AsString(odst);
9251 Py_BEGIN_ALLOW_THREADS
9252 res = symlinkat(src, dstfd, dst);
9253 Py_END_ALLOW_THREADS
9254 Py_DECREF(osrc);
9255 Py_DECREF(odst);
9256 if (res < 0)
9257 return posix_error();
9258 Py_RETURN_NONE;
9259}
9260#endif /* HAVE_SYMLINKAT */
9261
9262#ifdef HAVE_UNLINKAT
9263PyDoc_STRVAR(posix_unlinkat__doc__,
9264"unlinkat(dirfd, path, flags=0)\n\n\
9265Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9266flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9267specified, unlinkat() behaves like rmdir().\n\
9268If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9269is interpreted relative to the current working directory.");
9270
9271static PyObject *
9272posix_unlinkat(PyObject *self, PyObject *args)
9273{
9274 int dirfd, res, flags = 0;
9275 PyObject *opath;
9276 char *path;
9277
9278 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9279 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9280 return NULL;
9281 path = PyBytes_AsString(opath);
9282 Py_BEGIN_ALLOW_THREADS
9283 res = unlinkat(dirfd, path, flags);
9284 Py_END_ALLOW_THREADS
9285 Py_DECREF(opath);
9286 if (res < 0)
9287 return posix_error();
9288 Py_RETURN_NONE;
9289}
9290#endif
9291
9292#ifdef HAVE_UTIMENSAT
9293PyDoc_STRVAR(posix_utimensat__doc__,
9294"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9295 (mtime_sec, mtime_nsec), flags)\n\
9296utimensat(dirfd, path, None, None, flags)\n\n\
9297Updates the timestamps of a file with nanosecond precision. If path is\n\
9298relative, it is taken as relative to dirfd.\n\
9299The second form sets atime and mtime to the current time.\n\
9300flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9301If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9302is interpreted relative to the current working directory.\n\
9303If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9304current time.\n\
9305If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9306
9307static PyObject *
9308posix_utimensat(PyObject *self, PyObject *args)
9309{
9310 PyObject *opath;
9311 char *path;
9312 int res, dirfd, flags = 0;
9313 PyObject *atime, *mtime;
9314
9315 struct timespec buf[2];
9316
9317 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9318 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9319 return NULL;
9320 path = PyBytes_AsString(opath);
9321 if (atime == Py_None && mtime == Py_None) {
9322 /* optional time values not given */
9323 Py_BEGIN_ALLOW_THREADS
9324 res = utimensat(dirfd, path, NULL, flags);
9325 Py_END_ALLOW_THREADS
9326 }
9327 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9328 PyErr_SetString(PyExc_TypeError,
9329 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9330 Py_DECREF(opath);
9331 return NULL;
9332 }
9333 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9334 PyErr_SetString(PyExc_TypeError,
9335 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9336 Py_DECREF(opath);
9337 return NULL;
9338 }
9339 else {
9340 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9341 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9342 Py_DECREF(opath);
9343 return NULL;
9344 }
9345 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9346 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9347 Py_DECREF(opath);
9348 return NULL;
9349 }
9350 Py_BEGIN_ALLOW_THREADS
9351 res = utimensat(dirfd, path, buf, flags);
9352 Py_END_ALLOW_THREADS
9353 }
9354 Py_DECREF(opath);
9355 if (res < 0) {
9356 return posix_error();
9357 }
9358 Py_RETURN_NONE;
9359}
9360#endif
9361
9362#ifdef HAVE_MKFIFOAT
9363PyDoc_STRVAR(posix_mkfifoat__doc__,
9364"mkfifoat(dirfd, path, mode=0o666)\n\n\
9365Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9366If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9367is interpreted relative to the current working directory.");
9368
9369static PyObject *
9370posix_mkfifoat(PyObject *self, PyObject *args)
9371{
9372 PyObject *opath;
9373 char *filename;
9374 int mode = 0666;
9375 int res, dirfd;
9376 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9377 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9378 return NULL;
9379 filename = PyBytes_AS_STRING(opath);
9380 Py_BEGIN_ALLOW_THREADS
9381 res = mkfifoat(dirfd, filename, mode);
9382 Py_END_ALLOW_THREADS
9383 Py_DECREF(opath);
9384 if (res < 0)
9385 return posix_error();
9386 Py_RETURN_NONE;
9387}
9388#endif
9389
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009390static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009391 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009392#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009393 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009394#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009396#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009397 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009398#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009399 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009400#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009401 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009402#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009403#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009405#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009406#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009407 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009408#endif /* HAVE_LCHMOD */
9409#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009410 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009411#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009412#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009413 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009414#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009415#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009416 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009417#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009418#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009419 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009420#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009421#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009422 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009423#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009424#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009425 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9426 METH_NOARGS, posix_getcwd__doc__},
9427 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9428 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009429#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009430#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009431 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009432#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009433 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009434#ifdef HAVE_FDOPENDIR
9435 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9436#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009437 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
9438 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009439#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009441#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009442#ifdef HAVE_GETPRIORITY
9443 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
9444#endif /* HAVE_GETPRIORITY */
9445#ifdef HAVE_SETPRIORITY
9446 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
9447#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009448#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009449 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009450#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00009451#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009452 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00009453#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009454 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
9455 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
9456 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00009457 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00009458#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00009459 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009460#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00009461#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009462 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00009463 win_symlink__doc__},
9464#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009465#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00009466 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009467#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009468 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009469#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009470 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009471#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00009472 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
9473 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
9474 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009475#ifdef HAVE_FUTIMES
9476 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
9477#endif
9478#ifdef HAVE_LUTIMES
9479 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
9480#endif
9481#ifdef HAVE_FUTIMENS
9482 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
9483#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009484#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00009485 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009486#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00009487 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009488#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00009489 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
9490 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009491#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009492#ifdef HAVE_FEXECVE
9493 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
9494#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00009495#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00009496 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
9497 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009498#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00009499 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
9500 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009501#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00009502#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009503#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00009504 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009505#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009506#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00009507 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009508#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009509#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00009510 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009511#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00009512#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009513 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00009514#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009515#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009516 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009517#endif /* HAVE_GETEGID */
9518#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009519 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009520#endif /* HAVE_GETEUID */
9521#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009522 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009523#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02009524#ifdef HAVE_GETGROUPLIST
9525 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
9526#endif
Fred Drakec9680921999-12-13 16:37:25 +00009527#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009528 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009529#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009530 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009531#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009533#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009534#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009535 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009536#endif /* HAVE_GETPPID */
9537#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009539#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00009540#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009541 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00009542#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00009543#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00009544 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009545#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009546#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00009547 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009548#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00009549#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00009551#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00009552#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009553 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
9554 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00009555 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00009556#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009557#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009558 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009559#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009560#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009561 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009562#endif /* HAVE_SETEUID */
9563#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009564 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009565#endif /* HAVE_SETEGID */
9566#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009567 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009568#endif /* HAVE_SETREUID */
9569#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009570 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009571#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009572#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009573 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009574#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009575#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009576 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009577#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00009578#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009579 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00009580#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00009581#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009582 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00009583#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009584#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009585 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009586#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009587#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009588 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009589#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009590#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00009591 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009592#endif /* HAVE_WAIT3 */
9593#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00009594 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009595#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009596#if defined(HAVE_WAITID) && !defined(__APPLE__)
9597 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
9598#endif
Tim Petersab034fa2002-02-01 11:27:43 +00009599#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009600 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009601#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009602#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009603 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009604#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009605#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009606 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009607#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009608#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009609 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009610#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009611#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009612 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009613#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009614#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009615 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009616#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00009617 {"open", posix_open, METH_VARARGS, posix_open__doc__},
9618 {"close", posix_close, METH_VARARGS, posix_close__doc__},
9619 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
9620 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
9621 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
9622 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009623#ifdef HAVE_LOCKF
9624 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
9625#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009626 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
9627 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009628#ifdef HAVE_READV
9629 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
9630#endif
9631#ifdef HAVE_PREAD
9632 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
9633#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009634 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009635#ifdef HAVE_WRITEV
9636 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
9637#endif
9638#ifdef HAVE_PWRITE
9639 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
9640#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009641#ifdef HAVE_SENDFILE
9642 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
9643 posix_sendfile__doc__},
9644#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009645 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
9646 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009647#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009648 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009649#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009650#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +02009651 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009652#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009653#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00009654 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009655#endif
Neal Norwitz11690112002-07-30 01:08:28 +00009656#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00009657 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00009658#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009659#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00009660 {"major", posix_major, METH_VARARGS, posix_major__doc__},
9661 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
9662 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009663#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009664#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00009665 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009666#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009667#ifdef HAVE_TRUNCATE
9668 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
9669#endif
9670#ifdef HAVE_POSIX_FALLOCATE
9671 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
9672#endif
9673#ifdef HAVE_POSIX_FADVISE
9674 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
9675#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009676#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009677 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009678#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009679#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009680 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00009681#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009682 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009683#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00009684 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009685#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009686#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009687 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009688#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009689#ifdef HAVE_SYNC
9690 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
9691#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009692#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009693 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009694#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00009695#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009696#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00009697 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00009698#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009699#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009700 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009701#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00009702#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00009703 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009704#endif /* WIFSTOPPED */
9705#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00009706 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009707#endif /* WIFSIGNALED */
9708#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00009709 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009710#endif /* WIFEXITED */
9711#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00009712 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009713#endif /* WEXITSTATUS */
9714#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009715 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009716#endif /* WTERMSIG */
9717#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009718 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009719#endif /* WSTOPSIG */
9720#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009721#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009722 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009723#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00009724#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009725 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009726#endif
Fred Drakec9680921999-12-13 16:37:25 +00009727#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00009728 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009729#endif
9730#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009731 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009732#endif
9733#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009734 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009735#endif
9736#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009737 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009738#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009739 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009740#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009741 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00009742 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00009743 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -05009744 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009745 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00009746#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009747#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00009748 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009749#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009750 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009751 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009752 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009753 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00009754 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009755 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009756#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009757 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009758#endif
9759#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009760 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009761#endif
9762#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009763 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009764#endif
9765#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009766 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009767#endif
9768
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009769/* posix *at family of functions */
9770#ifdef HAVE_FACCESSAT
9771 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
9772#endif
9773#ifdef HAVE_FCHMODAT
9774 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
9775#endif /* HAVE_FCHMODAT */
9776#ifdef HAVE_FCHOWNAT
9777 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
9778#endif /* HAVE_FCHOWNAT */
9779#ifdef HAVE_FSTATAT
9780 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
9781#endif
9782#ifdef HAVE_FUTIMESAT
9783 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
9784#endif
9785#ifdef HAVE_LINKAT
9786 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
9787#endif /* HAVE_LINKAT */
9788#ifdef HAVE_MKDIRAT
9789 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
9790#endif
9791#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9792 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
9793#endif
9794#ifdef HAVE_OPENAT
9795 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
9796#endif
9797#ifdef HAVE_READLINKAT
9798 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
9799#endif /* HAVE_READLINKAT */
9800#ifdef HAVE_RENAMEAT
9801 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
9802#endif
9803#if HAVE_SYMLINKAT
9804 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
9805#endif /* HAVE_SYMLINKAT */
9806#ifdef HAVE_UNLINKAT
9807 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
9808#endif
9809#ifdef HAVE_UTIMENSAT
9810 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
9811#endif
9812#ifdef HAVE_MKFIFOAT
9813 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
9814#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009815 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009816};
9817
9818
Barry Warsaw4a342091996-12-19 23:50:02 +00009819static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009820ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009821{
Victor Stinner8c62be82010-05-06 00:08:46 +00009822 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009823}
9824
Guido van Rossumd48f2521997-12-05 22:19:34 +00009825#if defined(PYOS_OS2)
9826/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009827static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009828{
9829 APIRET rc;
9830 ULONG values[QSV_MAX+1];
9831 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009832 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009833
9834 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009835 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009836 Py_END_ALLOW_THREADS
9837
9838 if (rc != NO_ERROR) {
9839 os2_error(rc);
9840 return -1;
9841 }
9842
Fred Drake4d1e64b2002-04-15 19:40:07 +00009843 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9844 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9845 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9846 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9847 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9848 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9849 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009850
9851 switch (values[QSV_VERSION_MINOR]) {
9852 case 0: ver = "2.00"; break;
9853 case 10: ver = "2.10"; break;
9854 case 11: ver = "2.11"; break;
9855 case 30: ver = "3.00"; break;
9856 case 40: ver = "4.00"; break;
9857 case 50: ver = "5.00"; break;
9858 default:
Tim Peters885d4572001-11-28 20:27:42 +00009859 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00009860 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009861 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009862 ver = &tmp[0];
9863 }
9864
9865 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009866 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009867 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009868
9869 /* Add Indicator of Which Drive was Used to Boot the System */
9870 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9871 tmp[1] = ':';
9872 tmp[2] = '\0';
9873
Fred Drake4d1e64b2002-04-15 19:40:07 +00009874 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009875}
9876#endif
9877
Brian Curtin52173d42010-12-02 18:29:18 +00009878#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009879static int
Brian Curtin52173d42010-12-02 18:29:18 +00009880enable_symlink()
9881{
9882 HANDLE tok;
9883 TOKEN_PRIVILEGES tok_priv;
9884 LUID luid;
9885 int meth_idx = 0;
9886
9887 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009888 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009889
9890 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009891 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009892
9893 tok_priv.PrivilegeCount = 1;
9894 tok_priv.Privileges[0].Luid = luid;
9895 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
9896
9897 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
9898 sizeof(TOKEN_PRIVILEGES),
9899 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009900 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009901
Brian Curtin3b4499c2010-12-28 14:31:47 +00009902 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
9903 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00009904}
9905#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
9906
Barry Warsaw4a342091996-12-19 23:50:02 +00009907static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009908all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009909{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009910#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009911 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009912#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009913#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009914 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009915#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009916#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009917 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009918#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009919#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009920 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009921#endif
Fred Drakec9680921999-12-13 16:37:25 +00009922#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009923 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009924#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009925#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009926 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009927#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009928#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009929 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009930#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009931#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00009932 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009933#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009934#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00009935 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009936#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009937#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009938 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009939#endif
9940#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009941 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009942#endif
9943#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00009944 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009945#endif
9946#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00009947 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009948#endif
9949#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009950 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009951#endif
9952#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00009953 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009954#endif
9955#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009956 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009957#endif
9958#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009959 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009960#endif
9961#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009962 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009963#endif
9964#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009965 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009966#endif
9967#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009968 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009969#endif
9970#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00009971 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009972#endif
9973#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009974 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009975#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009976#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009977 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009978#endif
9979#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00009980 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009981#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009982#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009983 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009984#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009985#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009986 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009987#endif
9988#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009989 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009990#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009991#ifdef PRIO_PROCESS
9992 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
9993#endif
9994#ifdef PRIO_PGRP
9995 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
9996#endif
9997#ifdef PRIO_USER
9998 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
9999#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010000#ifdef O_CLOEXEC
10001 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
10002#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010003/* posix - constants for *at functions */
10004#ifdef AT_SYMLINK_NOFOLLOW
10005 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
10006#endif
10007#ifdef AT_EACCESS
10008 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
10009#endif
10010#ifdef AT_FDCWD
10011 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
10012#endif
10013#ifdef AT_REMOVEDIR
10014 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
10015#endif
10016#ifdef AT_SYMLINK_FOLLOW
10017 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
10018#endif
10019#ifdef UTIME_NOW
10020 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10021#endif
10022#ifdef UTIME_OMIT
10023 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10024#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010025
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010026
Tim Peters5aa91602002-01-30 05:46:57 +000010027/* MS Windows */
10028#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010029 /* Don't inherit in child processes. */
10030 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010031#endif
10032#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 /* Optimize for short life (keep in memory). */
10034 /* MS forgot to define this one with a non-underscore form too. */
10035 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010036#endif
10037#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010038 /* Automatically delete when last handle is closed. */
10039 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010040#endif
10041#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000010042 /* Optimize for random access. */
10043 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010044#endif
10045#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010046 /* Optimize for sequential access. */
10047 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010048#endif
10049
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010050/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010051#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010052 /* Send a SIGIO signal whenever input or output
10053 becomes available on file descriptor */
10054 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010055#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010056#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010057 /* Direct disk access. */
10058 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010059#endif
10060#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000010061 /* Must be a directory. */
10062 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010063#endif
10064#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000010065 /* Do not follow links. */
10066 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010067#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010068#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 /* Do not update the access time. */
10070 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010071#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000010072
Victor Stinner8c62be82010-05-06 00:08:46 +000010073 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010074#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010075 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010076#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010077#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010078 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010079#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010080#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010081 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010082#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010083#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010084 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010085#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010086#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000010087 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010088#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010089#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000010090 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010091#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010092#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010093 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010094#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010095#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000010096 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010097#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010098#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010099 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010100#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010101#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010102 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010103#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010104#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010105 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010106#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010107#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010108 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010109#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010110#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000010111 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010112#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010113#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000010114 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010115#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010116#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010117 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010118#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010119#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010121#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010122#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010124#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010125
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010126 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010127#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010128 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010129#endif /* ST_RDONLY */
10130#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010131 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010132#endif /* ST_NOSUID */
10133
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010134 /* FreeBSD sendfile() constants */
10135#ifdef SF_NODISKIO
10136 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
10137#endif
10138#ifdef SF_MNOWAIT
10139 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
10140#endif
10141#ifdef SF_SYNC
10142 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
10143#endif
10144
Ross Lagerwall7807c352011-03-17 20:20:30 +020010145 /* constants for posix_fadvise */
10146#ifdef POSIX_FADV_NORMAL
10147 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
10148#endif
10149#ifdef POSIX_FADV_SEQUENTIAL
10150 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
10151#endif
10152#ifdef POSIX_FADV_RANDOM
10153 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
10154#endif
10155#ifdef POSIX_FADV_NOREUSE
10156 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
10157#endif
10158#ifdef POSIX_FADV_WILLNEED
10159 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
10160#endif
10161#ifdef POSIX_FADV_DONTNEED
10162 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
10163#endif
10164
10165 /* constants for waitid */
10166#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
10167 if (ins(d, "P_PID", (long)P_PID)) return -1;
10168 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
10169 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
10170#endif
10171#ifdef WEXITED
10172 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
10173#endif
10174#ifdef WNOWAIT
10175 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
10176#endif
10177#ifdef WSTOPPED
10178 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
10179#endif
10180#ifdef CLD_EXITED
10181 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
10182#endif
10183#ifdef CLD_DUMPED
10184 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
10185#endif
10186#ifdef CLD_TRAPPED
10187 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
10188#endif
10189#ifdef CLD_CONTINUED
10190 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
10191#endif
10192
10193 /* constants for lockf */
10194#ifdef F_LOCK
10195 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
10196#endif
10197#ifdef F_TLOCK
10198 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
10199#endif
10200#ifdef F_ULOCK
10201 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
10202#endif
10203#ifdef F_TEST
10204 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10205#endif
10206
10207 /* constants for futimens */
10208#ifdef UTIME_NOW
10209 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10210#endif
10211#ifdef UTIME_OMIT
10212 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10213#endif
10214
Guido van Rossum246bc171999-02-01 23:54:31 +000010215#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010216#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010217 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10218 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10219 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10220 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10221 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10222 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10223 if (ins(d, "P_PM", (long)P_PM)) return -1;
10224 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10225 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10226 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10227 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10228 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10229 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10230 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10231 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10232 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10233 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10234 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10235 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10236 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010237#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010238 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10239 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10240 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10241 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10242 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010243#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010244#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010245
Guido van Rossumd48f2521997-12-05 22:19:34 +000010246#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010247 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010248#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010249 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010250}
10251
10252
Tim Peters5aa91602002-01-30 05:46:57 +000010253#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010254#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010255#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010256
10257#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010258#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010259#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010260
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010261#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010262#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010263#define MODNAME "posix"
10264#endif
10265
Martin v. Löwis1a214512008-06-11 05:26:20 +000010266static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010267 PyModuleDef_HEAD_INIT,
10268 MODNAME,
10269 posix__doc__,
10270 -1,
10271 posix_methods,
10272 NULL,
10273 NULL,
10274 NULL,
10275 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010276};
10277
10278
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010279PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010280INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010281{
Victor Stinner8c62be82010-05-06 00:08:46 +000010282 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010283
Brian Curtin52173d42010-12-02 18:29:18 +000010284#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010285 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010286#endif
10287
Victor Stinner8c62be82010-05-06 00:08:46 +000010288 m = PyModule_Create(&posixmodule);
10289 if (m == NULL)
10290 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010291
Victor Stinner8c62be82010-05-06 00:08:46 +000010292 /* Initialize environ dictionary */
10293 v = convertenviron();
10294 Py_XINCREF(v);
10295 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10296 return NULL;
10297 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010298
Victor Stinner8c62be82010-05-06 00:08:46 +000010299 if (all_ins(m))
10300 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010301
Victor Stinner8c62be82010-05-06 00:08:46 +000010302 if (setup_confname_tables(m))
10303 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010304
Victor Stinner8c62be82010-05-06 00:08:46 +000010305 Py_INCREF(PyExc_OSError);
10306 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010307
Guido van Rossumb3d39562000-01-31 18:41:26 +000010308#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010309 if (posix_putenv_garbage == NULL)
10310 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010311#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010312
Victor Stinner8c62be82010-05-06 00:08:46 +000010313 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010314#if defined(HAVE_WAITID) && !defined(__APPLE__)
10315 waitid_result_desc.name = MODNAME ".waitid_result";
10316 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10317#endif
10318
Victor Stinner8c62be82010-05-06 00:08:46 +000010319 stat_result_desc.name = MODNAME ".stat_result";
10320 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10321 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10322 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10323 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10324 structseq_new = StatResultType.tp_new;
10325 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010326
Victor Stinner8c62be82010-05-06 00:08:46 +000010327 statvfs_result_desc.name = MODNAME ".statvfs_result";
10328 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010329#ifdef NEED_TICKS_PER_SECOND
10330# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010331 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010332# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010333 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010334# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010336# endif
10337#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010338 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010339#if defined(HAVE_WAITID) && !defined(__APPLE__)
10340 Py_INCREF((PyObject*) &WaitidResultType);
10341 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10342#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010343 Py_INCREF((PyObject*) &StatResultType);
10344 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10345 Py_INCREF((PyObject*) &StatVFSResultType);
10346 PyModule_AddObject(m, "statvfs_result",
10347 (PyObject*) &StatVFSResultType);
10348 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010349
10350#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010351 /*
10352 * Step 2 of weak-linking support on Mac OS X.
10353 *
10354 * The code below removes functions that are not available on the
10355 * currently active platform.
10356 *
10357 * This block allow one to use a python binary that was build on
10358 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10359 * OSX 10.4.
10360 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010361#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010362 if (fstatvfs == NULL) {
10363 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10364 return NULL;
10365 }
10366 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010367#endif /* HAVE_FSTATVFS */
10368
10369#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010370 if (statvfs == NULL) {
10371 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10372 return NULL;
10373 }
10374 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010375#endif /* HAVE_STATVFS */
10376
10377# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010378 if (lchown == NULL) {
10379 if (PyObject_DelAttrString(m, "lchown") == -1) {
10380 return NULL;
10381 }
10382 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010383#endif /* HAVE_LCHOWN */
10384
10385
10386#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000010387 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010388
Guido van Rossumb6775db1994-08-01 11:34:53 +000010389}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010390
10391#ifdef __cplusplus
10392}
10393#endif