blob: 2d26a0e3fb1b6daf0fb4bffc93a36214fe9a90dc [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
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000512win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
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;
517 const wchar_t *ptr;
518 wchar_t *buf;
519 size_t len;
520
521 if (0 == DeviceIoControl(
522 reparse_point_handle,
523 FSCTL_GET_REPARSE_POINT,
524 NULL, 0, /* in buffer */
525 target_buffer, sizeof(target_buffer),
526 &n_bytes_returned,
527 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000528 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000529
530 if (reparse_tag)
531 *reparse_tag = rdb->ReparseTag;
532
533 if (target_path) {
534 switch (rdb->ReparseTag) {
535 case IO_REPARSE_TAG_SYMLINK:
536 /* XXX: Maybe should use SubstituteName? */
537 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
538 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
539 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
540 break;
541 case IO_REPARSE_TAG_MOUNT_POINT:
542 ptr = rdb->MountPointReparseBuffer.PathBuffer +
543 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
544 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
545 break;
546 default:
547 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000548 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000549 }
550 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
551 if (!buf) {
552 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000553 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000554 }
555 wcsncpy(buf, ptr, len);
556 buf[len] = L'\0';
557 if (wcsncmp(buf, L"\\??\\", 4) == 0)
558 buf[1] = L'\\';
559 *target_path = buf;
560 }
561
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000562 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000563}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000564#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000565
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000567#ifdef WITH_NEXT_FRAMEWORK
568/* On Darwin/MacOSX a shared library or framework has no access to
569** environ directly, we must obtain it with _NSGetEnviron().
570*/
571#include <crt_externs.h>
572static char **environ;
573#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000575#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576
Barry Warsaw53699e91996-12-10 23:23:01 +0000577static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000578convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579{
Victor Stinner8c62be82010-05-06 00:08:46 +0000580 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000581#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000582 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000583#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000584 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000585#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000586#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000587 APIRET rc;
588 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
589#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000590
Victor Stinner8c62be82010-05-06 00:08:46 +0000591 d = PyDict_New();
592 if (d == NULL)
593 return NULL;
594#ifdef WITH_NEXT_FRAMEWORK
595 if (environ == NULL)
596 environ = *_NSGetEnviron();
597#endif
598#ifdef MS_WINDOWS
599 /* _wenviron must be initialized in this way if the program is started
600 through main() instead of wmain(). */
601 _wgetenv(L"");
602 if (_wenviron == NULL)
603 return d;
604 /* This part ignores errors */
605 for (e = _wenviron; *e != NULL; e++) {
606 PyObject *k;
607 PyObject *v;
608 wchar_t *p = wcschr(*e, L'=');
609 if (p == NULL)
610 continue;
611 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
612 if (k == NULL) {
613 PyErr_Clear();
614 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000615 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000616 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
617 if (v == NULL) {
618 PyErr_Clear();
619 Py_DECREF(k);
620 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000621 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000622 if (PyDict_GetItem(d, k) == NULL) {
623 if (PyDict_SetItem(d, k, v) != 0)
624 PyErr_Clear();
625 }
626 Py_DECREF(k);
627 Py_DECREF(v);
628 }
629#else
630 if (environ == NULL)
631 return d;
632 /* This part ignores errors */
633 for (e = environ; *e != NULL; e++) {
634 PyObject *k;
635 PyObject *v;
636 char *p = strchr(*e, '=');
637 if (p == NULL)
638 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000639 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000640 if (k == NULL) {
641 PyErr_Clear();
642 continue;
643 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000644 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000645 if (v == NULL) {
646 PyErr_Clear();
647 Py_DECREF(k);
648 continue;
649 }
650 if (PyDict_GetItem(d, k) == NULL) {
651 if (PyDict_SetItem(d, k, v) != 0)
652 PyErr_Clear();
653 }
654 Py_DECREF(k);
655 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000656 }
657#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000658#if defined(PYOS_OS2)
659 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
660 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
661 PyObject *v = PyBytes_FromString(buffer);
662 PyDict_SetItemString(d, "BEGINLIBPATH", v);
663 Py_DECREF(v);
664 }
665 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
666 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
667 PyObject *v = PyBytes_FromString(buffer);
668 PyDict_SetItemString(d, "ENDLIBPATH", v);
669 Py_DECREF(v);
670 }
671#endif
672 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673}
674
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675/* Set a POSIX-specific error from errno, and return NULL */
676
Barry Warsawd58d7641998-07-23 16:14:40 +0000677static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000678posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000679{
Victor Stinner8c62be82010-05-06 00:08:46 +0000680 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681}
Barry Warsawd58d7641998-07-23 16:14:40 +0000682static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000683posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000684{
Victor Stinner8c62be82010-05-06 00:08:46 +0000685 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000686}
687
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000688
Mark Hammondef8b6542001-05-13 08:04:26 +0000689static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000690posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000691{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000692 PyObject *name_str, *rc;
693 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
694 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000695 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000696 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
697 name_str);
698 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000699 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000700}
701
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000702#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000703static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000704win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000705{
Victor Stinner8c62be82010-05-06 00:08:46 +0000706 /* XXX We should pass the function name along in the future.
707 (winreg.c also wants to pass the function name.)
708 This would however require an additional param to the
709 Windows error object, which is non-trivial.
710 */
711 errno = GetLastError();
712 if (filename)
713 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
714 else
715 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000716}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000717
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000718static PyObject *
719win32_error_unicode(char* function, Py_UNICODE* filename)
720{
Victor Stinner8c62be82010-05-06 00:08:46 +0000721 /* XXX - see win32_error for comments on 'function' */
722 errno = GetLastError();
723 if (filename)
724 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
725 else
726 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000727}
728
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000730convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000731{
Victor Stinner8c62be82010-05-06 00:08:46 +0000732 if (PyUnicode_CheckExact(*param))
733 Py_INCREF(*param);
734 else if (PyUnicode_Check(*param))
735 /* For a Unicode subtype that's not a Unicode object,
736 return a true Unicode object with the same data. */
737 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
738 PyUnicode_GET_SIZE(*param));
739 else
740 *param = PyUnicode_FromEncodedObject(*param,
741 Py_FileSystemDefaultEncoding,
742 "strict");
743 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000744}
745
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000746#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747
Guido van Rossumd48f2521997-12-05 22:19:34 +0000748#if defined(PYOS_OS2)
749/**********************************************************************
750 * Helper Function to Trim and Format OS/2 Messages
751 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000752static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000753os2_formatmsg(char *msgbuf, int msglen, char *reason)
754{
755 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
756
757 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
758 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
759
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000760 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000761 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
762 }
763
764 /* Add Optional Reason Text */
765 if (reason) {
766 strcat(msgbuf, " : ");
767 strcat(msgbuf, reason);
768 }
769}
770
771/**********************************************************************
772 * Decode an OS/2 Operating System Error Code
773 *
774 * A convenience function to lookup an OS/2 error code and return a
775 * text message we can use to raise a Python exception.
776 *
777 * Notes:
778 * The messages for errors returned from the OS/2 kernel reside in
779 * the file OSO001.MSG in the \OS2 directory hierarchy.
780 *
781 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000782static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000783os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
784{
785 APIRET rc;
786 ULONG msglen;
787
788 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
789 Py_BEGIN_ALLOW_THREADS
790 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
791 errorcode, "oso001.msg", &msglen);
792 Py_END_ALLOW_THREADS
793
794 if (rc == NO_ERROR)
795 os2_formatmsg(msgbuf, msglen, reason);
796 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000797 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000798 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000799
800 return msgbuf;
801}
802
803/* Set an OS/2-specific error and return NULL. OS/2 kernel
804 errors are not in a global variable e.g. 'errno' nor are
805 they congruent with posix error numbers. */
806
Victor Stinner8c62be82010-05-06 00:08:46 +0000807static PyObject *
808os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000809{
810 char text[1024];
811 PyObject *v;
812
813 os2_strerror(text, sizeof(text), code, "");
814
815 v = Py_BuildValue("(is)", code, text);
816 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000817 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000818 Py_DECREF(v);
819 }
820 return NULL; /* Signal to Python that an Exception is Pending */
821}
822
823#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000824
825/* POSIX generic methods */
826
Barry Warsaw53699e91996-12-10 23:23:01 +0000827static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000828posix_fildes(PyObject *fdobj, int (*func)(int))
829{
Victor Stinner8c62be82010-05-06 00:08:46 +0000830 int fd;
831 int res;
832 fd = PyObject_AsFileDescriptor(fdobj);
833 if (fd < 0)
834 return NULL;
835 if (!_PyVerify_fd(fd))
836 return posix_error();
837 Py_BEGIN_ALLOW_THREADS
838 res = (*func)(fd);
839 Py_END_ALLOW_THREADS
840 if (res < 0)
841 return posix_error();
842 Py_INCREF(Py_None);
843 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000844}
Guido van Rossum21142a01999-01-08 21:05:37 +0000845
846static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000848{
Victor Stinner8c62be82010-05-06 00:08:46 +0000849 PyObject *opath1 = NULL;
850 char *path1;
851 int res;
852 if (!PyArg_ParseTuple(args, format,
853 PyUnicode_FSConverter, &opath1))
854 return NULL;
855 path1 = PyBytes_AsString(opath1);
856 Py_BEGIN_ALLOW_THREADS
857 res = (*func)(path1);
858 Py_END_ALLOW_THREADS
859 if (res < 0)
860 return posix_error_with_allocated_filename(opath1);
861 Py_DECREF(opath1);
862 Py_INCREF(Py_None);
863 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000864}
865
Barry Warsaw53699e91996-12-10 23:23:01 +0000866static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000867posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000868 char *format,
869 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000870{
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 PyObject *opath1 = NULL, *opath2 = NULL;
872 char *path1, *path2;
873 int res;
874 if (!PyArg_ParseTuple(args, format,
875 PyUnicode_FSConverter, &opath1,
876 PyUnicode_FSConverter, &opath2)) {
877 return NULL;
878 }
879 path1 = PyBytes_AsString(opath1);
880 path2 = PyBytes_AsString(opath2);
881 Py_BEGIN_ALLOW_THREADS
882 res = (*func)(path1, path2);
883 Py_END_ALLOW_THREADS
884 Py_DECREF(opath1);
885 Py_DECREF(opath2);
886 if (res != 0)
887 /* XXX how to report both path1 and path2??? */
888 return posix_error();
889 Py_INCREF(Py_None);
890 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891}
892
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000893#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000895win32_1str(PyObject* args, char* func,
896 char* format, BOOL (__stdcall *funcA)(LPCSTR),
897 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000898{
Victor Stinner8c62be82010-05-06 00:08:46 +0000899 PyObject *uni;
900 char *ansi;
901 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000902
Victor Stinner8c62be82010-05-06 00:08:46 +0000903 if (!PyArg_ParseTuple(args, wformat, &uni))
904 PyErr_Clear();
905 else {
906 Py_BEGIN_ALLOW_THREADS
907 result = funcW(PyUnicode_AsUnicode(uni));
908 Py_END_ALLOW_THREADS
909 if (!result)
910 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
911 Py_INCREF(Py_None);
912 return Py_None;
913 }
914 if (!PyArg_ParseTuple(args, format, &ansi))
915 return NULL;
916 Py_BEGIN_ALLOW_THREADS
917 result = funcA(ansi);
918 Py_END_ALLOW_THREADS
919 if (!result)
920 return win32_error(func, ansi);
921 Py_INCREF(Py_None);
922 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923
924}
925
926/* This is a reimplementation of the C library's chdir function,
927 but one that produces Win32 errors instead of DOS error codes.
928 chdir is essentially a wrapper around SetCurrentDirectory; however,
929 it also needs to set "magic" environment variables indicating
930 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000931static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932win32_chdir(LPCSTR path)
933{
Victor Stinner8c62be82010-05-06 00:08:46 +0000934 char new_path[MAX_PATH+1];
935 int result;
936 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000937
Victor Stinner8c62be82010-05-06 00:08:46 +0000938 if(!SetCurrentDirectoryA(path))
939 return FALSE;
940 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
941 if (!result)
942 return FALSE;
943 /* In the ANSI API, there should not be any paths longer
944 than MAX_PATH. */
945 assert(result <= MAX_PATH+1);
946 if (strncmp(new_path, "\\\\", 2) == 0 ||
947 strncmp(new_path, "//", 2) == 0)
948 /* UNC path, nothing to do. */
949 return TRUE;
950 env[1] = new_path[0];
951 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000952}
953
954/* The Unicode version differs from the ANSI version
955 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000956static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000957win32_wchdir(LPCWSTR path)
958{
Victor Stinner8c62be82010-05-06 00:08:46 +0000959 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
960 int result;
961 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962
Victor Stinner8c62be82010-05-06 00:08:46 +0000963 if(!SetCurrentDirectoryW(path))
964 return FALSE;
965 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
966 if (!result)
967 return FALSE;
968 if (result > MAX_PATH+1) {
969 new_path = malloc(result * sizeof(wchar_t));
970 if (!new_path) {
971 SetLastError(ERROR_OUTOFMEMORY);
972 return FALSE;
973 }
974 result = GetCurrentDirectoryW(result, new_path);
975 if (!result) {
976 free(new_path);
977 return FALSE;
978 }
979 }
980 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
981 wcsncmp(new_path, L"//", 2) == 0)
982 /* UNC path, nothing to do. */
983 return TRUE;
984 env[1] = new_path[0];
985 result = SetEnvironmentVariableW(env, new_path);
986 if (new_path != _new_path)
987 free(new_path);
988 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000989}
990#endif
991
Martin v. Löwis14694662006-02-03 12:54:16 +0000992#ifdef MS_WINDOWS
993/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
994 - time stamps are restricted to second resolution
995 - file modification times suffer from forth-and-back conversions between
996 UTC and local time
997 Therefore, we implement our own stat, based on the Win32 API directly.
998*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000999#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001000
1001struct win32_stat{
1002 int st_dev;
1003 __int64 st_ino;
1004 unsigned short st_mode;
1005 int st_nlink;
1006 int st_uid;
1007 int st_gid;
1008 int st_rdev;
1009 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001010 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001011 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001012 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001013 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001014 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001015 int st_ctime_nsec;
1016};
1017
1018static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1019
1020static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001021FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001022{
Victor Stinner8c62be82010-05-06 00:08:46 +00001023 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1024 /* Cannot simply cast and dereference in_ptr,
1025 since it might not be aligned properly */
1026 __int64 in;
1027 memcpy(&in, in_ptr, sizeof(in));
1028 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001029 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001030}
1031
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001033time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001034{
Victor Stinner8c62be82010-05-06 00:08:46 +00001035 /* XXX endianness */
1036 __int64 out;
1037 out = time_in + secs_between_epochs;
1038 out = out * 10000000 + nsec_in / 100;
1039 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040}
1041
Martin v. Löwis14694662006-02-03 12:54:16 +00001042/* Below, we *know* that ugo+r is 0444 */
1043#if _S_IREAD != 0400
1044#error Unsupported C library
1045#endif
1046static int
1047attributes_to_mode(DWORD attr)
1048{
Victor Stinner8c62be82010-05-06 00:08:46 +00001049 int m = 0;
1050 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1051 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1052 else
1053 m |= _S_IFREG;
1054 if (attr & FILE_ATTRIBUTE_READONLY)
1055 m |= 0444;
1056 else
1057 m |= 0666;
1058 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001059}
1060
1061static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001062attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001063{
Victor Stinner8c62be82010-05-06 00:08:46 +00001064 memset(result, 0, sizeof(*result));
1065 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1066 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1067 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1068 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1069 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001070 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001071 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001072 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1073 /* first clear the S_IFMT bits */
1074 result->st_mode ^= (result->st_mode & 0170000);
1075 /* now set the bits that make this a symlink */
1076 result->st_mode |= 0120000;
1077 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001078
Victor Stinner8c62be82010-05-06 00:08:46 +00001079 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001080}
1081
Guido van Rossumd8faa362007-04-27 19:54:29 +00001082static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001083attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001084{
Victor Stinner8c62be82010-05-06 00:08:46 +00001085 HANDLE hFindFile;
1086 WIN32_FIND_DATAA FileData;
1087 hFindFile = FindFirstFileA(pszFile, &FileData);
1088 if (hFindFile == INVALID_HANDLE_VALUE)
1089 return FALSE;
1090 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001091 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001092 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001093 info->dwFileAttributes = FileData.dwFileAttributes;
1094 info->ftCreationTime = FileData.ftCreationTime;
1095 info->ftLastAccessTime = FileData.ftLastAccessTime;
1096 info->ftLastWriteTime = FileData.ftLastWriteTime;
1097 info->nFileSizeHigh = FileData.nFileSizeHigh;
1098 info->nFileSizeLow = FileData.nFileSizeLow;
1099/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001100 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1101 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001102 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001103}
1104
1105static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001106attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001107{
Victor Stinner8c62be82010-05-06 00:08:46 +00001108 HANDLE hFindFile;
1109 WIN32_FIND_DATAW FileData;
1110 hFindFile = FindFirstFileW(pszFile, &FileData);
1111 if (hFindFile == INVALID_HANDLE_VALUE)
1112 return FALSE;
1113 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001114 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001115 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001116 info->dwFileAttributes = FileData.dwFileAttributes;
1117 info->ftCreationTime = FileData.ftCreationTime;
1118 info->ftLastAccessTime = FileData.ftLastAccessTime;
1119 info->ftLastWriteTime = FileData.ftLastWriteTime;
1120 info->nFileSizeHigh = FileData.nFileSizeHigh;
1121 info->nFileSizeLow = FileData.nFileSizeLow;
1122/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001123 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1124 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001125 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001126}
1127
Brian Curtinf5e76d02010-11-24 13:14:05 +00001128#ifndef SYMLOOP_MAX
1129#define SYMLOOP_MAX ( 88 )
1130#endif
1131
1132static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001133win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001134
1135static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001136win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001137{
1138 int code;
1139 HANDLE hFile;
1140 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001141 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001142 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001143 const char *dot;
1144
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001145 if (depth > SYMLOOP_MAX) {
1146 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1147 return -1;
1148 }
1149
Brian Curtinf5e76d02010-11-24 13:14:05 +00001150 hFile = CreateFileA(
1151 path,
1152 0, /* desired access */
1153 0, /* share mode */
1154 NULL, /* security attributes */
1155 OPEN_EXISTING,
1156 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1157 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1158 NULL);
1159
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001160 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001161 /* Either the target doesn't exist, or we don't have access to
1162 get a handle to it. If the former, we need to return an error.
1163 If the latter, we can use attributes_from_dir. */
1164 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001165 return -1;
1166 /* Could not get attributes on open file. Fall back to
1167 reading the directory. */
1168 if (!attributes_from_dir(path, &info, &reparse_tag))
1169 /* Very strange. This should not fail now */
1170 return -1;
1171 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1172 if (traverse) {
1173 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001174 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001175 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001176 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001177 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001178 } else {
1179 if (!GetFileInformationByHandle(hFile, &info)) {
1180 CloseHandle(hFile);
1181 return -1;;
1182 }
1183 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1184 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1185 CloseHandle(hFile);
1186 if (code < 0)
1187 return code;
1188 if (traverse) {
1189 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1190 free(target_path);
1191 return code;
1192 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001193 } else
1194 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001195 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001196 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001197
1198 /* Set S_IEXEC if it is an .exe, .bat, ... */
1199 dot = strrchr(path, '.');
1200 if (dot) {
1201 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1202 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1203 result->st_mode |= 0111;
1204 }
1205 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001206}
1207
1208static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001209win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001210{
1211 int code;
1212 HANDLE hFile;
1213 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001214 ULONG reparse_tag = 0;
1215 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001216 const wchar_t *dot;
1217
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001218 if (depth > SYMLOOP_MAX) {
1219 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1220 return -1;
1221 }
1222
Brian Curtinf5e76d02010-11-24 13:14:05 +00001223 hFile = CreateFileW(
1224 path,
1225 0, /* desired access */
1226 0, /* share mode */
1227 NULL, /* security attributes */
1228 OPEN_EXISTING,
1229 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1230 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1231 NULL);
1232
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001233 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001234 /* Either the target doesn't exist, or we don't have access to
1235 get a handle to it. If the former, we need to return an error.
1236 If the latter, we can use attributes_from_dir. */
1237 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001238 return -1;
1239 /* Could not get attributes on open file. Fall back to
1240 reading the directory. */
1241 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1242 /* Very strange. This should not fail now */
1243 return -1;
1244 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1245 if (traverse) {
1246 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001247 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001248 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001249 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001250 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001251 } else {
1252 if (!GetFileInformationByHandle(hFile, &info)) {
1253 CloseHandle(hFile);
1254 return -1;;
1255 }
1256 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1257 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1258 CloseHandle(hFile);
1259 if (code < 0)
1260 return code;
1261 if (traverse) {
1262 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1263 free(target_path);
1264 return code;
1265 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001266 } else
1267 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001269 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001270
1271 /* Set S_IEXEC if it is an .exe, .bat, ... */
1272 dot = wcsrchr(path, '.');
1273 if (dot) {
1274 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1275 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1276 result->st_mode |= 0111;
1277 }
1278 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001279}
1280
1281static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001282win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001283{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001284 /* Protocol violation: we explicitly clear errno, instead of
1285 setting it to a POSIX error. Callers should use GetLastError. */
1286 int code = win32_xstat_impl(path, result, traverse, 0);
1287 errno = 0;
1288 return code;
1289}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001290
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001291static int
1292win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1293{
1294 /* Protocol violation: we explicitly clear errno, instead of
1295 setting it to a POSIX error. Callers should use GetLastError. */
1296 int code = win32_xstat_impl_w(path, result, traverse, 0);
1297 errno = 0;
1298 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001299}
1300
Brian Curtind40e6f72010-07-08 21:39:08 +00001301/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1302 win32_stat_w
1303
1304 In Posix, stat automatically traverses symlinks and returns the stat
1305 structure for the target. In Windows, the equivalent GetFileAttributes by
1306 default does not traverse symlinks and instead returns attributes for
1307 the symlink.
1308
1309 Therefore, win32_lstat will get the attributes traditionally, and
1310 win32_stat will first explicitly resolve the symlink target and then will
1311 call win32_lstat on that result.
1312
Ezio Melotti4969f702011-03-15 05:59:46 +02001313 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001314
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001315static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001316win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001317{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001318 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001319}
1320
Victor Stinner8c62be82010-05-06 00:08:46 +00001321static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001322win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001323{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001324 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001325}
1326
1327static int
1328win32_stat(const char* path, struct win32_stat *result)
1329{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001330 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001331}
1332
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001333static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001334win32_stat_w(const wchar_t* path, struct win32_stat *result)
1335{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001336 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001337}
1338
1339static int
1340win32_fstat(int file_number, struct win32_stat *result)
1341{
Victor Stinner8c62be82010-05-06 00:08:46 +00001342 BY_HANDLE_FILE_INFORMATION info;
1343 HANDLE h;
1344 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001345
Victor Stinner8c62be82010-05-06 00:08:46 +00001346 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001347
Victor Stinner8c62be82010-05-06 00:08:46 +00001348 /* Protocol violation: we explicitly clear errno, instead of
1349 setting it to a POSIX error. Callers should use GetLastError. */
1350 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001351
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 if (h == INVALID_HANDLE_VALUE) {
1353 /* This is really a C library error (invalid file handle).
1354 We set the Win32 error to the closes one matching. */
1355 SetLastError(ERROR_INVALID_HANDLE);
1356 return -1;
1357 }
1358 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001359
Victor Stinner8c62be82010-05-06 00:08:46 +00001360 type = GetFileType(h);
1361 if (type == FILE_TYPE_UNKNOWN) {
1362 DWORD error = GetLastError();
1363 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001364 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001365 }
1366 /* else: valid but unknown file */
1367 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001368
Victor Stinner8c62be82010-05-06 00:08:46 +00001369 if (type != FILE_TYPE_DISK) {
1370 if (type == FILE_TYPE_CHAR)
1371 result->st_mode = _S_IFCHR;
1372 else if (type == FILE_TYPE_PIPE)
1373 result->st_mode = _S_IFIFO;
1374 return 0;
1375 }
1376
1377 if (!GetFileInformationByHandle(h, &info)) {
1378 return -1;
1379 }
1380
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001381 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001382 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001383 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1384 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001385}
1386
1387#endif /* MS_WINDOWS */
1388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001389PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001390"stat_result: Result from stat or lstat.\n\n\
1391This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001392 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001393or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1394\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001395Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1396or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001397\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001398See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001399
1400static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001401 {"st_mode", "protection bits"},
1402 {"st_ino", "inode"},
1403 {"st_dev", "device"},
1404 {"st_nlink", "number of hard links"},
1405 {"st_uid", "user ID of owner"},
1406 {"st_gid", "group ID of owner"},
1407 {"st_size", "total size, in bytes"},
1408 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1409 {NULL, "integer time of last access"},
1410 {NULL, "integer time of last modification"},
1411 {NULL, "integer time of last change"},
1412 {"st_atime", "time of last access"},
1413 {"st_mtime", "time of last modification"},
1414 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001415#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001417#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001418#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001419 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001420#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001421#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001423#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001424#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001425 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001426#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001427#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001428 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001429#endif
1430#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001431 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001432#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001433 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001434};
1435
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001436#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001437#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001438#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001439#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001440#endif
1441
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001442#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001443#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1444#else
1445#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1446#endif
1447
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001448#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001449#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1450#else
1451#define ST_RDEV_IDX ST_BLOCKS_IDX
1452#endif
1453
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001454#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1455#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1456#else
1457#define ST_FLAGS_IDX ST_RDEV_IDX
1458#endif
1459
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001460#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001461#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001462#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001463#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001464#endif
1465
1466#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1467#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1468#else
1469#define ST_BIRTHTIME_IDX ST_GEN_IDX
1470#endif
1471
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001472static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001473 "stat_result", /* name */
1474 stat_result__doc__, /* doc */
1475 stat_result_fields,
1476 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001477};
1478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001480"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1481This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001482 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001483or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001484\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001485See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001486
1487static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 {"f_bsize", },
1489 {"f_frsize", },
1490 {"f_blocks", },
1491 {"f_bfree", },
1492 {"f_bavail", },
1493 {"f_files", },
1494 {"f_ffree", },
1495 {"f_favail", },
1496 {"f_flag", },
1497 {"f_namemax",},
1498 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001499};
1500
1501static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 "statvfs_result", /* name */
1503 statvfs_result__doc__, /* doc */
1504 statvfs_result_fields,
1505 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001506};
1507
Ross Lagerwall7807c352011-03-17 20:20:30 +02001508#if defined(HAVE_WAITID) && !defined(__APPLE__)
1509PyDoc_STRVAR(waitid_result__doc__,
1510"waitid_result: Result from waitid.\n\n\
1511This object may be accessed either as a tuple of\n\
1512 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1513or via the attributes si_pid, si_uid, and so on.\n\
1514\n\
1515See os.waitid for more information.");
1516
1517static PyStructSequence_Field waitid_result_fields[] = {
1518 {"si_pid", },
1519 {"si_uid", },
1520 {"si_signo", },
1521 {"si_status", },
1522 {"si_code", },
1523 {0}
1524};
1525
1526static PyStructSequence_Desc waitid_result_desc = {
1527 "waitid_result", /* name */
1528 waitid_result__doc__, /* doc */
1529 waitid_result_fields,
1530 5
1531};
1532static PyTypeObject WaitidResultType;
1533#endif
1534
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001535static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001536static PyTypeObject StatResultType;
1537static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001538static newfunc structseq_new;
1539
1540static PyObject *
1541statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1542{
Victor Stinner8c62be82010-05-06 00:08:46 +00001543 PyStructSequence *result;
1544 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001545
Victor Stinner8c62be82010-05-06 00:08:46 +00001546 result = (PyStructSequence*)structseq_new(type, args, kwds);
1547 if (!result)
1548 return NULL;
1549 /* If we have been initialized from a tuple,
1550 st_?time might be set to None. Initialize it
1551 from the int slots. */
1552 for (i = 7; i <= 9; i++) {
1553 if (result->ob_item[i+3] == Py_None) {
1554 Py_DECREF(Py_None);
1555 Py_INCREF(result->ob_item[i]);
1556 result->ob_item[i+3] = result->ob_item[i];
1557 }
1558 }
1559 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001560}
1561
1562
1563
1564/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001565static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001566
1567PyDoc_STRVAR(stat_float_times__doc__,
1568"stat_float_times([newval]) -> oldval\n\n\
1569Determine whether os.[lf]stat represents time stamps as float objects.\n\
1570If newval is True, future calls to stat() return floats, if it is False,\n\
1571future calls return ints. \n\
1572If newval is omitted, return the current setting.\n");
1573
1574static PyObject*
1575stat_float_times(PyObject* self, PyObject *args)
1576{
Victor Stinner8c62be82010-05-06 00:08:46 +00001577 int newval = -1;
1578 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1579 return NULL;
1580 if (newval == -1)
1581 /* Return old value */
1582 return PyBool_FromLong(_stat_float_times);
1583 _stat_float_times = newval;
1584 Py_INCREF(Py_None);
1585 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001586}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001587
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001588static void
1589fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1590{
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001592#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001594#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001596#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001597 if (!ival)
1598 return;
1599 if (_stat_float_times) {
1600 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1601 } else {
1602 fval = ival;
1603 Py_INCREF(fval);
1604 }
1605 PyStructSequence_SET_ITEM(v, index, ival);
1606 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001607}
1608
Tim Peters5aa91602002-01-30 05:46:57 +00001609/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001610 (used by posix_stat() and posix_fstat()) */
1611static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001612_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001613{
Victor Stinner8c62be82010-05-06 00:08:46 +00001614 unsigned long ansec, mnsec, cnsec;
1615 PyObject *v = PyStructSequence_New(&StatResultType);
1616 if (v == NULL)
1617 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001618
Victor Stinner8c62be82010-05-06 00:08:46 +00001619 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001620#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 PyStructSequence_SET_ITEM(v, 1,
1622 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001623#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001624 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001625#endif
1626#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 PyStructSequence_SET_ITEM(v, 2,
1628 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001629#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001630 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001631#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001632 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1633 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1634 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001635#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001636 PyStructSequence_SET_ITEM(v, 6,
1637 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001638#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001639 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001640#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001641
Martin v. Löwis14694662006-02-03 12:54:16 +00001642#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001643 ansec = st->st_atim.tv_nsec;
1644 mnsec = st->st_mtim.tv_nsec;
1645 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001646#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001647 ansec = st->st_atimespec.tv_nsec;
1648 mnsec = st->st_mtimespec.tv_nsec;
1649 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001650#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001651 ansec = st->st_atime_nsec;
1652 mnsec = st->st_mtime_nsec;
1653 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001654#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001656#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001657 fill_time(v, 7, st->st_atime, ansec);
1658 fill_time(v, 8, st->st_mtime, mnsec);
1659 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001660
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001661#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1663 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001664#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001665#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001666 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1667 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001668#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001669#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1671 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001672#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001673#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001674 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1675 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001676#endif
1677#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001678 {
1679 PyObject *val;
1680 unsigned long bsec,bnsec;
1681 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001682#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001684#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001686#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001687 if (_stat_float_times) {
1688 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1689 } else {
1690 val = PyLong_FromLong((long)bsec);
1691 }
1692 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1693 val);
1694 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001695#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001696#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001697 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1698 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001699#endif
Fred Drake699f3522000-06-29 21:12:41 +00001700
Victor Stinner8c62be82010-05-06 00:08:46 +00001701 if (PyErr_Occurred()) {
1702 Py_DECREF(v);
1703 return NULL;
1704 }
Fred Drake699f3522000-06-29 21:12:41 +00001705
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001707}
1708
Barry Warsaw53699e91996-12-10 23:23:01 +00001709static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001710posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001712#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001713 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001714#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001715 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001716#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001717 char *wformat,
1718 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001719{
Victor Stinner8c62be82010-05-06 00:08:46 +00001720 STRUCT_STAT st;
1721 PyObject *opath;
1722 char *path;
1723 int res;
1724 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001725
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001726#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001727 PyUnicodeObject *po;
1728 if (PyArg_ParseTuple(args, wformat, &po)) {
1729 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001730
Victor Stinner8c62be82010-05-06 00:08:46 +00001731 Py_BEGIN_ALLOW_THREADS
1732 /* PyUnicode_AS_UNICODE result OK without
1733 thread lock as it is a simple dereference. */
1734 res = wstatfunc(wpath, &st);
1735 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001736
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 if (res != 0)
1738 return win32_error_unicode("stat", wpath);
1739 return _pystat_fromstructstat(&st);
1740 }
1741 /* Drop the argument parsing error as narrow strings
1742 are also valid. */
1743 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001744#endif
1745
Victor Stinner8c62be82010-05-06 00:08:46 +00001746 if (!PyArg_ParseTuple(args, format,
1747 PyUnicode_FSConverter, &opath))
1748 return NULL;
1749 path = PyBytes_AsString(opath);
1750 Py_BEGIN_ALLOW_THREADS
1751 res = (*statfunc)(path, &st);
1752 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001753
Victor Stinner8c62be82010-05-06 00:08:46 +00001754 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001755#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001756 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001757#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001759#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 }
1761 else
1762 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001763
Victor Stinner8c62be82010-05-06 00:08:46 +00001764 Py_DECREF(opath);
1765 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001766}
1767
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001768/* POSIX methods */
1769
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001771"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001772Use the real uid/gid to test for access to a path. Note that most\n\
1773operations will use the effective uid/gid, therefore this routine can\n\
1774be used in a suid/sgid environment to test if the invoking user has the\n\
1775specified access to the path. The mode argument can be F_OK to test\n\
1776existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001777
1778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001779posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001780{
Victor Stinner8c62be82010-05-06 00:08:46 +00001781 PyObject *opath;
1782 char *path;
1783 int mode;
1784
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001785#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001786 DWORD attr;
1787 PyUnicodeObject *po;
1788 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1789 Py_BEGIN_ALLOW_THREADS
1790 /* PyUnicode_AS_UNICODE OK without thread lock as
1791 it is a simple dereference. */
1792 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1793 Py_END_ALLOW_THREADS
1794 goto finish;
1795 }
1796 /* Drop the argument parsing error as narrow strings
1797 are also valid. */
1798 PyErr_Clear();
1799 if (!PyArg_ParseTuple(args, "O&i:access",
1800 PyUnicode_FSConverter, &opath, &mode))
1801 return NULL;
1802 path = PyBytes_AsString(opath);
1803 Py_BEGIN_ALLOW_THREADS
1804 attr = GetFileAttributesA(path);
1805 Py_END_ALLOW_THREADS
1806 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001807finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001808 if (attr == 0xFFFFFFFF)
1809 /* File does not exist, or cannot read attributes */
1810 return PyBool_FromLong(0);
1811 /* Access is possible if either write access wasn't requested, or
1812 the file isn't read-only, or if it's a directory, as there are
1813 no read-only directories on Windows. */
1814 return PyBool_FromLong(!(mode & 2)
1815 || !(attr & FILE_ATTRIBUTE_READONLY)
1816 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001817#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001818 int res;
1819 if (!PyArg_ParseTuple(args, "O&i:access",
1820 PyUnicode_FSConverter, &opath, &mode))
1821 return NULL;
1822 path = PyBytes_AsString(opath);
1823 Py_BEGIN_ALLOW_THREADS
1824 res = access(path, mode);
1825 Py_END_ALLOW_THREADS
1826 Py_DECREF(opath);
1827 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001828#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001829}
1830
Guido van Rossumd371ff11999-01-25 16:12:23 +00001831#ifndef F_OK
1832#define F_OK 0
1833#endif
1834#ifndef R_OK
1835#define R_OK 4
1836#endif
1837#ifndef W_OK
1838#define W_OK 2
1839#endif
1840#ifndef X_OK
1841#define X_OK 1
1842#endif
1843
1844#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001846"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001848
1849static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001850posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001851{
Victor Stinner8c62be82010-05-06 00:08:46 +00001852 int id;
1853 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001854
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1856 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001857
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001858#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 /* file descriptor 0 only, the default input device (stdin) */
1860 if (id == 0) {
1861 ret = ttyname();
1862 }
1863 else {
1864 ret = NULL;
1865 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001866#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001867 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001868#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001869 if (ret == NULL)
1870 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001871 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001872}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001873#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001874
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001875#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001877"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001879
1880static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001881posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001882{
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 char *ret;
1884 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001885
Greg Wardb48bc172000-03-01 21:51:56 +00001886#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001887 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001888#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001889 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001890#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001891 if (ret == NULL)
1892 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001893 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001894}
1895#endif
1896
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001898"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001899Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001900
Barry Warsaw53699e91996-12-10 23:23:01 +00001901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001902posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001903{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001904#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001906#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001907 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001908#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001909 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001910#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001911 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001912#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001913}
1914
Fred Drake4d1e64b2002-04-15 19:40:07 +00001915#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001916PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001917"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001918Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001919opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001920
1921static PyObject *
1922posix_fchdir(PyObject *self, PyObject *fdobj)
1923{
Victor Stinner8c62be82010-05-06 00:08:46 +00001924 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001925}
1926#endif /* HAVE_FCHDIR */
1927
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001928
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001929PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001930"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001931Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001932
Barry Warsaw53699e91996-12-10 23:23:01 +00001933static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001934posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001935{
Victor Stinner8c62be82010-05-06 00:08:46 +00001936 PyObject *opath = NULL;
1937 char *path = NULL;
1938 int i;
1939 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001940#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001941 DWORD attr;
1942 PyUnicodeObject *po;
1943 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1944 Py_BEGIN_ALLOW_THREADS
1945 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1946 if (attr != 0xFFFFFFFF) {
1947 if (i & _S_IWRITE)
1948 attr &= ~FILE_ATTRIBUTE_READONLY;
1949 else
1950 attr |= FILE_ATTRIBUTE_READONLY;
1951 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1952 }
1953 else
1954 res = 0;
1955 Py_END_ALLOW_THREADS
1956 if (!res)
1957 return win32_error_unicode("chmod",
1958 PyUnicode_AS_UNICODE(po));
1959 Py_INCREF(Py_None);
1960 return Py_None;
1961 }
1962 /* Drop the argument parsing error as narrow strings
1963 are also valid. */
1964 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001965
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1967 &opath, &i))
1968 return NULL;
1969 path = PyBytes_AsString(opath);
1970 Py_BEGIN_ALLOW_THREADS
1971 attr = GetFileAttributesA(path);
1972 if (attr != 0xFFFFFFFF) {
1973 if (i & _S_IWRITE)
1974 attr &= ~FILE_ATTRIBUTE_READONLY;
1975 else
1976 attr |= FILE_ATTRIBUTE_READONLY;
1977 res = SetFileAttributesA(path, attr);
1978 }
1979 else
1980 res = 0;
1981 Py_END_ALLOW_THREADS
1982 if (!res) {
1983 win32_error("chmod", path);
1984 Py_DECREF(opath);
1985 return NULL;
1986 }
1987 Py_DECREF(opath);
1988 Py_INCREF(Py_None);
1989 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001990#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001991 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1992 &opath, &i))
1993 return NULL;
1994 path = PyBytes_AsString(opath);
1995 Py_BEGIN_ALLOW_THREADS
1996 res = chmod(path, i);
1997 Py_END_ALLOW_THREADS
1998 if (res < 0)
1999 return posix_error_with_allocated_filename(opath);
2000 Py_DECREF(opath);
2001 Py_INCREF(Py_None);
2002 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002003#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002004}
2005
Christian Heimes4e30a842007-11-30 22:12:06 +00002006#ifdef HAVE_FCHMOD
2007PyDoc_STRVAR(posix_fchmod__doc__,
2008"fchmod(fd, mode)\n\n\
2009Change the access permissions of the file given by file\n\
2010descriptor fd.");
2011
2012static PyObject *
2013posix_fchmod(PyObject *self, PyObject *args)
2014{
Victor Stinner8c62be82010-05-06 00:08:46 +00002015 int fd, mode, res;
2016 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2017 return NULL;
2018 Py_BEGIN_ALLOW_THREADS
2019 res = fchmod(fd, mode);
2020 Py_END_ALLOW_THREADS
2021 if (res < 0)
2022 return posix_error();
2023 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002024}
2025#endif /* HAVE_FCHMOD */
2026
2027#ifdef HAVE_LCHMOD
2028PyDoc_STRVAR(posix_lchmod__doc__,
2029"lchmod(path, mode)\n\n\
2030Change the access permissions of a file. If path is a symlink, this\n\
2031affects the link itself rather than the target.");
2032
2033static PyObject *
2034posix_lchmod(PyObject *self, PyObject *args)
2035{
Victor Stinner8c62be82010-05-06 00:08:46 +00002036 PyObject *opath;
2037 char *path;
2038 int i;
2039 int res;
2040 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2041 &opath, &i))
2042 return NULL;
2043 path = PyBytes_AsString(opath);
2044 Py_BEGIN_ALLOW_THREADS
2045 res = lchmod(path, i);
2046 Py_END_ALLOW_THREADS
2047 if (res < 0)
2048 return posix_error_with_allocated_filename(opath);
2049 Py_DECREF(opath);
2050 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002051}
2052#endif /* HAVE_LCHMOD */
2053
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002054
Thomas Wouterscf297e42007-02-23 15:07:44 +00002055#ifdef HAVE_CHFLAGS
2056PyDoc_STRVAR(posix_chflags__doc__,
2057"chflags(path, flags)\n\n\
2058Set file flags.");
2059
2060static PyObject *
2061posix_chflags(PyObject *self, PyObject *args)
2062{
Victor Stinner8c62be82010-05-06 00:08:46 +00002063 PyObject *opath;
2064 char *path;
2065 unsigned long flags;
2066 int res;
2067 if (!PyArg_ParseTuple(args, "O&k:chflags",
2068 PyUnicode_FSConverter, &opath, &flags))
2069 return NULL;
2070 path = PyBytes_AsString(opath);
2071 Py_BEGIN_ALLOW_THREADS
2072 res = chflags(path, flags);
2073 Py_END_ALLOW_THREADS
2074 if (res < 0)
2075 return posix_error_with_allocated_filename(opath);
2076 Py_DECREF(opath);
2077 Py_INCREF(Py_None);
2078 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002079}
2080#endif /* HAVE_CHFLAGS */
2081
2082#ifdef HAVE_LCHFLAGS
2083PyDoc_STRVAR(posix_lchflags__doc__,
2084"lchflags(path, flags)\n\n\
2085Set file flags.\n\
2086This function will not follow symbolic links.");
2087
2088static PyObject *
2089posix_lchflags(PyObject *self, PyObject *args)
2090{
Victor Stinner8c62be82010-05-06 00:08:46 +00002091 PyObject *opath;
2092 char *path;
2093 unsigned long flags;
2094 int res;
2095 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2096 PyUnicode_FSConverter, &opath, &flags))
2097 return NULL;
2098 path = PyBytes_AsString(opath);
2099 Py_BEGIN_ALLOW_THREADS
2100 res = lchflags(path, flags);
2101 Py_END_ALLOW_THREADS
2102 if (res < 0)
2103 return posix_error_with_allocated_filename(opath);
2104 Py_DECREF(opath);
2105 Py_INCREF(Py_None);
2106 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002107}
2108#endif /* HAVE_LCHFLAGS */
2109
Martin v. Löwis244edc82001-10-04 22:44:26 +00002110#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002111PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002112"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002113Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002114
2115static PyObject *
2116posix_chroot(PyObject *self, PyObject *args)
2117{
Victor Stinner8c62be82010-05-06 00:08:46 +00002118 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002119}
2120#endif
2121
Guido van Rossum21142a01999-01-08 21:05:37 +00002122#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002123PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002124"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002125force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002126
2127static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002128posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002129{
Stefan Krah0e803b32010-11-26 16:16:47 +00002130 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002131}
2132#endif /* HAVE_FSYNC */
2133
Ross Lagerwall7807c352011-03-17 20:20:30 +02002134#ifdef HAVE_SYNC
2135PyDoc_STRVAR(posix_sync__doc__,
2136"sync()\n\n\
2137Force write of everything to disk.");
2138
2139static PyObject *
2140posix_sync(PyObject *self, PyObject *noargs)
2141{
2142 Py_BEGIN_ALLOW_THREADS
2143 sync();
2144 Py_END_ALLOW_THREADS
2145 Py_RETURN_NONE;
2146}
2147#endif
2148
Guido van Rossum21142a01999-01-08 21:05:37 +00002149#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002150
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002151#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002152extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2153#endif
2154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002155PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002156"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002157force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002158 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002159
2160static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002161posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002162{
Stefan Krah0e803b32010-11-26 16:16:47 +00002163 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002164}
2165#endif /* HAVE_FDATASYNC */
2166
2167
Fredrik Lundh10723342000-07-10 16:38:09 +00002168#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002169PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002170"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002171Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002172
Barry Warsaw53699e91996-12-10 23:23:01 +00002173static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002174posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002175{
Victor Stinner8c62be82010-05-06 00:08:46 +00002176 PyObject *opath;
2177 char *path;
2178 long uid, gid;
2179 int res;
2180 if (!PyArg_ParseTuple(args, "O&ll:chown",
2181 PyUnicode_FSConverter, &opath,
2182 &uid, &gid))
2183 return NULL;
2184 path = PyBytes_AsString(opath);
2185 Py_BEGIN_ALLOW_THREADS
2186 res = chown(path, (uid_t) uid, (gid_t) gid);
2187 Py_END_ALLOW_THREADS
2188 if (res < 0)
2189 return posix_error_with_allocated_filename(opath);
2190 Py_DECREF(opath);
2191 Py_INCREF(Py_None);
2192 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002193}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002194#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002195
Christian Heimes4e30a842007-11-30 22:12:06 +00002196#ifdef HAVE_FCHOWN
2197PyDoc_STRVAR(posix_fchown__doc__,
2198"fchown(fd, uid, gid)\n\n\
2199Change the owner and group id of the file given by file descriptor\n\
2200fd to the numeric uid and gid.");
2201
2202static PyObject *
2203posix_fchown(PyObject *self, PyObject *args)
2204{
Victor Stinner8c62be82010-05-06 00:08:46 +00002205 int fd;
2206 long uid, gid;
2207 int res;
2208 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2209 return NULL;
2210 Py_BEGIN_ALLOW_THREADS
2211 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2212 Py_END_ALLOW_THREADS
2213 if (res < 0)
2214 return posix_error();
2215 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002216}
2217#endif /* HAVE_FCHOWN */
2218
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002219#ifdef HAVE_LCHOWN
2220PyDoc_STRVAR(posix_lchown__doc__,
2221"lchown(path, uid, gid)\n\n\
2222Change the owner and group id of path to the numeric uid and gid.\n\
2223This function will not follow symbolic links.");
2224
2225static PyObject *
2226posix_lchown(PyObject *self, PyObject *args)
2227{
Victor Stinner8c62be82010-05-06 00:08:46 +00002228 PyObject *opath;
2229 char *path;
2230 long uid, gid;
2231 int res;
2232 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2233 PyUnicode_FSConverter, &opath,
2234 &uid, &gid))
2235 return NULL;
2236 path = PyBytes_AsString(opath);
2237 Py_BEGIN_ALLOW_THREADS
2238 res = lchown(path, (uid_t) uid, (gid_t) gid);
2239 Py_END_ALLOW_THREADS
2240 if (res < 0)
2241 return posix_error_with_allocated_filename(opath);
2242 Py_DECREF(opath);
2243 Py_INCREF(Py_None);
2244 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002245}
2246#endif /* HAVE_LCHOWN */
2247
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002248
Guido van Rossum36bc6801995-06-14 22:54:23 +00002249#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002250static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002251posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002252{
Victor Stinner8c62be82010-05-06 00:08:46 +00002253 char buf[1026];
2254 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002255
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002256#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002257 if (!use_bytes) {
2258 wchar_t wbuf[1026];
2259 wchar_t *wbuf2 = wbuf;
2260 PyObject *resobj;
2261 DWORD len;
2262 Py_BEGIN_ALLOW_THREADS
2263 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2264 /* If the buffer is large enough, len does not include the
2265 terminating \0. If the buffer is too small, len includes
2266 the space needed for the terminator. */
2267 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2268 wbuf2 = malloc(len * sizeof(wchar_t));
2269 if (wbuf2)
2270 len = GetCurrentDirectoryW(len, wbuf2);
2271 }
2272 Py_END_ALLOW_THREADS
2273 if (!wbuf2) {
2274 PyErr_NoMemory();
2275 return NULL;
2276 }
2277 if (!len) {
2278 if (wbuf2 != wbuf) free(wbuf2);
2279 return win32_error("getcwdu", NULL);
2280 }
2281 resobj = PyUnicode_FromWideChar(wbuf2, len);
2282 if (wbuf2 != wbuf) free(wbuf2);
2283 return resobj;
2284 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002285#endif
2286
Victor Stinner8c62be82010-05-06 00:08:46 +00002287 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002288#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002289 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002290#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002291 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002292#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002293 Py_END_ALLOW_THREADS
2294 if (res == NULL)
2295 return posix_error();
2296 if (use_bytes)
2297 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002298 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002299}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002300
2301PyDoc_STRVAR(posix_getcwd__doc__,
2302"getcwd() -> path\n\n\
2303Return a unicode string representing the current working directory.");
2304
2305static PyObject *
2306posix_getcwd_unicode(PyObject *self)
2307{
2308 return posix_getcwd(0);
2309}
2310
2311PyDoc_STRVAR(posix_getcwdb__doc__,
2312"getcwdb() -> path\n\n\
2313Return a bytes string representing the current working directory.");
2314
2315static PyObject *
2316posix_getcwd_bytes(PyObject *self)
2317{
2318 return posix_getcwd(1);
2319}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002320#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002321
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002322
Guido van Rossumb6775db1994-08-01 11:34:53 +00002323#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002324PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002325"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002326Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002327
Barry Warsaw53699e91996-12-10 23:23:01 +00002328static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002329posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002330{
Victor Stinner8c62be82010-05-06 00:08:46 +00002331 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002332}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002333#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002334
Brian Curtin1b9df392010-11-24 20:24:31 +00002335#ifdef MS_WINDOWS
2336PyDoc_STRVAR(win32_link__doc__,
2337"link(src, dst)\n\n\
2338Create a hard link to a file.");
2339
2340static PyObject *
2341win32_link(PyObject *self, PyObject *args)
2342{
2343 PyObject *osrc, *odst;
2344 char *src, *dst;
2345 BOOL rslt;
2346
Brian Curtinfc889c42010-11-28 23:59:46 +00002347 PyUnicodeObject *usrc, *udst;
2348 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2349 Py_BEGIN_ALLOW_THREADS
2350 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2351 PyUnicode_AS_UNICODE(usrc), NULL);
2352 Py_END_ALLOW_THREADS
2353
2354 if (rslt == 0)
2355 return win32_error("link", NULL);
2356
2357 Py_RETURN_NONE;
2358 }
2359
2360 /* Narrow strings also valid. */
2361 PyErr_Clear();
2362
Brian Curtin1b9df392010-11-24 20:24:31 +00002363 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2364 PyUnicode_FSConverter, &odst))
2365 return NULL;
2366
2367 src = PyBytes_AsString(osrc);
2368 dst = PyBytes_AsString(odst);
2369
2370 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002371 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002372 Py_END_ALLOW_THREADS
2373
Stefan Krah30b341f2010-11-27 11:44:18 +00002374 Py_DECREF(osrc);
2375 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002376 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002377 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002378
2379 Py_RETURN_NONE;
2380}
2381#endif /* MS_WINDOWS */
2382
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002384PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002385"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002386Return a list containing the names of the entries in the directory.\n\
2387\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002388 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002389\n\
2390The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002391entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002392
Barry Warsaw53699e91996-12-10 23:23:01 +00002393static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002394posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002395{
Victor Stinner8c62be82010-05-06 00:08:46 +00002396 /* XXX Should redo this putting the (now four) versions of opendir
2397 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002398#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002399
Victor Stinner8c62be82010-05-06 00:08:46 +00002400 PyObject *d, *v;
2401 HANDLE hFindFile;
2402 BOOL result;
2403 WIN32_FIND_DATA FileData;
2404 PyObject *opath;
2405 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2406 char *bufptr = namebuf;
2407 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002408
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002409 PyObject *po = NULL;
2410 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002411 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002412 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002413
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002414 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002415 po_wchars = L".";
2416 len = 1;
2417 } else {
2418 po_wchars = PyUnicode_AS_UNICODE(po);
2419 len = PyUnicode_GET_SIZE(po);
2420 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002421 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002422 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2423 if (!wnamebuf) {
2424 PyErr_NoMemory();
2425 return NULL;
2426 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002427 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002428 if (len > 0) {
2429 Py_UNICODE wch = wnamebuf[len-1];
2430 if (wch != L'/' && wch != L'\\' && wch != L':')
2431 wnamebuf[len++] = L'\\';
2432 wcscpy(wnamebuf + len, L"*.*");
2433 }
2434 if ((d = PyList_New(0)) == NULL) {
2435 free(wnamebuf);
2436 return NULL;
2437 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002438 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002439 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002440 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002441 if (hFindFile == INVALID_HANDLE_VALUE) {
2442 int error = GetLastError();
2443 if (error == ERROR_FILE_NOT_FOUND) {
2444 free(wnamebuf);
2445 return d;
2446 }
2447 Py_DECREF(d);
2448 win32_error_unicode("FindFirstFileW", wnamebuf);
2449 free(wnamebuf);
2450 return NULL;
2451 }
2452 do {
2453 /* Skip over . and .. */
2454 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2455 wcscmp(wFileData.cFileName, L"..") != 0) {
2456 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2457 if (v == NULL) {
2458 Py_DECREF(d);
2459 d = NULL;
2460 break;
2461 }
2462 if (PyList_Append(d, v) != 0) {
2463 Py_DECREF(v);
2464 Py_DECREF(d);
2465 d = NULL;
2466 break;
2467 }
2468 Py_DECREF(v);
2469 }
2470 Py_BEGIN_ALLOW_THREADS
2471 result = FindNextFileW(hFindFile, &wFileData);
2472 Py_END_ALLOW_THREADS
2473 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2474 it got to the end of the directory. */
2475 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2476 Py_DECREF(d);
2477 win32_error_unicode("FindNextFileW", wnamebuf);
2478 FindClose(hFindFile);
2479 free(wnamebuf);
2480 return NULL;
2481 }
2482 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002483
Victor Stinner8c62be82010-05-06 00:08:46 +00002484 if (FindClose(hFindFile) == FALSE) {
2485 Py_DECREF(d);
2486 win32_error_unicode("FindClose", wnamebuf);
2487 free(wnamebuf);
2488 return NULL;
2489 }
2490 free(wnamebuf);
2491 return d;
2492 }
2493 /* Drop the argument parsing error as narrow strings
2494 are also valid. */
2495 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002496
Victor Stinner8c62be82010-05-06 00:08:46 +00002497 if (!PyArg_ParseTuple(args, "O&:listdir",
2498 PyUnicode_FSConverter, &opath))
2499 return NULL;
2500 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2501 PyErr_SetString(PyExc_ValueError, "path too long");
2502 Py_DECREF(opath);
2503 return NULL;
2504 }
2505 strcpy(namebuf, PyBytes_AsString(opath));
2506 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002507 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002508 if (len > 0) {
2509 char ch = namebuf[len-1];
2510 if (ch != SEP && ch != ALTSEP && ch != ':')
2511 namebuf[len++] = '/';
2512 strcpy(namebuf + len, "*.*");
2513 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002514
Victor Stinner8c62be82010-05-06 00:08:46 +00002515 if ((d = PyList_New(0)) == NULL)
2516 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002517
Antoine Pitroub73caab2010-08-09 23:39:31 +00002518 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002519 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002520 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002521 if (hFindFile == INVALID_HANDLE_VALUE) {
2522 int error = GetLastError();
2523 if (error == ERROR_FILE_NOT_FOUND)
2524 return d;
2525 Py_DECREF(d);
2526 return win32_error("FindFirstFile", namebuf);
2527 }
2528 do {
2529 /* Skip over . and .. */
2530 if (strcmp(FileData.cFileName, ".") != 0 &&
2531 strcmp(FileData.cFileName, "..") != 0) {
2532 v = PyBytes_FromString(FileData.cFileName);
2533 if (v == NULL) {
2534 Py_DECREF(d);
2535 d = NULL;
2536 break;
2537 }
2538 if (PyList_Append(d, v) != 0) {
2539 Py_DECREF(v);
2540 Py_DECREF(d);
2541 d = NULL;
2542 break;
2543 }
2544 Py_DECREF(v);
2545 }
2546 Py_BEGIN_ALLOW_THREADS
2547 result = FindNextFile(hFindFile, &FileData);
2548 Py_END_ALLOW_THREADS
2549 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2550 it got to the end of the directory. */
2551 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2552 Py_DECREF(d);
2553 win32_error("FindNextFile", namebuf);
2554 FindClose(hFindFile);
2555 return NULL;
2556 }
2557 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002558
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 if (FindClose(hFindFile) == FALSE) {
2560 Py_DECREF(d);
2561 return win32_error("FindClose", namebuf);
2562 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002563
Victor Stinner8c62be82010-05-06 00:08:46 +00002564 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002565
Tim Peters0bb44a42000-09-15 07:44:49 +00002566#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002567
2568#ifndef MAX_PATH
2569#define MAX_PATH CCHMAXPATH
2570#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002571 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002572 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002573 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002574 PyObject *d, *v;
2575 char namebuf[MAX_PATH+5];
2576 HDIR hdir = 1;
2577 ULONG srchcnt = 1;
2578 FILEFINDBUF3 ep;
2579 APIRET rc;
2580
Victor Stinner8c62be82010-05-06 00:08:46 +00002581 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002582 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002583 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002584 name = PyBytes_AsString(oname);
2585 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002586 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002587 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002588 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002589 return NULL;
2590 }
2591 strcpy(namebuf, name);
2592 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002593 if (*pt == ALTSEP)
2594 *pt = SEP;
2595 if (namebuf[len-1] != SEP)
2596 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002597 strcpy(namebuf + len, "*.*");
2598
Neal Norwitz6c913782007-10-14 03:23:09 +00002599 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002600 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002601 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002602 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002603
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002604 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2605 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002606 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002607 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2608 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2609 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002610
2611 if (rc != NO_ERROR) {
2612 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002613 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002614 }
2615
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002616 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002617 do {
2618 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002619 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002620 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002621
2622 strcpy(namebuf, ep.achName);
2623
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002624 /* Leave Case of Name Alone -- In Native Form */
2625 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002626
Christian Heimes72b710a2008-05-26 13:28:38 +00002627 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002628 if (v == NULL) {
2629 Py_DECREF(d);
2630 d = NULL;
2631 break;
2632 }
2633 if (PyList_Append(d, v) != 0) {
2634 Py_DECREF(v);
2635 Py_DECREF(d);
2636 d = NULL;
2637 break;
2638 }
2639 Py_DECREF(v);
2640 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2641 }
2642
Victor Stinnerdcb24032010-04-22 12:08:36 +00002643 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002644 return d;
2645#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002646 PyObject *oname;
2647 char *name;
2648 PyObject *d, *v;
2649 DIR *dirp;
2650 struct dirent *ep;
2651 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002652
Victor Stinner8c62be82010-05-06 00:08:46 +00002653 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002654 /* v is never read, so it does not need to be initialized yet. */
2655 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 arg_is_unicode = 0;
2657 PyErr_Clear();
2658 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002659 oname = NULL;
2660 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002661 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002662 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002663 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002664 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002665 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002666 Py_BEGIN_ALLOW_THREADS
2667 dirp = opendir(name);
2668 Py_END_ALLOW_THREADS
2669 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002670 return posix_error_with_allocated_filename(oname);
2671 }
2672 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002673 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002674 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002675 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002676 Py_DECREF(oname);
2677 return NULL;
2678 }
2679 for (;;) {
2680 errno = 0;
2681 Py_BEGIN_ALLOW_THREADS
2682 ep = readdir(dirp);
2683 Py_END_ALLOW_THREADS
2684 if (ep == NULL) {
2685 if (errno == 0) {
2686 break;
2687 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002688 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002689 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002690 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002691 Py_DECREF(d);
2692 return posix_error_with_allocated_filename(oname);
2693 }
2694 }
2695 if (ep->d_name[0] == '.' &&
2696 (NAMLEN(ep) == 1 ||
2697 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2698 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002699 if (arg_is_unicode)
2700 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2701 else
2702 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002703 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002704 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002705 break;
2706 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002707 if (PyList_Append(d, v) != 0) {
2708 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002709 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002710 break;
2711 }
2712 Py_DECREF(v);
2713 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002714 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002715 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002716 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002717 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002718
Victor Stinner8c62be82010-05-06 00:08:46 +00002719 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002720
Tim Peters0bb44a42000-09-15 07:44:49 +00002721#endif /* which OS */
2722} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002723
Antoine Pitrou8250e232011-02-25 23:41:16 +00002724#ifdef HAVE_FDOPENDIR
2725PyDoc_STRVAR(posix_fdlistdir__doc__,
2726"fdlistdir(fd) -> list_of_strings\n\n\
2727Like listdir(), but uses a file descriptor instead.\n\
2728After succesful execution of this function, fd will be closed.");
2729
2730static PyObject *
2731posix_fdlistdir(PyObject *self, PyObject *args)
2732{
2733 PyObject *d, *v;
2734 DIR *dirp;
2735 struct dirent *ep;
2736 int fd;
2737
2738 errno = 0;
2739 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2740 return NULL;
2741 Py_BEGIN_ALLOW_THREADS
2742 dirp = fdopendir(fd);
2743 Py_END_ALLOW_THREADS
2744 if (dirp == NULL) {
2745 close(fd);
2746 return posix_error();
2747 }
2748 if ((d = PyList_New(0)) == NULL) {
2749 Py_BEGIN_ALLOW_THREADS
2750 closedir(dirp);
2751 Py_END_ALLOW_THREADS
2752 return NULL;
2753 }
2754 for (;;) {
2755 errno = 0;
2756 Py_BEGIN_ALLOW_THREADS
2757 ep = readdir(dirp);
2758 Py_END_ALLOW_THREADS
2759 if (ep == NULL) {
2760 if (errno == 0) {
2761 break;
2762 } else {
2763 Py_BEGIN_ALLOW_THREADS
2764 closedir(dirp);
2765 Py_END_ALLOW_THREADS
2766 Py_DECREF(d);
2767 return posix_error();
2768 }
2769 }
2770 if (ep->d_name[0] == '.' &&
2771 (NAMLEN(ep) == 1 ||
2772 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2773 continue;
2774 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2775 if (v == NULL) {
2776 Py_CLEAR(d);
2777 break;
2778 }
2779 if (PyList_Append(d, v) != 0) {
2780 Py_DECREF(v);
2781 Py_CLEAR(d);
2782 break;
2783 }
2784 Py_DECREF(v);
2785 }
2786 Py_BEGIN_ALLOW_THREADS
2787 closedir(dirp);
2788 Py_END_ALLOW_THREADS
2789
2790 return d;
2791}
2792#endif
2793
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002794#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002795/* A helper function for abspath on win32 */
2796static PyObject *
2797posix__getfullpathname(PyObject *self, PyObject *args)
2798{
Victor Stinner8c62be82010-05-06 00:08:46 +00002799 PyObject *opath;
2800 char *path;
2801 char outbuf[MAX_PATH*2];
2802 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002803#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002804 PyUnicodeObject *po;
2805 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2806 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2807 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2808 Py_UNICODE *wtemp;
2809 DWORD result;
2810 PyObject *v;
2811 result = GetFullPathNameW(wpath,
2812 sizeof(woutbuf)/sizeof(woutbuf[0]),
2813 woutbuf, &wtemp);
2814 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2815 woutbufp = malloc(result * sizeof(Py_UNICODE));
2816 if (!woutbufp)
2817 return PyErr_NoMemory();
2818 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2819 }
2820 if (result)
2821 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2822 else
2823 v = win32_error_unicode("GetFullPathNameW", wpath);
2824 if (woutbufp != woutbuf)
2825 free(woutbufp);
2826 return v;
2827 }
2828 /* Drop the argument parsing error as narrow strings
2829 are also valid. */
2830 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002831
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002832#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002833 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2834 PyUnicode_FSConverter, &opath))
2835 return NULL;
2836 path = PyBytes_AsString(opath);
2837 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2838 outbuf, &temp)) {
2839 win32_error("GetFullPathName", path);
2840 Py_DECREF(opath);
2841 return NULL;
2842 }
2843 Py_DECREF(opath);
2844 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2845 return PyUnicode_Decode(outbuf, strlen(outbuf),
2846 Py_FileSystemDefaultEncoding, NULL);
2847 }
2848 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002849} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002850
Brian Curtinf5e76d02010-11-24 13:14:05 +00002851/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2852static int has_GetFinalPathNameByHandle = 0;
2853static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2854 DWORD);
2855static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2856 DWORD);
2857static int
2858check_GetFinalPathNameByHandle()
2859{
2860 HINSTANCE hKernel32;
2861 /* only recheck */
2862 if (!has_GetFinalPathNameByHandle)
2863 {
2864 hKernel32 = GetModuleHandle("KERNEL32");
2865 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2866 "GetFinalPathNameByHandleA");
2867 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2868 "GetFinalPathNameByHandleW");
2869 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2870 Py_GetFinalPathNameByHandleW;
2871 }
2872 return has_GetFinalPathNameByHandle;
2873}
2874
Brian Curtind40e6f72010-07-08 21:39:08 +00002875/* A helper function for samepath on windows */
2876static PyObject *
2877posix__getfinalpathname(PyObject *self, PyObject *args)
2878{
2879 HANDLE hFile;
2880 int buf_size;
2881 wchar_t *target_path;
2882 int result_length;
2883 PyObject *result;
2884 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002885
Brian Curtin94622b02010-09-24 00:03:39 +00002886 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002887 return NULL;
2888 }
2889
2890 if(!check_GetFinalPathNameByHandle()) {
2891 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2892 NotImplementedError. */
2893 return PyErr_Format(PyExc_NotImplementedError,
2894 "GetFinalPathNameByHandle not available on this platform");
2895 }
2896
2897 hFile = CreateFileW(
2898 path,
2899 0, /* desired access */
2900 0, /* share mode */
2901 NULL, /* security attributes */
2902 OPEN_EXISTING,
2903 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2904 FILE_FLAG_BACKUP_SEMANTICS,
2905 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002906
Brian Curtind40e6f72010-07-08 21:39:08 +00002907 if(hFile == INVALID_HANDLE_VALUE) {
2908 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002909 return PyErr_Format(PyExc_RuntimeError,
2910 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002911 }
2912
2913 /* We have a good handle to the target, use it to determine the
2914 target path name. */
2915 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2916
2917 if(!buf_size)
2918 return win32_error_unicode("GetFinalPathNameByHandle", path);
2919
2920 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2921 if(!target_path)
2922 return PyErr_NoMemory();
2923
2924 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2925 buf_size, VOLUME_NAME_DOS);
2926 if(!result_length)
2927 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2928
2929 if(!CloseHandle(hFile))
2930 return win32_error_unicode("GetFinalPathNameByHandle", path);
2931
2932 target_path[result_length] = 0;
2933 result = PyUnicode_FromUnicode(target_path, result_length);
2934 free(target_path);
2935 return result;
2936
2937} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002938
2939static PyObject *
2940posix__getfileinformation(PyObject *self, PyObject *args)
2941{
2942 HANDLE hFile;
2943 BY_HANDLE_FILE_INFORMATION info;
2944 int fd;
2945
2946 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2947 return NULL;
2948
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002949 if (!_PyVerify_fd(fd))
2950 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002951
2952 hFile = (HANDLE)_get_osfhandle(fd);
2953 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002954 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002955
2956 if (!GetFileInformationByHandle(hFile, &info))
2957 return win32_error("_getfileinformation", NULL);
2958
2959 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2960 info.nFileIndexHigh,
2961 info.nFileIndexLow);
2962}
Brian Curtin9c669cc2011-06-08 18:17:18 -05002963
Brian Curtin95d028f2011-06-09 09:10:38 -05002964PyDoc_STRVAR(posix__isdir__doc__,
2965"Return true if the pathname refers to an existing directory.");
2966
Brian Curtin9c669cc2011-06-08 18:17:18 -05002967static PyObject *
2968posix__isdir(PyObject *self, PyObject *args)
2969{
2970 PyObject *opath;
2971 char *path;
2972 PyUnicodeObject *po;
2973 DWORD attributes;
2974
2975 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
2976 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2977
2978 attributes = GetFileAttributesW(wpath);
2979 if (attributes == INVALID_FILE_ATTRIBUTES)
2980 Py_RETURN_FALSE;
2981 goto check;
2982 }
2983 /* Drop the argument parsing error as narrow strings
2984 are also valid. */
2985 PyErr_Clear();
2986
2987 if (!PyArg_ParseTuple(args, "O&:_isdir",
2988 PyUnicode_FSConverter, &opath))
2989 return NULL;
2990
2991 path = PyBytes_AsString(opath);
2992 attributes = GetFileAttributesA(path);
2993 if (attributes == INVALID_FILE_ATTRIBUTES)
2994 Py_RETURN_FALSE;
2995
2996check:
2997 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
2998 Py_RETURN_TRUE;
2999 else
3000 Py_RETURN_FALSE;
3001}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003002#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003004PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003005"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003006Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003007
Barry Warsaw53699e91996-12-10 23:23:01 +00003008static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003009posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003010{
Victor Stinner8c62be82010-05-06 00:08:46 +00003011 int res;
3012 PyObject *opath;
3013 char *path;
3014 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003015
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003016#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003017 PyUnicodeObject *po;
3018 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3019 Py_BEGIN_ALLOW_THREADS
3020 /* PyUnicode_AS_UNICODE OK without thread lock as
3021 it is a simple dereference. */
3022 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3023 Py_END_ALLOW_THREADS
3024 if (!res)
3025 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3026 Py_INCREF(Py_None);
3027 return Py_None;
3028 }
3029 /* Drop the argument parsing error as narrow strings
3030 are also valid. */
3031 PyErr_Clear();
3032 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3033 PyUnicode_FSConverter, &opath, &mode))
3034 return NULL;
3035 path = PyBytes_AsString(opath);
3036 Py_BEGIN_ALLOW_THREADS
3037 /* PyUnicode_AS_UNICODE OK without thread lock as
3038 it is a simple dereference. */
3039 res = CreateDirectoryA(path, NULL);
3040 Py_END_ALLOW_THREADS
3041 if (!res) {
3042 win32_error("mkdir", path);
3043 Py_DECREF(opath);
3044 return NULL;
3045 }
3046 Py_DECREF(opath);
3047 Py_INCREF(Py_None);
3048 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003049#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003050
Victor Stinner8c62be82010-05-06 00:08:46 +00003051 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3052 PyUnicode_FSConverter, &opath, &mode))
3053 return NULL;
3054 path = PyBytes_AsString(opath);
3055 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003056#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003057 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003058#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003059 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003060#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003061 Py_END_ALLOW_THREADS
3062 if (res < 0)
3063 return posix_error_with_allocated_filename(opath);
3064 Py_DECREF(opath);
3065 Py_INCREF(Py_None);
3066 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003067#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003068}
3069
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003070
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003071/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3072#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003073#include <sys/resource.h>
3074#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003075
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003076
3077#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003078PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003079"nice(inc) -> new_priority\n\n\
3080Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003081
Barry Warsaw53699e91996-12-10 23:23:01 +00003082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003083posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003084{
Victor Stinner8c62be82010-05-06 00:08:46 +00003085 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003086
Victor Stinner8c62be82010-05-06 00:08:46 +00003087 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3088 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003089
Victor Stinner8c62be82010-05-06 00:08:46 +00003090 /* There are two flavours of 'nice': one that returns the new
3091 priority (as required by almost all standards out there) and the
3092 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3093 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003094
Victor Stinner8c62be82010-05-06 00:08:46 +00003095 If we are of the nice family that returns the new priority, we
3096 need to clear errno before the call, and check if errno is filled
3097 before calling posix_error() on a returnvalue of -1, because the
3098 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003099
Victor Stinner8c62be82010-05-06 00:08:46 +00003100 errno = 0;
3101 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003102#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003103 if (value == 0)
3104 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003105#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003106 if (value == -1 && errno != 0)
3107 /* either nice() or getpriority() returned an error */
3108 return posix_error();
3109 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003110}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003111#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003112
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003113
3114#ifdef HAVE_GETPRIORITY
3115PyDoc_STRVAR(posix_getpriority__doc__,
3116"getpriority(which, who) -> current_priority\n\n\
3117Get program scheduling priority.");
3118
3119static PyObject *
3120posix_getpriority(PyObject *self, PyObject *args)
3121{
3122 int which, who, retval;
3123
3124 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3125 return NULL;
3126 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003127 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003128 if (errno != 0)
3129 return posix_error();
3130 return PyLong_FromLong((long)retval);
3131}
3132#endif /* HAVE_GETPRIORITY */
3133
3134
3135#ifdef HAVE_SETPRIORITY
3136PyDoc_STRVAR(posix_setpriority__doc__,
3137"setpriority(which, who, prio) -> None\n\n\
3138Set program scheduling priority.");
3139
3140static PyObject *
3141posix_setpriority(PyObject *self, PyObject *args)
3142{
3143 int which, who, prio, retval;
3144
3145 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3146 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003147 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003148 if (retval == -1)
3149 return posix_error();
3150 Py_RETURN_NONE;
3151}
3152#endif /* HAVE_SETPRIORITY */
3153
3154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003155PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003156"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003157Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003158
Barry Warsaw53699e91996-12-10 23:23:01 +00003159static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003160posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003161{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003162#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 PyObject *o1, *o2;
3164 char *p1, *p2;
3165 BOOL result;
3166 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3167 goto error;
3168 if (!convert_to_unicode(&o1))
3169 goto error;
3170 if (!convert_to_unicode(&o2)) {
3171 Py_DECREF(o1);
3172 goto error;
3173 }
3174 Py_BEGIN_ALLOW_THREADS
3175 result = MoveFileW(PyUnicode_AsUnicode(o1),
3176 PyUnicode_AsUnicode(o2));
3177 Py_END_ALLOW_THREADS
3178 Py_DECREF(o1);
3179 Py_DECREF(o2);
3180 if (!result)
3181 return win32_error("rename", NULL);
3182 Py_INCREF(Py_None);
3183 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003184error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003185 PyErr_Clear();
3186 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3187 return NULL;
3188 Py_BEGIN_ALLOW_THREADS
3189 result = MoveFileA(p1, p2);
3190 Py_END_ALLOW_THREADS
3191 if (!result)
3192 return win32_error("rename", NULL);
3193 Py_INCREF(Py_None);
3194 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003195#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003196 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003197#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003198}
3199
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003201PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003202"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003203Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003204
Barry Warsaw53699e91996-12-10 23:23:01 +00003205static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003206posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003207{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003208#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003210#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003212#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003213}
3214
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003216PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003217"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003218Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219
Barry Warsaw53699e91996-12-10 23:23:01 +00003220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003221posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003222{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003223#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003224 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003225#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003226 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003227#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003228}
3229
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003230
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003231#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003232PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003233"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003234Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003235
Barry Warsaw53699e91996-12-10 23:23:01 +00003236static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003237posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003238{
Victor Stinner8c62be82010-05-06 00:08:46 +00003239 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003240#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003241 wchar_t *command;
3242 if (!PyArg_ParseTuple(args, "u:system", &command))
3243 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003244
Victor Stinner8c62be82010-05-06 00:08:46 +00003245 Py_BEGIN_ALLOW_THREADS
3246 sts = _wsystem(command);
3247 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003248#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 PyObject *command_obj;
3250 char *command;
3251 if (!PyArg_ParseTuple(args, "O&:system",
3252 PyUnicode_FSConverter, &command_obj))
3253 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003254
Victor Stinner8c62be82010-05-06 00:08:46 +00003255 command = PyBytes_AsString(command_obj);
3256 Py_BEGIN_ALLOW_THREADS
3257 sts = system(command);
3258 Py_END_ALLOW_THREADS
3259 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003260#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003261 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003262}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003263#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003264
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003265
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003266PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003267"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003268Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003269
Barry Warsaw53699e91996-12-10 23:23:01 +00003270static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003271posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003272{
Victor Stinner8c62be82010-05-06 00:08:46 +00003273 int i;
3274 if (!PyArg_ParseTuple(args, "i:umask", &i))
3275 return NULL;
3276 i = (int)umask(i);
3277 if (i < 0)
3278 return posix_error();
3279 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003280}
3281
Brian Curtind40e6f72010-07-08 21:39:08 +00003282#ifdef MS_WINDOWS
3283
3284/* override the default DeleteFileW behavior so that directory
3285symlinks can be removed with this function, the same as with
3286Unix symlinks */
3287BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3288{
3289 WIN32_FILE_ATTRIBUTE_DATA info;
3290 WIN32_FIND_DATAW find_data;
3291 HANDLE find_data_handle;
3292 int is_directory = 0;
3293 int is_link = 0;
3294
3295 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3296 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003297
Brian Curtind40e6f72010-07-08 21:39:08 +00003298 /* Get WIN32_FIND_DATA structure for the path to determine if
3299 it is a symlink */
3300 if(is_directory &&
3301 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3302 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3303
3304 if(find_data_handle != INVALID_HANDLE_VALUE) {
3305 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3306 FindClose(find_data_handle);
3307 }
3308 }
3309 }
3310
3311 if (is_directory && is_link)
3312 return RemoveDirectoryW(lpFileName);
3313
3314 return DeleteFileW(lpFileName);
3315}
3316#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003317
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003318PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003319"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003320Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003321
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003322PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003323"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003324Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003325
Barry Warsaw53699e91996-12-10 23:23:01 +00003326static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003327posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003328{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003329#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003330 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3331 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003332#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003334#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003335}
3336
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003337
Guido van Rossumb6775db1994-08-01 11:34:53 +00003338#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003339PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003340"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003341Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003342
Barry Warsaw53699e91996-12-10 23:23:01 +00003343static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003344posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003345{
Victor Stinner8c62be82010-05-06 00:08:46 +00003346 struct utsname u;
3347 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003348
Victor Stinner8c62be82010-05-06 00:08:46 +00003349 Py_BEGIN_ALLOW_THREADS
3350 res = uname(&u);
3351 Py_END_ALLOW_THREADS
3352 if (res < 0)
3353 return posix_error();
3354 return Py_BuildValue("(sssss)",
3355 u.sysname,
3356 u.nodename,
3357 u.release,
3358 u.version,
3359 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003360}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003361#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003362
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003363static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003364extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003365{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003366 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003367 if (PyFloat_Check(t)) {
3368 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003369 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003370 if (!intobj)
3371 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003372#if SIZEOF_TIME_T > SIZEOF_LONG
3373 intval = PyLong_AsUnsignedLongLongMask(intobj);
3374#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003375 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003376#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003377 Py_DECREF(intobj);
3378 if (intval == -1 && PyErr_Occurred())
3379 return -1;
3380 *sec = intval;
3381 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3382 if (*usec < 0)
3383 /* If rounding gave us a negative number,
3384 truncate. */
3385 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003386 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003387 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003388#if SIZEOF_TIME_T > SIZEOF_LONG
3389 intval = PyLong_AsUnsignedLongLongMask(t);
3390#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003391 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003392#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003393 if (intval == -1 && PyErr_Occurred())
3394 return -1;
3395 *sec = intval;
3396 *usec = 0;
3397 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003398}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003399
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003400PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003401"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003402utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003403Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003404second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003405
Barry Warsaw53699e91996-12-10 23:23:01 +00003406static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003407posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003408{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003409#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003410 PyObject *arg;
3411 PyUnicodeObject *obwpath;
3412 wchar_t *wpath = NULL;
3413 PyObject *oapath;
3414 char *apath;
3415 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003416 time_t atimesec, mtimesec;
3417 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003418 FILETIME atime, mtime;
3419 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003420
Victor Stinner8c62be82010-05-06 00:08:46 +00003421 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3422 wpath = PyUnicode_AS_UNICODE(obwpath);
3423 Py_BEGIN_ALLOW_THREADS
3424 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3425 NULL, OPEN_EXISTING,
3426 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3427 Py_END_ALLOW_THREADS
3428 if (hFile == INVALID_HANDLE_VALUE)
3429 return win32_error_unicode("utime", wpath);
3430 } else
3431 /* Drop the argument parsing error as narrow strings
3432 are also valid. */
3433 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003434
Victor Stinner8c62be82010-05-06 00:08:46 +00003435 if (!wpath) {
3436 if (!PyArg_ParseTuple(args, "O&O:utime",
3437 PyUnicode_FSConverter, &oapath, &arg))
3438 return NULL;
3439 apath = PyBytes_AsString(oapath);
3440 Py_BEGIN_ALLOW_THREADS
3441 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3442 NULL, OPEN_EXISTING,
3443 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3444 Py_END_ALLOW_THREADS
3445 if (hFile == INVALID_HANDLE_VALUE) {
3446 win32_error("utime", apath);
3447 Py_DECREF(oapath);
3448 return NULL;
3449 }
3450 Py_DECREF(oapath);
3451 }
3452
3453 if (arg == Py_None) {
3454 SYSTEMTIME now;
3455 GetSystemTime(&now);
3456 if (!SystemTimeToFileTime(&now, &mtime) ||
3457 !SystemTimeToFileTime(&now, &atime)) {
3458 win32_error("utime", NULL);
3459 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003460 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003461 }
3462 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3463 PyErr_SetString(PyExc_TypeError,
3464 "utime() arg 2 must be a tuple (atime, mtime)");
3465 goto done;
3466 }
3467 else {
3468 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3469 &atimesec, &ausec) == -1)
3470 goto done;
3471 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3472 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3473 &mtimesec, &musec) == -1)
3474 goto done;
3475 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3476 }
3477 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3478 /* Avoid putting the file name into the error here,
3479 as that may confuse the user into believing that
3480 something is wrong with the file, when it also
3481 could be the time stamp that gives a problem. */
3482 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003483 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003484 }
3485 Py_INCREF(Py_None);
3486 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003487done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 CloseHandle(hFile);
3489 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003490#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003491
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 PyObject *opath;
3493 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003494 time_t atime, mtime;
3495 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003496 int res;
3497 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003498
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003499#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003501#define ATIME buf[0].tv_sec
3502#define MTIME buf[1].tv_sec
3503#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003504/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003505 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003506#define ATIME buf.actime
3507#define MTIME buf.modtime
3508#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003509#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003510 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003511#define ATIME buf[0]
3512#define MTIME buf[1]
3513#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003514#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003515
Mark Hammond817c9292003-12-03 01:22:38 +00003516
Victor Stinner8c62be82010-05-06 00:08:46 +00003517 if (!PyArg_ParseTuple(args, "O&O:utime",
3518 PyUnicode_FSConverter, &opath, &arg))
3519 return NULL;
3520 path = PyBytes_AsString(opath);
3521 if (arg == Py_None) {
3522 /* optional time values not given */
3523 Py_BEGIN_ALLOW_THREADS
3524 res = utime(path, NULL);
3525 Py_END_ALLOW_THREADS
3526 }
3527 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3528 PyErr_SetString(PyExc_TypeError,
3529 "utime() arg 2 must be a tuple (atime, mtime)");
3530 Py_DECREF(opath);
3531 return NULL;
3532 }
3533 else {
3534 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3535 &atime, &ausec) == -1) {
3536 Py_DECREF(opath);
3537 return NULL;
3538 }
3539 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3540 &mtime, &musec) == -1) {
3541 Py_DECREF(opath);
3542 return NULL;
3543 }
3544 ATIME = atime;
3545 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003546#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 buf[0].tv_usec = ausec;
3548 buf[1].tv_usec = musec;
3549 Py_BEGIN_ALLOW_THREADS
3550 res = utimes(path, buf);
3551 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003552#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 Py_BEGIN_ALLOW_THREADS
3554 res = utime(path, UTIME_ARG);
3555 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003556#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 }
3558 if (res < 0) {
3559 return posix_error_with_allocated_filename(opath);
3560 }
3561 Py_DECREF(opath);
3562 Py_INCREF(Py_None);
3563 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003564#undef UTIME_ARG
3565#undef ATIME
3566#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003567#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003568}
3569
Ross Lagerwall7807c352011-03-17 20:20:30 +02003570#ifdef HAVE_FUTIMES
3571PyDoc_STRVAR(posix_futimes__doc__,
3572"futimes(fd, (atime, mtime))\n\
3573futimes(fd, None)\n\n\
3574Set the access and modified time of the file specified by the file\n\
3575descriptor fd to the given values. If the second form is used, set the\n\
3576access and modified times to the current time.");
3577
3578static PyObject *
3579posix_futimes(PyObject *self, PyObject *args)
3580{
3581 int res, fd;
3582 PyObject* arg;
3583 struct timeval buf[2];
3584 long ausec, musec;
3585
3586 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3587 return NULL;
3588
3589 if (arg == Py_None) {
3590 /* optional time values not given */
3591 Py_BEGIN_ALLOW_THREADS
3592 res = futimes(fd, NULL);
3593 Py_END_ALLOW_THREADS
3594 }
3595 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3596 PyErr_SetString(PyExc_TypeError,
3597 "futimes() arg 2 must be a tuple (atime, mtime)");
3598 return NULL;
3599 }
3600 else {
3601 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3602 &(buf[0].tv_sec), &ausec) == -1) {
3603 return NULL;
3604 }
3605 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3606 &(buf[1].tv_sec), &musec) == -1) {
3607 return NULL;
3608 }
3609 buf[0].tv_usec = ausec;
3610 buf[1].tv_usec = musec;
3611 Py_BEGIN_ALLOW_THREADS
3612 res = futimes(fd, buf);
3613 Py_END_ALLOW_THREADS
3614 }
3615 if (res < 0)
3616 return posix_error();
3617 Py_RETURN_NONE;
3618}
3619#endif
3620
3621#ifdef HAVE_LUTIMES
3622PyDoc_STRVAR(posix_lutimes__doc__,
3623"lutimes(path, (atime, mtime))\n\
3624lutimes(path, None)\n\n\
3625Like utime(), but if path is a symbolic link, it is not dereferenced.");
3626
3627static PyObject *
3628posix_lutimes(PyObject *self, PyObject *args)
3629{
3630 PyObject *opath, *arg;
3631 const char *path;
3632 int res;
3633 struct timeval buf[2];
3634 long ausec, musec;
3635
3636 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3637 PyUnicode_FSConverter, &opath, &arg))
3638 return NULL;
3639 path = PyBytes_AsString(opath);
3640 if (arg == Py_None) {
3641 /* optional time values not given */
3642 Py_BEGIN_ALLOW_THREADS
3643 res = lutimes(path, NULL);
3644 Py_END_ALLOW_THREADS
3645 }
3646 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3647 PyErr_SetString(PyExc_TypeError,
3648 "lutimes() arg 2 must be a tuple (atime, mtime)");
3649 Py_DECREF(opath);
3650 return NULL;
3651 }
3652 else {
3653 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3654 &(buf[0].tv_sec), &ausec) == -1) {
3655 Py_DECREF(opath);
3656 return NULL;
3657 }
3658 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3659 &(buf[1].tv_sec), &musec) == -1) {
3660 Py_DECREF(opath);
3661 return NULL;
3662 }
3663 buf[0].tv_usec = ausec;
3664 buf[1].tv_usec = musec;
3665 Py_BEGIN_ALLOW_THREADS
3666 res = lutimes(path, buf);
3667 Py_END_ALLOW_THREADS
3668 }
3669 Py_DECREF(opath);
3670 if (res < 0)
3671 return posix_error();
3672 Py_RETURN_NONE;
3673}
3674#endif
3675
3676#ifdef HAVE_FUTIMENS
3677PyDoc_STRVAR(posix_futimens__doc__,
3678"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3679futimens(fd, None, None)\n\n\
3680Updates the timestamps of a file specified by the file descriptor fd, with\n\
3681nanosecond precision.\n\
3682The second form sets atime and mtime to the current time.\n\
3683If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3684current time.\n\
3685If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3686
3687static PyObject *
3688posix_futimens(PyObject *self, PyObject *args)
3689{
3690 int res, fd;
3691 PyObject *atime, *mtime;
3692 struct timespec buf[2];
3693
3694 if (!PyArg_ParseTuple(args, "iOO:futimens",
3695 &fd, &atime, &mtime))
3696 return NULL;
3697 if (atime == Py_None && mtime == Py_None) {
3698 /* optional time values not given */
3699 Py_BEGIN_ALLOW_THREADS
3700 res = futimens(fd, NULL);
3701 Py_END_ALLOW_THREADS
3702 }
3703 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3704 PyErr_SetString(PyExc_TypeError,
3705 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3706 return NULL;
3707 }
3708 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3709 PyErr_SetString(PyExc_TypeError,
3710 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3711 return NULL;
3712 }
3713 else {
3714 if (!PyArg_ParseTuple(atime, "ll:futimens",
3715 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3716 return NULL;
3717 }
3718 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3719 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3720 return NULL;
3721 }
3722 Py_BEGIN_ALLOW_THREADS
3723 res = futimens(fd, buf);
3724 Py_END_ALLOW_THREADS
3725 }
3726 if (res < 0)
3727 return posix_error();
3728 Py_RETURN_NONE;
3729}
3730#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003731
Guido van Rossum3b066191991-06-04 19:40:25 +00003732/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003734PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003735"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003736Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003737
Barry Warsaw53699e91996-12-10 23:23:01 +00003738static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003739posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003740{
Victor Stinner8c62be82010-05-06 00:08:46 +00003741 int sts;
3742 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3743 return NULL;
3744 _exit(sts);
3745 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003746}
3747
Martin v. Löwis114619e2002-10-07 06:44:21 +00003748#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3749static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003750free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003751{
Victor Stinner8c62be82010-05-06 00:08:46 +00003752 Py_ssize_t i;
3753 for (i = 0; i < count; i++)
3754 PyMem_Free(array[i]);
3755 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003756}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003757
Antoine Pitrou69f71142009-05-24 21:25:49 +00003758static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003759int fsconvert_strdup(PyObject *o, char**out)
3760{
Victor Stinner8c62be82010-05-06 00:08:46 +00003761 PyObject *bytes;
3762 Py_ssize_t size;
3763 if (!PyUnicode_FSConverter(o, &bytes))
3764 return 0;
3765 size = PyBytes_GET_SIZE(bytes);
3766 *out = PyMem_Malloc(size+1);
3767 if (!*out)
3768 return 0;
3769 memcpy(*out, PyBytes_AsString(bytes), size+1);
3770 Py_DECREF(bytes);
3771 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003772}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003773#endif
3774
Ross Lagerwall7807c352011-03-17 20:20:30 +02003775#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003776static char**
3777parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3778{
Victor Stinner8c62be82010-05-06 00:08:46 +00003779 char **envlist;
3780 Py_ssize_t i, pos, envc;
3781 PyObject *keys=NULL, *vals=NULL;
3782 PyObject *key, *val, *key2, *val2;
3783 char *p, *k, *v;
3784 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003785
Victor Stinner8c62be82010-05-06 00:08:46 +00003786 i = PyMapping_Size(env);
3787 if (i < 0)
3788 return NULL;
3789 envlist = PyMem_NEW(char *, i + 1);
3790 if (envlist == NULL) {
3791 PyErr_NoMemory();
3792 return NULL;
3793 }
3794 envc = 0;
3795 keys = PyMapping_Keys(env);
3796 vals = PyMapping_Values(env);
3797 if (!keys || !vals)
3798 goto error;
3799 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3800 PyErr_Format(PyExc_TypeError,
3801 "env.keys() or env.values() is not a list");
3802 goto error;
3803 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003804
Victor Stinner8c62be82010-05-06 00:08:46 +00003805 for (pos = 0; pos < i; pos++) {
3806 key = PyList_GetItem(keys, pos);
3807 val = PyList_GetItem(vals, pos);
3808 if (!key || !val)
3809 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003810
Victor Stinner8c62be82010-05-06 00:08:46 +00003811 if (PyUnicode_FSConverter(key, &key2) == 0)
3812 goto error;
3813 if (PyUnicode_FSConverter(val, &val2) == 0) {
3814 Py_DECREF(key2);
3815 goto error;
3816 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003817
3818#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3820 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003821#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003822 k = PyBytes_AsString(key2);
3823 v = PyBytes_AsString(val2);
3824 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003825
Victor Stinner8c62be82010-05-06 00:08:46 +00003826 p = PyMem_NEW(char, len);
3827 if (p == NULL) {
3828 PyErr_NoMemory();
3829 Py_DECREF(key2);
3830 Py_DECREF(val2);
3831 goto error;
3832 }
3833 PyOS_snprintf(p, len, "%s=%s", k, v);
3834 envlist[envc++] = p;
3835 Py_DECREF(key2);
3836 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003837#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003838 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003839#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003840 }
3841 Py_DECREF(vals);
3842 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003843
Victor Stinner8c62be82010-05-06 00:08:46 +00003844 envlist[envc] = 0;
3845 *envc_ptr = envc;
3846 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003847
3848error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003849 Py_XDECREF(keys);
3850 Py_XDECREF(vals);
3851 while (--envc >= 0)
3852 PyMem_DEL(envlist[envc]);
3853 PyMem_DEL(envlist);
3854 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003855}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003856
Ross Lagerwall7807c352011-03-17 20:20:30 +02003857static char**
3858parse_arglist(PyObject* argv, Py_ssize_t *argc)
3859{
3860 int i;
3861 char **argvlist = PyMem_NEW(char *, *argc+1);
3862 if (argvlist == NULL) {
3863 PyErr_NoMemory();
3864 return NULL;
3865 }
3866 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003867 PyObject* item = PySequence_ITEM(argv, i);
3868 if (item == NULL)
3869 goto fail;
3870 if (!fsconvert_strdup(item, &argvlist[i])) {
3871 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003872 goto fail;
3873 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003874 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003875 }
3876 argvlist[*argc] = NULL;
3877 return argvlist;
3878fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003879 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003880 free_string_array(argvlist, *argc);
3881 return NULL;
3882}
3883#endif
3884
3885#ifdef HAVE_EXECV
3886PyDoc_STRVAR(posix_execv__doc__,
3887"execv(path, args)\n\n\
3888Execute an executable path with arguments, replacing current process.\n\
3889\n\
3890 path: path of executable file\n\
3891 args: tuple or list of strings");
3892
3893static PyObject *
3894posix_execv(PyObject *self, PyObject *args)
3895{
3896 PyObject *opath;
3897 char *path;
3898 PyObject *argv;
3899 char **argvlist;
3900 Py_ssize_t argc;
3901
3902 /* execv has two arguments: (path, argv), where
3903 argv is a list or tuple of strings. */
3904
3905 if (!PyArg_ParseTuple(args, "O&O:execv",
3906 PyUnicode_FSConverter,
3907 &opath, &argv))
3908 return NULL;
3909 path = PyBytes_AsString(opath);
3910 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3911 PyErr_SetString(PyExc_TypeError,
3912 "execv() arg 2 must be a tuple or list");
3913 Py_DECREF(opath);
3914 return NULL;
3915 }
3916 argc = PySequence_Size(argv);
3917 if (argc < 1) {
3918 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3919 Py_DECREF(opath);
3920 return NULL;
3921 }
3922
3923 argvlist = parse_arglist(argv, &argc);
3924 if (argvlist == NULL) {
3925 Py_DECREF(opath);
3926 return NULL;
3927 }
3928
3929 execv(path, argvlist);
3930
3931 /* If we get here it's definitely an error */
3932
3933 free_string_array(argvlist, argc);
3934 Py_DECREF(opath);
3935 return posix_error();
3936}
3937
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003938PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003939"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003940Execute a path with arguments and environment, replacing current process.\n\
3941\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003942 path: path of executable file\n\
3943 args: tuple or list of arguments\n\
3944 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003945
Barry Warsaw53699e91996-12-10 23:23:01 +00003946static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003947posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003948{
Victor Stinner8c62be82010-05-06 00:08:46 +00003949 PyObject *opath;
3950 char *path;
3951 PyObject *argv, *env;
3952 char **argvlist;
3953 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003954 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003955
Victor Stinner8c62be82010-05-06 00:08:46 +00003956 /* execve has three arguments: (path, argv, env), where
3957 argv is a list or tuple of strings and env is a dictionary
3958 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003959
Victor Stinner8c62be82010-05-06 00:08:46 +00003960 if (!PyArg_ParseTuple(args, "O&OO:execve",
3961 PyUnicode_FSConverter,
3962 &opath, &argv, &env))
3963 return NULL;
3964 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003965 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003966 PyErr_SetString(PyExc_TypeError,
3967 "execve() arg 2 must be a tuple or list");
3968 goto fail_0;
3969 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02003970 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00003971 if (!PyMapping_Check(env)) {
3972 PyErr_SetString(PyExc_TypeError,
3973 "execve() arg 3 must be a mapping object");
3974 goto fail_0;
3975 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003976
Ross Lagerwall7807c352011-03-17 20:20:30 +02003977 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00003978 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003979 goto fail_0;
3980 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003981
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 envlist = parse_envlist(env, &envc);
3983 if (envlist == NULL)
3984 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003985
Victor Stinner8c62be82010-05-06 00:08:46 +00003986 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003987
Victor Stinner8c62be82010-05-06 00:08:46 +00003988 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003989
Victor Stinner8c62be82010-05-06 00:08:46 +00003990 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003991
Victor Stinner8c62be82010-05-06 00:08:46 +00003992 while (--envc >= 0)
3993 PyMem_DEL(envlist[envc]);
3994 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003995 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02003996 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003997 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003998 Py_DECREF(opath);
3999 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004000}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004001#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004002
Ross Lagerwall7807c352011-03-17 20:20:30 +02004003#ifdef HAVE_FEXECVE
4004PyDoc_STRVAR(posix_fexecve__doc__,
4005"fexecve(fd, args, env)\n\n\
4006Execute the program specified by a file descriptor with arguments and\n\
4007environment, replacing the current process.\n\
4008\n\
4009 fd: file descriptor of executable\n\
4010 args: tuple or list of arguments\n\
4011 env: dictionary of strings mapping to strings");
4012
4013static PyObject *
4014posix_fexecve(PyObject *self, PyObject *args)
4015{
4016 int fd;
4017 PyObject *argv, *env;
4018 char **argvlist;
4019 char **envlist;
4020 Py_ssize_t argc, envc;
4021
4022 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4023 &fd, &argv, &env))
4024 return NULL;
4025 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4026 PyErr_SetString(PyExc_TypeError,
4027 "fexecve() arg 2 must be a tuple or list");
4028 return NULL;
4029 }
4030 argc = PySequence_Size(argv);
4031 if (!PyMapping_Check(env)) {
4032 PyErr_SetString(PyExc_TypeError,
4033 "fexecve() arg 3 must be a mapping object");
4034 return NULL;
4035 }
4036
4037 argvlist = parse_arglist(argv, &argc);
4038 if (argvlist == NULL)
4039 return NULL;
4040
4041 envlist = parse_envlist(env, &envc);
4042 if (envlist == NULL)
4043 goto fail;
4044
4045 fexecve(fd, argvlist, envlist);
4046
4047 /* If we get here it's definitely an error */
4048
4049 (void) posix_error();
4050
4051 while (--envc >= 0)
4052 PyMem_DEL(envlist[envc]);
4053 PyMem_DEL(envlist);
4054 fail:
4055 free_string_array(argvlist, argc);
4056 return NULL;
4057}
4058#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004059
Guido van Rossuma1065681999-01-25 23:20:23 +00004060#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004061PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004062"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004063Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004064\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004065 mode: mode of process creation\n\
4066 path: path of executable file\n\
4067 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004068
4069static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004070posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004071{
Victor Stinner8c62be82010-05-06 00:08:46 +00004072 PyObject *opath;
4073 char *path;
4074 PyObject *argv;
4075 char **argvlist;
4076 int mode, i;
4077 Py_ssize_t argc;
4078 Py_intptr_t spawnval;
4079 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004080
Victor Stinner8c62be82010-05-06 00:08:46 +00004081 /* spawnv has three arguments: (mode, path, argv), where
4082 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004083
Victor Stinner8c62be82010-05-06 00:08:46 +00004084 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4085 PyUnicode_FSConverter,
4086 &opath, &argv))
4087 return NULL;
4088 path = PyBytes_AsString(opath);
4089 if (PyList_Check(argv)) {
4090 argc = PyList_Size(argv);
4091 getitem = PyList_GetItem;
4092 }
4093 else if (PyTuple_Check(argv)) {
4094 argc = PyTuple_Size(argv);
4095 getitem = PyTuple_GetItem;
4096 }
4097 else {
4098 PyErr_SetString(PyExc_TypeError,
4099 "spawnv() arg 2 must be a tuple or list");
4100 Py_DECREF(opath);
4101 return NULL;
4102 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004103
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 argvlist = PyMem_NEW(char *, argc+1);
4105 if (argvlist == NULL) {
4106 Py_DECREF(opath);
4107 return PyErr_NoMemory();
4108 }
4109 for (i = 0; i < argc; i++) {
4110 if (!fsconvert_strdup((*getitem)(argv, i),
4111 &argvlist[i])) {
4112 free_string_array(argvlist, i);
4113 PyErr_SetString(
4114 PyExc_TypeError,
4115 "spawnv() arg 2 must contain only strings");
4116 Py_DECREF(opath);
4117 return NULL;
4118 }
4119 }
4120 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004121
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004122#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004123 Py_BEGIN_ALLOW_THREADS
4124 spawnval = spawnv(mode, path, argvlist);
4125 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004126#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004127 if (mode == _OLD_P_OVERLAY)
4128 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004129
Victor Stinner8c62be82010-05-06 00:08:46 +00004130 Py_BEGIN_ALLOW_THREADS
4131 spawnval = _spawnv(mode, path, argvlist);
4132 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004133#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004134
Victor Stinner8c62be82010-05-06 00:08:46 +00004135 free_string_array(argvlist, argc);
4136 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004137
Victor Stinner8c62be82010-05-06 00:08:46 +00004138 if (spawnval == -1)
4139 return posix_error();
4140 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004141#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004142 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004143#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004144 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004145#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004146}
4147
4148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004149PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004150"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004151Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004152\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004153 mode: mode of process creation\n\
4154 path: path of executable file\n\
4155 args: tuple or list of arguments\n\
4156 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004157
4158static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004159posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004160{
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 PyObject *opath;
4162 char *path;
4163 PyObject *argv, *env;
4164 char **argvlist;
4165 char **envlist;
4166 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004167 int mode;
4168 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004169 Py_intptr_t spawnval;
4170 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4171 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004172
Victor Stinner8c62be82010-05-06 00:08:46 +00004173 /* spawnve has four arguments: (mode, path, argv, env), where
4174 argv is a list or tuple of strings and env is a dictionary
4175 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004176
Victor Stinner8c62be82010-05-06 00:08:46 +00004177 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4178 PyUnicode_FSConverter,
4179 &opath, &argv, &env))
4180 return NULL;
4181 path = PyBytes_AsString(opath);
4182 if (PyList_Check(argv)) {
4183 argc = PyList_Size(argv);
4184 getitem = PyList_GetItem;
4185 }
4186 else if (PyTuple_Check(argv)) {
4187 argc = PyTuple_Size(argv);
4188 getitem = PyTuple_GetItem;
4189 }
4190 else {
4191 PyErr_SetString(PyExc_TypeError,
4192 "spawnve() arg 2 must be a tuple or list");
4193 goto fail_0;
4194 }
4195 if (!PyMapping_Check(env)) {
4196 PyErr_SetString(PyExc_TypeError,
4197 "spawnve() arg 3 must be a mapping object");
4198 goto fail_0;
4199 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004200
Victor Stinner8c62be82010-05-06 00:08:46 +00004201 argvlist = PyMem_NEW(char *, argc+1);
4202 if (argvlist == NULL) {
4203 PyErr_NoMemory();
4204 goto fail_0;
4205 }
4206 for (i = 0; i < argc; i++) {
4207 if (!fsconvert_strdup((*getitem)(argv, i),
4208 &argvlist[i]))
4209 {
4210 lastarg = i;
4211 goto fail_1;
4212 }
4213 }
4214 lastarg = argc;
4215 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004216
Victor Stinner8c62be82010-05-06 00:08:46 +00004217 envlist = parse_envlist(env, &envc);
4218 if (envlist == NULL)
4219 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004220
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004221#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004222 Py_BEGIN_ALLOW_THREADS
4223 spawnval = spawnve(mode, path, argvlist, envlist);
4224 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004225#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 if (mode == _OLD_P_OVERLAY)
4227 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004228
Victor Stinner8c62be82010-05-06 00:08:46 +00004229 Py_BEGIN_ALLOW_THREADS
4230 spawnval = _spawnve(mode, path, argvlist, envlist);
4231 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004232#endif
Tim Peters25059d32001-12-07 20:35:43 +00004233
Victor Stinner8c62be82010-05-06 00:08:46 +00004234 if (spawnval == -1)
4235 (void) posix_error();
4236 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004237#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004239#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004240 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004241#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004242
Victor Stinner8c62be82010-05-06 00:08:46 +00004243 while (--envc >= 0)
4244 PyMem_DEL(envlist[envc]);
4245 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004246 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004247 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004248 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004249 Py_DECREF(opath);
4250 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004251}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004252
4253/* OS/2 supports spawnvp & spawnvpe natively */
4254#if defined(PYOS_OS2)
4255PyDoc_STRVAR(posix_spawnvp__doc__,
4256"spawnvp(mode, file, args)\n\n\
4257Execute the program 'file' in a new process, using the environment\n\
4258search path to find the file.\n\
4259\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004260 mode: mode of process creation\n\
4261 file: executable file name\n\
4262 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004263
4264static PyObject *
4265posix_spawnvp(PyObject *self, PyObject *args)
4266{
Victor Stinner8c62be82010-05-06 00:08:46 +00004267 PyObject *opath;
4268 char *path;
4269 PyObject *argv;
4270 char **argvlist;
4271 int mode, i, argc;
4272 Py_intptr_t spawnval;
4273 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004274
Victor Stinner8c62be82010-05-06 00:08:46 +00004275 /* spawnvp has three arguments: (mode, path, argv), where
4276 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004277
Victor Stinner8c62be82010-05-06 00:08:46 +00004278 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4279 PyUnicode_FSConverter,
4280 &opath, &argv))
4281 return NULL;
4282 path = PyBytes_AsString(opath);
4283 if (PyList_Check(argv)) {
4284 argc = PyList_Size(argv);
4285 getitem = PyList_GetItem;
4286 }
4287 else if (PyTuple_Check(argv)) {
4288 argc = PyTuple_Size(argv);
4289 getitem = PyTuple_GetItem;
4290 }
4291 else {
4292 PyErr_SetString(PyExc_TypeError,
4293 "spawnvp() arg 2 must be a tuple or list");
4294 Py_DECREF(opath);
4295 return NULL;
4296 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004297
Victor Stinner8c62be82010-05-06 00:08:46 +00004298 argvlist = PyMem_NEW(char *, argc+1);
4299 if (argvlist == NULL) {
4300 Py_DECREF(opath);
4301 return PyErr_NoMemory();
4302 }
4303 for (i = 0; i < argc; i++) {
4304 if (!fsconvert_strdup((*getitem)(argv, i),
4305 &argvlist[i])) {
4306 free_string_array(argvlist, i);
4307 PyErr_SetString(
4308 PyExc_TypeError,
4309 "spawnvp() arg 2 must contain only strings");
4310 Py_DECREF(opath);
4311 return NULL;
4312 }
4313 }
4314 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004315
Victor Stinner8c62be82010-05-06 00:08:46 +00004316 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004317#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004318 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004319#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004321#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004322 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004323
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 free_string_array(argvlist, argc);
4325 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004326
Victor Stinner8c62be82010-05-06 00:08:46 +00004327 if (spawnval == -1)
4328 return posix_error();
4329 else
4330 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004331}
4332
4333
4334PyDoc_STRVAR(posix_spawnvpe__doc__,
4335"spawnvpe(mode, file, args, env)\n\n\
4336Execute the program 'file' in a new process, using the environment\n\
4337search path to find the file.\n\
4338\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004339 mode: mode of process creation\n\
4340 file: executable file name\n\
4341 args: tuple or list of arguments\n\
4342 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004343
4344static PyObject *
4345posix_spawnvpe(PyObject *self, PyObject *args)
4346{
Victor Stinner8c62be82010-05-06 00:08:46 +00004347 PyObject *opath
4348 char *path;
4349 PyObject *argv, *env;
4350 char **argvlist;
4351 char **envlist;
4352 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004353 int mode;
4354 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004355 Py_intptr_t spawnval;
4356 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4357 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004358
Victor Stinner8c62be82010-05-06 00:08:46 +00004359 /* spawnvpe has four arguments: (mode, path, argv, env), where
4360 argv is a list or tuple of strings and env is a dictionary
4361 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004362
Victor Stinner8c62be82010-05-06 00:08:46 +00004363 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4364 PyUnicode_FSConverter,
4365 &opath, &argv, &env))
4366 return NULL;
4367 path = PyBytes_AsString(opath);
4368 if (PyList_Check(argv)) {
4369 argc = PyList_Size(argv);
4370 getitem = PyList_GetItem;
4371 }
4372 else if (PyTuple_Check(argv)) {
4373 argc = PyTuple_Size(argv);
4374 getitem = PyTuple_GetItem;
4375 }
4376 else {
4377 PyErr_SetString(PyExc_TypeError,
4378 "spawnvpe() arg 2 must be a tuple or list");
4379 goto fail_0;
4380 }
4381 if (!PyMapping_Check(env)) {
4382 PyErr_SetString(PyExc_TypeError,
4383 "spawnvpe() arg 3 must be a mapping object");
4384 goto fail_0;
4385 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004386
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 argvlist = PyMem_NEW(char *, argc+1);
4388 if (argvlist == NULL) {
4389 PyErr_NoMemory();
4390 goto fail_0;
4391 }
4392 for (i = 0; i < argc; i++) {
4393 if (!fsconvert_strdup((*getitem)(argv, i),
4394 &argvlist[i]))
4395 {
4396 lastarg = i;
4397 goto fail_1;
4398 }
4399 }
4400 lastarg = argc;
4401 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004402
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 envlist = parse_envlist(env, &envc);
4404 if (envlist == NULL)
4405 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004406
Victor Stinner8c62be82010-05-06 00:08:46 +00004407 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004408#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004409 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004410#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004411 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004412#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004413 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004414
Victor Stinner8c62be82010-05-06 00:08:46 +00004415 if (spawnval == -1)
4416 (void) posix_error();
4417 else
4418 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004419
Victor Stinner8c62be82010-05-06 00:08:46 +00004420 while (--envc >= 0)
4421 PyMem_DEL(envlist[envc]);
4422 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004423 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004424 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004425 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004426 Py_DECREF(opath);
4427 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004428}
4429#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004430#endif /* HAVE_SPAWNV */
4431
4432
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004433#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004434PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004435"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004436Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4437\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004438Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004439
4440static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004441posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004442{
Victor Stinner8c62be82010-05-06 00:08:46 +00004443 pid_t pid;
4444 int result = 0;
4445 _PyImport_AcquireLock();
4446 pid = fork1();
4447 if (pid == 0) {
4448 /* child: this clobbers and resets the import lock. */
4449 PyOS_AfterFork();
4450 } else {
4451 /* parent: release the import lock. */
4452 result = _PyImport_ReleaseLock();
4453 }
4454 if (pid == -1)
4455 return posix_error();
4456 if (result < 0) {
4457 /* Don't clobber the OSError if the fork failed. */
4458 PyErr_SetString(PyExc_RuntimeError,
4459 "not holding the import lock");
4460 return NULL;
4461 }
4462 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004463}
4464#endif
4465
4466
Guido van Rossumad0ee831995-03-01 10:34:45 +00004467#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004468PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004469"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004470Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004471Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004472
Barry Warsaw53699e91996-12-10 23:23:01 +00004473static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004474posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004475{
Victor Stinner8c62be82010-05-06 00:08:46 +00004476 pid_t pid;
4477 int result = 0;
4478 _PyImport_AcquireLock();
4479 pid = fork();
4480 if (pid == 0) {
4481 /* child: this clobbers and resets the import lock. */
4482 PyOS_AfterFork();
4483 } else {
4484 /* parent: release the import lock. */
4485 result = _PyImport_ReleaseLock();
4486 }
4487 if (pid == -1)
4488 return posix_error();
4489 if (result < 0) {
4490 /* Don't clobber the OSError if the fork failed. */
4491 PyErr_SetString(PyExc_RuntimeError,
4492 "not holding the import lock");
4493 return NULL;
4494 }
4495 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004496}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004497#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004498
Neal Norwitzb59798b2003-03-21 01:43:31 +00004499/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004500/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4501#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004502#define DEV_PTY_FILE "/dev/ptc"
4503#define HAVE_DEV_PTMX
4504#else
4505#define DEV_PTY_FILE "/dev/ptmx"
4506#endif
4507
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004508#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004509#ifdef HAVE_PTY_H
4510#include <pty.h>
4511#else
4512#ifdef HAVE_LIBUTIL_H
4513#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004514#else
4515#ifdef HAVE_UTIL_H
4516#include <util.h>
4517#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004518#endif /* HAVE_LIBUTIL_H */
4519#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004520#ifdef HAVE_STROPTS_H
4521#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004522#endif
4523#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004524
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004525#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004526PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004527"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004528Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004529
4530static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004531posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004532{
Victor Stinner8c62be82010-05-06 00:08:46 +00004533 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004534#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004535 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004536#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004537#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004538 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004539#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004540 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004541#endif
4542#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004543
Thomas Wouters70c21a12000-07-14 14:28:33 +00004544#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004545 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4546 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004547#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004548 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4549 if (slave_name == NULL)
4550 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004551
Victor Stinner8c62be82010-05-06 00:08:46 +00004552 slave_fd = open(slave_name, O_RDWR);
4553 if (slave_fd < 0)
4554 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004555#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004556 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4557 if (master_fd < 0)
4558 return posix_error();
4559 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4560 /* change permission of slave */
4561 if (grantpt(master_fd) < 0) {
4562 PyOS_setsig(SIGCHLD, sig_saved);
4563 return posix_error();
4564 }
4565 /* unlock slave */
4566 if (unlockpt(master_fd) < 0) {
4567 PyOS_setsig(SIGCHLD, sig_saved);
4568 return posix_error();
4569 }
4570 PyOS_setsig(SIGCHLD, sig_saved);
4571 slave_name = ptsname(master_fd); /* get name of slave */
4572 if (slave_name == NULL)
4573 return posix_error();
4574 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4575 if (slave_fd < 0)
4576 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004577#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004578 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4579 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004580#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004581 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004582#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004583#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004584#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004585
Victor Stinner8c62be82010-05-06 00:08:46 +00004586 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004587
Fred Drake8cef4cf2000-06-28 16:40:38 +00004588}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004589#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004590
4591#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004592PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004593"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004594Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4595Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004596To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004597
4598static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004599posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004600{
Victor Stinner8c62be82010-05-06 00:08:46 +00004601 int master_fd = -1, result = 0;
4602 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004603
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 _PyImport_AcquireLock();
4605 pid = forkpty(&master_fd, NULL, NULL, NULL);
4606 if (pid == 0) {
4607 /* child: this clobbers and resets the import lock. */
4608 PyOS_AfterFork();
4609 } else {
4610 /* parent: release the import lock. */
4611 result = _PyImport_ReleaseLock();
4612 }
4613 if (pid == -1)
4614 return posix_error();
4615 if (result < 0) {
4616 /* Don't clobber the OSError if the fork failed. */
4617 PyErr_SetString(PyExc_RuntimeError,
4618 "not holding the import lock");
4619 return NULL;
4620 }
4621 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004622}
4623#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004624
Ross Lagerwall7807c352011-03-17 20:20:30 +02004625
Guido van Rossumad0ee831995-03-01 10:34:45 +00004626#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004628"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004629Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004630
Barry Warsaw53699e91996-12-10 23:23:01 +00004631static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004632posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004633{
Victor Stinner8c62be82010-05-06 00:08:46 +00004634 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004635}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004636#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004637
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004638
Guido van Rossumad0ee831995-03-01 10:34:45 +00004639#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004640PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004641"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004642Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004643
Barry Warsaw53699e91996-12-10 23:23:01 +00004644static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004645posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004646{
Victor Stinner8c62be82010-05-06 00:08:46 +00004647 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004648}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004649#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004650
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004651
Guido van Rossumad0ee831995-03-01 10:34:45 +00004652#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004653PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004654"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004655Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004656
Barry Warsaw53699e91996-12-10 23:23:01 +00004657static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004658posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004659{
Victor Stinner8c62be82010-05-06 00:08:46 +00004660 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004661}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004662#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004663
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004664
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004665PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004666"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004667Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004668
Barry Warsaw53699e91996-12-10 23:23:01 +00004669static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004670posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004671{
Victor Stinner8c62be82010-05-06 00:08:46 +00004672 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004673}
4674
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02004675#ifdef HAVE_GETGROUPLIST
4676PyDoc_STRVAR(posix_getgrouplist__doc__,
4677"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
4678Returns a list of groups to which a user belongs.\n\n\
4679 user: username to lookup\n\
4680 group: base group id of the user");
4681
4682static PyObject *
4683posix_getgrouplist(PyObject *self, PyObject *args)
4684{
4685#ifdef NGROUPS_MAX
4686#define MAX_GROUPS NGROUPS_MAX
4687#else
4688 /* defined to be 16 on Solaris7, so this should be a small number */
4689#define MAX_GROUPS 64
4690#endif
4691
4692 const char *user;
4693 int i, ngroups;
4694 PyObject *list;
4695#ifdef __APPLE__
4696 int *groups, basegid;
4697#else
4698 gid_t *groups, basegid;
4699#endif
4700 ngroups = MAX_GROUPS;
4701
4702 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
4703 return NULL;
4704
4705#ifdef __APPLE__
4706 groups = PyMem_Malloc(ngroups * sizeof(int));
4707#else
4708 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
4709#endif
4710 if (groups == NULL)
4711 return PyErr_NoMemory();
4712
4713 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
4714 PyMem_Del(groups);
4715 return posix_error();
4716 }
4717
4718 list = PyList_New(ngroups);
4719 if (list == NULL) {
4720 PyMem_Del(groups);
4721 return NULL;
4722 }
4723
4724 for (i = 0; i < ngroups; i++) {
4725 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
4726 if (o == NULL) {
4727 Py_DECREF(list);
4728 PyMem_Del(groups);
4729 return NULL;
4730 }
4731 PyList_SET_ITEM(list, i, o);
4732 }
4733
4734 PyMem_Del(groups);
4735
4736 return list;
4737}
4738#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004739
Fred Drakec9680921999-12-13 16:37:25 +00004740#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004741PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004742"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004743Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004744
4745static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004746posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004747{
4748 PyObject *result = NULL;
4749
Fred Drakec9680921999-12-13 16:37:25 +00004750#ifdef NGROUPS_MAX
4751#define MAX_GROUPS NGROUPS_MAX
4752#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004753 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004754#define MAX_GROUPS 64
4755#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004756 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004757
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004758 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004759 * This is a helper variable to store the intermediate result when
4760 * that happens.
4761 *
4762 * To keep the code readable the OSX behaviour is unconditional,
4763 * according to the POSIX spec this should be safe on all unix-y
4764 * systems.
4765 */
4766 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004767 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004768
Victor Stinner8c62be82010-05-06 00:08:46 +00004769 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004770 if (n < 0) {
4771 if (errno == EINVAL) {
4772 n = getgroups(0, NULL);
4773 if (n == -1) {
4774 return posix_error();
4775 }
4776 if (n == 0) {
4777 /* Avoid malloc(0) */
4778 alt_grouplist = grouplist;
4779 } else {
4780 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4781 if (alt_grouplist == NULL) {
4782 errno = EINVAL;
4783 return posix_error();
4784 }
4785 n = getgroups(n, alt_grouplist);
4786 if (n == -1) {
4787 PyMem_Free(alt_grouplist);
4788 return posix_error();
4789 }
4790 }
4791 } else {
4792 return posix_error();
4793 }
4794 }
4795 result = PyList_New(n);
4796 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004797 int i;
4798 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004799 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004800 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004801 Py_DECREF(result);
4802 result = NULL;
4803 break;
Fred Drakec9680921999-12-13 16:37:25 +00004804 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004805 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004806 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004807 }
4808
4809 if (alt_grouplist != grouplist) {
4810 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004811 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004812
Fred Drakec9680921999-12-13 16:37:25 +00004813 return result;
4814}
4815#endif
4816
Antoine Pitroub7572f02009-12-02 20:46:48 +00004817#ifdef HAVE_INITGROUPS
4818PyDoc_STRVAR(posix_initgroups__doc__,
4819"initgroups(username, gid) -> None\n\n\
4820Call the system initgroups() to initialize the group access list with all of\n\
4821the groups of which the specified username is a member, plus the specified\n\
4822group id.");
4823
4824static PyObject *
4825posix_initgroups(PyObject *self, PyObject *args)
4826{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004827 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004828 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004829 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004830 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004831
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004832 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4833 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004834 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004835 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004836
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004837 res = initgroups(username, (gid_t) gid);
4838 Py_DECREF(oname);
4839 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004840 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004841
Victor Stinner8c62be82010-05-06 00:08:46 +00004842 Py_INCREF(Py_None);
4843 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004844}
4845#endif
4846
Martin v. Löwis606edc12002-06-13 21:09:11 +00004847#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004848PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004849"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004850Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004851
4852static PyObject *
4853posix_getpgid(PyObject *self, PyObject *args)
4854{
Victor Stinner8c62be82010-05-06 00:08:46 +00004855 pid_t pid, pgid;
4856 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4857 return NULL;
4858 pgid = getpgid(pid);
4859 if (pgid < 0)
4860 return posix_error();
4861 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004862}
4863#endif /* HAVE_GETPGID */
4864
4865
Guido van Rossumb6775db1994-08-01 11:34:53 +00004866#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004867PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004868"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004869Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004870
Barry Warsaw53699e91996-12-10 23:23:01 +00004871static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004872posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004873{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004874#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004875 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004876#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004877 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004878#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004879}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004880#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004881
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004882
Guido van Rossumb6775db1994-08-01 11:34:53 +00004883#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004884PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004885"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004886Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004887
Barry Warsaw53699e91996-12-10 23:23:01 +00004888static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004889posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004890{
Guido van Rossum64933891994-10-20 21:56:42 +00004891#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004892 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004893#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004895#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004896 return posix_error();
4897 Py_INCREF(Py_None);
4898 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004899}
4900
Guido van Rossumb6775db1994-08-01 11:34:53 +00004901#endif /* HAVE_SETPGRP */
4902
Guido van Rossumad0ee831995-03-01 10:34:45 +00004903#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004904
4905#ifdef MS_WINDOWS
4906#include <tlhelp32.h>
4907
4908static PyObject*
4909win32_getppid()
4910{
4911 HANDLE snapshot;
4912 pid_t mypid;
4913 PyObject* result = NULL;
4914 BOOL have_record;
4915 PROCESSENTRY32 pe;
4916
4917 mypid = getpid(); /* This function never fails */
4918
4919 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4920 if (snapshot == INVALID_HANDLE_VALUE)
4921 return PyErr_SetFromWindowsErr(GetLastError());
4922
4923 pe.dwSize = sizeof(pe);
4924 have_record = Process32First(snapshot, &pe);
4925 while (have_record) {
4926 if (mypid == (pid_t)pe.th32ProcessID) {
4927 /* We could cache the ulong value in a static variable. */
4928 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4929 break;
4930 }
4931
4932 have_record = Process32Next(snapshot, &pe);
4933 }
4934
4935 /* If our loop exits and our pid was not found (result will be NULL)
4936 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4937 * error anyway, so let's raise it. */
4938 if (!result)
4939 result = PyErr_SetFromWindowsErr(GetLastError());
4940
4941 CloseHandle(snapshot);
4942
4943 return result;
4944}
4945#endif /*MS_WINDOWS*/
4946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004947PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004948"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004949Return the parent's process id. If the parent process has already exited,\n\
4950Windows machines will still return its id; others systems will return the id\n\
4951of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004952
Barry Warsaw53699e91996-12-10 23:23:01 +00004953static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004954posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004955{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004956#ifdef MS_WINDOWS
4957 return win32_getppid();
4958#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004959 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004960#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004961}
4962#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004964
Fred Drake12c6e2d1999-12-14 21:25:03 +00004965#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004966PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004967"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004968Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004969
4970static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004971posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004972{
Victor Stinner8c62be82010-05-06 00:08:46 +00004973 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004974#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004975 wchar_t user_name[UNLEN + 1];
4976 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4977
4978 if (GetUserNameW(user_name, &num_chars)) {
4979 /* num_chars is the number of unicode chars plus null terminator */
4980 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004981 }
4982 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004983 result = PyErr_SetFromWindowsErr(GetLastError());
4984#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 char *name;
4986 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004987
Victor Stinner8c62be82010-05-06 00:08:46 +00004988 errno = 0;
4989 name = getlogin();
4990 if (name == NULL) {
4991 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004992 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004993 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004994 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004995 }
4996 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004997 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004998 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004999#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005000 return result;
5001}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005002#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005003
Guido van Rossumad0ee831995-03-01 10:34:45 +00005004#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005005PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005006"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005007Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005008
Barry Warsaw53699e91996-12-10 23:23:01 +00005009static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005010posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005011{
Victor Stinner8c62be82010-05-06 00:08:46 +00005012 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005013}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005014#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005015
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005016
Guido van Rossumad0ee831995-03-01 10:34:45 +00005017#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005018PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005019"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005020Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005021
Barry Warsaw53699e91996-12-10 23:23:01 +00005022static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005023posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005024{
Victor Stinner8c62be82010-05-06 00:08:46 +00005025 pid_t pid;
5026 int sig;
5027 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5028 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005029#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005030 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5031 APIRET rc;
5032 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005033 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005034
5035 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5036 APIRET rc;
5037 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005038 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005039
5040 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005041 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005042#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005043 if (kill(pid, sig) == -1)
5044 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005045#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005046 Py_INCREF(Py_None);
5047 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005048}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005049#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005050
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005051#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005052PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005053"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005054Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005055
5056static PyObject *
5057posix_killpg(PyObject *self, PyObject *args)
5058{
Victor Stinner8c62be82010-05-06 00:08:46 +00005059 int sig;
5060 pid_t pgid;
5061 /* XXX some man pages make the `pgid` parameter an int, others
5062 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5063 take the same type. Moreover, pid_t is always at least as wide as
5064 int (else compilation of this module fails), which is safe. */
5065 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5066 return NULL;
5067 if (killpg(pgid, sig) == -1)
5068 return posix_error();
5069 Py_INCREF(Py_None);
5070 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005071}
5072#endif
5073
Brian Curtineb24d742010-04-12 17:16:38 +00005074#ifdef MS_WINDOWS
5075PyDoc_STRVAR(win32_kill__doc__,
5076"kill(pid, sig)\n\n\
5077Kill a process with a signal.");
5078
5079static PyObject *
5080win32_kill(PyObject *self, PyObject *args)
5081{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005082 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005083 DWORD pid, sig, err;
5084 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005085
Victor Stinner8c62be82010-05-06 00:08:46 +00005086 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5087 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005088
Victor Stinner8c62be82010-05-06 00:08:46 +00005089 /* Console processes which share a common console can be sent CTRL+C or
5090 CTRL+BREAK events, provided they handle said events. */
5091 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5092 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5093 err = GetLastError();
5094 PyErr_SetFromWindowsErr(err);
5095 }
5096 else
5097 Py_RETURN_NONE;
5098 }
Brian Curtineb24d742010-04-12 17:16:38 +00005099
Victor Stinner8c62be82010-05-06 00:08:46 +00005100 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5101 attempt to open and terminate the process. */
5102 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5103 if (handle == NULL) {
5104 err = GetLastError();
5105 return PyErr_SetFromWindowsErr(err);
5106 }
Brian Curtineb24d742010-04-12 17:16:38 +00005107
Victor Stinner8c62be82010-05-06 00:08:46 +00005108 if (TerminateProcess(handle, sig) == 0) {
5109 err = GetLastError();
5110 result = PyErr_SetFromWindowsErr(err);
5111 } else {
5112 Py_INCREF(Py_None);
5113 result = Py_None;
5114 }
Brian Curtineb24d742010-04-12 17:16:38 +00005115
Victor Stinner8c62be82010-05-06 00:08:46 +00005116 CloseHandle(handle);
5117 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005118}
5119#endif /* MS_WINDOWS */
5120
Guido van Rossumc0125471996-06-28 18:55:32 +00005121#ifdef HAVE_PLOCK
5122
5123#ifdef HAVE_SYS_LOCK_H
5124#include <sys/lock.h>
5125#endif
5126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005127PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005128"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005129Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005130
Barry Warsaw53699e91996-12-10 23:23:01 +00005131static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005132posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005133{
Victor Stinner8c62be82010-05-06 00:08:46 +00005134 int op;
5135 if (!PyArg_ParseTuple(args, "i:plock", &op))
5136 return NULL;
5137 if (plock(op) == -1)
5138 return posix_error();
5139 Py_INCREF(Py_None);
5140 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005141}
5142#endif
5143
Guido van Rossumb6775db1994-08-01 11:34:53 +00005144#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005145PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005146"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005147Set the current process's user id.");
5148
Barry Warsaw53699e91996-12-10 23:23:01 +00005149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005150posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005151{
Victor Stinner8c62be82010-05-06 00:08:46 +00005152 long uid_arg;
5153 uid_t uid;
5154 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5155 return NULL;
5156 uid = uid_arg;
5157 if (uid != uid_arg) {
5158 PyErr_SetString(PyExc_OverflowError, "user id too big");
5159 return NULL;
5160 }
5161 if (setuid(uid) < 0)
5162 return posix_error();
5163 Py_INCREF(Py_None);
5164 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005165}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005166#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005167
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005168
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005169#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005170PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005171"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005172Set the current process's effective user id.");
5173
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005174static PyObject *
5175posix_seteuid (PyObject *self, PyObject *args)
5176{
Victor Stinner8c62be82010-05-06 00:08:46 +00005177 long euid_arg;
5178 uid_t euid;
5179 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5180 return NULL;
5181 euid = euid_arg;
5182 if (euid != euid_arg) {
5183 PyErr_SetString(PyExc_OverflowError, "user id too big");
5184 return NULL;
5185 }
5186 if (seteuid(euid) < 0) {
5187 return posix_error();
5188 } else {
5189 Py_INCREF(Py_None);
5190 return Py_None;
5191 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005192}
5193#endif /* HAVE_SETEUID */
5194
5195#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005196PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005197"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005198Set the current process's effective group id.");
5199
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005200static PyObject *
5201posix_setegid (PyObject *self, PyObject *args)
5202{
Victor Stinner8c62be82010-05-06 00:08:46 +00005203 long egid_arg;
5204 gid_t egid;
5205 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5206 return NULL;
5207 egid = egid_arg;
5208 if (egid != egid_arg) {
5209 PyErr_SetString(PyExc_OverflowError, "group id too big");
5210 return NULL;
5211 }
5212 if (setegid(egid) < 0) {
5213 return posix_error();
5214 } else {
5215 Py_INCREF(Py_None);
5216 return Py_None;
5217 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005218}
5219#endif /* HAVE_SETEGID */
5220
5221#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005222PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005223"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005224Set the current process's real and effective user ids.");
5225
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005226static PyObject *
5227posix_setreuid (PyObject *self, PyObject *args)
5228{
Victor Stinner8c62be82010-05-06 00:08:46 +00005229 long ruid_arg, euid_arg;
5230 uid_t ruid, euid;
5231 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5232 return NULL;
5233 if (ruid_arg == -1)
5234 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5235 else
5236 ruid = ruid_arg; /* otherwise, assign from our long */
5237 if (euid_arg == -1)
5238 euid = (uid_t)-1;
5239 else
5240 euid = euid_arg;
5241 if ((euid_arg != -1 && euid != euid_arg) ||
5242 (ruid_arg != -1 && ruid != ruid_arg)) {
5243 PyErr_SetString(PyExc_OverflowError, "user id too big");
5244 return NULL;
5245 }
5246 if (setreuid(ruid, euid) < 0) {
5247 return posix_error();
5248 } else {
5249 Py_INCREF(Py_None);
5250 return Py_None;
5251 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005252}
5253#endif /* HAVE_SETREUID */
5254
5255#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005256PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005257"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005258Set the current process's real and effective group ids.");
5259
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005260static PyObject *
5261posix_setregid (PyObject *self, PyObject *args)
5262{
Victor Stinner8c62be82010-05-06 00:08:46 +00005263 long rgid_arg, egid_arg;
5264 gid_t rgid, egid;
5265 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5266 return NULL;
5267 if (rgid_arg == -1)
5268 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5269 else
5270 rgid = rgid_arg; /* otherwise, assign from our long */
5271 if (egid_arg == -1)
5272 egid = (gid_t)-1;
5273 else
5274 egid = egid_arg;
5275 if ((egid_arg != -1 && egid != egid_arg) ||
5276 (rgid_arg != -1 && rgid != rgid_arg)) {
5277 PyErr_SetString(PyExc_OverflowError, "group id too big");
5278 return NULL;
5279 }
5280 if (setregid(rgid, egid) < 0) {
5281 return posix_error();
5282 } else {
5283 Py_INCREF(Py_None);
5284 return Py_None;
5285 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005286}
5287#endif /* HAVE_SETREGID */
5288
Guido van Rossumb6775db1994-08-01 11:34:53 +00005289#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005290PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005291"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005292Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005293
Barry Warsaw53699e91996-12-10 23:23:01 +00005294static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005295posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005296{
Victor Stinner8c62be82010-05-06 00:08:46 +00005297 long gid_arg;
5298 gid_t gid;
5299 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5300 return NULL;
5301 gid = gid_arg;
5302 if (gid != gid_arg) {
5303 PyErr_SetString(PyExc_OverflowError, "group id too big");
5304 return NULL;
5305 }
5306 if (setgid(gid) < 0)
5307 return posix_error();
5308 Py_INCREF(Py_None);
5309 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005310}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005311#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005312
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005313#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005314PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005315"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005316Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005317
5318static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005319posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005320{
Victor Stinner8c62be82010-05-06 00:08:46 +00005321 int i, len;
5322 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005323
Victor Stinner8c62be82010-05-06 00:08:46 +00005324 if (!PySequence_Check(groups)) {
5325 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5326 return NULL;
5327 }
5328 len = PySequence_Size(groups);
5329 if (len > MAX_GROUPS) {
5330 PyErr_SetString(PyExc_ValueError, "too many groups");
5331 return NULL;
5332 }
5333 for(i = 0; i < len; i++) {
5334 PyObject *elem;
5335 elem = PySequence_GetItem(groups, i);
5336 if (!elem)
5337 return NULL;
5338 if (!PyLong_Check(elem)) {
5339 PyErr_SetString(PyExc_TypeError,
5340 "groups must be integers");
5341 Py_DECREF(elem);
5342 return NULL;
5343 } else {
5344 unsigned long x = PyLong_AsUnsignedLong(elem);
5345 if (PyErr_Occurred()) {
5346 PyErr_SetString(PyExc_TypeError,
5347 "group id too big");
5348 Py_DECREF(elem);
5349 return NULL;
5350 }
5351 grouplist[i] = x;
5352 /* read back the value to see if it fitted in gid_t */
5353 if (grouplist[i] != x) {
5354 PyErr_SetString(PyExc_TypeError,
5355 "group id too big");
5356 Py_DECREF(elem);
5357 return NULL;
5358 }
5359 }
5360 Py_DECREF(elem);
5361 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005362
Victor Stinner8c62be82010-05-06 00:08:46 +00005363 if (setgroups(len, grouplist) < 0)
5364 return posix_error();
5365 Py_INCREF(Py_None);
5366 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005367}
5368#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005369
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005370#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5371static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005372wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005373{
Victor Stinner8c62be82010-05-06 00:08:46 +00005374 PyObject *result;
5375 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005376
Victor Stinner8c62be82010-05-06 00:08:46 +00005377 if (pid == -1)
5378 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005379
Victor Stinner8c62be82010-05-06 00:08:46 +00005380 if (struct_rusage == NULL) {
5381 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5382 if (m == NULL)
5383 return NULL;
5384 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5385 Py_DECREF(m);
5386 if (struct_rusage == NULL)
5387 return NULL;
5388 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005389
Victor Stinner8c62be82010-05-06 00:08:46 +00005390 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5391 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5392 if (!result)
5393 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005394
5395#ifndef doubletime
5396#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5397#endif
5398
Victor Stinner8c62be82010-05-06 00:08:46 +00005399 PyStructSequence_SET_ITEM(result, 0,
5400 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5401 PyStructSequence_SET_ITEM(result, 1,
5402 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005403#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005404 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5405 SET_INT(result, 2, ru->ru_maxrss);
5406 SET_INT(result, 3, ru->ru_ixrss);
5407 SET_INT(result, 4, ru->ru_idrss);
5408 SET_INT(result, 5, ru->ru_isrss);
5409 SET_INT(result, 6, ru->ru_minflt);
5410 SET_INT(result, 7, ru->ru_majflt);
5411 SET_INT(result, 8, ru->ru_nswap);
5412 SET_INT(result, 9, ru->ru_inblock);
5413 SET_INT(result, 10, ru->ru_oublock);
5414 SET_INT(result, 11, ru->ru_msgsnd);
5415 SET_INT(result, 12, ru->ru_msgrcv);
5416 SET_INT(result, 13, ru->ru_nsignals);
5417 SET_INT(result, 14, ru->ru_nvcsw);
5418 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005419#undef SET_INT
5420
Victor Stinner8c62be82010-05-06 00:08:46 +00005421 if (PyErr_Occurred()) {
5422 Py_DECREF(result);
5423 return NULL;
5424 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005425
Victor Stinner8c62be82010-05-06 00:08:46 +00005426 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005427}
5428#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
5429
5430#ifdef HAVE_WAIT3
5431PyDoc_STRVAR(posix_wait3__doc__,
5432"wait3(options) -> (pid, status, rusage)\n\n\
5433Wait for completion of a child process.");
5434
5435static PyObject *
5436posix_wait3(PyObject *self, PyObject *args)
5437{
Victor Stinner8c62be82010-05-06 00:08:46 +00005438 pid_t pid;
5439 int options;
5440 struct rusage ru;
5441 WAIT_TYPE status;
5442 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005443
Victor Stinner8c62be82010-05-06 00:08:46 +00005444 if (!PyArg_ParseTuple(args, "i:wait3", &options))
5445 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005446
Victor Stinner8c62be82010-05-06 00:08:46 +00005447 Py_BEGIN_ALLOW_THREADS
5448 pid = wait3(&status, options, &ru);
5449 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005450
Victor Stinner8c62be82010-05-06 00:08:46 +00005451 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005452}
5453#endif /* HAVE_WAIT3 */
5454
5455#ifdef HAVE_WAIT4
5456PyDoc_STRVAR(posix_wait4__doc__,
5457"wait4(pid, options) -> (pid, status, rusage)\n\n\
5458Wait for completion of a given child process.");
5459
5460static PyObject *
5461posix_wait4(PyObject *self, PyObject *args)
5462{
Victor Stinner8c62be82010-05-06 00:08:46 +00005463 pid_t pid;
5464 int options;
5465 struct rusage ru;
5466 WAIT_TYPE status;
5467 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005468
Victor Stinner8c62be82010-05-06 00:08:46 +00005469 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5470 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005471
Victor Stinner8c62be82010-05-06 00:08:46 +00005472 Py_BEGIN_ALLOW_THREADS
5473 pid = wait4(pid, &status, options, &ru);
5474 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005475
Victor Stinner8c62be82010-05-06 00:08:46 +00005476 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005477}
5478#endif /* HAVE_WAIT4 */
5479
Ross Lagerwall7807c352011-03-17 20:20:30 +02005480#if defined(HAVE_WAITID) && !defined(__APPLE__)
5481PyDoc_STRVAR(posix_waitid__doc__,
5482"waitid(idtype, id, options) -> waitid_result\n\n\
5483Wait for the completion of one or more child processes.\n\n\
5484idtype can be P_PID, P_PGID or P_ALL.\n\
5485id specifies the pid to wait on.\n\
5486options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
5487or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
5488Returns either waitid_result or None if WNOHANG is specified and there are\n\
5489no children in a waitable state.");
5490
5491static PyObject *
5492posix_waitid(PyObject *self, PyObject *args)
5493{
5494 PyObject *result;
5495 idtype_t idtype;
5496 id_t id;
5497 int options, res;
5498 siginfo_t si;
5499 si.si_pid = 0;
5500 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
5501 return NULL;
5502 Py_BEGIN_ALLOW_THREADS
5503 res = waitid(idtype, id, &si, options);
5504 Py_END_ALLOW_THREADS
5505 if (res == -1)
5506 return posix_error();
5507
5508 if (si.si_pid == 0)
5509 Py_RETURN_NONE;
5510
5511 result = PyStructSequence_New(&WaitidResultType);
5512 if (!result)
5513 return NULL;
5514
5515 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
5516 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
5517 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
5518 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
5519 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
5520 if (PyErr_Occurred()) {
5521 Py_DECREF(result);
5522 return NULL;
5523 }
5524
5525 return result;
5526}
5527#endif
5528
Guido van Rossumb6775db1994-08-01 11:34:53 +00005529#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005530PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005531"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005532Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005533
Barry Warsaw53699e91996-12-10 23:23:01 +00005534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005535posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005536{
Victor Stinner8c62be82010-05-06 00:08:46 +00005537 pid_t pid;
5538 int options;
5539 WAIT_TYPE status;
5540 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005541
Victor Stinner8c62be82010-05-06 00:08:46 +00005542 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5543 return NULL;
5544 Py_BEGIN_ALLOW_THREADS
5545 pid = waitpid(pid, &status, options);
5546 Py_END_ALLOW_THREADS
5547 if (pid == -1)
5548 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005549
Victor Stinner8c62be82010-05-06 00:08:46 +00005550 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005551}
5552
Tim Petersab034fa2002-02-01 11:27:43 +00005553#elif defined(HAVE_CWAIT)
5554
5555/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005557"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005559
5560static PyObject *
5561posix_waitpid(PyObject *self, PyObject *args)
5562{
Victor Stinner8c62be82010-05-06 00:08:46 +00005563 Py_intptr_t pid;
5564 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005565
Victor Stinner8c62be82010-05-06 00:08:46 +00005566 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5567 return NULL;
5568 Py_BEGIN_ALLOW_THREADS
5569 pid = _cwait(&status, pid, options);
5570 Py_END_ALLOW_THREADS
5571 if (pid == -1)
5572 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005573
Victor Stinner8c62be82010-05-06 00:08:46 +00005574 /* shift the status left a byte so this is more like the POSIX waitpid */
5575 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005576}
5577#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005578
Guido van Rossumad0ee831995-03-01 10:34:45 +00005579#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005581"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005582Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005583
Barry Warsaw53699e91996-12-10 23:23:01 +00005584static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005585posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005586{
Victor Stinner8c62be82010-05-06 00:08:46 +00005587 pid_t pid;
5588 WAIT_TYPE status;
5589 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005590
Victor Stinner8c62be82010-05-06 00:08:46 +00005591 Py_BEGIN_ALLOW_THREADS
5592 pid = wait(&status);
5593 Py_END_ALLOW_THREADS
5594 if (pid == -1)
5595 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005596
Victor Stinner8c62be82010-05-06 00:08:46 +00005597 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005598}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005599#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005600
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005602PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005603"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005604Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005605
Barry Warsaw53699e91996-12-10 23:23:01 +00005606static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005607posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005608{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005609#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005610 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005611#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005612#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005613 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5614 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005615#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005617#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005618#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005619}
5620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005621
Guido van Rossumb6775db1994-08-01 11:34:53 +00005622#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005623PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005624"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005625Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005626
Barry Warsaw53699e91996-12-10 23:23:01 +00005627static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005628posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005629{
Victor Stinner8c62be82010-05-06 00:08:46 +00005630 PyObject* v;
5631 char buf[MAXPATHLEN];
5632 PyObject *opath;
5633 char *path;
5634 int n;
5635 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005636
Victor Stinner8c62be82010-05-06 00:08:46 +00005637 if (!PyArg_ParseTuple(args, "O&:readlink",
5638 PyUnicode_FSConverter, &opath))
5639 return NULL;
5640 path = PyBytes_AsString(opath);
5641 v = PySequence_GetItem(args, 0);
5642 if (v == NULL) {
5643 Py_DECREF(opath);
5644 return NULL;
5645 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005646
Victor Stinner8c62be82010-05-06 00:08:46 +00005647 if (PyUnicode_Check(v)) {
5648 arg_is_unicode = 1;
5649 }
5650 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005651
Victor Stinner8c62be82010-05-06 00:08:46 +00005652 Py_BEGIN_ALLOW_THREADS
5653 n = readlink(path, buf, (int) sizeof buf);
5654 Py_END_ALLOW_THREADS
5655 if (n < 0)
5656 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005657
Victor Stinner8c62be82010-05-06 00:08:46 +00005658 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005659 if (arg_is_unicode)
5660 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5661 else
5662 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005663}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005664#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005665
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005666
Brian Curtin52173d42010-12-02 18:29:18 +00005667#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005668PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005669"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005670Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005671
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005672static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005673posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005674{
Victor Stinner8c62be82010-05-06 00:08:46 +00005675 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005676}
5677#endif /* HAVE_SYMLINK */
5678
Brian Curtind40e6f72010-07-08 21:39:08 +00005679#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5680
5681PyDoc_STRVAR(win_readlink__doc__,
5682"readlink(path) -> path\n\n\
5683Return a string representing the path to which the symbolic link points.");
5684
Brian Curtind40e6f72010-07-08 21:39:08 +00005685/* Windows readlink implementation */
5686static PyObject *
5687win_readlink(PyObject *self, PyObject *args)
5688{
5689 wchar_t *path;
5690 DWORD n_bytes_returned;
5691 DWORD io_result;
5692 PyObject *result;
5693 HANDLE reparse_point_handle;
5694
5695 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5696 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5697 wchar_t *print_name;
5698
5699 if (!PyArg_ParseTuple(args,
5700 "u:readlink",
5701 &path))
5702 return NULL;
5703
5704 /* First get a handle to the reparse point */
5705 Py_BEGIN_ALLOW_THREADS
5706 reparse_point_handle = CreateFileW(
5707 path,
5708 0,
5709 0,
5710 0,
5711 OPEN_EXISTING,
5712 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5713 0);
5714 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005715
Brian Curtind40e6f72010-07-08 21:39:08 +00005716 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5717 {
5718 return win32_error_unicode("readlink", path);
5719 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005720
Brian Curtind40e6f72010-07-08 21:39:08 +00005721 Py_BEGIN_ALLOW_THREADS
5722 /* New call DeviceIoControl to read the reparse point */
5723 io_result = DeviceIoControl(
5724 reparse_point_handle,
5725 FSCTL_GET_REPARSE_POINT,
5726 0, 0, /* in buffer */
5727 target_buffer, sizeof(target_buffer),
5728 &n_bytes_returned,
5729 0 /* we're not using OVERLAPPED_IO */
5730 );
5731 CloseHandle(reparse_point_handle);
5732 Py_END_ALLOW_THREADS
5733
5734 if (io_result==0)
5735 {
5736 return win32_error_unicode("readlink", path);
5737 }
5738
5739 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5740 {
5741 PyErr_SetString(PyExc_ValueError,
5742 "not a symbolic link");
5743 return NULL;
5744 }
Brian Curtin74e45612010-07-09 15:58:59 +00005745 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5746 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5747
5748 result = PyUnicode_FromWideChar(print_name,
5749 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005750 return result;
5751}
5752
5753#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5754
Brian Curtin52173d42010-12-02 18:29:18 +00005755#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005756
5757/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5758static int has_CreateSymbolicLinkW = 0;
5759static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5760static int
5761check_CreateSymbolicLinkW()
5762{
5763 HINSTANCE hKernel32;
5764 /* only recheck */
5765 if (has_CreateSymbolicLinkW)
5766 return has_CreateSymbolicLinkW;
5767 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005768 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5769 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005770 if (Py_CreateSymbolicLinkW)
5771 has_CreateSymbolicLinkW = 1;
5772 return has_CreateSymbolicLinkW;
5773}
5774
5775PyDoc_STRVAR(win_symlink__doc__,
5776"symlink(src, dst, target_is_directory=False)\n\n\
5777Create a symbolic link pointing to src named dst.\n\
5778target_is_directory is required if the target is to be interpreted as\n\
5779a directory.\n\
5780This function requires Windows 6.0 or greater, and raises a\n\
5781NotImplementedError otherwise.");
5782
5783static PyObject *
5784win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5785{
5786 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5787 PyObject *src, *dest;
5788 int target_is_directory = 0;
5789 DWORD res;
5790 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005791
Brian Curtind40e6f72010-07-08 21:39:08 +00005792 if (!check_CreateSymbolicLinkW())
5793 {
5794 /* raise NotImplementedError */
5795 return PyErr_Format(PyExc_NotImplementedError,
5796 "CreateSymbolicLinkW not found");
5797 }
5798 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5799 kwlist, &src, &dest, &target_is_directory))
5800 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005801
5802 if (win32_can_symlink == 0)
5803 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5804
Brian Curtind40e6f72010-07-08 21:39:08 +00005805 if (!convert_to_unicode(&src)) { return NULL; }
5806 if (!convert_to_unicode(&dest)) {
5807 Py_DECREF(src);
5808 return NULL;
5809 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005810
Brian Curtind40e6f72010-07-08 21:39:08 +00005811 /* if src is a directory, ensure target_is_directory==1 */
5812 if(
5813 GetFileAttributesExW(
5814 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5815 ))
5816 {
5817 target_is_directory = target_is_directory ||
5818 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5819 }
5820
5821 Py_BEGIN_ALLOW_THREADS
5822 res = Py_CreateSymbolicLinkW(
5823 PyUnicode_AsUnicode(dest),
5824 PyUnicode_AsUnicode(src),
5825 target_is_directory);
5826 Py_END_ALLOW_THREADS
5827 Py_DECREF(src);
5828 Py_DECREF(dest);
5829 if (!res)
5830 {
5831 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5832 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005833
Brian Curtind40e6f72010-07-08 21:39:08 +00005834 Py_INCREF(Py_None);
5835 return Py_None;
5836}
Brian Curtin52173d42010-12-02 18:29:18 +00005837#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005838
5839#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005840#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5841static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005842system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005843{
5844 ULONG value = 0;
5845
5846 Py_BEGIN_ALLOW_THREADS
5847 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5848 Py_END_ALLOW_THREADS
5849
5850 return value;
5851}
5852
5853static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005854posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005855{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005856 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005857 return Py_BuildValue("ddddd",
5858 (double)0 /* t.tms_utime / HZ */,
5859 (double)0 /* t.tms_stime / HZ */,
5860 (double)0 /* t.tms_cutime / HZ */,
5861 (double)0 /* t.tms_cstime / HZ */,
5862 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005863}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005864#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005865#define NEED_TICKS_PER_SECOND
5866static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005868posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005869{
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 struct tms t;
5871 clock_t c;
5872 errno = 0;
5873 c = times(&t);
5874 if (c == (clock_t) -1)
5875 return posix_error();
5876 return Py_BuildValue("ddddd",
5877 (double)t.tms_utime / ticks_per_second,
5878 (double)t.tms_stime / ticks_per_second,
5879 (double)t.tms_cutime / ticks_per_second,
5880 (double)t.tms_cstime / ticks_per_second,
5881 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005882}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005883#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005884#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005885
5886
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005887#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005888#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005889static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005890posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005891{
Victor Stinner8c62be82010-05-06 00:08:46 +00005892 FILETIME create, exit, kernel, user;
5893 HANDLE hProc;
5894 hProc = GetCurrentProcess();
5895 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5896 /* The fields of a FILETIME structure are the hi and lo part
5897 of a 64-bit value expressed in 100 nanosecond units.
5898 1e7 is one second in such units; 1e-7 the inverse.
5899 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5900 */
5901 return Py_BuildValue(
5902 "ddddd",
5903 (double)(user.dwHighDateTime*429.4967296 +
5904 user.dwLowDateTime*1e-7),
5905 (double)(kernel.dwHighDateTime*429.4967296 +
5906 kernel.dwLowDateTime*1e-7),
5907 (double)0,
5908 (double)0,
5909 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005910}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005911#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005912
5913#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005914PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005915"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005916Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005917#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005918
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005919
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005920#ifdef HAVE_GETSID
5921PyDoc_STRVAR(posix_getsid__doc__,
5922"getsid(pid) -> sid\n\n\
5923Call the system call getsid().");
5924
5925static PyObject *
5926posix_getsid(PyObject *self, PyObject *args)
5927{
Victor Stinner8c62be82010-05-06 00:08:46 +00005928 pid_t pid;
5929 int sid;
5930 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5931 return NULL;
5932 sid = getsid(pid);
5933 if (sid < 0)
5934 return posix_error();
5935 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005936}
5937#endif /* HAVE_GETSID */
5938
5939
Guido van Rossumb6775db1994-08-01 11:34:53 +00005940#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005941PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005942"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005943Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005944
Barry Warsaw53699e91996-12-10 23:23:01 +00005945static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005946posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005947{
Victor Stinner8c62be82010-05-06 00:08:46 +00005948 if (setsid() < 0)
5949 return posix_error();
5950 Py_INCREF(Py_None);
5951 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005952}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005953#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005954
Guido van Rossumb6775db1994-08-01 11:34:53 +00005955#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005956PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005957"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005958Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005959
Barry Warsaw53699e91996-12-10 23:23:01 +00005960static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005961posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005962{
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 pid_t pid;
5964 int pgrp;
5965 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5966 return NULL;
5967 if (setpgid(pid, pgrp) < 0)
5968 return posix_error();
5969 Py_INCREF(Py_None);
5970 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005971}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005972#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005973
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005974
Guido van Rossumb6775db1994-08-01 11:34:53 +00005975#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005976PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005977"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005979
Barry Warsaw53699e91996-12-10 23:23:01 +00005980static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005981posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005982{
Victor Stinner8c62be82010-05-06 00:08:46 +00005983 int fd;
5984 pid_t pgid;
5985 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5986 return NULL;
5987 pgid = tcgetpgrp(fd);
5988 if (pgid < 0)
5989 return posix_error();
5990 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005991}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005992#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005993
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005994
Guido van Rossumb6775db1994-08-01 11:34:53 +00005995#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005996PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005997"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005999
Barry Warsaw53699e91996-12-10 23:23:01 +00006000static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006001posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006002{
Victor Stinner8c62be82010-05-06 00:08:46 +00006003 int fd;
6004 pid_t pgid;
6005 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6006 return NULL;
6007 if (tcsetpgrp(fd, pgid) < 0)
6008 return posix_error();
6009 Py_INCREF(Py_None);
6010 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006011}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006012#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006013
Guido van Rossum687dd131993-05-17 08:34:16 +00006014/* Functions acting on file descriptors */
6015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006016PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006017"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006018Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006019
Barry Warsaw53699e91996-12-10 23:23:01 +00006020static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006021posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006022{
Victor Stinner8c62be82010-05-06 00:08:46 +00006023 PyObject *ofile;
6024 char *file;
6025 int flag;
6026 int mode = 0777;
6027 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006028
6029#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006030 PyUnicodeObject *po;
6031 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
6032 Py_BEGIN_ALLOW_THREADS
6033 /* PyUnicode_AS_UNICODE OK without thread
6034 lock as it is a simple dereference. */
6035 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6036 Py_END_ALLOW_THREADS
6037 if (fd < 0)
6038 return posix_error();
6039 return PyLong_FromLong((long)fd);
6040 }
6041 /* Drop the argument parsing error as narrow strings
6042 are also valid. */
6043 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006044#endif
6045
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 if (!PyArg_ParseTuple(args, "O&i|i",
6047 PyUnicode_FSConverter, &ofile,
6048 &flag, &mode))
6049 return NULL;
6050 file = PyBytes_AsString(ofile);
6051 Py_BEGIN_ALLOW_THREADS
6052 fd = open(file, flag, mode);
6053 Py_END_ALLOW_THREADS
6054 if (fd < 0)
6055 return posix_error_with_allocated_filename(ofile);
6056 Py_DECREF(ofile);
6057 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006058}
6059
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006061PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006062"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006063Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006064
Barry Warsaw53699e91996-12-10 23:23:01 +00006065static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006066posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006067{
Victor Stinner8c62be82010-05-06 00:08:46 +00006068 int fd, res;
6069 if (!PyArg_ParseTuple(args, "i:close", &fd))
6070 return NULL;
6071 if (!_PyVerify_fd(fd))
6072 return posix_error();
6073 Py_BEGIN_ALLOW_THREADS
6074 res = close(fd);
6075 Py_END_ALLOW_THREADS
6076 if (res < 0)
6077 return posix_error();
6078 Py_INCREF(Py_None);
6079 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006080}
6081
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006082
Victor Stinner8c62be82010-05-06 00:08:46 +00006083PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006084"closerange(fd_low, fd_high)\n\n\
6085Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6086
6087static PyObject *
6088posix_closerange(PyObject *self, PyObject *args)
6089{
Victor Stinner8c62be82010-05-06 00:08:46 +00006090 int fd_from, fd_to, i;
6091 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6092 return NULL;
6093 Py_BEGIN_ALLOW_THREADS
6094 for (i = fd_from; i < fd_to; i++)
6095 if (_PyVerify_fd(i))
6096 close(i);
6097 Py_END_ALLOW_THREADS
6098 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006099}
6100
6101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006102PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006103"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006104Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006105
Barry Warsaw53699e91996-12-10 23:23:01 +00006106static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006107posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006108{
Victor Stinner8c62be82010-05-06 00:08:46 +00006109 int fd;
6110 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6111 return NULL;
6112 if (!_PyVerify_fd(fd))
6113 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 if (fd < 0)
6116 return posix_error();
6117 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006118}
6119
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006121PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006122"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006123Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006124
Barry Warsaw53699e91996-12-10 23:23:01 +00006125static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006126posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006127{
Victor Stinner8c62be82010-05-06 00:08:46 +00006128 int fd, fd2, res;
6129 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6130 return NULL;
6131 if (!_PyVerify_fd_dup2(fd, fd2))
6132 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006133 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006134 if (res < 0)
6135 return posix_error();
6136 Py_INCREF(Py_None);
6137 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006138}
6139
Ross Lagerwall7807c352011-03-17 20:20:30 +02006140#ifdef HAVE_LOCKF
6141PyDoc_STRVAR(posix_lockf__doc__,
6142"lockf(fd, cmd, len)\n\n\
6143Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6144fd is an open file descriptor.\n\
6145cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6146F_TEST.\n\
6147len specifies the section of the file to lock.");
6148
6149static PyObject *
6150posix_lockf(PyObject *self, PyObject *args)
6151{
6152 int fd, cmd, res;
6153 off_t len;
6154 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6155 &fd, &cmd, _parse_off_t, &len))
6156 return NULL;
6157
6158 Py_BEGIN_ALLOW_THREADS
6159 res = lockf(fd, cmd, len);
6160 Py_END_ALLOW_THREADS
6161
6162 if (res < 0)
6163 return posix_error();
6164
6165 Py_RETURN_NONE;
6166}
6167#endif
6168
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006169
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006170PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006171"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006172Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006173
Barry Warsaw53699e91996-12-10 23:23:01 +00006174static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006175posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006176{
Victor Stinner8c62be82010-05-06 00:08:46 +00006177 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006178#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006180#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006182#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006183 PyObject *posobj;
6184 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006186#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6188 switch (how) {
6189 case 0: how = SEEK_SET; break;
6190 case 1: how = SEEK_CUR; break;
6191 case 2: how = SEEK_END; break;
6192 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006193#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006194
Ross Lagerwall8e749672011-03-17 21:54:07 +02006195#if !defined(HAVE_LARGEFILE_SUPPORT)
6196 pos = PyLong_AsLong(posobj);
6197#else
6198 pos = PyLong_AsLongLong(posobj);
6199#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 if (PyErr_Occurred())
6201 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006202
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 if (!_PyVerify_fd(fd))
6204 return posix_error();
6205 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006206#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006208#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006210#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006211 Py_END_ALLOW_THREADS
6212 if (res < 0)
6213 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006214
6215#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006216 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006217#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006218 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006219#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006220}
6221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006222
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006223PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006224"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006225Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006226
Barry Warsaw53699e91996-12-10 23:23:01 +00006227static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006228posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006229{
Victor Stinner8c62be82010-05-06 00:08:46 +00006230 int fd, size;
6231 Py_ssize_t n;
6232 PyObject *buffer;
6233 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6234 return NULL;
6235 if (size < 0) {
6236 errno = EINVAL;
6237 return posix_error();
6238 }
6239 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6240 if (buffer == NULL)
6241 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006242 if (!_PyVerify_fd(fd)) {
6243 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006245 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006246 Py_BEGIN_ALLOW_THREADS
6247 n = read(fd, PyBytes_AS_STRING(buffer), size);
6248 Py_END_ALLOW_THREADS
6249 if (n < 0) {
6250 Py_DECREF(buffer);
6251 return posix_error();
6252 }
6253 if (n != size)
6254 _PyBytes_Resize(&buffer, n);
6255 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006256}
6257
Ross Lagerwall7807c352011-03-17 20:20:30 +02006258#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6259 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006260static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006261iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6262{
6263 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006264 Py_ssize_t blen, total = 0;
6265
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006266 *iov = PyMem_New(struct iovec, cnt);
6267 if (*iov == NULL) {
6268 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006269 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006270 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006271
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006272 *buf = PyMem_New(Py_buffer, cnt);
6273 if (*buf == NULL) {
6274 PyMem_Del(*iov);
6275 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006276 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006277 }
6278
6279 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006280 PyObject *item = PySequence_GetItem(seq, i);
6281 if (item == NULL)
6282 goto fail;
6283 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6284 Py_DECREF(item);
6285 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006286 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006287 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006288 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006289 blen = (*buf)[i].len;
6290 (*iov)[i].iov_len = blen;
6291 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006292 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006293 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006294
6295fail:
6296 PyMem_Del(*iov);
6297 for (j = 0; j < i; j++) {
6298 PyBuffer_Release(&(*buf)[j]);
6299 }
6300 PyMem_Del(*buf);
6301 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006302}
6303
6304static void
6305iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6306{
6307 int i;
6308 PyMem_Del(iov);
6309 for (i = 0; i < cnt; i++) {
6310 PyBuffer_Release(&buf[i]);
6311 }
6312 PyMem_Del(buf);
6313}
6314#endif
6315
Ross Lagerwall7807c352011-03-17 20:20:30 +02006316#ifdef HAVE_READV
6317PyDoc_STRVAR(posix_readv__doc__,
6318"readv(fd, buffers) -> bytesread\n\n\
6319Read from a file descriptor into a number of writable buffers. buffers\n\
6320is an arbitrary sequence of writable buffers.\n\
6321Returns the total number of bytes read.");
6322
6323static PyObject *
6324posix_readv(PyObject *self, PyObject *args)
6325{
6326 int fd, cnt;
6327 Py_ssize_t n;
6328 PyObject *seq;
6329 struct iovec *iov;
6330 Py_buffer *buf;
6331
6332 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6333 return NULL;
6334 if (!PySequence_Check(seq)) {
6335 PyErr_SetString(PyExc_TypeError,
6336 "readv() arg 2 must be a sequence");
6337 return NULL;
6338 }
6339 cnt = PySequence_Size(seq);
6340
6341 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6342 return NULL;
6343
6344 Py_BEGIN_ALLOW_THREADS
6345 n = readv(fd, iov, cnt);
6346 Py_END_ALLOW_THREADS
6347
6348 iov_cleanup(iov, buf, cnt);
6349 return PyLong_FromSsize_t(n);
6350}
6351#endif
6352
6353#ifdef HAVE_PREAD
6354PyDoc_STRVAR(posix_pread__doc__,
6355"pread(fd, buffersize, offset) -> string\n\n\
6356Read from a file descriptor, fd, at a position of offset. It will read up\n\
6357to buffersize number of bytes. The file offset remains unchanged.");
6358
6359static PyObject *
6360posix_pread(PyObject *self, PyObject *args)
6361{
6362 int fd, size;
6363 off_t offset;
6364 Py_ssize_t n;
6365 PyObject *buffer;
6366 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6367 return NULL;
6368
6369 if (size < 0) {
6370 errno = EINVAL;
6371 return posix_error();
6372 }
6373 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6374 if (buffer == NULL)
6375 return NULL;
6376 if (!_PyVerify_fd(fd)) {
6377 Py_DECREF(buffer);
6378 return posix_error();
6379 }
6380 Py_BEGIN_ALLOW_THREADS
6381 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6382 Py_END_ALLOW_THREADS
6383 if (n < 0) {
6384 Py_DECREF(buffer);
6385 return posix_error();
6386 }
6387 if (n != size)
6388 _PyBytes_Resize(&buffer, n);
6389 return buffer;
6390}
6391#endif
6392
6393PyDoc_STRVAR(posix_write__doc__,
6394"write(fd, string) -> byteswritten\n\n\
6395Write a string to a file descriptor.");
6396
6397static PyObject *
6398posix_write(PyObject *self, PyObject *args)
6399{
6400 Py_buffer pbuf;
6401 int fd;
6402 Py_ssize_t size, len;
6403
6404 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
6405 return NULL;
6406 if (!_PyVerify_fd(fd)) {
6407 PyBuffer_Release(&pbuf);
6408 return posix_error();
6409 }
6410 len = pbuf.len;
6411 Py_BEGIN_ALLOW_THREADS
6412#if defined(MS_WIN64) || defined(MS_WINDOWS)
6413 if (len > INT_MAX)
6414 len = INT_MAX;
6415 size = write(fd, pbuf.buf, (int)len);
6416#else
6417 size = write(fd, pbuf.buf, len);
6418#endif
6419 Py_END_ALLOW_THREADS
6420 PyBuffer_Release(&pbuf);
6421 if (size < 0)
6422 return posix_error();
6423 return PyLong_FromSsize_t(size);
6424}
6425
6426#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006427PyDoc_STRVAR(posix_sendfile__doc__,
6428"sendfile(out, in, offset, nbytes) -> byteswritten\n\
6429sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
6430 -> byteswritten\n\
6431Copy nbytes bytes from file descriptor in to file descriptor out.");
6432
6433static PyObject *
6434posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
6435{
6436 int in, out;
6437 Py_ssize_t ret;
6438 off_t offset;
6439
6440#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
6441#ifndef __APPLE__
6442 Py_ssize_t len;
6443#endif
6444 PyObject *headers = NULL, *trailers = NULL;
6445 Py_buffer *hbuf, *tbuf;
6446 off_t sbytes;
6447 struct sf_hdtr sf;
6448 int flags = 0;
6449 sf.headers = NULL;
6450 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00006451 static char *keywords[] = {"out", "in",
6452 "offset", "count",
6453 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006454
6455#ifdef __APPLE__
6456 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006457 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006458#else
6459 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00006460 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006461#endif
6462 &headers, &trailers, &flags))
6463 return NULL;
6464 if (headers != NULL) {
6465 if (!PySequence_Check(headers)) {
6466 PyErr_SetString(PyExc_TypeError,
6467 "sendfile() headers must be a sequence or None");
6468 return NULL;
6469 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006470 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006471 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006472 if (sf.hdr_cnt > 0 &&
6473 !(i = iov_setup(&(sf.headers), &hbuf,
6474 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006475 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006476#ifdef __APPLE__
6477 sbytes += i;
6478#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006479 }
6480 }
6481 if (trailers != NULL) {
6482 if (!PySequence_Check(trailers)) {
6483 PyErr_SetString(PyExc_TypeError,
6484 "sendfile() trailers must be a sequence or None");
6485 return NULL;
6486 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006487 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006488 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006489 if (sf.trl_cnt > 0 &&
6490 !(i = iov_setup(&(sf.trailers), &tbuf,
6491 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006492 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006493#ifdef __APPLE__
6494 sbytes += i;
6495#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006496 }
6497 }
6498
6499 Py_BEGIN_ALLOW_THREADS
6500#ifdef __APPLE__
6501 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
6502#else
6503 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
6504#endif
6505 Py_END_ALLOW_THREADS
6506
6507 if (sf.headers != NULL)
6508 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
6509 if (sf.trailers != NULL)
6510 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
6511
6512 if (ret < 0) {
6513 if ((errno == EAGAIN) || (errno == EBUSY)) {
6514 if (sbytes != 0) {
6515 // some data has been sent
6516 goto done;
6517 }
6518 else {
6519 // no data has been sent; upper application is supposed
6520 // to retry on EAGAIN or EBUSY
6521 return posix_error();
6522 }
6523 }
6524 return posix_error();
6525 }
6526 goto done;
6527
6528done:
6529 #if !defined(HAVE_LARGEFILE_SUPPORT)
6530 return Py_BuildValue("l", sbytes);
6531 #else
6532 return Py_BuildValue("L", sbytes);
6533 #endif
6534
6535#else
6536 Py_ssize_t count;
6537 PyObject *offobj;
6538 static char *keywords[] = {"out", "in",
6539 "offset", "count", NULL};
6540 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
6541 keywords, &out, &in, &offobj, &count))
6542 return NULL;
6543#ifdef linux
6544 if (offobj == Py_None) {
6545 Py_BEGIN_ALLOW_THREADS
6546 ret = sendfile(out, in, NULL, count);
6547 Py_END_ALLOW_THREADS
6548 if (ret < 0)
6549 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02006550 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006551 }
6552#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00006553 if (!_parse_off_t(offobj, &offset))
6554 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006555 Py_BEGIN_ALLOW_THREADS
6556 ret = sendfile(out, in, &offset, count);
6557 Py_END_ALLOW_THREADS
6558 if (ret < 0)
6559 return posix_error();
6560 return Py_BuildValue("n", ret);
6561#endif
6562}
6563#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006565PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006566"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006567Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006568
Barry Warsaw53699e91996-12-10 23:23:01 +00006569static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006570posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006571{
Victor Stinner8c62be82010-05-06 00:08:46 +00006572 int fd;
6573 STRUCT_STAT st;
6574 int res;
6575 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
6576 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006577#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00006578 /* on OpenVMS we must ensure that all bytes are written to the file */
6579 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00006580#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006581 if (!_PyVerify_fd(fd))
6582 return posix_error();
6583 Py_BEGIN_ALLOW_THREADS
6584 res = FSTAT(fd, &st);
6585 Py_END_ALLOW_THREADS
6586 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00006587#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006588 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00006589#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00006591#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 }
Tim Peters5aa91602002-01-30 05:46:57 +00006593
Victor Stinner8c62be82010-05-06 00:08:46 +00006594 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00006595}
6596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006597PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006598"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006599Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006600connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00006601
6602static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00006603posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00006604{
Victor Stinner8c62be82010-05-06 00:08:46 +00006605 int fd;
6606 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
6607 return NULL;
6608 if (!_PyVerify_fd(fd))
6609 return PyBool_FromLong(0);
6610 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00006611}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006612
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006613#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006614PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006615"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006616Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006617
Barry Warsaw53699e91996-12-10 23:23:01 +00006618static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006619posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00006620{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006621#if defined(PYOS_OS2)
6622 HFILE read, write;
6623 APIRET rc;
6624
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006625 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006626 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006627 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006628
6629 return Py_BuildValue("(ii)", read, write);
6630#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006631#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006632 int fds[2];
6633 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00006635 if (res != 0)
6636 return posix_error();
6637 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006638#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 HANDLE read, write;
6640 int read_fd, write_fd;
6641 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00006642 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 if (!ok)
6644 return win32_error("CreatePipe", NULL);
6645 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
6646 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
6647 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006648#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00006649#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006650}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006651#endif /* HAVE_PIPE */
6652
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006653#ifdef HAVE_PIPE2
6654PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02006655"pipe2(flags) -> (read_end, write_end)\n\n\
6656Create a pipe with flags set atomically.\n\
6657flags can be constructed by ORing together one or more of these values:\n\
6658O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006659");
6660
6661static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02006662posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006663{
Charles-François Natali368f34b2011-06-06 19:49:47 +02006664 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006665 int fds[2];
6666 int res;
6667
Charles-François Natali368f34b2011-06-06 19:49:47 +02006668 flags = PyLong_AsLong(arg);
6669 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02006670 return NULL;
6671
6672 res = pipe2(fds, flags);
6673 if (res != 0)
6674 return posix_error();
6675 return Py_BuildValue("(ii)", fds[0], fds[1]);
6676}
6677#endif /* HAVE_PIPE2 */
6678
Ross Lagerwall7807c352011-03-17 20:20:30 +02006679#ifdef HAVE_WRITEV
6680PyDoc_STRVAR(posix_writev__doc__,
6681"writev(fd, buffers) -> byteswritten\n\n\
6682Write the contents of buffers to a file descriptor, where buffers is an\n\
6683arbitrary sequence of buffers.\n\
6684Returns the total bytes written.");
6685
6686static PyObject *
6687posix_writev(PyObject *self, PyObject *args)
6688{
6689 int fd, cnt;
6690 Py_ssize_t res;
6691 PyObject *seq;
6692 struct iovec *iov;
6693 Py_buffer *buf;
6694 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
6695 return NULL;
6696 if (!PySequence_Check(seq)) {
6697 PyErr_SetString(PyExc_TypeError,
6698 "writev() arg 2 must be a sequence");
6699 return NULL;
6700 }
6701 cnt = PySequence_Size(seq);
6702
6703 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
6704 return NULL;
6705 }
6706
6707 Py_BEGIN_ALLOW_THREADS
6708 res = writev(fd, iov, cnt);
6709 Py_END_ALLOW_THREADS
6710
6711 iov_cleanup(iov, buf, cnt);
6712 return PyLong_FromSsize_t(res);
6713}
6714#endif
6715
6716#ifdef HAVE_PWRITE
6717PyDoc_STRVAR(posix_pwrite__doc__,
6718"pwrite(fd, string, offset) -> byteswritten\n\n\
6719Write string to a file descriptor, fd, from offset, leaving the file\n\
6720offset unchanged.");
6721
6722static PyObject *
6723posix_pwrite(PyObject *self, PyObject *args)
6724{
6725 Py_buffer pbuf;
6726 int fd;
6727 off_t offset;
6728 Py_ssize_t size;
6729
6730 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
6731 return NULL;
6732
6733 if (!_PyVerify_fd(fd)) {
6734 PyBuffer_Release(&pbuf);
6735 return posix_error();
6736 }
6737 Py_BEGIN_ALLOW_THREADS
6738 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
6739 Py_END_ALLOW_THREADS
6740 PyBuffer_Release(&pbuf);
6741 if (size < 0)
6742 return posix_error();
6743 return PyLong_FromSsize_t(size);
6744}
6745#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006746
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006747#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006748PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006749"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006750Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006751
Barry Warsaw53699e91996-12-10 23:23:01 +00006752static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006753posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006754{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006755 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006756 char *filename;
6757 int mode = 0666;
6758 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006759 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
6760 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00006761 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006762 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 Py_BEGIN_ALLOW_THREADS
6764 res = mkfifo(filename, mode);
6765 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006766 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006767 if (res < 0)
6768 return posix_error();
6769 Py_INCREF(Py_None);
6770 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006771}
6772#endif
6773
6774
Neal Norwitz11690112002-07-30 01:08:28 +00006775#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006776PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00006777"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006778Create a filesystem node (file, device special file or named pipe)\n\
6779named filename. mode specifies both the permissions to use and the\n\
6780type of node to be created, being combined (bitwise OR) with one of\n\
6781S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006782device defines the newly created device special file (probably using\n\
6783os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00006784
6785
6786static PyObject *
6787posix_mknod(PyObject *self, PyObject *args)
6788{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006789 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 char *filename;
6791 int mode = 0600;
6792 int device = 0;
6793 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006794 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
6795 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006797 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 Py_BEGIN_ALLOW_THREADS
6799 res = mknod(filename, mode, device);
6800 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00006801 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 if (res < 0)
6803 return posix_error();
6804 Py_INCREF(Py_None);
6805 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006806}
6807#endif
6808
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006809#ifdef HAVE_DEVICE_MACROS
6810PyDoc_STRVAR(posix_major__doc__,
6811"major(device) -> major number\n\
6812Extracts a device major number from a raw device number.");
6813
6814static PyObject *
6815posix_major(PyObject *self, PyObject *args)
6816{
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 int device;
6818 if (!PyArg_ParseTuple(args, "i:major", &device))
6819 return NULL;
6820 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006821}
6822
6823PyDoc_STRVAR(posix_minor__doc__,
6824"minor(device) -> minor number\n\
6825Extracts a device minor number from a raw device number.");
6826
6827static PyObject *
6828posix_minor(PyObject *self, PyObject *args)
6829{
Victor Stinner8c62be82010-05-06 00:08:46 +00006830 int device;
6831 if (!PyArg_ParseTuple(args, "i:minor", &device))
6832 return NULL;
6833 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006834}
6835
6836PyDoc_STRVAR(posix_makedev__doc__,
6837"makedev(major, minor) -> device number\n\
6838Composes a raw device number from the major and minor device numbers.");
6839
6840static PyObject *
6841posix_makedev(PyObject *self, PyObject *args)
6842{
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 int major, minor;
6844 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
6845 return NULL;
6846 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00006847}
6848#endif /* device macros */
6849
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006850
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006851#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006852PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006853"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006854Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006855
Barry Warsaw53699e91996-12-10 23:23:01 +00006856static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006857posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006858{
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 int fd;
6860 off_t length;
6861 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006862
Ross Lagerwall7807c352011-03-17 20:20:30 +02006863 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006865
Victor Stinner8c62be82010-05-06 00:08:46 +00006866 Py_BEGIN_ALLOW_THREADS
6867 res = ftruncate(fd, length);
6868 Py_END_ALLOW_THREADS
6869 if (res < 0)
6870 return posix_error();
6871 Py_INCREF(Py_None);
6872 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00006873}
6874#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006875
Ross Lagerwall7807c352011-03-17 20:20:30 +02006876#ifdef HAVE_TRUNCATE
6877PyDoc_STRVAR(posix_truncate__doc__,
6878"truncate(path, length)\n\n\
6879Truncate the file given by path to length bytes.");
6880
6881static PyObject *
6882posix_truncate(PyObject *self, PyObject *args)
6883{
6884 PyObject *opath;
6885 const char *path;
6886 off_t length;
6887 int res;
6888
6889 if (!PyArg_ParseTuple(args, "O&O&:truncate",
6890 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
6891 return NULL;
6892 path = PyBytes_AsString(opath);
6893
6894 Py_BEGIN_ALLOW_THREADS
6895 res = truncate(path, length);
6896 Py_END_ALLOW_THREADS
6897 Py_DECREF(opath);
6898 if (res < 0)
6899 return posix_error();
6900 Py_RETURN_NONE;
6901}
6902#endif
6903
6904#ifdef HAVE_POSIX_FALLOCATE
6905PyDoc_STRVAR(posix_posix_fallocate__doc__,
6906"posix_fallocate(fd, offset, len)\n\n\
6907Ensures that enough disk space is allocated for the file specified by fd\n\
6908starting from offset and continuing for len bytes.");
6909
6910static PyObject *
6911posix_posix_fallocate(PyObject *self, PyObject *args)
6912{
6913 off_t len, offset;
6914 int res, fd;
6915
6916 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
6917 &fd, _parse_off_t, &offset, _parse_off_t, &len))
6918 return NULL;
6919
6920 Py_BEGIN_ALLOW_THREADS
6921 res = posix_fallocate(fd, offset, len);
6922 Py_END_ALLOW_THREADS
6923 if (res != 0) {
6924 errno = res;
6925 return posix_error();
6926 }
6927 Py_RETURN_NONE;
6928}
6929#endif
6930
6931#ifdef HAVE_POSIX_FADVISE
6932PyDoc_STRVAR(posix_posix_fadvise__doc__,
6933"posix_fadvise(fd, offset, len, advice)\n\n\
6934Announces an intention to access data in a specific pattern thus allowing\n\
6935the kernel to make optimizations.\n\
6936The advice applies to the region of the file specified by fd starting at\n\
6937offset and continuing for len bytes.\n\
6938advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
6939POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
6940POSIX_FADV_DONTNEED.");
6941
6942static PyObject *
6943posix_posix_fadvise(PyObject *self, PyObject *args)
6944{
6945 off_t len, offset;
6946 int res, fd, advice;
6947
6948 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
6949 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
6950 return NULL;
6951
6952 Py_BEGIN_ALLOW_THREADS
6953 res = posix_fadvise(fd, offset, len, advice);
6954 Py_END_ALLOW_THREADS
6955 if (res != 0) {
6956 errno = res;
6957 return posix_error();
6958 }
6959 Py_RETURN_NONE;
6960}
6961#endif
6962
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006963#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006964PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006965"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006966Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006967
Fred Drake762e2061999-08-26 17:23:54 +00006968/* Save putenv() parameters as values here, so we can collect them when they
6969 * get re-set with another call for the same key. */
6970static PyObject *posix_putenv_garbage;
6971
Tim Peters5aa91602002-01-30 05:46:57 +00006972static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006973posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006974{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006975#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 wchar_t *s1, *s2;
6977 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006978#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 PyObject *os1, *os2;
6980 char *s1, *s2;
6981 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006982#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006983 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006985
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006986#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 if (!PyArg_ParseTuple(args,
6988 "uu:putenv",
6989 &s1, &s2))
6990 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006991#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006992 if (!PyArg_ParseTuple(args,
6993 "O&O&:putenv",
6994 PyUnicode_FSConverter, &os1,
6995 PyUnicode_FSConverter, &os2))
6996 return NULL;
6997 s1 = PyBytes_AsString(os1);
6998 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006999#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007000
7001#if defined(PYOS_OS2)
7002 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7003 APIRET rc;
7004
Guido van Rossumd48f2521997-12-05 22:19:34 +00007005 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007006 if (rc != NO_ERROR) {
7007 os2_error(rc);
7008 goto error;
7009 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007010
7011 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7012 APIRET rc;
7013
Guido van Rossumd48f2521997-12-05 22:19:34 +00007014 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007015 if (rc != NO_ERROR) {
7016 os2_error(rc);
7017 goto error;
7018 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007019 } else {
7020#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 /* XXX This can leak memory -- not easy to fix :-( */
7022 /* len includes space for a trailing \0; the size arg to
7023 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007024#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 len = wcslen(s1) + wcslen(s2) + 2;
7026 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007027#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007028 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007030#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007031 if (newstr == NULL) {
7032 PyErr_NoMemory();
7033 goto error;
7034 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007035#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 newenv = PyUnicode_AsUnicode(newstr);
7037 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7038 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007040 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007042#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 newenv = PyBytes_AS_STRING(newstr);
7044 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7045 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007046 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007047 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007049#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007050
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 /* Install the first arg and newstr in posix_putenv_garbage;
7052 * this will cause previous value to be collected. This has to
7053 * happen after the real putenv() call because the old value
7054 * was still accessible until then. */
7055 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007056#ifdef MS_WINDOWS
7057 PyTuple_GET_ITEM(args, 0),
7058#else
7059 os1,
7060#endif
7061 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 /* really not much we can do; just leak */
7063 PyErr_Clear();
7064 }
7065 else {
7066 Py_DECREF(newstr);
7067 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007068
7069#if defined(PYOS_OS2)
7070 }
7071#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007072
Martin v. Löwis011e8422009-05-05 04:43:17 +00007073#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 Py_DECREF(os1);
7075 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007076#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007077 Py_RETURN_NONE;
7078
7079error:
7080#ifndef MS_WINDOWS
7081 Py_DECREF(os1);
7082 Py_DECREF(os2);
7083#endif
7084 Py_XDECREF(newstr);
7085 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007086}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007087#endif /* putenv */
7088
Guido van Rossumc524d952001-10-19 01:31:59 +00007089#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007090PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007091"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007092Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007093
7094static PyObject *
7095posix_unsetenv(PyObject *self, PyObject *args)
7096{
Victor Stinner84ae1182010-05-06 22:05:07 +00007097#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007099
Victor Stinner8c62be82010-05-06 00:08:46 +00007100 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7101 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007102#else
7103 PyObject *os1;
7104 char *s1;
7105
7106 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7107 PyUnicode_FSConverter, &os1))
7108 return NULL;
7109 s1 = PyBytes_AsString(os1);
7110#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007111
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007113
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 /* Remove the key from posix_putenv_garbage;
7115 * this will cause it to be collected. This has to
7116 * happen after the real unsetenv() call because the
7117 * old value was still accessible until then.
7118 */
7119 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007120#ifdef MS_WINDOWS
7121 PyTuple_GET_ITEM(args, 0)
7122#else
7123 os1
7124#endif
7125 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 /* really not much we can do; just leak */
7127 PyErr_Clear();
7128 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007129
Victor Stinner84ae1182010-05-06 22:05:07 +00007130#ifndef MS_WINDOWS
7131 Py_DECREF(os1);
7132#endif
7133 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007134}
7135#endif /* unsetenv */
7136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007137PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007138"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007139Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007140
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007141static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007142posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007143{
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 int code;
7145 char *message;
7146 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7147 return NULL;
7148 message = strerror(code);
7149 if (message == NULL) {
7150 PyErr_SetString(PyExc_ValueError,
7151 "strerror() argument out of range");
7152 return NULL;
7153 }
7154 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007155}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007156
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007157
Guido van Rossumc9641791998-08-04 15:26:23 +00007158#ifdef HAVE_SYS_WAIT_H
7159
Fred Drake106c1a02002-04-23 15:58:02 +00007160#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007161PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007162"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007163Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007164
7165static PyObject *
7166posix_WCOREDUMP(PyObject *self, PyObject *args)
7167{
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 WAIT_TYPE status;
7169 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007170
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7172 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007173
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007175}
7176#endif /* WCOREDUMP */
7177
7178#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007179PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007180"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007181Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007182job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007183
7184static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007185posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007186{
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 WAIT_TYPE status;
7188 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007189
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7191 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007192
Victor Stinner8c62be82010-05-06 00:08:46 +00007193 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007194}
7195#endif /* WIFCONTINUED */
7196
Guido van Rossumc9641791998-08-04 15:26:23 +00007197#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007198PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007199"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007200Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007201
7202static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007203posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007204{
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 WAIT_TYPE status;
7206 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007207
Victor Stinner8c62be82010-05-06 00:08:46 +00007208 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7209 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007210
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007212}
7213#endif /* WIFSTOPPED */
7214
7215#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007216PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007217"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007218Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007219
7220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007221posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007222{
Victor Stinner8c62be82010-05-06 00:08:46 +00007223 WAIT_TYPE status;
7224 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007225
Victor Stinner8c62be82010-05-06 00:08:46 +00007226 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7227 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007228
Victor Stinner8c62be82010-05-06 00:08:46 +00007229 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007230}
7231#endif /* WIFSIGNALED */
7232
7233#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007234PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007235"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007236Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007237system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007238
7239static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007240posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007241{
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 WAIT_TYPE status;
7243 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007244
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7246 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007247
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007249}
7250#endif /* WIFEXITED */
7251
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007252#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007253PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007254"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007255Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007256
7257static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007258posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007259{
Victor Stinner8c62be82010-05-06 00:08:46 +00007260 WAIT_TYPE status;
7261 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007262
Victor Stinner8c62be82010-05-06 00:08:46 +00007263 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7264 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007265
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007267}
7268#endif /* WEXITSTATUS */
7269
7270#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007271PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007272"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007273Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007274value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007275
7276static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007277posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007278{
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 WAIT_TYPE status;
7280 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007281
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7283 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007284
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007286}
7287#endif /* WTERMSIG */
7288
7289#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007290PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007291"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007292Return the signal that stopped the process that provided\n\
7293the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007294
7295static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007296posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007297{
Victor Stinner8c62be82010-05-06 00:08:46 +00007298 WAIT_TYPE status;
7299 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007300
Victor Stinner8c62be82010-05-06 00:08:46 +00007301 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7302 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007303
Victor Stinner8c62be82010-05-06 00:08:46 +00007304 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007305}
7306#endif /* WSTOPSIG */
7307
7308#endif /* HAVE_SYS_WAIT_H */
7309
7310
Thomas Wouters477c8d52006-05-27 19:21:47 +00007311#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007312#ifdef _SCO_DS
7313/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7314 needed definitions in sys/statvfs.h */
7315#define _SVID3
7316#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007317#include <sys/statvfs.h>
7318
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007319static PyObject*
7320_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007321 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7322 if (v == NULL)
7323 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007324
7325#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7327 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7328 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7329 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7330 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7331 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7332 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7333 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7334 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7335 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007336#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007337 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7338 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7339 PyStructSequence_SET_ITEM(v, 2,
7340 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7341 PyStructSequence_SET_ITEM(v, 3,
7342 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7343 PyStructSequence_SET_ITEM(v, 4,
7344 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7345 PyStructSequence_SET_ITEM(v, 5,
7346 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7347 PyStructSequence_SET_ITEM(v, 6,
7348 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7349 PyStructSequence_SET_ITEM(v, 7,
7350 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7351 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7352 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007353#endif
7354
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007356}
7357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007358PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007359"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007360Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007361
7362static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007363posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007364{
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 int fd, res;
7366 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007367
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7369 return NULL;
7370 Py_BEGIN_ALLOW_THREADS
7371 res = fstatvfs(fd, &st);
7372 Py_END_ALLOW_THREADS
7373 if (res != 0)
7374 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007375
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007377}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007378#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007379
7380
Thomas Wouters477c8d52006-05-27 19:21:47 +00007381#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007382#include <sys/statvfs.h>
7383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007384PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007385"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007386Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007387
7388static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007389posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007390{
Victor Stinner8c62be82010-05-06 00:08:46 +00007391 char *path;
7392 int res;
7393 struct statvfs st;
7394 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7395 return NULL;
7396 Py_BEGIN_ALLOW_THREADS
7397 res = statvfs(path, &st);
7398 Py_END_ALLOW_THREADS
7399 if (res != 0)
7400 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007401
Victor Stinner8c62be82010-05-06 00:08:46 +00007402 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007403}
7404#endif /* HAVE_STATVFS */
7405
Fred Drakec9680921999-12-13 16:37:25 +00007406/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
7407 * It maps strings representing configuration variable names to
7408 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00007409 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00007410 * rarely-used constants. There are three separate tables that use
7411 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00007412 *
7413 * This code is always included, even if none of the interfaces that
7414 * need it are included. The #if hackery needed to avoid it would be
7415 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00007416 */
7417struct constdef {
7418 char *name;
7419 long value;
7420};
7421
Fred Drake12c6e2d1999-12-14 21:25:03 +00007422static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007423conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00007424 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00007425{
Christian Heimes217cfd12007-12-02 14:31:20 +00007426 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007427 *valuep = PyLong_AS_LONG(arg);
7428 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007429 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00007430 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00007431 /* look up the value in the table using a binary search */
7432 size_t lo = 0;
7433 size_t mid;
7434 size_t hi = tablesize;
7435 int cmp;
7436 const char *confname;
7437 if (!PyUnicode_Check(arg)) {
7438 PyErr_SetString(PyExc_TypeError,
7439 "configuration names must be strings or integers");
7440 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007441 }
Stefan Krah0e803b32010-11-26 16:16:47 +00007442 confname = _PyUnicode_AsString(arg);
7443 if (confname == NULL)
7444 return 0;
7445 while (lo < hi) {
7446 mid = (lo + hi) / 2;
7447 cmp = strcmp(confname, table[mid].name);
7448 if (cmp < 0)
7449 hi = mid;
7450 else if (cmp > 0)
7451 lo = mid + 1;
7452 else {
7453 *valuep = table[mid].value;
7454 return 1;
7455 }
7456 }
7457 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
7458 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00007460}
7461
7462
7463#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
7464static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007465#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007467#endif
7468#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007469 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007470#endif
Fred Drakec9680921999-12-13 16:37:25 +00007471#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007473#endif
7474#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00007475 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00007476#endif
7477#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00007478 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00007479#endif
7480#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00007481 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00007482#endif
7483#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007484 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007485#endif
7486#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00007487 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00007488#endif
7489#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007490 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00007491#endif
7492#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007493 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007494#endif
7495#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007496 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00007497#endif
7498#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007500#endif
7501#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007502 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00007503#endif
7504#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007505 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007506#endif
7507#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00007509#endif
7510#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007512#endif
7513#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007514 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00007515#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00007516#ifdef _PC_ACL_ENABLED
7517 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
7518#endif
7519#ifdef _PC_MIN_HOLE_SIZE
7520 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
7521#endif
7522#ifdef _PC_ALLOC_SIZE_MIN
7523 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
7524#endif
7525#ifdef _PC_REC_INCR_XFER_SIZE
7526 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
7527#endif
7528#ifdef _PC_REC_MAX_XFER_SIZE
7529 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
7530#endif
7531#ifdef _PC_REC_MIN_XFER_SIZE
7532 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
7533#endif
7534#ifdef _PC_REC_XFER_ALIGN
7535 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
7536#endif
7537#ifdef _PC_SYMLINK_MAX
7538 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
7539#endif
7540#ifdef _PC_XATTR_ENABLED
7541 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
7542#endif
7543#ifdef _PC_XATTR_EXISTS
7544 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
7545#endif
7546#ifdef _PC_TIMESTAMP_RESOLUTION
7547 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
7548#endif
Fred Drakec9680921999-12-13 16:37:25 +00007549};
7550
Fred Drakec9680921999-12-13 16:37:25 +00007551static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007552conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007553{
7554 return conv_confname(arg, valuep, posix_constants_pathconf,
7555 sizeof(posix_constants_pathconf)
7556 / sizeof(struct constdef));
7557}
7558#endif
7559
7560#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007561PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007562"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007563Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007564If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007565
7566static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007567posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007568{
7569 PyObject *result = NULL;
7570 int name, fd;
7571
Fred Drake12c6e2d1999-12-14 21:25:03 +00007572 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
7573 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007574 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007575
Stefan Krah0e803b32010-11-26 16:16:47 +00007576 errno = 0;
7577 limit = fpathconf(fd, name);
7578 if (limit == -1 && errno != 0)
7579 posix_error();
7580 else
7581 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007582 }
7583 return result;
7584}
7585#endif
7586
7587
7588#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007589PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007590"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00007591Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007592If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00007593
7594static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007595posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007596{
7597 PyObject *result = NULL;
7598 int name;
7599 char *path;
7600
7601 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
7602 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007603 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00007604
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 errno = 0;
7606 limit = pathconf(path, name);
7607 if (limit == -1 && errno != 0) {
7608 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00007609 /* could be a path or name problem */
7610 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00007611 else
Stefan Krah99439262010-11-26 12:58:05 +00007612 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 }
7614 else
7615 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00007616 }
7617 return result;
7618}
7619#endif
7620
7621#ifdef HAVE_CONFSTR
7622static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00007623#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00007624 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00007625#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00007626#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007628#endif
7629#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00007631#endif
Fred Draked86ed291999-12-15 15:34:33 +00007632#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007633 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007634#endif
7635#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007636 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007637#endif
7638#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007639 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007640#endif
7641#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007643#endif
Fred Drakec9680921999-12-13 16:37:25 +00007644#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007645 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007646#endif
7647#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007648 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007649#endif
7650#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007652#endif
7653#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007654 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007655#endif
7656#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007657 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007658#endif
7659#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007661#endif
7662#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007664#endif
7665#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007666 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007667#endif
Fred Draked86ed291999-12-15 15:34:33 +00007668#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00007669 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00007670#endif
Fred Drakec9680921999-12-13 16:37:25 +00007671#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00007672 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00007673#endif
Fred Draked86ed291999-12-15 15:34:33 +00007674#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007675 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00007676#endif
7677#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007678 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00007679#endif
7680#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00007682#endif
7683#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007684 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00007685#endif
Fred Drakec9680921999-12-13 16:37:25 +00007686#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007688#endif
7689#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007690 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007691#endif
7692#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007693 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007694#endif
7695#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007696 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007697#endif
7698#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007699 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007700#endif
7701#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007703#endif
7704#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007705 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007706#endif
7707#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007708 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007709#endif
7710#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007712#endif
7713#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007714 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007715#endif
7716#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007718#endif
7719#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007720 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007721#endif
7722#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007723 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007724#endif
7725#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007726 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007727#endif
7728#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00007730#endif
7731#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007732 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00007733#endif
Fred Draked86ed291999-12-15 15:34:33 +00007734#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007735 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007736#endif
7737#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00007738 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00007739#endif
7740#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00007741 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00007742#endif
7743#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007745#endif
7746#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007747 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007748#endif
7749#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00007751#endif
7752#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007753 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00007754#endif
7755#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00007756 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00007757#endif
7758#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007759 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00007760#endif
7761#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00007762 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00007763#endif
7764#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00007765 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00007766#endif
7767#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007768 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00007769#endif
7770#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00007772#endif
Fred Drakec9680921999-12-13 16:37:25 +00007773};
7774
7775static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007776conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007777{
7778 return conv_confname(arg, valuep, posix_constants_confstr,
7779 sizeof(posix_constants_confstr)
7780 / sizeof(struct constdef));
7781}
7782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007783PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007784"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007785Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007786
7787static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007788posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007789{
7790 PyObject *result = NULL;
7791 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00007792 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00007793 int len;
Fred Drakec9680921999-12-13 16:37:25 +00007794
Victor Stinnercb043522010-09-10 23:49:04 +00007795 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
7796 return NULL;
7797
7798 errno = 0;
7799 len = confstr(name, buffer, sizeof(buffer));
7800 if (len == 0) {
7801 if (errno) {
7802 posix_error();
7803 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00007804 }
7805 else {
Victor Stinnercb043522010-09-10 23:49:04 +00007806 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00007807 }
7808 }
Victor Stinnercb043522010-09-10 23:49:04 +00007809
7810 if ((unsigned int)len >= sizeof(buffer)) {
7811 char *buf = PyMem_Malloc(len);
7812 if (buf == NULL)
7813 return PyErr_NoMemory();
7814 confstr(name, buf, len);
7815 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
7816 PyMem_Free(buf);
7817 }
7818 else
7819 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00007820 return result;
7821}
7822#endif
7823
7824
7825#ifdef HAVE_SYSCONF
7826static struct constdef posix_constants_sysconf[] = {
7827#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007828 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00007829#endif
7830#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00007832#endif
7833#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007835#endif
7836#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007837 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007838#endif
7839#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007840 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007841#endif
7842#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00007843 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00007844#endif
7845#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00007846 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00007847#endif
7848#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00007849 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00007850#endif
7851#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007852 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00007853#endif
7854#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007856#endif
Fred Draked86ed291999-12-15 15:34:33 +00007857#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00007859#endif
7860#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00007862#endif
Fred Drakec9680921999-12-13 16:37:25 +00007863#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007865#endif
Fred Drakec9680921999-12-13 16:37:25 +00007866#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007868#endif
7869#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007871#endif
7872#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007874#endif
7875#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007877#endif
7878#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007880#endif
Fred Draked86ed291999-12-15 15:34:33 +00007881#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00007883#endif
Fred Drakec9680921999-12-13 16:37:25 +00007884#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007885 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007886#endif
7887#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007888 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007889#endif
7890#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007891 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007892#endif
7893#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007895#endif
7896#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007898#endif
Fred Draked86ed291999-12-15 15:34:33 +00007899#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00007900 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00007901#endif
Fred Drakec9680921999-12-13 16:37:25 +00007902#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007903 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007904#endif
7905#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007907#endif
7908#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007910#endif
7911#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007913#endif
7914#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007916#endif
7917#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00007919#endif
7920#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007922#endif
7923#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007925#endif
7926#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007927 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007928#endif
7929#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007931#endif
7932#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007933 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007934#endif
7935#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007937#endif
7938#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007940#endif
7941#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007942 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007943#endif
7944#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007946#endif
7947#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007949#endif
7950#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00007952#endif
7953#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007955#endif
7956#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007958#endif
7959#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00007961#endif
7962#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007964#endif
7965#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007966 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007967#endif
7968#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007970#endif
Fred Draked86ed291999-12-15 15:34:33 +00007971#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007973#endif
Fred Drakec9680921999-12-13 16:37:25 +00007974#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007976#endif
7977#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007979#endif
7980#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007982#endif
Fred Draked86ed291999-12-15 15:34:33 +00007983#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007985#endif
Fred Drakec9680921999-12-13 16:37:25 +00007986#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007988#endif
Fred Draked86ed291999-12-15 15:34:33 +00007989#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007991#endif
7992#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007993 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007994#endif
Fred Drakec9680921999-12-13 16:37:25 +00007995#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007996 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007997#endif
7998#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008000#endif
8001#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008003#endif
8004#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008006#endif
Fred Draked86ed291999-12-15 15:34:33 +00008007#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008008 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008009#endif
Fred Drakec9680921999-12-13 16:37:25 +00008010#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008011 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008012#endif
8013#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008015#endif
8016#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008018#endif
8019#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008021#endif
8022#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008024#endif
8025#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008027#endif
8028#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008030#endif
Fred Draked86ed291999-12-15 15:34:33 +00008031#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008033#endif
Fred Drakec9680921999-12-13 16:37:25 +00008034#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008035 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008036#endif
8037#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008038 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008039#endif
Fred Draked86ed291999-12-15 15:34:33 +00008040#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008041 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008042#endif
Fred Drakec9680921999-12-13 16:37:25 +00008043#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008044 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008045#endif
8046#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008047 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008048#endif
8049#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008051#endif
8052#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008054#endif
8055#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008057#endif
8058#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008060#endif
8061#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008062 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008063#endif
8064#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008065 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008066#endif
8067#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008068 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008069#endif
Fred Draked86ed291999-12-15 15:34:33 +00008070#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008072#endif
8073#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008074 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008075#endif
Fred Drakec9680921999-12-13 16:37:25 +00008076#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008077 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008078#endif
8079#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008080 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008081#endif
8082#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008083 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008084#endif
8085#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008086 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008087#endif
8088#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008089 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008090#endif
8091#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008092 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008093#endif
8094#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008095 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008096#endif
8097#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008098 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008099#endif
8100#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008101 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008102#endif
8103#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008104 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008105#endif
8106#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008108#endif
8109#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008111#endif
8112#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008113 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008114#endif
8115#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008116 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008117#endif
8118#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008119 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008120#endif
8121#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008122 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008123#endif
8124#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008125 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008126#endif
8127#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008128 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008129#endif
8130#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008131 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008132#endif
8133#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008134 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008135#endif
8136#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008137 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008138#endif
8139#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008140 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008141#endif
8142#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008143 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008144#endif
8145#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008146 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008147#endif
8148#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008149 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008150#endif
8151#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008152 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008153#endif
8154#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008155 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008156#endif
8157#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008158 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008159#endif
8160#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008161 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008162#endif
8163#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008164 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008165#endif
8166#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008167 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008168#endif
8169#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008170 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008171#endif
8172#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008173 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008174#endif
8175#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008176 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008177#endif
8178#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008179 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008180#endif
Fred Draked86ed291999-12-15 15:34:33 +00008181#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008182 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008183#endif
Fred Drakec9680921999-12-13 16:37:25 +00008184#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008185 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008186#endif
8187#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008189#endif
8190#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008191 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008192#endif
8193#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008194 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008195#endif
8196#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008197 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008198#endif
8199#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008200 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008201#endif
8202#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008203 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008204#endif
8205#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008206 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008207#endif
8208#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008209 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008210#endif
8211#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008212 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008213#endif
8214#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008215 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008216#endif
8217#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008218 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008219#endif
8220#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008221 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008222#endif
8223#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008225#endif
8226#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008227 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008228#endif
8229#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008230 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008231#endif
8232#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008233 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008234#endif
8235#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008236 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008237#endif
8238#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008239 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008240#endif
8241#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008242 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008243#endif
8244#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008246#endif
8247#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008249#endif
8250#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008251 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008252#endif
8253#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008255#endif
8256#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008257 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008258#endif
8259#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008261#endif
8262#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008263 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008264#endif
8265#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008266 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008267#endif
8268#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008270#endif
8271#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008273#endif
8274#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008276#endif
8277#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008279#endif
8280#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008282#endif
8283#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008284 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008285#endif
8286#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008288#endif
8289#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008291#endif
8292#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008294#endif
8295#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008297#endif
8298#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008300#endif
8301#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008303#endif
8304#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008306#endif
8307#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008308 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008309#endif
8310#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008312#endif
8313#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008315#endif
8316#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008318#endif
8319};
8320
8321static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008322conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008323{
8324 return conv_confname(arg, valuep, posix_constants_sysconf,
8325 sizeof(posix_constants_sysconf)
8326 / sizeof(struct constdef));
8327}
8328
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008329PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008330"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008331Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008332
8333static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008334posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008335{
8336 PyObject *result = NULL;
8337 int name;
8338
8339 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8340 int value;
8341
8342 errno = 0;
8343 value = sysconf(name);
8344 if (value == -1 && errno != 0)
8345 posix_error();
8346 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008347 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008348 }
8349 return result;
8350}
8351#endif
8352
8353
Fred Drakebec628d1999-12-15 18:31:10 +00008354/* This code is used to ensure that the tables of configuration value names
8355 * are in sorted order as required by conv_confname(), and also to build the
8356 * the exported dictionaries that are used to publish information about the
8357 * names available on the host platform.
8358 *
8359 * Sorting the table at runtime ensures that the table is properly ordered
8360 * when used, even for platforms we're not able to test on. It also makes
8361 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008362 */
Fred Drakebec628d1999-12-15 18:31:10 +00008363
8364static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008365cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008366{
8367 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008369 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008371
8372 return strcmp(c1->name, c2->name);
8373}
8374
8375static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008376setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008378{
Fred Drakebec628d1999-12-15 18:31:10 +00008379 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008380 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008381
8382 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8383 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008384 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008386
Barry Warsaw3155db32000-04-13 15:20:40 +00008387 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 PyObject *o = PyLong_FromLong(table[i].value);
8389 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
8390 Py_XDECREF(o);
8391 Py_DECREF(d);
8392 return -1;
8393 }
8394 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00008395 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00008396 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00008397}
8398
Fred Drakebec628d1999-12-15 18:31:10 +00008399/* Return -1 on failure, 0 on success. */
8400static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008401setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008402{
8403#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00008404 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00008405 sizeof(posix_constants_pathconf)
8406 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008407 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008408 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008409#endif
8410#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00008411 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00008412 sizeof(posix_constants_confstr)
8413 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008414 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008415 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008416#endif
8417#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00008418 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00008419 sizeof(posix_constants_sysconf)
8420 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00008421 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00008422 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00008423#endif
Fred Drakebec628d1999-12-15 18:31:10 +00008424 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00008425}
Fred Draked86ed291999-12-15 15:34:33 +00008426
8427
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008428PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008429"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008430Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008431in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008432
8433static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008434posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008435{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008436 abort();
8437 /*NOTREACHED*/
8438 Py_FatalError("abort() called from Python code didn't abort!");
8439 return NULL;
8440}
Fred Drakebec628d1999-12-15 18:31:10 +00008441
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008442#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008443PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00008444"startfile(filepath [, operation]) - Start a file with its associated\n\
8445application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008446\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00008447When \"operation\" is not specified or \"open\", this acts like\n\
8448double-clicking the file in Explorer, or giving the file name as an\n\
8449argument to the DOS \"start\" command: the file is opened with whatever\n\
8450application (if any) its extension is associated.\n\
8451When another \"operation\" is given, it specifies what should be done with\n\
8452the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00008453\n\
8454startfile returns as soon as the associated application is launched.\n\
8455There is no option to wait for the application to close, and no way\n\
8456to retrieve the application's exit status.\n\
8457\n\
8458The filepath is relative to the current directory. If you want to use\n\
8459an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008460the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00008461
8462static PyObject *
8463win32_startfile(PyObject *self, PyObject *args)
8464{
Victor Stinner8c62be82010-05-06 00:08:46 +00008465 PyObject *ofilepath;
8466 char *filepath;
8467 char *operation = NULL;
8468 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00008469
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 PyObject *unipath, *woperation = NULL;
8471 if (!PyArg_ParseTuple(args, "U|s:startfile",
8472 &unipath, &operation)) {
8473 PyErr_Clear();
8474 goto normal;
8475 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008476
Victor Stinner8c62be82010-05-06 00:08:46 +00008477 if (operation) {
8478 woperation = PyUnicode_DecodeASCII(operation,
8479 strlen(operation), NULL);
8480 if (!woperation) {
8481 PyErr_Clear();
8482 operation = NULL;
8483 goto normal;
8484 }
8485 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00008486
Victor Stinner8c62be82010-05-06 00:08:46 +00008487 Py_BEGIN_ALLOW_THREADS
8488 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
8489 PyUnicode_AS_UNICODE(unipath),
8490 NULL, NULL, SW_SHOWNORMAL);
8491 Py_END_ALLOW_THREADS
8492
8493 Py_XDECREF(woperation);
8494 if (rc <= (HINSTANCE)32) {
8495 PyObject *errval = win32_error_unicode("startfile",
8496 PyUnicode_AS_UNICODE(unipath));
8497 return errval;
8498 }
8499 Py_INCREF(Py_None);
8500 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008501
8502normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 if (!PyArg_ParseTuple(args, "O&|s:startfile",
8504 PyUnicode_FSConverter, &ofilepath,
8505 &operation))
8506 return NULL;
8507 filepath = PyBytes_AsString(ofilepath);
8508 Py_BEGIN_ALLOW_THREADS
8509 rc = ShellExecute((HWND)0, operation, filepath,
8510 NULL, NULL, SW_SHOWNORMAL);
8511 Py_END_ALLOW_THREADS
8512 if (rc <= (HINSTANCE)32) {
8513 PyObject *errval = win32_error("startfile", filepath);
8514 Py_DECREF(ofilepath);
8515 return errval;
8516 }
8517 Py_DECREF(ofilepath);
8518 Py_INCREF(Py_None);
8519 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00008520}
8521#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008522
Martin v. Löwis438b5342002-12-27 10:16:42 +00008523#ifdef HAVE_GETLOADAVG
8524PyDoc_STRVAR(posix_getloadavg__doc__,
8525"getloadavg() -> (float, float, float)\n\n\
8526Return the number of processes in the system run queue averaged over\n\
8527the last 1, 5, and 15 minutes or raises OSError if the load average\n\
8528was unobtainable");
8529
8530static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00008531posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00008532{
8533 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00008534 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008535 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
8536 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00008537 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00008538 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00008539}
8540#endif
8541
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008542#ifdef MS_WINDOWS
8543
8544PyDoc_STRVAR(win32_urandom__doc__,
8545"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008546Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008547
8548typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
8549 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
8550 DWORD dwFlags );
8551typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
8552 BYTE *pbBuffer );
8553
8554static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00008555/* This handle is never explicitly released. Instead, the operating
8556 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008557static HCRYPTPROV hCryptProv = 0;
8558
Tim Peters4ad82172004-08-30 17:02:04 +00008559static PyObject*
8560win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008561{
Victor Stinner8c62be82010-05-06 00:08:46 +00008562 int howMany;
8563 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008564
Victor Stinner8c62be82010-05-06 00:08:46 +00008565 /* Read arguments */
8566 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8567 return NULL;
8568 if (howMany < 0)
8569 return PyErr_Format(PyExc_ValueError,
8570 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008571
Victor Stinner8c62be82010-05-06 00:08:46 +00008572 if (hCryptProv == 0) {
8573 HINSTANCE hAdvAPI32 = NULL;
8574 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008575
Victor Stinner8c62be82010-05-06 00:08:46 +00008576 /* Obtain handle to the DLL containing CryptoAPI
8577 This should not fail */
8578 hAdvAPI32 = GetModuleHandle("advapi32.dll");
8579 if(hAdvAPI32 == NULL)
8580 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008581
Victor Stinner8c62be82010-05-06 00:08:46 +00008582 /* Obtain pointers to the CryptoAPI functions
8583 This will fail on some early versions of Win95 */
8584 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
8585 hAdvAPI32,
8586 "CryptAcquireContextA");
8587 if (pCryptAcquireContext == NULL)
8588 return PyErr_Format(PyExc_NotImplementedError,
8589 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008590
Victor Stinner8c62be82010-05-06 00:08:46 +00008591 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
8592 hAdvAPI32, "CryptGenRandom");
8593 if (pCryptGenRandom == NULL)
8594 return PyErr_Format(PyExc_NotImplementedError,
8595 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008596
Victor Stinner8c62be82010-05-06 00:08:46 +00008597 /* Acquire context */
8598 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
8599 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
8600 return win32_error("CryptAcquireContext", NULL);
8601 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008602
Victor Stinner8c62be82010-05-06 00:08:46 +00008603 /* Allocate bytes */
8604 result = PyBytes_FromStringAndSize(NULL, howMany);
8605 if (result != NULL) {
8606 /* Get random data */
8607 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
8608 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8609 PyBytes_AS_STRING(result))) {
8610 Py_DECREF(result);
8611 return win32_error("CryptGenRandom", NULL);
8612 }
8613 }
8614 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008615}
8616#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008617
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008618PyDoc_STRVAR(device_encoding__doc__,
8619"device_encoding(fd) -> str\n\n\
8620Return a string describing the encoding of the device\n\
8621if the output is a terminal; else return None.");
8622
8623static PyObject *
8624device_encoding(PyObject *self, PyObject *args)
8625{
Victor Stinner8c62be82010-05-06 00:08:46 +00008626 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02008627#if defined(MS_WINDOWS) || defined(MS_WIN64)
8628 UINT cp;
8629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008630 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
8631 return NULL;
8632 if (!_PyVerify_fd(fd) || !isatty(fd)) {
8633 Py_INCREF(Py_None);
8634 return Py_None;
8635 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008636#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02008637 if (fd == 0)
8638 cp = GetConsoleCP();
8639 else if (fd == 1 || fd == 2)
8640 cp = GetConsoleOutputCP();
8641 else
8642 cp = 0;
8643 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
8644 has no console */
8645 if (cp != 0)
8646 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008647#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00008648 {
8649 char *codeset = nl_langinfo(CODESET);
8650 if (codeset != NULL && codeset[0] != 0)
8651 return PyUnicode_FromString(codeset);
8652 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008653#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008654 Py_INCREF(Py_None);
8655 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00008656}
8657
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008658#ifdef __VMS
8659/* Use openssl random routine */
8660#include <openssl/rand.h>
8661PyDoc_STRVAR(vms_urandom__doc__,
8662"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00008663Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008664
8665static PyObject*
8666vms_urandom(PyObject *self, PyObject *args)
8667{
Victor Stinner8c62be82010-05-06 00:08:46 +00008668 int howMany;
8669 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008670
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 /* Read arguments */
8672 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
8673 return NULL;
8674 if (howMany < 0)
8675 return PyErr_Format(PyExc_ValueError,
8676 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008677
Victor Stinner8c62be82010-05-06 00:08:46 +00008678 /* Allocate bytes */
8679 result = PyBytes_FromStringAndSize(NULL, howMany);
8680 if (result != NULL) {
8681 /* Get random data */
8682 if (RAND_pseudo_bytes((unsigned char*)
8683 PyBytes_AS_STRING(result),
8684 howMany) < 0) {
8685 Py_DECREF(result);
8686 return PyErr_Format(PyExc_ValueError,
8687 "RAND_pseudo_bytes");
8688 }
8689 }
8690 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008691}
8692#endif
8693
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008694#ifdef HAVE_SETRESUID
8695PyDoc_STRVAR(posix_setresuid__doc__,
8696"setresuid(ruid, euid, suid)\n\n\
8697Set the current process's real, effective, and saved user ids.");
8698
8699static PyObject*
8700posix_setresuid (PyObject *self, PyObject *args)
8701{
Victor Stinner8c62be82010-05-06 00:08:46 +00008702 /* We assume uid_t is no larger than a long. */
8703 long ruid, euid, suid;
8704 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
8705 return NULL;
8706 if (setresuid(ruid, euid, suid) < 0)
8707 return posix_error();
8708 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008709}
8710#endif
8711
8712#ifdef HAVE_SETRESGID
8713PyDoc_STRVAR(posix_setresgid__doc__,
8714"setresgid(rgid, egid, sgid)\n\n\
8715Set the current process's real, effective, and saved group ids.");
8716
8717static PyObject*
8718posix_setresgid (PyObject *self, PyObject *args)
8719{
Victor Stinner8c62be82010-05-06 00:08:46 +00008720 /* We assume uid_t is no larger than a long. */
8721 long rgid, egid, sgid;
8722 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
8723 return NULL;
8724 if (setresgid(rgid, egid, sgid) < 0)
8725 return posix_error();
8726 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008727}
8728#endif
8729
8730#ifdef HAVE_GETRESUID
8731PyDoc_STRVAR(posix_getresuid__doc__,
8732"getresuid() -> (ruid, euid, suid)\n\n\
8733Get tuple of the current process's real, effective, and saved user ids.");
8734
8735static PyObject*
8736posix_getresuid (PyObject *self, PyObject *noargs)
8737{
Victor Stinner8c62be82010-05-06 00:08:46 +00008738 uid_t ruid, euid, suid;
8739 long l_ruid, l_euid, l_suid;
8740 if (getresuid(&ruid, &euid, &suid) < 0)
8741 return posix_error();
8742 /* Force the values into long's as we don't know the size of uid_t. */
8743 l_ruid = ruid;
8744 l_euid = euid;
8745 l_suid = suid;
8746 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008747}
8748#endif
8749
8750#ifdef HAVE_GETRESGID
8751PyDoc_STRVAR(posix_getresgid__doc__,
8752"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00008753Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008754
8755static PyObject*
8756posix_getresgid (PyObject *self, PyObject *noargs)
8757{
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 uid_t rgid, egid, sgid;
8759 long l_rgid, l_egid, l_sgid;
8760 if (getresgid(&rgid, &egid, &sgid) < 0)
8761 return posix_error();
8762 /* Force the values into long's as we don't know the size of uid_t. */
8763 l_rgid = rgid;
8764 l_egid = egid;
8765 l_sgid = sgid;
8766 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008767}
8768#endif
8769
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008770/* Posix *at family of functions:
8771 faccessat, fchmodat, fchownat, fstatat, futimesat,
8772 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
8773 unlinkat, utimensat, mkfifoat */
8774
8775#ifdef HAVE_FACCESSAT
8776PyDoc_STRVAR(posix_faccessat__doc__,
8777"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
8778Like access() but if path is relative, it is taken as relative to dirfd.\n\
8779flags is optional and can be constructed by ORing together zero or more\n\
8780of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
8781If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8782is interpreted relative to the current working directory.");
8783
8784static PyObject *
8785posix_faccessat(PyObject *self, PyObject *args)
8786{
8787 PyObject *opath;
8788 char *path;
8789 int mode;
8790 int res;
8791 int dirfd, flags = 0;
8792 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
8793 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8794 return NULL;
8795 path = PyBytes_AsString(opath);
8796 Py_BEGIN_ALLOW_THREADS
8797 res = faccessat(dirfd, path, mode, flags);
8798 Py_END_ALLOW_THREADS
8799 Py_DECREF(opath);
8800 return PyBool_FromLong(res == 0);
8801}
8802#endif
8803
8804#ifdef HAVE_FCHMODAT
8805PyDoc_STRVAR(posix_fchmodat__doc__,
8806"fchmodat(dirfd, path, mode, flags=0)\n\n\
8807Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
8808flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8809If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8810is interpreted relative to the current working directory.");
8811
8812static PyObject *
8813posix_fchmodat(PyObject *self, PyObject *args)
8814{
8815 int dirfd, mode, res;
8816 int flags = 0;
8817 PyObject *opath;
8818 char *path;
8819
8820 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
8821 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
8822 return NULL;
8823
8824 path = PyBytes_AsString(opath);
8825
8826 Py_BEGIN_ALLOW_THREADS
8827 res = fchmodat(dirfd, path, mode, flags);
8828 Py_END_ALLOW_THREADS
8829 Py_DECREF(opath);
8830 if (res < 0)
8831 return posix_error();
8832 Py_RETURN_NONE;
8833}
8834#endif /* HAVE_FCHMODAT */
8835
8836#ifdef HAVE_FCHOWNAT
8837PyDoc_STRVAR(posix_fchownat__doc__,
8838"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
8839Like chown() but if path is relative, it is taken as relative to dirfd.\n\
8840flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8841If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8842is interpreted relative to the current working directory.");
8843
8844static PyObject *
8845posix_fchownat(PyObject *self, PyObject *args)
8846{
8847 PyObject *opath;
8848 int dirfd, res;
8849 long uid, gid;
8850 int flags = 0;
8851 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008852
Antoine Pitrouf65132d2011-02-25 23:25:17 +00008853 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
8854 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
8855 return NULL;
8856
8857 path = PyBytes_AsString(opath);
8858
8859 Py_BEGIN_ALLOW_THREADS
8860 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
8861 Py_END_ALLOW_THREADS
8862 Py_DECREF(opath);
8863 if (res < 0)
8864 return posix_error();
8865 Py_RETURN_NONE;
8866}
8867#endif /* HAVE_FCHOWNAT */
8868
8869#ifdef HAVE_FSTATAT
8870PyDoc_STRVAR(posix_fstatat__doc__,
8871"fstatat(dirfd, path, flags=0) -> stat result\n\n\
8872Like stat() but if path is relative, it is taken as relative to dirfd.\n\
8873flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
8874If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8875is interpreted relative to the current working directory.");
8876
8877static PyObject *
8878posix_fstatat(PyObject *self, PyObject *args)
8879{
8880 PyObject *opath;
8881 char *path;
8882 STRUCT_STAT st;
8883 int dirfd, res, flags = 0;
8884
8885 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
8886 &dirfd, PyUnicode_FSConverter, &opath, &flags))
8887 return NULL;
8888 path = PyBytes_AsString(opath);
8889
8890 Py_BEGIN_ALLOW_THREADS
8891 res = fstatat(dirfd, path, &st, flags);
8892 Py_END_ALLOW_THREADS
8893 Py_DECREF(opath);
8894 if (res != 0)
8895 return posix_error();
8896
8897 return _pystat_fromstructstat(&st);
8898}
8899#endif
8900
8901#ifdef HAVE_FUTIMESAT
8902PyDoc_STRVAR(posix_futimesat__doc__,
8903"futimesat(dirfd, path, (atime, mtime))\n\
8904futimesat(dirfd, path, None)\n\n\
8905Like utime() but if path is relative, it is taken as relative to dirfd.\n\
8906If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8907is interpreted relative to the current working directory.");
8908
8909static PyObject *
8910posix_futimesat(PyObject *self, PyObject *args)
8911{
8912 PyObject *opath;
8913 char *path;
8914 int res, dirfd;
8915 PyObject* arg;
8916
8917 struct timeval buf[2];
8918
8919 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
8920 &dirfd, PyUnicode_FSConverter, &opath, &arg))
8921 return NULL;
8922 path = PyBytes_AsString(opath);
8923 if (arg == Py_None) {
8924 /* optional time values not given */
8925 Py_BEGIN_ALLOW_THREADS
8926 res = futimesat(dirfd, path, NULL);
8927 Py_END_ALLOW_THREADS
8928 }
8929 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
8930 PyErr_SetString(PyExc_TypeError,
8931 "futimesat() arg 3 must be a tuple (atime, mtime)");
8932 Py_DECREF(opath);
8933 return NULL;
8934 }
8935 else {
8936 if (extract_time(PyTuple_GET_ITEM(arg, 0),
8937 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
8938 Py_DECREF(opath);
8939 return NULL;
8940 }
8941 if (extract_time(PyTuple_GET_ITEM(arg, 1),
8942 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
8943 Py_DECREF(opath);
8944 return NULL;
8945 }
8946 Py_BEGIN_ALLOW_THREADS
8947 res = futimesat(dirfd, path, buf);
8948 Py_END_ALLOW_THREADS
8949 }
8950 Py_DECREF(opath);
8951 if (res < 0) {
8952 return posix_error();
8953 }
8954 Py_RETURN_NONE;
8955}
8956#endif
8957
8958#ifdef HAVE_LINKAT
8959PyDoc_STRVAR(posix_linkat__doc__,
8960"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
8961Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
8962and if dstpath is relative, it is taken as relative to dstfd.\n\
8963flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
8964If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
8965srcpath is interpreted relative to the current working directory. This\n\
8966also applies for dstpath.");
8967
8968static PyObject *
8969posix_linkat(PyObject *self, PyObject *args)
8970{
8971 PyObject *osrc, *odst;
8972 char *src, *dst;
8973 int res, srcfd, dstfd;
8974 int flags = 0;
8975
8976 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
8977 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
8978 return NULL;
8979 src = PyBytes_AsString(osrc);
8980 dst = PyBytes_AsString(odst);
8981 Py_BEGIN_ALLOW_THREADS
8982 res = linkat(srcfd, src, dstfd, dst, flags);
8983 Py_END_ALLOW_THREADS
8984 Py_DECREF(osrc);
8985 Py_DECREF(odst);
8986 if (res < 0)
8987 return posix_error();
8988 Py_RETURN_NONE;
8989}
8990#endif /* HAVE_LINKAT */
8991
8992#ifdef HAVE_MKDIRAT
8993PyDoc_STRVAR(posix_mkdirat__doc__,
8994"mkdirat(dirfd, path, mode=0o777)\n\n\
8995Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
8996If path is relative and dirfd is the special value AT_FDCWD, then path\n\
8997is interpreted relative to the current working directory.");
8998
8999static PyObject *
9000posix_mkdirat(PyObject *self, PyObject *args)
9001{
9002 int res, dirfd;
9003 PyObject *opath;
9004 char *path;
9005 int mode = 0777;
9006
9007 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9008 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9009 return NULL;
9010 path = PyBytes_AsString(opath);
9011 Py_BEGIN_ALLOW_THREADS
9012 res = mkdirat(dirfd, path, mode);
9013 Py_END_ALLOW_THREADS
9014 Py_DECREF(opath);
9015 if (res < 0)
9016 return posix_error();
9017 Py_RETURN_NONE;
9018}
9019#endif
9020
9021#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9022PyDoc_STRVAR(posix_mknodat__doc__,
9023"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9024Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9025If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9026is interpreted relative to the current working directory.");
9027
9028static PyObject *
9029posix_mknodat(PyObject *self, PyObject *args)
9030{
9031 PyObject *opath;
9032 char *filename;
9033 int mode = 0600;
9034 int device = 0;
9035 int res, dirfd;
9036 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9037 PyUnicode_FSConverter, &opath, &mode, &device))
9038 return NULL;
9039 filename = PyBytes_AS_STRING(opath);
9040 Py_BEGIN_ALLOW_THREADS
9041 res = mknodat(dirfd, filename, mode, device);
9042 Py_END_ALLOW_THREADS
9043 Py_DECREF(opath);
9044 if (res < 0)
9045 return posix_error();
9046 Py_RETURN_NONE;
9047}
9048#endif
9049
9050#ifdef HAVE_OPENAT
9051PyDoc_STRVAR(posix_openat__doc__,
9052"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9053Like open() but if path is relative, it is taken as relative to dirfd.\n\
9054If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9055is interpreted relative to the current working directory.");
9056
9057static PyObject *
9058posix_openat(PyObject *self, PyObject *args)
9059{
9060 PyObject *ofile;
9061 char *file;
9062 int flag, dirfd, fd;
9063 int mode = 0777;
9064
9065 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9066 &dirfd, PyUnicode_FSConverter, &ofile,
9067 &flag, &mode))
9068 return NULL;
9069 file = PyBytes_AsString(ofile);
9070 Py_BEGIN_ALLOW_THREADS
9071 fd = openat(dirfd, file, flag, mode);
9072 Py_END_ALLOW_THREADS
9073 Py_DECREF(ofile);
9074 if (fd < 0)
9075 return posix_error();
9076 return PyLong_FromLong((long)fd);
9077}
9078#endif
9079
9080#ifdef HAVE_READLINKAT
9081PyDoc_STRVAR(posix_readlinkat__doc__,
9082"readlinkat(dirfd, path) -> path\n\n\
9083Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9084If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9085is interpreted relative to the current working directory.");
9086
9087static PyObject *
9088posix_readlinkat(PyObject *self, PyObject *args)
9089{
9090 PyObject *v, *opath;
9091 char buf[MAXPATHLEN];
9092 char *path;
9093 int n, dirfd;
9094 int arg_is_unicode = 0;
9095
9096 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9097 &dirfd, PyUnicode_FSConverter, &opath))
9098 return NULL;
9099 path = PyBytes_AsString(opath);
9100 v = PySequence_GetItem(args, 1);
9101 if (v == NULL) {
9102 Py_DECREF(opath);
9103 return NULL;
9104 }
9105
9106 if (PyUnicode_Check(v)) {
9107 arg_is_unicode = 1;
9108 }
9109 Py_DECREF(v);
9110
9111 Py_BEGIN_ALLOW_THREADS
9112 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9113 Py_END_ALLOW_THREADS
9114 Py_DECREF(opath);
9115 if (n < 0)
9116 return posix_error();
9117
9118 if (arg_is_unicode)
9119 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9120 else
9121 return PyBytes_FromStringAndSize(buf, n);
9122}
9123#endif /* HAVE_READLINKAT */
9124
9125#ifdef HAVE_RENAMEAT
9126PyDoc_STRVAR(posix_renameat__doc__,
9127"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9128Like rename() but if oldpath is relative, it is taken as relative to\n\
9129olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9130If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9131oldpath is interpreted relative to the current working directory. This\n\
9132also applies for newpath.");
9133
9134static PyObject *
9135posix_renameat(PyObject *self, PyObject *args)
9136{
9137 int res;
9138 PyObject *opathold, *opathnew;
9139 char *opath, *npath;
9140 int oldfd, newfd;
9141
9142 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9143 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9144 return NULL;
9145 opath = PyBytes_AsString(opathold);
9146 npath = PyBytes_AsString(opathnew);
9147 Py_BEGIN_ALLOW_THREADS
9148 res = renameat(oldfd, opath, newfd, npath);
9149 Py_END_ALLOW_THREADS
9150 Py_DECREF(opathold);
9151 Py_DECREF(opathnew);
9152 if (res < 0)
9153 return posix_error();
9154 Py_RETURN_NONE;
9155}
9156#endif
9157
9158#if HAVE_SYMLINKAT
9159PyDoc_STRVAR(posix_symlinkat__doc__,
9160"symlinkat(src, dstfd, dst)\n\n\
9161Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9162If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9163is interpreted relative to the current working directory.");
9164
9165static PyObject *
9166posix_symlinkat(PyObject *self, PyObject *args)
9167{
9168 int res, dstfd;
9169 PyObject *osrc, *odst;
9170 char *src, *dst;
9171
9172 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9173 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9174 return NULL;
9175 src = PyBytes_AsString(osrc);
9176 dst = PyBytes_AsString(odst);
9177 Py_BEGIN_ALLOW_THREADS
9178 res = symlinkat(src, dstfd, dst);
9179 Py_END_ALLOW_THREADS
9180 Py_DECREF(osrc);
9181 Py_DECREF(odst);
9182 if (res < 0)
9183 return posix_error();
9184 Py_RETURN_NONE;
9185}
9186#endif /* HAVE_SYMLINKAT */
9187
9188#ifdef HAVE_UNLINKAT
9189PyDoc_STRVAR(posix_unlinkat__doc__,
9190"unlinkat(dirfd, path, flags=0)\n\n\
9191Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9192flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9193specified, unlinkat() behaves like rmdir().\n\
9194If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9195is interpreted relative to the current working directory.");
9196
9197static PyObject *
9198posix_unlinkat(PyObject *self, PyObject *args)
9199{
9200 int dirfd, res, flags = 0;
9201 PyObject *opath;
9202 char *path;
9203
9204 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9205 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9206 return NULL;
9207 path = PyBytes_AsString(opath);
9208 Py_BEGIN_ALLOW_THREADS
9209 res = unlinkat(dirfd, path, flags);
9210 Py_END_ALLOW_THREADS
9211 Py_DECREF(opath);
9212 if (res < 0)
9213 return posix_error();
9214 Py_RETURN_NONE;
9215}
9216#endif
9217
9218#ifdef HAVE_UTIMENSAT
9219PyDoc_STRVAR(posix_utimensat__doc__,
9220"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9221 (mtime_sec, mtime_nsec), flags)\n\
9222utimensat(dirfd, path, None, None, flags)\n\n\
9223Updates the timestamps of a file with nanosecond precision. If path is\n\
9224relative, it is taken as relative to dirfd.\n\
9225The second form sets atime and mtime to the current time.\n\
9226flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9227If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9228is interpreted relative to the current working directory.\n\
9229If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9230current time.\n\
9231If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9232
9233static PyObject *
9234posix_utimensat(PyObject *self, PyObject *args)
9235{
9236 PyObject *opath;
9237 char *path;
9238 int res, dirfd, flags = 0;
9239 PyObject *atime, *mtime;
9240
9241 struct timespec buf[2];
9242
9243 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9244 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9245 return NULL;
9246 path = PyBytes_AsString(opath);
9247 if (atime == Py_None && mtime == Py_None) {
9248 /* optional time values not given */
9249 Py_BEGIN_ALLOW_THREADS
9250 res = utimensat(dirfd, path, NULL, flags);
9251 Py_END_ALLOW_THREADS
9252 }
9253 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9254 PyErr_SetString(PyExc_TypeError,
9255 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9256 Py_DECREF(opath);
9257 return NULL;
9258 }
9259 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9260 PyErr_SetString(PyExc_TypeError,
9261 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9262 Py_DECREF(opath);
9263 return NULL;
9264 }
9265 else {
9266 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9267 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9268 Py_DECREF(opath);
9269 return NULL;
9270 }
9271 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9272 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9273 Py_DECREF(opath);
9274 return NULL;
9275 }
9276 Py_BEGIN_ALLOW_THREADS
9277 res = utimensat(dirfd, path, buf, flags);
9278 Py_END_ALLOW_THREADS
9279 }
9280 Py_DECREF(opath);
9281 if (res < 0) {
9282 return posix_error();
9283 }
9284 Py_RETURN_NONE;
9285}
9286#endif
9287
9288#ifdef HAVE_MKFIFOAT
9289PyDoc_STRVAR(posix_mkfifoat__doc__,
9290"mkfifoat(dirfd, path, mode=0o666)\n\n\
9291Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9292If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9293is interpreted relative to the current working directory.");
9294
9295static PyObject *
9296posix_mkfifoat(PyObject *self, PyObject *args)
9297{
9298 PyObject *opath;
9299 char *filename;
9300 int mode = 0666;
9301 int res, dirfd;
9302 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9303 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9304 return NULL;
9305 filename = PyBytes_AS_STRING(opath);
9306 Py_BEGIN_ALLOW_THREADS
9307 res = mkfifoat(dirfd, filename, mode);
9308 Py_END_ALLOW_THREADS
9309 Py_DECREF(opath);
9310 if (res < 0)
9311 return posix_error();
9312 Py_RETURN_NONE;
9313}
9314#endif
9315
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009316static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009318#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009319 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009320#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009321 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009322#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009323 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009324#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009325 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009326#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009327 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009328#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009329#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009330 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009331#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009332#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009333 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009334#endif /* HAVE_LCHMOD */
9335#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009336 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009337#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009338#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009339 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009340#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009341#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009342 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009343#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009344#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009345 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009346#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009347#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009348 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009349#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009350#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009351 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9352 METH_NOARGS, posix_getcwd__doc__},
9353 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9354 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009355#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009356#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009357 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009358#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009359 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009360#ifdef HAVE_FDOPENDIR
9361 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9362#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009363 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
9364 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009365#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00009366 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009367#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009368#ifdef HAVE_GETPRIORITY
9369 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
9370#endif /* HAVE_GETPRIORITY */
9371#ifdef HAVE_SETPRIORITY
9372 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
9373#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009374#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009375 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009376#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00009377#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009378 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00009379#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009380 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
9381 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
9382 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00009384#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00009385 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009386#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00009387#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009388 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00009389 win_symlink__doc__},
9390#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009391#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00009392 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009393#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009394 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009395#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009396 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009397#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00009398 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
9399 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
9400 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009401#ifdef HAVE_FUTIMES
9402 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
9403#endif
9404#ifdef HAVE_LUTIMES
9405 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
9406#endif
9407#ifdef HAVE_FUTIMENS
9408 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
9409#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009410#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00009411 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009412#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00009413 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009414#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00009415 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
9416 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009417#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009418#ifdef HAVE_FEXECVE
9419 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
9420#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00009421#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00009422 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
9423 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009424#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00009425 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
9426 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00009427#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00009428#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009429#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00009430 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00009431#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009432#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00009433 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009434#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009435#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00009436 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00009437#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00009438#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009439 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00009440#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009441#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009442 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009443#endif /* HAVE_GETEGID */
9444#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009445 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009446#endif /* HAVE_GETEUID */
9447#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009448 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009449#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02009450#ifdef HAVE_GETGROUPLIST
9451 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
9452#endif
Fred Drakec9680921999-12-13 16:37:25 +00009453#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009454 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009455#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009456 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009457#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009458 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009459#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009460#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009461 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009462#endif /* HAVE_GETPPID */
9463#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009464 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009465#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00009466#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009467 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00009468#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00009469#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00009470 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009471#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009472#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00009473 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00009474#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00009475#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009476 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00009477#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00009478#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009479 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
9480 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00009481 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00009482#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009483#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009484 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009485#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009486#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009487 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009488#endif /* HAVE_SETEUID */
9489#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009490 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009491#endif /* HAVE_SETEGID */
9492#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009493 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009494#endif /* HAVE_SETREUID */
9495#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009496 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00009497#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009498#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009499 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009500#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009501#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009502 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00009503#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00009504#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00009505 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00009506#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00009507#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009508 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00009509#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009510#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009511 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009512#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00009513#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009514 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00009515#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009516#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00009517 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009518#endif /* HAVE_WAIT3 */
9519#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00009520 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009521#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009522#if defined(HAVE_WAITID) && !defined(__APPLE__)
9523 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
9524#endif
Tim Petersab034fa2002-02-01 11:27:43 +00009525#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009526 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009527#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009528#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009529 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00009530#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009531#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009533#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009534#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009535 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009536#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009537#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009539#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009540#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00009541 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009542#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00009543 {"open", posix_open, METH_VARARGS, posix_open__doc__},
9544 {"close", posix_close, METH_VARARGS, posix_close__doc__},
9545 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
9546 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
9547 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
9548 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009549#ifdef HAVE_LOCKF
9550 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
9551#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009552 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
9553 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009554#ifdef HAVE_READV
9555 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
9556#endif
9557#ifdef HAVE_PREAD
9558 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
9559#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009560 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +02009561#ifdef HAVE_WRITEV
9562 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
9563#endif
9564#ifdef HAVE_PWRITE
9565 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
9566#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009567#ifdef HAVE_SENDFILE
9568 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
9569 posix_sendfile__doc__},
9570#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009571 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
9572 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009573#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009574 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009575#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009576#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +02009577 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009578#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009579#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00009580 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009581#endif
Neal Norwitz11690112002-07-30 01:08:28 +00009582#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00009583 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00009584#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009585#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00009586 {"major", posix_major, METH_VARARGS, posix_major__doc__},
9587 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
9588 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00009589#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009590#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00009591 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009592#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009593#ifdef HAVE_TRUNCATE
9594 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
9595#endif
9596#ifdef HAVE_POSIX_FALLOCATE
9597 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
9598#endif
9599#ifdef HAVE_POSIX_FADVISE
9600 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
9601#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009602#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009603 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009604#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009605#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00009606 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00009607#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009608 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009609#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00009610 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00009611#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009612#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009613 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009614#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +02009615#ifdef HAVE_SYNC
9616 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
9617#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00009618#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009619 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00009620#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00009621#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009622#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00009623 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00009624#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009625#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009626 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00009627#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00009628#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00009629 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009630#endif /* WIFSTOPPED */
9631#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00009632 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009633#endif /* WIFSIGNALED */
9634#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00009635 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009636#endif /* WIFEXITED */
9637#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00009638 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009639#endif /* WEXITSTATUS */
9640#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009641 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009642#endif /* WTERMSIG */
9643#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009644 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00009645#endif /* WSTOPSIG */
9646#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00009647#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009648 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009649#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00009650#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00009651 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00009652#endif
Fred Drakec9680921999-12-13 16:37:25 +00009653#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00009654 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009655#endif
9656#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009657 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009658#endif
9659#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009660 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009661#endif
9662#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009663 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00009664#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009665 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009666#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009667 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00009668 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00009669 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -05009670 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00009671#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009672#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00009673 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00009674#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009675 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009676 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009677 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009678 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00009679 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009680 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009681#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009682 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009683#endif
9684#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009685 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009686#endif
9687#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00009688 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009689#endif
9690#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00009691 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009692#endif
9693
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009694/* posix *at family of functions */
9695#ifdef HAVE_FACCESSAT
9696 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
9697#endif
9698#ifdef HAVE_FCHMODAT
9699 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
9700#endif /* HAVE_FCHMODAT */
9701#ifdef HAVE_FCHOWNAT
9702 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
9703#endif /* HAVE_FCHOWNAT */
9704#ifdef HAVE_FSTATAT
9705 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
9706#endif
9707#ifdef HAVE_FUTIMESAT
9708 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
9709#endif
9710#ifdef HAVE_LINKAT
9711 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
9712#endif /* HAVE_LINKAT */
9713#ifdef HAVE_MKDIRAT
9714 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
9715#endif
9716#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9717 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
9718#endif
9719#ifdef HAVE_OPENAT
9720 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
9721#endif
9722#ifdef HAVE_READLINKAT
9723 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
9724#endif /* HAVE_READLINKAT */
9725#ifdef HAVE_RENAMEAT
9726 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
9727#endif
9728#if HAVE_SYMLINKAT
9729 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
9730#endif /* HAVE_SYMLINKAT */
9731#ifdef HAVE_UNLINKAT
9732 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
9733#endif
9734#ifdef HAVE_UTIMENSAT
9735 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
9736#endif
9737#ifdef HAVE_MKFIFOAT
9738 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
9739#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009740 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009741};
9742
9743
Barry Warsaw4a342091996-12-19 23:50:02 +00009744static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009745ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00009746{
Victor Stinner8c62be82010-05-06 00:08:46 +00009747 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00009748}
9749
Guido van Rossumd48f2521997-12-05 22:19:34 +00009750#if defined(PYOS_OS2)
9751/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009752static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009753{
9754 APIRET rc;
9755 ULONG values[QSV_MAX+1];
9756 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00009757 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00009758
9759 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00009760 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009761 Py_END_ALLOW_THREADS
9762
9763 if (rc != NO_ERROR) {
9764 os2_error(rc);
9765 return -1;
9766 }
9767
Fred Drake4d1e64b2002-04-15 19:40:07 +00009768 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
9769 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
9770 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
9771 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
9772 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
9773 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
9774 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009775
9776 switch (values[QSV_VERSION_MINOR]) {
9777 case 0: ver = "2.00"; break;
9778 case 10: ver = "2.10"; break;
9779 case 11: ver = "2.11"; break;
9780 case 30: ver = "3.00"; break;
9781 case 40: ver = "4.00"; break;
9782 case 50: ver = "5.00"; break;
9783 default:
Tim Peters885d4572001-11-28 20:27:42 +00009784 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00009785 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00009786 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009787 ver = &tmp[0];
9788 }
9789
9790 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00009791 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00009792 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00009793
9794 /* Add Indicator of Which Drive was Used to Boot the System */
9795 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
9796 tmp[1] = ':';
9797 tmp[2] = '\0';
9798
Fred Drake4d1e64b2002-04-15 19:40:07 +00009799 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00009800}
9801#endif
9802
Brian Curtin52173d42010-12-02 18:29:18 +00009803#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00009804static int
Brian Curtin52173d42010-12-02 18:29:18 +00009805enable_symlink()
9806{
9807 HANDLE tok;
9808 TOKEN_PRIVILEGES tok_priv;
9809 LUID luid;
9810 int meth_idx = 0;
9811
9812 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009813 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009814
9815 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009816 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009817
9818 tok_priv.PrivilegeCount = 1;
9819 tok_priv.Privileges[0].Luid = luid;
9820 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
9821
9822 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
9823 sizeof(TOKEN_PRIVILEGES),
9824 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00009825 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00009826
Brian Curtin3b4499c2010-12-28 14:31:47 +00009827 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
9828 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00009829}
9830#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
9831
Barry Warsaw4a342091996-12-19 23:50:02 +00009832static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00009833all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00009834{
Guido van Rossum94f6f721999-01-06 18:42:14 +00009835#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009836 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009837#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009838#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009839 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009840#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009841#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009842 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009843#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009844#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00009845 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009846#endif
Fred Drakec9680921999-12-13 16:37:25 +00009847#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009848 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00009849#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009850#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009851 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009852#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009853#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00009854 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009855#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009856#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00009857 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009858#endif
Fred Drake106c1a02002-04-23 15:58:02 +00009859#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00009860 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00009861#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00009862#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009863 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009864#endif
9865#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00009866 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009867#endif
9868#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00009869 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009870#endif
9871#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00009872 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009873#endif
9874#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009875 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009876#endif
9877#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00009878 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009879#endif
9880#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009881 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009882#endif
9883#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009884 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009885#endif
9886#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009887 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009888#endif
9889#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00009890 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009891#endif
9892#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00009893 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009894#endif
9895#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00009896 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009897#endif
9898#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009899 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00009900#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00009901#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009902 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009903#endif
9904#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00009905 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00009906#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009907#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00009908 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009909#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00009910#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009911 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009912#endif
9913#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009914 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00009915#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009916#ifdef PRIO_PROCESS
9917 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
9918#endif
9919#ifdef PRIO_PGRP
9920 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
9921#endif
9922#ifdef PRIO_USER
9923 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
9924#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +02009925#ifdef O_CLOEXEC
9926 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
9927#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009928/* posix - constants for *at functions */
9929#ifdef AT_SYMLINK_NOFOLLOW
9930 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
9931#endif
9932#ifdef AT_EACCESS
9933 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
9934#endif
9935#ifdef AT_FDCWD
9936 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
9937#endif
9938#ifdef AT_REMOVEDIR
9939 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
9940#endif
9941#ifdef AT_SYMLINK_FOLLOW
9942 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
9943#endif
9944#ifdef UTIME_NOW
9945 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
9946#endif
9947#ifdef UTIME_OMIT
9948 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
9949#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009950
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009951
Tim Peters5aa91602002-01-30 05:46:57 +00009952/* MS Windows */
9953#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009954 /* Don't inherit in child processes. */
9955 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009956#endif
9957#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 /* Optimize for short life (keep in memory). */
9959 /* MS forgot to define this one with a non-underscore form too. */
9960 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009961#endif
9962#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00009963 /* Automatically delete when last handle is closed. */
9964 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009965#endif
9966#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00009967 /* Optimize for random access. */
9968 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009969#endif
9970#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009971 /* Optimize for sequential access. */
9972 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00009973#endif
9974
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009975/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009976#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009977 /* Send a SIGIO signal whenever input or output
9978 becomes available on file descriptor */
9979 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00009980#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009981#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009982 /* Direct disk access. */
9983 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009984#endif
9985#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00009986 /* Must be a directory. */
9987 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009988#endif
9989#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00009990 /* Do not follow links. */
9991 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00009992#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009993#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 /* Do not update the access time. */
9995 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00009996#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00009997
Victor Stinner8c62be82010-05-06 00:08:46 +00009998 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00009999#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010001#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010002#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010003 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010004#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010005#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010006 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010007#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010008#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010009 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010010#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010011#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000010012 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010013#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010014#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000010015 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010016#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010017#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010018 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010019#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010020#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000010021 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010022#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010023#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010024 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010025#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010026#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010027 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010028#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010029#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010030 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010031#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010032#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010033 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010034#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010035#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000010036 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010037#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010038#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000010039 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010040#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010041#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010042 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010043#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010044#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010045 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010046#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010047#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000010048 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010049#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010050
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010051 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010052#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010053 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010054#endif /* ST_RDONLY */
10055#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010056 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010057#endif /* ST_NOSUID */
10058
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010059 /* FreeBSD sendfile() constants */
10060#ifdef SF_NODISKIO
10061 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
10062#endif
10063#ifdef SF_MNOWAIT
10064 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
10065#endif
10066#ifdef SF_SYNC
10067 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
10068#endif
10069
Ross Lagerwall7807c352011-03-17 20:20:30 +020010070 /* constants for posix_fadvise */
10071#ifdef POSIX_FADV_NORMAL
10072 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
10073#endif
10074#ifdef POSIX_FADV_SEQUENTIAL
10075 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
10076#endif
10077#ifdef POSIX_FADV_RANDOM
10078 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
10079#endif
10080#ifdef POSIX_FADV_NOREUSE
10081 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
10082#endif
10083#ifdef POSIX_FADV_WILLNEED
10084 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
10085#endif
10086#ifdef POSIX_FADV_DONTNEED
10087 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
10088#endif
10089
10090 /* constants for waitid */
10091#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
10092 if (ins(d, "P_PID", (long)P_PID)) return -1;
10093 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
10094 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
10095#endif
10096#ifdef WEXITED
10097 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
10098#endif
10099#ifdef WNOWAIT
10100 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
10101#endif
10102#ifdef WSTOPPED
10103 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
10104#endif
10105#ifdef CLD_EXITED
10106 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
10107#endif
10108#ifdef CLD_DUMPED
10109 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
10110#endif
10111#ifdef CLD_TRAPPED
10112 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
10113#endif
10114#ifdef CLD_CONTINUED
10115 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
10116#endif
10117
10118 /* constants for lockf */
10119#ifdef F_LOCK
10120 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
10121#endif
10122#ifdef F_TLOCK
10123 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
10124#endif
10125#ifdef F_ULOCK
10126 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
10127#endif
10128#ifdef F_TEST
10129 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10130#endif
10131
10132 /* constants for futimens */
10133#ifdef UTIME_NOW
10134 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10135#endif
10136#ifdef UTIME_OMIT
10137 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10138#endif
10139
Guido van Rossum246bc171999-02-01 23:54:31 +000010140#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010141#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010142 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10143 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10144 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10145 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10146 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10147 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10148 if (ins(d, "P_PM", (long)P_PM)) return -1;
10149 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10150 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10151 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10152 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10153 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10154 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10155 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10156 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10157 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10158 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10159 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10160 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10161 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010162#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010163 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10164 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10165 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10166 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10167 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010168#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010169#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010170
Guido van Rossumd48f2521997-12-05 22:19:34 +000010171#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010172 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010173#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010174 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010175}
10176
10177
Tim Peters5aa91602002-01-30 05:46:57 +000010178#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010179#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010180#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010181
10182#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010183#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010184#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010185
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010186#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010187#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010188#define MODNAME "posix"
10189#endif
10190
Martin v. Löwis1a214512008-06-11 05:26:20 +000010191static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010192 PyModuleDef_HEAD_INIT,
10193 MODNAME,
10194 posix__doc__,
10195 -1,
10196 posix_methods,
10197 NULL,
10198 NULL,
10199 NULL,
10200 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010201};
10202
10203
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010204PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010205INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010206{
Victor Stinner8c62be82010-05-06 00:08:46 +000010207 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010208
Brian Curtin52173d42010-12-02 18:29:18 +000010209#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010210 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010211#endif
10212
Victor Stinner8c62be82010-05-06 00:08:46 +000010213 m = PyModule_Create(&posixmodule);
10214 if (m == NULL)
10215 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010216
Victor Stinner8c62be82010-05-06 00:08:46 +000010217 /* Initialize environ dictionary */
10218 v = convertenviron();
10219 Py_XINCREF(v);
10220 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10221 return NULL;
10222 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010223
Victor Stinner8c62be82010-05-06 00:08:46 +000010224 if (all_ins(m))
10225 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010226
Victor Stinner8c62be82010-05-06 00:08:46 +000010227 if (setup_confname_tables(m))
10228 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010229
Victor Stinner8c62be82010-05-06 00:08:46 +000010230 Py_INCREF(PyExc_OSError);
10231 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010232
Guido van Rossumb3d39562000-01-31 18:41:26 +000010233#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010234 if (posix_putenv_garbage == NULL)
10235 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010236#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010237
Victor Stinner8c62be82010-05-06 00:08:46 +000010238 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010239#if defined(HAVE_WAITID) && !defined(__APPLE__)
10240 waitid_result_desc.name = MODNAME ".waitid_result";
10241 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10242#endif
10243
Victor Stinner8c62be82010-05-06 00:08:46 +000010244 stat_result_desc.name = MODNAME ".stat_result";
10245 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10246 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10247 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10248 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10249 structseq_new = StatResultType.tp_new;
10250 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010251
Victor Stinner8c62be82010-05-06 00:08:46 +000010252 statvfs_result_desc.name = MODNAME ".statvfs_result";
10253 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010254#ifdef NEED_TICKS_PER_SECOND
10255# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010256 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010257# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010258 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010259# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010260 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010261# endif
10262#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010263 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010264#if defined(HAVE_WAITID) && !defined(__APPLE__)
10265 Py_INCREF((PyObject*) &WaitidResultType);
10266 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10267#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010268 Py_INCREF((PyObject*) &StatResultType);
10269 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10270 Py_INCREF((PyObject*) &StatVFSResultType);
10271 PyModule_AddObject(m, "statvfs_result",
10272 (PyObject*) &StatVFSResultType);
10273 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010274
10275#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010276 /*
10277 * Step 2 of weak-linking support on Mac OS X.
10278 *
10279 * The code below removes functions that are not available on the
10280 * currently active platform.
10281 *
10282 * This block allow one to use a python binary that was build on
10283 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10284 * OSX 10.4.
10285 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010286#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010287 if (fstatvfs == NULL) {
10288 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10289 return NULL;
10290 }
10291 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010292#endif /* HAVE_FSTATVFS */
10293
10294#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010295 if (statvfs == NULL) {
10296 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10297 return NULL;
10298 }
10299 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010300#endif /* HAVE_STATVFS */
10301
10302# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010303 if (lchown == NULL) {
10304 if (PyObject_DelAttrString(m, "lchown") == -1) {
10305 return NULL;
10306 }
10307 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010308#endif /* HAVE_LCHOWN */
10309
10310
10311#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000010312 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010313
Guido van Rossumb6775db1994-08-01 11:34:53 +000010314}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010315
10316#ifdef __cplusplus
10317}
10318#endif