blob: ef8f82eb4c4edb7849b6216f661d2d13d66dc08e [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
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500108#ifdef HAVE_SCHED_H
109#include <sched.h>
110#endif
111
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000112#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
113#ifdef HAVE_SYS_SOCKET_H
114#include <sys/socket.h>
115#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000116#endif
117
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000118/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000119/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000120#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000121#include <process.h>
122#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000123#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000124#define HAVE_GETCWD 1
125#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000126#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000127#if defined(__OS2__)
128#define HAVE_EXECV 1
129#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000130#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000131#include <process.h>
132#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000133#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000134#define HAVE_EXECV 1
135#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136#define HAVE_OPENDIR 1
137#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000138#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000139#define HAVE_WAIT 1
140#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000141#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000142#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000143#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000144#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000145#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#define HAVE_EXECV 1
147#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#define HAVE_SYSTEM 1
149#define HAVE_CWAIT 1
150#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000151#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000152#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000153#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
154/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000156/* Unix functions that the configure script doesn't check for */
157#define HAVE_EXECV 1
158#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000159#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000160#define HAVE_FORK1 1
161#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162#define HAVE_GETCWD 1
163#define HAVE_GETEGID 1
164#define HAVE_GETEUID 1
165#define HAVE_GETGID 1
166#define HAVE_GETPPID 1
167#define HAVE_GETUID 1
168#define HAVE_KILL 1
169#define HAVE_OPENDIR 1
170#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000171#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000173#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000174#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif /* _MSC_VER */
176#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000177#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000178#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000179
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000180#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000181
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000182#if defined(__sgi)&&_COMPILER_VERSION>=700
183/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
184 (default) */
185extern char *ctermid_r(char *);
186#endif
187
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000188#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000189#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000190extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000191#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000192#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000193extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000194#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000196#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000197#endif
198#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000199extern int chdir(char *);
200extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000201#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000202extern int chdir(const char *);
203extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000204#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000205#ifdef __BORLANDC__
206extern int chmod(const char *, int);
207#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000208extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000209#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000210/*#ifdef HAVE_FCHMOD
211extern int fchmod(int, mode_t);
212#endif*/
213/*#ifdef HAVE_LCHMOD
214extern int lchmod(const char *, mode_t);
215#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000216extern int chown(const char *, uid_t, gid_t);
217extern char *getcwd(char *, int);
218extern char *strerror(int);
219extern int link(const char *, const char *);
220extern int rename(const char *, const char *);
221extern int stat(const char *, struct stat *);
222extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000224extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000225#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000227extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000230
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000231#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233#ifdef HAVE_UTIME_H
234#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000235#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000236
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000237#ifdef HAVE_SYS_UTIME_H
238#include <sys/utime.h>
239#define HAVE_UTIME_H /* pretend we do for the rest of this file */
240#endif /* HAVE_SYS_UTIME_H */
241
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#ifdef HAVE_SYS_TIMES_H
243#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000244#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245
246#ifdef HAVE_SYS_PARAM_H
247#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000248#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249
250#ifdef HAVE_SYS_UTSNAME_H
251#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000252#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000254#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000256#define NAMLEN(dirent) strlen((dirent)->d_name)
257#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000258#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000259#include <direct.h>
260#define NAMLEN(dirent) strlen((dirent)->d_name)
261#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000263#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000264#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000265#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000267#endif
268#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000270#endif
271#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#endif
274#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279#endif
280#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282#endif
283#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000286#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000287#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000288#endif
289#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000290#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000291#endif
292#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000293#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000294#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000295#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000296#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000298#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000299#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000300#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
301#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000302static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000303#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000304#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305
Guido van Rossumd48f2521997-12-05 22:19:34 +0000306#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000307#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000308#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309
Tim Petersbc2e10e2002-03-03 23:17:02 +0000310#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000311#if defined(PATH_MAX) && PATH_MAX > 1024
312#define MAXPATHLEN PATH_MAX
313#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000314#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000315#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#endif /* MAXPATHLEN */
317
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000318#ifdef UNION_WAIT
319/* Emulate some macros on systems that have a union instead of macros */
320
321#ifndef WIFEXITED
322#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
323#endif
324
325#ifndef WEXITSTATUS
326#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
327#endif
328
329#ifndef WTERMSIG
330#define WTERMSIG(u_wait) ((u_wait).w_termsig)
331#endif
332
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333#define WAIT_TYPE union wait
334#define WAIT_STATUS_INT(s) (s.w_status)
335
336#else /* !UNION_WAIT */
337#define WAIT_TYPE int
338#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000339#endif /* UNION_WAIT */
340
Greg Wardb48bc172000-03-01 21:51:56 +0000341/* Don't use the "_r" form if we don't need it (also, won't have a
342 prototype for it, at least on Solaris -- maybe others as well?). */
343#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
344#define USE_CTERMID_R
345#endif
346
Fred Drake699f3522000-06-29 21:12:41 +0000347/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000348#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000349#undef FSTAT
350#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000351#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000352# define STAT win32_stat
353# define FSTAT win32_fstat
354# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000355#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000356# define STAT stat
357# define FSTAT fstat
358# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000359#endif
360
Tim Peters11b23062003-04-23 02:39:17 +0000361#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000362#include <sys/mkdev.h>
363#else
364#if defined(MAJOR_IN_SYSMACROS)
365#include <sys/sysmacros.h>
366#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000367#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
368#include <sys/mkdev.h>
369#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000370#endif
Fred Drake699f3522000-06-29 21:12:41 +0000371
Georg Brandla391b112011-02-25 15:23:18 +0000372static int
373_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000374{
375#if !defined(HAVE_LARGEFILE_SUPPORT)
376 *((off_t*)addr) = PyLong_AsLong(arg);
377#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000378 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000379#endif
380 if (PyErr_Occurred())
381 return 0;
382 return 1;
383}
384
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000385#if defined _MSC_VER && _MSC_VER >= 1400
386/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
387 * valid and throw an assertion if it isn't.
388 * Normally, an invalid fd is likely to be a C program error and therefore
389 * an assertion can be useful, but it does contradict the POSIX standard
390 * which for write(2) states:
391 * "Otherwise, -1 shall be returned and errno set to indicate the error."
392 * "[EBADF] The fildes argument is not a valid file descriptor open for
393 * writing."
394 * Furthermore, python allows the user to enter any old integer
395 * as a fd and should merely raise a python exception on error.
396 * The Microsoft CRT doesn't provide an official way to check for the
397 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000398 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000399 * internal structures involved.
400 * The structures below must be updated for each version of visual studio
401 * according to the file internal.h in the CRT source, until MS comes
402 * up with a less hacky way to do this.
403 * (all of this is to avoid globally modifying the CRT behaviour using
404 * _set_invalid_parameter_handler() and _CrtSetReportMode())
405 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000406/* The actual size of the structure is determined at runtime.
407 * Only the first items must be present.
408 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000409typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000410 intptr_t osfhnd;
411 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000412} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000413
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000414extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000415#define IOINFO_L2E 5
416#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
417#define IOINFO_ARRAYS 64
418#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
419#define FOPEN 0x01
420#define _NO_CONSOLE_FILENO (intptr_t)-2
421
422/* This function emulates what the windows CRT does to validate file handles */
423int
424_PyVerify_fd(int fd)
425{
Victor Stinner8c62be82010-05-06 00:08:46 +0000426 const int i1 = fd >> IOINFO_L2E;
427 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000428
Antoine Pitrou22e41552010-08-15 18:07:50 +0000429 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000430
Victor Stinner8c62be82010-05-06 00:08:46 +0000431 /* Determine the actual size of the ioinfo structure,
432 * as used by the CRT loaded in memory
433 */
434 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
435 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
436 }
437 if (sizeof_ioinfo == 0) {
438 /* This should not happen... */
439 goto fail;
440 }
441
442 /* See that it isn't a special CLEAR fileno */
443 if (fd != _NO_CONSOLE_FILENO) {
444 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
445 * we check pointer validity and other info
446 */
447 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
448 /* finally, check that the file is open */
449 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
450 if (info->osfile & FOPEN) {
451 return 1;
452 }
453 }
454 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000455 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000456 errno = EBADF;
457 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000458}
459
460/* the special case of checking dup2. The target fd must be in a sensible range */
461static int
462_PyVerify_fd_dup2(int fd1, int fd2)
463{
Victor Stinner8c62be82010-05-06 00:08:46 +0000464 if (!_PyVerify_fd(fd1))
465 return 0;
466 if (fd2 == _NO_CONSOLE_FILENO)
467 return 0;
468 if ((unsigned)fd2 < _NHANDLE_)
469 return 1;
470 else
471 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000472}
473#else
474/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
475#define _PyVerify_fd_dup2(A, B) (1)
476#endif
477
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000478#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000479/* The following structure was copied from
480 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
481 include doesn't seem to be present in the Windows SDK (at least as included
482 with Visual Studio Express). */
483typedef struct _REPARSE_DATA_BUFFER {
484 ULONG ReparseTag;
485 USHORT ReparseDataLength;
486 USHORT Reserved;
487 union {
488 struct {
489 USHORT SubstituteNameOffset;
490 USHORT SubstituteNameLength;
491 USHORT PrintNameOffset;
492 USHORT PrintNameLength;
493 ULONG Flags;
494 WCHAR PathBuffer[1];
495 } SymbolicLinkReparseBuffer;
496
497 struct {
498 USHORT SubstituteNameOffset;
499 USHORT SubstituteNameLength;
500 USHORT PrintNameOffset;
501 USHORT PrintNameLength;
502 WCHAR PathBuffer[1];
503 } MountPointReparseBuffer;
504
505 struct {
506 UCHAR DataBuffer[1];
507 } GenericReparseBuffer;
508 };
509} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
510
511#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
512 GenericReparseBuffer)
513#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
514
515static int
Brian Curtind25aef52011-06-13 15:16:04 -0500516win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000517{
518 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
519 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
520 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000521
522 if (0 == DeviceIoControl(
523 reparse_point_handle,
524 FSCTL_GET_REPARSE_POINT,
525 NULL, 0, /* in buffer */
526 target_buffer, sizeof(target_buffer),
527 &n_bytes_returned,
528 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -0500529 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000530
531 if (reparse_tag)
532 *reparse_tag = rdb->ReparseTag;
533
Brian Curtind25aef52011-06-13 15:16:04 -0500534 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000535}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000536#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000537
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000539#ifdef WITH_NEXT_FRAMEWORK
540/* On Darwin/MacOSX a shared library or framework has no access to
541** environ directly, we must obtain it with _NSGetEnviron().
542*/
543#include <crt_externs.h>
544static char **environ;
545#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000547#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548
Barry Warsaw53699e91996-12-10 23:23:01 +0000549static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000550convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551{
Victor Stinner8c62be82010-05-06 00:08:46 +0000552 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000553#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000555#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000557#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000558#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000559 APIRET rc;
560 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
561#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000562
Victor Stinner8c62be82010-05-06 00:08:46 +0000563 d = PyDict_New();
564 if (d == NULL)
565 return NULL;
566#ifdef WITH_NEXT_FRAMEWORK
567 if (environ == NULL)
568 environ = *_NSGetEnviron();
569#endif
570#ifdef MS_WINDOWS
571 /* _wenviron must be initialized in this way if the program is started
572 through main() instead of wmain(). */
573 _wgetenv(L"");
574 if (_wenviron == NULL)
575 return d;
576 /* This part ignores errors */
577 for (e = _wenviron; *e != NULL; e++) {
578 PyObject *k;
579 PyObject *v;
580 wchar_t *p = wcschr(*e, L'=');
581 if (p == NULL)
582 continue;
583 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
584 if (k == NULL) {
585 PyErr_Clear();
586 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000587 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000588 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
589 if (v == NULL) {
590 PyErr_Clear();
591 Py_DECREF(k);
592 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000593 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000594 if (PyDict_GetItem(d, k) == NULL) {
595 if (PyDict_SetItem(d, k, v) != 0)
596 PyErr_Clear();
597 }
598 Py_DECREF(k);
599 Py_DECREF(v);
600 }
601#else
602 if (environ == NULL)
603 return d;
604 /* This part ignores errors */
605 for (e = environ; *e != NULL; e++) {
606 PyObject *k;
607 PyObject *v;
608 char *p = strchr(*e, '=');
609 if (p == NULL)
610 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000611 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000612 if (k == NULL) {
613 PyErr_Clear();
614 continue;
615 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000616 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000617 if (v == NULL) {
618 PyErr_Clear();
619 Py_DECREF(k);
620 continue;
621 }
622 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);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000628 }
629#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000630#if defined(PYOS_OS2)
631 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
632 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
633 PyObject *v = PyBytes_FromString(buffer);
634 PyDict_SetItemString(d, "BEGINLIBPATH", v);
635 Py_DECREF(v);
636 }
637 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
638 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
639 PyObject *v = PyBytes_FromString(buffer);
640 PyDict_SetItemString(d, "ENDLIBPATH", v);
641 Py_DECREF(v);
642 }
643#endif
644 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000645}
646
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647/* Set a POSIX-specific error from errno, and return NULL */
648
Barry Warsawd58d7641998-07-23 16:14:40 +0000649static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000650posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000651{
Victor Stinner8c62be82010-05-06 00:08:46 +0000652 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000653}
Barry Warsawd58d7641998-07-23 16:14:40 +0000654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000655posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000656{
Victor Stinner8c62be82010-05-06 00:08:46 +0000657 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000658}
659
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000660
Mark Hammondef8b6542001-05-13 08:04:26 +0000661static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000662posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000663{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000664 PyObject *name_str, *rc;
665 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
666 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000667 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000668 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
669 name_str);
670 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000671 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000672}
673
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000674#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000675static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000676win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000677{
Victor Stinner8c62be82010-05-06 00:08:46 +0000678 /* XXX We should pass the function name along in the future.
679 (winreg.c also wants to pass the function name.)
680 This would however require an additional param to the
681 Windows error object, which is non-trivial.
682 */
683 errno = GetLastError();
684 if (filename)
685 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
686 else
687 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000688}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000689
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000690static PyObject *
691win32_error_unicode(char* function, Py_UNICODE* filename)
692{
Victor Stinner8c62be82010-05-06 00:08:46 +0000693 /* XXX - see win32_error for comments on 'function' */
694 errno = GetLastError();
695 if (filename)
696 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
697 else
698 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000699}
700
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000702convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703{
Victor Stinner8c62be82010-05-06 00:08:46 +0000704 if (PyUnicode_CheckExact(*param))
705 Py_INCREF(*param);
706 else if (PyUnicode_Check(*param))
707 /* For a Unicode subtype that's not a Unicode object,
708 return a true Unicode object with the same data. */
709 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
710 PyUnicode_GET_SIZE(*param));
711 else
712 *param = PyUnicode_FromEncodedObject(*param,
713 Py_FileSystemDefaultEncoding,
714 "strict");
715 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000716}
717
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000718#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719
Guido van Rossumd48f2521997-12-05 22:19:34 +0000720#if defined(PYOS_OS2)
721/**********************************************************************
722 * Helper Function to Trim and Format OS/2 Messages
723 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000724static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000725os2_formatmsg(char *msgbuf, int msglen, char *reason)
726{
727 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
728
729 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
730 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
731
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000732 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000733 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
734 }
735
736 /* Add Optional Reason Text */
737 if (reason) {
738 strcat(msgbuf, " : ");
739 strcat(msgbuf, reason);
740 }
741}
742
743/**********************************************************************
744 * Decode an OS/2 Operating System Error Code
745 *
746 * A convenience function to lookup an OS/2 error code and return a
747 * text message we can use to raise a Python exception.
748 *
749 * Notes:
750 * The messages for errors returned from the OS/2 kernel reside in
751 * the file OSO001.MSG in the \OS2 directory hierarchy.
752 *
753 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000754static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000755os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
756{
757 APIRET rc;
758 ULONG msglen;
759
760 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
761 Py_BEGIN_ALLOW_THREADS
762 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
763 errorcode, "oso001.msg", &msglen);
764 Py_END_ALLOW_THREADS
765
766 if (rc == NO_ERROR)
767 os2_formatmsg(msgbuf, msglen, reason);
768 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000769 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000770 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000771
772 return msgbuf;
773}
774
775/* Set an OS/2-specific error and return NULL. OS/2 kernel
776 errors are not in a global variable e.g. 'errno' nor are
777 they congruent with posix error numbers. */
778
Victor Stinner8c62be82010-05-06 00:08:46 +0000779static PyObject *
780os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000781{
782 char text[1024];
783 PyObject *v;
784
785 os2_strerror(text, sizeof(text), code, "");
786
787 v = Py_BuildValue("(is)", code, text);
788 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000789 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000790 Py_DECREF(v);
791 }
792 return NULL; /* Signal to Python that an Exception is Pending */
793}
794
795#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796
797/* POSIX generic methods */
798
Barry Warsaw53699e91996-12-10 23:23:01 +0000799static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000800posix_fildes(PyObject *fdobj, int (*func)(int))
801{
Victor Stinner8c62be82010-05-06 00:08:46 +0000802 int fd;
803 int res;
804 fd = PyObject_AsFileDescriptor(fdobj);
805 if (fd < 0)
806 return NULL;
807 if (!_PyVerify_fd(fd))
808 return posix_error();
809 Py_BEGIN_ALLOW_THREADS
810 res = (*func)(fd);
811 Py_END_ALLOW_THREADS
812 if (res < 0)
813 return posix_error();
814 Py_INCREF(Py_None);
815 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000816}
Guido van Rossum21142a01999-01-08 21:05:37 +0000817
818static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000820{
Victor Stinner8c62be82010-05-06 00:08:46 +0000821 PyObject *opath1 = NULL;
822 char *path1;
823 int res;
824 if (!PyArg_ParseTuple(args, format,
825 PyUnicode_FSConverter, &opath1))
826 return NULL;
827 path1 = PyBytes_AsString(opath1);
828 Py_BEGIN_ALLOW_THREADS
829 res = (*func)(path1);
830 Py_END_ALLOW_THREADS
831 if (res < 0)
832 return posix_error_with_allocated_filename(opath1);
833 Py_DECREF(opath1);
834 Py_INCREF(Py_None);
835 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836}
837
Barry Warsaw53699e91996-12-10 23:23:01 +0000838static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000839posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000840 char *format,
841 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000842{
Victor Stinner8c62be82010-05-06 00:08:46 +0000843 PyObject *opath1 = NULL, *opath2 = NULL;
844 char *path1, *path2;
845 int res;
846 if (!PyArg_ParseTuple(args, format,
847 PyUnicode_FSConverter, &opath1,
848 PyUnicode_FSConverter, &opath2)) {
849 return NULL;
850 }
851 path1 = PyBytes_AsString(opath1);
852 path2 = PyBytes_AsString(opath2);
853 Py_BEGIN_ALLOW_THREADS
854 res = (*func)(path1, path2);
855 Py_END_ALLOW_THREADS
856 Py_DECREF(opath1);
857 Py_DECREF(opath2);
858 if (res != 0)
859 /* XXX how to report both path1 and path2??? */
860 return posix_error();
861 Py_INCREF(Py_None);
862 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863}
864
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000865#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000866static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000867win32_1str(PyObject* args, char* func,
868 char* format, BOOL (__stdcall *funcA)(LPCSTR),
869 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000870{
Victor Stinner8c62be82010-05-06 00:08:46 +0000871 PyObject *uni;
872 char *ansi;
873 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000874
Victor Stinner8c62be82010-05-06 00:08:46 +0000875 if (!PyArg_ParseTuple(args, wformat, &uni))
876 PyErr_Clear();
877 else {
878 Py_BEGIN_ALLOW_THREADS
879 result = funcW(PyUnicode_AsUnicode(uni));
880 Py_END_ALLOW_THREADS
881 if (!result)
882 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
883 Py_INCREF(Py_None);
884 return Py_None;
885 }
886 if (!PyArg_ParseTuple(args, format, &ansi))
887 return NULL;
888 Py_BEGIN_ALLOW_THREADS
889 result = funcA(ansi);
890 Py_END_ALLOW_THREADS
891 if (!result)
892 return win32_error(func, ansi);
893 Py_INCREF(Py_None);
894 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000895
896}
897
898/* This is a reimplementation of the C library's chdir function,
899 but one that produces Win32 errors instead of DOS error codes.
900 chdir is essentially a wrapper around SetCurrentDirectory; however,
901 it also needs to set "magic" environment variables indicating
902 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000903static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000904win32_chdir(LPCSTR path)
905{
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 char new_path[MAX_PATH+1];
907 int result;
908 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909
Victor Stinner8c62be82010-05-06 00:08:46 +0000910 if(!SetCurrentDirectoryA(path))
911 return FALSE;
912 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
913 if (!result)
914 return FALSE;
915 /* In the ANSI API, there should not be any paths longer
916 than MAX_PATH. */
917 assert(result <= MAX_PATH+1);
918 if (strncmp(new_path, "\\\\", 2) == 0 ||
919 strncmp(new_path, "//", 2) == 0)
920 /* UNC path, nothing to do. */
921 return TRUE;
922 env[1] = new_path[0];
923 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924}
925
926/* The Unicode version differs from the ANSI version
927 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000928static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000929win32_wchdir(LPCWSTR path)
930{
Victor Stinner8c62be82010-05-06 00:08:46 +0000931 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
932 int result;
933 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000934
Victor Stinner8c62be82010-05-06 00:08:46 +0000935 if(!SetCurrentDirectoryW(path))
936 return FALSE;
937 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
938 if (!result)
939 return FALSE;
940 if (result > MAX_PATH+1) {
941 new_path = malloc(result * sizeof(wchar_t));
942 if (!new_path) {
943 SetLastError(ERROR_OUTOFMEMORY);
944 return FALSE;
945 }
946 result = GetCurrentDirectoryW(result, new_path);
947 if (!result) {
948 free(new_path);
949 return FALSE;
950 }
951 }
952 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
953 wcsncmp(new_path, L"//", 2) == 0)
954 /* UNC path, nothing to do. */
955 return TRUE;
956 env[1] = new_path[0];
957 result = SetEnvironmentVariableW(env, new_path);
958 if (new_path != _new_path)
959 free(new_path);
960 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000961}
962#endif
963
Martin v. Löwis14694662006-02-03 12:54:16 +0000964#ifdef MS_WINDOWS
965/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
966 - time stamps are restricted to second resolution
967 - file modification times suffer from forth-and-back conversions between
968 UTC and local time
969 Therefore, we implement our own stat, based on the Win32 API directly.
970*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000971#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000972
973struct win32_stat{
974 int st_dev;
975 __int64 st_ino;
976 unsigned short st_mode;
977 int st_nlink;
978 int st_uid;
979 int st_gid;
980 int st_rdev;
981 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000982 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000983 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000984 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000985 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000986 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000987 int st_ctime_nsec;
988};
989
990static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
991
992static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000993FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000994{
Victor Stinner8c62be82010-05-06 00:08:46 +0000995 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
996 /* Cannot simply cast and dereference in_ptr,
997 since it might not be aligned properly */
998 __int64 in;
999 memcpy(&in, in_ptr, sizeof(in));
1000 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001001 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001002}
1003
Thomas Wouters477c8d52006-05-27 19:21:47 +00001004static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001005time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001006{
Victor Stinner8c62be82010-05-06 00:08:46 +00001007 /* XXX endianness */
1008 __int64 out;
1009 out = time_in + secs_between_epochs;
1010 out = out * 10000000 + nsec_in / 100;
1011 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001012}
1013
Martin v. Löwis14694662006-02-03 12:54:16 +00001014/* Below, we *know* that ugo+r is 0444 */
1015#if _S_IREAD != 0400
1016#error Unsupported C library
1017#endif
1018static int
1019attributes_to_mode(DWORD attr)
1020{
Victor Stinner8c62be82010-05-06 00:08:46 +00001021 int m = 0;
1022 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1023 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1024 else
1025 m |= _S_IFREG;
1026 if (attr & FILE_ATTRIBUTE_READONLY)
1027 m |= 0444;
1028 else
1029 m |= 0666;
1030 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001031}
1032
1033static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001034attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001035{
Victor Stinner8c62be82010-05-06 00:08:46 +00001036 memset(result, 0, sizeof(*result));
1037 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1038 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1039 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1040 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1041 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001042 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001043 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001044 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1045 /* first clear the S_IFMT bits */
1046 result->st_mode ^= (result->st_mode & 0170000);
1047 /* now set the bits that make this a symlink */
1048 result->st_mode |= 0120000;
1049 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001050
Victor Stinner8c62be82010-05-06 00:08:46 +00001051 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001052}
1053
Guido van Rossumd8faa362007-04-27 19:54:29 +00001054static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001055attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056{
Victor Stinner8c62be82010-05-06 00:08:46 +00001057 HANDLE hFindFile;
1058 WIN32_FIND_DATAA FileData;
1059 hFindFile = FindFirstFileA(pszFile, &FileData);
1060 if (hFindFile == INVALID_HANDLE_VALUE)
1061 return FALSE;
1062 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001063 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001064 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001065 info->dwFileAttributes = FileData.dwFileAttributes;
1066 info->ftCreationTime = FileData.ftCreationTime;
1067 info->ftLastAccessTime = FileData.ftLastAccessTime;
1068 info->ftLastWriteTime = FileData.ftLastWriteTime;
1069 info->nFileSizeHigh = FileData.nFileSizeHigh;
1070 info->nFileSizeLow = FileData.nFileSizeLow;
1071/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001072 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1073 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001074 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001075}
1076
1077static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001078attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001079{
Victor Stinner8c62be82010-05-06 00:08:46 +00001080 HANDLE hFindFile;
1081 WIN32_FIND_DATAW FileData;
1082 hFindFile = FindFirstFileW(pszFile, &FileData);
1083 if (hFindFile == INVALID_HANDLE_VALUE)
1084 return FALSE;
1085 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001086 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001087 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001088 info->dwFileAttributes = FileData.dwFileAttributes;
1089 info->ftCreationTime = FileData.ftCreationTime;
1090 info->ftLastAccessTime = FileData.ftLastAccessTime;
1091 info->ftLastWriteTime = FileData.ftLastWriteTime;
1092 info->nFileSizeHigh = FileData.nFileSizeHigh;
1093 info->nFileSizeLow = FileData.nFileSizeLow;
1094/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001095 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1096 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001097 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001098}
1099
Brian Curtind25aef52011-06-13 15:16:04 -05001100/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1101static int has_GetFinalPathNameByHandle = 0;
1102static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1103 DWORD);
1104static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1105 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001106static int
Brian Curtind25aef52011-06-13 15:16:04 -05001107check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001108{
Brian Curtind25aef52011-06-13 15:16:04 -05001109 HINSTANCE hKernel32;
1110 /* only recheck */
1111 if (!has_GetFinalPathNameByHandle)
1112 {
1113 hKernel32 = GetModuleHandle("KERNEL32");
1114 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1115 "GetFinalPathNameByHandleA");
1116 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1117 "GetFinalPathNameByHandleW");
1118 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1119 Py_GetFinalPathNameByHandleW;
1120 }
1121 return has_GetFinalPathNameByHandle;
1122}
1123
1124static BOOL
1125get_target_path(HANDLE hdl, wchar_t **target_path)
1126{
1127 int buf_size, result_length;
1128 wchar_t *buf;
1129
1130 /* We have a good handle to the target, use it to determine
1131 the target path name (then we'll call lstat on it). */
1132 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1133 VOLUME_NAME_DOS);
1134 if(!buf_size)
1135 return FALSE;
1136
1137 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001138 if (!buf) {
1139 SetLastError(ERROR_OUTOFMEMORY);
1140 return FALSE;
1141 }
1142
Brian Curtind25aef52011-06-13 15:16:04 -05001143 result_length = Py_GetFinalPathNameByHandleW(hdl,
1144 buf, buf_size, VOLUME_NAME_DOS);
1145
1146 if(!result_length) {
1147 free(buf);
1148 return FALSE;
1149 }
1150
1151 if(!CloseHandle(hdl)) {
1152 free(buf);
1153 return FALSE;
1154 }
1155
1156 buf[result_length] = 0;
1157
1158 *target_path = buf;
1159 return TRUE;
1160}
1161
1162static int
1163win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1164 BOOL traverse);
1165static int
1166win32_xstat_impl(const char *path, struct win32_stat *result,
1167 BOOL traverse)
1168{
Victor Stinner26de69d2011-06-17 15:15:38 +02001169 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001170 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001171 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001172 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001173 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001174 const char *dot;
1175
Brian Curtind25aef52011-06-13 15:16:04 -05001176 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001177 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1178 traverse reparse point. */
1179 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001180 }
1181
Brian Curtinf5e76d02010-11-24 13:14:05 +00001182 hFile = CreateFileA(
1183 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001184 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001185 0, /* share mode */
1186 NULL, /* security attributes */
1187 OPEN_EXISTING,
1188 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001189 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1190 Because of this, calls like GetFinalPathNameByHandle will return
1191 the symlink path agin and not the actual final path. */
1192 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1193 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001194 NULL);
1195
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001196 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001197 /* Either the target doesn't exist, or we don't have access to
1198 get a handle to it. If the former, we need to return an error.
1199 If the latter, we can use attributes_from_dir. */
1200 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001201 return -1;
1202 /* Could not get attributes on open file. Fall back to
1203 reading the directory. */
1204 if (!attributes_from_dir(path, &info, &reparse_tag))
1205 /* Very strange. This should not fail now */
1206 return -1;
1207 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1208 if (traverse) {
1209 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001210 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001211 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001212 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001213 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001214 } else {
1215 if (!GetFileInformationByHandle(hFile, &info)) {
1216 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001217 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001218 }
1219 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001220 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1221 return -1;
1222
1223 /* Close the outer open file handle now that we're about to
1224 reopen it with different flags. */
1225 if (!CloseHandle(hFile))
1226 return -1;
1227
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001228 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001229 /* In order to call GetFinalPathNameByHandle we need to open
1230 the file without the reparse handling flag set. */
1231 hFile2 = CreateFileA(
1232 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1233 NULL, OPEN_EXISTING,
1234 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1235 NULL);
1236 if (hFile2 == INVALID_HANDLE_VALUE)
1237 return -1;
1238
1239 if (!get_target_path(hFile2, &target_path))
1240 return -1;
1241
1242 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001243 free(target_path);
1244 return code;
1245 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001246 } else
1247 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001249 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001250
1251 /* Set S_IEXEC if it is an .exe, .bat, ... */
1252 dot = strrchr(path, '.');
1253 if (dot) {
1254 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1255 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1256 result->st_mode |= 0111;
1257 }
1258 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001259}
1260
1261static int
Brian Curtind25aef52011-06-13 15:16:04 -05001262win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1263 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001264{
1265 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001266 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001267 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001268 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001269 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001270 const wchar_t *dot;
1271
Brian Curtind25aef52011-06-13 15:16:04 -05001272 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001273 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1274 traverse reparse point. */
1275 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001276 }
1277
Brian Curtinf5e76d02010-11-24 13:14:05 +00001278 hFile = CreateFileW(
1279 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001280 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001281 0, /* share mode */
1282 NULL, /* security attributes */
1283 OPEN_EXISTING,
1284 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001285 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1286 Because of this, calls like GetFinalPathNameByHandle will return
1287 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001288 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001289 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001290 NULL);
1291
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001292 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001293 /* Either the target doesn't exist, or we don't have access to
1294 get a handle to it. If the former, we need to return an error.
1295 If the latter, we can use attributes_from_dir. */
1296 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001297 return -1;
1298 /* Could not get attributes on open file. Fall back to
1299 reading the directory. */
1300 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1301 /* Very strange. This should not fail now */
1302 return -1;
1303 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1304 if (traverse) {
1305 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001306 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001307 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001309 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001310 } else {
1311 if (!GetFileInformationByHandle(hFile, &info)) {
1312 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001313 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001314 }
1315 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001316 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1317 return -1;
1318
1319 /* Close the outer open file handle now that we're about to
1320 reopen it with different flags. */
1321 if (!CloseHandle(hFile))
1322 return -1;
1323
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001324 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001325 /* In order to call GetFinalPathNameByHandle we need to open
1326 the file without the reparse handling flag set. */
1327 hFile2 = CreateFileW(
1328 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1329 NULL, OPEN_EXISTING,
1330 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1331 NULL);
1332 if (hFile2 == INVALID_HANDLE_VALUE)
1333 return -1;
1334
1335 if (!get_target_path(hFile2, &target_path))
1336 return -1;
1337
1338 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001339 free(target_path);
1340 return code;
1341 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001342 } else
1343 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001344 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001345 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001346
1347 /* Set S_IEXEC if it is an .exe, .bat, ... */
1348 dot = wcsrchr(path, '.');
1349 if (dot) {
1350 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1351 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1352 result->st_mode |= 0111;
1353 }
1354 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001355}
1356
1357static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001358win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001359{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001360 /* Protocol violation: we explicitly clear errno, instead of
1361 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001362 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001363 errno = 0;
1364 return code;
1365}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001366
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001367static int
1368win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1369{
1370 /* Protocol violation: we explicitly clear errno, instead of
1371 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001372 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001373 errno = 0;
1374 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001375}
Brian Curtind25aef52011-06-13 15:16:04 -05001376/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001377
1378 In Posix, stat automatically traverses symlinks and returns the stat
1379 structure for the target. In Windows, the equivalent GetFileAttributes by
1380 default does not traverse symlinks and instead returns attributes for
1381 the symlink.
1382
1383 Therefore, win32_lstat will get the attributes traditionally, and
1384 win32_stat will first explicitly resolve the symlink target and then will
1385 call win32_lstat on that result.
1386
Ezio Melotti4969f702011-03-15 05:59:46 +02001387 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001388
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001389static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001390win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001391{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001392 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001393}
1394
Victor Stinner8c62be82010-05-06 00:08:46 +00001395static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001396win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001397{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001398 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001399}
1400
1401static int
1402win32_stat(const char* path, struct win32_stat *result)
1403{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001404 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001405}
1406
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001407static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001408win32_stat_w(const wchar_t* path, struct win32_stat *result)
1409{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001410 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001411}
1412
1413static int
1414win32_fstat(int file_number, struct win32_stat *result)
1415{
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 BY_HANDLE_FILE_INFORMATION info;
1417 HANDLE h;
1418 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001419
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 /* Protocol violation: we explicitly clear errno, instead of
1423 setting it to a POSIX error. Callers should use GetLastError. */
1424 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001425
Victor Stinner8c62be82010-05-06 00:08:46 +00001426 if (h == INVALID_HANDLE_VALUE) {
1427 /* This is really a C library error (invalid file handle).
1428 We set the Win32 error to the closes one matching. */
1429 SetLastError(ERROR_INVALID_HANDLE);
1430 return -1;
1431 }
1432 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001433
Victor Stinner8c62be82010-05-06 00:08:46 +00001434 type = GetFileType(h);
1435 if (type == FILE_TYPE_UNKNOWN) {
1436 DWORD error = GetLastError();
1437 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001438 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001439 }
1440 /* else: valid but unknown file */
1441 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001442
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 if (type != FILE_TYPE_DISK) {
1444 if (type == FILE_TYPE_CHAR)
1445 result->st_mode = _S_IFCHR;
1446 else if (type == FILE_TYPE_PIPE)
1447 result->st_mode = _S_IFIFO;
1448 return 0;
1449 }
1450
1451 if (!GetFileInformationByHandle(h, &info)) {
1452 return -1;
1453 }
1454
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001455 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001456 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001457 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1458 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001459}
1460
1461#endif /* MS_WINDOWS */
1462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001463PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001464"stat_result: Result from stat or lstat.\n\n\
1465This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001466 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001467or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1468\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001469Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1470or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001471\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001472See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001473
1474static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001475 {"st_mode", "protection bits"},
1476 {"st_ino", "inode"},
1477 {"st_dev", "device"},
1478 {"st_nlink", "number of hard links"},
1479 {"st_uid", "user ID of owner"},
1480 {"st_gid", "group ID of owner"},
1481 {"st_size", "total size, in bytes"},
1482 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1483 {NULL, "integer time of last access"},
1484 {NULL, "integer time of last modification"},
1485 {NULL, "integer time of last change"},
1486 {"st_atime", "time of last access"},
1487 {"st_mtime", "time of last modification"},
1488 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001489#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001490 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001491#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001492#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001493 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001494#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001495#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001496 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001497#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001498#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001499 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001500#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001501#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001502 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001503#endif
1504#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001505 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001506#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508};
1509
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001510#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001511#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001512#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001513#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001514#endif
1515
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001516#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001517#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1518#else
1519#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1520#endif
1521
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001522#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001523#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1524#else
1525#define ST_RDEV_IDX ST_BLOCKS_IDX
1526#endif
1527
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001528#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1529#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1530#else
1531#define ST_FLAGS_IDX ST_RDEV_IDX
1532#endif
1533
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001534#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001535#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001536#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001537#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001538#endif
1539
1540#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1541#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1542#else
1543#define ST_BIRTHTIME_IDX ST_GEN_IDX
1544#endif
1545
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001546static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001547 "stat_result", /* name */
1548 stat_result__doc__, /* doc */
1549 stat_result_fields,
1550 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001551};
1552
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001553PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001554"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1555This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001556 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001557or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001558\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001559See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001560
1561static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001562 {"f_bsize", },
1563 {"f_frsize", },
1564 {"f_blocks", },
1565 {"f_bfree", },
1566 {"f_bavail", },
1567 {"f_files", },
1568 {"f_ffree", },
1569 {"f_favail", },
1570 {"f_flag", },
1571 {"f_namemax",},
1572 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001573};
1574
1575static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001576 "statvfs_result", /* name */
1577 statvfs_result__doc__, /* doc */
1578 statvfs_result_fields,
1579 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001580};
1581
Ross Lagerwall7807c352011-03-17 20:20:30 +02001582#if defined(HAVE_WAITID) && !defined(__APPLE__)
1583PyDoc_STRVAR(waitid_result__doc__,
1584"waitid_result: Result from waitid.\n\n\
1585This object may be accessed either as a tuple of\n\
1586 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1587or via the attributes si_pid, si_uid, and so on.\n\
1588\n\
1589See os.waitid for more information.");
1590
1591static PyStructSequence_Field waitid_result_fields[] = {
1592 {"si_pid", },
1593 {"si_uid", },
1594 {"si_signo", },
1595 {"si_status", },
1596 {"si_code", },
1597 {0}
1598};
1599
1600static PyStructSequence_Desc waitid_result_desc = {
1601 "waitid_result", /* name */
1602 waitid_result__doc__, /* doc */
1603 waitid_result_fields,
1604 5
1605};
1606static PyTypeObject WaitidResultType;
1607#endif
1608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001609static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001610static PyTypeObject StatResultType;
1611static PyTypeObject StatVFSResultType;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001612static PyTypeObject SchedParamType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001613static newfunc structseq_new;
1614
1615static PyObject *
1616statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1617{
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 PyStructSequence *result;
1619 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001620
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 result = (PyStructSequence*)structseq_new(type, args, kwds);
1622 if (!result)
1623 return NULL;
1624 /* If we have been initialized from a tuple,
1625 st_?time might be set to None. Initialize it
1626 from the int slots. */
1627 for (i = 7; i <= 9; i++) {
1628 if (result->ob_item[i+3] == Py_None) {
1629 Py_DECREF(Py_None);
1630 Py_INCREF(result->ob_item[i]);
1631 result->ob_item[i+3] = result->ob_item[i];
1632 }
1633 }
1634 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001635}
1636
1637
1638
1639/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001640static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001641
1642PyDoc_STRVAR(stat_float_times__doc__,
1643"stat_float_times([newval]) -> oldval\n\n\
1644Determine whether os.[lf]stat represents time stamps as float objects.\n\
1645If newval is True, future calls to stat() return floats, if it is False,\n\
1646future calls return ints. \n\
1647If newval is omitted, return the current setting.\n");
1648
1649static PyObject*
1650stat_float_times(PyObject* self, PyObject *args)
1651{
Victor Stinner8c62be82010-05-06 00:08:46 +00001652 int newval = -1;
1653 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1654 return NULL;
1655 if (newval == -1)
1656 /* Return old value */
1657 return PyBool_FromLong(_stat_float_times);
1658 _stat_float_times = newval;
1659 Py_INCREF(Py_None);
1660 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001661}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001662
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001663static void
1664fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1665{
Victor Stinner8c62be82010-05-06 00:08:46 +00001666 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001667#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001668 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001669#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001670 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001671#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001672 if (!ival)
1673 return;
1674 if (_stat_float_times) {
1675 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1676 } else {
1677 fval = ival;
1678 Py_INCREF(fval);
1679 }
1680 PyStructSequence_SET_ITEM(v, index, ival);
1681 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001682}
1683
Tim Peters5aa91602002-01-30 05:46:57 +00001684/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001685 (used by posix_stat() and posix_fstat()) */
1686static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001687_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001688{
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 unsigned long ansec, mnsec, cnsec;
1690 PyObject *v = PyStructSequence_New(&StatResultType);
1691 if (v == NULL)
1692 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001693
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001695#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 PyStructSequence_SET_ITEM(v, 1,
1697 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001698#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001700#endif
1701#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 PyStructSequence_SET_ITEM(v, 2,
1703 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001704#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001705 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001706#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001707 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1708 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1709 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001710#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001711 PyStructSequence_SET_ITEM(v, 6,
1712 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001713#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001714 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001715#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001716
Martin v. Löwis14694662006-02-03 12:54:16 +00001717#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 ansec = st->st_atim.tv_nsec;
1719 mnsec = st->st_mtim.tv_nsec;
1720 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001721#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001722 ansec = st->st_atimespec.tv_nsec;
1723 mnsec = st->st_mtimespec.tv_nsec;
1724 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001725#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 ansec = st->st_atime_nsec;
1727 mnsec = st->st_mtime_nsec;
1728 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001729#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001731#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 fill_time(v, 7, st->st_atime, ansec);
1733 fill_time(v, 8, st->st_mtime, mnsec);
1734 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001735
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001736#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1738 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001739#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001740#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1742 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001743#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001744#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001745 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1746 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001747#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001748#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001749 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1750 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001751#endif
1752#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 {
1754 PyObject *val;
1755 unsigned long bsec,bnsec;
1756 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001757#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001759#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001761#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 if (_stat_float_times) {
1763 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1764 } else {
1765 val = PyLong_FromLong((long)bsec);
1766 }
1767 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1768 val);
1769 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001770#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001771#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001772 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1773 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001774#endif
Fred Drake699f3522000-06-29 21:12:41 +00001775
Victor Stinner8c62be82010-05-06 00:08:46 +00001776 if (PyErr_Occurred()) {
1777 Py_DECREF(v);
1778 return NULL;
1779 }
Fred Drake699f3522000-06-29 21:12:41 +00001780
Victor Stinner8c62be82010-05-06 00:08:46 +00001781 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001782}
1783
Barry Warsaw53699e91996-12-10 23:23:01 +00001784static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001785posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001786 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001787#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001789#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001791#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 char *wformat,
1793 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001794{
Victor Stinner8c62be82010-05-06 00:08:46 +00001795 STRUCT_STAT st;
1796 PyObject *opath;
1797 char *path;
1798 int res;
1799 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001800
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001801#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001802 PyUnicodeObject *po;
1803 if (PyArg_ParseTuple(args, wformat, &po)) {
1804 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001805
Victor Stinner8c62be82010-05-06 00:08:46 +00001806 Py_BEGIN_ALLOW_THREADS
1807 /* PyUnicode_AS_UNICODE result OK without
1808 thread lock as it is a simple dereference. */
1809 res = wstatfunc(wpath, &st);
1810 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001811
Victor Stinner8c62be82010-05-06 00:08:46 +00001812 if (res != 0)
1813 return win32_error_unicode("stat", wpath);
1814 return _pystat_fromstructstat(&st);
1815 }
1816 /* Drop the argument parsing error as narrow strings
1817 are also valid. */
1818 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001819#endif
1820
Victor Stinner8c62be82010-05-06 00:08:46 +00001821 if (!PyArg_ParseTuple(args, format,
1822 PyUnicode_FSConverter, &opath))
1823 return NULL;
1824 path = PyBytes_AsString(opath);
1825 Py_BEGIN_ALLOW_THREADS
1826 res = (*statfunc)(path, &st);
1827 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001828
Victor Stinner8c62be82010-05-06 00:08:46 +00001829 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001830#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001832#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001834#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 }
1836 else
1837 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001838
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 Py_DECREF(opath);
1840 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841}
1842
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001843/* POSIX methods */
1844
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001845PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001846"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001847Use the real uid/gid to test for access to a path. Note that most\n\
1848operations will use the effective uid/gid, therefore this routine can\n\
1849be used in a suid/sgid environment to test if the invoking user has the\n\
1850specified access to the path. The mode argument can be F_OK to test\n\
1851existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001852
1853static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001854posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001855{
Victor Stinner8c62be82010-05-06 00:08:46 +00001856 PyObject *opath;
1857 char *path;
1858 int mode;
1859
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001860#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 DWORD attr;
1862 PyUnicodeObject *po;
1863 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1864 Py_BEGIN_ALLOW_THREADS
1865 /* PyUnicode_AS_UNICODE OK without thread lock as
1866 it is a simple dereference. */
1867 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1868 Py_END_ALLOW_THREADS
1869 goto finish;
1870 }
1871 /* Drop the argument parsing error as narrow strings
1872 are also valid. */
1873 PyErr_Clear();
1874 if (!PyArg_ParseTuple(args, "O&i:access",
1875 PyUnicode_FSConverter, &opath, &mode))
1876 return NULL;
1877 path = PyBytes_AsString(opath);
1878 Py_BEGIN_ALLOW_THREADS
1879 attr = GetFileAttributesA(path);
1880 Py_END_ALLOW_THREADS
1881 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001882finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 if (attr == 0xFFFFFFFF)
1884 /* File does not exist, or cannot read attributes */
1885 return PyBool_FromLong(0);
1886 /* Access is possible if either write access wasn't requested, or
1887 the file isn't read-only, or if it's a directory, as there are
1888 no read-only directories on Windows. */
1889 return PyBool_FromLong(!(mode & 2)
1890 || !(attr & FILE_ATTRIBUTE_READONLY)
1891 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001892#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001893 int res;
1894 if (!PyArg_ParseTuple(args, "O&i:access",
1895 PyUnicode_FSConverter, &opath, &mode))
1896 return NULL;
1897 path = PyBytes_AsString(opath);
1898 Py_BEGIN_ALLOW_THREADS
1899 res = access(path, mode);
1900 Py_END_ALLOW_THREADS
1901 Py_DECREF(opath);
1902 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001903#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001904}
1905
Guido van Rossumd371ff11999-01-25 16:12:23 +00001906#ifndef F_OK
1907#define F_OK 0
1908#endif
1909#ifndef R_OK
1910#define R_OK 4
1911#endif
1912#ifndef W_OK
1913#define W_OK 2
1914#endif
1915#ifndef X_OK
1916#define X_OK 1
1917#endif
1918
1919#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001921"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001923
1924static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001925posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001926{
Victor Stinner8c62be82010-05-06 00:08:46 +00001927 int id;
1928 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001929
Victor Stinner8c62be82010-05-06 00:08:46 +00001930 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1931 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001932
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001933#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001934 /* file descriptor 0 only, the default input device (stdin) */
1935 if (id == 0) {
1936 ret = ttyname();
1937 }
1938 else {
1939 ret = NULL;
1940 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001941#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001942 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001943#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 if (ret == NULL)
1945 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001946 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001947}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001948#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001949
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001950#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001951PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001952"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001954
1955static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001956posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001957{
Victor Stinner8c62be82010-05-06 00:08:46 +00001958 char *ret;
1959 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001960
Greg Wardb48bc172000-03-01 21:51:56 +00001961#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001962 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001963#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001964 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001965#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 if (ret == NULL)
1967 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001968 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001969}
1970#endif
1971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001973"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001974Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Barry Warsaw53699e91996-12-10 23:23:01 +00001976static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001977posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001979#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001980 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001981#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001982 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001983#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001984 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001985#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001986 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001987#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001988}
1989
Fred Drake4d1e64b2002-04-15 19:40:07 +00001990#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001992"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001993Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001995
1996static PyObject *
1997posix_fchdir(PyObject *self, PyObject *fdobj)
1998{
Victor Stinner8c62be82010-05-06 00:08:46 +00001999 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002000}
2001#endif /* HAVE_FCHDIR */
2002
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002004PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002005"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002006Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002007
Barry Warsaw53699e91996-12-10 23:23:01 +00002008static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002009posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002010{
Victor Stinner8c62be82010-05-06 00:08:46 +00002011 PyObject *opath = NULL;
2012 char *path = NULL;
2013 int i;
2014 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002015#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002016 DWORD attr;
2017 PyUnicodeObject *po;
2018 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
2019 Py_BEGIN_ALLOW_THREADS
2020 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
2021 if (attr != 0xFFFFFFFF) {
2022 if (i & _S_IWRITE)
2023 attr &= ~FILE_ATTRIBUTE_READONLY;
2024 else
2025 attr |= FILE_ATTRIBUTE_READONLY;
2026 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
2027 }
2028 else
2029 res = 0;
2030 Py_END_ALLOW_THREADS
2031 if (!res)
2032 return win32_error_unicode("chmod",
2033 PyUnicode_AS_UNICODE(po));
2034 Py_INCREF(Py_None);
2035 return Py_None;
2036 }
2037 /* Drop the argument parsing error as narrow strings
2038 are also valid. */
2039 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002040
Victor Stinner8c62be82010-05-06 00:08:46 +00002041 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2042 &opath, &i))
2043 return NULL;
2044 path = PyBytes_AsString(opath);
2045 Py_BEGIN_ALLOW_THREADS
2046 attr = GetFileAttributesA(path);
2047 if (attr != 0xFFFFFFFF) {
2048 if (i & _S_IWRITE)
2049 attr &= ~FILE_ATTRIBUTE_READONLY;
2050 else
2051 attr |= FILE_ATTRIBUTE_READONLY;
2052 res = SetFileAttributesA(path, attr);
2053 }
2054 else
2055 res = 0;
2056 Py_END_ALLOW_THREADS
2057 if (!res) {
2058 win32_error("chmod", path);
2059 Py_DECREF(opath);
2060 return NULL;
2061 }
2062 Py_DECREF(opath);
2063 Py_INCREF(Py_None);
2064 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002065#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00002066 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
2067 &opath, &i))
2068 return NULL;
2069 path = PyBytes_AsString(opath);
2070 Py_BEGIN_ALLOW_THREADS
2071 res = chmod(path, i);
2072 Py_END_ALLOW_THREADS
2073 if (res < 0)
2074 return posix_error_with_allocated_filename(opath);
2075 Py_DECREF(opath);
2076 Py_INCREF(Py_None);
2077 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002078#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002079}
2080
Christian Heimes4e30a842007-11-30 22:12:06 +00002081#ifdef HAVE_FCHMOD
2082PyDoc_STRVAR(posix_fchmod__doc__,
2083"fchmod(fd, mode)\n\n\
2084Change the access permissions of the file given by file\n\
2085descriptor fd.");
2086
2087static PyObject *
2088posix_fchmod(PyObject *self, PyObject *args)
2089{
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 int fd, mode, res;
2091 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2092 return NULL;
2093 Py_BEGIN_ALLOW_THREADS
2094 res = fchmod(fd, mode);
2095 Py_END_ALLOW_THREADS
2096 if (res < 0)
2097 return posix_error();
2098 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002099}
2100#endif /* HAVE_FCHMOD */
2101
2102#ifdef HAVE_LCHMOD
2103PyDoc_STRVAR(posix_lchmod__doc__,
2104"lchmod(path, mode)\n\n\
2105Change the access permissions of a file. If path is a symlink, this\n\
2106affects the link itself rather than the target.");
2107
2108static PyObject *
2109posix_lchmod(PyObject *self, PyObject *args)
2110{
Victor Stinner8c62be82010-05-06 00:08:46 +00002111 PyObject *opath;
2112 char *path;
2113 int i;
2114 int res;
2115 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2116 &opath, &i))
2117 return NULL;
2118 path = PyBytes_AsString(opath);
2119 Py_BEGIN_ALLOW_THREADS
2120 res = lchmod(path, i);
2121 Py_END_ALLOW_THREADS
2122 if (res < 0)
2123 return posix_error_with_allocated_filename(opath);
2124 Py_DECREF(opath);
2125 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002126}
2127#endif /* HAVE_LCHMOD */
2128
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002129
Thomas Wouterscf297e42007-02-23 15:07:44 +00002130#ifdef HAVE_CHFLAGS
2131PyDoc_STRVAR(posix_chflags__doc__,
2132"chflags(path, flags)\n\n\
2133Set file flags.");
2134
2135static PyObject *
2136posix_chflags(PyObject *self, PyObject *args)
2137{
Victor Stinner8c62be82010-05-06 00:08:46 +00002138 PyObject *opath;
2139 char *path;
2140 unsigned long flags;
2141 int res;
2142 if (!PyArg_ParseTuple(args, "O&k:chflags",
2143 PyUnicode_FSConverter, &opath, &flags))
2144 return NULL;
2145 path = PyBytes_AsString(opath);
2146 Py_BEGIN_ALLOW_THREADS
2147 res = chflags(path, flags);
2148 Py_END_ALLOW_THREADS
2149 if (res < 0)
2150 return posix_error_with_allocated_filename(opath);
2151 Py_DECREF(opath);
2152 Py_INCREF(Py_None);
2153 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002154}
2155#endif /* HAVE_CHFLAGS */
2156
2157#ifdef HAVE_LCHFLAGS
2158PyDoc_STRVAR(posix_lchflags__doc__,
2159"lchflags(path, flags)\n\n\
2160Set file flags.\n\
2161This function will not follow symbolic links.");
2162
2163static PyObject *
2164posix_lchflags(PyObject *self, PyObject *args)
2165{
Victor Stinner8c62be82010-05-06 00:08:46 +00002166 PyObject *opath;
2167 char *path;
2168 unsigned long flags;
2169 int res;
2170 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2171 PyUnicode_FSConverter, &opath, &flags))
2172 return NULL;
2173 path = PyBytes_AsString(opath);
2174 Py_BEGIN_ALLOW_THREADS
2175 res = lchflags(path, flags);
2176 Py_END_ALLOW_THREADS
2177 if (res < 0)
2178 return posix_error_with_allocated_filename(opath);
2179 Py_DECREF(opath);
2180 Py_INCREF(Py_None);
2181 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002182}
2183#endif /* HAVE_LCHFLAGS */
2184
Martin v. Löwis244edc82001-10-04 22:44:26 +00002185#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002186PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002187"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002188Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002189
2190static PyObject *
2191posix_chroot(PyObject *self, PyObject *args)
2192{
Victor Stinner8c62be82010-05-06 00:08:46 +00002193 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002194}
2195#endif
2196
Guido van Rossum21142a01999-01-08 21:05:37 +00002197#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002198PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002199"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002200force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002201
2202static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002203posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002204{
Stefan Krah0e803b32010-11-26 16:16:47 +00002205 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002206}
2207#endif /* HAVE_FSYNC */
2208
Ross Lagerwall7807c352011-03-17 20:20:30 +02002209#ifdef HAVE_SYNC
2210PyDoc_STRVAR(posix_sync__doc__,
2211"sync()\n\n\
2212Force write of everything to disk.");
2213
2214static PyObject *
2215posix_sync(PyObject *self, PyObject *noargs)
2216{
2217 Py_BEGIN_ALLOW_THREADS
2218 sync();
2219 Py_END_ALLOW_THREADS
2220 Py_RETURN_NONE;
2221}
2222#endif
2223
Guido van Rossum21142a01999-01-08 21:05:37 +00002224#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002225
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002226#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002227extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2228#endif
2229
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002230PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002231"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002232force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002233 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002234
2235static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002236posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002237{
Stefan Krah0e803b32010-11-26 16:16:47 +00002238 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002239}
2240#endif /* HAVE_FDATASYNC */
2241
2242
Fredrik Lundh10723342000-07-10 16:38:09 +00002243#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002244PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002245"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002246Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002247
Barry Warsaw53699e91996-12-10 23:23:01 +00002248static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002249posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250{
Victor Stinner8c62be82010-05-06 00:08:46 +00002251 PyObject *opath;
2252 char *path;
2253 long uid, gid;
2254 int res;
2255 if (!PyArg_ParseTuple(args, "O&ll:chown",
2256 PyUnicode_FSConverter, &opath,
2257 &uid, &gid))
2258 return NULL;
2259 path = PyBytes_AsString(opath);
2260 Py_BEGIN_ALLOW_THREADS
2261 res = chown(path, (uid_t) uid, (gid_t) gid);
2262 Py_END_ALLOW_THREADS
2263 if (res < 0)
2264 return posix_error_with_allocated_filename(opath);
2265 Py_DECREF(opath);
2266 Py_INCREF(Py_None);
2267 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002268}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002269#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002270
Christian Heimes4e30a842007-11-30 22:12:06 +00002271#ifdef HAVE_FCHOWN
2272PyDoc_STRVAR(posix_fchown__doc__,
2273"fchown(fd, uid, gid)\n\n\
2274Change the owner and group id of the file given by file descriptor\n\
2275fd to the numeric uid and gid.");
2276
2277static PyObject *
2278posix_fchown(PyObject *self, PyObject *args)
2279{
Victor Stinner8c62be82010-05-06 00:08:46 +00002280 int fd;
2281 long uid, gid;
2282 int res;
Victor Stinner26de69d2011-06-17 15:15:38 +02002283 if (!PyArg_ParseTuple(args, "ill:fchown", &fd, &uid, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00002284 return NULL;
2285 Py_BEGIN_ALLOW_THREADS
2286 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2287 Py_END_ALLOW_THREADS
2288 if (res < 0)
2289 return posix_error();
2290 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002291}
2292#endif /* HAVE_FCHOWN */
2293
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002294#ifdef HAVE_LCHOWN
2295PyDoc_STRVAR(posix_lchown__doc__,
2296"lchown(path, uid, gid)\n\n\
2297Change the owner and group id of path to the numeric uid and gid.\n\
2298This function will not follow symbolic links.");
2299
2300static PyObject *
2301posix_lchown(PyObject *self, PyObject *args)
2302{
Victor Stinner8c62be82010-05-06 00:08:46 +00002303 PyObject *opath;
2304 char *path;
2305 long uid, gid;
2306 int res;
2307 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2308 PyUnicode_FSConverter, &opath,
2309 &uid, &gid))
2310 return NULL;
2311 path = PyBytes_AsString(opath);
2312 Py_BEGIN_ALLOW_THREADS
2313 res = lchown(path, (uid_t) uid, (gid_t) gid);
2314 Py_END_ALLOW_THREADS
2315 if (res < 0)
2316 return posix_error_with_allocated_filename(opath);
2317 Py_DECREF(opath);
2318 Py_INCREF(Py_None);
2319 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002320}
2321#endif /* HAVE_LCHOWN */
2322
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002323
Guido van Rossum36bc6801995-06-14 22:54:23 +00002324#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002325static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002326posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002327{
Victor Stinner8c62be82010-05-06 00:08:46 +00002328 char buf[1026];
2329 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002330
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002331#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002332 if (!use_bytes) {
2333 wchar_t wbuf[1026];
2334 wchar_t *wbuf2 = wbuf;
2335 PyObject *resobj;
2336 DWORD len;
2337 Py_BEGIN_ALLOW_THREADS
2338 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2339 /* If the buffer is large enough, len does not include the
2340 terminating \0. If the buffer is too small, len includes
2341 the space needed for the terminator. */
2342 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2343 wbuf2 = malloc(len * sizeof(wchar_t));
2344 if (wbuf2)
2345 len = GetCurrentDirectoryW(len, wbuf2);
2346 }
2347 Py_END_ALLOW_THREADS
2348 if (!wbuf2) {
2349 PyErr_NoMemory();
2350 return NULL;
2351 }
2352 if (!len) {
2353 if (wbuf2 != wbuf) free(wbuf2);
2354 return win32_error("getcwdu", NULL);
2355 }
2356 resobj = PyUnicode_FromWideChar(wbuf2, len);
2357 if (wbuf2 != wbuf) free(wbuf2);
2358 return resobj;
2359 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002360#endif
2361
Victor Stinner8c62be82010-05-06 00:08:46 +00002362 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002363#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002364 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002365#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002367#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 Py_END_ALLOW_THREADS
2369 if (res == NULL)
2370 return posix_error();
2371 if (use_bytes)
2372 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002373 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002374}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002375
2376PyDoc_STRVAR(posix_getcwd__doc__,
2377"getcwd() -> path\n\n\
2378Return a unicode string representing the current working directory.");
2379
2380static PyObject *
2381posix_getcwd_unicode(PyObject *self)
2382{
2383 return posix_getcwd(0);
2384}
2385
2386PyDoc_STRVAR(posix_getcwdb__doc__,
2387"getcwdb() -> path\n\n\
2388Return a bytes string representing the current working directory.");
2389
2390static PyObject *
2391posix_getcwd_bytes(PyObject *self)
2392{
2393 return posix_getcwd(1);
2394}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002395#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002396
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002397
Guido van Rossumb6775db1994-08-01 11:34:53 +00002398#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002399PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002400"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002401Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002402
Barry Warsaw53699e91996-12-10 23:23:01 +00002403static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002404posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002405{
Victor Stinner8c62be82010-05-06 00:08:46 +00002406 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002407}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002408#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002409
Brian Curtin1b9df392010-11-24 20:24:31 +00002410#ifdef MS_WINDOWS
2411PyDoc_STRVAR(win32_link__doc__,
2412"link(src, dst)\n\n\
2413Create a hard link to a file.");
2414
2415static PyObject *
2416win32_link(PyObject *self, PyObject *args)
2417{
2418 PyObject *osrc, *odst;
2419 char *src, *dst;
2420 BOOL rslt;
2421
Brian Curtinfc889c42010-11-28 23:59:46 +00002422 PyUnicodeObject *usrc, *udst;
2423 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2424 Py_BEGIN_ALLOW_THREADS
2425 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2426 PyUnicode_AS_UNICODE(usrc), NULL);
2427 Py_END_ALLOW_THREADS
2428
2429 if (rslt == 0)
2430 return win32_error("link", NULL);
2431
2432 Py_RETURN_NONE;
2433 }
2434
2435 /* Narrow strings also valid. */
2436 PyErr_Clear();
2437
Brian Curtin1b9df392010-11-24 20:24:31 +00002438 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2439 PyUnicode_FSConverter, &odst))
2440 return NULL;
2441
2442 src = PyBytes_AsString(osrc);
2443 dst = PyBytes_AsString(odst);
2444
2445 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002446 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002447 Py_END_ALLOW_THREADS
2448
Stefan Krah30b341f2010-11-27 11:44:18 +00002449 Py_DECREF(osrc);
2450 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002451 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002452 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002453
2454 Py_RETURN_NONE;
2455}
2456#endif /* MS_WINDOWS */
2457
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002459PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002460"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002461Return a list containing the names of the entries in the directory.\n\
2462\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002463 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002464\n\
2465The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002466entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002467
Barry Warsaw53699e91996-12-10 23:23:01 +00002468static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002469posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002470{
Victor Stinner8c62be82010-05-06 00:08:46 +00002471 /* XXX Should redo this putting the (now four) versions of opendir
2472 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002473#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002474
Victor Stinner8c62be82010-05-06 00:08:46 +00002475 PyObject *d, *v;
2476 HANDLE hFindFile;
2477 BOOL result;
2478 WIN32_FIND_DATA FileData;
2479 PyObject *opath;
2480 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2481 char *bufptr = namebuf;
2482 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002483
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002484 PyObject *po = NULL;
2485 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002487 Py_UNICODE *wnamebuf, *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002488
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002489 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002490 po_wchars = L".";
2491 len = 1;
2492 } else {
2493 po_wchars = PyUnicode_AS_UNICODE(po);
2494 len = PyUnicode_GET_SIZE(po);
2495 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002496 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002497 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2498 if (!wnamebuf) {
2499 PyErr_NoMemory();
2500 return NULL;
2501 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002502 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002503 if (len > 0) {
2504 Py_UNICODE wch = wnamebuf[len-1];
2505 if (wch != L'/' && wch != L'\\' && wch != L':')
2506 wnamebuf[len++] = L'\\';
2507 wcscpy(wnamebuf + len, L"*.*");
2508 }
2509 if ((d = PyList_New(0)) == NULL) {
2510 free(wnamebuf);
2511 return NULL;
2512 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002513 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002514 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002515 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002516 if (hFindFile == INVALID_HANDLE_VALUE) {
2517 int error = GetLastError();
2518 if (error == ERROR_FILE_NOT_FOUND) {
2519 free(wnamebuf);
2520 return d;
2521 }
2522 Py_DECREF(d);
2523 win32_error_unicode("FindFirstFileW", wnamebuf);
2524 free(wnamebuf);
2525 return NULL;
2526 }
2527 do {
2528 /* Skip over . and .. */
2529 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2530 wcscmp(wFileData.cFileName, L"..") != 0) {
2531 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2532 if (v == NULL) {
2533 Py_DECREF(d);
2534 d = NULL;
2535 break;
2536 }
2537 if (PyList_Append(d, v) != 0) {
2538 Py_DECREF(v);
2539 Py_DECREF(d);
2540 d = NULL;
2541 break;
2542 }
2543 Py_DECREF(v);
2544 }
2545 Py_BEGIN_ALLOW_THREADS
2546 result = FindNextFileW(hFindFile, &wFileData);
2547 Py_END_ALLOW_THREADS
2548 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2549 it got to the end of the directory. */
2550 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2551 Py_DECREF(d);
2552 win32_error_unicode("FindNextFileW", wnamebuf);
2553 FindClose(hFindFile);
2554 free(wnamebuf);
2555 return NULL;
2556 }
2557 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002558
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 if (FindClose(hFindFile) == FALSE) {
2560 Py_DECREF(d);
2561 win32_error_unicode("FindClose", wnamebuf);
2562 free(wnamebuf);
2563 return NULL;
2564 }
2565 free(wnamebuf);
2566 return d;
2567 }
2568 /* Drop the argument parsing error as narrow strings
2569 are also valid. */
2570 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002571
Victor Stinner8c62be82010-05-06 00:08:46 +00002572 if (!PyArg_ParseTuple(args, "O&:listdir",
2573 PyUnicode_FSConverter, &opath))
2574 return NULL;
2575 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2576 PyErr_SetString(PyExc_ValueError, "path too long");
2577 Py_DECREF(opath);
2578 return NULL;
2579 }
2580 strcpy(namebuf, PyBytes_AsString(opath));
2581 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002582 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 if (len > 0) {
2584 char ch = namebuf[len-1];
2585 if (ch != SEP && ch != ALTSEP && ch != ':')
2586 namebuf[len++] = '/';
2587 strcpy(namebuf + len, "*.*");
2588 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002589
Victor Stinner8c62be82010-05-06 00:08:46 +00002590 if ((d = PyList_New(0)) == NULL)
2591 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002592
Antoine Pitroub73caab2010-08-09 23:39:31 +00002593 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002595 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002596 if (hFindFile == INVALID_HANDLE_VALUE) {
2597 int error = GetLastError();
2598 if (error == ERROR_FILE_NOT_FOUND)
2599 return d;
2600 Py_DECREF(d);
2601 return win32_error("FindFirstFile", namebuf);
2602 }
2603 do {
2604 /* Skip over . and .. */
2605 if (strcmp(FileData.cFileName, ".") != 0 &&
2606 strcmp(FileData.cFileName, "..") != 0) {
2607 v = PyBytes_FromString(FileData.cFileName);
2608 if (v == NULL) {
2609 Py_DECREF(d);
2610 d = NULL;
2611 break;
2612 }
2613 if (PyList_Append(d, v) != 0) {
2614 Py_DECREF(v);
2615 Py_DECREF(d);
2616 d = NULL;
2617 break;
2618 }
2619 Py_DECREF(v);
2620 }
2621 Py_BEGIN_ALLOW_THREADS
2622 result = FindNextFile(hFindFile, &FileData);
2623 Py_END_ALLOW_THREADS
2624 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2625 it got to the end of the directory. */
2626 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2627 Py_DECREF(d);
2628 win32_error("FindNextFile", namebuf);
2629 FindClose(hFindFile);
2630 return NULL;
2631 }
2632 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002633
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 if (FindClose(hFindFile) == FALSE) {
2635 Py_DECREF(d);
2636 return win32_error("FindClose", namebuf);
2637 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002638
Victor Stinner8c62be82010-05-06 00:08:46 +00002639 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002640
Tim Peters0bb44a42000-09-15 07:44:49 +00002641#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002642
2643#ifndef MAX_PATH
2644#define MAX_PATH CCHMAXPATH
2645#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002646 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002647 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002648 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002649 PyObject *d, *v;
2650 char namebuf[MAX_PATH+5];
2651 HDIR hdir = 1;
2652 ULONG srchcnt = 1;
2653 FILEFINDBUF3 ep;
2654 APIRET rc;
2655
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002657 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002658 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002659 name = PyBytes_AsString(oname);
2660 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002661 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002662 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002663 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002664 return NULL;
2665 }
2666 strcpy(namebuf, name);
2667 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002668 if (*pt == ALTSEP)
2669 *pt = SEP;
2670 if (namebuf[len-1] != SEP)
2671 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002672 strcpy(namebuf + len, "*.*");
2673
Neal Norwitz6c913782007-10-14 03:23:09 +00002674 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002675 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002676 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002677 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002678
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002679 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2680 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002681 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002682 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2683 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2684 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002685
2686 if (rc != NO_ERROR) {
2687 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002688 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002689 }
2690
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002691 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002692 do {
2693 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002694 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002695 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002696
2697 strcpy(namebuf, ep.achName);
2698
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002699 /* Leave Case of Name Alone -- In Native Form */
2700 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002701
Christian Heimes72b710a2008-05-26 13:28:38 +00002702 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002703 if (v == NULL) {
2704 Py_DECREF(d);
2705 d = NULL;
2706 break;
2707 }
2708 if (PyList_Append(d, v) != 0) {
2709 Py_DECREF(v);
2710 Py_DECREF(d);
2711 d = NULL;
2712 break;
2713 }
2714 Py_DECREF(v);
2715 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2716 }
2717
Victor Stinnerdcb24032010-04-22 12:08:36 +00002718 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002719 return d;
2720#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002721 PyObject *oname;
2722 char *name;
2723 PyObject *d, *v;
2724 DIR *dirp;
2725 struct dirent *ep;
2726 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002727
Victor Stinner8c62be82010-05-06 00:08:46 +00002728 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002729 /* v is never read, so it does not need to be initialized yet. */
2730 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002731 arg_is_unicode = 0;
2732 PyErr_Clear();
2733 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002734 oname = NULL;
2735 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002736 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002737 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002738 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002739 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002740 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002741 Py_BEGIN_ALLOW_THREADS
2742 dirp = opendir(name);
2743 Py_END_ALLOW_THREADS
2744 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002745 return posix_error_with_allocated_filename(oname);
2746 }
2747 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002748 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002750 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 Py_DECREF(oname);
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 {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002763 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002764 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002765 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002766 Py_DECREF(d);
2767 return posix_error_with_allocated_filename(oname);
2768 }
2769 }
2770 if (ep->d_name[0] == '.' &&
2771 (NAMLEN(ep) == 1 ||
2772 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2773 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002774 if (arg_is_unicode)
2775 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2776 else
2777 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002778 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002779 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002780 break;
2781 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 if (PyList_Append(d, v) != 0) {
2783 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002784 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002785 break;
2786 }
2787 Py_DECREF(v);
2788 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002789 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002790 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002791 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002792 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002793
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002795
Tim Peters0bb44a42000-09-15 07:44:49 +00002796#endif /* which OS */
2797} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002798
Antoine Pitrou8250e232011-02-25 23:41:16 +00002799#ifdef HAVE_FDOPENDIR
2800PyDoc_STRVAR(posix_fdlistdir__doc__,
2801"fdlistdir(fd) -> list_of_strings\n\n\
2802Like listdir(), but uses a file descriptor instead.\n\
2803After succesful execution of this function, fd will be closed.");
2804
2805static PyObject *
2806posix_fdlistdir(PyObject *self, PyObject *args)
2807{
2808 PyObject *d, *v;
2809 DIR *dirp;
2810 struct dirent *ep;
2811 int fd;
2812
2813 errno = 0;
2814 if (!PyArg_ParseTuple(args, "i:fdlistdir", &fd))
2815 return NULL;
2816 Py_BEGIN_ALLOW_THREADS
2817 dirp = fdopendir(fd);
2818 Py_END_ALLOW_THREADS
2819 if (dirp == NULL) {
2820 close(fd);
2821 return posix_error();
2822 }
2823 if ((d = PyList_New(0)) == NULL) {
2824 Py_BEGIN_ALLOW_THREADS
2825 closedir(dirp);
2826 Py_END_ALLOW_THREADS
2827 return NULL;
2828 }
2829 for (;;) {
2830 errno = 0;
2831 Py_BEGIN_ALLOW_THREADS
2832 ep = readdir(dirp);
2833 Py_END_ALLOW_THREADS
2834 if (ep == NULL) {
2835 if (errno == 0) {
2836 break;
2837 } else {
2838 Py_BEGIN_ALLOW_THREADS
2839 closedir(dirp);
2840 Py_END_ALLOW_THREADS
2841 Py_DECREF(d);
2842 return posix_error();
2843 }
2844 }
2845 if (ep->d_name[0] == '.' &&
2846 (NAMLEN(ep) == 1 ||
2847 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2848 continue;
2849 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2850 if (v == NULL) {
2851 Py_CLEAR(d);
2852 break;
2853 }
2854 if (PyList_Append(d, v) != 0) {
2855 Py_DECREF(v);
2856 Py_CLEAR(d);
2857 break;
2858 }
2859 Py_DECREF(v);
2860 }
2861 Py_BEGIN_ALLOW_THREADS
2862 closedir(dirp);
2863 Py_END_ALLOW_THREADS
2864
2865 return d;
2866}
2867#endif
2868
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002869#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002870/* A helper function for abspath on win32 */
2871static PyObject *
2872posix__getfullpathname(PyObject *self, PyObject *args)
2873{
Victor Stinner8c62be82010-05-06 00:08:46 +00002874 PyObject *opath;
2875 char *path;
2876 char outbuf[MAX_PATH*2];
2877 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002878#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 PyUnicodeObject *po;
2880 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2881 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2882 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2883 Py_UNICODE *wtemp;
2884 DWORD result;
2885 PyObject *v;
2886 result = GetFullPathNameW(wpath,
2887 sizeof(woutbuf)/sizeof(woutbuf[0]),
2888 woutbuf, &wtemp);
2889 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2890 woutbufp = malloc(result * sizeof(Py_UNICODE));
2891 if (!woutbufp)
2892 return PyErr_NoMemory();
2893 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2894 }
2895 if (result)
2896 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2897 else
2898 v = win32_error_unicode("GetFullPathNameW", wpath);
2899 if (woutbufp != woutbuf)
2900 free(woutbufp);
2901 return v;
2902 }
2903 /* Drop the argument parsing error as narrow strings
2904 are also valid. */
2905 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002906
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002907#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002908 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2909 PyUnicode_FSConverter, &opath))
2910 return NULL;
2911 path = PyBytes_AsString(opath);
2912 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2913 outbuf, &temp)) {
2914 win32_error("GetFullPathName", path);
2915 Py_DECREF(opath);
2916 return NULL;
2917 }
2918 Py_DECREF(opath);
2919 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2920 return PyUnicode_Decode(outbuf, strlen(outbuf),
2921 Py_FileSystemDefaultEncoding, NULL);
2922 }
2923 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002924} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002925
Brian Curtind25aef52011-06-13 15:16:04 -05002926
Brian Curtinf5e76d02010-11-24 13:14:05 +00002927
Brian Curtind40e6f72010-07-08 21:39:08 +00002928/* A helper function for samepath on windows */
2929static PyObject *
2930posix__getfinalpathname(PyObject *self, PyObject *args)
2931{
2932 HANDLE hFile;
2933 int buf_size;
2934 wchar_t *target_path;
2935 int result_length;
2936 PyObject *result;
2937 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002938
Brian Curtin94622b02010-09-24 00:03:39 +00002939 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002940 return NULL;
2941 }
2942
2943 if(!check_GetFinalPathNameByHandle()) {
2944 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2945 NotImplementedError. */
2946 return PyErr_Format(PyExc_NotImplementedError,
2947 "GetFinalPathNameByHandle not available on this platform");
2948 }
2949
2950 hFile = CreateFileW(
2951 path,
2952 0, /* desired access */
2953 0, /* share mode */
2954 NULL, /* security attributes */
2955 OPEN_EXISTING,
2956 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2957 FILE_FLAG_BACKUP_SEMANTICS,
2958 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002959
Brian Curtind40e6f72010-07-08 21:39:08 +00002960 if(hFile == INVALID_HANDLE_VALUE) {
2961 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002962 return PyErr_Format(PyExc_RuntimeError,
2963 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002964 }
2965
2966 /* We have a good handle to the target, use it to determine the
2967 target path name. */
2968 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2969
2970 if(!buf_size)
2971 return win32_error_unicode("GetFinalPathNameByHandle", path);
2972
2973 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2974 if(!target_path)
2975 return PyErr_NoMemory();
2976
2977 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2978 buf_size, VOLUME_NAME_DOS);
2979 if(!result_length)
2980 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2981
2982 if(!CloseHandle(hFile))
2983 return win32_error_unicode("GetFinalPathNameByHandle", path);
2984
2985 target_path[result_length] = 0;
2986 result = PyUnicode_FromUnicode(target_path, result_length);
2987 free(target_path);
2988 return result;
2989
2990} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002991
2992static PyObject *
2993posix__getfileinformation(PyObject *self, PyObject *args)
2994{
2995 HANDLE hFile;
2996 BY_HANDLE_FILE_INFORMATION info;
2997 int fd;
2998
2999 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
3000 return NULL;
3001
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003002 if (!_PyVerify_fd(fd))
3003 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003004
3005 hFile = (HANDLE)_get_osfhandle(fd);
3006 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00003007 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00003008
3009 if (!GetFileInformationByHandle(hFile, &info))
3010 return win32_error("_getfileinformation", NULL);
3011
3012 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
3013 info.nFileIndexHigh,
3014 info.nFileIndexLow);
3015}
Brian Curtin9c669cc2011-06-08 18:17:18 -05003016
Brian Curtin95d028f2011-06-09 09:10:38 -05003017PyDoc_STRVAR(posix__isdir__doc__,
3018"Return true if the pathname refers to an existing directory.");
3019
Brian Curtin9c669cc2011-06-08 18:17:18 -05003020static PyObject *
3021posix__isdir(PyObject *self, PyObject *args)
3022{
3023 PyObject *opath;
3024 char *path;
3025 PyUnicodeObject *po;
3026 DWORD attributes;
3027
3028 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
3029 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
3030
3031 attributes = GetFileAttributesW(wpath);
3032 if (attributes == INVALID_FILE_ATTRIBUTES)
3033 Py_RETURN_FALSE;
3034 goto check;
3035 }
3036 /* Drop the argument parsing error as narrow strings
3037 are also valid. */
3038 PyErr_Clear();
3039
3040 if (!PyArg_ParseTuple(args, "O&:_isdir",
3041 PyUnicode_FSConverter, &opath))
3042 return NULL;
3043
3044 path = PyBytes_AsString(opath);
3045 attributes = GetFileAttributesA(path);
3046 if (attributes == INVALID_FILE_ATTRIBUTES)
3047 Py_RETURN_FALSE;
3048
3049check:
3050 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3051 Py_RETURN_TRUE;
3052 else
3053 Py_RETURN_FALSE;
3054}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003055#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003057PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003058"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003059Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003060
Barry Warsaw53699e91996-12-10 23:23:01 +00003061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003062posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003063{
Victor Stinner8c62be82010-05-06 00:08:46 +00003064 int res;
3065 PyObject *opath;
3066 char *path;
3067 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003068
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003069#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003070 PyUnicodeObject *po;
3071 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
3072 Py_BEGIN_ALLOW_THREADS
3073 /* PyUnicode_AS_UNICODE OK without thread lock as
3074 it is a simple dereference. */
3075 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
3076 Py_END_ALLOW_THREADS
3077 if (!res)
3078 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
3079 Py_INCREF(Py_None);
3080 return Py_None;
3081 }
3082 /* Drop the argument parsing error as narrow strings
3083 are also valid. */
3084 PyErr_Clear();
3085 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3086 PyUnicode_FSConverter, &opath, &mode))
3087 return NULL;
3088 path = PyBytes_AsString(opath);
3089 Py_BEGIN_ALLOW_THREADS
3090 /* PyUnicode_AS_UNICODE OK without thread lock as
3091 it is a simple dereference. */
3092 res = CreateDirectoryA(path, NULL);
3093 Py_END_ALLOW_THREADS
3094 if (!res) {
3095 win32_error("mkdir", path);
3096 Py_DECREF(opath);
3097 return NULL;
3098 }
3099 Py_DECREF(opath);
3100 Py_INCREF(Py_None);
3101 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003102#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003103
Victor Stinner8c62be82010-05-06 00:08:46 +00003104 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
3105 PyUnicode_FSConverter, &opath, &mode))
3106 return NULL;
3107 path = PyBytes_AsString(opath);
3108 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00003109#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003111#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003112 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003113#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003114 Py_END_ALLOW_THREADS
3115 if (res < 0)
3116 return posix_error_with_allocated_filename(opath);
3117 Py_DECREF(opath);
3118 Py_INCREF(Py_None);
3119 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003120#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003121}
3122
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003123
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003124/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3125#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003126#include <sys/resource.h>
3127#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003128
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003129
3130#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003131PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003132"nice(inc) -> new_priority\n\n\
3133Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003134
Barry Warsaw53699e91996-12-10 23:23:01 +00003135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003136posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003137{
Victor Stinner8c62be82010-05-06 00:08:46 +00003138 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003139
Victor Stinner8c62be82010-05-06 00:08:46 +00003140 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3141 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003142
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 /* There are two flavours of 'nice': one that returns the new
3144 priority (as required by almost all standards out there) and the
3145 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3146 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003147
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 If we are of the nice family that returns the new priority, we
3149 need to clear errno before the call, and check if errno is filled
3150 before calling posix_error() on a returnvalue of -1, because the
3151 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003152
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 errno = 0;
3154 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003155#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003156 if (value == 0)
3157 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003158#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 if (value == -1 && errno != 0)
3160 /* either nice() or getpriority() returned an error */
3161 return posix_error();
3162 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003163}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003164#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003165
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003166
3167#ifdef HAVE_GETPRIORITY
3168PyDoc_STRVAR(posix_getpriority__doc__,
3169"getpriority(which, who) -> current_priority\n\n\
3170Get program scheduling priority.");
3171
3172static PyObject *
3173posix_getpriority(PyObject *self, PyObject *args)
3174{
3175 int which, who, retval;
3176
3177 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3178 return NULL;
3179 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003180 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003181 if (errno != 0)
3182 return posix_error();
3183 return PyLong_FromLong((long)retval);
3184}
3185#endif /* HAVE_GETPRIORITY */
3186
3187
3188#ifdef HAVE_SETPRIORITY
3189PyDoc_STRVAR(posix_setpriority__doc__,
3190"setpriority(which, who, prio) -> None\n\n\
3191Set program scheduling priority.");
3192
3193static PyObject *
3194posix_setpriority(PyObject *self, PyObject *args)
3195{
3196 int which, who, prio, retval;
3197
3198 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3199 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003200 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003201 if (retval == -1)
3202 return posix_error();
3203 Py_RETURN_NONE;
3204}
3205#endif /* HAVE_SETPRIORITY */
3206
3207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003208PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003209"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003210Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211
Barry Warsaw53699e91996-12-10 23:23:01 +00003212static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003213posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003214{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003215#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 PyObject *o1, *o2;
3217 char *p1, *p2;
3218 BOOL result;
3219 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
3220 goto error;
3221 if (!convert_to_unicode(&o1))
3222 goto error;
3223 if (!convert_to_unicode(&o2)) {
3224 Py_DECREF(o1);
3225 goto error;
3226 }
3227 Py_BEGIN_ALLOW_THREADS
3228 result = MoveFileW(PyUnicode_AsUnicode(o1),
3229 PyUnicode_AsUnicode(o2));
3230 Py_END_ALLOW_THREADS
3231 Py_DECREF(o1);
3232 Py_DECREF(o2);
3233 if (!result)
3234 return win32_error("rename", NULL);
3235 Py_INCREF(Py_None);
3236 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003237error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003238 PyErr_Clear();
3239 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3240 return NULL;
3241 Py_BEGIN_ALLOW_THREADS
3242 result = MoveFileA(p1, p2);
3243 Py_END_ALLOW_THREADS
3244 if (!result)
3245 return win32_error("rename", NULL);
3246 Py_INCREF(Py_None);
3247 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003248#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003250#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003251}
3252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003254PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003255"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003256Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003257
Barry Warsaw53699e91996-12-10 23:23:01 +00003258static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003259posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003260{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003261#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003262 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003263#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003265#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003266}
3267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003269PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003270"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003271Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003272
Barry Warsaw53699e91996-12-10 23:23:01 +00003273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003274posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003275{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003276#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003277 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003278#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003279 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003280#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003281}
3282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003283
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003284#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003285PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003286"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003287Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003288
Barry Warsaw53699e91996-12-10 23:23:01 +00003289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003290posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003291{
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003293#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003294 wchar_t *command;
3295 if (!PyArg_ParseTuple(args, "u:system", &command))
3296 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003297
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 Py_BEGIN_ALLOW_THREADS
3299 sts = _wsystem(command);
3300 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003301#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 PyObject *command_obj;
3303 char *command;
3304 if (!PyArg_ParseTuple(args, "O&:system",
3305 PyUnicode_FSConverter, &command_obj))
3306 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003307
Victor Stinner8c62be82010-05-06 00:08:46 +00003308 command = PyBytes_AsString(command_obj);
3309 Py_BEGIN_ALLOW_THREADS
3310 sts = system(command);
3311 Py_END_ALLOW_THREADS
3312 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003313#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003315}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003316#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003317
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003318
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003319PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003320"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003321Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003322
Barry Warsaw53699e91996-12-10 23:23:01 +00003323static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003324posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003325{
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 int i;
3327 if (!PyArg_ParseTuple(args, "i:umask", &i))
3328 return NULL;
3329 i = (int)umask(i);
3330 if (i < 0)
3331 return posix_error();
3332 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003333}
3334
Brian Curtind40e6f72010-07-08 21:39:08 +00003335#ifdef MS_WINDOWS
3336
3337/* override the default DeleteFileW behavior so that directory
3338symlinks can be removed with this function, the same as with
3339Unix symlinks */
3340BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3341{
3342 WIN32_FILE_ATTRIBUTE_DATA info;
3343 WIN32_FIND_DATAW find_data;
3344 HANDLE find_data_handle;
3345 int is_directory = 0;
3346 int is_link = 0;
3347
3348 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3349 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003350
Brian Curtind40e6f72010-07-08 21:39:08 +00003351 /* Get WIN32_FIND_DATA structure for the path to determine if
3352 it is a symlink */
3353 if(is_directory &&
3354 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3355 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3356
3357 if(find_data_handle != INVALID_HANDLE_VALUE) {
3358 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3359 FindClose(find_data_handle);
3360 }
3361 }
3362 }
3363
3364 if (is_directory && is_link)
3365 return RemoveDirectoryW(lpFileName);
3366
3367 return DeleteFileW(lpFileName);
3368}
3369#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003371PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003372"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003373Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003375PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003376"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003377Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003378
Barry Warsaw53699e91996-12-10 23:23:01 +00003379static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003380posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003381{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003382#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003383 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3384 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003385#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003386 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003387#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003388}
3389
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003390
Guido van Rossumb6775db1994-08-01 11:34:53 +00003391#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003392PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003393"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003394Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003395
Barry Warsaw53699e91996-12-10 23:23:01 +00003396static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003397posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003398{
Victor Stinner8c62be82010-05-06 00:08:46 +00003399 struct utsname u;
3400 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003401
Victor Stinner8c62be82010-05-06 00:08:46 +00003402 Py_BEGIN_ALLOW_THREADS
3403 res = uname(&u);
3404 Py_END_ALLOW_THREADS
3405 if (res < 0)
3406 return posix_error();
3407 return Py_BuildValue("(sssss)",
3408 u.sysname,
3409 u.nodename,
3410 u.release,
3411 u.version,
3412 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003413}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003414#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003415
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003416static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003417extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003418{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003419 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003420 if (PyFloat_Check(t)) {
3421 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003422 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003423 if (!intobj)
3424 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003425#if SIZEOF_TIME_T > SIZEOF_LONG
3426 intval = PyLong_AsUnsignedLongLongMask(intobj);
3427#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003428 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003429#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 Py_DECREF(intobj);
3431 if (intval == -1 && PyErr_Occurred())
3432 return -1;
3433 *sec = intval;
3434 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3435 if (*usec < 0)
3436 /* If rounding gave us a negative number,
3437 truncate. */
3438 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003439 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003441#if SIZEOF_TIME_T > SIZEOF_LONG
3442 intval = PyLong_AsUnsignedLongLongMask(t);
3443#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003445#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 if (intval == -1 && PyErr_Occurred())
3447 return -1;
3448 *sec = intval;
3449 *usec = 0;
3450 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003451}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003453PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003454"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003455utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003456Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003457second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003458
Barry Warsaw53699e91996-12-10 23:23:01 +00003459static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003460posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003461{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003462#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 PyObject *arg;
3464 PyUnicodeObject *obwpath;
3465 wchar_t *wpath = NULL;
3466 PyObject *oapath;
3467 char *apath;
3468 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003469 time_t atimesec, mtimesec;
3470 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003471 FILETIME atime, mtime;
3472 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003473
Victor Stinner8c62be82010-05-06 00:08:46 +00003474 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3475 wpath = PyUnicode_AS_UNICODE(obwpath);
3476 Py_BEGIN_ALLOW_THREADS
3477 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3478 NULL, OPEN_EXISTING,
3479 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3480 Py_END_ALLOW_THREADS
3481 if (hFile == INVALID_HANDLE_VALUE)
3482 return win32_error_unicode("utime", wpath);
3483 } else
3484 /* Drop the argument parsing error as narrow strings
3485 are also valid. */
3486 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003487
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 if (!wpath) {
3489 if (!PyArg_ParseTuple(args, "O&O:utime",
3490 PyUnicode_FSConverter, &oapath, &arg))
3491 return NULL;
3492 apath = PyBytes_AsString(oapath);
3493 Py_BEGIN_ALLOW_THREADS
3494 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3495 NULL, OPEN_EXISTING,
3496 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3497 Py_END_ALLOW_THREADS
3498 if (hFile == INVALID_HANDLE_VALUE) {
3499 win32_error("utime", apath);
3500 Py_DECREF(oapath);
3501 return NULL;
3502 }
3503 Py_DECREF(oapath);
3504 }
3505
3506 if (arg == Py_None) {
3507 SYSTEMTIME now;
3508 GetSystemTime(&now);
3509 if (!SystemTimeToFileTime(&now, &mtime) ||
3510 !SystemTimeToFileTime(&now, &atime)) {
3511 win32_error("utime", NULL);
3512 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003513 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 }
3515 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3516 PyErr_SetString(PyExc_TypeError,
3517 "utime() arg 2 must be a tuple (atime, mtime)");
3518 goto done;
3519 }
3520 else {
3521 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3522 &atimesec, &ausec) == -1)
3523 goto done;
3524 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3525 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3526 &mtimesec, &musec) == -1)
3527 goto done;
3528 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3529 }
3530 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3531 /* Avoid putting the file name into the error here,
3532 as that may confuse the user into believing that
3533 something is wrong with the file, when it also
3534 could be the time stamp that gives a problem. */
3535 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003536 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003537 }
3538 Py_INCREF(Py_None);
3539 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003540done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003541 CloseHandle(hFile);
3542 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003543#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003544
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 PyObject *opath;
3546 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003547 time_t atime, mtime;
3548 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003549 int res;
3550 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003551
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003552#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003554#define ATIME buf[0].tv_sec
3555#define MTIME buf[1].tv_sec
3556#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003557/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003558 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003559#define ATIME buf.actime
3560#define MTIME buf.modtime
3561#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003562#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003564#define ATIME buf[0]
3565#define MTIME buf[1]
3566#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003567#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003568
Mark Hammond817c9292003-12-03 01:22:38 +00003569
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 if (!PyArg_ParseTuple(args, "O&O:utime",
3571 PyUnicode_FSConverter, &opath, &arg))
3572 return NULL;
3573 path = PyBytes_AsString(opath);
3574 if (arg == Py_None) {
3575 /* optional time values not given */
3576 Py_BEGIN_ALLOW_THREADS
3577 res = utime(path, NULL);
3578 Py_END_ALLOW_THREADS
3579 }
3580 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3581 PyErr_SetString(PyExc_TypeError,
3582 "utime() arg 2 must be a tuple (atime, mtime)");
3583 Py_DECREF(opath);
3584 return NULL;
3585 }
3586 else {
3587 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3588 &atime, &ausec) == -1) {
3589 Py_DECREF(opath);
3590 return NULL;
3591 }
3592 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3593 &mtime, &musec) == -1) {
3594 Py_DECREF(opath);
3595 return NULL;
3596 }
3597 ATIME = atime;
3598 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003599#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003600 buf[0].tv_usec = ausec;
3601 buf[1].tv_usec = musec;
3602 Py_BEGIN_ALLOW_THREADS
3603 res = utimes(path, buf);
3604 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003605#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003606 Py_BEGIN_ALLOW_THREADS
3607 res = utime(path, UTIME_ARG);
3608 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003609#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 }
3611 if (res < 0) {
3612 return posix_error_with_allocated_filename(opath);
3613 }
3614 Py_DECREF(opath);
3615 Py_INCREF(Py_None);
3616 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003617#undef UTIME_ARG
3618#undef ATIME
3619#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003620#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003621}
3622
Ross Lagerwall7807c352011-03-17 20:20:30 +02003623#ifdef HAVE_FUTIMES
3624PyDoc_STRVAR(posix_futimes__doc__,
3625"futimes(fd, (atime, mtime))\n\
3626futimes(fd, None)\n\n\
3627Set the access and modified time of the file specified by the file\n\
3628descriptor fd to the given values. If the second form is used, set the\n\
3629access and modified times to the current time.");
3630
3631static PyObject *
3632posix_futimes(PyObject *self, PyObject *args)
3633{
3634 int res, fd;
3635 PyObject* arg;
3636 struct timeval buf[2];
3637 long ausec, musec;
3638
3639 if (!PyArg_ParseTuple(args, "iO:futimes", &fd, &arg))
3640 return NULL;
3641
3642 if (arg == Py_None) {
3643 /* optional time values not given */
3644 Py_BEGIN_ALLOW_THREADS
3645 res = futimes(fd, NULL);
3646 Py_END_ALLOW_THREADS
3647 }
3648 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3649 PyErr_SetString(PyExc_TypeError,
3650 "futimes() arg 2 must be a tuple (atime, mtime)");
3651 return NULL;
3652 }
3653 else {
3654 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3655 &(buf[0].tv_sec), &ausec) == -1) {
3656 return NULL;
3657 }
3658 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3659 &(buf[1].tv_sec), &musec) == -1) {
3660 return NULL;
3661 }
3662 buf[0].tv_usec = ausec;
3663 buf[1].tv_usec = musec;
3664 Py_BEGIN_ALLOW_THREADS
3665 res = futimes(fd, buf);
3666 Py_END_ALLOW_THREADS
3667 }
3668 if (res < 0)
3669 return posix_error();
3670 Py_RETURN_NONE;
3671}
3672#endif
3673
3674#ifdef HAVE_LUTIMES
3675PyDoc_STRVAR(posix_lutimes__doc__,
3676"lutimes(path, (atime, mtime))\n\
3677lutimes(path, None)\n\n\
3678Like utime(), but if path is a symbolic link, it is not dereferenced.");
3679
3680static PyObject *
3681posix_lutimes(PyObject *self, PyObject *args)
3682{
3683 PyObject *opath, *arg;
3684 const char *path;
3685 int res;
3686 struct timeval buf[2];
3687 long ausec, musec;
3688
3689 if (!PyArg_ParseTuple(args, "O&O:lutimes",
3690 PyUnicode_FSConverter, &opath, &arg))
3691 return NULL;
3692 path = PyBytes_AsString(opath);
3693 if (arg == Py_None) {
3694 /* optional time values not given */
3695 Py_BEGIN_ALLOW_THREADS
3696 res = lutimes(path, NULL);
3697 Py_END_ALLOW_THREADS
3698 }
3699 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3700 PyErr_SetString(PyExc_TypeError,
3701 "lutimes() arg 2 must be a tuple (atime, mtime)");
3702 Py_DECREF(opath);
3703 return NULL;
3704 }
3705 else {
3706 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3707 &(buf[0].tv_sec), &ausec) == -1) {
3708 Py_DECREF(opath);
3709 return NULL;
3710 }
3711 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3712 &(buf[1].tv_sec), &musec) == -1) {
3713 Py_DECREF(opath);
3714 return NULL;
3715 }
3716 buf[0].tv_usec = ausec;
3717 buf[1].tv_usec = musec;
3718 Py_BEGIN_ALLOW_THREADS
3719 res = lutimes(path, buf);
3720 Py_END_ALLOW_THREADS
3721 }
3722 Py_DECREF(opath);
3723 if (res < 0)
3724 return posix_error();
3725 Py_RETURN_NONE;
3726}
3727#endif
3728
3729#ifdef HAVE_FUTIMENS
3730PyDoc_STRVAR(posix_futimens__doc__,
3731"futimens(fd, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec))\n\
3732futimens(fd, None, None)\n\n\
3733Updates the timestamps of a file specified by the file descriptor fd, with\n\
3734nanosecond precision.\n\
3735The second form sets atime and mtime to the current time.\n\
3736If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
3737current time.\n\
3738If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
3739
3740static PyObject *
3741posix_futimens(PyObject *self, PyObject *args)
3742{
3743 int res, fd;
3744 PyObject *atime, *mtime;
3745 struct timespec buf[2];
3746
3747 if (!PyArg_ParseTuple(args, "iOO:futimens",
3748 &fd, &atime, &mtime))
3749 return NULL;
3750 if (atime == Py_None && mtime == Py_None) {
3751 /* optional time values not given */
3752 Py_BEGIN_ALLOW_THREADS
3753 res = futimens(fd, NULL);
3754 Py_END_ALLOW_THREADS
3755 }
3756 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
3757 PyErr_SetString(PyExc_TypeError,
3758 "futimens() arg 2 must be a tuple (atime_sec, atime_nsec)");
3759 return NULL;
3760 }
3761 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
3762 PyErr_SetString(PyExc_TypeError,
3763 "futimens() arg 3 must be a tuple (mtime_sec, mtime_nsec)");
3764 return NULL;
3765 }
3766 else {
3767 if (!PyArg_ParseTuple(atime, "ll:futimens",
3768 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
3769 return NULL;
3770 }
3771 if (!PyArg_ParseTuple(mtime, "ll:futimens",
3772 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
3773 return NULL;
3774 }
3775 Py_BEGIN_ALLOW_THREADS
3776 res = futimens(fd, buf);
3777 Py_END_ALLOW_THREADS
3778 }
3779 if (res < 0)
3780 return posix_error();
3781 Py_RETURN_NONE;
3782}
3783#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003784
Guido van Rossum3b066191991-06-04 19:40:25 +00003785/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003786
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003787PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003788"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003789Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003790
Barry Warsaw53699e91996-12-10 23:23:01 +00003791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003792posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003793{
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 int sts;
3795 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3796 return NULL;
3797 _exit(sts);
3798 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003799}
3800
Martin v. Löwis114619e2002-10-07 06:44:21 +00003801#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3802static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003803free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003804{
Victor Stinner8c62be82010-05-06 00:08:46 +00003805 Py_ssize_t i;
3806 for (i = 0; i < count; i++)
3807 PyMem_Free(array[i]);
3808 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003809}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003810
Antoine Pitrou69f71142009-05-24 21:25:49 +00003811static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003812int fsconvert_strdup(PyObject *o, char**out)
3813{
Victor Stinner8c62be82010-05-06 00:08:46 +00003814 PyObject *bytes;
3815 Py_ssize_t size;
3816 if (!PyUnicode_FSConverter(o, &bytes))
3817 return 0;
3818 size = PyBytes_GET_SIZE(bytes);
3819 *out = PyMem_Malloc(size+1);
3820 if (!*out)
3821 return 0;
3822 memcpy(*out, PyBytes_AsString(bytes), size+1);
3823 Py_DECREF(bytes);
3824 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003825}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003826#endif
3827
Ross Lagerwall7807c352011-03-17 20:20:30 +02003828#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00003829static char**
3830parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3831{
Victor Stinner8c62be82010-05-06 00:08:46 +00003832 char **envlist;
3833 Py_ssize_t i, pos, envc;
3834 PyObject *keys=NULL, *vals=NULL;
3835 PyObject *key, *val, *key2, *val2;
3836 char *p, *k, *v;
3837 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003838
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 i = PyMapping_Size(env);
3840 if (i < 0)
3841 return NULL;
3842 envlist = PyMem_NEW(char *, i + 1);
3843 if (envlist == NULL) {
3844 PyErr_NoMemory();
3845 return NULL;
3846 }
3847 envc = 0;
3848 keys = PyMapping_Keys(env);
3849 vals = PyMapping_Values(env);
3850 if (!keys || !vals)
3851 goto error;
3852 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3853 PyErr_Format(PyExc_TypeError,
3854 "env.keys() or env.values() is not a list");
3855 goto error;
3856 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003857
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 for (pos = 0; pos < i; pos++) {
3859 key = PyList_GetItem(keys, pos);
3860 val = PyList_GetItem(vals, pos);
3861 if (!key || !val)
3862 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003863
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 if (PyUnicode_FSConverter(key, &key2) == 0)
3865 goto error;
3866 if (PyUnicode_FSConverter(val, &val2) == 0) {
3867 Py_DECREF(key2);
3868 goto error;
3869 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003870
3871#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003872 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3873 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003874#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003875 k = PyBytes_AsString(key2);
3876 v = PyBytes_AsString(val2);
3877 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003878
Victor Stinner8c62be82010-05-06 00:08:46 +00003879 p = PyMem_NEW(char, len);
3880 if (p == NULL) {
3881 PyErr_NoMemory();
3882 Py_DECREF(key2);
3883 Py_DECREF(val2);
3884 goto error;
3885 }
3886 PyOS_snprintf(p, len, "%s=%s", k, v);
3887 envlist[envc++] = p;
3888 Py_DECREF(key2);
3889 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003890#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003891 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003892#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 }
3894 Py_DECREF(vals);
3895 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003896
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 envlist[envc] = 0;
3898 *envc_ptr = envc;
3899 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003900
3901error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003902 Py_XDECREF(keys);
3903 Py_XDECREF(vals);
3904 while (--envc >= 0)
3905 PyMem_DEL(envlist[envc]);
3906 PyMem_DEL(envlist);
3907 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003908}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003909
Ross Lagerwall7807c352011-03-17 20:20:30 +02003910static char**
3911parse_arglist(PyObject* argv, Py_ssize_t *argc)
3912{
3913 int i;
3914 char **argvlist = PyMem_NEW(char *, *argc+1);
3915 if (argvlist == NULL) {
3916 PyErr_NoMemory();
3917 return NULL;
3918 }
3919 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003920 PyObject* item = PySequence_ITEM(argv, i);
3921 if (item == NULL)
3922 goto fail;
3923 if (!fsconvert_strdup(item, &argvlist[i])) {
3924 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003925 goto fail;
3926 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003927 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02003928 }
3929 argvlist[*argc] = NULL;
3930 return argvlist;
3931fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02003932 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02003933 free_string_array(argvlist, *argc);
3934 return NULL;
3935}
3936#endif
3937
3938#ifdef HAVE_EXECV
3939PyDoc_STRVAR(posix_execv__doc__,
3940"execv(path, args)\n\n\
3941Execute an executable path with arguments, replacing current process.\n\
3942\n\
3943 path: path of executable file\n\
3944 args: tuple or list of strings");
3945
3946static PyObject *
3947posix_execv(PyObject *self, PyObject *args)
3948{
3949 PyObject *opath;
3950 char *path;
3951 PyObject *argv;
3952 char **argvlist;
3953 Py_ssize_t argc;
3954
3955 /* execv has two arguments: (path, argv), where
3956 argv is a list or tuple of strings. */
3957
3958 if (!PyArg_ParseTuple(args, "O&O:execv",
3959 PyUnicode_FSConverter,
3960 &opath, &argv))
3961 return NULL;
3962 path = PyBytes_AsString(opath);
3963 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
3964 PyErr_SetString(PyExc_TypeError,
3965 "execv() arg 2 must be a tuple or list");
3966 Py_DECREF(opath);
3967 return NULL;
3968 }
3969 argc = PySequence_Size(argv);
3970 if (argc < 1) {
3971 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3972 Py_DECREF(opath);
3973 return NULL;
3974 }
3975
3976 argvlist = parse_arglist(argv, &argc);
3977 if (argvlist == NULL) {
3978 Py_DECREF(opath);
3979 return NULL;
3980 }
3981
3982 execv(path, argvlist);
3983
3984 /* If we get here it's definitely an error */
3985
3986 free_string_array(argvlist, argc);
3987 Py_DECREF(opath);
3988 return posix_error();
3989}
3990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003991PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003992"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003993Execute a path with arguments and environment, replacing current process.\n\
3994\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 path: path of executable file\n\
3996 args: tuple or list of arguments\n\
3997 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003998
Barry Warsaw53699e91996-12-10 23:23:01 +00003999static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004000posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004001{
Victor Stinner8c62be82010-05-06 00:08:46 +00004002 PyObject *opath;
4003 char *path;
4004 PyObject *argv, *env;
4005 char **argvlist;
4006 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004007 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004008
Victor Stinner8c62be82010-05-06 00:08:46 +00004009 /* execve has three arguments: (path, argv, env), where
4010 argv is a list or tuple of strings and env is a dictionary
4011 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004012
Victor Stinner8c62be82010-05-06 00:08:46 +00004013 if (!PyArg_ParseTuple(args, "O&OO:execve",
4014 PyUnicode_FSConverter,
4015 &opath, &argv, &env))
4016 return NULL;
4017 path = PyBytes_AsString(opath);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004018 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004019 PyErr_SetString(PyExc_TypeError,
4020 "execve() arg 2 must be a tuple or list");
4021 goto fail_0;
4022 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004023 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004024 if (!PyMapping_Check(env)) {
4025 PyErr_SetString(PyExc_TypeError,
4026 "execve() arg 3 must be a mapping object");
4027 goto fail_0;
4028 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004029
Ross Lagerwall7807c352011-03-17 20:20:30 +02004030 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 goto fail_0;
4033 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004034
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 envlist = parse_envlist(env, &envc);
4036 if (envlist == NULL)
4037 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004038
Victor Stinner8c62be82010-05-06 00:08:46 +00004039 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00004040
Victor Stinner8c62be82010-05-06 00:08:46 +00004041 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004042
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004044
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 while (--envc >= 0)
4046 PyMem_DEL(envlist[envc]);
4047 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004048 fail_1:
Ross Lagerwall7807c352011-03-17 20:20:30 +02004049 free_string_array(argvlist, argc);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004050 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004051 Py_DECREF(opath);
4052 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004053}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004054#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004055
Ross Lagerwall7807c352011-03-17 20:20:30 +02004056#ifdef HAVE_FEXECVE
4057PyDoc_STRVAR(posix_fexecve__doc__,
4058"fexecve(fd, args, env)\n\n\
4059Execute the program specified by a file descriptor with arguments and\n\
4060environment, replacing the current process.\n\
4061\n\
4062 fd: file descriptor of executable\n\
4063 args: tuple or list of arguments\n\
4064 env: dictionary of strings mapping to strings");
4065
4066static PyObject *
4067posix_fexecve(PyObject *self, PyObject *args)
4068{
4069 int fd;
4070 PyObject *argv, *env;
4071 char **argvlist;
4072 char **envlist;
4073 Py_ssize_t argc, envc;
4074
4075 if (!PyArg_ParseTuple(args, "iOO:fexecve",
4076 &fd, &argv, &env))
4077 return NULL;
4078 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4079 PyErr_SetString(PyExc_TypeError,
4080 "fexecve() arg 2 must be a tuple or list");
4081 return NULL;
4082 }
4083 argc = PySequence_Size(argv);
4084 if (!PyMapping_Check(env)) {
4085 PyErr_SetString(PyExc_TypeError,
4086 "fexecve() arg 3 must be a mapping object");
4087 return NULL;
4088 }
4089
4090 argvlist = parse_arglist(argv, &argc);
4091 if (argvlist == NULL)
4092 return NULL;
4093
4094 envlist = parse_envlist(env, &envc);
4095 if (envlist == NULL)
4096 goto fail;
4097
4098 fexecve(fd, argvlist, envlist);
4099
4100 /* If we get here it's definitely an error */
4101
4102 (void) posix_error();
4103
4104 while (--envc >= 0)
4105 PyMem_DEL(envlist[envc]);
4106 PyMem_DEL(envlist);
4107 fail:
4108 free_string_array(argvlist, argc);
4109 return NULL;
4110}
4111#endif /* HAVE_FEXECVE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004112
Guido van Rossuma1065681999-01-25 23:20:23 +00004113#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004114PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004115"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004116Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004117\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004118 mode: mode of process creation\n\
4119 path: path of executable file\n\
4120 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004121
4122static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004123posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004124{
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 PyObject *opath;
4126 char *path;
4127 PyObject *argv;
4128 char **argvlist;
4129 int mode, i;
4130 Py_ssize_t argc;
4131 Py_intptr_t spawnval;
4132 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004133
Victor Stinner8c62be82010-05-06 00:08:46 +00004134 /* spawnv has three arguments: (mode, path, argv), where
4135 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004136
Victor Stinner8c62be82010-05-06 00:08:46 +00004137 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4138 PyUnicode_FSConverter,
4139 &opath, &argv))
4140 return NULL;
4141 path = PyBytes_AsString(opath);
4142 if (PyList_Check(argv)) {
4143 argc = PyList_Size(argv);
4144 getitem = PyList_GetItem;
4145 }
4146 else if (PyTuple_Check(argv)) {
4147 argc = PyTuple_Size(argv);
4148 getitem = PyTuple_GetItem;
4149 }
4150 else {
4151 PyErr_SetString(PyExc_TypeError,
4152 "spawnv() arg 2 must be a tuple or list");
4153 Py_DECREF(opath);
4154 return NULL;
4155 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004156
Victor Stinner8c62be82010-05-06 00:08:46 +00004157 argvlist = PyMem_NEW(char *, argc+1);
4158 if (argvlist == NULL) {
4159 Py_DECREF(opath);
4160 return PyErr_NoMemory();
4161 }
4162 for (i = 0; i < argc; i++) {
4163 if (!fsconvert_strdup((*getitem)(argv, i),
4164 &argvlist[i])) {
4165 free_string_array(argvlist, i);
4166 PyErr_SetString(
4167 PyExc_TypeError,
4168 "spawnv() arg 2 must contain only strings");
4169 Py_DECREF(opath);
4170 return NULL;
4171 }
4172 }
4173 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004174
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004175#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 Py_BEGIN_ALLOW_THREADS
4177 spawnval = spawnv(mode, path, argvlist);
4178 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004179#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004180 if (mode == _OLD_P_OVERLAY)
4181 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00004182
Victor Stinner8c62be82010-05-06 00:08:46 +00004183 Py_BEGIN_ALLOW_THREADS
4184 spawnval = _spawnv(mode, path, argvlist);
4185 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004186#endif
Tim Peters5aa91602002-01-30 05:46:57 +00004187
Victor Stinner8c62be82010-05-06 00:08:46 +00004188 free_string_array(argvlist, argc);
4189 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00004190
Victor Stinner8c62be82010-05-06 00:08:46 +00004191 if (spawnval == -1)
4192 return posix_error();
4193 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004194#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004195 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004196#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004197 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004198#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004199}
4200
4201
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004202PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004203"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004204Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004205\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004206 mode: mode of process creation\n\
4207 path: path of executable file\n\
4208 args: tuple or list of arguments\n\
4209 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004210
4211static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004212posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004213{
Victor Stinner8c62be82010-05-06 00:08:46 +00004214 PyObject *opath;
4215 char *path;
4216 PyObject *argv, *env;
4217 char **argvlist;
4218 char **envlist;
4219 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004220 int mode;
4221 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004222 Py_intptr_t spawnval;
4223 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4224 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00004225
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 /* spawnve has four arguments: (mode, path, argv, env), where
4227 argv is a list or tuple of strings and env is a dictionary
4228 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004229
Victor Stinner8c62be82010-05-06 00:08:46 +00004230 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
4231 PyUnicode_FSConverter,
4232 &opath, &argv, &env))
4233 return NULL;
4234 path = PyBytes_AsString(opath);
4235 if (PyList_Check(argv)) {
4236 argc = PyList_Size(argv);
4237 getitem = PyList_GetItem;
4238 }
4239 else if (PyTuple_Check(argv)) {
4240 argc = PyTuple_Size(argv);
4241 getitem = PyTuple_GetItem;
4242 }
4243 else {
4244 PyErr_SetString(PyExc_TypeError,
4245 "spawnve() arg 2 must be a tuple or list");
4246 goto fail_0;
4247 }
4248 if (!PyMapping_Check(env)) {
4249 PyErr_SetString(PyExc_TypeError,
4250 "spawnve() arg 3 must be a mapping object");
4251 goto fail_0;
4252 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004253
Victor Stinner8c62be82010-05-06 00:08:46 +00004254 argvlist = PyMem_NEW(char *, argc+1);
4255 if (argvlist == NULL) {
4256 PyErr_NoMemory();
4257 goto fail_0;
4258 }
4259 for (i = 0; i < argc; i++) {
4260 if (!fsconvert_strdup((*getitem)(argv, i),
4261 &argvlist[i]))
4262 {
4263 lastarg = i;
4264 goto fail_1;
4265 }
4266 }
4267 lastarg = argc;
4268 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00004269
Victor Stinner8c62be82010-05-06 00:08:46 +00004270 envlist = parse_envlist(env, &envc);
4271 if (envlist == NULL)
4272 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00004273
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004274#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004275 Py_BEGIN_ALLOW_THREADS
4276 spawnval = spawnve(mode, path, argvlist, envlist);
4277 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004278#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004279 if (mode == _OLD_P_OVERLAY)
4280 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00004281
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 Py_BEGIN_ALLOW_THREADS
4283 spawnval = _spawnve(mode, path, argvlist, envlist);
4284 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004285#endif
Tim Peters25059d32001-12-07 20:35:43 +00004286
Victor Stinner8c62be82010-05-06 00:08:46 +00004287 if (spawnval == -1)
4288 (void) posix_error();
4289 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00004290#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00004291 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004292#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004293 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00004294#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00004295
Victor Stinner8c62be82010-05-06 00:08:46 +00004296 while (--envc >= 0)
4297 PyMem_DEL(envlist[envc]);
4298 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00004299 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004300 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004301 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 Py_DECREF(opath);
4303 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00004304}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004305
4306/* OS/2 supports spawnvp & spawnvpe natively */
4307#if defined(PYOS_OS2)
4308PyDoc_STRVAR(posix_spawnvp__doc__,
4309"spawnvp(mode, file, args)\n\n\
4310Execute the program 'file' in a new process, using the environment\n\
4311search path to find the file.\n\
4312\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 mode: mode of process creation\n\
4314 file: executable file name\n\
4315 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004316
4317static PyObject *
4318posix_spawnvp(PyObject *self, PyObject *args)
4319{
Victor Stinner8c62be82010-05-06 00:08:46 +00004320 PyObject *opath;
4321 char *path;
4322 PyObject *argv;
4323 char **argvlist;
4324 int mode, i, argc;
4325 Py_intptr_t spawnval;
4326 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004327
Victor Stinner8c62be82010-05-06 00:08:46 +00004328 /* spawnvp has three arguments: (mode, path, argv), where
4329 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004330
Victor Stinner8c62be82010-05-06 00:08:46 +00004331 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
4332 PyUnicode_FSConverter,
4333 &opath, &argv))
4334 return NULL;
4335 path = PyBytes_AsString(opath);
4336 if (PyList_Check(argv)) {
4337 argc = PyList_Size(argv);
4338 getitem = PyList_GetItem;
4339 }
4340 else if (PyTuple_Check(argv)) {
4341 argc = PyTuple_Size(argv);
4342 getitem = PyTuple_GetItem;
4343 }
4344 else {
4345 PyErr_SetString(PyExc_TypeError,
4346 "spawnvp() arg 2 must be a tuple or list");
4347 Py_DECREF(opath);
4348 return NULL;
4349 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004350
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 argvlist = PyMem_NEW(char *, argc+1);
4352 if (argvlist == NULL) {
4353 Py_DECREF(opath);
4354 return PyErr_NoMemory();
4355 }
4356 for (i = 0; i < argc; i++) {
4357 if (!fsconvert_strdup((*getitem)(argv, i),
4358 &argvlist[i])) {
4359 free_string_array(argvlist, i);
4360 PyErr_SetString(
4361 PyExc_TypeError,
4362 "spawnvp() arg 2 must contain only strings");
4363 Py_DECREF(opath);
4364 return NULL;
4365 }
4366 }
4367 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004368
Victor Stinner8c62be82010-05-06 00:08:46 +00004369 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004370#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004372#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004374#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004375 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004376
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 free_string_array(argvlist, argc);
4378 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004379
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 if (spawnval == -1)
4381 return posix_error();
4382 else
4383 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004384}
4385
4386
4387PyDoc_STRVAR(posix_spawnvpe__doc__,
4388"spawnvpe(mode, file, args, env)\n\n\
4389Execute the program 'file' in a new process, using the environment\n\
4390search path to find the file.\n\
4391\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004392 mode: mode of process creation\n\
4393 file: executable file name\n\
4394 args: tuple or list of arguments\n\
4395 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004396
4397static PyObject *
4398posix_spawnvpe(PyObject *self, PyObject *args)
4399{
Victor Stinner8c62be82010-05-06 00:08:46 +00004400 PyObject *opath
4401 char *path;
4402 PyObject *argv, *env;
4403 char **argvlist;
4404 char **envlist;
4405 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00004406 int mode;
4407 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00004408 Py_intptr_t spawnval;
4409 PyObject *(*getitem)(PyObject *, Py_ssize_t);
4410 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004411
Victor Stinner8c62be82010-05-06 00:08:46 +00004412 /* spawnvpe has four arguments: (mode, path, argv, env), where
4413 argv is a list or tuple of strings and env is a dictionary
4414 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004415
Victor Stinner8c62be82010-05-06 00:08:46 +00004416 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
4417 PyUnicode_FSConverter,
4418 &opath, &argv, &env))
4419 return NULL;
4420 path = PyBytes_AsString(opath);
4421 if (PyList_Check(argv)) {
4422 argc = PyList_Size(argv);
4423 getitem = PyList_GetItem;
4424 }
4425 else if (PyTuple_Check(argv)) {
4426 argc = PyTuple_Size(argv);
4427 getitem = PyTuple_GetItem;
4428 }
4429 else {
4430 PyErr_SetString(PyExc_TypeError,
4431 "spawnvpe() arg 2 must be a tuple or list");
4432 goto fail_0;
4433 }
4434 if (!PyMapping_Check(env)) {
4435 PyErr_SetString(PyExc_TypeError,
4436 "spawnvpe() arg 3 must be a mapping object");
4437 goto fail_0;
4438 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004439
Victor Stinner8c62be82010-05-06 00:08:46 +00004440 argvlist = PyMem_NEW(char *, argc+1);
4441 if (argvlist == NULL) {
4442 PyErr_NoMemory();
4443 goto fail_0;
4444 }
4445 for (i = 0; i < argc; i++) {
4446 if (!fsconvert_strdup((*getitem)(argv, i),
4447 &argvlist[i]))
4448 {
4449 lastarg = i;
4450 goto fail_1;
4451 }
4452 }
4453 lastarg = argc;
4454 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004455
Victor Stinner8c62be82010-05-06 00:08:46 +00004456 envlist = parse_envlist(env, &envc);
4457 if (envlist == NULL)
4458 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004459
Victor Stinner8c62be82010-05-06 00:08:46 +00004460 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004461#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004462 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004463#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004465#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004467
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 if (spawnval == -1)
4469 (void) posix_error();
4470 else
4471 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004472
Victor Stinner8c62be82010-05-06 00:08:46 +00004473 while (--envc >= 0)
4474 PyMem_DEL(envlist[envc]);
4475 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004476 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004477 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004478 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004479 Py_DECREF(opath);
4480 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004481}
4482#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004483#endif /* HAVE_SPAWNV */
4484
4485
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004486#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004487PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004488"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004489Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4490\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004491Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004492
4493static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004494posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004495{
Victor Stinner8c62be82010-05-06 00:08:46 +00004496 pid_t pid;
4497 int result = 0;
4498 _PyImport_AcquireLock();
4499 pid = fork1();
4500 if (pid == 0) {
4501 /* child: this clobbers and resets the import lock. */
4502 PyOS_AfterFork();
4503 } else {
4504 /* parent: release the import lock. */
4505 result = _PyImport_ReleaseLock();
4506 }
4507 if (pid == -1)
4508 return posix_error();
4509 if (result < 0) {
4510 /* Don't clobber the OSError if the fork failed. */
4511 PyErr_SetString(PyExc_RuntimeError,
4512 "not holding the import lock");
4513 return NULL;
4514 }
4515 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004516}
4517#endif
4518
4519
Guido van Rossumad0ee831995-03-01 10:34:45 +00004520#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004521PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004522"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004523Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004524Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004525
Barry Warsaw53699e91996-12-10 23:23:01 +00004526static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004527posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004528{
Victor Stinner8c62be82010-05-06 00:08:46 +00004529 pid_t pid;
4530 int result = 0;
4531 _PyImport_AcquireLock();
4532 pid = fork();
4533 if (pid == 0) {
4534 /* child: this clobbers and resets the import lock. */
4535 PyOS_AfterFork();
4536 } else {
4537 /* parent: release the import lock. */
4538 result = _PyImport_ReleaseLock();
4539 }
4540 if (pid == -1)
4541 return posix_error();
4542 if (result < 0) {
4543 /* Don't clobber the OSError if the fork failed. */
4544 PyErr_SetString(PyExc_RuntimeError,
4545 "not holding the import lock");
4546 return NULL;
4547 }
4548 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004549}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004550#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004551
Benjamin Peterson94b580d2011-08-02 17:30:04 -05004552#ifdef HAVE_SCHED_H
4553
4554PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
4555"sched_get_priority_max(policy)\n\n\
4556Get the maximum scheduling priority for *policy*.");
4557
4558static PyObject *
4559posix_sched_get_priority_max(PyObject *self, PyObject *args)
4560{
4561 int policy, max;
4562
4563 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
4564 return NULL;
4565 max = sched_get_priority_max(policy);
4566 if (max < 0)
4567 return posix_error();
4568 return PyLong_FromLong(max);
4569}
4570
4571PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
4572"sched_get_priority_min(policy)\n\n\
4573Get the minimum scheduling priority for *policy*.");
4574
4575static PyObject *
4576posix_sched_get_priority_min(PyObject *self, PyObject *args)
4577{
4578 int policy, min;
4579
4580 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
4581 return NULL;
4582 min = sched_get_priority_min(policy);
4583 if (min < 0)
4584 return posix_error();
4585 return PyLong_FromLong(min);
4586}
4587
4588PyDoc_STRVAR(posix_sched_getscheduler__doc__,
4589"sched_getscheduler(pid)\n\n\
4590Get the scheduling policy for the process with a PID of *pid*.\n\
4591Passing a PID of 0 returns the scheduling policy for the calling process.");
4592
4593static PyObject *
4594posix_sched_getscheduler(PyObject *self, PyObject *args)
4595{
4596 pid_t pid;
4597 int policy;
4598
4599 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
4600 return NULL;
4601 policy = sched_getscheduler(pid);
4602 if (policy < 0)
4603 return posix_error();
4604 return PyLong_FromLong(policy);
4605}
4606
4607static PyObject *
4608sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4609{
4610 PyObject *res, *priority;
4611 static char *kwlist[] = {"sched_priority"};
4612
4613 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
4614 return NULL;
4615 res = PyStructSequence_New(type);
4616 if (!res)
4617 return NULL;
4618 Py_INCREF(priority);
4619 PyStructSequence_SET_ITEM(res, 0, priority);
4620 return res;
4621}
4622
4623PyDoc_STRVAR(sched_param__doc__,
4624"sched_param(sched_priority): A scheduling parameter.\n\n\
4625Current has only one field: sched_priority");
4626
4627static PyStructSequence_Field sched_param_fields[] = {
4628 {"sched_priority", "the scheduling priority"},
4629 {0}
4630};
4631
4632static PyStructSequence_Desc sched_param_desc = {
4633 "sched_param", /* name */
4634 sched_param__doc__, /* doc */
4635 sched_param_fields,
4636 1
4637};
4638
4639static int
4640convert_sched_param(PyObject *param, struct sched_param *res)
4641{
4642 long priority;
4643
4644 if (Py_TYPE(param) != &SchedParamType) {
4645 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
4646 return 0;
4647 }
4648 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
4649 if (priority == -1 && PyErr_Occurred())
4650 return 0;
4651 if (priority > INT_MAX || priority < INT_MIN) {
4652 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
4653 return 0;
4654 }
4655 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
4656 return 1;
4657}
4658
4659PyDoc_STRVAR(posix_sched_setscheduler__doc__,
4660"sched_setscheduler(pid, policy, param)\n\n\
4661Set the scheduling policy, *policy*, for *pid*.\n\
4662If *pid* is 0, the calling process is changed.\n\
4663*param* is an instance of sched_param.");
4664
4665static PyObject *
4666posix_sched_setscheduler(PyObject *self, PyObject *args)
4667{
4668 pid_t pid;
4669 int policy;
4670 struct sched_param param;
4671
4672 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
4673 &pid, &policy, &convert_sched_param, &param))
4674 return NULL;
4675 if (sched_setscheduler(pid, policy, &param))
4676 return posix_error();
4677 Py_RETURN_NONE;
4678}
4679
4680PyDoc_STRVAR(posix_sched_getparam__doc__,
4681"sched_getparam(pid) -> sched_param\n\n\
4682Returns scheduling parameters for the process with *pid* as an instance of the\n\
4683sched_param class. A PID of 0 means the calling process.");
4684
4685static PyObject *
4686posix_sched_getparam(PyObject *self, PyObject *args)
4687{
4688 pid_t pid;
4689 struct sched_param param;
4690 PyObject *res, *priority;
4691
4692 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
4693 return NULL;
4694 if (sched_getparam(pid, &param))
4695 return posix_error();
4696 res = PyStructSequence_New(&SchedParamType);
4697 if (!res)
4698 return NULL;
4699 priority = PyLong_FromLong(param.sched_priority);
4700 if (!priority) {
4701 Py_DECREF(res);
4702 return NULL;
4703 }
4704 PyStructSequence_SET_ITEM(res, 0, priority);
4705 return res;
4706}
4707
4708PyDoc_STRVAR(posix_sched_setparam__doc__,
4709"sched_setparam(pid, param)\n\n\
4710Set scheduling parameters for a process with PID *pid*.\n\
4711A PID of 0 means the calling process.");
4712
4713static PyObject *
4714posix_sched_setparam(PyObject *self, PyObject *args)
4715{
4716 pid_t pid;
4717 struct sched_param param;
4718
4719 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
4720 &pid, &convert_sched_param, &param))
4721 return NULL;
4722 if (sched_setparam(pid, &param))
4723 return posix_error();
4724 Py_RETURN_NONE;
4725}
4726
4727PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
4728"sched_rr_get_interval(pid) -> float\n\n\
4729Return the round-robin quantum for the process with PID *pid* in seconds.");
4730
4731static PyObject *
4732posix_sched_rr_get_interval(PyObject *self, PyObject *args)
4733{
4734 pid_t pid;
4735 struct timespec interval;
4736
4737 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
4738 return NULL;
4739 if (sched_rr_get_interval(pid, &interval))
4740 return posix_error();
4741 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
4742}
4743
4744PyDoc_STRVAR(posix_sched_yield__doc__,
4745"sched_yield()\n\n\
4746Voluntarily relinquish the CPU.");
4747
4748static PyObject *
4749posix_sched_yield(PyObject *self, PyObject *noargs)
4750{
4751 if (sched_yield())
4752 return posix_error();
4753 Py_RETURN_NONE;
4754}
4755
4756typedef struct {
4757 PyObject_HEAD;
4758 Py_ssize_t size;
4759 int ncpus;
4760 cpu_set_t *set;
4761} Py_cpu_set;
4762
4763static PyTypeObject cpu_set_type;
4764
4765static void
4766cpu_set_dealloc(Py_cpu_set *set)
4767{
4768 assert(set->set);
4769 CPU_FREE(set->set);
4770 Py_TYPE(set)->tp_free(set);
4771}
4772
4773static Py_cpu_set *
4774make_new_cpu_set(PyTypeObject *type, Py_ssize_t size)
4775{
4776 Py_cpu_set *set;
4777
4778 if (size < 0) {
4779 PyErr_SetString(PyExc_ValueError, "negative size");
4780 return NULL;
4781 }
4782 set = (Py_cpu_set *)type->tp_alloc(type, 0);
4783 if (!set)
4784 return NULL;
4785 set->ncpus = size;
4786 set->size = CPU_ALLOC_SIZE(size);
4787 set->set = CPU_ALLOC(size);
4788 if (!set->set) {
4789 type->tp_free(set);
4790 PyErr_NoMemory();
4791 return NULL;
4792 }
4793 CPU_ZERO_S(set->size, set->set);
4794 return set;
4795}
4796
4797static PyObject *
4798cpu_set_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
4799{
4800 int size;
4801
4802 if (!_PyArg_NoKeywords("cpu_set()", kwargs) ||
4803 !PyArg_ParseTuple(args, "i:cpu_set", &size))
4804 return NULL;
4805 return (PyObject *)make_new_cpu_set(type, size);
4806}
4807
4808static PyObject *
4809cpu_set_repr(Py_cpu_set *set)
4810{
4811 return PyUnicode_FromFormat("<cpu_set with %li entries>", set->ncpus);
4812}
4813
4814static Py_ssize_t
4815cpu_set_len(Py_cpu_set *set)
4816{
4817 return set->ncpus;
4818}
4819
4820static int
4821_get_cpu(Py_cpu_set *set, const char *requester, PyObject *args)
4822{
4823 int cpu;
4824 if (!PyArg_ParseTuple(args, requester, &cpu))
4825 return -1;
4826 if (cpu < 0) {
4827 PyErr_SetString(PyExc_ValueError, "cpu < 0 not valid");
4828 return -1;
4829 }
4830 if (cpu >= set->ncpus) {
4831 PyErr_SetString(PyExc_ValueError, "cpu too large for set");
4832 return -1;
4833 }
4834 return cpu;
4835}
4836
4837PyDoc_STRVAR(cpu_set_set_doc,
4838"cpu_set.set(i)\n\n\
4839Add CPU *i* to the set.");
4840
4841static PyObject *
4842cpu_set_set(Py_cpu_set *set, PyObject *args)
4843{
4844 int cpu = _get_cpu(set, "i|set", args);
4845 if (cpu == -1)
4846 return NULL;
4847 CPU_SET_S(cpu, set->size, set->set);
4848 Py_RETURN_NONE;
4849}
4850
4851PyDoc_STRVAR(cpu_set_count_doc,
4852"cpu_set.count() -> int\n\n\
4853Return the number of CPUs active in the set.");
4854
4855static PyObject *
4856cpu_set_count(Py_cpu_set *set, PyObject *noargs)
4857{
4858 return PyLong_FromLong(CPU_COUNT_S(set->size, set->set));
4859}
4860
4861PyDoc_STRVAR(cpu_set_clear_doc,
4862"cpu_set.clear(i)\n\n\
4863Remove CPU *i* from the set.");
4864
4865static PyObject *
4866cpu_set_clear(Py_cpu_set *set, PyObject *args)
4867{
4868 int cpu = _get_cpu(set, "i|clear", args);
4869 if (cpu == -1)
4870 return NULL;
4871 CPU_CLR_S(cpu, set->size, set->set);
4872 Py_RETURN_NONE;
4873}
4874
4875PyDoc_STRVAR(cpu_set_isset_doc,
4876"cpu_set.isset(i) -> bool\n\n\
4877Test if CPU *i* is in the set.");
4878
4879static PyObject *
4880cpu_set_isset(Py_cpu_set *set, PyObject *args)
4881{
4882 int cpu = _get_cpu(set, "i|isset", args);
4883 if (cpu == -1)
4884 return NULL;
4885 if (CPU_ISSET_S(cpu, set->size, set->set))
4886 Py_RETURN_TRUE;
4887 Py_RETURN_FALSE;
4888}
4889
4890PyDoc_STRVAR(cpu_set_zero_doc,
4891"cpu_set.zero()\n\n\
4892Clear the cpu_set.");
4893
4894static PyObject *
4895cpu_set_zero(Py_cpu_set *set, PyObject *noargs)
4896{
4897 CPU_ZERO_S(set->size, set->set);
4898 Py_RETURN_NONE;
4899}
4900
4901static PyObject *
4902cpu_set_richcompare(Py_cpu_set *set, Py_cpu_set *other, int op)
4903{
4904 int eq;
4905
4906 if ((op != Py_EQ && op != Py_NE) || Py_TYPE(other) != &cpu_set_type) {
4907 Py_INCREF(Py_NotImplemented);
4908 return Py_NotImplemented;
4909 }
4910 eq = set->ncpus == other->ncpus && CPU_EQUAL_S(set->size, set->set, other->set);
4911 if ((op == Py_EQ) ? eq : !eq)
4912 Py_RETURN_TRUE;
4913 else
4914 Py_RETURN_FALSE;
4915}
4916
4917#define CPU_SET_BINOP(name, op) \
4918 static PyObject * \
4919 do_cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right, Py_cpu_set *res) { \
4920 if (res) { \
4921 Py_INCREF(res); \
4922 } \
4923 else { \
4924 res = make_new_cpu_set(&cpu_set_type, left->size); \
4925 if (!res) \
4926 return NULL; \
4927 } \
4928 if (Py_TYPE(right) != &cpu_set_type || left->size != right->size) { \
4929 Py_DECREF(res); \
4930 Py_INCREF(Py_NotImplemented); \
4931 return Py_NotImplemented; \
4932 } \
4933 assert(left->size == right->size == res->size); \
4934 op(res->size, res->set, left->set, right->set); \
4935 return (PyObject *)res; \
4936 } \
4937 static PyObject * \
4938 cpu_set_##name(Py_cpu_set *left, Py_cpu_set *right) { \
4939 return do_cpu_set_##name(left, right, NULL); \
4940 } \
4941 static PyObject * \
4942 cpu_set_i##name(Py_cpu_set *left, Py_cpu_set *right) { \
4943 return do_cpu_set_##name(left, right, left); \
4944 } \
4945
4946CPU_SET_BINOP(and, CPU_AND_S)
4947CPU_SET_BINOP(or, CPU_OR_S)
4948CPU_SET_BINOP(xor, CPU_XOR_S)
4949#undef CPU_SET_BINOP
4950
4951PyDoc_STRVAR(cpu_set_doc,
4952"cpu_set(size)\n\n\
4953Create an empty mask of CPUs.");
4954
4955static PyNumberMethods cpu_set_as_number = {
4956 0, /*nb_add*/
4957 0, /*nb_subtract*/
4958 0, /*nb_multiply*/
4959 0, /*nb_remainder*/
4960 0, /*nb_divmod*/
4961 0, /*nb_power*/
4962 0, /*nb_negative*/
4963 0, /*nb_positive*/
4964 0, /*nb_absolute*/
4965 0, /*nb_bool*/
4966 0, /*nb_invert*/
4967 0, /*nb_lshift*/
4968 0, /*nb_rshift*/
4969 (binaryfunc)cpu_set_and, /*nb_and*/
4970 (binaryfunc)cpu_set_xor, /*nb_xor*/
4971 (binaryfunc)cpu_set_or, /*nb_or*/
4972 0, /*nb_int*/
4973 0, /*nb_reserved*/
4974 0, /*nb_float*/
4975 0, /*nb_inplace_add*/
4976 0, /*nb_inplace_subtract*/
4977 0, /*nb_inplace_multiply*/
4978 0, /*nb_inplace_remainder*/
4979 0, /*nb_inplace_power*/
4980 0, /*nb_inplace_lshift*/
4981 0, /*nb_inplace_rshift*/
4982 (binaryfunc)cpu_set_iand, /*nb_inplace_and*/
4983 (binaryfunc)cpu_set_ixor, /*nb_inplace_xor*/
4984 (binaryfunc)cpu_set_ior, /*nb_inplace_or*/
4985};
4986
4987static PySequenceMethods cpu_set_as_sequence = {
4988 (lenfunc)cpu_set_len, /* sq_length */
4989};
4990
4991static PyMethodDef cpu_set_methods[] = {
4992 {"clear", (PyCFunction)cpu_set_clear, METH_VARARGS, cpu_set_clear_doc},
4993 {"count", (PyCFunction)cpu_set_count, METH_NOARGS, cpu_set_count_doc},
4994 {"isset", (PyCFunction)cpu_set_isset, METH_VARARGS, cpu_set_isset_doc},
4995 {"set", (PyCFunction)cpu_set_set, METH_VARARGS, cpu_set_set_doc},
4996 {"zero", (PyCFunction)cpu_set_zero, METH_NOARGS, cpu_set_zero_doc},
4997 {NULL, NULL} /* sentinel */
4998};
4999
5000static PyTypeObject cpu_set_type = {
5001 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5002 "posix.cpu_set", /* tp_name */
5003 sizeof(Py_cpu_set), /* tp_basicsize */
5004 0, /* tp_itemsize */
5005 /* methods */
5006 (destructor)cpu_set_dealloc, /* tp_dealloc */
5007 0, /* tp_print */
5008 0, /* tp_getattr */
5009 0, /* tp_setattr */
5010 0, /* tp_reserved */
5011 (reprfunc)cpu_set_repr, /* tp_repr */
5012 &cpu_set_as_number, /* tp_as_number */
5013 &cpu_set_as_sequence, /* tp_as_sequence */
5014 0, /* tp_as_mapping */
5015 PyObject_HashNotImplemented, /* tp_hash */
5016 0, /* tp_call */
5017 0, /* tp_str */
5018 PyObject_GenericGetAttr, /* tp_getattro */
5019 0, /* tp_setattro */
5020 0, /* tp_as_buffer */
5021 Py_TPFLAGS_DEFAULT, /* tp_flags */
5022 cpu_set_doc, /* tp_doc */
5023 0, /* tp_traverse */
5024 0, /* tp_clear */
5025 (richcmpfunc)cpu_set_richcompare, /* tp_richcompare */
5026 0, /* tp_weaklistoffset */
5027 0, /* tp_iter */
5028 0, /* tp_iternext */
5029 cpu_set_methods, /* tp_methods */
5030 0, /* tp_members */
5031 0, /* tp_getset */
5032 0, /* tp_base */
5033 0, /* tp_dict */
5034 0, /* tp_descr_get */
5035 0, /* tp_descr_set */
5036 0, /* tp_dictoffset */
5037 0, /* tp_init */
5038 PyType_GenericAlloc, /* tp_alloc */
5039 cpu_set_new, /* tp_new */
5040 PyObject_Del, /* tp_free */
5041};
5042
5043PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5044"sched_setaffinity(pid, cpu_set)\n\n\
5045Set the affinity of the process with PID *pid* to *cpu_set*.");
5046
5047static PyObject *
5048posix_sched_setaffinity(PyObject *self, PyObject *args)
5049{
5050 pid_t pid;
5051 Py_cpu_set *cpu_set;
5052
5053 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O!|sched_setaffinity",
5054 &pid, &cpu_set_type, &cpu_set))
5055 return NULL;
5056 if (sched_setaffinity(pid, cpu_set->size, cpu_set->set))
5057 return posix_error();
5058 Py_RETURN_NONE;
5059}
5060
5061PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5062"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5063Return the affinity of the process with PID *pid*.\n\
5064The returned cpu_set will be of size *ncpus*.");
5065
5066static PyObject *
5067posix_sched_getaffinity(PyObject *self, PyObject *args)
5068{
5069 pid_t pid;
5070 int ncpus;
5071 Py_cpu_set *res;
5072
5073 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|sched_getaffinity",
5074 &pid, &ncpus))
5075 return NULL;
5076 res = make_new_cpu_set(&cpu_set_type, ncpus);
5077 if (!res)
5078 return NULL;
5079 if (sched_getaffinity(pid, res->size, res->set)) {
5080 Py_DECREF(res);
5081 return posix_error();
5082 }
5083 return (PyObject *)res;
5084}
5085
5086#endif /* HAVE_SCHED_H */
5087
Neal Norwitzb59798b2003-03-21 01:43:31 +00005088/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005089/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5090#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005091#define DEV_PTY_FILE "/dev/ptc"
5092#define HAVE_DEV_PTMX
5093#else
5094#define DEV_PTY_FILE "/dev/ptmx"
5095#endif
5096
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005097#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005098#ifdef HAVE_PTY_H
5099#include <pty.h>
5100#else
5101#ifdef HAVE_LIBUTIL_H
5102#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005103#else
5104#ifdef HAVE_UTIL_H
5105#include <util.h>
5106#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005107#endif /* HAVE_LIBUTIL_H */
5108#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005109#ifdef HAVE_STROPTS_H
5110#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005111#endif
5112#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005113
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005114#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005115PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005116"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005117Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005118
5119static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005120posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005121{
Victor Stinner8c62be82010-05-06 00:08:46 +00005122 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005123#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005124 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005125#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005126#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005127 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005128#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005129 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005130#endif
5131#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005132
Thomas Wouters70c21a12000-07-14 14:28:33 +00005133#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005134 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5135 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005136#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005137 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5138 if (slave_name == NULL)
5139 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005140
Victor Stinner8c62be82010-05-06 00:08:46 +00005141 slave_fd = open(slave_name, O_RDWR);
5142 if (slave_fd < 0)
5143 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005144#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005145 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5146 if (master_fd < 0)
5147 return posix_error();
5148 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5149 /* change permission of slave */
5150 if (grantpt(master_fd) < 0) {
5151 PyOS_setsig(SIGCHLD, sig_saved);
5152 return posix_error();
5153 }
5154 /* unlock slave */
5155 if (unlockpt(master_fd) < 0) {
5156 PyOS_setsig(SIGCHLD, sig_saved);
5157 return posix_error();
5158 }
5159 PyOS_setsig(SIGCHLD, sig_saved);
5160 slave_name = ptsname(master_fd); /* get name of slave */
5161 if (slave_name == NULL)
5162 return posix_error();
5163 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5164 if (slave_fd < 0)
5165 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005166#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005167 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5168 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005169#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005170 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005171#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005172#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005173#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005174
Victor Stinner8c62be82010-05-06 00:08:46 +00005175 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005176
Fred Drake8cef4cf2000-06-28 16:40:38 +00005177}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005178#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005179
5180#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005181PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005182"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005183Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5184Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005185To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005186
5187static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005188posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005189{
Victor Stinner8c62be82010-05-06 00:08:46 +00005190 int master_fd = -1, result = 0;
5191 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005192
Victor Stinner8c62be82010-05-06 00:08:46 +00005193 _PyImport_AcquireLock();
5194 pid = forkpty(&master_fd, NULL, NULL, NULL);
5195 if (pid == 0) {
5196 /* child: this clobbers and resets the import lock. */
5197 PyOS_AfterFork();
5198 } else {
5199 /* parent: release the import lock. */
5200 result = _PyImport_ReleaseLock();
5201 }
5202 if (pid == -1)
5203 return posix_error();
5204 if (result < 0) {
5205 /* Don't clobber the OSError if the fork failed. */
5206 PyErr_SetString(PyExc_RuntimeError,
5207 "not holding the import lock");
5208 return NULL;
5209 }
5210 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005211}
5212#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005213
Ross Lagerwall7807c352011-03-17 20:20:30 +02005214
Guido van Rossumad0ee831995-03-01 10:34:45 +00005215#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005216PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005217"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005218Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005219
Barry Warsaw53699e91996-12-10 23:23:01 +00005220static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005221posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005222{
Victor Stinner8c62be82010-05-06 00:08:46 +00005223 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005224}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005225#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005226
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005227
Guido van Rossumad0ee831995-03-01 10:34:45 +00005228#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005229PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005230"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005231Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005232
Barry Warsaw53699e91996-12-10 23:23:01 +00005233static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005234posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005235{
Victor Stinner8c62be82010-05-06 00:08:46 +00005236 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005237}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005238#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005239
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005240
Guido van Rossumad0ee831995-03-01 10:34:45 +00005241#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005242PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005243"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005244Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005245
Barry Warsaw53699e91996-12-10 23:23:01 +00005246static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005247posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005248{
Victor Stinner8c62be82010-05-06 00:08:46 +00005249 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005250}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005251#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005253
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005254PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005255"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005256Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005257
Barry Warsaw53699e91996-12-10 23:23:01 +00005258static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005259posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005260{
Victor Stinner8c62be82010-05-06 00:08:46 +00005261 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005262}
5263
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005264#ifdef HAVE_GETGROUPLIST
5265PyDoc_STRVAR(posix_getgrouplist__doc__,
5266"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5267Returns a list of groups to which a user belongs.\n\n\
5268 user: username to lookup\n\
5269 group: base group id of the user");
5270
5271static PyObject *
5272posix_getgrouplist(PyObject *self, PyObject *args)
5273{
5274#ifdef NGROUPS_MAX
5275#define MAX_GROUPS NGROUPS_MAX
5276#else
5277 /* defined to be 16 on Solaris7, so this should be a small number */
5278#define MAX_GROUPS 64
5279#endif
5280
5281 const char *user;
5282 int i, ngroups;
5283 PyObject *list;
5284#ifdef __APPLE__
5285 int *groups, basegid;
5286#else
5287 gid_t *groups, basegid;
5288#endif
5289 ngroups = MAX_GROUPS;
5290
5291 if (!PyArg_ParseTuple(args, "si", &user, &basegid))
5292 return NULL;
5293
5294#ifdef __APPLE__
5295 groups = PyMem_Malloc(ngroups * sizeof(int));
5296#else
5297 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5298#endif
5299 if (groups == NULL)
5300 return PyErr_NoMemory();
5301
5302 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5303 PyMem_Del(groups);
5304 return posix_error();
5305 }
5306
5307 list = PyList_New(ngroups);
5308 if (list == NULL) {
5309 PyMem_Del(groups);
5310 return NULL;
5311 }
5312
5313 for (i = 0; i < ngroups; i++) {
5314 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
5315 if (o == NULL) {
5316 Py_DECREF(list);
5317 PyMem_Del(groups);
5318 return NULL;
5319 }
5320 PyList_SET_ITEM(list, i, o);
5321 }
5322
5323 PyMem_Del(groups);
5324
5325 return list;
5326}
5327#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005328
Fred Drakec9680921999-12-13 16:37:25 +00005329#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005330PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005331"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005332Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005333
5334static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005335posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005336{
5337 PyObject *result = NULL;
5338
Fred Drakec9680921999-12-13 16:37:25 +00005339#ifdef NGROUPS_MAX
5340#define MAX_GROUPS NGROUPS_MAX
5341#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005342 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005343#define MAX_GROUPS 64
5344#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005345 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005346
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005347 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005348 * This is a helper variable to store the intermediate result when
5349 * that happens.
5350 *
5351 * To keep the code readable the OSX behaviour is unconditional,
5352 * according to the POSIX spec this should be safe on all unix-y
5353 * systems.
5354 */
5355 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005356 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005357
Victor Stinner8c62be82010-05-06 00:08:46 +00005358 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005359 if (n < 0) {
5360 if (errno == EINVAL) {
5361 n = getgroups(0, NULL);
5362 if (n == -1) {
5363 return posix_error();
5364 }
5365 if (n == 0) {
5366 /* Avoid malloc(0) */
5367 alt_grouplist = grouplist;
5368 } else {
5369 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5370 if (alt_grouplist == NULL) {
5371 errno = EINVAL;
5372 return posix_error();
5373 }
5374 n = getgroups(n, alt_grouplist);
5375 if (n == -1) {
5376 PyMem_Free(alt_grouplist);
5377 return posix_error();
5378 }
5379 }
5380 } else {
5381 return posix_error();
5382 }
5383 }
5384 result = PyList_New(n);
5385 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 int i;
5387 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005388 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005389 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005390 Py_DECREF(result);
5391 result = NULL;
5392 break;
Fred Drakec9680921999-12-13 16:37:25 +00005393 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005394 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005395 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005396 }
5397
5398 if (alt_grouplist != grouplist) {
5399 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005400 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005401
Fred Drakec9680921999-12-13 16:37:25 +00005402 return result;
5403}
5404#endif
5405
Antoine Pitroub7572f02009-12-02 20:46:48 +00005406#ifdef HAVE_INITGROUPS
5407PyDoc_STRVAR(posix_initgroups__doc__,
5408"initgroups(username, gid) -> None\n\n\
5409Call the system initgroups() to initialize the group access list with all of\n\
5410the groups of which the specified username is a member, plus the specified\n\
5411group id.");
5412
5413static PyObject *
5414posix_initgroups(PyObject *self, PyObject *args)
5415{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005416 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005417 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005418 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00005419 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005420
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005421 if (!PyArg_ParseTuple(args, "O&l:initgroups",
5422 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00005423 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005424 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005425
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005426 res = initgroups(username, (gid_t) gid);
5427 Py_DECREF(oname);
5428 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005429 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005430
Victor Stinner8c62be82010-05-06 00:08:46 +00005431 Py_INCREF(Py_None);
5432 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005433}
5434#endif
5435
Martin v. Löwis606edc12002-06-13 21:09:11 +00005436#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005437PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005438"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005439Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005440
5441static PyObject *
5442posix_getpgid(PyObject *self, PyObject *args)
5443{
Victor Stinner8c62be82010-05-06 00:08:46 +00005444 pid_t pid, pgid;
5445 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5446 return NULL;
5447 pgid = getpgid(pid);
5448 if (pgid < 0)
5449 return posix_error();
5450 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005451}
5452#endif /* HAVE_GETPGID */
5453
5454
Guido van Rossumb6775db1994-08-01 11:34:53 +00005455#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005457"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005458Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005459
Barry Warsaw53699e91996-12-10 23:23:01 +00005460static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005461posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005462{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005463#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005464 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005465#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005466 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005467#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005468}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005469#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005471
Guido van Rossumb6775db1994-08-01 11:34:53 +00005472#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005473PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005474"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005475Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005476
Barry Warsaw53699e91996-12-10 23:23:01 +00005477static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005478posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005479{
Guido van Rossum64933891994-10-20 21:56:42 +00005480#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005481 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005482#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005483 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00005484#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005485 return posix_error();
5486 Py_INCREF(Py_None);
5487 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005488}
5489
Guido van Rossumb6775db1994-08-01 11:34:53 +00005490#endif /* HAVE_SETPGRP */
5491
Guido van Rossumad0ee831995-03-01 10:34:45 +00005492#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005493
5494#ifdef MS_WINDOWS
5495#include <tlhelp32.h>
5496
5497static PyObject*
5498win32_getppid()
5499{
5500 HANDLE snapshot;
5501 pid_t mypid;
5502 PyObject* result = NULL;
5503 BOOL have_record;
5504 PROCESSENTRY32 pe;
5505
5506 mypid = getpid(); /* This function never fails */
5507
5508 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
5509 if (snapshot == INVALID_HANDLE_VALUE)
5510 return PyErr_SetFromWindowsErr(GetLastError());
5511
5512 pe.dwSize = sizeof(pe);
5513 have_record = Process32First(snapshot, &pe);
5514 while (have_record) {
5515 if (mypid == (pid_t)pe.th32ProcessID) {
5516 /* We could cache the ulong value in a static variable. */
5517 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
5518 break;
5519 }
5520
5521 have_record = Process32Next(snapshot, &pe);
5522 }
5523
5524 /* If our loop exits and our pid was not found (result will be NULL)
5525 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
5526 * error anyway, so let's raise it. */
5527 if (!result)
5528 result = PyErr_SetFromWindowsErr(GetLastError());
5529
5530 CloseHandle(snapshot);
5531
5532 return result;
5533}
5534#endif /*MS_WINDOWS*/
5535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005536PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005537"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005538Return the parent's process id. If the parent process has already exited,\n\
5539Windows machines will still return its id; others systems will return the id\n\
5540of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005541
Barry Warsaw53699e91996-12-10 23:23:01 +00005542static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005543posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005544{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005545#ifdef MS_WINDOWS
5546 return win32_getppid();
5547#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005548 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00005549#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00005550}
5551#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005552
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005553
Fred Drake12c6e2d1999-12-14 21:25:03 +00005554#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005555PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005556"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005557Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00005558
5559static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005560posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005561{
Victor Stinner8c62be82010-05-06 00:08:46 +00005562 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005563#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005564 wchar_t user_name[UNLEN + 1];
5565 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
5566
5567 if (GetUserNameW(user_name, &num_chars)) {
5568 /* num_chars is the number of unicode chars plus null terminator */
5569 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005570 }
5571 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005572 result = PyErr_SetFromWindowsErr(GetLastError());
5573#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005574 char *name;
5575 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005576
Victor Stinner8c62be82010-05-06 00:08:46 +00005577 errno = 0;
5578 name = getlogin();
5579 if (name == NULL) {
5580 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00005581 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00005582 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005583 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00005584 }
5585 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00005586 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00005587 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005588#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00005589 return result;
5590}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00005591#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00005592
Guido van Rossumad0ee831995-03-01 10:34:45 +00005593#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005594PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005595"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005596Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005597
Barry Warsaw53699e91996-12-10 23:23:01 +00005598static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005599posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005600{
Victor Stinner8c62be82010-05-06 00:08:46 +00005601 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005602}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005603#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005604
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005605
Guido van Rossumad0ee831995-03-01 10:34:45 +00005606#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005607PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005608"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005609Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005610
Barry Warsaw53699e91996-12-10 23:23:01 +00005611static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005612posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005613{
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 pid_t pid;
5615 int sig;
5616 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
5617 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00005618#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005619 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
5620 APIRET rc;
5621 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005622 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005623
5624 } else if (sig == XCPT_SIGNAL_KILLPROC) {
5625 APIRET rc;
5626 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005627 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005628
5629 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00005630 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005631#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 if (kill(pid, sig) == -1)
5633 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005634#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 Py_INCREF(Py_None);
5636 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00005637}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005638#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005639
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005640#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005641PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005642"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005643Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005644
5645static PyObject *
5646posix_killpg(PyObject *self, PyObject *args)
5647{
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 int sig;
5649 pid_t pgid;
5650 /* XXX some man pages make the `pgid` parameter an int, others
5651 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
5652 take the same type. Moreover, pid_t is always at least as wide as
5653 int (else compilation of this module fails), which is safe. */
5654 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
5655 return NULL;
5656 if (killpg(pgid, sig) == -1)
5657 return posix_error();
5658 Py_INCREF(Py_None);
5659 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00005660}
5661#endif
5662
Brian Curtineb24d742010-04-12 17:16:38 +00005663#ifdef MS_WINDOWS
5664PyDoc_STRVAR(win32_kill__doc__,
5665"kill(pid, sig)\n\n\
5666Kill a process with a signal.");
5667
5668static PyObject *
5669win32_kill(PyObject *self, PyObject *args)
5670{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00005671 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00005672 DWORD pid, sig, err;
5673 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00005674
Victor Stinner8c62be82010-05-06 00:08:46 +00005675 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
5676 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00005677
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 /* Console processes which share a common console can be sent CTRL+C or
5679 CTRL+BREAK events, provided they handle said events. */
5680 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
5681 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
5682 err = GetLastError();
5683 PyErr_SetFromWindowsErr(err);
5684 }
5685 else
5686 Py_RETURN_NONE;
5687 }
Brian Curtineb24d742010-04-12 17:16:38 +00005688
Victor Stinner8c62be82010-05-06 00:08:46 +00005689 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
5690 attempt to open and terminate the process. */
5691 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
5692 if (handle == NULL) {
5693 err = GetLastError();
5694 return PyErr_SetFromWindowsErr(err);
5695 }
Brian Curtineb24d742010-04-12 17:16:38 +00005696
Victor Stinner8c62be82010-05-06 00:08:46 +00005697 if (TerminateProcess(handle, sig) == 0) {
5698 err = GetLastError();
5699 result = PyErr_SetFromWindowsErr(err);
5700 } else {
5701 Py_INCREF(Py_None);
5702 result = Py_None;
5703 }
Brian Curtineb24d742010-04-12 17:16:38 +00005704
Victor Stinner8c62be82010-05-06 00:08:46 +00005705 CloseHandle(handle);
5706 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00005707}
5708#endif /* MS_WINDOWS */
5709
Guido van Rossumc0125471996-06-28 18:55:32 +00005710#ifdef HAVE_PLOCK
5711
5712#ifdef HAVE_SYS_LOCK_H
5713#include <sys/lock.h>
5714#endif
5715
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005716PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005717"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005718Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005719
Barry Warsaw53699e91996-12-10 23:23:01 +00005720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005721posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00005722{
Victor Stinner8c62be82010-05-06 00:08:46 +00005723 int op;
5724 if (!PyArg_ParseTuple(args, "i:plock", &op))
5725 return NULL;
5726 if (plock(op) == -1)
5727 return posix_error();
5728 Py_INCREF(Py_None);
5729 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00005730}
5731#endif
5732
Guido van Rossumb6775db1994-08-01 11:34:53 +00005733#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005734PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005735"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005736Set the current process's user id.");
5737
Barry Warsaw53699e91996-12-10 23:23:01 +00005738static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005739posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005740{
Victor Stinner8c62be82010-05-06 00:08:46 +00005741 long uid_arg;
5742 uid_t uid;
5743 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
5744 return NULL;
5745 uid = uid_arg;
5746 if (uid != uid_arg) {
5747 PyErr_SetString(PyExc_OverflowError, "user id too big");
5748 return NULL;
5749 }
5750 if (setuid(uid) < 0)
5751 return posix_error();
5752 Py_INCREF(Py_None);
5753 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005754}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005755#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005756
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005757
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005758#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005759PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005760"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005761Set the current process's effective user id.");
5762
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005763static PyObject *
5764posix_seteuid (PyObject *self, PyObject *args)
5765{
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 long euid_arg;
5767 uid_t euid;
5768 if (!PyArg_ParseTuple(args, "l", &euid_arg))
5769 return NULL;
5770 euid = euid_arg;
5771 if (euid != euid_arg) {
5772 PyErr_SetString(PyExc_OverflowError, "user id too big");
5773 return NULL;
5774 }
5775 if (seteuid(euid) < 0) {
5776 return posix_error();
5777 } else {
5778 Py_INCREF(Py_None);
5779 return Py_None;
5780 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005781}
5782#endif /* HAVE_SETEUID */
5783
5784#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005785PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005786"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005787Set the current process's effective group id.");
5788
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005789static PyObject *
5790posix_setegid (PyObject *self, PyObject *args)
5791{
Victor Stinner8c62be82010-05-06 00:08:46 +00005792 long egid_arg;
5793 gid_t egid;
5794 if (!PyArg_ParseTuple(args, "l", &egid_arg))
5795 return NULL;
5796 egid = egid_arg;
5797 if (egid != egid_arg) {
5798 PyErr_SetString(PyExc_OverflowError, "group id too big");
5799 return NULL;
5800 }
5801 if (setegid(egid) < 0) {
5802 return posix_error();
5803 } else {
5804 Py_INCREF(Py_None);
5805 return Py_None;
5806 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005807}
5808#endif /* HAVE_SETEGID */
5809
5810#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005811PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005812"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005813Set the current process's real and effective user ids.");
5814
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005815static PyObject *
5816posix_setreuid (PyObject *self, PyObject *args)
5817{
Victor Stinner8c62be82010-05-06 00:08:46 +00005818 long ruid_arg, euid_arg;
5819 uid_t ruid, euid;
5820 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
5821 return NULL;
5822 if (ruid_arg == -1)
5823 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
5824 else
5825 ruid = ruid_arg; /* otherwise, assign from our long */
5826 if (euid_arg == -1)
5827 euid = (uid_t)-1;
5828 else
5829 euid = euid_arg;
5830 if ((euid_arg != -1 && euid != euid_arg) ||
5831 (ruid_arg != -1 && ruid != ruid_arg)) {
5832 PyErr_SetString(PyExc_OverflowError, "user id too big");
5833 return NULL;
5834 }
5835 if (setreuid(ruid, euid) < 0) {
5836 return posix_error();
5837 } else {
5838 Py_INCREF(Py_None);
5839 return Py_None;
5840 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005841}
5842#endif /* HAVE_SETREUID */
5843
5844#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005845PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00005846"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005847Set the current process's real and effective group ids.");
5848
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005849static PyObject *
5850posix_setregid (PyObject *self, PyObject *args)
5851{
Victor Stinner8c62be82010-05-06 00:08:46 +00005852 long rgid_arg, egid_arg;
5853 gid_t rgid, egid;
5854 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
5855 return NULL;
5856 if (rgid_arg == -1)
5857 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
5858 else
5859 rgid = rgid_arg; /* otherwise, assign from our long */
5860 if (egid_arg == -1)
5861 egid = (gid_t)-1;
5862 else
5863 egid = egid_arg;
5864 if ((egid_arg != -1 && egid != egid_arg) ||
5865 (rgid_arg != -1 && rgid != rgid_arg)) {
5866 PyErr_SetString(PyExc_OverflowError, "group id too big");
5867 return NULL;
5868 }
5869 if (setregid(rgid, egid) < 0) {
5870 return posix_error();
5871 } else {
5872 Py_INCREF(Py_None);
5873 return Py_None;
5874 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00005875}
5876#endif /* HAVE_SETREGID */
5877
Guido van Rossumb6775db1994-08-01 11:34:53 +00005878#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005879PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005880"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005881Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005882
Barry Warsaw53699e91996-12-10 23:23:01 +00005883static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005884posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005885{
Victor Stinner8c62be82010-05-06 00:08:46 +00005886 long gid_arg;
5887 gid_t gid;
5888 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
5889 return NULL;
5890 gid = gid_arg;
5891 if (gid != gid_arg) {
5892 PyErr_SetString(PyExc_OverflowError, "group id too big");
5893 return NULL;
5894 }
5895 if (setgid(gid) < 0)
5896 return posix_error();
5897 Py_INCREF(Py_None);
5898 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005899}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005900#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00005901
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005902#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005903PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005904"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005905Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005906
5907static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00005908posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005909{
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 int i, len;
5911 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00005912
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 if (!PySequence_Check(groups)) {
5914 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
5915 return NULL;
5916 }
5917 len = PySequence_Size(groups);
5918 if (len > MAX_GROUPS) {
5919 PyErr_SetString(PyExc_ValueError, "too many groups");
5920 return NULL;
5921 }
5922 for(i = 0; i < len; i++) {
5923 PyObject *elem;
5924 elem = PySequence_GetItem(groups, i);
5925 if (!elem)
5926 return NULL;
5927 if (!PyLong_Check(elem)) {
5928 PyErr_SetString(PyExc_TypeError,
5929 "groups must be integers");
5930 Py_DECREF(elem);
5931 return NULL;
5932 } else {
5933 unsigned long x = PyLong_AsUnsignedLong(elem);
5934 if (PyErr_Occurred()) {
5935 PyErr_SetString(PyExc_TypeError,
5936 "group id too big");
5937 Py_DECREF(elem);
5938 return NULL;
5939 }
5940 grouplist[i] = x;
5941 /* read back the value to see if it fitted in gid_t */
5942 if (grouplist[i] != x) {
5943 PyErr_SetString(PyExc_TypeError,
5944 "group id too big");
5945 Py_DECREF(elem);
5946 return NULL;
5947 }
5948 }
5949 Py_DECREF(elem);
5950 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005951
Victor Stinner8c62be82010-05-06 00:08:46 +00005952 if (setgroups(len, grouplist) < 0)
5953 return posix_error();
5954 Py_INCREF(Py_None);
5955 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00005956}
5957#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005958
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005959#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
5960static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00005961wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005962{
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 PyObject *result;
5964 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005965
Victor Stinner8c62be82010-05-06 00:08:46 +00005966 if (pid == -1)
5967 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005968
Victor Stinner8c62be82010-05-06 00:08:46 +00005969 if (struct_rusage == NULL) {
5970 PyObject *m = PyImport_ImportModuleNoBlock("resource");
5971 if (m == NULL)
5972 return NULL;
5973 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
5974 Py_DECREF(m);
5975 if (struct_rusage == NULL)
5976 return NULL;
5977 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005978
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
5980 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
5981 if (!result)
5982 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005983
5984#ifndef doubletime
5985#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
5986#endif
5987
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 PyStructSequence_SET_ITEM(result, 0,
5989 PyFloat_FromDouble(doubletime(ru->ru_utime)));
5990 PyStructSequence_SET_ITEM(result, 1,
5991 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005992#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00005993 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
5994 SET_INT(result, 2, ru->ru_maxrss);
5995 SET_INT(result, 3, ru->ru_ixrss);
5996 SET_INT(result, 4, ru->ru_idrss);
5997 SET_INT(result, 5, ru->ru_isrss);
5998 SET_INT(result, 6, ru->ru_minflt);
5999 SET_INT(result, 7, ru->ru_majflt);
6000 SET_INT(result, 8, ru->ru_nswap);
6001 SET_INT(result, 9, ru->ru_inblock);
6002 SET_INT(result, 10, ru->ru_oublock);
6003 SET_INT(result, 11, ru->ru_msgsnd);
6004 SET_INT(result, 12, ru->ru_msgrcv);
6005 SET_INT(result, 13, ru->ru_nsignals);
6006 SET_INT(result, 14, ru->ru_nvcsw);
6007 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006008#undef SET_INT
6009
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 if (PyErr_Occurred()) {
6011 Py_DECREF(result);
6012 return NULL;
6013 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006014
Victor Stinner8c62be82010-05-06 00:08:46 +00006015 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006016}
6017#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6018
6019#ifdef HAVE_WAIT3
6020PyDoc_STRVAR(posix_wait3__doc__,
6021"wait3(options) -> (pid, status, rusage)\n\n\
6022Wait for completion of a child process.");
6023
6024static PyObject *
6025posix_wait3(PyObject *self, PyObject *args)
6026{
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 pid_t pid;
6028 int options;
6029 struct rusage ru;
6030 WAIT_TYPE status;
6031 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006032
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 if (!PyArg_ParseTuple(args, "i:wait3", &options))
6034 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006035
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 Py_BEGIN_ALLOW_THREADS
6037 pid = wait3(&status, options, &ru);
6038 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006039
Victor Stinner8c62be82010-05-06 00:08:46 +00006040 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006041}
6042#endif /* HAVE_WAIT3 */
6043
6044#ifdef HAVE_WAIT4
6045PyDoc_STRVAR(posix_wait4__doc__,
6046"wait4(pid, options) -> (pid, status, rusage)\n\n\
6047Wait for completion of a given child process.");
6048
6049static PyObject *
6050posix_wait4(PyObject *self, PyObject *args)
6051{
Victor Stinner8c62be82010-05-06 00:08:46 +00006052 pid_t pid;
6053 int options;
6054 struct rusage ru;
6055 WAIT_TYPE status;
6056 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006057
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
6059 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006060
Victor Stinner8c62be82010-05-06 00:08:46 +00006061 Py_BEGIN_ALLOW_THREADS
6062 pid = wait4(pid, &status, options, &ru);
6063 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006064
Victor Stinner8c62be82010-05-06 00:08:46 +00006065 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006066}
6067#endif /* HAVE_WAIT4 */
6068
Ross Lagerwall7807c352011-03-17 20:20:30 +02006069#if defined(HAVE_WAITID) && !defined(__APPLE__)
6070PyDoc_STRVAR(posix_waitid__doc__,
6071"waitid(idtype, id, options) -> waitid_result\n\n\
6072Wait for the completion of one or more child processes.\n\n\
6073idtype can be P_PID, P_PGID or P_ALL.\n\
6074id specifies the pid to wait on.\n\
6075options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6076or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6077Returns either waitid_result or None if WNOHANG is specified and there are\n\
6078no children in a waitable state.");
6079
6080static PyObject *
6081posix_waitid(PyObject *self, PyObject *args)
6082{
6083 PyObject *result;
6084 idtype_t idtype;
6085 id_t id;
6086 int options, res;
6087 siginfo_t si;
6088 si.si_pid = 0;
6089 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6090 return NULL;
6091 Py_BEGIN_ALLOW_THREADS
6092 res = waitid(idtype, id, &si, options);
6093 Py_END_ALLOW_THREADS
6094 if (res == -1)
6095 return posix_error();
6096
6097 if (si.si_pid == 0)
6098 Py_RETURN_NONE;
6099
6100 result = PyStructSequence_New(&WaitidResultType);
6101 if (!result)
6102 return NULL;
6103
6104 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
6105 PyStructSequence_SET_ITEM(result, 1, PyLong_FromPid(si.si_uid));
6106 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6107 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6108 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6109 if (PyErr_Occurred()) {
6110 Py_DECREF(result);
6111 return NULL;
6112 }
6113
6114 return result;
6115}
6116#endif
6117
Guido van Rossumb6775db1994-08-01 11:34:53 +00006118#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006119PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006120"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006121Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006122
Barry Warsaw53699e91996-12-10 23:23:01 +00006123static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006124posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006125{
Victor Stinner8c62be82010-05-06 00:08:46 +00006126 pid_t pid;
6127 int options;
6128 WAIT_TYPE status;
6129 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006130
Victor Stinner8c62be82010-05-06 00:08:46 +00006131 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6132 return NULL;
6133 Py_BEGIN_ALLOW_THREADS
6134 pid = waitpid(pid, &status, options);
6135 Py_END_ALLOW_THREADS
6136 if (pid == -1)
6137 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006138
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006140}
6141
Tim Petersab034fa2002-02-01 11:27:43 +00006142#elif defined(HAVE_CWAIT)
6143
6144/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006145PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006146"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006147"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006148
6149static PyObject *
6150posix_waitpid(PyObject *self, PyObject *args)
6151{
Victor Stinner8c62be82010-05-06 00:08:46 +00006152 Py_intptr_t pid;
6153 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006154
Victor Stinner8c62be82010-05-06 00:08:46 +00006155 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6156 return NULL;
6157 Py_BEGIN_ALLOW_THREADS
6158 pid = _cwait(&status, pid, options);
6159 Py_END_ALLOW_THREADS
6160 if (pid == -1)
6161 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006162
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 /* shift the status left a byte so this is more like the POSIX waitpid */
6164 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006165}
6166#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006167
Guido van Rossumad0ee831995-03-01 10:34:45 +00006168#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006169PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006170"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006172
Barry Warsaw53699e91996-12-10 23:23:01 +00006173static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006174posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006175{
Victor Stinner8c62be82010-05-06 00:08:46 +00006176 pid_t pid;
6177 WAIT_TYPE status;
6178 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006179
Victor Stinner8c62be82010-05-06 00:08:46 +00006180 Py_BEGIN_ALLOW_THREADS
6181 pid = wait(&status);
6182 Py_END_ALLOW_THREADS
6183 if (pid == -1)
6184 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006185
Victor Stinner8c62be82010-05-06 00:08:46 +00006186 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006187}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006188#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006189
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006190
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006191PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006192"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006193Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006194
Barry Warsaw53699e91996-12-10 23:23:01 +00006195static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006196posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006197{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006198#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00006199 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00006200#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006201#ifdef MS_WINDOWS
Brian Curtind25aef52011-06-13 15:16:04 -05006202 return posix_do_stat(self, args, "O&:lstat", win32_lstat, "U:lstat",
Brian Curtind40e6f72010-07-08 21:39:08 +00006203 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006204#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006206#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00006207#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006208}
6209
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006210
Guido van Rossumb6775db1994-08-01 11:34:53 +00006211#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006212PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006213"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006214Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006215
Barry Warsaw53699e91996-12-10 23:23:01 +00006216static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006217posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006218{
Victor Stinner8c62be82010-05-06 00:08:46 +00006219 PyObject* v;
6220 char buf[MAXPATHLEN];
6221 PyObject *opath;
6222 char *path;
6223 int n;
6224 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006225
Victor Stinner8c62be82010-05-06 00:08:46 +00006226 if (!PyArg_ParseTuple(args, "O&:readlink",
6227 PyUnicode_FSConverter, &opath))
6228 return NULL;
6229 path = PyBytes_AsString(opath);
6230 v = PySequence_GetItem(args, 0);
6231 if (v == NULL) {
6232 Py_DECREF(opath);
6233 return NULL;
6234 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00006235
Victor Stinner8c62be82010-05-06 00:08:46 +00006236 if (PyUnicode_Check(v)) {
6237 arg_is_unicode = 1;
6238 }
6239 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006240
Victor Stinner8c62be82010-05-06 00:08:46 +00006241 Py_BEGIN_ALLOW_THREADS
6242 n = readlink(path, buf, (int) sizeof buf);
6243 Py_END_ALLOW_THREADS
6244 if (n < 0)
6245 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00006246
Victor Stinner8c62be82010-05-06 00:08:46 +00006247 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00006248 if (arg_is_unicode)
6249 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
6250 else
6251 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006252}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006253#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006254
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006255
Brian Curtin52173d42010-12-02 18:29:18 +00006256#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006257PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006258"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00006259Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006260
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006261static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006262posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006263{
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006265}
6266#endif /* HAVE_SYMLINK */
6267
Brian Curtind40e6f72010-07-08 21:39:08 +00006268#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6269
6270PyDoc_STRVAR(win_readlink__doc__,
6271"readlink(path) -> path\n\n\
6272Return a string representing the path to which the symbolic link points.");
6273
Brian Curtind40e6f72010-07-08 21:39:08 +00006274/* Windows readlink implementation */
6275static PyObject *
6276win_readlink(PyObject *self, PyObject *args)
6277{
6278 wchar_t *path;
6279 DWORD n_bytes_returned;
6280 DWORD io_result;
6281 PyObject *result;
6282 HANDLE reparse_point_handle;
6283
6284 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6285 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6286 wchar_t *print_name;
6287
6288 if (!PyArg_ParseTuple(args,
6289 "u:readlink",
6290 &path))
6291 return NULL;
6292
6293 /* First get a handle to the reparse point */
6294 Py_BEGIN_ALLOW_THREADS
6295 reparse_point_handle = CreateFileW(
6296 path,
6297 0,
6298 0,
6299 0,
6300 OPEN_EXISTING,
6301 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6302 0);
6303 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006304
Brian Curtind40e6f72010-07-08 21:39:08 +00006305 if (reparse_point_handle==INVALID_HANDLE_VALUE)
6306 {
6307 return win32_error_unicode("readlink", path);
6308 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006309
Brian Curtind40e6f72010-07-08 21:39:08 +00006310 Py_BEGIN_ALLOW_THREADS
6311 /* New call DeviceIoControl to read the reparse point */
6312 io_result = DeviceIoControl(
6313 reparse_point_handle,
6314 FSCTL_GET_REPARSE_POINT,
6315 0, 0, /* in buffer */
6316 target_buffer, sizeof(target_buffer),
6317 &n_bytes_returned,
6318 0 /* we're not using OVERLAPPED_IO */
6319 );
6320 CloseHandle(reparse_point_handle);
6321 Py_END_ALLOW_THREADS
6322
6323 if (io_result==0)
6324 {
6325 return win32_error_unicode("readlink", path);
6326 }
6327
6328 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6329 {
6330 PyErr_SetString(PyExc_ValueError,
6331 "not a symbolic link");
6332 return NULL;
6333 }
Brian Curtin74e45612010-07-09 15:58:59 +00006334 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6335 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6336
6337 result = PyUnicode_FromWideChar(print_name,
6338 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006339 return result;
6340}
6341
6342#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6343
Brian Curtin52173d42010-12-02 18:29:18 +00006344#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00006345
6346/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6347static int has_CreateSymbolicLinkW = 0;
6348static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
6349static int
6350check_CreateSymbolicLinkW()
6351{
6352 HINSTANCE hKernel32;
6353 /* only recheck */
6354 if (has_CreateSymbolicLinkW)
6355 return has_CreateSymbolicLinkW;
6356 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00006357 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6358 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00006359 if (Py_CreateSymbolicLinkW)
6360 has_CreateSymbolicLinkW = 1;
6361 return has_CreateSymbolicLinkW;
6362}
6363
6364PyDoc_STRVAR(win_symlink__doc__,
6365"symlink(src, dst, target_is_directory=False)\n\n\
6366Create a symbolic link pointing to src named dst.\n\
6367target_is_directory is required if the target is to be interpreted as\n\
6368a directory.\n\
6369This function requires Windows 6.0 or greater, and raises a\n\
6370NotImplementedError otherwise.");
6371
6372static PyObject *
6373win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
6374{
6375 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
6376 PyObject *src, *dest;
6377 int target_is_directory = 0;
6378 DWORD res;
6379 WIN32_FILE_ATTRIBUTE_DATA src_info;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006380
Brian Curtind40e6f72010-07-08 21:39:08 +00006381 if (!check_CreateSymbolicLinkW())
6382 {
6383 /* raise NotImplementedError */
6384 return PyErr_Format(PyExc_NotImplementedError,
6385 "CreateSymbolicLinkW not found");
6386 }
6387 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
6388 kwlist, &src, &dest, &target_is_directory))
6389 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00006390
6391 if (win32_can_symlink == 0)
6392 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
6393
Brian Curtind40e6f72010-07-08 21:39:08 +00006394 if (!convert_to_unicode(&src)) { return NULL; }
6395 if (!convert_to_unicode(&dest)) {
6396 Py_DECREF(src);
6397 return NULL;
6398 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006399
Brian Curtind40e6f72010-07-08 21:39:08 +00006400 /* if src is a directory, ensure target_is_directory==1 */
6401 if(
6402 GetFileAttributesExW(
6403 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
6404 ))
6405 {
6406 target_is_directory = target_is_directory ||
6407 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
6408 }
6409
6410 Py_BEGIN_ALLOW_THREADS
6411 res = Py_CreateSymbolicLinkW(
6412 PyUnicode_AsUnicode(dest),
6413 PyUnicode_AsUnicode(src),
6414 target_is_directory);
6415 Py_END_ALLOW_THREADS
6416 Py_DECREF(src);
6417 Py_DECREF(dest);
6418 if (!res)
6419 {
6420 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
6421 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006422
Brian Curtind40e6f72010-07-08 21:39:08 +00006423 Py_INCREF(Py_None);
6424 return Py_None;
6425}
Brian Curtin52173d42010-12-02 18:29:18 +00006426#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006427
6428#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00006429#if defined(PYCC_VACPP) && defined(PYOS_OS2)
6430static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00006431system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006432{
6433 ULONG value = 0;
6434
6435 Py_BEGIN_ALLOW_THREADS
6436 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
6437 Py_END_ALLOW_THREADS
6438
6439 return value;
6440}
6441
6442static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006443posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00006444{
Guido van Rossumd48f2521997-12-05 22:19:34 +00006445 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00006446 return Py_BuildValue("ddddd",
6447 (double)0 /* t.tms_utime / HZ */,
6448 (double)0 /* t.tms_stime / HZ */,
6449 (double)0 /* t.tms_cutime / HZ */,
6450 (double)0 /* t.tms_cstime / HZ */,
6451 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00006452}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006453#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00006454#define NEED_TICKS_PER_SECOND
6455static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00006456static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006457posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00006458{
Victor Stinner8c62be82010-05-06 00:08:46 +00006459 struct tms t;
6460 clock_t c;
6461 errno = 0;
6462 c = times(&t);
6463 if (c == (clock_t) -1)
6464 return posix_error();
6465 return Py_BuildValue("ddddd",
6466 (double)t.tms_utime / ticks_per_second,
6467 (double)t.tms_stime / ticks_per_second,
6468 (double)t.tms_cutime / ticks_per_second,
6469 (double)t.tms_cstime / ticks_per_second,
6470 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00006471}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006472#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00006473#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006474
6475
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006476#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006477#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00006478static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006479posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006480{
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 FILETIME create, exit, kernel, user;
6482 HANDLE hProc;
6483 hProc = GetCurrentProcess();
6484 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6485 /* The fields of a FILETIME structure are the hi and lo part
6486 of a 64-bit value expressed in 100 nanosecond units.
6487 1e7 is one second in such units; 1e-7 the inverse.
6488 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6489 */
6490 return Py_BuildValue(
6491 "ddddd",
6492 (double)(user.dwHighDateTime*429.4967296 +
6493 user.dwLowDateTime*1e-7),
6494 (double)(kernel.dwHighDateTime*429.4967296 +
6495 kernel.dwLowDateTime*1e-7),
6496 (double)0,
6497 (double)0,
6498 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006499}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006500#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006501
6502#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006503PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006504"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006505Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006506#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00006507
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006508
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006509#ifdef HAVE_GETSID
6510PyDoc_STRVAR(posix_getsid__doc__,
6511"getsid(pid) -> sid\n\n\
6512Call the system call getsid().");
6513
6514static PyObject *
6515posix_getsid(PyObject *self, PyObject *args)
6516{
Victor Stinner8c62be82010-05-06 00:08:46 +00006517 pid_t pid;
6518 int sid;
6519 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
6520 return NULL;
6521 sid = getsid(pid);
6522 if (sid < 0)
6523 return posix_error();
6524 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00006525}
6526#endif /* HAVE_GETSID */
6527
6528
Guido van Rossumb6775db1994-08-01 11:34:53 +00006529#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006530PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006531"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006532Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006533
Barry Warsaw53699e91996-12-10 23:23:01 +00006534static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006535posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006536{
Victor Stinner8c62be82010-05-06 00:08:46 +00006537 if (setsid() < 0)
6538 return posix_error();
6539 Py_INCREF(Py_None);
6540 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006541}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006542#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006543
Guido van Rossumb6775db1994-08-01 11:34:53 +00006544#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006545PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006546"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006547Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006548
Barry Warsaw53699e91996-12-10 23:23:01 +00006549static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006550posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006551{
Victor Stinner8c62be82010-05-06 00:08:46 +00006552 pid_t pid;
6553 int pgrp;
6554 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
6555 return NULL;
6556 if (setpgid(pid, pgrp) < 0)
6557 return posix_error();
6558 Py_INCREF(Py_None);
6559 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006560}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006561#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00006562
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006563
Guido van Rossumb6775db1994-08-01 11:34:53 +00006564#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006565PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006566"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006567Return the process group associated with the terminal given by a fd.");
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_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006571{
Victor Stinner8c62be82010-05-06 00:08:46 +00006572 int fd;
6573 pid_t pgid;
6574 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
6575 return NULL;
6576 pgid = tcgetpgrp(fd);
6577 if (pgid < 0)
6578 return posix_error();
6579 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00006580}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006581#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00006582
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006583
Guido van Rossumb6775db1994-08-01 11:34:53 +00006584#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006585PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006586"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006587Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006588
Barry Warsaw53699e91996-12-10 23:23:01 +00006589static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006590posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00006591{
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 int fd;
6593 pid_t pgid;
6594 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
6595 return NULL;
6596 if (tcsetpgrp(fd, pgid) < 0)
6597 return posix_error();
6598 Py_INCREF(Py_None);
6599 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00006600}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006601#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00006602
Guido van Rossum687dd131993-05-17 08:34:16 +00006603/* Functions acting on file descriptors */
6604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006605PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006606"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006607Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006608
Barry Warsaw53699e91996-12-10 23:23:01 +00006609static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006610posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006611{
Victor Stinner8c62be82010-05-06 00:08:46 +00006612 PyObject *ofile;
6613 char *file;
6614 int flag;
6615 int mode = 0777;
6616 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006617
6618#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 PyUnicodeObject *po;
Victor Stinner26de69d2011-06-17 15:15:38 +02006620 if (PyArg_ParseTuple(args, "Ui|i:open", &po, &flag, &mode)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006621 Py_BEGIN_ALLOW_THREADS
6622 /* PyUnicode_AS_UNICODE OK without thread
6623 lock as it is a simple dereference. */
6624 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
6625 Py_END_ALLOW_THREADS
6626 if (fd < 0)
6627 return posix_error();
6628 return PyLong_FromLong((long)fd);
6629 }
6630 /* Drop the argument parsing error as narrow strings
6631 are also valid. */
6632 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006633#endif
6634
Victor Stinner26de69d2011-06-17 15:15:38 +02006635 if (!PyArg_ParseTuple(args, "O&i|i:open",
Victor Stinner8c62be82010-05-06 00:08:46 +00006636 PyUnicode_FSConverter, &ofile,
6637 &flag, &mode))
6638 return NULL;
6639 file = PyBytes_AsString(ofile);
6640 Py_BEGIN_ALLOW_THREADS
6641 fd = open(file, flag, mode);
6642 Py_END_ALLOW_THREADS
6643 if (fd < 0)
6644 return posix_error_with_allocated_filename(ofile);
6645 Py_DECREF(ofile);
6646 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006647}
6648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006650PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006651"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006652Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006653
Barry Warsaw53699e91996-12-10 23:23:01 +00006654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006655posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006656{
Victor Stinner8c62be82010-05-06 00:08:46 +00006657 int fd, res;
6658 if (!PyArg_ParseTuple(args, "i:close", &fd))
6659 return NULL;
6660 if (!_PyVerify_fd(fd))
6661 return posix_error();
6662 Py_BEGIN_ALLOW_THREADS
6663 res = close(fd);
6664 Py_END_ALLOW_THREADS
6665 if (res < 0)
6666 return posix_error();
6667 Py_INCREF(Py_None);
6668 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006669}
6670
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006671
Victor Stinner8c62be82010-05-06 00:08:46 +00006672PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00006673"closerange(fd_low, fd_high)\n\n\
6674Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
6675
6676static PyObject *
6677posix_closerange(PyObject *self, PyObject *args)
6678{
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 int fd_from, fd_to, i;
6680 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
6681 return NULL;
6682 Py_BEGIN_ALLOW_THREADS
6683 for (i = fd_from; i < fd_to; i++)
6684 if (_PyVerify_fd(i))
6685 close(i);
6686 Py_END_ALLOW_THREADS
6687 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00006688}
6689
6690
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006691PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006692"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006693Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006694
Barry Warsaw53699e91996-12-10 23:23:01 +00006695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006696posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006697{
Victor Stinner8c62be82010-05-06 00:08:46 +00006698 int fd;
6699 if (!PyArg_ParseTuple(args, "i:dup", &fd))
6700 return NULL;
6701 if (!_PyVerify_fd(fd))
6702 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 if (fd < 0)
6705 return posix_error();
6706 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00006707}
6708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006710PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00006711"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006712Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006713
Barry Warsaw53699e91996-12-10 23:23:01 +00006714static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006715posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006716{
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 int fd, fd2, res;
6718 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
6719 return NULL;
6720 if (!_PyVerify_fd_dup2(fd, fd2))
6721 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006722 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 if (res < 0)
6724 return posix_error();
6725 Py_INCREF(Py_None);
6726 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00006727}
6728
Ross Lagerwall7807c352011-03-17 20:20:30 +02006729#ifdef HAVE_LOCKF
6730PyDoc_STRVAR(posix_lockf__doc__,
6731"lockf(fd, cmd, len)\n\n\
6732Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
6733fd is an open file descriptor.\n\
6734cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
6735F_TEST.\n\
6736len specifies the section of the file to lock.");
6737
6738static PyObject *
6739posix_lockf(PyObject *self, PyObject *args)
6740{
6741 int fd, cmd, res;
6742 off_t len;
6743 if (!PyArg_ParseTuple(args, "iiO&:lockf",
6744 &fd, &cmd, _parse_off_t, &len))
6745 return NULL;
6746
6747 Py_BEGIN_ALLOW_THREADS
6748 res = lockf(fd, cmd, len);
6749 Py_END_ALLOW_THREADS
6750
6751 if (res < 0)
6752 return posix_error();
6753
6754 Py_RETURN_NONE;
6755}
6756#endif
6757
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006759PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006760"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006761Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006762
Barry Warsaw53699e91996-12-10 23:23:01 +00006763static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006764posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006765{
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006767#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006768 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006769#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006770 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00006771#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02006772 PyObject *posobj;
6773 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00006775#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00006776 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
6777 switch (how) {
6778 case 0: how = SEEK_SET; break;
6779 case 1: how = SEEK_CUR; break;
6780 case 2: how = SEEK_END; break;
6781 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006782#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006783
Ross Lagerwall8e749672011-03-17 21:54:07 +02006784#if !defined(HAVE_LARGEFILE_SUPPORT)
6785 pos = PyLong_AsLong(posobj);
6786#else
6787 pos = PyLong_AsLongLong(posobj);
6788#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 if (PyErr_Occurred())
6790 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00006791
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 if (!_PyVerify_fd(fd))
6793 return posix_error();
6794 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006795#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006797#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00006799#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 Py_END_ALLOW_THREADS
6801 if (res < 0)
6802 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00006803
6804#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006806#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006808#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00006809}
6810
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006812PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006813"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006814Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006815
Barry Warsaw53699e91996-12-10 23:23:01 +00006816static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006817posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00006818{
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 int fd, size;
6820 Py_ssize_t n;
6821 PyObject *buffer;
6822 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
6823 return NULL;
6824 if (size < 0) {
6825 errno = EINVAL;
6826 return posix_error();
6827 }
6828 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6829 if (buffer == NULL)
6830 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00006831 if (!_PyVerify_fd(fd)) {
6832 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00006833 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00006834 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 Py_BEGIN_ALLOW_THREADS
6836 n = read(fd, PyBytes_AS_STRING(buffer), size);
6837 Py_END_ALLOW_THREADS
6838 if (n < 0) {
6839 Py_DECREF(buffer);
6840 return posix_error();
6841 }
6842 if (n != size)
6843 _PyBytes_Resize(&buffer, n);
6844 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00006845}
6846
Ross Lagerwall7807c352011-03-17 20:20:30 +02006847#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
6848 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006849static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006850iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
6851{
6852 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006853 Py_ssize_t blen, total = 0;
6854
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006855 *iov = PyMem_New(struct iovec, cnt);
6856 if (*iov == NULL) {
6857 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006858 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006859 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006860
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006861 *buf = PyMem_New(Py_buffer, cnt);
6862 if (*buf == NULL) {
6863 PyMem_Del(*iov);
6864 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006865 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006866 }
6867
6868 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006869 PyObject *item = PySequence_GetItem(seq, i);
6870 if (item == NULL)
6871 goto fail;
6872 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
6873 Py_DECREF(item);
6874 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006875 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006876 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006877 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006878 blen = (*buf)[i].len;
6879 (*iov)[i].iov_len = blen;
6880 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006881 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00006882 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02006883
6884fail:
6885 PyMem_Del(*iov);
6886 for (j = 0; j < i; j++) {
6887 PyBuffer_Release(&(*buf)[j]);
6888 }
6889 PyMem_Del(*buf);
6890 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006891}
6892
6893static void
6894iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
6895{
6896 int i;
6897 PyMem_Del(iov);
6898 for (i = 0; i < cnt; i++) {
6899 PyBuffer_Release(&buf[i]);
6900 }
6901 PyMem_Del(buf);
6902}
6903#endif
6904
Ross Lagerwall7807c352011-03-17 20:20:30 +02006905#ifdef HAVE_READV
6906PyDoc_STRVAR(posix_readv__doc__,
6907"readv(fd, buffers) -> bytesread\n\n\
6908Read from a file descriptor into a number of writable buffers. buffers\n\
6909is an arbitrary sequence of writable buffers.\n\
6910Returns the total number of bytes read.");
6911
6912static PyObject *
6913posix_readv(PyObject *self, PyObject *args)
6914{
6915 int fd, cnt;
6916 Py_ssize_t n;
6917 PyObject *seq;
6918 struct iovec *iov;
6919 Py_buffer *buf;
6920
6921 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
6922 return NULL;
6923 if (!PySequence_Check(seq)) {
6924 PyErr_SetString(PyExc_TypeError,
6925 "readv() arg 2 must be a sequence");
6926 return NULL;
6927 }
6928 cnt = PySequence_Size(seq);
6929
6930 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
6931 return NULL;
6932
6933 Py_BEGIN_ALLOW_THREADS
6934 n = readv(fd, iov, cnt);
6935 Py_END_ALLOW_THREADS
6936
6937 iov_cleanup(iov, buf, cnt);
6938 return PyLong_FromSsize_t(n);
6939}
6940#endif
6941
6942#ifdef HAVE_PREAD
6943PyDoc_STRVAR(posix_pread__doc__,
6944"pread(fd, buffersize, offset) -> string\n\n\
6945Read from a file descriptor, fd, at a position of offset. It will read up\n\
6946to buffersize number of bytes. The file offset remains unchanged.");
6947
6948static PyObject *
6949posix_pread(PyObject *self, PyObject *args)
6950{
6951 int fd, size;
6952 off_t offset;
6953 Py_ssize_t n;
6954 PyObject *buffer;
6955 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
6956 return NULL;
6957
6958 if (size < 0) {
6959 errno = EINVAL;
6960 return posix_error();
6961 }
6962 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
6963 if (buffer == NULL)
6964 return NULL;
6965 if (!_PyVerify_fd(fd)) {
6966 Py_DECREF(buffer);
6967 return posix_error();
6968 }
6969 Py_BEGIN_ALLOW_THREADS
6970 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
6971 Py_END_ALLOW_THREADS
6972 if (n < 0) {
6973 Py_DECREF(buffer);
6974 return posix_error();
6975 }
6976 if (n != size)
6977 _PyBytes_Resize(&buffer, n);
6978 return buffer;
6979}
6980#endif
6981
6982PyDoc_STRVAR(posix_write__doc__,
6983"write(fd, string) -> byteswritten\n\n\
6984Write a string to a file descriptor.");
6985
6986static PyObject *
6987posix_write(PyObject *self, PyObject *args)
6988{
6989 Py_buffer pbuf;
6990 int fd;
6991 Py_ssize_t size, len;
6992
6993 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
6994 return NULL;
6995 if (!_PyVerify_fd(fd)) {
6996 PyBuffer_Release(&pbuf);
6997 return posix_error();
6998 }
6999 len = pbuf.len;
7000 Py_BEGIN_ALLOW_THREADS
7001#if defined(MS_WIN64) || defined(MS_WINDOWS)
7002 if (len > INT_MAX)
7003 len = INT_MAX;
7004 size = write(fd, pbuf.buf, (int)len);
7005#else
7006 size = write(fd, pbuf.buf, len);
7007#endif
7008 Py_END_ALLOW_THREADS
7009 PyBuffer_Release(&pbuf);
7010 if (size < 0)
7011 return posix_error();
7012 return PyLong_FromSsize_t(size);
7013}
7014
7015#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007016PyDoc_STRVAR(posix_sendfile__doc__,
7017"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7018sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7019 -> byteswritten\n\
7020Copy nbytes bytes from file descriptor in to file descriptor out.");
7021
7022static PyObject *
7023posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7024{
7025 int in, out;
7026 Py_ssize_t ret;
7027 off_t offset;
7028
7029#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7030#ifndef __APPLE__
7031 Py_ssize_t len;
7032#endif
7033 PyObject *headers = NULL, *trailers = NULL;
7034 Py_buffer *hbuf, *tbuf;
7035 off_t sbytes;
7036 struct sf_hdtr sf;
7037 int flags = 0;
7038 sf.headers = NULL;
7039 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007040 static char *keywords[] = {"out", "in",
7041 "offset", "count",
7042 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007043
7044#ifdef __APPLE__
7045 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007046 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007047#else
7048 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007049 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007050#endif
7051 &headers, &trailers, &flags))
7052 return NULL;
7053 if (headers != NULL) {
7054 if (!PySequence_Check(headers)) {
7055 PyErr_SetString(PyExc_TypeError,
7056 "sendfile() headers must be a sequence or None");
7057 return NULL;
7058 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007059 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007060 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007061 if (sf.hdr_cnt > 0 &&
7062 !(i = iov_setup(&(sf.headers), &hbuf,
7063 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007064 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007065#ifdef __APPLE__
7066 sbytes += i;
7067#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007068 }
7069 }
7070 if (trailers != NULL) {
7071 if (!PySequence_Check(trailers)) {
7072 PyErr_SetString(PyExc_TypeError,
7073 "sendfile() trailers must be a sequence or None");
7074 return NULL;
7075 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007076 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007077 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007078 if (sf.trl_cnt > 0 &&
7079 !(i = iov_setup(&(sf.trailers), &tbuf,
7080 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007081 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007082#ifdef __APPLE__
7083 sbytes += i;
7084#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007085 }
7086 }
7087
7088 Py_BEGIN_ALLOW_THREADS
7089#ifdef __APPLE__
7090 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7091#else
7092 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7093#endif
7094 Py_END_ALLOW_THREADS
7095
7096 if (sf.headers != NULL)
7097 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7098 if (sf.trailers != NULL)
7099 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7100
7101 if (ret < 0) {
7102 if ((errno == EAGAIN) || (errno == EBUSY)) {
7103 if (sbytes != 0) {
7104 // some data has been sent
7105 goto done;
7106 }
7107 else {
7108 // no data has been sent; upper application is supposed
7109 // to retry on EAGAIN or EBUSY
7110 return posix_error();
7111 }
7112 }
7113 return posix_error();
7114 }
7115 goto done;
7116
7117done:
7118 #if !defined(HAVE_LARGEFILE_SUPPORT)
7119 return Py_BuildValue("l", sbytes);
7120 #else
7121 return Py_BuildValue("L", sbytes);
7122 #endif
7123
7124#else
7125 Py_ssize_t count;
7126 PyObject *offobj;
7127 static char *keywords[] = {"out", "in",
7128 "offset", "count", NULL};
7129 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7130 keywords, &out, &in, &offobj, &count))
7131 return NULL;
7132#ifdef linux
7133 if (offobj == Py_None) {
7134 Py_BEGIN_ALLOW_THREADS
7135 ret = sendfile(out, in, NULL, count);
7136 Py_END_ALLOW_THREADS
7137 if (ret < 0)
7138 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007139 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007140 }
7141#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007142 if (!_parse_off_t(offobj, &offset))
7143 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007144 Py_BEGIN_ALLOW_THREADS
7145 ret = sendfile(out, in, &offset, count);
7146 Py_END_ALLOW_THREADS
7147 if (ret < 0)
7148 return posix_error();
7149 return Py_BuildValue("n", ret);
7150#endif
7151}
7152#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007154PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007155"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007156Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007157
Barry Warsaw53699e91996-12-10 23:23:01 +00007158static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007159posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007160{
Victor Stinner8c62be82010-05-06 00:08:46 +00007161 int fd;
7162 STRUCT_STAT st;
7163 int res;
7164 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
7165 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007166#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 /* on OpenVMS we must ensure that all bytes are written to the file */
7168 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007169#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 if (!_PyVerify_fd(fd))
7171 return posix_error();
7172 Py_BEGIN_ALLOW_THREADS
7173 res = FSTAT(fd, &st);
7174 Py_END_ALLOW_THREADS
7175 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007176#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00007178#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007179 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007180#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 }
Tim Peters5aa91602002-01-30 05:46:57 +00007182
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007184}
7185
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007186PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007187"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007188Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007189connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007190
7191static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007192posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007193{
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 int fd;
7195 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7196 return NULL;
7197 if (!_PyVerify_fd(fd))
7198 return PyBool_FromLong(0);
7199 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007200}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007201
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007202#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007203PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007204"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007205Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007206
Barry Warsaw53699e91996-12-10 23:23:01 +00007207static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007208posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007209{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007210#if defined(PYOS_OS2)
7211 HFILE read, write;
7212 APIRET rc;
7213
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007214 rc = DosCreatePipe( &read, &write, 4096);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007215 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007216 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007217
7218 return Py_BuildValue("(ii)", read, write);
7219#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007220#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007221 int fds[2];
7222 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007223 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 if (res != 0)
7225 return posix_error();
7226 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007227#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 HANDLE read, write;
7229 int read_fd, write_fd;
7230 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 if (!ok)
7233 return win32_error("CreatePipe", NULL);
7234 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7235 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7236 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007237#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007238#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007239}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007240#endif /* HAVE_PIPE */
7241
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007242#ifdef HAVE_PIPE2
7243PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007244"pipe2(flags) -> (read_end, write_end)\n\n\
7245Create a pipe with flags set atomically.\n\
7246flags can be constructed by ORing together one or more of these values:\n\
7247O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007248");
7249
7250static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007251posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007252{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007253 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007254 int fds[2];
7255 int res;
7256
Charles-François Natali368f34b2011-06-06 19:49:47 +02007257 flags = PyLong_AsLong(arg);
7258 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007259 return NULL;
7260
7261 res = pipe2(fds, flags);
7262 if (res != 0)
7263 return posix_error();
7264 return Py_BuildValue("(ii)", fds[0], fds[1]);
7265}
7266#endif /* HAVE_PIPE2 */
7267
Ross Lagerwall7807c352011-03-17 20:20:30 +02007268#ifdef HAVE_WRITEV
7269PyDoc_STRVAR(posix_writev__doc__,
7270"writev(fd, buffers) -> byteswritten\n\n\
7271Write the contents of buffers to a file descriptor, where buffers is an\n\
7272arbitrary sequence of buffers.\n\
7273Returns the total bytes written.");
7274
7275static PyObject *
7276posix_writev(PyObject *self, PyObject *args)
7277{
7278 int fd, cnt;
7279 Py_ssize_t res;
7280 PyObject *seq;
7281 struct iovec *iov;
7282 Py_buffer *buf;
7283 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7284 return NULL;
7285 if (!PySequence_Check(seq)) {
7286 PyErr_SetString(PyExc_TypeError,
7287 "writev() arg 2 must be a sequence");
7288 return NULL;
7289 }
7290 cnt = PySequence_Size(seq);
7291
7292 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7293 return NULL;
7294 }
7295
7296 Py_BEGIN_ALLOW_THREADS
7297 res = writev(fd, iov, cnt);
7298 Py_END_ALLOW_THREADS
7299
7300 iov_cleanup(iov, buf, cnt);
7301 return PyLong_FromSsize_t(res);
7302}
7303#endif
7304
7305#ifdef HAVE_PWRITE
7306PyDoc_STRVAR(posix_pwrite__doc__,
7307"pwrite(fd, string, offset) -> byteswritten\n\n\
7308Write string to a file descriptor, fd, from offset, leaving the file\n\
7309offset unchanged.");
7310
7311static PyObject *
7312posix_pwrite(PyObject *self, PyObject *args)
7313{
7314 Py_buffer pbuf;
7315 int fd;
7316 off_t offset;
7317 Py_ssize_t size;
7318
7319 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7320 return NULL;
7321
7322 if (!_PyVerify_fd(fd)) {
7323 PyBuffer_Release(&pbuf);
7324 return posix_error();
7325 }
7326 Py_BEGIN_ALLOW_THREADS
7327 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7328 Py_END_ALLOW_THREADS
7329 PyBuffer_Release(&pbuf);
7330 if (size < 0)
7331 return posix_error();
7332 return PyLong_FromSsize_t(size);
7333}
7334#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007335
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007336#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007337PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007338"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007339Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007340
Barry Warsaw53699e91996-12-10 23:23:01 +00007341static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007342posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007343{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007344 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 char *filename;
7346 int mode = 0666;
7347 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007348 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
7349 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007351 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007352 Py_BEGIN_ALLOW_THREADS
7353 res = mkfifo(filename, mode);
7354 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007355 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007356 if (res < 0)
7357 return posix_error();
7358 Py_INCREF(Py_None);
7359 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007360}
7361#endif
7362
7363
Neal Norwitz11690112002-07-30 01:08:28 +00007364#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007365PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00007366"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007367Create a filesystem node (file, device special file or named pipe)\n\
7368named filename. mode specifies both the permissions to use and the\n\
7369type of node to be created, being combined (bitwise OR) with one of\n\
7370S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007371device defines the newly created device special file (probably using\n\
7372os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007373
7374
7375static PyObject *
7376posix_mknod(PyObject *self, PyObject *args)
7377{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007378 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00007379 char *filename;
7380 int mode = 0600;
7381 int device = 0;
7382 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007383 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
7384 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007386 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007387 Py_BEGIN_ALLOW_THREADS
7388 res = mknod(filename, mode, device);
7389 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00007390 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00007391 if (res < 0)
7392 return posix_error();
7393 Py_INCREF(Py_None);
7394 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007395}
7396#endif
7397
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007398#ifdef HAVE_DEVICE_MACROS
7399PyDoc_STRVAR(posix_major__doc__,
7400"major(device) -> major number\n\
7401Extracts a device major number from a raw device number.");
7402
7403static PyObject *
7404posix_major(PyObject *self, PyObject *args)
7405{
Victor Stinner8c62be82010-05-06 00:08:46 +00007406 int device;
7407 if (!PyArg_ParseTuple(args, "i:major", &device))
7408 return NULL;
7409 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007410}
7411
7412PyDoc_STRVAR(posix_minor__doc__,
7413"minor(device) -> minor number\n\
7414Extracts a device minor number from a raw device number.");
7415
7416static PyObject *
7417posix_minor(PyObject *self, PyObject *args)
7418{
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 int device;
7420 if (!PyArg_ParseTuple(args, "i:minor", &device))
7421 return NULL;
7422 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007423}
7424
7425PyDoc_STRVAR(posix_makedev__doc__,
7426"makedev(major, minor) -> device number\n\
7427Composes a raw device number from the major and minor device numbers.");
7428
7429static PyObject *
7430posix_makedev(PyObject *self, PyObject *args)
7431{
Victor Stinner8c62be82010-05-06 00:08:46 +00007432 int major, minor;
7433 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
7434 return NULL;
7435 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007436}
7437#endif /* device macros */
7438
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007439
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007440#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007441PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007442"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007443Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007444
Barry Warsaw53699e91996-12-10 23:23:01 +00007445static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007446posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007447{
Victor Stinner8c62be82010-05-06 00:08:46 +00007448 int fd;
7449 off_t length;
7450 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007451
Ross Lagerwall7807c352011-03-17 20:20:30 +02007452 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007454
Victor Stinner8c62be82010-05-06 00:08:46 +00007455 Py_BEGIN_ALLOW_THREADS
7456 res = ftruncate(fd, length);
7457 Py_END_ALLOW_THREADS
7458 if (res < 0)
7459 return posix_error();
7460 Py_INCREF(Py_None);
7461 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007462}
7463#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007464
Ross Lagerwall7807c352011-03-17 20:20:30 +02007465#ifdef HAVE_TRUNCATE
7466PyDoc_STRVAR(posix_truncate__doc__,
7467"truncate(path, length)\n\n\
7468Truncate the file given by path to length bytes.");
7469
7470static PyObject *
7471posix_truncate(PyObject *self, PyObject *args)
7472{
7473 PyObject *opath;
7474 const char *path;
7475 off_t length;
7476 int res;
7477
7478 if (!PyArg_ParseTuple(args, "O&O&:truncate",
7479 PyUnicode_FSConverter, &opath, _parse_off_t, &length))
7480 return NULL;
7481 path = PyBytes_AsString(opath);
7482
7483 Py_BEGIN_ALLOW_THREADS
7484 res = truncate(path, length);
7485 Py_END_ALLOW_THREADS
7486 Py_DECREF(opath);
7487 if (res < 0)
7488 return posix_error();
7489 Py_RETURN_NONE;
7490}
7491#endif
7492
7493#ifdef HAVE_POSIX_FALLOCATE
7494PyDoc_STRVAR(posix_posix_fallocate__doc__,
7495"posix_fallocate(fd, offset, len)\n\n\
7496Ensures that enough disk space is allocated for the file specified by fd\n\
7497starting from offset and continuing for len bytes.");
7498
7499static PyObject *
7500posix_posix_fallocate(PyObject *self, PyObject *args)
7501{
7502 off_t len, offset;
7503 int res, fd;
7504
7505 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
7506 &fd, _parse_off_t, &offset, _parse_off_t, &len))
7507 return NULL;
7508
7509 Py_BEGIN_ALLOW_THREADS
7510 res = posix_fallocate(fd, offset, len);
7511 Py_END_ALLOW_THREADS
7512 if (res != 0) {
7513 errno = res;
7514 return posix_error();
7515 }
7516 Py_RETURN_NONE;
7517}
7518#endif
7519
7520#ifdef HAVE_POSIX_FADVISE
7521PyDoc_STRVAR(posix_posix_fadvise__doc__,
7522"posix_fadvise(fd, offset, len, advice)\n\n\
7523Announces an intention to access data in a specific pattern thus allowing\n\
7524the kernel to make optimizations.\n\
7525The advice applies to the region of the file specified by fd starting at\n\
7526offset and continuing for len bytes.\n\
7527advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
7528POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
7529POSIX_FADV_DONTNEED.");
7530
7531static PyObject *
7532posix_posix_fadvise(PyObject *self, PyObject *args)
7533{
7534 off_t len, offset;
7535 int res, fd, advice;
7536
7537 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
7538 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
7539 return NULL;
7540
7541 Py_BEGIN_ALLOW_THREADS
7542 res = posix_fadvise(fd, offset, len, advice);
7543 Py_END_ALLOW_THREADS
7544 if (res != 0) {
7545 errno = res;
7546 return posix_error();
7547 }
7548 Py_RETURN_NONE;
7549}
7550#endif
7551
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007552#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007553PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007554"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007555Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007556
Fred Drake762e2061999-08-26 17:23:54 +00007557/* Save putenv() parameters as values here, so we can collect them when they
7558 * get re-set with another call for the same key. */
7559static PyObject *posix_putenv_garbage;
7560
Tim Peters5aa91602002-01-30 05:46:57 +00007561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007562posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007563{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007564#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007565 wchar_t *s1, *s2;
7566 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007567#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007568 PyObject *os1, *os2;
7569 char *s1, *s2;
7570 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007571#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007572 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007574
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007575#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007576 if (!PyArg_ParseTuple(args,
7577 "uu:putenv",
7578 &s1, &s2))
7579 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00007580#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007581 if (!PyArg_ParseTuple(args,
7582 "O&O&:putenv",
7583 PyUnicode_FSConverter, &os1,
7584 PyUnicode_FSConverter, &os2))
7585 return NULL;
7586 s1 = PyBytes_AsString(os1);
7587 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007588#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007589
7590#if defined(PYOS_OS2)
7591 if (stricmp(s1, "BEGINLIBPATH") == 0) {
7592 APIRET rc;
7593
Guido van Rossumd48f2521997-12-05 22:19:34 +00007594 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007595 if (rc != NO_ERROR) {
7596 os2_error(rc);
7597 goto error;
7598 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007599
7600 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
7601 APIRET rc;
7602
Guido van Rossumd48f2521997-12-05 22:19:34 +00007603 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00007604 if (rc != NO_ERROR) {
7605 os2_error(rc);
7606 goto error;
7607 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007608 } else {
7609#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 /* XXX This can leak memory -- not easy to fix :-( */
7611 /* len includes space for a trailing \0; the size arg to
7612 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007613#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007614 len = wcslen(s1) + wcslen(s2) + 2;
7615 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007616#else
Victor Stinner84ae1182010-05-06 22:05:07 +00007617 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00007618 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007619#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007620 if (newstr == NULL) {
7621 PyErr_NoMemory();
7622 goto error;
7623 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007624#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 newenv = PyUnicode_AsUnicode(newstr);
7626 _snwprintf(newenv, len, L"%s=%s", s1, s2);
7627 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007629 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007631#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 newenv = PyBytes_AS_STRING(newstr);
7633 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
7634 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007635 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00007636 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00007638#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007639
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 /* Install the first arg and newstr in posix_putenv_garbage;
7641 * this will cause previous value to be collected. This has to
7642 * happen after the real putenv() call because the old value
7643 * was still accessible until then. */
7644 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007645#ifdef MS_WINDOWS
7646 PyTuple_GET_ITEM(args, 0),
7647#else
7648 os1,
7649#endif
7650 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 /* really not much we can do; just leak */
7652 PyErr_Clear();
7653 }
7654 else {
7655 Py_DECREF(newstr);
7656 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00007657
7658#if defined(PYOS_OS2)
7659 }
7660#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007661
Martin v. Löwis011e8422009-05-05 04:43:17 +00007662#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 Py_DECREF(os1);
7664 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00007665#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00007666 Py_RETURN_NONE;
7667
7668error:
7669#ifndef MS_WINDOWS
7670 Py_DECREF(os1);
7671 Py_DECREF(os2);
7672#endif
7673 Py_XDECREF(newstr);
7674 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007675}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007676#endif /* putenv */
7677
Guido van Rossumc524d952001-10-19 01:31:59 +00007678#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007679PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007680"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007681Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00007682
7683static PyObject *
7684posix_unsetenv(PyObject *self, PyObject *args)
7685{
Victor Stinner84ae1182010-05-06 22:05:07 +00007686#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00007688
Victor Stinner8c62be82010-05-06 00:08:46 +00007689 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
7690 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00007691#else
7692 PyObject *os1;
7693 char *s1;
7694
7695 if (!PyArg_ParseTuple(args, "O&:unsetenv",
7696 PyUnicode_FSConverter, &os1))
7697 return NULL;
7698 s1 = PyBytes_AsString(os1);
7699#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007700
Victor Stinner8c62be82010-05-06 00:08:46 +00007701 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00007702
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 /* Remove the key from posix_putenv_garbage;
7704 * this will cause it to be collected. This has to
7705 * happen after the real unsetenv() call because the
7706 * old value was still accessible until then.
7707 */
7708 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00007709#ifdef MS_WINDOWS
7710 PyTuple_GET_ITEM(args, 0)
7711#else
7712 os1
7713#endif
7714 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 /* really not much we can do; just leak */
7716 PyErr_Clear();
7717 }
Guido van Rossumc524d952001-10-19 01:31:59 +00007718
Victor Stinner84ae1182010-05-06 22:05:07 +00007719#ifndef MS_WINDOWS
7720 Py_DECREF(os1);
7721#endif
7722 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00007723}
7724#endif /* unsetenv */
7725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007726PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007727"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007728Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00007729
Guido van Rossumf68d8e52001-04-14 17:55:09 +00007730static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007731posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00007732{
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 int code;
7734 char *message;
7735 if (!PyArg_ParseTuple(args, "i:strerror", &code))
7736 return NULL;
7737 message = strerror(code);
7738 if (message == NULL) {
7739 PyErr_SetString(PyExc_ValueError,
7740 "strerror() argument out of range");
7741 return NULL;
7742 }
7743 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00007744}
Guido van Rossumb6a47161997-09-15 22:54:34 +00007745
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007746
Guido van Rossumc9641791998-08-04 15:26:23 +00007747#ifdef HAVE_SYS_WAIT_H
7748
Fred Drake106c1a02002-04-23 15:58:02 +00007749#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007750PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007751"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007752Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00007753
7754static PyObject *
7755posix_WCOREDUMP(PyObject *self, PyObject *args)
7756{
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 WAIT_TYPE status;
7758 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007759
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
7761 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007762
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007764}
7765#endif /* WCOREDUMP */
7766
7767#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007768PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007769"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007770Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007771job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00007772
7773static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007774posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00007775{
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 WAIT_TYPE status;
7777 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00007778
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
7780 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00007781
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00007783}
7784#endif /* WIFCONTINUED */
7785
Guido van Rossumc9641791998-08-04 15:26:23 +00007786#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007787PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007788"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007789Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007790
7791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007792posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007793{
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 WAIT_TYPE status;
7795 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007796
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
7798 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007799
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007801}
7802#endif /* WIFSTOPPED */
7803
7804#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007805PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007806"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007807Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007808
7809static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007810posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007811{
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 WAIT_TYPE status;
7813 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007814
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
7816 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007817
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007819}
7820#endif /* WIFSIGNALED */
7821
7822#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007823PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007824"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007825Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007826system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007827
7828static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007829posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007830{
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 WAIT_TYPE status;
7832 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007833
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
7835 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007836
Victor Stinner8c62be82010-05-06 00:08:46 +00007837 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007838}
7839#endif /* WIFEXITED */
7840
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007841#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007842PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007843"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007844Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007845
7846static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007847posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007848{
Victor Stinner8c62be82010-05-06 00:08:46 +00007849 WAIT_TYPE status;
7850 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007851
Victor Stinner8c62be82010-05-06 00:08:46 +00007852 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
7853 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007854
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007856}
7857#endif /* WEXITSTATUS */
7858
7859#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007860PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007861"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00007862Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007863value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007864
7865static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007866posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007867{
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 WAIT_TYPE status;
7869 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007870
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
7872 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007873
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007875}
7876#endif /* WTERMSIG */
7877
7878#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007879PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007880"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007881Return the signal that stopped the process that provided\n\
7882the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00007883
7884static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007885posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00007886{
Victor Stinner8c62be82010-05-06 00:08:46 +00007887 WAIT_TYPE status;
7888 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00007889
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
7891 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007892
Victor Stinner8c62be82010-05-06 00:08:46 +00007893 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00007894}
7895#endif /* WSTOPSIG */
7896
7897#endif /* HAVE_SYS_WAIT_H */
7898
7899
Thomas Wouters477c8d52006-05-27 19:21:47 +00007900#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00007901#ifdef _SCO_DS
7902/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
7903 needed definitions in sys/statvfs.h */
7904#define _SVID3
7905#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007906#include <sys/statvfs.h>
7907
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007908static PyObject*
7909_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007910 PyObject *v = PyStructSequence_New(&StatVFSResultType);
7911 if (v == NULL)
7912 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007913
7914#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7916 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7917 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
7918 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
7919 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
7920 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
7921 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
7922 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
7923 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7924 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007925#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007926 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
7927 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
7928 PyStructSequence_SET_ITEM(v, 2,
7929 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
7930 PyStructSequence_SET_ITEM(v, 3,
7931 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
7932 PyStructSequence_SET_ITEM(v, 4,
7933 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
7934 PyStructSequence_SET_ITEM(v, 5,
7935 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
7936 PyStructSequence_SET_ITEM(v, 6,
7937 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
7938 PyStructSequence_SET_ITEM(v, 7,
7939 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
7940 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
7941 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007942#endif
7943
Victor Stinner8c62be82010-05-06 00:08:46 +00007944 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007945}
7946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007947PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007948"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007949Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007950
7951static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007952posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007953{
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 int fd, res;
7955 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007956
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
7958 return NULL;
7959 Py_BEGIN_ALLOW_THREADS
7960 res = fstatvfs(fd, &st);
7961 Py_END_ALLOW_THREADS
7962 if (res != 0)
7963 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007964
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007966}
Thomas Wouters477c8d52006-05-27 19:21:47 +00007967#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007968
7969
Thomas Wouters477c8d52006-05-27 19:21:47 +00007970#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007971#include <sys/statvfs.h>
7972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007973PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007974"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007975Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00007976
7977static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007978posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00007979{
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 char *path;
7981 int res;
7982 struct statvfs st;
7983 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
7984 return NULL;
7985 Py_BEGIN_ALLOW_THREADS
7986 res = statvfs(path, &st);
7987 Py_END_ALLOW_THREADS
7988 if (res != 0)
7989 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007990
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007992}
7993#endif /* HAVE_STATVFS */
7994
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02007995#ifdef MS_WINDOWS
7996PyDoc_STRVAR(win32__getdiskusage__doc__,
7997"_getdiskusage(path) -> (total, free)\n\n\
7998Return disk usage statistics about the given path as (total, free) tuple.");
7999
8000static PyObject *
8001win32__getdiskusage(PyObject *self, PyObject *args)
8002{
8003 BOOL retval;
8004 ULARGE_INTEGER _, total, free;
8005 LPCTSTR path;
8006
8007 if (! PyArg_ParseTuple(args, "s", &path))
8008 return NULL;
8009
8010 Py_BEGIN_ALLOW_THREADS
8011 retval = GetDiskFreeSpaceEx(path, &_, &total, &free);
8012 Py_END_ALLOW_THREADS
8013 if (retval == 0)
8014 return PyErr_SetFromWindowsErr(0);
8015
8016 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8017}
8018#endif
8019
8020
Fred Drakec9680921999-12-13 16:37:25 +00008021/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8022 * It maps strings representing configuration variable names to
8023 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008024 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008025 * rarely-used constants. There are three separate tables that use
8026 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008027 *
8028 * This code is always included, even if none of the interfaces that
8029 * need it are included. The #if hackery needed to avoid it would be
8030 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008031 */
8032struct constdef {
8033 char *name;
8034 long value;
8035};
8036
Fred Drake12c6e2d1999-12-14 21:25:03 +00008037static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008038conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008039 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008040{
Christian Heimes217cfd12007-12-02 14:31:20 +00008041 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008042 *valuep = PyLong_AS_LONG(arg);
8043 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008044 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008045 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008046 /* look up the value in the table using a binary search */
8047 size_t lo = 0;
8048 size_t mid;
8049 size_t hi = tablesize;
8050 int cmp;
8051 const char *confname;
8052 if (!PyUnicode_Check(arg)) {
8053 PyErr_SetString(PyExc_TypeError,
8054 "configuration names must be strings or integers");
8055 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008057 confname = _PyUnicode_AsString(arg);
8058 if (confname == NULL)
8059 return 0;
8060 while (lo < hi) {
8061 mid = (lo + hi) / 2;
8062 cmp = strcmp(confname, table[mid].name);
8063 if (cmp < 0)
8064 hi = mid;
8065 else if (cmp > 0)
8066 lo = mid + 1;
8067 else {
8068 *valuep = table[mid].value;
8069 return 1;
8070 }
8071 }
8072 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8073 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008074 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008075}
8076
8077
8078#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8079static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008080#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008082#endif
8083#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008085#endif
Fred Drakec9680921999-12-13 16:37:25 +00008086#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
8092#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008093 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008094#endif
8095#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008096 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008097#endif
8098#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008099 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008100#endif
8101#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008103#endif
8104#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008105 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008106#endif
8107#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008108 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008109#endif
8110#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008111 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008112#endif
8113#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008114 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008115#endif
8116#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008117 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008118#endif
8119#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008121#endif
8122#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008124#endif
8125#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008127#endif
8128#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008130#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008131#ifdef _PC_ACL_ENABLED
8132 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8133#endif
8134#ifdef _PC_MIN_HOLE_SIZE
8135 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8136#endif
8137#ifdef _PC_ALLOC_SIZE_MIN
8138 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8139#endif
8140#ifdef _PC_REC_INCR_XFER_SIZE
8141 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8142#endif
8143#ifdef _PC_REC_MAX_XFER_SIZE
8144 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8145#endif
8146#ifdef _PC_REC_MIN_XFER_SIZE
8147 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8148#endif
8149#ifdef _PC_REC_XFER_ALIGN
8150 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8151#endif
8152#ifdef _PC_SYMLINK_MAX
8153 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8154#endif
8155#ifdef _PC_XATTR_ENABLED
8156 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8157#endif
8158#ifdef _PC_XATTR_EXISTS
8159 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8160#endif
8161#ifdef _PC_TIMESTAMP_RESOLUTION
8162 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8163#endif
Fred Drakec9680921999-12-13 16:37:25 +00008164};
8165
Fred Drakec9680921999-12-13 16:37:25 +00008166static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008167conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008168{
8169 return conv_confname(arg, valuep, posix_constants_pathconf,
8170 sizeof(posix_constants_pathconf)
8171 / sizeof(struct constdef));
8172}
8173#endif
8174
8175#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008176PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008177"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008178Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008179If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008180
8181static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008182posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008183{
8184 PyObject *result = NULL;
8185 int name, fd;
8186
Fred Drake12c6e2d1999-12-14 21:25:03 +00008187 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8188 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008189 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008190
Stefan Krah0e803b32010-11-26 16:16:47 +00008191 errno = 0;
8192 limit = fpathconf(fd, name);
8193 if (limit == -1 && errno != 0)
8194 posix_error();
8195 else
8196 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008197 }
8198 return result;
8199}
8200#endif
8201
8202
8203#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008204PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008205"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008206Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008207If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008208
8209static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008210posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008211{
8212 PyObject *result = NULL;
8213 int name;
8214 char *path;
8215
8216 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
8217 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008218 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008219
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 errno = 0;
8221 limit = pathconf(path, name);
8222 if (limit == -1 && errno != 0) {
8223 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008224 /* could be a path or name problem */
8225 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008226 else
Stefan Krah99439262010-11-26 12:58:05 +00008227 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008228 }
8229 else
8230 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008231 }
8232 return result;
8233}
8234#endif
8235
8236#ifdef HAVE_CONFSTR
8237static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008238#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008239 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008240#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008241#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008242 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008243#endif
8244#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008246#endif
Fred Draked86ed291999-12-15 15:34:33 +00008247#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008249#endif
8250#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008251 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008252#endif
8253#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008255#endif
8256#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008257 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008258#endif
Fred Drakec9680921999-12-13 16:37:25 +00008259#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008261#endif
8262#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008263 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008264#endif
8265#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008266 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008267#endif
8268#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008270#endif
8271#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008272 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008273#endif
8274#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008275 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008276#endif
8277#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008279#endif
8280#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008281 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008282#endif
Fred Draked86ed291999-12-15 15:34:33 +00008283#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008284 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008285#endif
Fred Drakec9680921999-12-13 16:37:25 +00008286#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008288#endif
Fred Draked86ed291999-12-15 15:34:33 +00008289#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008291#endif
8292#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008294#endif
8295#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008297#endif
8298#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008299 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008300#endif
Fred Drakec9680921999-12-13 16:37:25 +00008301#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008302 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008303#endif
8304#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008306#endif
8307#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008308 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008309#endif
8310#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008312#endif
8313#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008315#endif
8316#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008318#endif
8319#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008321#endif
8322#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008324#endif
8325#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008327#endif
8328#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008329 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008330#endif
8331#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008332 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008333#endif
8334#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008336#endif
8337#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008338 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008339#endif
8340#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008341 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008342#endif
8343#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008344 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008345#endif
8346#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008347 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008348#endif
Fred Draked86ed291999-12-15 15:34:33 +00008349#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008350 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008351#endif
8352#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008353 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008354#endif
8355#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008357#endif
8358#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008359 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008360#endif
8361#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008362 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008363#endif
8364#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008365 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008366#endif
8367#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008368 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008369#endif
8370#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008372#endif
8373#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008374 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008375#endif
8376#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008378#endif
8379#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008380 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008381#endif
8382#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008383 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008384#endif
8385#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008386 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008387#endif
Fred Drakec9680921999-12-13 16:37:25 +00008388};
8389
8390static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008391conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008392{
8393 return conv_confname(arg, valuep, posix_constants_confstr,
8394 sizeof(posix_constants_confstr)
8395 / sizeof(struct constdef));
8396}
8397
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008398PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008399"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008400Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008401
8402static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008403posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008404{
8405 PyObject *result = NULL;
8406 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00008407 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00008408 int len;
Fred Drakec9680921999-12-13 16:37:25 +00008409
Victor Stinnercb043522010-09-10 23:49:04 +00008410 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
8411 return NULL;
8412
8413 errno = 0;
8414 len = confstr(name, buffer, sizeof(buffer));
8415 if (len == 0) {
8416 if (errno) {
8417 posix_error();
8418 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00008419 }
8420 else {
Victor Stinnercb043522010-09-10 23:49:04 +00008421 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00008422 }
8423 }
Victor Stinnercb043522010-09-10 23:49:04 +00008424
8425 if ((unsigned int)len >= sizeof(buffer)) {
8426 char *buf = PyMem_Malloc(len);
8427 if (buf == NULL)
8428 return PyErr_NoMemory();
8429 confstr(name, buf, len);
8430 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
8431 PyMem_Free(buf);
8432 }
8433 else
8434 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00008435 return result;
8436}
8437#endif
8438
8439
8440#ifdef HAVE_SYSCONF
8441static struct constdef posix_constants_sysconf[] = {
8442#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008443 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00008444#endif
8445#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00008447#endif
8448#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008450#endif
8451#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008453#endif
8454#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008455 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008456#endif
8457#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00008458 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00008459#endif
8460#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00008461 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00008462#endif
8463#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00008464 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00008465#endif
8466#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008467 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00008468#endif
8469#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008470 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008471#endif
Fred Draked86ed291999-12-15 15:34:33 +00008472#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008474#endif
8475#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00008477#endif
Fred Drakec9680921999-12-13 16:37:25 +00008478#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008479 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008480#endif
Fred Drakec9680921999-12-13 16:37:25 +00008481#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008483#endif
8484#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008486#endif
8487#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008488 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008489#endif
8490#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008491 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008492#endif
8493#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008494 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008495#endif
Fred Draked86ed291999-12-15 15:34:33 +00008496#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008497 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00008498#endif
Fred Drakec9680921999-12-13 16:37:25 +00008499#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008501#endif
8502#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008504#endif
8505#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008506 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008507#endif
8508#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008510#endif
8511#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008512 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008513#endif
Fred Draked86ed291999-12-15 15:34:33 +00008514#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00008516#endif
Fred Drakec9680921999-12-13 16:37:25 +00008517#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008519#endif
8520#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008522#endif
8523#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008524 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008525#endif
8526#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008527 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008528#endif
8529#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008530 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008531#endif
8532#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00008534#endif
8535#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008537#endif
8538#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008540#endif
8541#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008542 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008543#endif
8544#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008545 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008546#endif
8547#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008548 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008549#endif
8550#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008552#endif
8553#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008554 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008555#endif
8556#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008557 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008558#endif
8559#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008560 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008561#endif
8562#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008563 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008564#endif
8565#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008566 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00008567#endif
8568#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008569 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008570#endif
8571#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008572 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008573#endif
8574#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00008575 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00008576#endif
8577#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008578 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00008579#endif
8580#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008581 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00008582#endif
8583#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00008584 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00008585#endif
Fred Draked86ed291999-12-15 15:34:33 +00008586#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00008587 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00008588#endif
Fred Drakec9680921999-12-13 16:37:25 +00008589#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008590 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008591#endif
8592#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008593 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008594#endif
8595#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008596 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008597#endif
Fred Draked86ed291999-12-15 15:34:33 +00008598#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00008600#endif
Fred Drakec9680921999-12-13 16:37:25 +00008601#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00008602 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00008603#endif
Fred Draked86ed291999-12-15 15:34:33 +00008604#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008605 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00008606#endif
8607#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00008608 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00008609#endif
Fred Drakec9680921999-12-13 16:37:25 +00008610#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008611 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008612#endif
8613#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008614 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008615#endif
8616#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008617 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008618#endif
8619#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008620 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008621#endif
Fred Draked86ed291999-12-15 15:34:33 +00008622#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00008623 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00008624#endif
Fred Drakec9680921999-12-13 16:37:25 +00008625#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00008626 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00008627#endif
8628#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00008629 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00008630#endif
8631#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008632 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008633#endif
8634#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008635 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00008636#endif
8637#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008638 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00008639#endif
8640#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00008641 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00008642#endif
8643#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00008644 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00008645#endif
Fred Draked86ed291999-12-15 15:34:33 +00008646#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00008647 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00008648#endif
Fred Drakec9680921999-12-13 16:37:25 +00008649#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008651#endif
8652#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008653 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008654#endif
Fred Draked86ed291999-12-15 15:34:33 +00008655#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008656 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008657#endif
Fred Drakec9680921999-12-13 16:37:25 +00008658#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008659 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008660#endif
8661#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008662 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008663#endif
8664#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008665 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008666#endif
8667#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008668 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008669#endif
8670#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008671 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008672#endif
8673#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008675#endif
8676#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008677 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00008678#endif
8679#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008680 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00008681#endif
8682#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008683 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00008684#endif
Fred Draked86ed291999-12-15 15:34:33 +00008685#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008686 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00008687#endif
8688#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00008689 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00008690#endif
Fred Drakec9680921999-12-13 16:37:25 +00008691#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00008692 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00008693#endif
8694#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008695 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008696#endif
8697#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008698 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008699#endif
8700#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008701 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008702#endif
8703#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008704 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008705#endif
8706#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00008707 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00008708#endif
8709#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00008710 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00008711#endif
8712#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00008713 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00008714#endif
8715#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008716 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00008717#endif
8718#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00008719 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00008720#endif
8721#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00008722 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00008723#endif
8724#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008725 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00008726#endif
8727#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008728 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00008729#endif
8730#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00008731 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00008732#endif
8733#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00008734 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00008735#endif
8736#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00008737 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00008738#endif
8739#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00008740 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00008741#endif
8742#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008743 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008744#endif
8745#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008746 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008747#endif
8748#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00008749 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00008750#endif
8751#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008752 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008753#endif
8754#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008756#endif
8757#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00008758 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00008759#endif
8760#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008761 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008762#endif
8763#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008764 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008765#endif
8766#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008767 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00008768#endif
8769#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00008770 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00008771#endif
8772#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008773 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008774#endif
8775#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008777#endif
8778#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00008779 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00008780#endif
8781#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008782 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008783#endif
8784#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008785 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008786#endif
8787#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008788 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008789#endif
8790#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008791 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008792#endif
8793#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008795#endif
Fred Draked86ed291999-12-15 15:34:33 +00008796#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00008797 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00008798#endif
Fred Drakec9680921999-12-13 16:37:25 +00008799#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00008800 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00008801#endif
8802#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008804#endif
8805#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00008806 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00008807#endif
8808#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008809 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008810#endif
8811#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008812 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008813#endif
8814#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008816#endif
8817#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00008818 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00008819#endif
8820#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00008821 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00008822#endif
8823#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008825#endif
8826#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008827 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008828#endif
8829#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00008830 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00008831#endif
8832#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008833 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00008834#endif
8835#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008836 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00008837#endif
8838#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00008839 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00008840#endif
8841#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00008842 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00008843#endif
8844#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008845 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00008846#endif
8847#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008848 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008849#endif
8850#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00008851 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00008852#endif
8853#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008854 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008855#endif
8856#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008857 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008858#endif
8859#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008860 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008861#endif
8862#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008863 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008864#endif
8865#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008866 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008867#endif
8868#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008869 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008870#endif
8871#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00008872 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00008873#endif
8874#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008875 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008876#endif
8877#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008878 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008879#endif
8880#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008881 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008882#endif
8883#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008884 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00008885#endif
8886#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00008887 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00008888#endif
8889#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008890 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008891#endif
8892#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00008893 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00008894#endif
8895#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008896 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00008897#endif
8898#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00008899 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00008900#endif
8901#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00008902 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00008903#endif
8904#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00008905 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00008906#endif
8907#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008908 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00008909#endif
8910#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00008911 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00008912#endif
8913#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00008914 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00008915#endif
8916#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00008917 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00008918#endif
8919#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008920 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008921#endif
8922#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008923 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00008924#endif
8925#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00008926 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00008927#endif
8928#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00008929 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00008930#endif
8931#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00008932 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00008933#endif
8934};
8935
8936static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008937conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008938{
8939 return conv_confname(arg, valuep, posix_constants_sysconf,
8940 sizeof(posix_constants_sysconf)
8941 / sizeof(struct constdef));
8942}
8943
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008944PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008945"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008946Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00008947
8948static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008949posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008950{
8951 PyObject *result = NULL;
8952 int name;
8953
8954 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
8955 int value;
8956
8957 errno = 0;
8958 value = sysconf(name);
8959 if (value == -1 && errno != 0)
8960 posix_error();
8961 else
Christian Heimes217cfd12007-12-02 14:31:20 +00008962 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00008963 }
8964 return result;
8965}
8966#endif
8967
8968
Fred Drakebec628d1999-12-15 18:31:10 +00008969/* This code is used to ensure that the tables of configuration value names
8970 * are in sorted order as required by conv_confname(), and also to build the
8971 * the exported dictionaries that are used to publish information about the
8972 * names available on the host platform.
8973 *
8974 * Sorting the table at runtime ensures that the table is properly ordered
8975 * when used, even for platforms we're not able to test on. It also makes
8976 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00008977 */
Fred Drakebec628d1999-12-15 18:31:10 +00008978
8979static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008980cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00008981{
8982 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008983 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00008984 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00008985 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00008986
8987 return strcmp(c1->name, c2->name);
8988}
8989
8990static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008991setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00008992 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00008993{
Fred Drakebec628d1999-12-15 18:31:10 +00008994 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00008995 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00008996
8997 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
8998 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00008999 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009000 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009001
Barry Warsaw3155db32000-04-13 15:20:40 +00009002 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009003 PyObject *o = PyLong_FromLong(table[i].value);
9004 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9005 Py_XDECREF(o);
9006 Py_DECREF(d);
9007 return -1;
9008 }
9009 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009010 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009011 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009012}
9013
Fred Drakebec628d1999-12-15 18:31:10 +00009014/* Return -1 on failure, 0 on success. */
9015static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009016setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009017{
9018#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009019 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009020 sizeof(posix_constants_pathconf)
9021 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009022 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009023 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009024#endif
9025#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009026 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009027 sizeof(posix_constants_confstr)
9028 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009029 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009030 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009031#endif
9032#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009033 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009034 sizeof(posix_constants_sysconf)
9035 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009036 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009037 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009038#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009039 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009040}
Fred Draked86ed291999-12-15 15:34:33 +00009041
9042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009043PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009044"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009045Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009046in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009047
9048static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009049posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009050{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009051 abort();
9052 /*NOTREACHED*/
9053 Py_FatalError("abort() called from Python code didn't abort!");
9054 return NULL;
9055}
Fred Drakebec628d1999-12-15 18:31:10 +00009056
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009057#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009058PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009059"startfile(filepath [, operation]) - Start a file with its associated\n\
9060application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009061\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009062When \"operation\" is not specified or \"open\", this acts like\n\
9063double-clicking the file in Explorer, or giving the file name as an\n\
9064argument to the DOS \"start\" command: the file is opened with whatever\n\
9065application (if any) its extension is associated.\n\
9066When another \"operation\" is given, it specifies what should be done with\n\
9067the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009068\n\
9069startfile returns as soon as the associated application is launched.\n\
9070There is no option to wait for the application to close, and no way\n\
9071to retrieve the application's exit status.\n\
9072\n\
9073The filepath is relative to the current directory. If you want to use\n\
9074an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009075the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009076
9077static PyObject *
9078win32_startfile(PyObject *self, PyObject *args)
9079{
Victor Stinner8c62be82010-05-06 00:08:46 +00009080 PyObject *ofilepath;
9081 char *filepath;
9082 char *operation = NULL;
9083 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009084
Victor Stinner8c62be82010-05-06 00:08:46 +00009085 PyObject *unipath, *woperation = NULL;
9086 if (!PyArg_ParseTuple(args, "U|s:startfile",
9087 &unipath, &operation)) {
9088 PyErr_Clear();
9089 goto normal;
9090 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009091
Victor Stinner8c62be82010-05-06 00:08:46 +00009092 if (operation) {
9093 woperation = PyUnicode_DecodeASCII(operation,
9094 strlen(operation), NULL);
9095 if (!woperation) {
9096 PyErr_Clear();
9097 operation = NULL;
9098 goto normal;
9099 }
9100 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009101
Victor Stinner8c62be82010-05-06 00:08:46 +00009102 Py_BEGIN_ALLOW_THREADS
9103 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
9104 PyUnicode_AS_UNICODE(unipath),
9105 NULL, NULL, SW_SHOWNORMAL);
9106 Py_END_ALLOW_THREADS
9107
9108 Py_XDECREF(woperation);
9109 if (rc <= (HINSTANCE)32) {
9110 PyObject *errval = win32_error_unicode("startfile",
9111 PyUnicode_AS_UNICODE(unipath));
9112 return errval;
9113 }
9114 Py_INCREF(Py_None);
9115 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009116
9117normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9119 PyUnicode_FSConverter, &ofilepath,
9120 &operation))
9121 return NULL;
9122 filepath = PyBytes_AsString(ofilepath);
9123 Py_BEGIN_ALLOW_THREADS
9124 rc = ShellExecute((HWND)0, operation, filepath,
9125 NULL, NULL, SW_SHOWNORMAL);
9126 Py_END_ALLOW_THREADS
9127 if (rc <= (HINSTANCE)32) {
9128 PyObject *errval = win32_error("startfile", filepath);
9129 Py_DECREF(ofilepath);
9130 return errval;
9131 }
9132 Py_DECREF(ofilepath);
9133 Py_INCREF(Py_None);
9134 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009135}
9136#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009137
Martin v. Löwis438b5342002-12-27 10:16:42 +00009138#ifdef HAVE_GETLOADAVG
9139PyDoc_STRVAR(posix_getloadavg__doc__,
9140"getloadavg() -> (float, float, float)\n\n\
9141Return the number of processes in the system run queue averaged over\n\
9142the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9143was unobtainable");
9144
9145static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009146posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009147{
9148 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009149 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009150 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9151 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009152 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009153 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009154}
9155#endif
9156
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009157#ifdef MS_WINDOWS
9158
9159PyDoc_STRVAR(win32_urandom__doc__,
9160"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009161Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009162
9163typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
9164 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
9165 DWORD dwFlags );
9166typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
9167 BYTE *pbBuffer );
9168
9169static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00009170/* This handle is never explicitly released. Instead, the operating
9171 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009172static HCRYPTPROV hCryptProv = 0;
9173
Tim Peters4ad82172004-08-30 17:02:04 +00009174static PyObject*
9175win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009176{
Victor Stinner8c62be82010-05-06 00:08:46 +00009177 int howMany;
9178 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009179
Victor Stinner8c62be82010-05-06 00:08:46 +00009180 /* Read arguments */
9181 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9182 return NULL;
9183 if (howMany < 0)
9184 return PyErr_Format(PyExc_ValueError,
9185 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009186
Victor Stinner8c62be82010-05-06 00:08:46 +00009187 if (hCryptProv == 0) {
9188 HINSTANCE hAdvAPI32 = NULL;
9189 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009190
Victor Stinner8c62be82010-05-06 00:08:46 +00009191 /* Obtain handle to the DLL containing CryptoAPI
9192 This should not fail */
9193 hAdvAPI32 = GetModuleHandle("advapi32.dll");
9194 if(hAdvAPI32 == NULL)
9195 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009196
Victor Stinner8c62be82010-05-06 00:08:46 +00009197 /* Obtain pointers to the CryptoAPI functions
9198 This will fail on some early versions of Win95 */
9199 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
9200 hAdvAPI32,
9201 "CryptAcquireContextA");
9202 if (pCryptAcquireContext == NULL)
9203 return PyErr_Format(PyExc_NotImplementedError,
9204 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009205
Victor Stinner8c62be82010-05-06 00:08:46 +00009206 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
9207 hAdvAPI32, "CryptGenRandom");
9208 if (pCryptGenRandom == NULL)
9209 return PyErr_Format(PyExc_NotImplementedError,
9210 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009211
Victor Stinner8c62be82010-05-06 00:08:46 +00009212 /* Acquire context */
9213 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
9214 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
9215 return win32_error("CryptAcquireContext", NULL);
9216 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009217
Victor Stinner8c62be82010-05-06 00:08:46 +00009218 /* Allocate bytes */
9219 result = PyBytes_FromStringAndSize(NULL, howMany);
9220 if (result != NULL) {
9221 /* Get random data */
9222 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
9223 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
9224 PyBytes_AS_STRING(result))) {
9225 Py_DECREF(result);
9226 return win32_error("CryptGenRandom", NULL);
9227 }
9228 }
9229 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00009230}
9231#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00009232
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009233PyDoc_STRVAR(device_encoding__doc__,
9234"device_encoding(fd) -> str\n\n\
9235Return a string describing the encoding of the device\n\
9236if the output is a terminal; else return None.");
9237
9238static PyObject *
9239device_encoding(PyObject *self, PyObject *args)
9240{
Victor Stinner8c62be82010-05-06 00:08:46 +00009241 int fd;
Victor Stinner7870bdf2011-05-23 18:12:52 +02009242#if defined(MS_WINDOWS) || defined(MS_WIN64)
9243 UINT cp;
9244#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009245 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9246 return NULL;
9247 if (!_PyVerify_fd(fd) || !isatty(fd)) {
9248 Py_INCREF(Py_None);
9249 return Py_None;
9250 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009251#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner7870bdf2011-05-23 18:12:52 +02009252 if (fd == 0)
9253 cp = GetConsoleCP();
9254 else if (fd == 1 || fd == 2)
9255 cp = GetConsoleOutputCP();
9256 else
9257 cp = 0;
9258 /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
9259 has no console */
9260 if (cp != 0)
9261 return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009262#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00009263 {
9264 char *codeset = nl_langinfo(CODESET);
9265 if (codeset != NULL && codeset[0] != 0)
9266 return PyUnicode_FromString(codeset);
9267 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009268#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009269 Py_INCREF(Py_None);
9270 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009271}
9272
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009273#ifdef __VMS
9274/* Use openssl random routine */
9275#include <openssl/rand.h>
9276PyDoc_STRVAR(vms_urandom__doc__,
9277"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00009278Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009279
9280static PyObject*
9281vms_urandom(PyObject *self, PyObject *args)
9282{
Victor Stinner8c62be82010-05-06 00:08:46 +00009283 int howMany;
9284 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009285
Victor Stinner8c62be82010-05-06 00:08:46 +00009286 /* Read arguments */
9287 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
9288 return NULL;
9289 if (howMany < 0)
9290 return PyErr_Format(PyExc_ValueError,
9291 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009292
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 /* Allocate bytes */
9294 result = PyBytes_FromStringAndSize(NULL, howMany);
9295 if (result != NULL) {
9296 /* Get random data */
9297 if (RAND_pseudo_bytes((unsigned char*)
9298 PyBytes_AS_STRING(result),
9299 howMany) < 0) {
9300 Py_DECREF(result);
9301 return PyErr_Format(PyExc_ValueError,
9302 "RAND_pseudo_bytes");
9303 }
9304 }
9305 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009306}
9307#endif
9308
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009309#ifdef HAVE_SETRESUID
9310PyDoc_STRVAR(posix_setresuid__doc__,
9311"setresuid(ruid, euid, suid)\n\n\
9312Set the current process's real, effective, and saved user ids.");
9313
9314static PyObject*
9315posix_setresuid (PyObject *self, PyObject *args)
9316{
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 /* We assume uid_t is no larger than a long. */
9318 long ruid, euid, suid;
9319 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
9320 return NULL;
9321 if (setresuid(ruid, euid, suid) < 0)
9322 return posix_error();
9323 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009324}
9325#endif
9326
9327#ifdef HAVE_SETRESGID
9328PyDoc_STRVAR(posix_setresgid__doc__,
9329"setresgid(rgid, egid, sgid)\n\n\
9330Set the current process's real, effective, and saved group ids.");
9331
9332static PyObject*
9333posix_setresgid (PyObject *self, PyObject *args)
9334{
Victor Stinner8c62be82010-05-06 00:08:46 +00009335 /* We assume uid_t is no larger than a long. */
9336 long rgid, egid, sgid;
9337 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
9338 return NULL;
9339 if (setresgid(rgid, egid, sgid) < 0)
9340 return posix_error();
9341 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009342}
9343#endif
9344
9345#ifdef HAVE_GETRESUID
9346PyDoc_STRVAR(posix_getresuid__doc__,
9347"getresuid() -> (ruid, euid, suid)\n\n\
9348Get tuple of the current process's real, effective, and saved user ids.");
9349
9350static PyObject*
9351posix_getresuid (PyObject *self, PyObject *noargs)
9352{
Victor Stinner8c62be82010-05-06 00:08:46 +00009353 uid_t ruid, euid, suid;
9354 long l_ruid, l_euid, l_suid;
9355 if (getresuid(&ruid, &euid, &suid) < 0)
9356 return posix_error();
9357 /* Force the values into long's as we don't know the size of uid_t. */
9358 l_ruid = ruid;
9359 l_euid = euid;
9360 l_suid = suid;
9361 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009362}
9363#endif
9364
9365#ifdef HAVE_GETRESGID
9366PyDoc_STRVAR(posix_getresgid__doc__,
9367"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009368Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009369
9370static PyObject*
9371posix_getresgid (PyObject *self, PyObject *noargs)
9372{
Victor Stinner8c62be82010-05-06 00:08:46 +00009373 uid_t rgid, egid, sgid;
9374 long l_rgid, l_egid, l_sgid;
9375 if (getresgid(&rgid, &egid, &sgid) < 0)
9376 return posix_error();
9377 /* Force the values into long's as we don't know the size of uid_t. */
9378 l_rgid = rgid;
9379 l_egid = egid;
9380 l_sgid = sgid;
9381 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009382}
9383#endif
9384
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009385/* Posix *at family of functions:
9386 faccessat, fchmodat, fchownat, fstatat, futimesat,
9387 linkat, mkdirat, mknodat, openat, readlinkat, renameat, symlinkat,
9388 unlinkat, utimensat, mkfifoat */
9389
9390#ifdef HAVE_FACCESSAT
9391PyDoc_STRVAR(posix_faccessat__doc__,
9392"faccessat(dirfd, path, mode, flags=0) -> True if granted, False otherwise\n\n\
9393Like access() but if path is relative, it is taken as relative to dirfd.\n\
9394flags is optional and can be constructed by ORing together zero or more\n\
9395of these values: AT_SYMLINK_NOFOLLOW, AT_EACCESS.\n\
9396If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9397is interpreted relative to the current working directory.");
9398
9399static PyObject *
9400posix_faccessat(PyObject *self, PyObject *args)
9401{
9402 PyObject *opath;
9403 char *path;
9404 int mode;
9405 int res;
9406 int dirfd, flags = 0;
9407 if (!PyArg_ParseTuple(args, "iO&i|i:faccessat",
9408 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9409 return NULL;
9410 path = PyBytes_AsString(opath);
9411 Py_BEGIN_ALLOW_THREADS
9412 res = faccessat(dirfd, path, mode, flags);
9413 Py_END_ALLOW_THREADS
9414 Py_DECREF(opath);
9415 return PyBool_FromLong(res == 0);
9416}
9417#endif
9418
9419#ifdef HAVE_FCHMODAT
9420PyDoc_STRVAR(posix_fchmodat__doc__,
9421"fchmodat(dirfd, path, mode, flags=0)\n\n\
9422Like chmod() but if path is relative, it is taken as relative to dirfd.\n\
9423flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9424If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9425is interpreted relative to the current working directory.");
9426
9427static PyObject *
9428posix_fchmodat(PyObject *self, PyObject *args)
9429{
9430 int dirfd, mode, res;
9431 int flags = 0;
9432 PyObject *opath;
9433 char *path;
9434
9435 if (!PyArg_ParseTuple(args, "iO&i|i:fchmodat",
9436 &dirfd, PyUnicode_FSConverter, &opath, &mode, &flags))
9437 return NULL;
9438
9439 path = PyBytes_AsString(opath);
9440
9441 Py_BEGIN_ALLOW_THREADS
9442 res = fchmodat(dirfd, path, mode, flags);
9443 Py_END_ALLOW_THREADS
9444 Py_DECREF(opath);
9445 if (res < 0)
9446 return posix_error();
9447 Py_RETURN_NONE;
9448}
9449#endif /* HAVE_FCHMODAT */
9450
9451#ifdef HAVE_FCHOWNAT
9452PyDoc_STRVAR(posix_fchownat__doc__,
9453"fchownat(dirfd, path, uid, gid, flags=0)\n\n\
9454Like chown() but if path is relative, it is taken as relative to dirfd.\n\
9455flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9456If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9457is interpreted relative to the current working directory.");
9458
9459static PyObject *
9460posix_fchownat(PyObject *self, PyObject *args)
9461{
9462 PyObject *opath;
9463 int dirfd, res;
9464 long uid, gid;
9465 int flags = 0;
9466 char *path;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009467
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009468 if (!PyArg_ParseTuple(args, "iO&ll|i:fchownat",
9469 &dirfd, PyUnicode_FSConverter, &opath, &uid, &gid, &flags))
9470 return NULL;
9471
9472 path = PyBytes_AsString(opath);
9473
9474 Py_BEGIN_ALLOW_THREADS
9475 res = fchownat(dirfd, path, (uid_t) uid, (gid_t) gid, flags);
9476 Py_END_ALLOW_THREADS
9477 Py_DECREF(opath);
9478 if (res < 0)
9479 return posix_error();
9480 Py_RETURN_NONE;
9481}
9482#endif /* HAVE_FCHOWNAT */
9483
9484#ifdef HAVE_FSTATAT
9485PyDoc_STRVAR(posix_fstatat__doc__,
9486"fstatat(dirfd, path, flags=0) -> stat result\n\n\
9487Like stat() but if path is relative, it is taken as relative to dirfd.\n\
9488flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9489If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9490is interpreted relative to the current working directory.");
9491
9492static PyObject *
9493posix_fstatat(PyObject *self, PyObject *args)
9494{
9495 PyObject *opath;
9496 char *path;
9497 STRUCT_STAT st;
9498 int dirfd, res, flags = 0;
9499
9500 if (!PyArg_ParseTuple(args, "iO&|i:fstatat",
9501 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9502 return NULL;
9503 path = PyBytes_AsString(opath);
9504
9505 Py_BEGIN_ALLOW_THREADS
9506 res = fstatat(dirfd, path, &st, flags);
9507 Py_END_ALLOW_THREADS
9508 Py_DECREF(opath);
9509 if (res != 0)
9510 return posix_error();
9511
9512 return _pystat_fromstructstat(&st);
9513}
9514#endif
9515
9516#ifdef HAVE_FUTIMESAT
9517PyDoc_STRVAR(posix_futimesat__doc__,
9518"futimesat(dirfd, path, (atime, mtime))\n\
9519futimesat(dirfd, path, None)\n\n\
9520Like utime() but if path is relative, it is taken as relative to dirfd.\n\
9521If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9522is interpreted relative to the current working directory.");
9523
9524static PyObject *
9525posix_futimesat(PyObject *self, PyObject *args)
9526{
9527 PyObject *opath;
9528 char *path;
9529 int res, dirfd;
9530 PyObject* arg;
9531
9532 struct timeval buf[2];
9533
9534 if (!PyArg_ParseTuple(args, "iO&O:futimesat",
9535 &dirfd, PyUnicode_FSConverter, &opath, &arg))
9536 return NULL;
9537 path = PyBytes_AsString(opath);
9538 if (arg == Py_None) {
9539 /* optional time values not given */
9540 Py_BEGIN_ALLOW_THREADS
9541 res = futimesat(dirfd, path, NULL);
9542 Py_END_ALLOW_THREADS
9543 }
9544 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
9545 PyErr_SetString(PyExc_TypeError,
9546 "futimesat() arg 3 must be a tuple (atime, mtime)");
9547 Py_DECREF(opath);
9548 return NULL;
9549 }
9550 else {
9551 if (extract_time(PyTuple_GET_ITEM(arg, 0),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009552 &(buf[0].tv_sec), &(buf[0].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009553 Py_DECREF(opath);
9554 return NULL;
9555 }
9556 if (extract_time(PyTuple_GET_ITEM(arg, 1),
Victor Stinner6ee7a572011-06-17 15:18:16 +02009557 &(buf[1].tv_sec), &(buf[1].tv_usec)) == -1) {
Antoine Pitrouf65132d2011-02-25 23:25:17 +00009558 Py_DECREF(opath);
9559 return NULL;
9560 }
9561 Py_BEGIN_ALLOW_THREADS
9562 res = futimesat(dirfd, path, buf);
9563 Py_END_ALLOW_THREADS
9564 }
9565 Py_DECREF(opath);
9566 if (res < 0) {
9567 return posix_error();
9568 }
9569 Py_RETURN_NONE;
9570}
9571#endif
9572
9573#ifdef HAVE_LINKAT
9574PyDoc_STRVAR(posix_linkat__doc__,
9575"linkat(srcfd, srcpath, dstfd, dstpath, flags=0)\n\n\
9576Like link() but if srcpath is relative, it is taken as relative to srcfd\n\
9577and if dstpath is relative, it is taken as relative to dstfd.\n\
9578flags is optional and may be 0 or AT_SYMLINK_FOLLOW.\n\
9579If srcpath is relative and srcfd is the special value AT_FDCWD, then\n\
9580srcpath is interpreted relative to the current working directory. This\n\
9581also applies for dstpath.");
9582
9583static PyObject *
9584posix_linkat(PyObject *self, PyObject *args)
9585{
9586 PyObject *osrc, *odst;
9587 char *src, *dst;
9588 int res, srcfd, dstfd;
9589 int flags = 0;
9590
9591 if (!PyArg_ParseTuple(args, "iO&iO&|i:linkat",
9592 &srcfd, PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst, &flags))
9593 return NULL;
9594 src = PyBytes_AsString(osrc);
9595 dst = PyBytes_AsString(odst);
9596 Py_BEGIN_ALLOW_THREADS
9597 res = linkat(srcfd, src, dstfd, dst, flags);
9598 Py_END_ALLOW_THREADS
9599 Py_DECREF(osrc);
9600 Py_DECREF(odst);
9601 if (res < 0)
9602 return posix_error();
9603 Py_RETURN_NONE;
9604}
9605#endif /* HAVE_LINKAT */
9606
9607#ifdef HAVE_MKDIRAT
9608PyDoc_STRVAR(posix_mkdirat__doc__,
9609"mkdirat(dirfd, path, mode=0o777)\n\n\
9610Like mkdir() but if path is relative, it is taken as relative to dirfd.\n\
9611If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9612is interpreted relative to the current working directory.");
9613
9614static PyObject *
9615posix_mkdirat(PyObject *self, PyObject *args)
9616{
9617 int res, dirfd;
9618 PyObject *opath;
9619 char *path;
9620 int mode = 0777;
9621
9622 if (!PyArg_ParseTuple(args, "iO&|i:mkdirat",
9623 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9624 return NULL;
9625 path = PyBytes_AsString(opath);
9626 Py_BEGIN_ALLOW_THREADS
9627 res = mkdirat(dirfd, path, mode);
9628 Py_END_ALLOW_THREADS
9629 Py_DECREF(opath);
9630 if (res < 0)
9631 return posix_error();
9632 Py_RETURN_NONE;
9633}
9634#endif
9635
9636#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
9637PyDoc_STRVAR(posix_mknodat__doc__,
9638"mknodat(dirfd, path, mode=0o600, device=0)\n\n\
9639Like mknod() but if path is relative, it is taken as relative to dirfd.\n\
9640If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9641is interpreted relative to the current working directory.");
9642
9643static PyObject *
9644posix_mknodat(PyObject *self, PyObject *args)
9645{
9646 PyObject *opath;
9647 char *filename;
9648 int mode = 0600;
9649 int device = 0;
9650 int res, dirfd;
9651 if (!PyArg_ParseTuple(args, "iO&|ii:mknodat", &dirfd,
9652 PyUnicode_FSConverter, &opath, &mode, &device))
9653 return NULL;
9654 filename = PyBytes_AS_STRING(opath);
9655 Py_BEGIN_ALLOW_THREADS
9656 res = mknodat(dirfd, filename, mode, device);
9657 Py_END_ALLOW_THREADS
9658 Py_DECREF(opath);
9659 if (res < 0)
9660 return posix_error();
9661 Py_RETURN_NONE;
9662}
9663#endif
9664
9665#ifdef HAVE_OPENAT
9666PyDoc_STRVAR(posix_openat__doc__,
9667"openat(dirfd, path, flag, mode=0o777) -> fd\n\n\
9668Like open() but if path is relative, it is taken as relative to dirfd.\n\
9669If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9670is interpreted relative to the current working directory.");
9671
9672static PyObject *
9673posix_openat(PyObject *self, PyObject *args)
9674{
9675 PyObject *ofile;
9676 char *file;
9677 int flag, dirfd, fd;
9678 int mode = 0777;
9679
9680 if (!PyArg_ParseTuple(args, "iO&i|i:openat",
9681 &dirfd, PyUnicode_FSConverter, &ofile,
9682 &flag, &mode))
9683 return NULL;
9684 file = PyBytes_AsString(ofile);
9685 Py_BEGIN_ALLOW_THREADS
9686 fd = openat(dirfd, file, flag, mode);
9687 Py_END_ALLOW_THREADS
9688 Py_DECREF(ofile);
9689 if (fd < 0)
9690 return posix_error();
9691 return PyLong_FromLong((long)fd);
9692}
9693#endif
9694
9695#ifdef HAVE_READLINKAT
9696PyDoc_STRVAR(posix_readlinkat__doc__,
9697"readlinkat(dirfd, path) -> path\n\n\
9698Like readlink() but if path is relative, it is taken as relative to dirfd.\n\
9699If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9700is interpreted relative to the current working directory.");
9701
9702static PyObject *
9703posix_readlinkat(PyObject *self, PyObject *args)
9704{
9705 PyObject *v, *opath;
9706 char buf[MAXPATHLEN];
9707 char *path;
9708 int n, dirfd;
9709 int arg_is_unicode = 0;
9710
9711 if (!PyArg_ParseTuple(args, "iO&:readlinkat",
9712 &dirfd, PyUnicode_FSConverter, &opath))
9713 return NULL;
9714 path = PyBytes_AsString(opath);
9715 v = PySequence_GetItem(args, 1);
9716 if (v == NULL) {
9717 Py_DECREF(opath);
9718 return NULL;
9719 }
9720
9721 if (PyUnicode_Check(v)) {
9722 arg_is_unicode = 1;
9723 }
9724 Py_DECREF(v);
9725
9726 Py_BEGIN_ALLOW_THREADS
9727 n = readlinkat(dirfd, path, buf, (int) sizeof buf);
9728 Py_END_ALLOW_THREADS
9729 Py_DECREF(opath);
9730 if (n < 0)
9731 return posix_error();
9732
9733 if (arg_is_unicode)
9734 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
9735 else
9736 return PyBytes_FromStringAndSize(buf, n);
9737}
9738#endif /* HAVE_READLINKAT */
9739
9740#ifdef HAVE_RENAMEAT
9741PyDoc_STRVAR(posix_renameat__doc__,
9742"renameat(olddirfd, oldpath, newdirfd, newpath)\n\n\
9743Like rename() but if oldpath is relative, it is taken as relative to\n\
9744olddirfd and if newpath is relative, it is taken as relative to newdirfd.\n\
9745If oldpath is relative and olddirfd is the special value AT_FDCWD, then\n\
9746oldpath is interpreted relative to the current working directory. This\n\
9747also applies for newpath.");
9748
9749static PyObject *
9750posix_renameat(PyObject *self, PyObject *args)
9751{
9752 int res;
9753 PyObject *opathold, *opathnew;
9754 char *opath, *npath;
9755 int oldfd, newfd;
9756
9757 if (!PyArg_ParseTuple(args, "iO&iO&:renameat",
9758 &oldfd, PyUnicode_FSConverter, &opathold, &newfd, PyUnicode_FSConverter, &opathnew))
9759 return NULL;
9760 opath = PyBytes_AsString(opathold);
9761 npath = PyBytes_AsString(opathnew);
9762 Py_BEGIN_ALLOW_THREADS
9763 res = renameat(oldfd, opath, newfd, npath);
9764 Py_END_ALLOW_THREADS
9765 Py_DECREF(opathold);
9766 Py_DECREF(opathnew);
9767 if (res < 0)
9768 return posix_error();
9769 Py_RETURN_NONE;
9770}
9771#endif
9772
9773#if HAVE_SYMLINKAT
9774PyDoc_STRVAR(posix_symlinkat__doc__,
9775"symlinkat(src, dstfd, dst)\n\n\
9776Like symlink() but if dst is relative, it is taken as relative to dstfd.\n\
9777If dst is relative and dstfd is the special value AT_FDCWD, then dst\n\
9778is interpreted relative to the current working directory.");
9779
9780static PyObject *
9781posix_symlinkat(PyObject *self, PyObject *args)
9782{
9783 int res, dstfd;
9784 PyObject *osrc, *odst;
9785 char *src, *dst;
9786
9787 if (!PyArg_ParseTuple(args, "O&iO&:symlinkat",
9788 PyUnicode_FSConverter, &osrc, &dstfd, PyUnicode_FSConverter, &odst))
9789 return NULL;
9790 src = PyBytes_AsString(osrc);
9791 dst = PyBytes_AsString(odst);
9792 Py_BEGIN_ALLOW_THREADS
9793 res = symlinkat(src, dstfd, dst);
9794 Py_END_ALLOW_THREADS
9795 Py_DECREF(osrc);
9796 Py_DECREF(odst);
9797 if (res < 0)
9798 return posix_error();
9799 Py_RETURN_NONE;
9800}
9801#endif /* HAVE_SYMLINKAT */
9802
9803#ifdef HAVE_UNLINKAT
9804PyDoc_STRVAR(posix_unlinkat__doc__,
9805"unlinkat(dirfd, path, flags=0)\n\n\
9806Like unlink() but if path is relative, it is taken as relative to dirfd.\n\
9807flags is optional and may be 0 or AT_REMOVEDIR. If AT_REMOVEDIR is\n\
9808specified, unlinkat() behaves like rmdir().\n\
9809If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9810is interpreted relative to the current working directory.");
9811
9812static PyObject *
9813posix_unlinkat(PyObject *self, PyObject *args)
9814{
9815 int dirfd, res, flags = 0;
9816 PyObject *opath;
9817 char *path;
9818
9819 if (!PyArg_ParseTuple(args, "iO&|i:unlinkat",
9820 &dirfd, PyUnicode_FSConverter, &opath, &flags))
9821 return NULL;
9822 path = PyBytes_AsString(opath);
9823 Py_BEGIN_ALLOW_THREADS
9824 res = unlinkat(dirfd, path, flags);
9825 Py_END_ALLOW_THREADS
9826 Py_DECREF(opath);
9827 if (res < 0)
9828 return posix_error();
9829 Py_RETURN_NONE;
9830}
9831#endif
9832
9833#ifdef HAVE_UTIMENSAT
9834PyDoc_STRVAR(posix_utimensat__doc__,
9835"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\
9836 (mtime_sec, mtime_nsec), flags)\n\
9837utimensat(dirfd, path, None, None, flags)\n\n\
9838Updates the timestamps of a file with nanosecond precision. If path is\n\
9839relative, it is taken as relative to dirfd.\n\
9840The second form sets atime and mtime to the current time.\n\
9841flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
9842If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9843is interpreted relative to the current working directory.\n\
9844If *_nsec is specified as UTIME_NOW, the timestamp is updated to the\n\
9845current time.\n\
9846If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
9847
9848static PyObject *
9849posix_utimensat(PyObject *self, PyObject *args)
9850{
9851 PyObject *opath;
9852 char *path;
9853 int res, dirfd, flags = 0;
9854 PyObject *atime, *mtime;
9855
9856 struct timespec buf[2];
9857
9858 if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat",
9859 &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
9860 return NULL;
9861 path = PyBytes_AsString(opath);
9862 if (atime == Py_None && mtime == Py_None) {
9863 /* optional time values not given */
9864 Py_BEGIN_ALLOW_THREADS
9865 res = utimensat(dirfd, path, NULL, flags);
9866 Py_END_ALLOW_THREADS
9867 }
9868 else if (!PyTuple_Check(atime) || PyTuple_Size(atime) != 2) {
9869 PyErr_SetString(PyExc_TypeError,
9870 "utimensat() arg 3 must be a tuple (atime_sec, atime_nsec)");
9871 Py_DECREF(opath);
9872 return NULL;
9873 }
9874 else if (!PyTuple_Check(mtime) || PyTuple_Size(mtime) != 2) {
9875 PyErr_SetString(PyExc_TypeError,
9876 "utimensat() arg 4 must be a tuple (mtime_sec, mtime_nsec)");
9877 Py_DECREF(opath);
9878 return NULL;
9879 }
9880 else {
9881 if (!PyArg_ParseTuple(atime, "ll:utimensat",
9882 &(buf[0].tv_sec), &(buf[0].tv_nsec))) {
9883 Py_DECREF(opath);
9884 return NULL;
9885 }
9886 if (!PyArg_ParseTuple(mtime, "ll:utimensat",
9887 &(buf[1].tv_sec), &(buf[1].tv_nsec))) {
9888 Py_DECREF(opath);
9889 return NULL;
9890 }
9891 Py_BEGIN_ALLOW_THREADS
9892 res = utimensat(dirfd, path, buf, flags);
9893 Py_END_ALLOW_THREADS
9894 }
9895 Py_DECREF(opath);
9896 if (res < 0) {
9897 return posix_error();
9898 }
9899 Py_RETURN_NONE;
9900}
9901#endif
9902
9903#ifdef HAVE_MKFIFOAT
9904PyDoc_STRVAR(posix_mkfifoat__doc__,
9905"mkfifoat(dirfd, path, mode=0o666)\n\n\
9906Like mkfifo() but if path is relative, it is taken as relative to dirfd.\n\
9907If path is relative and dirfd is the special value AT_FDCWD, then path\n\
9908is interpreted relative to the current working directory.");
9909
9910static PyObject *
9911posix_mkfifoat(PyObject *self, PyObject *args)
9912{
9913 PyObject *opath;
9914 char *filename;
9915 int mode = 0666;
9916 int res, dirfd;
9917 if (!PyArg_ParseTuple(args, "iO&|i:mkfifoat",
9918 &dirfd, PyUnicode_FSConverter, &opath, &mode))
9919 return NULL;
9920 filename = PyBytes_AS_STRING(opath);
9921 Py_BEGIN_ALLOW_THREADS
9922 res = mkfifoat(dirfd, filename, mode);
9923 Py_END_ALLOW_THREADS
9924 Py_DECREF(opath);
9925 if (res < 0)
9926 return posix_error();
9927 Py_RETURN_NONE;
9928}
9929#endif
9930
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009931static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00009932 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009933#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009934 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009935#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009936 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009937#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009938 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009939#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00009940 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009941#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009942 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009943#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009944#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009945 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009946#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00009947#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00009948 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009949#endif /* HAVE_LCHMOD */
9950#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009951 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00009952#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00009953#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009954 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00009955#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009956#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00009957 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00009958#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00009959#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00009960 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00009961#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009962#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00009963 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009964#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00009965#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00009966 {"getcwd", (PyCFunction)posix_getcwd_unicode,
9967 METH_NOARGS, posix_getcwd__doc__},
9968 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
9969 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00009970#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00009971#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009972 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009973#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00009974 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
Antoine Pitrou8250e232011-02-25 23:41:16 +00009975#ifdef HAVE_FDOPENDIR
9976 {"fdlistdir", posix_fdlistdir, METH_VARARGS, posix_fdlistdir__doc__},
9977#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009978 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
9979 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00009980#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00009981 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009982#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00009983#ifdef HAVE_GETPRIORITY
9984 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
9985#endif /* HAVE_GETPRIORITY */
9986#ifdef HAVE_SETPRIORITY
9987 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
9988#endif /* HAVE_SETPRIORITY */
Guido van Rossumb6775db1994-08-01 11:34:53 +00009989#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00009990 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009991#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00009992#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009993 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00009994#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00009995 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
9996 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
9997 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00009998 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00009999#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +000010000 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010001#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +000010002#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010003 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +000010004 win_symlink__doc__},
10005#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010006#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010007 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010008#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010009 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010010#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010011 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010012#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +000010013 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
10014 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
10015 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010016#ifdef HAVE_FUTIMES
10017 {"futimes", posix_futimes, METH_VARARGS, posix_futimes__doc__},
10018#endif
10019#ifdef HAVE_LUTIMES
10020 {"lutimes", posix_lutimes, METH_VARARGS, posix_lutimes__doc__},
10021#endif
10022#ifdef HAVE_FUTIMENS
10023 {"futimens", posix_futimens, METH_VARARGS, posix_futimens__doc__},
10024#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010025#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010026 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010027#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010028 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010029#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010030 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
10031 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010032#endif /* HAVE_EXECV */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010033#ifdef HAVE_FEXECVE
10034 {"fexecve", posix_fexecve, METH_VARARGS, posix_fexecve__doc__},
10035#endif
Guido van Rossuma1065681999-01-25 23:20:23 +000010036#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010037 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10038 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010039#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010040 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
10041 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +000010042#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +000010043#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010044#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010045 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010046#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010047#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010048 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010049#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010050#ifdef HAVE_SCHED_H
10051 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10052 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
10053 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
10054 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
10055 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
10056 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
10057 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
10058 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
10059 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10060 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10061#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010062#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010064#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010065#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010066 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010067#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010068#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010069 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010070#endif /* HAVE_GETEGID */
10071#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010073#endif /* HAVE_GETEUID */
10074#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010075 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010076#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010077#ifdef HAVE_GETGROUPLIST
10078 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10079#endif
Fred Drakec9680921999-12-13 16:37:25 +000010080#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010081 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010082#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010083 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010084#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010085 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010086#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010087#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010088 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010089#endif /* HAVE_GETPPID */
10090#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010091 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010092#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010093#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010094 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010095#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010096#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010097 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010098#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010099#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010100 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010101#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010102#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010103 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010104#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010105#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010106 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10107 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +000010108 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010109#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010110#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010111 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010112#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010113#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010114 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010115#endif /* HAVE_SETEUID */
10116#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010117 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010118#endif /* HAVE_SETEGID */
10119#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010120 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010121#endif /* HAVE_SETREUID */
10122#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010123 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010124#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010125#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010127#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010128#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010129 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010130#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010131#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010132 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010133#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010134#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010135 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010136#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010137#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010138 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010139#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010140#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010141 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010142#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010143#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +000010144 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010145#endif /* HAVE_WAIT3 */
10146#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +000010147 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010148#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010149#if defined(HAVE_WAITID) && !defined(__APPLE__)
10150 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10151#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010152#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010153 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010154#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010155#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010156 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010157#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010158#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010159 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010160#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010161#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010162 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010163#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010164#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010165 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010166#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010167#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010168 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010169#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +000010170 {"open", posix_open, METH_VARARGS, posix_open__doc__},
10171 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10172 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10173 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10174 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10175 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010176#ifdef HAVE_LOCKF
10177 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10178#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010179 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10180 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010181#ifdef HAVE_READV
10182 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10183#endif
10184#ifdef HAVE_PREAD
10185 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10186#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010187 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010188#ifdef HAVE_WRITEV
10189 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10190#endif
10191#ifdef HAVE_PWRITE
10192 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10193#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010194#ifdef HAVE_SENDFILE
10195 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10196 posix_sendfile__doc__},
10197#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010198 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
10199 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010200#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010201 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010202#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010203#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010204 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010205#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010206#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +000010207 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010208#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010209#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +000010210 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010211#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010212#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010213 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10214 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10215 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010216#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010217#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010218 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010219#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010220#ifdef HAVE_TRUNCATE
10221 {"truncate", posix_truncate, METH_VARARGS, posix_truncate__doc__},
10222#endif
10223#ifdef HAVE_POSIX_FALLOCATE
10224 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10225#endif
10226#ifdef HAVE_POSIX_FADVISE
10227 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10228#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010229#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010230 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010231#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010232#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010233 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010234#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010235 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010236#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010237 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010238#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010239#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010240 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010241#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010242#ifdef HAVE_SYNC
10243 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10244#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010245#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010246 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010247#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010248#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010249#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010250 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010251#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010252#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010253 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010254#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010255#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010256 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010257#endif /* WIFSTOPPED */
10258#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010259 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010260#endif /* WIFSIGNALED */
10261#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010262 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010263#endif /* WIFEXITED */
10264#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010265 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010266#endif /* WEXITSTATUS */
10267#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010268 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010269#endif /* WTERMSIG */
10270#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010271 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010272#endif /* WSTOPSIG */
10273#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010274#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010275 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010276#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010277#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010278 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010279#endif
Fred Drakec9680921999-12-13 16:37:25 +000010280#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010281 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010282#endif
10283#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010284 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010285#endif
10286#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010287 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010288#endif
10289#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010290 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010291#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010292 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010293#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010294 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010295 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +000010296 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010297 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010298 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010299#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010300#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010301 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010302#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010303 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010304 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +000010305 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010306 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +000010307 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +000010308 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010309#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010310 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010311#endif
10312#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010313 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010314#endif
10315#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010316 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010317#endif
10318#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010319 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010320#endif
10321
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010322/* posix *at family of functions */
10323#ifdef HAVE_FACCESSAT
10324 {"faccessat", posix_faccessat, METH_VARARGS, posix_faccessat__doc__},
10325#endif
10326#ifdef HAVE_FCHMODAT
10327 {"fchmodat", posix_fchmodat, METH_VARARGS, posix_fchmodat__doc__},
10328#endif /* HAVE_FCHMODAT */
10329#ifdef HAVE_FCHOWNAT
10330 {"fchownat", posix_fchownat, METH_VARARGS, posix_fchownat__doc__},
10331#endif /* HAVE_FCHOWNAT */
10332#ifdef HAVE_FSTATAT
10333 {"fstatat", posix_fstatat, METH_VARARGS, posix_fstatat__doc__},
10334#endif
10335#ifdef HAVE_FUTIMESAT
10336 {"futimesat", posix_futimesat, METH_VARARGS, posix_futimesat__doc__},
10337#endif
10338#ifdef HAVE_LINKAT
10339 {"linkat", posix_linkat, METH_VARARGS, posix_linkat__doc__},
10340#endif /* HAVE_LINKAT */
10341#ifdef HAVE_MKDIRAT
10342 {"mkdirat", posix_mkdirat, METH_VARARGS, posix_mkdirat__doc__},
10343#endif
10344#if defined(HAVE_MKNODAT) && defined(HAVE_MAKEDEV)
10345 {"mknodat", posix_mknodat, METH_VARARGS, posix_mknodat__doc__},
10346#endif
10347#ifdef HAVE_OPENAT
10348 {"openat", posix_openat, METH_VARARGS, posix_openat__doc__},
10349#endif
10350#ifdef HAVE_READLINKAT
10351 {"readlinkat", posix_readlinkat, METH_VARARGS, posix_readlinkat__doc__},
10352#endif /* HAVE_READLINKAT */
10353#ifdef HAVE_RENAMEAT
10354 {"renameat", posix_renameat, METH_VARARGS, posix_renameat__doc__},
10355#endif
10356#if HAVE_SYMLINKAT
10357 {"symlinkat", posix_symlinkat, METH_VARARGS, posix_symlinkat__doc__},
10358#endif /* HAVE_SYMLINKAT */
10359#ifdef HAVE_UNLINKAT
10360 {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
10361#endif
10362#ifdef HAVE_UTIMENSAT
10363 {"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__},
10364#endif
10365#ifdef HAVE_MKFIFOAT
10366 {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},
10367#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010368 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010369};
10370
10371
Barry Warsaw4a342091996-12-19 23:50:02 +000010372static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010373ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +000010374{
Victor Stinner8c62be82010-05-06 00:08:46 +000010375 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +000010376}
10377
Guido van Rossumd48f2521997-12-05 22:19:34 +000010378#if defined(PYOS_OS2)
10379/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010380static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010381{
10382 APIRET rc;
10383 ULONG values[QSV_MAX+1];
10384 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +000010385 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +000010386
10387 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +000010388 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010389 Py_END_ALLOW_THREADS
10390
10391 if (rc != NO_ERROR) {
10392 os2_error(rc);
10393 return -1;
10394 }
10395
Fred Drake4d1e64b2002-04-15 19:40:07 +000010396 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
10397 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
10398 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
10399 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
10400 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
10401 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
10402 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010403
10404 switch (values[QSV_VERSION_MINOR]) {
10405 case 0: ver = "2.00"; break;
10406 case 10: ver = "2.10"; break;
10407 case 11: ver = "2.11"; break;
10408 case 30: ver = "3.00"; break;
10409 case 40: ver = "4.00"; break;
10410 case 50: ver = "5.00"; break;
10411 default:
Tim Peters885d4572001-11-28 20:27:42 +000010412 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +000010413 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +000010414 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010415 ver = &tmp[0];
10416 }
10417
10418 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +000010419 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +000010420 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010421
10422 /* Add Indicator of Which Drive was Used to Boot the System */
10423 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
10424 tmp[1] = ':';
10425 tmp[2] = '\0';
10426
Fred Drake4d1e64b2002-04-15 19:40:07 +000010427 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +000010428}
10429#endif
10430
Brian Curtin52173d42010-12-02 18:29:18 +000010431#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010432static int
Brian Curtin52173d42010-12-02 18:29:18 +000010433enable_symlink()
10434{
10435 HANDLE tok;
10436 TOKEN_PRIVILEGES tok_priv;
10437 LUID luid;
10438 int meth_idx = 0;
10439
10440 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010441 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010442
10443 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010444 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010445
10446 tok_priv.PrivilegeCount = 1;
10447 tok_priv.Privileges[0].Luid = luid;
10448 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10449
10450 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10451 sizeof(TOKEN_PRIVILEGES),
10452 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010453 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010454
Brian Curtin3b4499c2010-12-28 14:31:47 +000010455 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10456 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010457}
10458#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10459
Barry Warsaw4a342091996-12-19 23:50:02 +000010460static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010461all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +000010462{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010463#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010464 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010465#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010466#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010467 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010468#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010469#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010471#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010472#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010473 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010474#endif
Fred Drakec9680921999-12-13 16:37:25 +000010475#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010476 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010477#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010478#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010479 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010480#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010481#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010482 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010483#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010484#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +000010485 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010486#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010487#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010489#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010490#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010492#endif
10493#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010495#endif
10496#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010498#endif
10499#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010501#endif
10502#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010504#endif
10505#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010507#endif
10508#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010510#endif
10511#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010513#endif
10514#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010516#endif
10517#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010519#endif
10520#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010522#endif
10523#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010525#endif
10526#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010528#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000010529#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010531#endif
10532#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010534#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010535#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010537#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000010538#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010539 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010540#endif
10541#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010543#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010544#ifdef PRIO_PROCESS
10545 if (ins(d, "PRIO_PROCESS", (long)PRIO_PROCESS)) return -1;
10546#endif
10547#ifdef PRIO_PGRP
10548 if (ins(d, "PRIO_PGRP", (long)PRIO_PGRP)) return -1;
10549#endif
10550#ifdef PRIO_USER
10551 if (ins(d, "PRIO_USER", (long)PRIO_USER)) return -1;
10552#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010553#ifdef O_CLOEXEC
10554 if (ins(d, "O_CLOEXEC", (long)O_CLOEXEC)) return -1;
10555#endif
Antoine Pitrouf65132d2011-02-25 23:25:17 +000010556/* posix - constants for *at functions */
10557#ifdef AT_SYMLINK_NOFOLLOW
10558 if (ins(d, "AT_SYMLINK_NOFOLLOW", (long)AT_SYMLINK_NOFOLLOW)) return -1;
10559#endif
10560#ifdef AT_EACCESS
10561 if (ins(d, "AT_EACCESS", (long)AT_EACCESS)) return -1;
10562#endif
10563#ifdef AT_FDCWD
10564 if (ins(d, "AT_FDCWD", (long)AT_FDCWD)) return -1;
10565#endif
10566#ifdef AT_REMOVEDIR
10567 if (ins(d, "AT_REMOVEDIR", (long)AT_REMOVEDIR)) return -1;
10568#endif
10569#ifdef AT_SYMLINK_FOLLOW
10570 if (ins(d, "AT_SYMLINK_FOLLOW", (long)AT_SYMLINK_FOLLOW)) return -1;
10571#endif
10572#ifdef UTIME_NOW
10573 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10574#endif
10575#ifdef UTIME_OMIT
10576 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10577#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010578
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010579
Tim Peters5aa91602002-01-30 05:46:57 +000010580/* MS Windows */
10581#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010582 /* Don't inherit in child processes. */
10583 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010584#endif
10585#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000010586 /* Optimize for short life (keep in memory). */
10587 /* MS forgot to define this one with a non-underscore form too. */
10588 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010589#endif
10590#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010591 /* Automatically delete when last handle is closed. */
10592 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010593#endif
10594#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000010595 /* Optimize for random access. */
10596 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010597#endif
10598#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 /* Optimize for sequential access. */
10600 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010601#endif
10602
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010603/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010604#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 /* Send a SIGIO signal whenever input or output
10606 becomes available on file descriptor */
10607 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010608#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010609#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010610 /* Direct disk access. */
10611 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010612#endif
10613#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 /* Must be a directory. */
10615 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010616#endif
10617#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000010618 /* Do not follow links. */
10619 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010620#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010621#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010622 /* Do not update the access time. */
10623 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000010624#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000010625
Victor Stinner8c62be82010-05-06 00:08:46 +000010626 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010627#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +000010628 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010629#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010630#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010631 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010632#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010633#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010634 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010635#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010636#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010637 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010638#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010639#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +000010640 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010641#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010642#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +000010643 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010644#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010645#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010646 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010647#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010648#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +000010649 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010650#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010651#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010652 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010653#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010654#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +000010655 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010656#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010657#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +000010658 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010659#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010660#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010662#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010663#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +000010664 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010665#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010666#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +000010667 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010668#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010669#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010671#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010672#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010673 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010674#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010675#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +000010676 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000010677#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000010678
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010679 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010680#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010681 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010682#endif /* ST_RDONLY */
10683#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000010684 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000010685#endif /* ST_NOSUID */
10686
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010687 /* FreeBSD sendfile() constants */
10688#ifdef SF_NODISKIO
10689 if (ins(d, "SF_NODISKIO", (long)SF_NODISKIO)) return -1;
10690#endif
10691#ifdef SF_MNOWAIT
10692 if (ins(d, "SF_MNOWAIT", (long)SF_MNOWAIT)) return -1;
10693#endif
10694#ifdef SF_SYNC
10695 if (ins(d, "SF_SYNC", (long)SF_SYNC)) return -1;
10696#endif
10697
Ross Lagerwall7807c352011-03-17 20:20:30 +020010698 /* constants for posix_fadvise */
10699#ifdef POSIX_FADV_NORMAL
10700 if (ins(d, "POSIX_FADV_NORMAL", (long)POSIX_FADV_NORMAL)) return -1;
10701#endif
10702#ifdef POSIX_FADV_SEQUENTIAL
10703 if (ins(d, "POSIX_FADV_SEQUENTIAL", (long)POSIX_FADV_SEQUENTIAL)) return -1;
10704#endif
10705#ifdef POSIX_FADV_RANDOM
10706 if (ins(d, "POSIX_FADV_RANDOM", (long)POSIX_FADV_RANDOM)) return -1;
10707#endif
10708#ifdef POSIX_FADV_NOREUSE
10709 if (ins(d, "POSIX_FADV_NOREUSE", (long)POSIX_FADV_NOREUSE)) return -1;
10710#endif
10711#ifdef POSIX_FADV_WILLNEED
10712 if (ins(d, "POSIX_FADV_WILLNEED", (long)POSIX_FADV_WILLNEED)) return -1;
10713#endif
10714#ifdef POSIX_FADV_DONTNEED
10715 if (ins(d, "POSIX_FADV_DONTNEED", (long)POSIX_FADV_DONTNEED)) return -1;
10716#endif
10717
10718 /* constants for waitid */
10719#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
10720 if (ins(d, "P_PID", (long)P_PID)) return -1;
10721 if (ins(d, "P_PGID", (long)P_PGID)) return -1;
10722 if (ins(d, "P_ALL", (long)P_ALL)) return -1;
10723#endif
10724#ifdef WEXITED
10725 if (ins(d, "WEXITED", (long)WEXITED)) return -1;
10726#endif
10727#ifdef WNOWAIT
10728 if (ins(d, "WNOWAIT", (long)WNOWAIT)) return -1;
10729#endif
10730#ifdef WSTOPPED
10731 if (ins(d, "WSTOPPED", (long)WSTOPPED)) return -1;
10732#endif
10733#ifdef CLD_EXITED
10734 if (ins(d, "CLD_EXITED", (long)CLD_EXITED)) return -1;
10735#endif
10736#ifdef CLD_DUMPED
10737 if (ins(d, "CLD_DUMPED", (long)CLD_DUMPED)) return -1;
10738#endif
10739#ifdef CLD_TRAPPED
10740 if (ins(d, "CLD_TRAPPED", (long)CLD_TRAPPED)) return -1;
10741#endif
10742#ifdef CLD_CONTINUED
10743 if (ins(d, "CLD_CONTINUED", (long)CLD_CONTINUED)) return -1;
10744#endif
10745
10746 /* constants for lockf */
10747#ifdef F_LOCK
10748 if (ins(d, "F_LOCK", (long)F_LOCK)) return -1;
10749#endif
10750#ifdef F_TLOCK
10751 if (ins(d, "F_TLOCK", (long)F_TLOCK)) return -1;
10752#endif
10753#ifdef F_ULOCK
10754 if (ins(d, "F_ULOCK", (long)F_ULOCK)) return -1;
10755#endif
10756#ifdef F_TEST
10757 if (ins(d, "F_TEST", (long)F_TEST)) return -1;
10758#endif
10759
10760 /* constants for futimens */
10761#ifdef UTIME_NOW
10762 if (ins(d, "UTIME_NOW", (long)UTIME_NOW)) return -1;
10763#endif
10764#ifdef UTIME_OMIT
10765 if (ins(d, "UTIME_OMIT", (long)UTIME_OMIT)) return -1;
10766#endif
10767
Guido van Rossum246bc171999-02-01 23:54:31 +000010768#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010769#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
10771 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
10772 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
10773 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
10774 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
10775 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
10776 if (ins(d, "P_PM", (long)P_PM)) return -1;
10777 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
10778 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
10779 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
10780 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
10781 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
10782 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
10783 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
10784 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
10785 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
10786 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
10787 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
10788 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
10789 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010790#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
10792 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
10793 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
10794 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
10795 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000010796#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000010797#endif
Guido van Rossum246bc171999-02-01 23:54:31 +000010798
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010799#ifdef HAVE_SCHED_H
10800 if (ins(d, "SCHED_FIFO", (long)SCHED_FIFO)) return -1;
10801 if (ins(d, "SCHED_RR", (long)SCHED_RR)) return -1;
10802#ifdef SCHED_SPORADIC
10803 if (ins(d, "SCHED_SPORADIC", (long)SCHED_SPORADIC) return -1;
10804#endif
10805 if (ins(d, "SCHED_OTHER", (long)SCHED_OTHER)) return -1;
10806#ifdef SCHED_BATCH
10807 if (ins(d, "SCHED_BATCH", (long)SCHED_BATCH)) return -1;
10808#endif
10809#ifdef SCHED_IDLE
10810 if (ins(d, "SCHED_IDLE", (long)SCHED_IDLE)) return -1;
10811#endif
10812#ifdef SCHED_RESET_ON_FORK
10813 if (ins(d, "SCHED_RESET_ON_FORK", (long)SCHED_RESET_ON_FORK)) return -1;
10814#endif
10815#endif
10816
Guido van Rossumd48f2521997-12-05 22:19:34 +000010817#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +000010819#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010820 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000010821}
10822
10823
Tim Peters5aa91602002-01-30 05:46:57 +000010824#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010825#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010826#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010827
10828#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +000010829#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010830#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +000010831
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000010832#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000010833#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000010834#define MODNAME "posix"
10835#endif
10836
Martin v. Löwis1a214512008-06-11 05:26:20 +000010837static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000010838 PyModuleDef_HEAD_INIT,
10839 MODNAME,
10840 posix__doc__,
10841 -1,
10842 posix_methods,
10843 NULL,
10844 NULL,
10845 NULL,
10846 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000010847};
10848
10849
Mark Hammondfe51c6d2002-08-02 02:27:13 +000010850PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000010851INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000010852{
Victor Stinner8c62be82010-05-06 00:08:46 +000010853 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +000010854
Brian Curtin52173d42010-12-02 18:29:18 +000010855#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010856 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000010857#endif
10858
Victor Stinner8c62be82010-05-06 00:08:46 +000010859 m = PyModule_Create(&posixmodule);
10860 if (m == NULL)
10861 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000010862
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 /* Initialize environ dictionary */
10864 v = convertenviron();
10865 Py_XINCREF(v);
10866 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
10867 return NULL;
10868 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000010869
Victor Stinner8c62be82010-05-06 00:08:46 +000010870 if (all_ins(m))
10871 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000010872
Victor Stinner8c62be82010-05-06 00:08:46 +000010873 if (setup_confname_tables(m))
10874 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000010875
Victor Stinner8c62be82010-05-06 00:08:46 +000010876 Py_INCREF(PyExc_OSError);
10877 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000010878
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010879 if (PyType_Ready(&cpu_set_type) < 0)
10880 return NULL;
10881 Py_INCREF(&cpu_set_type);
10882 PyModule_AddObject(m, "cpu_set", (PyObject *)&cpu_set_type);
10883
Guido van Rossumb3d39562000-01-31 18:41:26 +000010884#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010885 if (posix_putenv_garbage == NULL)
10886 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000010887#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010888
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010890#if defined(HAVE_WAITID) && !defined(__APPLE__)
10891 waitid_result_desc.name = MODNAME ".waitid_result";
10892 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
10893#endif
10894
Victor Stinner8c62be82010-05-06 00:08:46 +000010895 stat_result_desc.name = MODNAME ".stat_result";
10896 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
10897 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
10898 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
10899 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
10900 structseq_new = StatResultType.tp_new;
10901 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010902
Victor Stinner8c62be82010-05-06 00:08:46 +000010903 statvfs_result_desc.name = MODNAME ".statvfs_result";
10904 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010905#ifdef NEED_TICKS_PER_SECOND
10906# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000010907 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010908# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000010909 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010910# else
Victor Stinner8c62be82010-05-06 00:08:46 +000010911 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000010912# endif
10913#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010914
10915#ifdef HAVE_SCHED_H
10916 sched_param_desc.name = MODNAME ".sched_param";
10917 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
10918 SchedParamType.tp_new = sched_param_new;
10919#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010920 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020010921#if defined(HAVE_WAITID) && !defined(__APPLE__)
10922 Py_INCREF((PyObject*) &WaitidResultType);
10923 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
10924#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010925 Py_INCREF((PyObject*) &StatResultType);
10926 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
10927 Py_INCREF((PyObject*) &StatVFSResultType);
10928 PyModule_AddObject(m, "statvfs_result",
10929 (PyObject*) &StatVFSResultType);
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010930 Py_INCREF(&SchedParamType);
10931 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010932 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010933
10934#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 /*
10936 * Step 2 of weak-linking support on Mac OS X.
10937 *
10938 * The code below removes functions that are not available on the
10939 * currently active platform.
10940 *
10941 * This block allow one to use a python binary that was build on
10942 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
10943 * OSX 10.4.
10944 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010945#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010946 if (fstatvfs == NULL) {
10947 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
10948 return NULL;
10949 }
10950 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010951#endif /* HAVE_FSTATVFS */
10952
10953#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000010954 if (statvfs == NULL) {
10955 if (PyObject_DelAttrString(m, "statvfs") == -1) {
10956 return NULL;
10957 }
10958 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010959#endif /* HAVE_STATVFS */
10960
10961# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010962 if (lchown == NULL) {
10963 if (PyObject_DelAttrString(m, "lchown") == -1) {
10964 return NULL;
10965 }
10966 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000010967#endif /* HAVE_LCHOWN */
10968
10969
10970#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000010972
Guido van Rossumb6775db1994-08-01 11:34:53 +000010973}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010974
10975#ifdef __cplusplus
10976}
10977#endif