blob: 767ed612903af84fe9e1a0c247403354a2a060ff [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004/* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default
11 compiler is assumed to be IBM's VisualAge C++ (VACPP). PYCC_GCC is used
12 as the compiler specific macro for the EMX port of gcc to OS/2. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000013
Guido van Rossuma4916fa1996-05-23 22:58:55 +000014/* See also ../Dos/dosmodule.c */
Guido van Rossumad0ee831995-03-01 10:34:45 +000015
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#ifdef __APPLE__
17 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000018 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000019 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
20 * at the end of this file for more information.
21 */
22# pragma weak lchown
23# pragma weak statvfs
24# pragma weak fstatvfs
25
26#endif /* __APPLE__ */
27
Thomas Wouters68bc4f92006-03-01 01:05:10 +000028#define PY_SSIZE_T_CLEAN
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030#include "Python.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034#endif /* defined(__VMS) */
35
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000041"This module provides access to operating system functionality that is\n\
42standardized by the C Standard and the POSIX standard (a thinly\n\
43disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000046
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000047#if defined(PYOS_OS2)
48#define INCL_DOS
49#define INCL_DOSERRORS
50#define INCL_DOSPROCESS
51#define INCL_NOPMAPI
52#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000053#if defined(PYCC_GCC)
54#include <ctype.h>
55#include <io.h>
56#include <stdio.h>
57#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000058#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000059#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000060#endif
61
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_TYPES_H */
65
66#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000067#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000069
Guido van Rossum36bc6801995-06-14 22:54:23 +000070#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000071#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000073
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000075#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000077
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#ifdef HAVE_FCNTL_H
79#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000080#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000081
Guido van Rossuma6535fd2001-10-18 19:44:10 +000082#ifdef HAVE_GRP_H
83#include <grp.h>
84#endif
85
Barry Warsaw5676bd12003-01-07 20:57:09 +000086#ifdef HAVE_SYSEXITS_H
87#include <sysexits.h>
88#endif /* HAVE_SYSEXITS_H */
89
Anthony Baxter8a560de2004-10-13 15:30:56 +000090#ifdef HAVE_SYS_LOADAVG_H
91#include <sys/loadavg.h>
92#endif
93
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000094#ifdef HAVE_LANGINFO_H
95#include <langinfo.h>
96#endif
97
Guido van Rossuma4916fa1996-05-23 22:58:55 +000098/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +000099/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000100#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#include <process.h>
102#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000103#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000104#define HAVE_GETCWD 1
105#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000106#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000107#if defined(__OS2__)
108#define HAVE_EXECV 1
109#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000110#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000111#include <process.h>
112#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000113#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000114#define HAVE_EXECV 1
115#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000116#define HAVE_OPENDIR 1
117#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000118#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000119#define HAVE_WAIT 1
120#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000121#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000122#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000123#define HAVE_GETPPID 1
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000124#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000125#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000126#define HAVE_EXECV 1
127#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000128#define HAVE_SYSTEM 1
129#define HAVE_CWAIT 1
130#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000131#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000132#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000133#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
134/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000135#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000136/* Unix functions that the configure script doesn't check for */
137#define HAVE_EXECV 1
138#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000139#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000140#define HAVE_FORK1 1
141#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000142#define HAVE_GETCWD 1
143#define HAVE_GETEGID 1
144#define HAVE_GETEUID 1
145#define HAVE_GETGID 1
146#define HAVE_GETPPID 1
147#define HAVE_GETUID 1
148#define HAVE_KILL 1
149#define HAVE_OPENDIR 1
150#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000151#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000152#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000154#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000155#endif /* _MSC_VER */
156#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000157#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000158#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000159
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000160#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000161
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000162#if defined(__sgi)&&_COMPILER_VERSION>=700
163/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
164 (default) */
165extern char *ctermid_r(char *);
166#endif
167
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000168#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000169#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000170extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000171#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000172#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000173extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000174#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000175extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000177#endif
178#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000179extern int chdir(char *);
180extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000181#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000182extern int chdir(const char *);
183extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000184#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000185#ifdef __BORLANDC__
186extern int chmod(const char *, int);
187#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000188extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000189#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000190/*#ifdef HAVE_FCHMOD
191extern int fchmod(int, mode_t);
192#endif*/
193/*#ifdef HAVE_LCHMOD
194extern int lchmod(const char *, mode_t);
195#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000196extern int chown(const char *, uid_t, gid_t);
197extern char *getcwd(char *, int);
198extern char *strerror(int);
199extern int link(const char *, const char *);
200extern int rename(const char *, const char *);
201extern int stat(const char *, struct stat *);
202extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000204extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000205#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000208#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213#ifdef HAVE_UTIME_H
214#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000215#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000216
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000217#ifdef HAVE_SYS_UTIME_H
218#include <sys/utime.h>
219#define HAVE_UTIME_H /* pretend we do for the rest of this file */
220#endif /* HAVE_SYS_UTIME_H */
221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#ifdef HAVE_SYS_TIMES_H
223#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000224#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225
226#ifdef HAVE_SYS_PARAM_H
227#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000228#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229
230#ifdef HAVE_SYS_UTSNAME_H
231#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000232#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000233
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000234#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#define NAMLEN(dirent) strlen((dirent)->d_name)
237#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000238#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#include <direct.h>
240#define NAMLEN(dirent) strlen((dirent)->d_name)
241#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000243#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000244#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000245#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000247#endif
248#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000250#endif
251#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#endif
254#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000256#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000257#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000259#endif
260#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000261#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262#endif
263#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000266#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000267#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000268#endif
269#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000270#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000271#endif
272#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000273#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000274#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000276#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000278#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000279#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000280#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
281#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000282static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000283#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000284#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285
Guido van Rossumd48f2521997-12-05 22:19:34 +0000286#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000288#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000291#if defined(PATH_MAX) && PATH_MAX > 1024
292#define MAXPATHLEN PATH_MAX
293#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000294#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000295#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000296#endif /* MAXPATHLEN */
297
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000298#ifdef UNION_WAIT
299/* Emulate some macros on systems that have a union instead of macros */
300
301#ifndef WIFEXITED
302#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
303#endif
304
305#ifndef WEXITSTATUS
306#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
307#endif
308
309#ifndef WTERMSIG
310#define WTERMSIG(u_wait) ((u_wait).w_termsig)
311#endif
312
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313#define WAIT_TYPE union wait
314#define WAIT_STATUS_INT(s) (s.w_status)
315
316#else /* !UNION_WAIT */
317#define WAIT_TYPE int
318#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000319#endif /* UNION_WAIT */
320
Greg Wardb48bc172000-03-01 21:51:56 +0000321/* Don't use the "_r" form if we don't need it (also, won't have a
322 prototype for it, at least on Solaris -- maybe others as well?). */
323#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
324#define USE_CTERMID_R
325#endif
326
Fred Drake699f3522000-06-29 21:12:41 +0000327/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000328#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000329#undef FSTAT
330#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000331#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000332# define STAT win32_stat
333# define FSTAT win32_fstat
334# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000335#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000336# define STAT stat
337# define FSTAT fstat
338# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000339#endif
340
Tim Peters11b23062003-04-23 02:39:17 +0000341#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000342#include <sys/mkdev.h>
343#else
344#if defined(MAJOR_IN_SYSMACROS)
345#include <sys/sysmacros.h>
346#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000347#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
348#include <sys/mkdev.h>
349#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000350#endif
Fred Drake699f3522000-06-29 21:12:41 +0000351
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000352#if defined _MSC_VER && _MSC_VER >= 1400
353/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
354 * valid and throw an assertion if it isn't.
355 * Normally, an invalid fd is likely to be a C program error and therefore
356 * an assertion can be useful, but it does contradict the POSIX standard
357 * which for write(2) states:
358 * "Otherwise, -1 shall be returned and errno set to indicate the error."
359 * "[EBADF] The fildes argument is not a valid file descriptor open for
360 * writing."
361 * Furthermore, python allows the user to enter any old integer
362 * as a fd and should merely raise a python exception on error.
363 * The Microsoft CRT doesn't provide an official way to check for the
364 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000365 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000366 * internal structures involved.
367 * The structures below must be updated for each version of visual studio
368 * according to the file internal.h in the CRT source, until MS comes
369 * up with a less hacky way to do this.
370 * (all of this is to avoid globally modifying the CRT behaviour using
371 * _set_invalid_parameter_handler() and _CrtSetReportMode())
372 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000373/* The actual size of the structure is determined at runtime.
374 * Only the first items must be present.
375 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000376typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000377 intptr_t osfhnd;
378 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000381extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000382#define IOINFO_L2E 5
383#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
384#define IOINFO_ARRAYS 64
385#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
386#define FOPEN 0x01
387#define _NO_CONSOLE_FILENO (intptr_t)-2
388
389/* This function emulates what the windows CRT does to validate file handles */
390int
391_PyVerify_fd(int fd)
392{
Victor Stinner8c62be82010-05-06 00:08:46 +0000393 const int i1 = fd >> IOINFO_L2E;
394 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000395
Antoine Pitrou22e41552010-08-15 18:07:50 +0000396 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000397
Victor Stinner8c62be82010-05-06 00:08:46 +0000398 /* Determine the actual size of the ioinfo structure,
399 * as used by the CRT loaded in memory
400 */
401 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
402 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
403 }
404 if (sizeof_ioinfo == 0) {
405 /* This should not happen... */
406 goto fail;
407 }
408
409 /* See that it isn't a special CLEAR fileno */
410 if (fd != _NO_CONSOLE_FILENO) {
411 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
412 * we check pointer validity and other info
413 */
414 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
415 /* finally, check that the file is open */
416 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
417 if (info->osfile & FOPEN) {
418 return 1;
419 }
420 }
421 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000422 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000423 errno = EBADF;
424 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000425}
426
427/* the special case of checking dup2. The target fd must be in a sensible range */
428static int
429_PyVerify_fd_dup2(int fd1, int fd2)
430{
Victor Stinner8c62be82010-05-06 00:08:46 +0000431 if (!_PyVerify_fd(fd1))
432 return 0;
433 if (fd2 == _NO_CONSOLE_FILENO)
434 return 0;
435 if ((unsigned)fd2 < _NHANDLE_)
436 return 1;
437 else
438 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000439}
440#else
441/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
442#define _PyVerify_fd_dup2(A, B) (1)
443#endif
444
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000445#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000446/* The following structure was copied from
447 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
448 include doesn't seem to be present in the Windows SDK (at least as included
449 with Visual Studio Express). */
450typedef struct _REPARSE_DATA_BUFFER {
451 ULONG ReparseTag;
452 USHORT ReparseDataLength;
453 USHORT Reserved;
454 union {
455 struct {
456 USHORT SubstituteNameOffset;
457 USHORT SubstituteNameLength;
458 USHORT PrintNameOffset;
459 USHORT PrintNameLength;
460 ULONG Flags;
461 WCHAR PathBuffer[1];
462 } SymbolicLinkReparseBuffer;
463
464 struct {
465 USHORT SubstituteNameOffset;
466 USHORT SubstituteNameLength;
467 USHORT PrintNameOffset;
468 USHORT PrintNameLength;
469 WCHAR PathBuffer[1];
470 } MountPointReparseBuffer;
471
472 struct {
473 UCHAR DataBuffer[1];
474 } GenericReparseBuffer;
475 };
476} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
477
478#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
479 GenericReparseBuffer)
480#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
481
482static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000483win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000484{
485 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
486 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
487 DWORD n_bytes_returned;
488 const wchar_t *ptr;
489 wchar_t *buf;
490 size_t len;
491
492 if (0 == DeviceIoControl(
493 reparse_point_handle,
494 FSCTL_GET_REPARSE_POINT,
495 NULL, 0, /* in buffer */
496 target_buffer, sizeof(target_buffer),
497 &n_bytes_returned,
498 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000499 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000500
501 if (reparse_tag)
502 *reparse_tag = rdb->ReparseTag;
503
504 if (target_path) {
505 switch (rdb->ReparseTag) {
506 case IO_REPARSE_TAG_SYMLINK:
507 /* XXX: Maybe should use SubstituteName? */
508 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
509 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
510 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
511 break;
512 case IO_REPARSE_TAG_MOUNT_POINT:
513 ptr = rdb->MountPointReparseBuffer.PathBuffer +
514 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
515 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
516 break;
517 default:
518 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000519 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000520 }
521 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
522 if (!buf) {
523 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000524 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000525 }
526 wcsncpy(buf, ptr, len);
527 buf[len] = L'\0';
528 if (wcsncmp(buf, L"\\??\\", 4) == 0)
529 buf[1] = L'\\';
530 *target_path = buf;
531 }
532
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000533 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000534}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000535#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000536
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000538#ifdef WITH_NEXT_FRAMEWORK
539/* On Darwin/MacOSX a shared library or framework has no access to
540** environ directly, we must obtain it with _NSGetEnviron().
541*/
542#include <crt_externs.h>
543static char **environ;
544#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000546#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547
Barry Warsaw53699e91996-12-10 23:23:01 +0000548static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000549convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550{
Victor Stinner8c62be82010-05-06 00:08:46 +0000551 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000552#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000553 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000554#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000555 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000556#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000557#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000558 APIRET rc;
559 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
560#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000561
Victor Stinner8c62be82010-05-06 00:08:46 +0000562 d = PyDict_New();
563 if (d == NULL)
564 return NULL;
565#ifdef WITH_NEXT_FRAMEWORK
566 if (environ == NULL)
567 environ = *_NSGetEnviron();
568#endif
569#ifdef MS_WINDOWS
570 /* _wenviron must be initialized in this way if the program is started
571 through main() instead of wmain(). */
572 _wgetenv(L"");
573 if (_wenviron == NULL)
574 return d;
575 /* This part ignores errors */
576 for (e = _wenviron; *e != NULL; e++) {
577 PyObject *k;
578 PyObject *v;
579 wchar_t *p = wcschr(*e, L'=');
580 if (p == NULL)
581 continue;
582 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
583 if (k == NULL) {
584 PyErr_Clear();
585 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000586 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000587 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
588 if (v == NULL) {
589 PyErr_Clear();
590 Py_DECREF(k);
591 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000592 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000593 if (PyDict_GetItem(d, k) == NULL) {
594 if (PyDict_SetItem(d, k, v) != 0)
595 PyErr_Clear();
596 }
597 Py_DECREF(k);
598 Py_DECREF(v);
599 }
600#else
601 if (environ == NULL)
602 return d;
603 /* This part ignores errors */
604 for (e = environ; *e != NULL; e++) {
605 PyObject *k;
606 PyObject *v;
607 char *p = strchr(*e, '=');
608 if (p == NULL)
609 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000610 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000611 if (k == NULL) {
612 PyErr_Clear();
613 continue;
614 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000615 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000616 if (v == NULL) {
617 PyErr_Clear();
618 Py_DECREF(k);
619 continue;
620 }
621 if (PyDict_GetItem(d, k) == NULL) {
622 if (PyDict_SetItem(d, k, v) != 0)
623 PyErr_Clear();
624 }
625 Py_DECREF(k);
626 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000627 }
628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000629#if defined(PYOS_OS2)
630 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
631 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
632 PyObject *v = PyBytes_FromString(buffer);
633 PyDict_SetItemString(d, "BEGINLIBPATH", v);
634 Py_DECREF(v);
635 }
636 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
637 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
638 PyObject *v = PyBytes_FromString(buffer);
639 PyDict_SetItemString(d, "ENDLIBPATH", v);
640 Py_DECREF(v);
641 }
642#endif
643 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644}
645
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646/* Set a POSIX-specific error from errno, and return NULL */
647
Barry Warsawd58d7641998-07-23 16:14:40 +0000648static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000649posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000650{
Victor Stinner8c62be82010-05-06 00:08:46 +0000651 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652}
Barry Warsawd58d7641998-07-23 16:14:40 +0000653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000654posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000655{
Victor Stinner8c62be82010-05-06 00:08:46 +0000656 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000657}
658
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000659
Mark Hammondef8b6542001-05-13 08:04:26 +0000660static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000661posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000662{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000663 PyObject *name_str, *rc;
664 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
665 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000666 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000667 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
668 name_str);
669 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000670 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000671}
672
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000673#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000674static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000675win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000676{
Victor Stinner8c62be82010-05-06 00:08:46 +0000677 /* XXX We should pass the function name along in the future.
678 (winreg.c also wants to pass the function name.)
679 This would however require an additional param to the
680 Windows error object, which is non-trivial.
681 */
682 errno = GetLastError();
683 if (filename)
684 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
685 else
686 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000687}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000688
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000689static PyObject *
690win32_error_unicode(char* function, Py_UNICODE* filename)
691{
Victor Stinner8c62be82010-05-06 00:08:46 +0000692 /* XXX - see win32_error for comments on 'function' */
693 errno = GetLastError();
694 if (filename)
695 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
696 else
697 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000698}
699
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000701convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000702{
Victor Stinner8c62be82010-05-06 00:08:46 +0000703 if (PyUnicode_CheckExact(*param))
704 Py_INCREF(*param);
705 else if (PyUnicode_Check(*param))
706 /* For a Unicode subtype that's not a Unicode object,
707 return a true Unicode object with the same data. */
708 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
709 PyUnicode_GET_SIZE(*param));
710 else
711 *param = PyUnicode_FromEncodedObject(*param,
712 Py_FileSystemDefaultEncoding,
713 "strict");
714 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000715}
716
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000717#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718
Guido van Rossumd48f2521997-12-05 22:19:34 +0000719#if defined(PYOS_OS2)
720/**********************************************************************
721 * Helper Function to Trim and Format OS/2 Messages
722 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000723static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000724os2_formatmsg(char *msgbuf, int msglen, char *reason)
725{
726 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
727
728 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
729 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
730
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000731 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000732 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
733 }
734
735 /* Add Optional Reason Text */
736 if (reason) {
737 strcat(msgbuf, " : ");
738 strcat(msgbuf, reason);
739 }
740}
741
742/**********************************************************************
743 * Decode an OS/2 Operating System Error Code
744 *
745 * A convenience function to lookup an OS/2 error code and return a
746 * text message we can use to raise a Python exception.
747 *
748 * Notes:
749 * The messages for errors returned from the OS/2 kernel reside in
750 * the file OSO001.MSG in the \OS2 directory hierarchy.
751 *
752 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000753static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000754os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
755{
756 APIRET rc;
757 ULONG msglen;
758
759 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
760 Py_BEGIN_ALLOW_THREADS
761 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
762 errorcode, "oso001.msg", &msglen);
763 Py_END_ALLOW_THREADS
764
765 if (rc == NO_ERROR)
766 os2_formatmsg(msgbuf, msglen, reason);
767 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000768 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000769 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000770
771 return msgbuf;
772}
773
774/* Set an OS/2-specific error and return NULL. OS/2 kernel
775 errors are not in a global variable e.g. 'errno' nor are
776 they congruent with posix error numbers. */
777
Victor Stinner8c62be82010-05-06 00:08:46 +0000778static PyObject *
779os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000780{
781 char text[1024];
782 PyObject *v;
783
784 os2_strerror(text, sizeof(text), code, "");
785
786 v = Py_BuildValue("(is)", code, text);
787 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000788 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000789 Py_DECREF(v);
790 }
791 return NULL; /* Signal to Python that an Exception is Pending */
792}
793
794#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795
796/* POSIX generic methods */
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000799posix_fildes(PyObject *fdobj, int (*func)(int))
800{
Victor Stinner8c62be82010-05-06 00:08:46 +0000801 int fd;
802 int res;
803 fd = PyObject_AsFileDescriptor(fdobj);
804 if (fd < 0)
805 return NULL;
806 if (!_PyVerify_fd(fd))
807 return posix_error();
808 Py_BEGIN_ALLOW_THREADS
809 res = (*func)(fd);
810 Py_END_ALLOW_THREADS
811 if (res < 0)
812 return posix_error();
813 Py_INCREF(Py_None);
814 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000815}
Guido van Rossum21142a01999-01-08 21:05:37 +0000816
817static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000818posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819{
Victor Stinner8c62be82010-05-06 00:08:46 +0000820 PyObject *opath1 = NULL;
821 char *path1;
822 int res;
823 if (!PyArg_ParseTuple(args, format,
824 PyUnicode_FSConverter, &opath1))
825 return NULL;
826 path1 = PyBytes_AsString(opath1);
827 Py_BEGIN_ALLOW_THREADS
828 res = (*func)(path1);
829 Py_END_ALLOW_THREADS
830 if (res < 0)
831 return posix_error_with_allocated_filename(opath1);
832 Py_DECREF(opath1);
833 Py_INCREF(Py_None);
834 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000835}
836
Barry Warsaw53699e91996-12-10 23:23:01 +0000837static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000838posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000839 char *format,
840 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000841{
Victor Stinner8c62be82010-05-06 00:08:46 +0000842 PyObject *opath1 = NULL, *opath2 = NULL;
843 char *path1, *path2;
844 int res;
845 if (!PyArg_ParseTuple(args, format,
846 PyUnicode_FSConverter, &opath1,
847 PyUnicode_FSConverter, &opath2)) {
848 return NULL;
849 }
850 path1 = PyBytes_AsString(opath1);
851 path2 = PyBytes_AsString(opath2);
852 Py_BEGIN_ALLOW_THREADS
853 res = (*func)(path1, path2);
854 Py_END_ALLOW_THREADS
855 Py_DECREF(opath1);
856 Py_DECREF(opath2);
857 if (res != 0)
858 /* XXX how to report both path1 and path2??? */
859 return posix_error();
860 Py_INCREF(Py_None);
861 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862}
863
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000864#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000865static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000866win32_1str(PyObject* args, char* func,
867 char* format, BOOL (__stdcall *funcA)(LPCSTR),
868 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000869{
Victor Stinner8c62be82010-05-06 00:08:46 +0000870 PyObject *uni;
871 char *ansi;
872 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000873
Victor Stinner8c62be82010-05-06 00:08:46 +0000874 if (!PyArg_ParseTuple(args, wformat, &uni))
875 PyErr_Clear();
876 else {
877 Py_BEGIN_ALLOW_THREADS
878 result = funcW(PyUnicode_AsUnicode(uni));
879 Py_END_ALLOW_THREADS
880 if (!result)
881 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
882 Py_INCREF(Py_None);
883 return Py_None;
884 }
885 if (!PyArg_ParseTuple(args, format, &ansi))
886 return NULL;
887 Py_BEGIN_ALLOW_THREADS
888 result = funcA(ansi);
889 Py_END_ALLOW_THREADS
890 if (!result)
891 return win32_error(func, ansi);
892 Py_INCREF(Py_None);
893 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894
895}
896
897/* This is a reimplementation of the C library's chdir function,
898 but one that produces Win32 errors instead of DOS error codes.
899 chdir is essentially a wrapper around SetCurrentDirectory; however,
900 it also needs to set "magic" environment variables indicating
901 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000902static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903win32_chdir(LPCSTR path)
904{
Victor Stinner8c62be82010-05-06 00:08:46 +0000905 char new_path[MAX_PATH+1];
906 int result;
907 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000908
Victor Stinner8c62be82010-05-06 00:08:46 +0000909 if(!SetCurrentDirectoryA(path))
910 return FALSE;
911 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
912 if (!result)
913 return FALSE;
914 /* In the ANSI API, there should not be any paths longer
915 than MAX_PATH. */
916 assert(result <= MAX_PATH+1);
917 if (strncmp(new_path, "\\\\", 2) == 0 ||
918 strncmp(new_path, "//", 2) == 0)
919 /* UNC path, nothing to do. */
920 return TRUE;
921 env[1] = new_path[0];
922 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000923}
924
925/* The Unicode version differs from the ANSI version
926 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000927static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928win32_wchdir(LPCWSTR path)
929{
Victor Stinner8c62be82010-05-06 00:08:46 +0000930 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
931 int result;
932 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000933
Victor Stinner8c62be82010-05-06 00:08:46 +0000934 if(!SetCurrentDirectoryW(path))
935 return FALSE;
936 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
937 if (!result)
938 return FALSE;
939 if (result > MAX_PATH+1) {
940 new_path = malloc(result * sizeof(wchar_t));
941 if (!new_path) {
942 SetLastError(ERROR_OUTOFMEMORY);
943 return FALSE;
944 }
945 result = GetCurrentDirectoryW(result, new_path);
946 if (!result) {
947 free(new_path);
948 return FALSE;
949 }
950 }
951 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
952 wcsncmp(new_path, L"//", 2) == 0)
953 /* UNC path, nothing to do. */
954 return TRUE;
955 env[1] = new_path[0];
956 result = SetEnvironmentVariableW(env, new_path);
957 if (new_path != _new_path)
958 free(new_path);
959 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960}
961#endif
962
Martin v. Löwis14694662006-02-03 12:54:16 +0000963#ifdef MS_WINDOWS
964/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
965 - time stamps are restricted to second resolution
966 - file modification times suffer from forth-and-back conversions between
967 UTC and local time
968 Therefore, we implement our own stat, based on the Win32 API directly.
969*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000970#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000971
972struct win32_stat{
973 int st_dev;
974 __int64 st_ino;
975 unsigned short st_mode;
976 int st_nlink;
977 int st_uid;
978 int st_gid;
979 int st_rdev;
980 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000981 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000982 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000983 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000984 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000985 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000986 int st_ctime_nsec;
987};
988
989static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
990
991static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000992FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000993{
Victor Stinner8c62be82010-05-06 00:08:46 +0000994 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
995 /* Cannot simply cast and dereference in_ptr,
996 since it might not be aligned properly */
997 __int64 in;
998 memcpy(&in, in_ptr, sizeof(in));
999 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001000 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001001}
1002
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001004time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005{
Victor Stinner8c62be82010-05-06 00:08:46 +00001006 /* XXX endianness */
1007 __int64 out;
1008 out = time_in + secs_between_epochs;
1009 out = out * 10000000 + nsec_in / 100;
1010 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001011}
1012
Martin v. Löwis14694662006-02-03 12:54:16 +00001013/* Below, we *know* that ugo+r is 0444 */
1014#if _S_IREAD != 0400
1015#error Unsupported C library
1016#endif
1017static int
1018attributes_to_mode(DWORD attr)
1019{
Victor Stinner8c62be82010-05-06 00:08:46 +00001020 int m = 0;
1021 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1022 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1023 else
1024 m |= _S_IFREG;
1025 if (attr & FILE_ATTRIBUTE_READONLY)
1026 m |= 0444;
1027 else
1028 m |= 0666;
1029 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001030}
1031
1032static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001033attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001034{
Victor Stinner8c62be82010-05-06 00:08:46 +00001035 memset(result, 0, sizeof(*result));
1036 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1037 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1038 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1039 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1040 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001041 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001042 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001043 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1044 /* first clear the S_IFMT bits */
1045 result->st_mode ^= (result->st_mode & 0170000);
1046 /* now set the bits that make this a symlink */
1047 result->st_mode |= 0120000;
1048 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001049
Victor Stinner8c62be82010-05-06 00:08:46 +00001050 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001051}
1052
Guido van Rossumd8faa362007-04-27 19:54:29 +00001053static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001054attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001055{
Victor Stinner8c62be82010-05-06 00:08:46 +00001056 HANDLE hFindFile;
1057 WIN32_FIND_DATAA FileData;
1058 hFindFile = FindFirstFileA(pszFile, &FileData);
1059 if (hFindFile == INVALID_HANDLE_VALUE)
1060 return FALSE;
1061 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001062 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001063 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001064 info->dwFileAttributes = FileData.dwFileAttributes;
1065 info->ftCreationTime = FileData.ftCreationTime;
1066 info->ftLastAccessTime = FileData.ftLastAccessTime;
1067 info->ftLastWriteTime = FileData.ftLastWriteTime;
1068 info->nFileSizeHigh = FileData.nFileSizeHigh;
1069 info->nFileSizeLow = FileData.nFileSizeLow;
1070/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001071 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1072 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001073 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001074}
1075
1076static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001077attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001078{
Victor Stinner8c62be82010-05-06 00:08:46 +00001079 HANDLE hFindFile;
1080 WIN32_FIND_DATAW FileData;
1081 hFindFile = FindFirstFileW(pszFile, &FileData);
1082 if (hFindFile == INVALID_HANDLE_VALUE)
1083 return FALSE;
1084 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001085 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001086 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001087 info->dwFileAttributes = FileData.dwFileAttributes;
1088 info->ftCreationTime = FileData.ftCreationTime;
1089 info->ftLastAccessTime = FileData.ftLastAccessTime;
1090 info->ftLastWriteTime = FileData.ftLastWriteTime;
1091 info->nFileSizeHigh = FileData.nFileSizeHigh;
1092 info->nFileSizeLow = FileData.nFileSizeLow;
1093/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001094 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1095 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001096 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001097}
1098
Brian Curtinf5e76d02010-11-24 13:14:05 +00001099#ifndef SYMLOOP_MAX
1100#define SYMLOOP_MAX ( 88 )
1101#endif
1102
1103static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001104win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001105
1106static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001107win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001108{
1109 int code;
1110 HANDLE hFile;
1111 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001112 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001113 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001114 const char *dot;
1115
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001116 if (depth > SYMLOOP_MAX) {
1117 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1118 return -1;
1119 }
1120
Brian Curtinf5e76d02010-11-24 13:14:05 +00001121 hFile = CreateFileA(
1122 path,
1123 0, /* desired access */
1124 0, /* share mode */
1125 NULL, /* security attributes */
1126 OPEN_EXISTING,
1127 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1128 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1129 NULL);
1130
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001131 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001132 /* Either the target doesn't exist, or we don't have access to
1133 get a handle to it. If the former, we need to return an error.
1134 If the latter, we can use attributes_from_dir. */
1135 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001136 return -1;
1137 /* Could not get attributes on open file. Fall back to
1138 reading the directory. */
1139 if (!attributes_from_dir(path, &info, &reparse_tag))
1140 /* Very strange. This should not fail now */
1141 return -1;
1142 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1143 if (traverse) {
1144 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001145 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001146 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001147 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001148 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001149 } else {
1150 if (!GetFileInformationByHandle(hFile, &info)) {
1151 CloseHandle(hFile);
1152 return -1;;
1153 }
1154 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1155 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1156 CloseHandle(hFile);
1157 if (code < 0)
1158 return code;
1159 if (traverse) {
1160 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1161 free(target_path);
1162 return code;
1163 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001164 } else
1165 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001166 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001167 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001168
1169 /* Set S_IEXEC if it is an .exe, .bat, ... */
1170 dot = strrchr(path, '.');
1171 if (dot) {
1172 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1173 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1174 result->st_mode |= 0111;
1175 }
1176 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001177}
1178
1179static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001180win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001181{
1182 int code;
1183 HANDLE hFile;
1184 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001185 ULONG reparse_tag = 0;
1186 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001187 const wchar_t *dot;
1188
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001189 if (depth > SYMLOOP_MAX) {
1190 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1191 return -1;
1192 }
1193
Brian Curtinf5e76d02010-11-24 13:14:05 +00001194 hFile = CreateFileW(
1195 path,
1196 0, /* desired access */
1197 0, /* share mode */
1198 NULL, /* security attributes */
1199 OPEN_EXISTING,
1200 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1201 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1202 NULL);
1203
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001204 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001205 /* Either the target doesn't exist, or we don't have access to
1206 get a handle to it. If the former, we need to return an error.
1207 If the latter, we can use attributes_from_dir. */
1208 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001209 return -1;
1210 /* Could not get attributes on open file. Fall back to
1211 reading the directory. */
1212 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1213 /* Very strange. This should not fail now */
1214 return -1;
1215 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1216 if (traverse) {
1217 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001218 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001219 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001220 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001221 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001222 } else {
1223 if (!GetFileInformationByHandle(hFile, &info)) {
1224 CloseHandle(hFile);
1225 return -1;;
1226 }
1227 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1228 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1229 CloseHandle(hFile);
1230 if (code < 0)
1231 return code;
1232 if (traverse) {
1233 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1234 free(target_path);
1235 return code;
1236 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001237 } else
1238 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001239 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001240 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001241
1242 /* Set S_IEXEC if it is an .exe, .bat, ... */
1243 dot = wcsrchr(path, '.');
1244 if (dot) {
1245 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1246 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1247 result->st_mode |= 0111;
1248 }
1249 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001250}
1251
1252static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001253win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001254{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001255 /* Protocol violation: we explicitly clear errno, instead of
1256 setting it to a POSIX error. Callers should use GetLastError. */
1257 int code = win32_xstat_impl(path, result, traverse, 0);
1258 errno = 0;
1259 return code;
1260}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001261
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001262static int
1263win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1264{
1265 /* Protocol violation: we explicitly clear errno, instead of
1266 setting it to a POSIX error. Callers should use GetLastError. */
1267 int code = win32_xstat_impl_w(path, result, traverse, 0);
1268 errno = 0;
1269 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001270}
1271
Brian Curtind40e6f72010-07-08 21:39:08 +00001272/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1273 win32_stat_w
1274
1275 In Posix, stat automatically traverses symlinks and returns the stat
1276 structure for the target. In Windows, the equivalent GetFileAttributes by
1277 default does not traverse symlinks and instead returns attributes for
1278 the symlink.
1279
1280 Therefore, win32_lstat will get the attributes traditionally, and
1281 win32_stat will first explicitly resolve the symlink target and then will
1282 call win32_lstat on that result.
1283
Ezio Melotti4969f702011-03-15 05:59:46 +02001284 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001285
1286static int
1287win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001288{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001289 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001290}
1291
Victor Stinner8c62be82010-05-06 00:08:46 +00001292static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001293win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001294{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001295 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001296}
1297
1298static int
1299win32_stat(const char* path, struct win32_stat *result)
1300{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001301 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001302}
1303
1304static int
1305win32_stat_w(const wchar_t* path, struct win32_stat *result)
1306{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001307 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001308}
1309
1310static int
1311win32_fstat(int file_number, struct win32_stat *result)
1312{
Victor Stinner8c62be82010-05-06 00:08:46 +00001313 BY_HANDLE_FILE_INFORMATION info;
1314 HANDLE h;
1315 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001316
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001318
Victor Stinner8c62be82010-05-06 00:08:46 +00001319 /* Protocol violation: we explicitly clear errno, instead of
1320 setting it to a POSIX error. Callers should use GetLastError. */
1321 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001322
Victor Stinner8c62be82010-05-06 00:08:46 +00001323 if (h == INVALID_HANDLE_VALUE) {
1324 /* This is really a C library error (invalid file handle).
1325 We set the Win32 error to the closes one matching. */
1326 SetLastError(ERROR_INVALID_HANDLE);
1327 return -1;
1328 }
1329 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001330
Victor Stinner8c62be82010-05-06 00:08:46 +00001331 type = GetFileType(h);
1332 if (type == FILE_TYPE_UNKNOWN) {
1333 DWORD error = GetLastError();
1334 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001335 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001336 }
1337 /* else: valid but unknown file */
1338 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001339
Victor Stinner8c62be82010-05-06 00:08:46 +00001340 if (type != FILE_TYPE_DISK) {
1341 if (type == FILE_TYPE_CHAR)
1342 result->st_mode = _S_IFCHR;
1343 else if (type == FILE_TYPE_PIPE)
1344 result->st_mode = _S_IFIFO;
1345 return 0;
1346 }
1347
1348 if (!GetFileInformationByHandle(h, &info)) {
1349 return -1;
1350 }
1351
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001352 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001353 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001354 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1355 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001356}
1357
1358#endif /* MS_WINDOWS */
1359
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001360PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001361"stat_result: Result from stat or lstat.\n\n\
1362This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001363 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001364or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1365\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001366Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1367or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001368\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001369See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001370
1371static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001372 {"st_mode", "protection bits"},
1373 {"st_ino", "inode"},
1374 {"st_dev", "device"},
1375 {"st_nlink", "number of hard links"},
1376 {"st_uid", "user ID of owner"},
1377 {"st_gid", "group ID of owner"},
1378 {"st_size", "total size, in bytes"},
1379 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1380 {NULL, "integer time of last access"},
1381 {NULL, "integer time of last modification"},
1382 {NULL, "integer time of last change"},
1383 {"st_atime", "time of last access"},
1384 {"st_mtime", "time of last modification"},
1385 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001386#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001387 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001388#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001389#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001390 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001391#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001392#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001393 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001394#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001395#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001396 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001397#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001398#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001400#endif
1401#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001402 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001403#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001404 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001405};
1406
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001407#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001408#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001409#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001410#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001411#endif
1412
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001413#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001414#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1415#else
1416#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1417#endif
1418
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001419#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001420#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1421#else
1422#define ST_RDEV_IDX ST_BLOCKS_IDX
1423#endif
1424
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001425#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1426#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1427#else
1428#define ST_FLAGS_IDX ST_RDEV_IDX
1429#endif
1430
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001431#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001432#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001433#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001434#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001435#endif
1436
1437#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1438#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1439#else
1440#define ST_BIRTHTIME_IDX ST_GEN_IDX
1441#endif
1442
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001443static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001444 "stat_result", /* name */
1445 stat_result__doc__, /* doc */
1446 stat_result_fields,
1447 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001448};
1449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001450PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001451"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1452This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001453 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001454or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001455\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001456See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001457
1458static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 {"f_bsize", },
1460 {"f_frsize", },
1461 {"f_blocks", },
1462 {"f_bfree", },
1463 {"f_bavail", },
1464 {"f_files", },
1465 {"f_ffree", },
1466 {"f_favail", },
1467 {"f_flag", },
1468 {"f_namemax",},
1469 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001470};
1471
1472static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001473 "statvfs_result", /* name */
1474 statvfs_result__doc__, /* doc */
1475 statvfs_result_fields,
1476 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001477};
1478
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001479static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001480static PyTypeObject StatResultType;
1481static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001482static newfunc structseq_new;
1483
1484static PyObject *
1485statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1486{
Victor Stinner8c62be82010-05-06 00:08:46 +00001487 PyStructSequence *result;
1488 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001489
Victor Stinner8c62be82010-05-06 00:08:46 +00001490 result = (PyStructSequence*)structseq_new(type, args, kwds);
1491 if (!result)
1492 return NULL;
1493 /* If we have been initialized from a tuple,
1494 st_?time might be set to None. Initialize it
1495 from the int slots. */
1496 for (i = 7; i <= 9; i++) {
1497 if (result->ob_item[i+3] == Py_None) {
1498 Py_DECREF(Py_None);
1499 Py_INCREF(result->ob_item[i]);
1500 result->ob_item[i+3] = result->ob_item[i];
1501 }
1502 }
1503 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001504}
1505
1506
1507
1508/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001509static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001510
1511PyDoc_STRVAR(stat_float_times__doc__,
1512"stat_float_times([newval]) -> oldval\n\n\
1513Determine whether os.[lf]stat represents time stamps as float objects.\n\
1514If newval is True, future calls to stat() return floats, if it is False,\n\
1515future calls return ints. \n\
1516If newval is omitted, return the current setting.\n");
1517
1518static PyObject*
1519stat_float_times(PyObject* self, PyObject *args)
1520{
Victor Stinner8c62be82010-05-06 00:08:46 +00001521 int newval = -1;
1522 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1523 return NULL;
1524 if (newval == -1)
1525 /* Return old value */
1526 return PyBool_FromLong(_stat_float_times);
1527 _stat_float_times = newval;
1528 Py_INCREF(Py_None);
1529 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001530}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001531
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001532static void
1533fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1534{
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001536#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001537 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001538#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001539 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001540#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001541 if (!ival)
1542 return;
1543 if (_stat_float_times) {
1544 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1545 } else {
1546 fval = ival;
1547 Py_INCREF(fval);
1548 }
1549 PyStructSequence_SET_ITEM(v, index, ival);
1550 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001551}
1552
Tim Peters5aa91602002-01-30 05:46:57 +00001553/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001554 (used by posix_stat() and posix_fstat()) */
1555static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001556_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001557{
Victor Stinner8c62be82010-05-06 00:08:46 +00001558 unsigned long ansec, mnsec, cnsec;
1559 PyObject *v = PyStructSequence_New(&StatResultType);
1560 if (v == NULL)
1561 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001562
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001564#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001565 PyStructSequence_SET_ITEM(v, 1,
1566 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001567#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001568 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001569#endif
1570#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001571 PyStructSequence_SET_ITEM(v, 2,
1572 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001573#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001575#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001576 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1577 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1578 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001579#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001580 PyStructSequence_SET_ITEM(v, 6,
1581 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001582#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001583 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001584#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001585
Martin v. Löwis14694662006-02-03 12:54:16 +00001586#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001587 ansec = st->st_atim.tv_nsec;
1588 mnsec = st->st_mtim.tv_nsec;
1589 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001590#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 ansec = st->st_atimespec.tv_nsec;
1592 mnsec = st->st_mtimespec.tv_nsec;
1593 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001594#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 ansec = st->st_atime_nsec;
1596 mnsec = st->st_mtime_nsec;
1597 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001598#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001600#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001601 fill_time(v, 7, st->st_atime, ansec);
1602 fill_time(v, 8, st->st_mtime, mnsec);
1603 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001604
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001605#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001606 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1607 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001608#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001609#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1611 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001612#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001613#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001614 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1615 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001616#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001617#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1619 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001620#endif
1621#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 {
1623 PyObject *val;
1624 unsigned long bsec,bnsec;
1625 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001626#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001628#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001630#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001631 if (_stat_float_times) {
1632 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1633 } else {
1634 val = PyLong_FromLong((long)bsec);
1635 }
1636 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1637 val);
1638 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001639#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001640#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001641 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1642 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001643#endif
Fred Drake699f3522000-06-29 21:12:41 +00001644
Victor Stinner8c62be82010-05-06 00:08:46 +00001645 if (PyErr_Occurred()) {
1646 Py_DECREF(v);
1647 return NULL;
1648 }
Fred Drake699f3522000-06-29 21:12:41 +00001649
Victor Stinner8c62be82010-05-06 00:08:46 +00001650 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001651}
1652
Barry Warsaw53699e91996-12-10 23:23:01 +00001653static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001654posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001656#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001657 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001658#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001660#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001661 char *wformat,
1662 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001663{
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 STRUCT_STAT st;
1665 PyObject *opath;
1666 char *path;
1667 int res;
1668 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001669
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001670#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001671 PyUnicodeObject *po;
1672 if (PyArg_ParseTuple(args, wformat, &po)) {
1673 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001674
Victor Stinner8c62be82010-05-06 00:08:46 +00001675 Py_BEGIN_ALLOW_THREADS
1676 /* PyUnicode_AS_UNICODE result OK without
1677 thread lock as it is a simple dereference. */
1678 res = wstatfunc(wpath, &st);
1679 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001680
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 if (res != 0)
1682 return win32_error_unicode("stat", wpath);
1683 return _pystat_fromstructstat(&st);
1684 }
1685 /* Drop the argument parsing error as narrow strings
1686 are also valid. */
1687 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001688#endif
1689
Victor Stinner8c62be82010-05-06 00:08:46 +00001690 if (!PyArg_ParseTuple(args, format,
1691 PyUnicode_FSConverter, &opath))
1692 return NULL;
1693 path = PyBytes_AsString(opath);
1694 Py_BEGIN_ALLOW_THREADS
1695 res = (*statfunc)(path, &st);
1696 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001697
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001699#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001701#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001703#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001704 }
1705 else
1706 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001707
Victor Stinner8c62be82010-05-06 00:08:46 +00001708 Py_DECREF(opath);
1709 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710}
1711
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001712/* POSIX methods */
1713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001714PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001715"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001716Use the real uid/gid to test for access to a path. Note that most\n\
1717operations will use the effective uid/gid, therefore this routine can\n\
1718be used in a suid/sgid environment to test if the invoking user has the\n\
1719specified access to the path. The mode argument can be F_OK to test\n\
1720existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001721
1722static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001723posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001724{
Victor Stinner8c62be82010-05-06 00:08:46 +00001725 PyObject *opath;
1726 char *path;
1727 int mode;
1728
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001729#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 DWORD attr;
1731 PyUnicodeObject *po;
1732 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1733 Py_BEGIN_ALLOW_THREADS
1734 /* PyUnicode_AS_UNICODE OK without thread lock as
1735 it is a simple dereference. */
1736 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1737 Py_END_ALLOW_THREADS
1738 goto finish;
1739 }
1740 /* Drop the argument parsing error as narrow strings
1741 are also valid. */
1742 PyErr_Clear();
1743 if (!PyArg_ParseTuple(args, "O&i:access",
1744 PyUnicode_FSConverter, &opath, &mode))
1745 return NULL;
1746 path = PyBytes_AsString(opath);
1747 Py_BEGIN_ALLOW_THREADS
1748 attr = GetFileAttributesA(path);
1749 Py_END_ALLOW_THREADS
1750 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001751finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001752 if (attr == 0xFFFFFFFF)
1753 /* File does not exist, or cannot read attributes */
1754 return PyBool_FromLong(0);
1755 /* Access is possible if either write access wasn't requested, or
1756 the file isn't read-only, or if it's a directory, as there are
1757 no read-only directories on Windows. */
1758 return PyBool_FromLong(!(mode & 2)
1759 || !(attr & FILE_ATTRIBUTE_READONLY)
1760 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001761#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 int res;
1763 if (!PyArg_ParseTuple(args, "O&i:access",
1764 PyUnicode_FSConverter, &opath, &mode))
1765 return NULL;
1766 path = PyBytes_AsString(opath);
1767 Py_BEGIN_ALLOW_THREADS
1768 res = access(path, mode);
1769 Py_END_ALLOW_THREADS
1770 Py_DECREF(opath);
1771 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001772#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001773}
1774
Guido van Rossumd371ff11999-01-25 16:12:23 +00001775#ifndef F_OK
1776#define F_OK 0
1777#endif
1778#ifndef R_OK
1779#define R_OK 4
1780#endif
1781#ifndef W_OK
1782#define W_OK 2
1783#endif
1784#ifndef X_OK
1785#define X_OK 1
1786#endif
1787
1788#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001790"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001791Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001792
1793static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001794posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001795{
Victor Stinner8c62be82010-05-06 00:08:46 +00001796 int id;
1797 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001798
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1800 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001801
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001802#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001803 /* file descriptor 0 only, the default input device (stdin) */
1804 if (id == 0) {
1805 ret = ttyname();
1806 }
1807 else {
1808 ret = NULL;
1809 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001810#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001811 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001812#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001813 if (ret == NULL)
1814 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001815 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001816}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001817#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001818
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001819#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001821"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001822Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001823
1824static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001825posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001826{
Victor Stinner8c62be82010-05-06 00:08:46 +00001827 char *ret;
1828 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001829
Greg Wardb48bc172000-03-01 21:51:56 +00001830#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001832#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001834#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 if (ret == NULL)
1836 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001837 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001838}
1839#endif
1840
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001842"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001843Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001844
Barry Warsaw53699e91996-12-10 23:23:01 +00001845static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001846posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001847{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001848#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001849 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001850#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001851 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001852#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001853 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001854#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001856#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001857}
1858
Fred Drake4d1e64b2002-04-15 19:40:07 +00001859#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001860PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001861"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001862Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001863opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001864
1865static PyObject *
1866posix_fchdir(PyObject *self, PyObject *fdobj)
1867{
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001869}
1870#endif /* HAVE_FCHDIR */
1871
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001872
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001874"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001875Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001876
Barry Warsaw53699e91996-12-10 23:23:01 +00001877static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001878posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001879{
Victor Stinner8c62be82010-05-06 00:08:46 +00001880 PyObject *opath = NULL;
1881 char *path = NULL;
1882 int i;
1883 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001884#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 DWORD attr;
1886 PyUnicodeObject *po;
1887 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1888 Py_BEGIN_ALLOW_THREADS
1889 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1890 if (attr != 0xFFFFFFFF) {
1891 if (i & _S_IWRITE)
1892 attr &= ~FILE_ATTRIBUTE_READONLY;
1893 else
1894 attr |= FILE_ATTRIBUTE_READONLY;
1895 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1896 }
1897 else
1898 res = 0;
1899 Py_END_ALLOW_THREADS
1900 if (!res)
1901 return win32_error_unicode("chmod",
1902 PyUnicode_AS_UNICODE(po));
1903 Py_INCREF(Py_None);
1904 return Py_None;
1905 }
1906 /* Drop the argument parsing error as narrow strings
1907 are also valid. */
1908 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001909
Victor Stinner8c62be82010-05-06 00:08:46 +00001910 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1911 &opath, &i))
1912 return NULL;
1913 path = PyBytes_AsString(opath);
1914 Py_BEGIN_ALLOW_THREADS
1915 attr = GetFileAttributesA(path);
1916 if (attr != 0xFFFFFFFF) {
1917 if (i & _S_IWRITE)
1918 attr &= ~FILE_ATTRIBUTE_READONLY;
1919 else
1920 attr |= FILE_ATTRIBUTE_READONLY;
1921 res = SetFileAttributesA(path, attr);
1922 }
1923 else
1924 res = 0;
1925 Py_END_ALLOW_THREADS
1926 if (!res) {
1927 win32_error("chmod", path);
1928 Py_DECREF(opath);
1929 return NULL;
1930 }
1931 Py_DECREF(opath);
1932 Py_INCREF(Py_None);
1933 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001934#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001935 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1936 &opath, &i))
1937 return NULL;
1938 path = PyBytes_AsString(opath);
1939 Py_BEGIN_ALLOW_THREADS
1940 res = chmod(path, i);
1941 Py_END_ALLOW_THREADS
1942 if (res < 0)
1943 return posix_error_with_allocated_filename(opath);
1944 Py_DECREF(opath);
1945 Py_INCREF(Py_None);
1946 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001947#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001948}
1949
Christian Heimes4e30a842007-11-30 22:12:06 +00001950#ifdef HAVE_FCHMOD
1951PyDoc_STRVAR(posix_fchmod__doc__,
1952"fchmod(fd, mode)\n\n\
1953Change the access permissions of the file given by file\n\
1954descriptor fd.");
1955
1956static PyObject *
1957posix_fchmod(PyObject *self, PyObject *args)
1958{
Victor Stinner8c62be82010-05-06 00:08:46 +00001959 int fd, mode, res;
1960 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1961 return NULL;
1962 Py_BEGIN_ALLOW_THREADS
1963 res = fchmod(fd, mode);
1964 Py_END_ALLOW_THREADS
1965 if (res < 0)
1966 return posix_error();
1967 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001968}
1969#endif /* HAVE_FCHMOD */
1970
1971#ifdef HAVE_LCHMOD
1972PyDoc_STRVAR(posix_lchmod__doc__,
1973"lchmod(path, mode)\n\n\
1974Change the access permissions of a file. If path is a symlink, this\n\
1975affects the link itself rather than the target.");
1976
1977static PyObject *
1978posix_lchmod(PyObject *self, PyObject *args)
1979{
Victor Stinner8c62be82010-05-06 00:08:46 +00001980 PyObject *opath;
1981 char *path;
1982 int i;
1983 int res;
1984 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1985 &opath, &i))
1986 return NULL;
1987 path = PyBytes_AsString(opath);
1988 Py_BEGIN_ALLOW_THREADS
1989 res = lchmod(path, i);
1990 Py_END_ALLOW_THREADS
1991 if (res < 0)
1992 return posix_error_with_allocated_filename(opath);
1993 Py_DECREF(opath);
1994 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001995}
1996#endif /* HAVE_LCHMOD */
1997
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001998
Thomas Wouterscf297e42007-02-23 15:07:44 +00001999#ifdef HAVE_CHFLAGS
2000PyDoc_STRVAR(posix_chflags__doc__,
2001"chflags(path, flags)\n\n\
2002Set file flags.");
2003
2004static PyObject *
2005posix_chflags(PyObject *self, PyObject *args)
2006{
Victor Stinner8c62be82010-05-06 00:08:46 +00002007 PyObject *opath;
2008 char *path;
2009 unsigned long flags;
2010 int res;
2011 if (!PyArg_ParseTuple(args, "O&k:chflags",
2012 PyUnicode_FSConverter, &opath, &flags))
2013 return NULL;
2014 path = PyBytes_AsString(opath);
2015 Py_BEGIN_ALLOW_THREADS
2016 res = chflags(path, flags);
2017 Py_END_ALLOW_THREADS
2018 if (res < 0)
2019 return posix_error_with_allocated_filename(opath);
2020 Py_DECREF(opath);
2021 Py_INCREF(Py_None);
2022 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002023}
2024#endif /* HAVE_CHFLAGS */
2025
2026#ifdef HAVE_LCHFLAGS
2027PyDoc_STRVAR(posix_lchflags__doc__,
2028"lchflags(path, flags)\n\n\
2029Set file flags.\n\
2030This function will not follow symbolic links.");
2031
2032static PyObject *
2033posix_lchflags(PyObject *self, PyObject *args)
2034{
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 PyObject *opath;
2036 char *path;
2037 unsigned long flags;
2038 int res;
2039 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2040 PyUnicode_FSConverter, &opath, &flags))
2041 return NULL;
2042 path = PyBytes_AsString(opath);
2043 Py_BEGIN_ALLOW_THREADS
2044 res = lchflags(path, flags);
2045 Py_END_ALLOW_THREADS
2046 if (res < 0)
2047 return posix_error_with_allocated_filename(opath);
2048 Py_DECREF(opath);
2049 Py_INCREF(Py_None);
2050 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002051}
2052#endif /* HAVE_LCHFLAGS */
2053
Martin v. Löwis244edc82001-10-04 22:44:26 +00002054#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002056"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002057Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002058
2059static PyObject *
2060posix_chroot(PyObject *self, PyObject *args)
2061{
Victor Stinner8c62be82010-05-06 00:08:46 +00002062 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002063}
2064#endif
2065
Guido van Rossum21142a01999-01-08 21:05:37 +00002066#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002068"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002069force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002070
2071static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002072posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002073{
Stefan Krah0e803b32010-11-26 16:16:47 +00002074 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002075}
2076#endif /* HAVE_FSYNC */
2077
2078#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002079
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002080#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002081extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2082#endif
2083
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002084PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002085"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002086force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002087 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002088
2089static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002090posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002091{
Stefan Krah0e803b32010-11-26 16:16:47 +00002092 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002093}
2094#endif /* HAVE_FDATASYNC */
2095
2096
Fredrik Lundh10723342000-07-10 16:38:09 +00002097#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002098PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002099"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002100Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002101
Barry Warsaw53699e91996-12-10 23:23:01 +00002102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002103posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002104{
Victor Stinner8c62be82010-05-06 00:08:46 +00002105 PyObject *opath;
2106 char *path;
2107 long uid, gid;
2108 int res;
2109 if (!PyArg_ParseTuple(args, "O&ll:chown",
2110 PyUnicode_FSConverter, &opath,
2111 &uid, &gid))
2112 return NULL;
2113 path = PyBytes_AsString(opath);
2114 Py_BEGIN_ALLOW_THREADS
2115 res = chown(path, (uid_t) uid, (gid_t) gid);
2116 Py_END_ALLOW_THREADS
2117 if (res < 0)
2118 return posix_error_with_allocated_filename(opath);
2119 Py_DECREF(opath);
2120 Py_INCREF(Py_None);
2121 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002123#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002124
Christian Heimes4e30a842007-11-30 22:12:06 +00002125#ifdef HAVE_FCHOWN
2126PyDoc_STRVAR(posix_fchown__doc__,
2127"fchown(fd, uid, gid)\n\n\
2128Change the owner and group id of the file given by file descriptor\n\
2129fd to the numeric uid and gid.");
2130
2131static PyObject *
2132posix_fchown(PyObject *self, PyObject *args)
2133{
Victor Stinner8c62be82010-05-06 00:08:46 +00002134 int fd;
2135 long uid, gid;
2136 int res;
2137 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2138 return NULL;
2139 Py_BEGIN_ALLOW_THREADS
2140 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2141 Py_END_ALLOW_THREADS
2142 if (res < 0)
2143 return posix_error();
2144 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002145}
2146#endif /* HAVE_FCHOWN */
2147
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002148#ifdef HAVE_LCHOWN
2149PyDoc_STRVAR(posix_lchown__doc__,
2150"lchown(path, uid, gid)\n\n\
2151Change the owner and group id of path to the numeric uid and gid.\n\
2152This function will not follow symbolic links.");
2153
2154static PyObject *
2155posix_lchown(PyObject *self, PyObject *args)
2156{
Victor Stinner8c62be82010-05-06 00:08:46 +00002157 PyObject *opath;
2158 char *path;
2159 long uid, gid;
2160 int res;
2161 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2162 PyUnicode_FSConverter, &opath,
2163 &uid, &gid))
2164 return NULL;
2165 path = PyBytes_AsString(opath);
2166 Py_BEGIN_ALLOW_THREADS
2167 res = lchown(path, (uid_t) uid, (gid_t) gid);
2168 Py_END_ALLOW_THREADS
2169 if (res < 0)
2170 return posix_error_with_allocated_filename(opath);
2171 Py_DECREF(opath);
2172 Py_INCREF(Py_None);
2173 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002174}
2175#endif /* HAVE_LCHOWN */
2176
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002177
Guido van Rossum36bc6801995-06-14 22:54:23 +00002178#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002179static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002180posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002181{
Victor Stinner8c62be82010-05-06 00:08:46 +00002182 char buf[1026];
2183 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002184
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002185#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002186 if (!use_bytes) {
2187 wchar_t wbuf[1026];
2188 wchar_t *wbuf2 = wbuf;
2189 PyObject *resobj;
2190 DWORD len;
2191 Py_BEGIN_ALLOW_THREADS
2192 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2193 /* If the buffer is large enough, len does not include the
2194 terminating \0. If the buffer is too small, len includes
2195 the space needed for the terminator. */
2196 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2197 wbuf2 = malloc(len * sizeof(wchar_t));
2198 if (wbuf2)
2199 len = GetCurrentDirectoryW(len, wbuf2);
2200 }
2201 Py_END_ALLOW_THREADS
2202 if (!wbuf2) {
2203 PyErr_NoMemory();
2204 return NULL;
2205 }
2206 if (!len) {
2207 if (wbuf2 != wbuf) free(wbuf2);
2208 return win32_error("getcwdu", NULL);
2209 }
2210 resobj = PyUnicode_FromWideChar(wbuf2, len);
2211 if (wbuf2 != wbuf) free(wbuf2);
2212 return resobj;
2213 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002214#endif
2215
Victor Stinner8c62be82010-05-06 00:08:46 +00002216 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002217#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002218 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002219#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002220 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002221#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002222 Py_END_ALLOW_THREADS
2223 if (res == NULL)
2224 return posix_error();
2225 if (use_bytes)
2226 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002227 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002228}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002229
2230PyDoc_STRVAR(posix_getcwd__doc__,
2231"getcwd() -> path\n\n\
2232Return a unicode string representing the current working directory.");
2233
2234static PyObject *
2235posix_getcwd_unicode(PyObject *self)
2236{
2237 return posix_getcwd(0);
2238}
2239
2240PyDoc_STRVAR(posix_getcwdb__doc__,
2241"getcwdb() -> path\n\n\
2242Return a bytes string representing the current working directory.");
2243
2244static PyObject *
2245posix_getcwd_bytes(PyObject *self)
2246{
2247 return posix_getcwd(1);
2248}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002249#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002250
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002251
Guido van Rossumb6775db1994-08-01 11:34:53 +00002252#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002254"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002255Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002256
Barry Warsaw53699e91996-12-10 23:23:01 +00002257static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002258posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002259{
Victor Stinner8c62be82010-05-06 00:08:46 +00002260 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002262#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002263
Brian Curtin1b9df392010-11-24 20:24:31 +00002264#ifdef MS_WINDOWS
2265PyDoc_STRVAR(win32_link__doc__,
2266"link(src, dst)\n\n\
2267Create a hard link to a file.");
2268
2269static PyObject *
2270win32_link(PyObject *self, PyObject *args)
2271{
2272 PyObject *osrc, *odst;
2273 char *src, *dst;
2274 BOOL rslt;
2275
Brian Curtinfc889c42010-11-28 23:59:46 +00002276 PyUnicodeObject *usrc, *udst;
2277 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2278 Py_BEGIN_ALLOW_THREADS
2279 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2280 PyUnicode_AS_UNICODE(usrc), NULL);
2281 Py_END_ALLOW_THREADS
2282
2283 if (rslt == 0)
2284 return win32_error("link", NULL);
2285
2286 Py_RETURN_NONE;
2287 }
2288
2289 /* Narrow strings also valid. */
2290 PyErr_Clear();
2291
Brian Curtin1b9df392010-11-24 20:24:31 +00002292 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2293 PyUnicode_FSConverter, &odst))
2294 return NULL;
2295
2296 src = PyBytes_AsString(osrc);
2297 dst = PyBytes_AsString(odst);
2298
2299 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002300 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002301 Py_END_ALLOW_THREADS
2302
Stefan Krah30b341f2010-11-27 11:44:18 +00002303 Py_DECREF(osrc);
2304 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002305 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002306 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002307
2308 Py_RETURN_NONE;
2309}
2310#endif /* MS_WINDOWS */
2311
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002313PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002314"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002315Return a list containing the names of the entries in the directory.\n\
2316\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002317 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002318\n\
2319The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002320entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002321
Barry Warsaw53699e91996-12-10 23:23:01 +00002322static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002323posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002324{
Victor Stinner8c62be82010-05-06 00:08:46 +00002325 /* XXX Should redo this putting the (now four) versions of opendir
2326 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002327#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002328
Victor Stinner8c62be82010-05-06 00:08:46 +00002329 PyObject *d, *v;
2330 HANDLE hFindFile;
2331 BOOL result;
2332 WIN32_FIND_DATA FileData;
2333 PyObject *opath;
2334 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2335 char *bufptr = namebuf;
2336 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002337
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002338 PyObject *po = NULL;
2339 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002340 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002341 Py_UNICODE *wnamebuf, *po_wchars;
2342
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002343 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002344 po_wchars = L".";
2345 len = 1;
2346 } else {
2347 po_wchars = PyUnicode_AS_UNICODE(po);
2348 len = PyUnicode_GET_SIZE(po);
2349 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002350 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002351 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2352 if (!wnamebuf) {
2353 PyErr_NoMemory();
2354 return NULL;
2355 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002356 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002357 if (len > 0) {
2358 Py_UNICODE wch = wnamebuf[len-1];
2359 if (wch != L'/' && wch != L'\\' && wch != L':')
2360 wnamebuf[len++] = L'\\';
2361 wcscpy(wnamebuf + len, L"*.*");
2362 }
2363 if ((d = PyList_New(0)) == NULL) {
2364 free(wnamebuf);
2365 return NULL;
2366 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002367 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002369 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002370 if (hFindFile == INVALID_HANDLE_VALUE) {
2371 int error = GetLastError();
2372 if (error == ERROR_FILE_NOT_FOUND) {
2373 free(wnamebuf);
2374 return d;
2375 }
2376 Py_DECREF(d);
2377 win32_error_unicode("FindFirstFileW", wnamebuf);
2378 free(wnamebuf);
2379 return NULL;
2380 }
2381 do {
2382 /* Skip over . and .. */
2383 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2384 wcscmp(wFileData.cFileName, L"..") != 0) {
2385 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2386 if (v == NULL) {
2387 Py_DECREF(d);
2388 d = NULL;
2389 break;
2390 }
2391 if (PyList_Append(d, v) != 0) {
2392 Py_DECREF(v);
2393 Py_DECREF(d);
2394 d = NULL;
2395 break;
2396 }
2397 Py_DECREF(v);
2398 }
2399 Py_BEGIN_ALLOW_THREADS
2400 result = FindNextFileW(hFindFile, &wFileData);
2401 Py_END_ALLOW_THREADS
2402 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2403 it got to the end of the directory. */
2404 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2405 Py_DECREF(d);
2406 win32_error_unicode("FindNextFileW", wnamebuf);
2407 FindClose(hFindFile);
2408 free(wnamebuf);
2409 return NULL;
2410 }
2411 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002412
Victor Stinner8c62be82010-05-06 00:08:46 +00002413 if (FindClose(hFindFile) == FALSE) {
2414 Py_DECREF(d);
2415 win32_error_unicode("FindClose", wnamebuf);
2416 free(wnamebuf);
2417 return NULL;
2418 }
2419 free(wnamebuf);
2420 return d;
2421 }
2422 /* Drop the argument parsing error as narrow strings
2423 are also valid. */
2424 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002425
Victor Stinner8c62be82010-05-06 00:08:46 +00002426 if (!PyArg_ParseTuple(args, "O&:listdir",
2427 PyUnicode_FSConverter, &opath))
2428 return NULL;
2429 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2430 PyErr_SetString(PyExc_ValueError, "path too long");
2431 Py_DECREF(opath);
2432 return NULL;
2433 }
2434 strcpy(namebuf, PyBytes_AsString(opath));
2435 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002436 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002437 if (len > 0) {
2438 char ch = namebuf[len-1];
2439 if (ch != SEP && ch != ALTSEP && ch != ':')
2440 namebuf[len++] = '/';
2441 strcpy(namebuf + len, "*.*");
2442 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002443
Victor Stinner8c62be82010-05-06 00:08:46 +00002444 if ((d = PyList_New(0)) == NULL)
2445 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002446
Antoine Pitroub73caab2010-08-09 23:39:31 +00002447 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002448 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002449 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002450 if (hFindFile == INVALID_HANDLE_VALUE) {
2451 int error = GetLastError();
2452 if (error == ERROR_FILE_NOT_FOUND)
2453 return d;
2454 Py_DECREF(d);
2455 return win32_error("FindFirstFile", namebuf);
2456 }
2457 do {
2458 /* Skip over . and .. */
2459 if (strcmp(FileData.cFileName, ".") != 0 &&
2460 strcmp(FileData.cFileName, "..") != 0) {
2461 v = PyBytes_FromString(FileData.cFileName);
2462 if (v == NULL) {
2463 Py_DECREF(d);
2464 d = NULL;
2465 break;
2466 }
2467 if (PyList_Append(d, v) != 0) {
2468 Py_DECREF(v);
2469 Py_DECREF(d);
2470 d = NULL;
2471 break;
2472 }
2473 Py_DECREF(v);
2474 }
2475 Py_BEGIN_ALLOW_THREADS
2476 result = FindNextFile(hFindFile, &FileData);
2477 Py_END_ALLOW_THREADS
2478 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2479 it got to the end of the directory. */
2480 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2481 Py_DECREF(d);
2482 win32_error("FindNextFile", namebuf);
2483 FindClose(hFindFile);
2484 return NULL;
2485 }
2486 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002487
Victor Stinner8c62be82010-05-06 00:08:46 +00002488 if (FindClose(hFindFile) == FALSE) {
2489 Py_DECREF(d);
2490 return win32_error("FindClose", namebuf);
2491 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002492
Victor Stinner8c62be82010-05-06 00:08:46 +00002493 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002494
Tim Peters0bb44a42000-09-15 07:44:49 +00002495#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002496
2497#ifndef MAX_PATH
2498#define MAX_PATH CCHMAXPATH
2499#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002500 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002501 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002502 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002503 PyObject *d, *v;
2504 char namebuf[MAX_PATH+5];
2505 HDIR hdir = 1;
2506 ULONG srchcnt = 1;
2507 FILEFINDBUF3 ep;
2508 APIRET rc;
2509
Victor Stinner8c62be82010-05-06 00:08:46 +00002510 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002511 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002512 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002513 name = PyBytes_AsString(oname);
2514 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002515 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002516 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002517 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002518 return NULL;
2519 }
2520 strcpy(namebuf, name);
2521 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002522 if (*pt == ALTSEP)
2523 *pt = SEP;
2524 if (namebuf[len-1] != SEP)
2525 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002526 strcpy(namebuf + len, "*.*");
2527
Neal Norwitz6c913782007-10-14 03:23:09 +00002528 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002529 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002530 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002531 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002532
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002533 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2534 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002535 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002536 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2537 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2538 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002539
2540 if (rc != NO_ERROR) {
2541 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002542 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002543 }
2544
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002545 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002546 do {
2547 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002548 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002549 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002550
2551 strcpy(namebuf, ep.achName);
2552
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002553 /* Leave Case of Name Alone -- In Native Form */
2554 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002555
Christian Heimes72b710a2008-05-26 13:28:38 +00002556 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002557 if (v == NULL) {
2558 Py_DECREF(d);
2559 d = NULL;
2560 break;
2561 }
2562 if (PyList_Append(d, v) != 0) {
2563 Py_DECREF(v);
2564 Py_DECREF(d);
2565 d = NULL;
2566 break;
2567 }
2568 Py_DECREF(v);
2569 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2570 }
2571
Victor Stinnerdcb24032010-04-22 12:08:36 +00002572 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002573 return d;
2574#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002575 PyObject *oname;
2576 char *name;
2577 PyObject *d, *v;
2578 DIR *dirp;
2579 struct dirent *ep;
2580 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002581
Victor Stinner8c62be82010-05-06 00:08:46 +00002582 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002583 /* v is never read, so it does not need to be initialized yet. */
2584 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002585 arg_is_unicode = 0;
2586 PyErr_Clear();
2587 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002588 oname = NULL;
2589 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002590 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002591 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002592 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002593 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002595 Py_BEGIN_ALLOW_THREADS
2596 dirp = opendir(name);
2597 Py_END_ALLOW_THREADS
2598 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002599 return posix_error_with_allocated_filename(oname);
2600 }
2601 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002602 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002604 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002605 Py_DECREF(oname);
2606 return NULL;
2607 }
2608 for (;;) {
2609 errno = 0;
2610 Py_BEGIN_ALLOW_THREADS
2611 ep = readdir(dirp);
2612 Py_END_ALLOW_THREADS
2613 if (ep == NULL) {
2614 if (errno == 0) {
2615 break;
2616 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002617 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002618 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002619 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002620 Py_DECREF(d);
2621 return posix_error_with_allocated_filename(oname);
2622 }
2623 }
2624 if (ep->d_name[0] == '.' &&
2625 (NAMLEN(ep) == 1 ||
2626 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2627 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002628 if (arg_is_unicode)
2629 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2630 else
2631 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002633 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 break;
2635 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002636 if (PyList_Append(d, v) != 0) {
2637 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002638 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002639 break;
2640 }
2641 Py_DECREF(v);
2642 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002643 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002645 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002646 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002647
Victor Stinner8c62be82010-05-06 00:08:46 +00002648 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002649
Tim Peters0bb44a42000-09-15 07:44:49 +00002650#endif /* which OS */
2651} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002652
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002653#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002654/* A helper function for abspath on win32 */
2655static PyObject *
2656posix__getfullpathname(PyObject *self, PyObject *args)
2657{
Victor Stinner8c62be82010-05-06 00:08:46 +00002658 PyObject *opath;
2659 char *path;
2660 char outbuf[MAX_PATH*2];
2661 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002662#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002663 PyUnicodeObject *po;
2664 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2665 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2666 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2667 Py_UNICODE *wtemp;
2668 DWORD result;
2669 PyObject *v;
2670 result = GetFullPathNameW(wpath,
2671 sizeof(woutbuf)/sizeof(woutbuf[0]),
2672 woutbuf, &wtemp);
2673 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2674 woutbufp = malloc(result * sizeof(Py_UNICODE));
2675 if (!woutbufp)
2676 return PyErr_NoMemory();
2677 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2678 }
2679 if (result)
2680 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2681 else
2682 v = win32_error_unicode("GetFullPathNameW", wpath);
2683 if (woutbufp != woutbuf)
2684 free(woutbufp);
2685 return v;
2686 }
2687 /* Drop the argument parsing error as narrow strings
2688 are also valid. */
2689 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002690
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002691#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002692 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2693 PyUnicode_FSConverter, &opath))
2694 return NULL;
2695 path = PyBytes_AsString(opath);
2696 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2697 outbuf, &temp)) {
2698 win32_error("GetFullPathName", path);
2699 Py_DECREF(opath);
2700 return NULL;
2701 }
2702 Py_DECREF(opath);
2703 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2704 return PyUnicode_Decode(outbuf, strlen(outbuf),
2705 Py_FileSystemDefaultEncoding, NULL);
2706 }
2707 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002708} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002709
Brian Curtinf5e76d02010-11-24 13:14:05 +00002710/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2711static int has_GetFinalPathNameByHandle = 0;
2712static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2713 DWORD);
2714static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2715 DWORD);
2716static int
2717check_GetFinalPathNameByHandle()
2718{
2719 HINSTANCE hKernel32;
2720 /* only recheck */
2721 if (!has_GetFinalPathNameByHandle)
2722 {
2723 hKernel32 = GetModuleHandle("KERNEL32");
2724 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2725 "GetFinalPathNameByHandleA");
2726 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2727 "GetFinalPathNameByHandleW");
2728 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2729 Py_GetFinalPathNameByHandleW;
2730 }
2731 return has_GetFinalPathNameByHandle;
2732}
2733
Brian Curtind40e6f72010-07-08 21:39:08 +00002734/* A helper function for samepath on windows */
2735static PyObject *
2736posix__getfinalpathname(PyObject *self, PyObject *args)
2737{
2738 HANDLE hFile;
2739 int buf_size;
2740 wchar_t *target_path;
2741 int result_length;
2742 PyObject *result;
2743 wchar_t *path;
2744
Brian Curtin94622b02010-09-24 00:03:39 +00002745 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002746 return NULL;
2747 }
2748
2749 if(!check_GetFinalPathNameByHandle()) {
2750 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2751 NotImplementedError. */
2752 return PyErr_Format(PyExc_NotImplementedError,
2753 "GetFinalPathNameByHandle not available on this platform");
2754 }
2755
2756 hFile = CreateFileW(
2757 path,
2758 0, /* desired access */
2759 0, /* share mode */
2760 NULL, /* security attributes */
2761 OPEN_EXISTING,
2762 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2763 FILE_FLAG_BACKUP_SEMANTICS,
2764 NULL);
2765
2766 if(hFile == INVALID_HANDLE_VALUE) {
2767 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002768 return PyErr_Format(PyExc_RuntimeError,
2769 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002770 }
2771
2772 /* We have a good handle to the target, use it to determine the
2773 target path name. */
2774 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2775
2776 if(!buf_size)
2777 return win32_error_unicode("GetFinalPathNameByHandle", path);
2778
2779 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2780 if(!target_path)
2781 return PyErr_NoMemory();
2782
2783 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2784 buf_size, VOLUME_NAME_DOS);
2785 if(!result_length)
2786 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2787
2788 if(!CloseHandle(hFile))
2789 return win32_error_unicode("GetFinalPathNameByHandle", path);
2790
2791 target_path[result_length] = 0;
2792 result = PyUnicode_FromUnicode(target_path, result_length);
2793 free(target_path);
2794 return result;
2795
2796} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002797
2798static PyObject *
2799posix__getfileinformation(PyObject *self, PyObject *args)
2800{
2801 HANDLE hFile;
2802 BY_HANDLE_FILE_INFORMATION info;
2803 int fd;
2804
2805 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2806 return NULL;
2807
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002808 if (!_PyVerify_fd(fd))
2809 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002810
2811 hFile = (HANDLE)_get_osfhandle(fd);
2812 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002813 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002814
2815 if (!GetFileInformationByHandle(hFile, &info))
2816 return win32_error("_getfileinformation", NULL);
2817
2818 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2819 info.nFileIndexHigh,
2820 info.nFileIndexLow);
2821}
Brian Curtin9c669cc2011-06-08 18:17:18 -05002822
2823static PyObject *
2824posix__isdir(PyObject *self, PyObject *args)
2825{
2826 PyObject *opath;
2827 char *path;
2828 PyUnicodeObject *po;
2829 DWORD attributes;
2830
2831 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
2832 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2833
2834 attributes = GetFileAttributesW(wpath);
2835 if (attributes == INVALID_FILE_ATTRIBUTES)
2836 Py_RETURN_FALSE;
2837 goto check;
2838 }
2839 /* Drop the argument parsing error as narrow strings
2840 are also valid. */
2841 PyErr_Clear();
2842
2843 if (!PyArg_ParseTuple(args, "O&:_isdir",
2844 PyUnicode_FSConverter, &opath))
2845 return NULL;
2846
2847 path = PyBytes_AsString(opath);
2848 attributes = GetFileAttributesA(path);
2849 if (attributes == INVALID_FILE_ATTRIBUTES)
2850 Py_RETURN_FALSE;
2851
2852check:
2853 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
2854 Py_RETURN_TRUE;
2855 else
2856 Py_RETURN_FALSE;
2857}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002858#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002859
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002860PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002861"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002862Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002863
Barry Warsaw53699e91996-12-10 23:23:01 +00002864static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002865posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002866{
Victor Stinner8c62be82010-05-06 00:08:46 +00002867 int res;
2868 PyObject *opath;
2869 char *path;
2870 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002871
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002872#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002873 PyUnicodeObject *po;
2874 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2875 Py_BEGIN_ALLOW_THREADS
2876 /* PyUnicode_AS_UNICODE OK without thread lock as
2877 it is a simple dereference. */
2878 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2879 Py_END_ALLOW_THREADS
2880 if (!res)
2881 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2882 Py_INCREF(Py_None);
2883 return Py_None;
2884 }
2885 /* Drop the argument parsing error as narrow strings
2886 are also valid. */
2887 PyErr_Clear();
2888 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2889 PyUnicode_FSConverter, &opath, &mode))
2890 return NULL;
2891 path = PyBytes_AsString(opath);
2892 Py_BEGIN_ALLOW_THREADS
2893 /* PyUnicode_AS_UNICODE OK without thread lock as
2894 it is a simple dereference. */
2895 res = CreateDirectoryA(path, NULL);
2896 Py_END_ALLOW_THREADS
2897 if (!res) {
2898 win32_error("mkdir", path);
2899 Py_DECREF(opath);
2900 return NULL;
2901 }
2902 Py_DECREF(opath);
2903 Py_INCREF(Py_None);
2904 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002905#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002906
Victor Stinner8c62be82010-05-06 00:08:46 +00002907 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2908 PyUnicode_FSConverter, &opath, &mode))
2909 return NULL;
2910 path = PyBytes_AsString(opath);
2911 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002912#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002913 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002914#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002915 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002916#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002917 Py_END_ALLOW_THREADS
2918 if (res < 0)
2919 return posix_error_with_allocated_filename(opath);
2920 Py_DECREF(opath);
2921 Py_INCREF(Py_None);
2922 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002923#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002924}
2925
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002926
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002927/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2928#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002929#include <sys/resource.h>
2930#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002931
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002932
2933#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002934PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002935"nice(inc) -> new_priority\n\n\
2936Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002937
Barry Warsaw53699e91996-12-10 23:23:01 +00002938static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002939posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002940{
Victor Stinner8c62be82010-05-06 00:08:46 +00002941 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002942
Victor Stinner8c62be82010-05-06 00:08:46 +00002943 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2944 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002945
Victor Stinner8c62be82010-05-06 00:08:46 +00002946 /* There are two flavours of 'nice': one that returns the new
2947 priority (as required by almost all standards out there) and the
2948 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2949 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002950
Victor Stinner8c62be82010-05-06 00:08:46 +00002951 If we are of the nice family that returns the new priority, we
2952 need to clear errno before the call, and check if errno is filled
2953 before calling posix_error() on a returnvalue of -1, because the
2954 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002955
Victor Stinner8c62be82010-05-06 00:08:46 +00002956 errno = 0;
2957 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002958#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002959 if (value == 0)
2960 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002961#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002962 if (value == -1 && errno != 0)
2963 /* either nice() or getpriority() returned an error */
2964 return posix_error();
2965 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002966}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002967#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002969PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002970"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002971Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002972
Barry Warsaw53699e91996-12-10 23:23:01 +00002973static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002974posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002975{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002976#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002977 PyObject *o1, *o2;
2978 char *p1, *p2;
2979 BOOL result;
2980 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2981 goto error;
2982 if (!convert_to_unicode(&o1))
2983 goto error;
2984 if (!convert_to_unicode(&o2)) {
2985 Py_DECREF(o1);
2986 goto error;
2987 }
2988 Py_BEGIN_ALLOW_THREADS
2989 result = MoveFileW(PyUnicode_AsUnicode(o1),
2990 PyUnicode_AsUnicode(o2));
2991 Py_END_ALLOW_THREADS
2992 Py_DECREF(o1);
2993 Py_DECREF(o2);
2994 if (!result)
2995 return win32_error("rename", NULL);
2996 Py_INCREF(Py_None);
2997 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002998error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002999 PyErr_Clear();
3000 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3001 return NULL;
3002 Py_BEGIN_ALLOW_THREADS
3003 result = MoveFileA(p1, p2);
3004 Py_END_ALLOW_THREADS
3005 if (!result)
3006 return win32_error("rename", NULL);
3007 Py_INCREF(Py_None);
3008 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003009#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003010 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003011#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003012}
3013
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003015PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003016"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003018
Barry Warsaw53699e91996-12-10 23:23:01 +00003019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003020posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003021{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003022#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003023 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003024#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003025 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003026#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003027}
3028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003030PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003031"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003032Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003033
Barry Warsaw53699e91996-12-10 23:23:01 +00003034static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003035posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003036{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003037#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003038 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003039#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003040 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003041#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003042}
3043
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003044
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003045#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003046PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003047"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003048Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003049
Barry Warsaw53699e91996-12-10 23:23:01 +00003050static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003051posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003052{
Victor Stinner8c62be82010-05-06 00:08:46 +00003053 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003054#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003055 wchar_t *command;
3056 if (!PyArg_ParseTuple(args, "u:system", &command))
3057 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003058
Victor Stinner8c62be82010-05-06 00:08:46 +00003059 Py_BEGIN_ALLOW_THREADS
3060 sts = _wsystem(command);
3061 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003062#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003063 PyObject *command_obj;
3064 char *command;
3065 if (!PyArg_ParseTuple(args, "O&:system",
3066 PyUnicode_FSConverter, &command_obj))
3067 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003068
Victor Stinner8c62be82010-05-06 00:08:46 +00003069 command = PyBytes_AsString(command_obj);
3070 Py_BEGIN_ALLOW_THREADS
3071 sts = system(command);
3072 Py_END_ALLOW_THREADS
3073 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003074#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003075 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003076}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003077#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003079
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003080PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003081"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003082Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003083
Barry Warsaw53699e91996-12-10 23:23:01 +00003084static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003085posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003086{
Victor Stinner8c62be82010-05-06 00:08:46 +00003087 int i;
3088 if (!PyArg_ParseTuple(args, "i:umask", &i))
3089 return NULL;
3090 i = (int)umask(i);
3091 if (i < 0)
3092 return posix_error();
3093 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003094}
3095
Brian Curtind40e6f72010-07-08 21:39:08 +00003096#ifdef MS_WINDOWS
3097
3098/* override the default DeleteFileW behavior so that directory
3099symlinks can be removed with this function, the same as with
3100Unix symlinks */
3101BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3102{
3103 WIN32_FILE_ATTRIBUTE_DATA info;
3104 WIN32_FIND_DATAW find_data;
3105 HANDLE find_data_handle;
3106 int is_directory = 0;
3107 int is_link = 0;
3108
3109 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3110 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3111
3112 /* Get WIN32_FIND_DATA structure for the path to determine if
3113 it is a symlink */
3114 if(is_directory &&
3115 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3116 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3117
3118 if(find_data_handle != INVALID_HANDLE_VALUE) {
3119 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3120 FindClose(find_data_handle);
3121 }
3122 }
3123 }
3124
3125 if (is_directory && is_link)
3126 return RemoveDirectoryW(lpFileName);
3127
3128 return DeleteFileW(lpFileName);
3129}
3130#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003132PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003133"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003134Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003136PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003137"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003138Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139
Barry Warsaw53699e91996-12-10 23:23:01 +00003140static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003141posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003142{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003143#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003144 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3145 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003146#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003148#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003149}
3150
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003151
Guido van Rossumb6775db1994-08-01 11:34:53 +00003152#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003153PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003154"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003155Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003156
Barry Warsaw53699e91996-12-10 23:23:01 +00003157static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003158posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003159{
Victor Stinner8c62be82010-05-06 00:08:46 +00003160 struct utsname u;
3161 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003162
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 Py_BEGIN_ALLOW_THREADS
3164 res = uname(&u);
3165 Py_END_ALLOW_THREADS
3166 if (res < 0)
3167 return posix_error();
3168 return Py_BuildValue("(sssss)",
3169 u.sysname,
3170 u.nodename,
3171 u.release,
3172 u.version,
3173 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003174}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003175#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003176
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003177static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003178extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003179{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003180 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003181 if (PyFloat_Check(t)) {
3182 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003183 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003184 if (!intobj)
3185 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003186#if SIZEOF_TIME_T > SIZEOF_LONG
3187 intval = PyLong_AsUnsignedLongLongMask(intobj);
3188#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003190#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003191 Py_DECREF(intobj);
3192 if (intval == -1 && PyErr_Occurred())
3193 return -1;
3194 *sec = intval;
3195 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3196 if (*usec < 0)
3197 /* If rounding gave us a negative number,
3198 truncate. */
3199 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003200 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003201 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003202#if SIZEOF_TIME_T > SIZEOF_LONG
3203 intval = PyLong_AsUnsignedLongLongMask(t);
3204#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003205 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003206#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003207 if (intval == -1 && PyErr_Occurred())
3208 return -1;
3209 *sec = intval;
3210 *usec = 0;
3211 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003212}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003214PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003215"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003216utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003217Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003218second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219
Barry Warsaw53699e91996-12-10 23:23:01 +00003220static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003221posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003222{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003223#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003224 PyObject *arg;
3225 PyUnicodeObject *obwpath;
3226 wchar_t *wpath = NULL;
3227 PyObject *oapath;
3228 char *apath;
3229 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003230 time_t atimesec, mtimesec;
3231 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003232 FILETIME atime, mtime;
3233 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003234
Victor Stinner8c62be82010-05-06 00:08:46 +00003235 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3236 wpath = PyUnicode_AS_UNICODE(obwpath);
3237 Py_BEGIN_ALLOW_THREADS
3238 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3239 NULL, OPEN_EXISTING,
3240 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3241 Py_END_ALLOW_THREADS
3242 if (hFile == INVALID_HANDLE_VALUE)
3243 return win32_error_unicode("utime", wpath);
3244 } else
3245 /* Drop the argument parsing error as narrow strings
3246 are also valid. */
3247 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003248
Victor Stinner8c62be82010-05-06 00:08:46 +00003249 if (!wpath) {
3250 if (!PyArg_ParseTuple(args, "O&O:utime",
3251 PyUnicode_FSConverter, &oapath, &arg))
3252 return NULL;
3253 apath = PyBytes_AsString(oapath);
3254 Py_BEGIN_ALLOW_THREADS
3255 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3256 NULL, OPEN_EXISTING,
3257 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3258 Py_END_ALLOW_THREADS
3259 if (hFile == INVALID_HANDLE_VALUE) {
3260 win32_error("utime", apath);
3261 Py_DECREF(oapath);
3262 return NULL;
3263 }
3264 Py_DECREF(oapath);
3265 }
3266
3267 if (arg == Py_None) {
3268 SYSTEMTIME now;
3269 GetSystemTime(&now);
3270 if (!SystemTimeToFileTime(&now, &mtime) ||
3271 !SystemTimeToFileTime(&now, &atime)) {
3272 win32_error("utime", NULL);
3273 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003274 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003275 }
3276 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3277 PyErr_SetString(PyExc_TypeError,
3278 "utime() arg 2 must be a tuple (atime, mtime)");
3279 goto done;
3280 }
3281 else {
3282 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3283 &atimesec, &ausec) == -1)
3284 goto done;
3285 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3286 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3287 &mtimesec, &musec) == -1)
3288 goto done;
3289 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3290 }
3291 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3292 /* Avoid putting the file name into the error here,
3293 as that may confuse the user into believing that
3294 something is wrong with the file, when it also
3295 could be the time stamp that gives a problem. */
3296 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003297 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 }
3299 Py_INCREF(Py_None);
3300 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003301done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003302 CloseHandle(hFile);
3303 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003304#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003305
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 PyObject *opath;
3307 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003308 time_t atime, mtime;
3309 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 int res;
3311 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003312
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003313#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003315#define ATIME buf[0].tv_sec
3316#define MTIME buf[1].tv_sec
3317#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003318/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003319 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003320#define ATIME buf.actime
3321#define MTIME buf.modtime
3322#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003323#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003325#define ATIME buf[0]
3326#define MTIME buf[1]
3327#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003328#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003329
Mark Hammond817c9292003-12-03 01:22:38 +00003330
Victor Stinner8c62be82010-05-06 00:08:46 +00003331 if (!PyArg_ParseTuple(args, "O&O:utime",
3332 PyUnicode_FSConverter, &opath, &arg))
3333 return NULL;
3334 path = PyBytes_AsString(opath);
3335 if (arg == Py_None) {
3336 /* optional time values not given */
3337 Py_BEGIN_ALLOW_THREADS
3338 res = utime(path, NULL);
3339 Py_END_ALLOW_THREADS
3340 }
3341 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3342 PyErr_SetString(PyExc_TypeError,
3343 "utime() arg 2 must be a tuple (atime, mtime)");
3344 Py_DECREF(opath);
3345 return NULL;
3346 }
3347 else {
3348 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3349 &atime, &ausec) == -1) {
3350 Py_DECREF(opath);
3351 return NULL;
3352 }
3353 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3354 &mtime, &musec) == -1) {
3355 Py_DECREF(opath);
3356 return NULL;
3357 }
3358 ATIME = atime;
3359 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003360#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003361 buf[0].tv_usec = ausec;
3362 buf[1].tv_usec = musec;
3363 Py_BEGIN_ALLOW_THREADS
3364 res = utimes(path, buf);
3365 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003366#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003367 Py_BEGIN_ALLOW_THREADS
3368 res = utime(path, UTIME_ARG);
3369 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003370#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003371 }
3372 if (res < 0) {
3373 return posix_error_with_allocated_filename(opath);
3374 }
3375 Py_DECREF(opath);
3376 Py_INCREF(Py_None);
3377 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003378#undef UTIME_ARG
3379#undef ATIME
3380#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003381#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003382}
3383
Guido van Rossum85e3b011991-06-03 12:42:10 +00003384
Guido van Rossum3b066191991-06-04 19:40:25 +00003385/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003387PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003388"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003389Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003390
Barry Warsaw53699e91996-12-10 23:23:01 +00003391static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003392posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003393{
Victor Stinner8c62be82010-05-06 00:08:46 +00003394 int sts;
3395 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3396 return NULL;
3397 _exit(sts);
3398 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003399}
3400
Martin v. Löwis114619e2002-10-07 06:44:21 +00003401#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3402static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003403free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003404{
Victor Stinner8c62be82010-05-06 00:08:46 +00003405 Py_ssize_t i;
3406 for (i = 0; i < count; i++)
3407 PyMem_Free(array[i]);
3408 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003409}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003410
Antoine Pitrou69f71142009-05-24 21:25:49 +00003411static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003412int fsconvert_strdup(PyObject *o, char**out)
3413{
Victor Stinner8c62be82010-05-06 00:08:46 +00003414 PyObject *bytes;
3415 Py_ssize_t size;
3416 if (!PyUnicode_FSConverter(o, &bytes))
3417 return 0;
3418 size = PyBytes_GET_SIZE(bytes);
3419 *out = PyMem_Malloc(size+1);
3420 if (!*out)
3421 return 0;
3422 memcpy(*out, PyBytes_AsString(bytes), size+1);
3423 Py_DECREF(bytes);
3424 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003425}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003426#endif
3427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003428
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003429#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003430PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003431"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003432Execute an executable path with arguments, replacing current process.\n\
3433\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003434 path: path of executable file\n\
3435 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003436
Barry Warsaw53699e91996-12-10 23:23:01 +00003437static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003438posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003439{
Victor Stinner8c62be82010-05-06 00:08:46 +00003440 PyObject *opath;
3441 char *path;
3442 PyObject *argv;
3443 char **argvlist;
3444 Py_ssize_t i, argc;
3445 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003446
Victor Stinner8c62be82010-05-06 00:08:46 +00003447 /* execv has two arguments: (path, argv), where
3448 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003449
Victor Stinner8c62be82010-05-06 00:08:46 +00003450 if (!PyArg_ParseTuple(args, "O&O:execv",
3451 PyUnicode_FSConverter,
3452 &opath, &argv))
3453 return NULL;
3454 path = PyBytes_AsString(opath);
3455 if (PyList_Check(argv)) {
3456 argc = PyList_Size(argv);
3457 getitem = PyList_GetItem;
3458 }
3459 else if (PyTuple_Check(argv)) {
3460 argc = PyTuple_Size(argv);
3461 getitem = PyTuple_GetItem;
3462 }
3463 else {
3464 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3465 Py_DECREF(opath);
3466 return NULL;
3467 }
3468 if (argc < 1) {
3469 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3470 Py_DECREF(opath);
3471 return NULL;
3472 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003473
Victor Stinner8c62be82010-05-06 00:08:46 +00003474 argvlist = PyMem_NEW(char *, argc+1);
3475 if (argvlist == NULL) {
3476 Py_DECREF(opath);
3477 return PyErr_NoMemory();
3478 }
3479 for (i = 0; i < argc; i++) {
3480 if (!fsconvert_strdup((*getitem)(argv, i),
3481 &argvlist[i])) {
3482 free_string_array(argvlist, i);
3483 PyErr_SetString(PyExc_TypeError,
3484 "execv() arg 2 must contain only strings");
3485 Py_DECREF(opath);
3486 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003487
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 }
3489 }
3490 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003491
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003493
Victor Stinner8c62be82010-05-06 00:08:46 +00003494 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003495
Victor Stinner8c62be82010-05-06 00:08:46 +00003496 free_string_array(argvlist, argc);
3497 Py_DECREF(opath);
3498 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003499}
3500
Victor Stinner13bb71c2010-04-23 21:41:56 +00003501static char**
3502parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3503{
Victor Stinner8c62be82010-05-06 00:08:46 +00003504 char **envlist;
3505 Py_ssize_t i, pos, envc;
3506 PyObject *keys=NULL, *vals=NULL;
3507 PyObject *key, *val, *key2, *val2;
3508 char *p, *k, *v;
3509 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003510
Victor Stinner8c62be82010-05-06 00:08:46 +00003511 i = PyMapping_Size(env);
3512 if (i < 0)
3513 return NULL;
3514 envlist = PyMem_NEW(char *, i + 1);
3515 if (envlist == NULL) {
3516 PyErr_NoMemory();
3517 return NULL;
3518 }
3519 envc = 0;
3520 keys = PyMapping_Keys(env);
3521 vals = PyMapping_Values(env);
3522 if (!keys || !vals)
3523 goto error;
3524 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3525 PyErr_Format(PyExc_TypeError,
3526 "env.keys() or env.values() is not a list");
3527 goto error;
3528 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003529
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 for (pos = 0; pos < i; pos++) {
3531 key = PyList_GetItem(keys, pos);
3532 val = PyList_GetItem(vals, pos);
3533 if (!key || !val)
3534 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003535
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 if (PyUnicode_FSConverter(key, &key2) == 0)
3537 goto error;
3538 if (PyUnicode_FSConverter(val, &val2) == 0) {
3539 Py_DECREF(key2);
3540 goto error;
3541 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003542
3543#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003544 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3545 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003546#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 k = PyBytes_AsString(key2);
3548 v = PyBytes_AsString(val2);
3549 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003550
Victor Stinner8c62be82010-05-06 00:08:46 +00003551 p = PyMem_NEW(char, len);
3552 if (p == NULL) {
3553 PyErr_NoMemory();
3554 Py_DECREF(key2);
3555 Py_DECREF(val2);
3556 goto error;
3557 }
3558 PyOS_snprintf(p, len, "%s=%s", k, v);
3559 envlist[envc++] = p;
3560 Py_DECREF(key2);
3561 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003562#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003564#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 }
3566 Py_DECREF(vals);
3567 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003568
Victor Stinner8c62be82010-05-06 00:08:46 +00003569 envlist[envc] = 0;
3570 *envc_ptr = envc;
3571 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003572
3573error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003574 Py_XDECREF(keys);
3575 Py_XDECREF(vals);
3576 while (--envc >= 0)
3577 PyMem_DEL(envlist[envc]);
3578 PyMem_DEL(envlist);
3579 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003580}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003582PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003583"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003584Execute a path with arguments and environment, replacing current process.\n\
3585\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003586 path: path of executable file\n\
3587 args: tuple or list of arguments\n\
3588 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003589
Barry Warsaw53699e91996-12-10 23:23:01 +00003590static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003591posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003592{
Victor Stinner8c62be82010-05-06 00:08:46 +00003593 PyObject *opath;
3594 char *path;
3595 PyObject *argv, *env;
3596 char **argvlist;
3597 char **envlist;
3598 Py_ssize_t i, argc, envc;
3599 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3600 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003601
Victor Stinner8c62be82010-05-06 00:08:46 +00003602 /* execve has three arguments: (path, argv, env), where
3603 argv is a list or tuple of strings and env is a dictionary
3604 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003605
Victor Stinner8c62be82010-05-06 00:08:46 +00003606 if (!PyArg_ParseTuple(args, "O&OO:execve",
3607 PyUnicode_FSConverter,
3608 &opath, &argv, &env))
3609 return NULL;
3610 path = PyBytes_AsString(opath);
3611 if (PyList_Check(argv)) {
3612 argc = PyList_Size(argv);
3613 getitem = PyList_GetItem;
3614 }
3615 else if (PyTuple_Check(argv)) {
3616 argc = PyTuple_Size(argv);
3617 getitem = PyTuple_GetItem;
3618 }
3619 else {
3620 PyErr_SetString(PyExc_TypeError,
3621 "execve() arg 2 must be a tuple or list");
3622 goto fail_0;
3623 }
3624 if (!PyMapping_Check(env)) {
3625 PyErr_SetString(PyExc_TypeError,
3626 "execve() arg 3 must be a mapping object");
3627 goto fail_0;
3628 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003629
Victor Stinner8c62be82010-05-06 00:08:46 +00003630 argvlist = PyMem_NEW(char *, argc+1);
3631 if (argvlist == NULL) {
3632 PyErr_NoMemory();
3633 goto fail_0;
3634 }
3635 for (i = 0; i < argc; i++) {
3636 if (!fsconvert_strdup((*getitem)(argv, i),
3637 &argvlist[i]))
3638 {
3639 lastarg = i;
3640 goto fail_1;
3641 }
3642 }
3643 lastarg = argc;
3644 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003645
Victor Stinner8c62be82010-05-06 00:08:46 +00003646 envlist = parse_envlist(env, &envc);
3647 if (envlist == NULL)
3648 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003649
Victor Stinner8c62be82010-05-06 00:08:46 +00003650 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003651
Victor Stinner8c62be82010-05-06 00:08:46 +00003652 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003653
Victor Stinner8c62be82010-05-06 00:08:46 +00003654 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003655
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 while (--envc >= 0)
3657 PyMem_DEL(envlist[envc]);
3658 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003659 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003660 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003661 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003662 Py_DECREF(opath);
3663 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003664}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003665#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003667
Guido van Rossuma1065681999-01-25 23:20:23 +00003668#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003669PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003670"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003671Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003672\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003673 mode: mode of process creation\n\
3674 path: path of executable file\n\
3675 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003676
3677static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003678posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003679{
Victor Stinner8c62be82010-05-06 00:08:46 +00003680 PyObject *opath;
3681 char *path;
3682 PyObject *argv;
3683 char **argvlist;
3684 int mode, i;
3685 Py_ssize_t argc;
3686 Py_intptr_t spawnval;
3687 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003688
Victor Stinner8c62be82010-05-06 00:08:46 +00003689 /* spawnv has three arguments: (mode, path, argv), where
3690 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003691
Victor Stinner8c62be82010-05-06 00:08:46 +00003692 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3693 PyUnicode_FSConverter,
3694 &opath, &argv))
3695 return NULL;
3696 path = PyBytes_AsString(opath);
3697 if (PyList_Check(argv)) {
3698 argc = PyList_Size(argv);
3699 getitem = PyList_GetItem;
3700 }
3701 else if (PyTuple_Check(argv)) {
3702 argc = PyTuple_Size(argv);
3703 getitem = PyTuple_GetItem;
3704 }
3705 else {
3706 PyErr_SetString(PyExc_TypeError,
3707 "spawnv() arg 2 must be a tuple or list");
3708 Py_DECREF(opath);
3709 return NULL;
3710 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003711
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 argvlist = PyMem_NEW(char *, argc+1);
3713 if (argvlist == NULL) {
3714 Py_DECREF(opath);
3715 return PyErr_NoMemory();
3716 }
3717 for (i = 0; i < argc; i++) {
3718 if (!fsconvert_strdup((*getitem)(argv, i),
3719 &argvlist[i])) {
3720 free_string_array(argvlist, i);
3721 PyErr_SetString(
3722 PyExc_TypeError,
3723 "spawnv() arg 2 must contain only strings");
3724 Py_DECREF(opath);
3725 return NULL;
3726 }
3727 }
3728 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003729
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003730#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003731 Py_BEGIN_ALLOW_THREADS
3732 spawnval = spawnv(mode, path, argvlist);
3733 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003734#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003735 if (mode == _OLD_P_OVERLAY)
3736 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003737
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 Py_BEGIN_ALLOW_THREADS
3739 spawnval = _spawnv(mode, path, argvlist);
3740 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003741#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003742
Victor Stinner8c62be82010-05-06 00:08:46 +00003743 free_string_array(argvlist, argc);
3744 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003745
Victor Stinner8c62be82010-05-06 00:08:46 +00003746 if (spawnval == -1)
3747 return posix_error();
3748 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003749#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003750 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003751#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003752 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003753#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003754}
3755
3756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003757PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003758"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003759Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003760\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003761 mode: mode of process creation\n\
3762 path: path of executable file\n\
3763 args: tuple or list of arguments\n\
3764 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003765
3766static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003767posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003768{
Victor Stinner8c62be82010-05-06 00:08:46 +00003769 PyObject *opath;
3770 char *path;
3771 PyObject *argv, *env;
3772 char **argvlist;
3773 char **envlist;
3774 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003775 int mode;
3776 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003777 Py_intptr_t spawnval;
3778 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3779 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003780
Victor Stinner8c62be82010-05-06 00:08:46 +00003781 /* spawnve has four arguments: (mode, path, argv, env), where
3782 argv is a list or tuple of strings and env is a dictionary
3783 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003784
Victor Stinner8c62be82010-05-06 00:08:46 +00003785 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3786 PyUnicode_FSConverter,
3787 &opath, &argv, &env))
3788 return NULL;
3789 path = PyBytes_AsString(opath);
3790 if (PyList_Check(argv)) {
3791 argc = PyList_Size(argv);
3792 getitem = PyList_GetItem;
3793 }
3794 else if (PyTuple_Check(argv)) {
3795 argc = PyTuple_Size(argv);
3796 getitem = PyTuple_GetItem;
3797 }
3798 else {
3799 PyErr_SetString(PyExc_TypeError,
3800 "spawnve() arg 2 must be a tuple or list");
3801 goto fail_0;
3802 }
3803 if (!PyMapping_Check(env)) {
3804 PyErr_SetString(PyExc_TypeError,
3805 "spawnve() arg 3 must be a mapping object");
3806 goto fail_0;
3807 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003808
Victor Stinner8c62be82010-05-06 00:08:46 +00003809 argvlist = PyMem_NEW(char *, argc+1);
3810 if (argvlist == NULL) {
3811 PyErr_NoMemory();
3812 goto fail_0;
3813 }
3814 for (i = 0; i < argc; i++) {
3815 if (!fsconvert_strdup((*getitem)(argv, i),
3816 &argvlist[i]))
3817 {
3818 lastarg = i;
3819 goto fail_1;
3820 }
3821 }
3822 lastarg = argc;
3823 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003824
Victor Stinner8c62be82010-05-06 00:08:46 +00003825 envlist = parse_envlist(env, &envc);
3826 if (envlist == NULL)
3827 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003828
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003829#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003830 Py_BEGIN_ALLOW_THREADS
3831 spawnval = spawnve(mode, path, argvlist, envlist);
3832 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003833#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003834 if (mode == _OLD_P_OVERLAY)
3835 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003836
Victor Stinner8c62be82010-05-06 00:08:46 +00003837 Py_BEGIN_ALLOW_THREADS
3838 spawnval = _spawnve(mode, path, argvlist, envlist);
3839 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003840#endif
Tim Peters25059d32001-12-07 20:35:43 +00003841
Victor Stinner8c62be82010-05-06 00:08:46 +00003842 if (spawnval == -1)
3843 (void) posix_error();
3844 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003845#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003846 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003847#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003848 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003849#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003850
Victor Stinner8c62be82010-05-06 00:08:46 +00003851 while (--envc >= 0)
3852 PyMem_DEL(envlist[envc]);
3853 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003854 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003855 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003856 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003857 Py_DECREF(opath);
3858 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003859}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003860
3861/* OS/2 supports spawnvp & spawnvpe natively */
3862#if defined(PYOS_OS2)
3863PyDoc_STRVAR(posix_spawnvp__doc__,
3864"spawnvp(mode, file, args)\n\n\
3865Execute the program 'file' in a new process, using the environment\n\
3866search path to find the file.\n\
3867\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003868 mode: mode of process creation\n\
3869 file: executable file name\n\
3870 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003871
3872static PyObject *
3873posix_spawnvp(PyObject *self, PyObject *args)
3874{
Victor Stinner8c62be82010-05-06 00:08:46 +00003875 PyObject *opath;
3876 char *path;
3877 PyObject *argv;
3878 char **argvlist;
3879 int mode, i, argc;
3880 Py_intptr_t spawnval;
3881 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003882
Victor Stinner8c62be82010-05-06 00:08:46 +00003883 /* spawnvp has three arguments: (mode, path, argv), where
3884 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003885
Victor Stinner8c62be82010-05-06 00:08:46 +00003886 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3887 PyUnicode_FSConverter,
3888 &opath, &argv))
3889 return NULL;
3890 path = PyBytes_AsString(opath);
3891 if (PyList_Check(argv)) {
3892 argc = PyList_Size(argv);
3893 getitem = PyList_GetItem;
3894 }
3895 else if (PyTuple_Check(argv)) {
3896 argc = PyTuple_Size(argv);
3897 getitem = PyTuple_GetItem;
3898 }
3899 else {
3900 PyErr_SetString(PyExc_TypeError,
3901 "spawnvp() arg 2 must be a tuple or list");
3902 Py_DECREF(opath);
3903 return NULL;
3904 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003905
Victor Stinner8c62be82010-05-06 00:08:46 +00003906 argvlist = PyMem_NEW(char *, argc+1);
3907 if (argvlist == NULL) {
3908 Py_DECREF(opath);
3909 return PyErr_NoMemory();
3910 }
3911 for (i = 0; i < argc; i++) {
3912 if (!fsconvert_strdup((*getitem)(argv, i),
3913 &argvlist[i])) {
3914 free_string_array(argvlist, i);
3915 PyErr_SetString(
3916 PyExc_TypeError,
3917 "spawnvp() arg 2 must contain only strings");
3918 Py_DECREF(opath);
3919 return NULL;
3920 }
3921 }
3922 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003923
Victor Stinner8c62be82010-05-06 00:08:46 +00003924 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003925#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003926 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003927#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003928 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003929#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003930 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003931
Victor Stinner8c62be82010-05-06 00:08:46 +00003932 free_string_array(argvlist, argc);
3933 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003934
Victor Stinner8c62be82010-05-06 00:08:46 +00003935 if (spawnval == -1)
3936 return posix_error();
3937 else
3938 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003939}
3940
3941
3942PyDoc_STRVAR(posix_spawnvpe__doc__,
3943"spawnvpe(mode, file, args, env)\n\n\
3944Execute the program 'file' in a new process, using the environment\n\
3945search path to find the file.\n\
3946\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003947 mode: mode of process creation\n\
3948 file: executable file name\n\
3949 args: tuple or list of arguments\n\
3950 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003951
3952static PyObject *
3953posix_spawnvpe(PyObject *self, PyObject *args)
3954{
Victor Stinner8c62be82010-05-06 00:08:46 +00003955 PyObject *opath
3956 char *path;
3957 PyObject *argv, *env;
3958 char **argvlist;
3959 char **envlist;
3960 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003961 int mode;
3962 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003963 Py_intptr_t spawnval;
3964 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3965 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003966
Victor Stinner8c62be82010-05-06 00:08:46 +00003967 /* spawnvpe has four arguments: (mode, path, argv, env), where
3968 argv is a list or tuple of strings and env is a dictionary
3969 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003970
Victor Stinner8c62be82010-05-06 00:08:46 +00003971 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3972 PyUnicode_FSConverter,
3973 &opath, &argv, &env))
3974 return NULL;
3975 path = PyBytes_AsString(opath);
3976 if (PyList_Check(argv)) {
3977 argc = PyList_Size(argv);
3978 getitem = PyList_GetItem;
3979 }
3980 else if (PyTuple_Check(argv)) {
3981 argc = PyTuple_Size(argv);
3982 getitem = PyTuple_GetItem;
3983 }
3984 else {
3985 PyErr_SetString(PyExc_TypeError,
3986 "spawnvpe() arg 2 must be a tuple or list");
3987 goto fail_0;
3988 }
3989 if (!PyMapping_Check(env)) {
3990 PyErr_SetString(PyExc_TypeError,
3991 "spawnvpe() arg 3 must be a mapping object");
3992 goto fail_0;
3993 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003994
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 argvlist = PyMem_NEW(char *, argc+1);
3996 if (argvlist == NULL) {
3997 PyErr_NoMemory();
3998 goto fail_0;
3999 }
4000 for (i = 0; i < argc; i++) {
4001 if (!fsconvert_strdup((*getitem)(argv, i),
4002 &argvlist[i]))
4003 {
4004 lastarg = i;
4005 goto fail_1;
4006 }
4007 }
4008 lastarg = argc;
4009 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004010
Victor Stinner8c62be82010-05-06 00:08:46 +00004011 envlist = parse_envlist(env, &envc);
4012 if (envlist == NULL)
4013 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004014
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004016#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004017 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004018#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004019 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004020#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004021 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004022
Victor Stinner8c62be82010-05-06 00:08:46 +00004023 if (spawnval == -1)
4024 (void) posix_error();
4025 else
4026 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004027
Victor Stinner8c62be82010-05-06 00:08:46 +00004028 while (--envc >= 0)
4029 PyMem_DEL(envlist[envc]);
4030 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004031 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004032 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004033 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004034 Py_DECREF(opath);
4035 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004036}
4037#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004038#endif /* HAVE_SPAWNV */
4039
4040
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004041#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004042PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004043"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004044Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4045\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004046Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004047
4048static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004049posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004050{
Victor Stinner8c62be82010-05-06 00:08:46 +00004051 pid_t pid;
4052 int result = 0;
4053 _PyImport_AcquireLock();
4054 pid = fork1();
4055 if (pid == 0) {
4056 /* child: this clobbers and resets the import lock. */
4057 PyOS_AfterFork();
4058 } else {
4059 /* parent: release the import lock. */
4060 result = _PyImport_ReleaseLock();
4061 }
4062 if (pid == -1)
4063 return posix_error();
4064 if (result < 0) {
4065 /* Don't clobber the OSError if the fork failed. */
4066 PyErr_SetString(PyExc_RuntimeError,
4067 "not holding the import lock");
4068 return NULL;
4069 }
4070 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004071}
4072#endif
4073
4074
Guido van Rossumad0ee831995-03-01 10:34:45 +00004075#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004076PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004077"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004078Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004079Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004080
Barry Warsaw53699e91996-12-10 23:23:01 +00004081static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004082posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004083{
Victor Stinner8c62be82010-05-06 00:08:46 +00004084 pid_t pid;
4085 int result = 0;
4086 _PyImport_AcquireLock();
4087 pid = fork();
4088 if (pid == 0) {
4089 /* child: this clobbers and resets the import lock. */
4090 PyOS_AfterFork();
4091 } else {
4092 /* parent: release the import lock. */
4093 result = _PyImport_ReleaseLock();
4094 }
4095 if (pid == -1)
4096 return posix_error();
4097 if (result < 0) {
4098 /* Don't clobber the OSError if the fork failed. */
4099 PyErr_SetString(PyExc_RuntimeError,
4100 "not holding the import lock");
4101 return NULL;
4102 }
4103 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004104}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004105#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004106
Neal Norwitzb59798b2003-03-21 01:43:31 +00004107/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004108/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4109#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004110#define DEV_PTY_FILE "/dev/ptc"
4111#define HAVE_DEV_PTMX
4112#else
4113#define DEV_PTY_FILE "/dev/ptmx"
4114#endif
4115
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004116#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004117#ifdef HAVE_PTY_H
4118#include <pty.h>
4119#else
4120#ifdef HAVE_LIBUTIL_H
4121#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004122#else
4123#ifdef HAVE_UTIL_H
4124#include <util.h>
4125#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004126#endif /* HAVE_LIBUTIL_H */
4127#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004128#ifdef HAVE_STROPTS_H
4129#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004130#endif
4131#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004132
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004133#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004134PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004135"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004136Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004137
4138static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004139posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004140{
Victor Stinner8c62be82010-05-06 00:08:46 +00004141 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004142#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004143 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004144#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004145#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004146 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004147#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004148 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004149#endif
4150#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004151
Thomas Wouters70c21a12000-07-14 14:28:33 +00004152#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004153 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4154 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004155#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004156 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4157 if (slave_name == NULL)
4158 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004159
Victor Stinner8c62be82010-05-06 00:08:46 +00004160 slave_fd = open(slave_name, O_RDWR);
4161 if (slave_fd < 0)
4162 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004163#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004164 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4165 if (master_fd < 0)
4166 return posix_error();
4167 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4168 /* change permission of slave */
4169 if (grantpt(master_fd) < 0) {
4170 PyOS_setsig(SIGCHLD, sig_saved);
4171 return posix_error();
4172 }
4173 /* unlock slave */
4174 if (unlockpt(master_fd) < 0) {
4175 PyOS_setsig(SIGCHLD, sig_saved);
4176 return posix_error();
4177 }
4178 PyOS_setsig(SIGCHLD, sig_saved);
4179 slave_name = ptsname(master_fd); /* get name of slave */
4180 if (slave_name == NULL)
4181 return posix_error();
4182 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4183 if (slave_fd < 0)
4184 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004185#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004186 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4187 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004188#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004189 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004190#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004191#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004192#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004193
Victor Stinner8c62be82010-05-06 00:08:46 +00004194 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004195
Fred Drake8cef4cf2000-06-28 16:40:38 +00004196}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004197#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004198
4199#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004200PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004201"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004202Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4203Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004204To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004205
4206static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004207posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004208{
Victor Stinner8c62be82010-05-06 00:08:46 +00004209 int master_fd = -1, result = 0;
4210 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004211
Victor Stinner8c62be82010-05-06 00:08:46 +00004212 _PyImport_AcquireLock();
4213 pid = forkpty(&master_fd, NULL, NULL, NULL);
4214 if (pid == 0) {
4215 /* child: this clobbers and resets the import lock. */
4216 PyOS_AfterFork();
4217 } else {
4218 /* parent: release the import lock. */
4219 result = _PyImport_ReleaseLock();
4220 }
4221 if (pid == -1)
4222 return posix_error();
4223 if (result < 0) {
4224 /* Don't clobber the OSError if the fork failed. */
4225 PyErr_SetString(PyExc_RuntimeError,
4226 "not holding the import lock");
4227 return NULL;
4228 }
4229 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004230}
4231#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004232
Guido van Rossumad0ee831995-03-01 10:34:45 +00004233#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004234PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004235"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004236Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004237
Barry Warsaw53699e91996-12-10 23:23:01 +00004238static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004239posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004240{
Victor Stinner8c62be82010-05-06 00:08:46 +00004241 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004242}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004243#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004244
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004245
Guido van Rossumad0ee831995-03-01 10:34:45 +00004246#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004247PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004248"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004249Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004250
Barry Warsaw53699e91996-12-10 23:23:01 +00004251static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004252posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004253{
Victor Stinner8c62be82010-05-06 00:08:46 +00004254 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004255}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004256#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004257
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004258
Guido van Rossumad0ee831995-03-01 10:34:45 +00004259#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004260PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004261"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004262Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004263
Barry Warsaw53699e91996-12-10 23:23:01 +00004264static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004265posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004266{
Victor Stinner8c62be82010-05-06 00:08:46 +00004267 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004268}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004269#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004270
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004271
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004272PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004273"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004274Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004275
Barry Warsaw53699e91996-12-10 23:23:01 +00004276static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004277posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004278{
Victor Stinner8c62be82010-05-06 00:08:46 +00004279 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004280}
4281
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004282
Fred Drakec9680921999-12-13 16:37:25 +00004283#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004284PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004285"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004286Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004287
4288static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004289posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004290{
4291 PyObject *result = NULL;
4292
Fred Drakec9680921999-12-13 16:37:25 +00004293#ifdef NGROUPS_MAX
4294#define MAX_GROUPS NGROUPS_MAX
4295#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004296 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004297#define MAX_GROUPS 64
4298#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004299 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004300
4301 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4302 * This is a helper variable to store the intermediate result when
4303 * that happens.
4304 *
4305 * To keep the code readable the OSX behaviour is unconditional,
4306 * according to the POSIX spec this should be safe on all unix-y
4307 * systems.
4308 */
4309 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004310 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004311
Victor Stinner8c62be82010-05-06 00:08:46 +00004312 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004313 if (n < 0) {
4314 if (errno == EINVAL) {
4315 n = getgroups(0, NULL);
4316 if (n == -1) {
4317 return posix_error();
4318 }
4319 if (n == 0) {
4320 /* Avoid malloc(0) */
4321 alt_grouplist = grouplist;
4322 } else {
4323 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4324 if (alt_grouplist == NULL) {
4325 errno = EINVAL;
4326 return posix_error();
4327 }
4328 n = getgroups(n, alt_grouplist);
4329 if (n == -1) {
4330 PyMem_Free(alt_grouplist);
4331 return posix_error();
4332 }
4333 }
4334 } else {
4335 return posix_error();
4336 }
4337 }
4338 result = PyList_New(n);
4339 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004340 int i;
4341 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004342 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004343 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004344 Py_DECREF(result);
4345 result = NULL;
4346 break;
Fred Drakec9680921999-12-13 16:37:25 +00004347 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004348 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004349 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004350 }
4351
4352 if (alt_grouplist != grouplist) {
4353 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004354 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004355
Fred Drakec9680921999-12-13 16:37:25 +00004356 return result;
4357}
4358#endif
4359
Antoine Pitroub7572f02009-12-02 20:46:48 +00004360#ifdef HAVE_INITGROUPS
4361PyDoc_STRVAR(posix_initgroups__doc__,
4362"initgroups(username, gid) -> None\n\n\
4363Call the system initgroups() to initialize the group access list with all of\n\
4364the groups of which the specified username is a member, plus the specified\n\
4365group id.");
4366
4367static PyObject *
4368posix_initgroups(PyObject *self, PyObject *args)
4369{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004370 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004372 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004373 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004374
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004375 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4376 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004378 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004379
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004380 res = initgroups(username, (gid_t) gid);
4381 Py_DECREF(oname);
4382 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004383 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004384
Victor Stinner8c62be82010-05-06 00:08:46 +00004385 Py_INCREF(Py_None);
4386 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004387}
4388#endif
4389
Martin v. Löwis606edc12002-06-13 21:09:11 +00004390#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004391PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004392"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004393Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004394
4395static PyObject *
4396posix_getpgid(PyObject *self, PyObject *args)
4397{
Victor Stinner8c62be82010-05-06 00:08:46 +00004398 pid_t pid, pgid;
4399 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4400 return NULL;
4401 pgid = getpgid(pid);
4402 if (pgid < 0)
4403 return posix_error();
4404 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004405}
4406#endif /* HAVE_GETPGID */
4407
4408
Guido van Rossumb6775db1994-08-01 11:34:53 +00004409#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004410PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004411"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004412Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004413
Barry Warsaw53699e91996-12-10 23:23:01 +00004414static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004415posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004416{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004417#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004418 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004419#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004420 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004421#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004422}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004423#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004424
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004425
Guido van Rossumb6775db1994-08-01 11:34:53 +00004426#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004427PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004428"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004429Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004430
Barry Warsaw53699e91996-12-10 23:23:01 +00004431static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004432posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004433{
Guido van Rossum64933891994-10-20 21:56:42 +00004434#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004435 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004436#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004437 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004438#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004439 return posix_error();
4440 Py_INCREF(Py_None);
4441 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004442}
4443
Guido van Rossumb6775db1994-08-01 11:34:53 +00004444#endif /* HAVE_SETPGRP */
4445
Guido van Rossumad0ee831995-03-01 10:34:45 +00004446#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004447
4448#ifdef MS_WINDOWS
4449#include <tlhelp32.h>
4450
4451static PyObject*
4452win32_getppid()
4453{
4454 HANDLE snapshot;
4455 pid_t mypid;
4456 PyObject* result = NULL;
4457 BOOL have_record;
4458 PROCESSENTRY32 pe;
4459
4460 mypid = getpid(); /* This function never fails */
4461
4462 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4463 if (snapshot == INVALID_HANDLE_VALUE)
4464 return PyErr_SetFromWindowsErr(GetLastError());
4465
4466 pe.dwSize = sizeof(pe);
4467 have_record = Process32First(snapshot, &pe);
4468 while (have_record) {
4469 if (mypid == (pid_t)pe.th32ProcessID) {
4470 /* We could cache the ulong value in a static variable. */
4471 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4472 break;
4473 }
4474
4475 have_record = Process32Next(snapshot, &pe);
4476 }
4477
4478 /* If our loop exits and our pid was not found (result will be NULL)
4479 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4480 * error anyway, so let's raise it. */
4481 if (!result)
4482 result = PyErr_SetFromWindowsErr(GetLastError());
4483
4484 CloseHandle(snapshot);
4485
4486 return result;
4487}
4488#endif /*MS_WINDOWS*/
4489
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004490PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004491"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004492Return the parent's process id. If the parent process has already exited,\n\
4493Windows machines will still return its id; others systems will return the id\n\
4494of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004495
Barry Warsaw53699e91996-12-10 23:23:01 +00004496static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004497posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004498{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004499#ifdef MS_WINDOWS
4500 return win32_getppid();
4501#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004502 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004503#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004504}
4505#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004506
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004507
Fred Drake12c6e2d1999-12-14 21:25:03 +00004508#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004509PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004510"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004511Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004512
4513static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004514posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004515{
Victor Stinner8c62be82010-05-06 00:08:46 +00004516 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004517#ifdef MS_WINDOWS
4518 wchar_t user_name[UNLEN + 1];
4519 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4520
4521 if (GetUserNameW(user_name, &num_chars)) {
4522 /* num_chars is the number of unicode chars plus null terminator */
4523 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4524 }
4525 else
4526 result = PyErr_SetFromWindowsErr(GetLastError());
4527#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004528 char *name;
4529 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004530
Victor Stinner8c62be82010-05-06 00:08:46 +00004531 errno = 0;
4532 name = getlogin();
4533 if (name == NULL) {
4534 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004535 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004536 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004537 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004538 }
4539 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004540 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004542#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004543 return result;
4544}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004545#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004546
Guido van Rossumad0ee831995-03-01 10:34:45 +00004547#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004548PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004549"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004550Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004551
Barry Warsaw53699e91996-12-10 23:23:01 +00004552static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004553posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004554{
Victor Stinner8c62be82010-05-06 00:08:46 +00004555 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004556}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004557#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004558
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004559
Guido van Rossumad0ee831995-03-01 10:34:45 +00004560#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004561PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004562"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004563Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004564
Barry Warsaw53699e91996-12-10 23:23:01 +00004565static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004566posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004567{
Victor Stinner8c62be82010-05-06 00:08:46 +00004568 pid_t pid;
4569 int sig;
4570 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4571 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004572#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004573 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4574 APIRET rc;
4575 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004576 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004577
4578 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4579 APIRET rc;
4580 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004581 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004582
4583 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004584 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004585#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004586 if (kill(pid, sig) == -1)
4587 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004588#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004589 Py_INCREF(Py_None);
4590 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004591}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004592#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004593
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004594#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004595PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004596"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004597Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004598
4599static PyObject *
4600posix_killpg(PyObject *self, PyObject *args)
4601{
Victor Stinner8c62be82010-05-06 00:08:46 +00004602 int sig;
4603 pid_t pgid;
4604 /* XXX some man pages make the `pgid` parameter an int, others
4605 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4606 take the same type. Moreover, pid_t is always at least as wide as
4607 int (else compilation of this module fails), which is safe. */
4608 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4609 return NULL;
4610 if (killpg(pgid, sig) == -1)
4611 return posix_error();
4612 Py_INCREF(Py_None);
4613 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004614}
4615#endif
4616
Brian Curtineb24d742010-04-12 17:16:38 +00004617#ifdef MS_WINDOWS
4618PyDoc_STRVAR(win32_kill__doc__,
4619"kill(pid, sig)\n\n\
4620Kill a process with a signal.");
4621
4622static PyObject *
4623win32_kill(PyObject *self, PyObject *args)
4624{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004625 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004626 DWORD pid, sig, err;
4627 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004628
Victor Stinner8c62be82010-05-06 00:08:46 +00004629 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4630 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004631
Victor Stinner8c62be82010-05-06 00:08:46 +00004632 /* Console processes which share a common console can be sent CTRL+C or
4633 CTRL+BREAK events, provided they handle said events. */
4634 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4635 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4636 err = GetLastError();
4637 PyErr_SetFromWindowsErr(err);
4638 }
4639 else
4640 Py_RETURN_NONE;
4641 }
Brian Curtineb24d742010-04-12 17:16:38 +00004642
Victor Stinner8c62be82010-05-06 00:08:46 +00004643 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4644 attempt to open and terminate the process. */
4645 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4646 if (handle == NULL) {
4647 err = GetLastError();
4648 return PyErr_SetFromWindowsErr(err);
4649 }
Brian Curtineb24d742010-04-12 17:16:38 +00004650
Victor Stinner8c62be82010-05-06 00:08:46 +00004651 if (TerminateProcess(handle, sig) == 0) {
4652 err = GetLastError();
4653 result = PyErr_SetFromWindowsErr(err);
4654 } else {
4655 Py_INCREF(Py_None);
4656 result = Py_None;
4657 }
Brian Curtineb24d742010-04-12 17:16:38 +00004658
Victor Stinner8c62be82010-05-06 00:08:46 +00004659 CloseHandle(handle);
4660 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004661}
4662#endif /* MS_WINDOWS */
4663
Guido van Rossumc0125471996-06-28 18:55:32 +00004664#ifdef HAVE_PLOCK
4665
4666#ifdef HAVE_SYS_LOCK_H
4667#include <sys/lock.h>
4668#endif
4669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004670PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004671"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004672Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004673
Barry Warsaw53699e91996-12-10 23:23:01 +00004674static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004675posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004676{
Victor Stinner8c62be82010-05-06 00:08:46 +00004677 int op;
4678 if (!PyArg_ParseTuple(args, "i:plock", &op))
4679 return NULL;
4680 if (plock(op) == -1)
4681 return posix_error();
4682 Py_INCREF(Py_None);
4683 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004684}
4685#endif
4686
Guido van Rossumb6775db1994-08-01 11:34:53 +00004687#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004688PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004689"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004690Set the current process's user id.");
4691
Barry Warsaw53699e91996-12-10 23:23:01 +00004692static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004693posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004694{
Victor Stinner8c62be82010-05-06 00:08:46 +00004695 long uid_arg;
4696 uid_t uid;
4697 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4698 return NULL;
4699 uid = uid_arg;
4700 if (uid != uid_arg) {
4701 PyErr_SetString(PyExc_OverflowError, "user id too big");
4702 return NULL;
4703 }
4704 if (setuid(uid) < 0)
4705 return posix_error();
4706 Py_INCREF(Py_None);
4707 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004708}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004709#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004710
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004711
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004712#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004713PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004714"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004715Set the current process's effective user id.");
4716
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004717static PyObject *
4718posix_seteuid (PyObject *self, PyObject *args)
4719{
Victor Stinner8c62be82010-05-06 00:08:46 +00004720 long euid_arg;
4721 uid_t euid;
4722 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4723 return NULL;
4724 euid = euid_arg;
4725 if (euid != euid_arg) {
4726 PyErr_SetString(PyExc_OverflowError, "user id too big");
4727 return NULL;
4728 }
4729 if (seteuid(euid) < 0) {
4730 return posix_error();
4731 } else {
4732 Py_INCREF(Py_None);
4733 return Py_None;
4734 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004735}
4736#endif /* HAVE_SETEUID */
4737
4738#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004739PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004740"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004741Set the current process's effective group id.");
4742
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004743static PyObject *
4744posix_setegid (PyObject *self, PyObject *args)
4745{
Victor Stinner8c62be82010-05-06 00:08:46 +00004746 long egid_arg;
4747 gid_t egid;
4748 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4749 return NULL;
4750 egid = egid_arg;
4751 if (egid != egid_arg) {
4752 PyErr_SetString(PyExc_OverflowError, "group id too big");
4753 return NULL;
4754 }
4755 if (setegid(egid) < 0) {
4756 return posix_error();
4757 } else {
4758 Py_INCREF(Py_None);
4759 return Py_None;
4760 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004761}
4762#endif /* HAVE_SETEGID */
4763
4764#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004765PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004766"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004767Set the current process's real and effective user ids.");
4768
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004769static PyObject *
4770posix_setreuid (PyObject *self, PyObject *args)
4771{
Victor Stinner8c62be82010-05-06 00:08:46 +00004772 long ruid_arg, euid_arg;
4773 uid_t ruid, euid;
4774 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4775 return NULL;
4776 if (ruid_arg == -1)
4777 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4778 else
4779 ruid = ruid_arg; /* otherwise, assign from our long */
4780 if (euid_arg == -1)
4781 euid = (uid_t)-1;
4782 else
4783 euid = euid_arg;
4784 if ((euid_arg != -1 && euid != euid_arg) ||
4785 (ruid_arg != -1 && ruid != ruid_arg)) {
4786 PyErr_SetString(PyExc_OverflowError, "user id too big");
4787 return NULL;
4788 }
4789 if (setreuid(ruid, euid) < 0) {
4790 return posix_error();
4791 } else {
4792 Py_INCREF(Py_None);
4793 return Py_None;
4794 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004795}
4796#endif /* HAVE_SETREUID */
4797
4798#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004799PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004800"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004801Set the current process's real and effective group ids.");
4802
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004803static PyObject *
4804posix_setregid (PyObject *self, PyObject *args)
4805{
Victor Stinner8c62be82010-05-06 00:08:46 +00004806 long rgid_arg, egid_arg;
4807 gid_t rgid, egid;
4808 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4809 return NULL;
4810 if (rgid_arg == -1)
4811 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4812 else
4813 rgid = rgid_arg; /* otherwise, assign from our long */
4814 if (egid_arg == -1)
4815 egid = (gid_t)-1;
4816 else
4817 egid = egid_arg;
4818 if ((egid_arg != -1 && egid != egid_arg) ||
4819 (rgid_arg != -1 && rgid != rgid_arg)) {
4820 PyErr_SetString(PyExc_OverflowError, "group id too big");
4821 return NULL;
4822 }
4823 if (setregid(rgid, egid) < 0) {
4824 return posix_error();
4825 } else {
4826 Py_INCREF(Py_None);
4827 return Py_None;
4828 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004829}
4830#endif /* HAVE_SETREGID */
4831
Guido van Rossumb6775db1994-08-01 11:34:53 +00004832#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004833PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004834"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004835Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004836
Barry Warsaw53699e91996-12-10 23:23:01 +00004837static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004838posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004839{
Victor Stinner8c62be82010-05-06 00:08:46 +00004840 long gid_arg;
4841 gid_t gid;
4842 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4843 return NULL;
4844 gid = gid_arg;
4845 if (gid != gid_arg) {
4846 PyErr_SetString(PyExc_OverflowError, "group id too big");
4847 return NULL;
4848 }
4849 if (setgid(gid) < 0)
4850 return posix_error();
4851 Py_INCREF(Py_None);
4852 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004853}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004854#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004855
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004856#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004857PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004858"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004859Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004860
4861static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004862posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004863{
Victor Stinner8c62be82010-05-06 00:08:46 +00004864 int i, len;
4865 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004866
Victor Stinner8c62be82010-05-06 00:08:46 +00004867 if (!PySequence_Check(groups)) {
4868 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4869 return NULL;
4870 }
4871 len = PySequence_Size(groups);
4872 if (len > MAX_GROUPS) {
4873 PyErr_SetString(PyExc_ValueError, "too many groups");
4874 return NULL;
4875 }
4876 for(i = 0; i < len; i++) {
4877 PyObject *elem;
4878 elem = PySequence_GetItem(groups, i);
4879 if (!elem)
4880 return NULL;
4881 if (!PyLong_Check(elem)) {
4882 PyErr_SetString(PyExc_TypeError,
4883 "groups must be integers");
4884 Py_DECREF(elem);
4885 return NULL;
4886 } else {
4887 unsigned long x = PyLong_AsUnsignedLong(elem);
4888 if (PyErr_Occurred()) {
4889 PyErr_SetString(PyExc_TypeError,
4890 "group id too big");
4891 Py_DECREF(elem);
4892 return NULL;
4893 }
4894 grouplist[i] = x;
4895 /* read back the value to see if it fitted in gid_t */
4896 if (grouplist[i] != x) {
4897 PyErr_SetString(PyExc_TypeError,
4898 "group id too big");
4899 Py_DECREF(elem);
4900 return NULL;
4901 }
4902 }
4903 Py_DECREF(elem);
4904 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004905
Victor Stinner8c62be82010-05-06 00:08:46 +00004906 if (setgroups(len, grouplist) < 0)
4907 return posix_error();
4908 Py_INCREF(Py_None);
4909 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004910}
4911#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004912
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004913#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4914static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004915wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004916{
Victor Stinner8c62be82010-05-06 00:08:46 +00004917 PyObject *result;
4918 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004919
Victor Stinner8c62be82010-05-06 00:08:46 +00004920 if (pid == -1)
4921 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004922
Victor Stinner8c62be82010-05-06 00:08:46 +00004923 if (struct_rusage == NULL) {
4924 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4925 if (m == NULL)
4926 return NULL;
4927 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4928 Py_DECREF(m);
4929 if (struct_rusage == NULL)
4930 return NULL;
4931 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004932
Victor Stinner8c62be82010-05-06 00:08:46 +00004933 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4934 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4935 if (!result)
4936 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004937
4938#ifndef doubletime
4939#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4940#endif
4941
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 PyStructSequence_SET_ITEM(result, 0,
4943 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4944 PyStructSequence_SET_ITEM(result, 1,
4945 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004946#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004947 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4948 SET_INT(result, 2, ru->ru_maxrss);
4949 SET_INT(result, 3, ru->ru_ixrss);
4950 SET_INT(result, 4, ru->ru_idrss);
4951 SET_INT(result, 5, ru->ru_isrss);
4952 SET_INT(result, 6, ru->ru_minflt);
4953 SET_INT(result, 7, ru->ru_majflt);
4954 SET_INT(result, 8, ru->ru_nswap);
4955 SET_INT(result, 9, ru->ru_inblock);
4956 SET_INT(result, 10, ru->ru_oublock);
4957 SET_INT(result, 11, ru->ru_msgsnd);
4958 SET_INT(result, 12, ru->ru_msgrcv);
4959 SET_INT(result, 13, ru->ru_nsignals);
4960 SET_INT(result, 14, ru->ru_nvcsw);
4961 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004962#undef SET_INT
4963
Victor Stinner8c62be82010-05-06 00:08:46 +00004964 if (PyErr_Occurred()) {
4965 Py_DECREF(result);
4966 return NULL;
4967 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004968
Victor Stinner8c62be82010-05-06 00:08:46 +00004969 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004970}
4971#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4972
4973#ifdef HAVE_WAIT3
4974PyDoc_STRVAR(posix_wait3__doc__,
4975"wait3(options) -> (pid, status, rusage)\n\n\
4976Wait for completion of a child process.");
4977
4978static PyObject *
4979posix_wait3(PyObject *self, PyObject *args)
4980{
Victor Stinner8c62be82010-05-06 00:08:46 +00004981 pid_t pid;
4982 int options;
4983 struct rusage ru;
4984 WAIT_TYPE status;
4985 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004986
Victor Stinner8c62be82010-05-06 00:08:46 +00004987 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4988 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004989
Victor Stinner8c62be82010-05-06 00:08:46 +00004990 Py_BEGIN_ALLOW_THREADS
4991 pid = wait3(&status, options, &ru);
4992 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004993
Victor Stinner8c62be82010-05-06 00:08:46 +00004994 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004995}
4996#endif /* HAVE_WAIT3 */
4997
4998#ifdef HAVE_WAIT4
4999PyDoc_STRVAR(posix_wait4__doc__,
5000"wait4(pid, options) -> (pid, status, rusage)\n\n\
5001Wait for completion of a given child process.");
5002
5003static PyObject *
5004posix_wait4(PyObject *self, PyObject *args)
5005{
Victor Stinner8c62be82010-05-06 00:08:46 +00005006 pid_t pid;
5007 int options;
5008 struct rusage ru;
5009 WAIT_TYPE status;
5010 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005011
Victor Stinner8c62be82010-05-06 00:08:46 +00005012 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5013 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005014
Victor Stinner8c62be82010-05-06 00:08:46 +00005015 Py_BEGIN_ALLOW_THREADS
5016 pid = wait4(pid, &status, options, &ru);
5017 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005018
Victor Stinner8c62be82010-05-06 00:08:46 +00005019 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005020}
5021#endif /* HAVE_WAIT4 */
5022
Guido van Rossumb6775db1994-08-01 11:34:53 +00005023#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005024PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005025"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005026Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005027
Barry Warsaw53699e91996-12-10 23:23:01 +00005028static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005029posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005030{
Victor Stinner8c62be82010-05-06 00:08:46 +00005031 pid_t pid;
5032 int options;
5033 WAIT_TYPE status;
5034 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005035
Victor Stinner8c62be82010-05-06 00:08:46 +00005036 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5037 return NULL;
5038 Py_BEGIN_ALLOW_THREADS
5039 pid = waitpid(pid, &status, options);
5040 Py_END_ALLOW_THREADS
5041 if (pid == -1)
5042 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005043
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005045}
5046
Tim Petersab034fa2002-02-01 11:27:43 +00005047#elif defined(HAVE_CWAIT)
5048
5049/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005050PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005051"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005052"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005053
5054static PyObject *
5055posix_waitpid(PyObject *self, PyObject *args)
5056{
Victor Stinner8c62be82010-05-06 00:08:46 +00005057 Py_intptr_t pid;
5058 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005059
Victor Stinner8c62be82010-05-06 00:08:46 +00005060 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5061 return NULL;
5062 Py_BEGIN_ALLOW_THREADS
5063 pid = _cwait(&status, pid, options);
5064 Py_END_ALLOW_THREADS
5065 if (pid == -1)
5066 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005067
Victor Stinner8c62be82010-05-06 00:08:46 +00005068 /* shift the status left a byte so this is more like the POSIX waitpid */
5069 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005070}
5071#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005072
Guido van Rossumad0ee831995-03-01 10:34:45 +00005073#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005074PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005075"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005076Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005077
Barry Warsaw53699e91996-12-10 23:23:01 +00005078static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005079posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005080{
Victor Stinner8c62be82010-05-06 00:08:46 +00005081 pid_t pid;
5082 WAIT_TYPE status;
5083 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005084
Victor Stinner8c62be82010-05-06 00:08:46 +00005085 Py_BEGIN_ALLOW_THREADS
5086 pid = wait(&status);
5087 Py_END_ALLOW_THREADS
5088 if (pid == -1)
5089 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005090
Victor Stinner8c62be82010-05-06 00:08:46 +00005091 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005092}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005093#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005094
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005096PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005097"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005098Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005099
Barry Warsaw53699e91996-12-10 23:23:01 +00005100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005101posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005102{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005103#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005104 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005105#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005106#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005107 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5108 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005109#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005110 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005111#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005112#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005113}
5114
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005115
Guido van Rossumb6775db1994-08-01 11:34:53 +00005116#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005117PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005118"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005119Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005120
Barry Warsaw53699e91996-12-10 23:23:01 +00005121static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005122posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005123{
Victor Stinner8c62be82010-05-06 00:08:46 +00005124 PyObject* v;
5125 char buf[MAXPATHLEN];
5126 PyObject *opath;
5127 char *path;
5128 int n;
5129 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005130
Victor Stinner8c62be82010-05-06 00:08:46 +00005131 if (!PyArg_ParseTuple(args, "O&:readlink",
5132 PyUnicode_FSConverter, &opath))
5133 return NULL;
5134 path = PyBytes_AsString(opath);
5135 v = PySequence_GetItem(args, 0);
5136 if (v == NULL) {
5137 Py_DECREF(opath);
5138 return NULL;
5139 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005140
Victor Stinner8c62be82010-05-06 00:08:46 +00005141 if (PyUnicode_Check(v)) {
5142 arg_is_unicode = 1;
5143 }
5144 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005145
Victor Stinner8c62be82010-05-06 00:08:46 +00005146 Py_BEGIN_ALLOW_THREADS
5147 n = readlink(path, buf, (int) sizeof buf);
5148 Py_END_ALLOW_THREADS
5149 if (n < 0)
5150 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005151
Victor Stinner8c62be82010-05-06 00:08:46 +00005152 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005153 if (arg_is_unicode)
5154 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5155 else
5156 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005157}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005158#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005159
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005160
Brian Curtin52173d42010-12-02 18:29:18 +00005161#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005162PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005163"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005164Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005165
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005166static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005167posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005168{
Victor Stinner8c62be82010-05-06 00:08:46 +00005169 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005170}
5171#endif /* HAVE_SYMLINK */
5172
Brian Curtind40e6f72010-07-08 21:39:08 +00005173#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5174
5175PyDoc_STRVAR(win_readlink__doc__,
5176"readlink(path) -> path\n\n\
5177Return a string representing the path to which the symbolic link points.");
5178
Brian Curtind40e6f72010-07-08 21:39:08 +00005179/* Windows readlink implementation */
5180static PyObject *
5181win_readlink(PyObject *self, PyObject *args)
5182{
5183 wchar_t *path;
5184 DWORD n_bytes_returned;
5185 DWORD io_result;
5186 PyObject *result;
5187 HANDLE reparse_point_handle;
5188
5189 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5190 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5191 wchar_t *print_name;
5192
5193 if (!PyArg_ParseTuple(args,
5194 "u:readlink",
5195 &path))
5196 return NULL;
5197
5198 /* First get a handle to the reparse point */
5199 Py_BEGIN_ALLOW_THREADS
5200 reparse_point_handle = CreateFileW(
5201 path,
5202 0,
5203 0,
5204 0,
5205 OPEN_EXISTING,
5206 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5207 0);
5208 Py_END_ALLOW_THREADS
5209
5210 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5211 {
5212 return win32_error_unicode("readlink", path);
5213 }
5214
5215 Py_BEGIN_ALLOW_THREADS
5216 /* New call DeviceIoControl to read the reparse point */
5217 io_result = DeviceIoControl(
5218 reparse_point_handle,
5219 FSCTL_GET_REPARSE_POINT,
5220 0, 0, /* in buffer */
5221 target_buffer, sizeof(target_buffer),
5222 &n_bytes_returned,
5223 0 /* we're not using OVERLAPPED_IO */
5224 );
5225 CloseHandle(reparse_point_handle);
5226 Py_END_ALLOW_THREADS
5227
5228 if (io_result==0)
5229 {
5230 return win32_error_unicode("readlink", path);
5231 }
5232
5233 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5234 {
5235 PyErr_SetString(PyExc_ValueError,
5236 "not a symbolic link");
5237 return NULL;
5238 }
Brian Curtin74e45612010-07-09 15:58:59 +00005239 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5240 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5241
5242 result = PyUnicode_FromWideChar(print_name,
5243 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005244 return result;
5245}
5246
5247#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5248
Brian Curtin52173d42010-12-02 18:29:18 +00005249#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005250
5251/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5252static int has_CreateSymbolicLinkW = 0;
5253static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5254static int
5255check_CreateSymbolicLinkW()
5256{
5257 HINSTANCE hKernel32;
5258 /* only recheck */
5259 if (has_CreateSymbolicLinkW)
5260 return has_CreateSymbolicLinkW;
5261 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005262 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5263 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005264 if (Py_CreateSymbolicLinkW)
5265 has_CreateSymbolicLinkW = 1;
5266 return has_CreateSymbolicLinkW;
5267}
5268
5269PyDoc_STRVAR(win_symlink__doc__,
5270"symlink(src, dst, target_is_directory=False)\n\n\
5271Create a symbolic link pointing to src named dst.\n\
5272target_is_directory is required if the target is to be interpreted as\n\
5273a directory.\n\
5274This function requires Windows 6.0 or greater, and raises a\n\
5275NotImplementedError otherwise.");
5276
5277static PyObject *
5278win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5279{
5280 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5281 PyObject *src, *dest;
5282 int target_is_directory = 0;
5283 DWORD res;
5284 WIN32_FILE_ATTRIBUTE_DATA src_info;
5285
5286 if (!check_CreateSymbolicLinkW())
5287 {
5288 /* raise NotImplementedError */
5289 return PyErr_Format(PyExc_NotImplementedError,
5290 "CreateSymbolicLinkW not found");
5291 }
5292 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5293 kwlist, &src, &dest, &target_is_directory))
5294 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005295
5296 if (win32_can_symlink == 0)
5297 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5298
Brian Curtind40e6f72010-07-08 21:39:08 +00005299 if (!convert_to_unicode(&src)) { return NULL; }
5300 if (!convert_to_unicode(&dest)) {
5301 Py_DECREF(src);
5302 return NULL;
5303 }
5304
5305 /* if src is a directory, ensure target_is_directory==1 */
5306 if(
5307 GetFileAttributesExW(
5308 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5309 ))
5310 {
5311 target_is_directory = target_is_directory ||
5312 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5313 }
5314
5315 Py_BEGIN_ALLOW_THREADS
5316 res = Py_CreateSymbolicLinkW(
5317 PyUnicode_AsUnicode(dest),
5318 PyUnicode_AsUnicode(src),
5319 target_is_directory);
5320 Py_END_ALLOW_THREADS
5321 Py_DECREF(src);
5322 Py_DECREF(dest);
5323 if (!res)
5324 {
5325 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5326 }
5327
5328 Py_INCREF(Py_None);
5329 return Py_None;
5330}
Brian Curtin52173d42010-12-02 18:29:18 +00005331#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005332
5333#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005334#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5335static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005336system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005337{
5338 ULONG value = 0;
5339
5340 Py_BEGIN_ALLOW_THREADS
5341 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5342 Py_END_ALLOW_THREADS
5343
5344 return value;
5345}
5346
5347static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005348posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005349{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005350 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005351 return Py_BuildValue("ddddd",
5352 (double)0 /* t.tms_utime / HZ */,
5353 (double)0 /* t.tms_stime / HZ */,
5354 (double)0 /* t.tms_cutime / HZ */,
5355 (double)0 /* t.tms_cstime / HZ */,
5356 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005357}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005358#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005359#define NEED_TICKS_PER_SECOND
5360static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005361static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005362posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005363{
Victor Stinner8c62be82010-05-06 00:08:46 +00005364 struct tms t;
5365 clock_t c;
5366 errno = 0;
5367 c = times(&t);
5368 if (c == (clock_t) -1)
5369 return posix_error();
5370 return Py_BuildValue("ddddd",
5371 (double)t.tms_utime / ticks_per_second,
5372 (double)t.tms_stime / ticks_per_second,
5373 (double)t.tms_cutime / ticks_per_second,
5374 (double)t.tms_cstime / ticks_per_second,
5375 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005376}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005377#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005378#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005379
5380
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005381#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005382#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005383static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005384posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005385{
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 FILETIME create, exit, kernel, user;
5387 HANDLE hProc;
5388 hProc = GetCurrentProcess();
5389 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5390 /* The fields of a FILETIME structure are the hi and lo part
5391 of a 64-bit value expressed in 100 nanosecond units.
5392 1e7 is one second in such units; 1e-7 the inverse.
5393 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5394 */
5395 return Py_BuildValue(
5396 "ddddd",
5397 (double)(user.dwHighDateTime*429.4967296 +
5398 user.dwLowDateTime*1e-7),
5399 (double)(kernel.dwHighDateTime*429.4967296 +
5400 kernel.dwLowDateTime*1e-7),
5401 (double)0,
5402 (double)0,
5403 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005404}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005405#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005406
5407#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005408PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005409"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005410Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005411#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005413
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005414#ifdef HAVE_GETSID
5415PyDoc_STRVAR(posix_getsid__doc__,
5416"getsid(pid) -> sid\n\n\
5417Call the system call getsid().");
5418
5419static PyObject *
5420posix_getsid(PyObject *self, PyObject *args)
5421{
Victor Stinner8c62be82010-05-06 00:08:46 +00005422 pid_t pid;
5423 int sid;
5424 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5425 return NULL;
5426 sid = getsid(pid);
5427 if (sid < 0)
5428 return posix_error();
5429 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005430}
5431#endif /* HAVE_GETSID */
5432
5433
Guido van Rossumb6775db1994-08-01 11:34:53 +00005434#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005435PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005436"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005437Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005438
Barry Warsaw53699e91996-12-10 23:23:01 +00005439static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005440posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005441{
Victor Stinner8c62be82010-05-06 00:08:46 +00005442 if (setsid() < 0)
5443 return posix_error();
5444 Py_INCREF(Py_None);
5445 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005446}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005447#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005448
Guido van Rossumb6775db1994-08-01 11:34:53 +00005449#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005450PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005451"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005452Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005453
Barry Warsaw53699e91996-12-10 23:23:01 +00005454static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005455posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005456{
Victor Stinner8c62be82010-05-06 00:08:46 +00005457 pid_t pid;
5458 int pgrp;
5459 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5460 return NULL;
5461 if (setpgid(pid, pgrp) < 0)
5462 return posix_error();
5463 Py_INCREF(Py_None);
5464 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005465}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005466#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005468
Guido van Rossumb6775db1994-08-01 11:34:53 +00005469#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005470PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005471"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005472Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005473
Barry Warsaw53699e91996-12-10 23:23:01 +00005474static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005475posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005476{
Victor Stinner8c62be82010-05-06 00:08:46 +00005477 int fd;
5478 pid_t pgid;
5479 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5480 return NULL;
5481 pgid = tcgetpgrp(fd);
5482 if (pgid < 0)
5483 return posix_error();
5484 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005485}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005486#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005487
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005488
Guido van Rossumb6775db1994-08-01 11:34:53 +00005489#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005490PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005491"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005492Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005493
Barry Warsaw53699e91996-12-10 23:23:01 +00005494static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005495posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005496{
Victor Stinner8c62be82010-05-06 00:08:46 +00005497 int fd;
5498 pid_t pgid;
5499 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5500 return NULL;
5501 if (tcsetpgrp(fd, pgid) < 0)
5502 return posix_error();
5503 Py_INCREF(Py_None);
5504 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005505}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005506#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005507
Guido van Rossum687dd131993-05-17 08:34:16 +00005508/* Functions acting on file descriptors */
5509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005510PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005511"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005512Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005513
Barry Warsaw53699e91996-12-10 23:23:01 +00005514static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005515posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005516{
Victor Stinner8c62be82010-05-06 00:08:46 +00005517 PyObject *ofile;
5518 char *file;
5519 int flag;
5520 int mode = 0777;
5521 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005522
5523#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005524 PyUnicodeObject *po;
5525 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5526 Py_BEGIN_ALLOW_THREADS
5527 /* PyUnicode_AS_UNICODE OK without thread
5528 lock as it is a simple dereference. */
5529 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5530 Py_END_ALLOW_THREADS
5531 if (fd < 0)
5532 return posix_error();
5533 return PyLong_FromLong((long)fd);
5534 }
5535 /* Drop the argument parsing error as narrow strings
5536 are also valid. */
5537 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005538#endif
5539
Victor Stinner8c62be82010-05-06 00:08:46 +00005540 if (!PyArg_ParseTuple(args, "O&i|i",
5541 PyUnicode_FSConverter, &ofile,
5542 &flag, &mode))
5543 return NULL;
5544 file = PyBytes_AsString(ofile);
5545 Py_BEGIN_ALLOW_THREADS
5546 fd = open(file, flag, mode);
5547 Py_END_ALLOW_THREADS
5548 if (fd < 0)
5549 return posix_error_with_allocated_filename(ofile);
5550 Py_DECREF(ofile);
5551 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005552}
5553
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005555PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005556"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005557Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005558
Barry Warsaw53699e91996-12-10 23:23:01 +00005559static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005560posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005561{
Victor Stinner8c62be82010-05-06 00:08:46 +00005562 int fd, res;
5563 if (!PyArg_ParseTuple(args, "i:close", &fd))
5564 return NULL;
5565 if (!_PyVerify_fd(fd))
5566 return posix_error();
5567 Py_BEGIN_ALLOW_THREADS
5568 res = close(fd);
5569 Py_END_ALLOW_THREADS
5570 if (res < 0)
5571 return posix_error();
5572 Py_INCREF(Py_None);
5573 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005574}
5575
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005576
Victor Stinner8c62be82010-05-06 00:08:46 +00005577PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005578"closerange(fd_low, fd_high)\n\n\
5579Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5580
5581static PyObject *
5582posix_closerange(PyObject *self, PyObject *args)
5583{
Victor Stinner8c62be82010-05-06 00:08:46 +00005584 int fd_from, fd_to, i;
5585 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5586 return NULL;
5587 Py_BEGIN_ALLOW_THREADS
5588 for (i = fd_from; i < fd_to; i++)
5589 if (_PyVerify_fd(i))
5590 close(i);
5591 Py_END_ALLOW_THREADS
5592 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005593}
5594
5595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005596PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005597"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005598Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005599
Barry Warsaw53699e91996-12-10 23:23:01 +00005600static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005601posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005602{
Victor Stinner8c62be82010-05-06 00:08:46 +00005603 int fd;
5604 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5605 return NULL;
5606 if (!_PyVerify_fd(fd))
5607 return posix_error();
5608 Py_BEGIN_ALLOW_THREADS
5609 fd = dup(fd);
5610 Py_END_ALLOW_THREADS
5611 if (fd < 0)
5612 return posix_error();
5613 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005614}
5615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005617PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005618"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005619Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005620
Barry Warsaw53699e91996-12-10 23:23:01 +00005621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005622posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005623{
Victor Stinner8c62be82010-05-06 00:08:46 +00005624 int fd, fd2, res;
5625 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5626 return NULL;
5627 if (!_PyVerify_fd_dup2(fd, fd2))
5628 return posix_error();
5629 Py_BEGIN_ALLOW_THREADS
5630 res = dup2(fd, fd2);
5631 Py_END_ALLOW_THREADS
5632 if (res < 0)
5633 return posix_error();
5634 Py_INCREF(Py_None);
5635 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005636}
5637
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005639PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005640"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005641Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005642
Barry Warsaw53699e91996-12-10 23:23:01 +00005643static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005644posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005645{
Victor Stinner8c62be82010-05-06 00:08:46 +00005646 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005647#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005649#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005650 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005651#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005652 PyObject *posobj;
5653 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5654 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005655#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005656 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5657 switch (how) {
5658 case 0: how = SEEK_SET; break;
5659 case 1: how = SEEK_CUR; break;
5660 case 2: how = SEEK_END; break;
5661 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005662#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005663
5664#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005665 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005666#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005667 pos = PyLong_Check(posobj) ?
5668 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005669#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005670 if (PyErr_Occurred())
5671 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005672
Victor Stinner8c62be82010-05-06 00:08:46 +00005673 if (!_PyVerify_fd(fd))
5674 return posix_error();
5675 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005676#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005677 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005678#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005679 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005680#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005681 Py_END_ALLOW_THREADS
5682 if (res < 0)
5683 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005684
5685#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005686 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005687#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005688 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005689#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005690}
5691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005693PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005694"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005695Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005696
Barry Warsaw53699e91996-12-10 23:23:01 +00005697static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005698posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005699{
Victor Stinner8c62be82010-05-06 00:08:46 +00005700 int fd, size;
5701 Py_ssize_t n;
5702 PyObject *buffer;
5703 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5704 return NULL;
5705 if (size < 0) {
5706 errno = EINVAL;
5707 return posix_error();
5708 }
5709 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5710 if (buffer == NULL)
5711 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005712 if (!_PyVerify_fd(fd)) {
5713 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005714 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005715 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005716 Py_BEGIN_ALLOW_THREADS
5717 n = read(fd, PyBytes_AS_STRING(buffer), size);
5718 Py_END_ALLOW_THREADS
5719 if (n < 0) {
5720 Py_DECREF(buffer);
5721 return posix_error();
5722 }
5723 if (n != size)
5724 _PyBytes_Resize(&buffer, n);
5725 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005726}
5727
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005728
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005729PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005730"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005731Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005732
Barry Warsaw53699e91996-12-10 23:23:01 +00005733static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005734posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005735{
Victor Stinner8c62be82010-05-06 00:08:46 +00005736 Py_buffer pbuf;
5737 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005738 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005739
Victor Stinner8c62be82010-05-06 00:08:46 +00005740 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5741 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005742 if (!_PyVerify_fd(fd)) {
5743 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005744 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005745 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005746 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005747 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005748#if defined(MS_WIN64) || defined(MS_WINDOWS)
5749 if (len > INT_MAX)
5750 len = INT_MAX;
5751 size = write(fd, pbuf.buf, (int)len);
5752#else
Victor Stinner72344792011-01-11 00:04:12 +00005753 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005754#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005755 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005756 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005757 if (size < 0)
5758 return posix_error();
5759 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005760}
5761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005762
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005763PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005764"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005765Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005766
Barry Warsaw53699e91996-12-10 23:23:01 +00005767static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005768posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005769{
Victor Stinner8c62be82010-05-06 00:08:46 +00005770 int fd;
5771 STRUCT_STAT st;
5772 int res;
5773 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5774 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005775#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005776 /* on OpenVMS we must ensure that all bytes are written to the file */
5777 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005778#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005779 if (!_PyVerify_fd(fd))
5780 return posix_error();
5781 Py_BEGIN_ALLOW_THREADS
5782 res = FSTAT(fd, &st);
5783 Py_END_ALLOW_THREADS
5784 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005785#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005786 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005787#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005789#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 }
Tim Peters5aa91602002-01-30 05:46:57 +00005791
Victor Stinner8c62be82010-05-06 00:08:46 +00005792 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005793}
5794
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005795PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005796"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005797Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005798connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005799
5800static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005801posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005802{
Victor Stinner8c62be82010-05-06 00:08:46 +00005803 int fd;
5804 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5805 return NULL;
5806 if (!_PyVerify_fd(fd))
5807 return PyBool_FromLong(0);
5808 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005809}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005810
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005811#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005812PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005813"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005814Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005815
Barry Warsaw53699e91996-12-10 23:23:01 +00005816static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005817posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005818{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005819#if defined(PYOS_OS2)
5820 HFILE read, write;
5821 APIRET rc;
5822
Victor Stinner8c62be82010-05-06 00:08:46 +00005823 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005824 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005825 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005826 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005827 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005828
5829 return Py_BuildValue("(ii)", read, write);
5830#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005831#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005832 int fds[2];
5833 int res;
5834 Py_BEGIN_ALLOW_THREADS
5835 res = pipe(fds);
5836 Py_END_ALLOW_THREADS
5837 if (res != 0)
5838 return posix_error();
5839 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005840#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005841 HANDLE read, write;
5842 int read_fd, write_fd;
5843 BOOL ok;
5844 Py_BEGIN_ALLOW_THREADS
5845 ok = CreatePipe(&read, &write, NULL, 0);
5846 Py_END_ALLOW_THREADS
5847 if (!ok)
5848 return win32_error("CreatePipe", NULL);
5849 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5850 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5851 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005852#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005853#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005854}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005855#endif /* HAVE_PIPE */
5856
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005857
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005858#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005859PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005860"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005861Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005862
Barry Warsaw53699e91996-12-10 23:23:01 +00005863static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005864posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005865{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005866 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005867 char *filename;
5868 int mode = 0666;
5869 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005870 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5871 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005872 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005873 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005874 Py_BEGIN_ALLOW_THREADS
5875 res = mkfifo(filename, mode);
5876 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005877 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005878 if (res < 0)
5879 return posix_error();
5880 Py_INCREF(Py_None);
5881 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005882}
5883#endif
5884
5885
Neal Norwitz11690112002-07-30 01:08:28 +00005886#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005887PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005888"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005889Create a filesystem node (file, device special file or named pipe)\n\
5890named filename. mode specifies both the permissions to use and the\n\
5891type of node to be created, being combined (bitwise OR) with one of\n\
5892S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005893device defines the newly created device special file (probably using\n\
5894os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005895
5896
5897static PyObject *
5898posix_mknod(PyObject *self, PyObject *args)
5899{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005900 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005901 char *filename;
5902 int mode = 0600;
5903 int device = 0;
5904 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005905 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5906 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005907 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005908 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005909 Py_BEGIN_ALLOW_THREADS
5910 res = mknod(filename, mode, device);
5911 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005912 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 if (res < 0)
5914 return posix_error();
5915 Py_INCREF(Py_None);
5916 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005917}
5918#endif
5919
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005920#ifdef HAVE_DEVICE_MACROS
5921PyDoc_STRVAR(posix_major__doc__,
5922"major(device) -> major number\n\
5923Extracts a device major number from a raw device number.");
5924
5925static PyObject *
5926posix_major(PyObject *self, PyObject *args)
5927{
Victor Stinner8c62be82010-05-06 00:08:46 +00005928 int device;
5929 if (!PyArg_ParseTuple(args, "i:major", &device))
5930 return NULL;
5931 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005932}
5933
5934PyDoc_STRVAR(posix_minor__doc__,
5935"minor(device) -> minor number\n\
5936Extracts a device minor number from a raw device number.");
5937
5938static PyObject *
5939posix_minor(PyObject *self, PyObject *args)
5940{
Victor Stinner8c62be82010-05-06 00:08:46 +00005941 int device;
5942 if (!PyArg_ParseTuple(args, "i:minor", &device))
5943 return NULL;
5944 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005945}
5946
5947PyDoc_STRVAR(posix_makedev__doc__,
5948"makedev(major, minor) -> device number\n\
5949Composes a raw device number from the major and minor device numbers.");
5950
5951static PyObject *
5952posix_makedev(PyObject *self, PyObject *args)
5953{
Victor Stinner8c62be82010-05-06 00:08:46 +00005954 int major, minor;
5955 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5956 return NULL;
5957 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005958}
5959#endif /* device macros */
5960
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005961
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005962#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005963PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005964"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005965Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005966
Barry Warsaw53699e91996-12-10 23:23:01 +00005967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005968posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005969{
Victor Stinner8c62be82010-05-06 00:08:46 +00005970 int fd;
5971 off_t length;
5972 int res;
5973 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005974
Victor Stinner8c62be82010-05-06 00:08:46 +00005975 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5976 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005977
5978#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005980#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005981 length = PyLong_Check(lenobj) ?
5982 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005983#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005984 if (PyErr_Occurred())
5985 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005986
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 Py_BEGIN_ALLOW_THREADS
5988 res = ftruncate(fd, length);
5989 Py_END_ALLOW_THREADS
5990 if (res < 0)
5991 return posix_error();
5992 Py_INCREF(Py_None);
5993 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005994}
5995#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005996
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005997#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005998PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005999"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006000Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006001
Fred Drake762e2061999-08-26 17:23:54 +00006002/* Save putenv() parameters as values here, so we can collect them when they
6003 * get re-set with another call for the same key. */
6004static PyObject *posix_putenv_garbage;
6005
Tim Peters5aa91602002-01-30 05:46:57 +00006006static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006007posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006008{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006009#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 wchar_t *s1, *s2;
6011 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006012#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006013 PyObject *os1, *os2;
6014 char *s1, *s2;
6015 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006016#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006017 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006018 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006019
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006020#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 if (!PyArg_ParseTuple(args,
6022 "uu:putenv",
6023 &s1, &s2))
6024 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006025#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006026 if (!PyArg_ParseTuple(args,
6027 "O&O&:putenv",
6028 PyUnicode_FSConverter, &os1,
6029 PyUnicode_FSConverter, &os2))
6030 return NULL;
6031 s1 = PyBytes_AsString(os1);
6032 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006033#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006034
6035#if defined(PYOS_OS2)
6036 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6037 APIRET rc;
6038
Guido van Rossumd48f2521997-12-05 22:19:34 +00006039 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006040 if (rc != NO_ERROR) {
6041 os2_error(rc);
6042 goto error;
6043 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006044
6045 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6046 APIRET rc;
6047
Guido van Rossumd48f2521997-12-05 22:19:34 +00006048 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006049 if (rc != NO_ERROR) {
6050 os2_error(rc);
6051 goto error;
6052 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006053 } else {
6054#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006055 /* XXX This can leak memory -- not easy to fix :-( */
6056 /* len includes space for a trailing \0; the size arg to
6057 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006058#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006059 len = wcslen(s1) + wcslen(s2) + 2;
6060 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006061#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006062 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006064#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006065 if (newstr == NULL) {
6066 PyErr_NoMemory();
6067 goto error;
6068 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006069#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006070 newenv = PyUnicode_AsUnicode(newstr);
6071 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6072 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006073 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006074 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006075 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006076#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006077 newenv = PyBytes_AS_STRING(newstr);
6078 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6079 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006081 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006082 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006083#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006084
Victor Stinner8c62be82010-05-06 00:08:46 +00006085 /* Install the first arg and newstr in posix_putenv_garbage;
6086 * this will cause previous value to be collected. This has to
6087 * happen after the real putenv() call because the old value
6088 * was still accessible until then. */
6089 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006090#ifdef MS_WINDOWS
6091 PyTuple_GET_ITEM(args, 0),
6092#else
6093 os1,
6094#endif
6095 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006096 /* really not much we can do; just leak */
6097 PyErr_Clear();
6098 }
6099 else {
6100 Py_DECREF(newstr);
6101 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006102
6103#if defined(PYOS_OS2)
6104 }
6105#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006106
Martin v. Löwis011e8422009-05-05 04:43:17 +00006107#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006108 Py_DECREF(os1);
6109 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006110#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006111 Py_RETURN_NONE;
6112
6113error:
6114#ifndef MS_WINDOWS
6115 Py_DECREF(os1);
6116 Py_DECREF(os2);
6117#endif
6118 Py_XDECREF(newstr);
6119 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006120}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006121#endif /* putenv */
6122
Guido van Rossumc524d952001-10-19 01:31:59 +00006123#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006124PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006125"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006126Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006127
6128static PyObject *
6129posix_unsetenv(PyObject *self, PyObject *args)
6130{
Victor Stinner84ae1182010-05-06 22:05:07 +00006131#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006132 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006133
Victor Stinner8c62be82010-05-06 00:08:46 +00006134 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6135 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006136#else
6137 PyObject *os1;
6138 char *s1;
6139
6140 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6141 PyUnicode_FSConverter, &os1))
6142 return NULL;
6143 s1 = PyBytes_AsString(os1);
6144#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006145
Victor Stinner8c62be82010-05-06 00:08:46 +00006146 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006147
Victor Stinner8c62be82010-05-06 00:08:46 +00006148 /* Remove the key from posix_putenv_garbage;
6149 * this will cause it to be collected. This has to
6150 * happen after the real unsetenv() call because the
6151 * old value was still accessible until then.
6152 */
6153 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006154#ifdef MS_WINDOWS
6155 PyTuple_GET_ITEM(args, 0)
6156#else
6157 os1
6158#endif
6159 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006160 /* really not much we can do; just leak */
6161 PyErr_Clear();
6162 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006163
Victor Stinner84ae1182010-05-06 22:05:07 +00006164#ifndef MS_WINDOWS
6165 Py_DECREF(os1);
6166#endif
6167 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006168}
6169#endif /* unsetenv */
6170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006171PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006172"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006173Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006174
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006175static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006176posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006177{
Victor Stinner8c62be82010-05-06 00:08:46 +00006178 int code;
6179 char *message;
6180 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6181 return NULL;
6182 message = strerror(code);
6183 if (message == NULL) {
6184 PyErr_SetString(PyExc_ValueError,
6185 "strerror() argument out of range");
6186 return NULL;
6187 }
6188 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006189}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006190
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006191
Guido van Rossumc9641791998-08-04 15:26:23 +00006192#ifdef HAVE_SYS_WAIT_H
6193
Fred Drake106c1a02002-04-23 15:58:02 +00006194#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006195PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006196"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006197Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006198
6199static PyObject *
6200posix_WCOREDUMP(PyObject *self, PyObject *args)
6201{
Victor Stinner8c62be82010-05-06 00:08:46 +00006202 WAIT_TYPE status;
6203 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006204
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6206 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006207
Victor Stinner8c62be82010-05-06 00:08:46 +00006208 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006209}
6210#endif /* WCOREDUMP */
6211
6212#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006213PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006214"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006215Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006216job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006217
6218static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006219posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006220{
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 WAIT_TYPE status;
6222 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006223
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6225 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006226
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006228}
6229#endif /* WIFCONTINUED */
6230
Guido van Rossumc9641791998-08-04 15:26:23 +00006231#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006232PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006233"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006234Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006235
6236static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006237posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006238{
Victor Stinner8c62be82010-05-06 00:08:46 +00006239 WAIT_TYPE status;
6240 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006241
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6243 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006244
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006246}
6247#endif /* WIFSTOPPED */
6248
6249#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006250PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006251"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006252Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006253
6254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006255posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006256{
Victor Stinner8c62be82010-05-06 00:08:46 +00006257 WAIT_TYPE status;
6258 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006259
Victor Stinner8c62be82010-05-06 00:08:46 +00006260 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6261 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006262
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006264}
6265#endif /* WIFSIGNALED */
6266
6267#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006268PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006269"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006270Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006271system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006272
6273static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006274posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006275{
Victor Stinner8c62be82010-05-06 00:08:46 +00006276 WAIT_TYPE status;
6277 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006278
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6280 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006281
Victor Stinner8c62be82010-05-06 00:08:46 +00006282 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006283}
6284#endif /* WIFEXITED */
6285
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006286#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006287PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006288"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006289Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006290
6291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006292posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006293{
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 WAIT_TYPE status;
6295 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006296
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6298 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006299
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006301}
6302#endif /* WEXITSTATUS */
6303
6304#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006305PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006306"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006307Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006308value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006309
6310static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006311posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006312{
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 WAIT_TYPE status;
6314 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006315
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6317 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006318
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006320}
6321#endif /* WTERMSIG */
6322
6323#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006324PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006325"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006326Return the signal that stopped the process that provided\n\
6327the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006328
6329static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006330posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006331{
Victor Stinner8c62be82010-05-06 00:08:46 +00006332 WAIT_TYPE status;
6333 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006334
Victor Stinner8c62be82010-05-06 00:08:46 +00006335 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6336 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006337
Victor Stinner8c62be82010-05-06 00:08:46 +00006338 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006339}
6340#endif /* WSTOPSIG */
6341
6342#endif /* HAVE_SYS_WAIT_H */
6343
6344
Thomas Wouters477c8d52006-05-27 19:21:47 +00006345#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006346#ifdef _SCO_DS
6347/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6348 needed definitions in sys/statvfs.h */
6349#define _SVID3
6350#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006351#include <sys/statvfs.h>
6352
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006353static PyObject*
6354_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006355 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6356 if (v == NULL)
6357 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006358
6359#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006360 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6361 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6362 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6363 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6364 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6365 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6366 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6367 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6368 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6369 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006370#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006371 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6372 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6373 PyStructSequence_SET_ITEM(v, 2,
6374 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6375 PyStructSequence_SET_ITEM(v, 3,
6376 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6377 PyStructSequence_SET_ITEM(v, 4,
6378 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6379 PyStructSequence_SET_ITEM(v, 5,
6380 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6381 PyStructSequence_SET_ITEM(v, 6,
6382 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6383 PyStructSequence_SET_ITEM(v, 7,
6384 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6385 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6386 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006387#endif
6388
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006390}
6391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006392PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006393"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006394Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006395
6396static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006397posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006398{
Victor Stinner8c62be82010-05-06 00:08:46 +00006399 int fd, res;
6400 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006401
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6403 return NULL;
6404 Py_BEGIN_ALLOW_THREADS
6405 res = fstatvfs(fd, &st);
6406 Py_END_ALLOW_THREADS
6407 if (res != 0)
6408 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006409
Victor Stinner8c62be82010-05-06 00:08:46 +00006410 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006411}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006412#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006413
6414
Thomas Wouters477c8d52006-05-27 19:21:47 +00006415#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006416#include <sys/statvfs.h>
6417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006418PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006419"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006420Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006421
6422static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006423posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006424{
Victor Stinner8c62be82010-05-06 00:08:46 +00006425 char *path;
6426 int res;
6427 struct statvfs st;
6428 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6429 return NULL;
6430 Py_BEGIN_ALLOW_THREADS
6431 res = statvfs(path, &st);
6432 Py_END_ALLOW_THREADS
6433 if (res != 0)
6434 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006435
Victor Stinner8c62be82010-05-06 00:08:46 +00006436 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006437}
6438#endif /* HAVE_STATVFS */
6439
Fred Drakec9680921999-12-13 16:37:25 +00006440/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6441 * It maps strings representing configuration variable names to
6442 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006443 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006444 * rarely-used constants. There are three separate tables that use
6445 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006446 *
6447 * This code is always included, even if none of the interfaces that
6448 * need it are included. The #if hackery needed to avoid it would be
6449 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006450 */
6451struct constdef {
6452 char *name;
6453 long value;
6454};
6455
Fred Drake12c6e2d1999-12-14 21:25:03 +00006456static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006457conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006458 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006459{
Christian Heimes217cfd12007-12-02 14:31:20 +00006460 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006461 *valuep = PyLong_AS_LONG(arg);
6462 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006463 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006464 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006465 /* look up the value in the table using a binary search */
6466 size_t lo = 0;
6467 size_t mid;
6468 size_t hi = tablesize;
6469 int cmp;
6470 const char *confname;
6471 if (!PyUnicode_Check(arg)) {
6472 PyErr_SetString(PyExc_TypeError,
6473 "configuration names must be strings or integers");
6474 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006476 confname = _PyUnicode_AsString(arg);
6477 if (confname == NULL)
6478 return 0;
6479 while (lo < hi) {
6480 mid = (lo + hi) / 2;
6481 cmp = strcmp(confname, table[mid].name);
6482 if (cmp < 0)
6483 hi = mid;
6484 else if (cmp > 0)
6485 lo = mid + 1;
6486 else {
6487 *valuep = table[mid].value;
6488 return 1;
6489 }
6490 }
6491 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6492 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006493 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006494}
6495
6496
6497#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6498static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006499#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006501#endif
6502#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006504#endif
Fred Drakec9680921999-12-13 16:37:25 +00006505#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006507#endif
6508#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
6511#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006513#endif
6514#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006515 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006516#endif
6517#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006518 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006519#endif
6520#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006522#endif
6523#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006524 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006525#endif
6526#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006527 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006528#endif
6529#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006530 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006531#endif
6532#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006533 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006534#endif
6535#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006536 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006537#endif
6538#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006540#endif
6541#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006542 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006543#endif
6544#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006545 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006546#endif
6547#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006549#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006550#ifdef _PC_ACL_ENABLED
6551 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6552#endif
6553#ifdef _PC_MIN_HOLE_SIZE
6554 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6555#endif
6556#ifdef _PC_ALLOC_SIZE_MIN
6557 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6558#endif
6559#ifdef _PC_REC_INCR_XFER_SIZE
6560 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6561#endif
6562#ifdef _PC_REC_MAX_XFER_SIZE
6563 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6564#endif
6565#ifdef _PC_REC_MIN_XFER_SIZE
6566 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6567#endif
6568#ifdef _PC_REC_XFER_ALIGN
6569 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6570#endif
6571#ifdef _PC_SYMLINK_MAX
6572 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6573#endif
6574#ifdef _PC_XATTR_ENABLED
6575 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6576#endif
6577#ifdef _PC_XATTR_EXISTS
6578 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6579#endif
6580#ifdef _PC_TIMESTAMP_RESOLUTION
6581 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6582#endif
Fred Drakec9680921999-12-13 16:37:25 +00006583};
6584
Fred Drakec9680921999-12-13 16:37:25 +00006585static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006586conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006587{
6588 return conv_confname(arg, valuep, posix_constants_pathconf,
6589 sizeof(posix_constants_pathconf)
6590 / sizeof(struct constdef));
6591}
6592#endif
6593
6594#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006595PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006596"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006597Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006598If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006599
6600static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006601posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006602{
6603 PyObject *result = NULL;
6604 int name, fd;
6605
Fred Drake12c6e2d1999-12-14 21:25:03 +00006606 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6607 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006608 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006609
Stefan Krah0e803b32010-11-26 16:16:47 +00006610 errno = 0;
6611 limit = fpathconf(fd, name);
6612 if (limit == -1 && errno != 0)
6613 posix_error();
6614 else
6615 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006616 }
6617 return result;
6618}
6619#endif
6620
6621
6622#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006623PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006624"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006625Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006626If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006627
6628static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006629posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006630{
6631 PyObject *result = NULL;
6632 int name;
6633 char *path;
6634
6635 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6636 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006638
Victor Stinner8c62be82010-05-06 00:08:46 +00006639 errno = 0;
6640 limit = pathconf(path, name);
6641 if (limit == -1 && errno != 0) {
6642 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006643 /* could be a path or name problem */
6644 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006645 else
Stefan Krah99439262010-11-26 12:58:05 +00006646 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006647 }
6648 else
6649 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006650 }
6651 return result;
6652}
6653#endif
6654
6655#ifdef HAVE_CONFSTR
6656static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006657#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006659#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006660#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006662#endif
6663#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006665#endif
Fred Draked86ed291999-12-15 15:34:33 +00006666#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006668#endif
6669#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006671#endif
6672#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006674#endif
6675#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006677#endif
Fred Drakec9680921999-12-13 16:37:25 +00006678#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006680#endif
6681#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006683#endif
6684#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006686#endif
6687#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006689#endif
6690#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006692#endif
6693#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006695#endif
6696#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006698#endif
6699#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006701#endif
Fred Draked86ed291999-12-15 15:34:33 +00006702#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006704#endif
Fred Drakec9680921999-12-13 16:37:25 +00006705#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006707#endif
Fred Draked86ed291999-12-15 15:34:33 +00006708#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006710#endif
6711#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006713#endif
6714#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006716#endif
6717#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006719#endif
Fred Drakec9680921999-12-13 16:37:25 +00006720#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006722#endif
6723#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006725#endif
6726#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006728#endif
6729#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006731#endif
6732#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006734#endif
6735#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006736 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006737#endif
6738#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006740#endif
6741#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006742 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006743#endif
6744#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006746#endif
6747#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006748 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006749#endif
6750#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006751 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006752#endif
6753#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006754 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006755#endif
6756#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006758#endif
6759#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006760 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006761#endif
6762#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006764#endif
6765#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006767#endif
Fred Draked86ed291999-12-15 15:34:33 +00006768#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006769 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006770#endif
6771#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006772 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006773#endif
6774#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006775 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006776#endif
6777#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006779#endif
6780#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006781 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006782#endif
6783#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006784 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006785#endif
6786#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006788#endif
6789#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006791#endif
6792#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006794#endif
6795#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006797#endif
6798#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006799 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006800#endif
6801#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006803#endif
6804#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006806#endif
Fred Drakec9680921999-12-13 16:37:25 +00006807};
6808
6809static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006810conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006811{
6812 return conv_confname(arg, valuep, posix_constants_confstr,
6813 sizeof(posix_constants_confstr)
6814 / sizeof(struct constdef));
6815}
6816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006817PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006818"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006819Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006820
6821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006822posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006823{
6824 PyObject *result = NULL;
6825 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006826 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006827 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006828
Victor Stinnercb043522010-09-10 23:49:04 +00006829 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6830 return NULL;
6831
6832 errno = 0;
6833 len = confstr(name, buffer, sizeof(buffer));
6834 if (len == 0) {
6835 if (errno) {
6836 posix_error();
6837 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006838 }
6839 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006840 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006841 }
6842 }
Victor Stinnercb043522010-09-10 23:49:04 +00006843
6844 if ((unsigned int)len >= sizeof(buffer)) {
6845 char *buf = PyMem_Malloc(len);
6846 if (buf == NULL)
6847 return PyErr_NoMemory();
6848 confstr(name, buf, len);
6849 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6850 PyMem_Free(buf);
6851 }
6852 else
6853 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006854 return result;
6855}
6856#endif
6857
6858
6859#ifdef HAVE_SYSCONF
6860static struct constdef posix_constants_sysconf[] = {
6861#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006863#endif
6864#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
6873#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006875#endif
6876#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006878#endif
6879#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
6882#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
Fred Draked86ed291999-12-15 15:34:33 +00006891#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006893#endif
6894#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006896#endif
Fred Drakec9680921999-12-13 16:37:25 +00006897#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006899#endif
Fred Drakec9680921999-12-13 16:37:25 +00006900#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
6903#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
6909#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006911#endif
6912#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006914#endif
Fred Draked86ed291999-12-15 15:34:33 +00006915#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006917#endif
Fred Drakec9680921999-12-13 16:37:25 +00006918#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006920#endif
6921#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006923#endif
6924#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006926#endif
6927#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006929#endif
6930#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006932#endif
Fred Draked86ed291999-12-15 15:34:33 +00006933#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006935#endif
Fred Drakec9680921999-12-13 16:37:25 +00006936#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006938#endif
6939#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006941#endif
6942#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006944#endif
6945#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006947#endif
6948#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006950#endif
6951#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006953#endif
6954#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006956#endif
6957#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006959#endif
6960#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
6963#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006965#endif
6966#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006968#endif
6969#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
6972#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
6978#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006980#endif
6981#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006983#endif
6984#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006986#endif
6987#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006989#endif
6990#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006992#endif
6993#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006995#endif
6996#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006998#endif
6999#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007001#endif
7002#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
Fred Draked86ed291999-12-15 15:34:33 +00007005#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007007#endif
Fred Drakec9680921999-12-13 16:37:25 +00007008#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
Fred Draked86ed291999-12-15 15:34:33 +00007017#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007019#endif
Fred Drakec9680921999-12-13 16:37:25 +00007020#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
Fred Draked86ed291999-12-15 15:34:33 +00007023#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007025#endif
7026#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007028#endif
Fred Drakec9680921999-12-13 16:37:25 +00007029#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007031#endif
7032#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007034#endif
7035#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007037#endif
7038#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
Fred Draked86ed291999-12-15 15:34:33 +00007041#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007043#endif
Fred Drakec9680921999-12-13 16:37:25 +00007044#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007046#endif
7047#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007049#endif
7050#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
7056#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007058#endif
7059#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
Fred Draked86ed291999-12-15 15:34:33 +00007065#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007067#endif
Fred Drakec9680921999-12-13 16:37:25 +00007068#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007070#endif
7071#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007073#endif
Fred Draked86ed291999-12-15 15:34:33 +00007074#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007076#endif
Fred Drakec9680921999-12-13 16:37:25 +00007077#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007079#endif
7080#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007103#endif
Fred Draked86ed291999-12-15 15:34:33 +00007104#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007106#endif
7107#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007109#endif
Fred Drakec9680921999-12-13 16:37:25 +00007110#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007112#endif
7113#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007115#endif
7116#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007118#endif
7119#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007121#endif
7122#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007124#endif
7125#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
7128#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007130#endif
7131#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007133#endif
7134#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007136#endif
7137#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007139#endif
7140#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007142#endif
7143#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
7146#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
7149#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
7155#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007157#endif
7158#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007160#endif
7161#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
7164#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007166#endif
7167#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007169#endif
7170#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007172#endif
7173#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007175#endif
7176#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007178#endif
7179#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007181#endif
7182#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007184#endif
7185#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
7194#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007196#endif
7197#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
7206#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007208#endif
7209#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
7212#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007214#endif
Fred Draked86ed291999-12-15 15:34:33 +00007215#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007217#endif
Fred Drakec9680921999-12-13 16:37:25 +00007218#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007220#endif
7221#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
7245#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007247#endif
7248#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007250#endif
7251#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007280#endif
7281#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007283#endif
7284#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007286#endif
7287#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007289#endif
7290#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007292#endif
7293#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007295#endif
7296#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007298#endif
7299#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007301#endif
7302#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007303 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007304#endif
7305#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007306 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007307#endif
7308#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007310#endif
7311#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007313#endif
7314#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007316#endif
7317#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007319#endif
7320#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007321 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007322#endif
7323#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007325#endif
7326#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007328#endif
7329#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007331#endif
7332#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007334#endif
7335#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007337#endif
7338#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007339 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007340#endif
7341#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007342 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007343#endif
7344#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007346#endif
7347#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007348 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007349#endif
7350#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007352#endif
7353};
7354
7355static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007356conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007357{
7358 return conv_confname(arg, valuep, posix_constants_sysconf,
7359 sizeof(posix_constants_sysconf)
7360 / sizeof(struct constdef));
7361}
7362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007363PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007364"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007365Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007366
7367static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007368posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007369{
7370 PyObject *result = NULL;
7371 int name;
7372
7373 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7374 int value;
7375
7376 errno = 0;
7377 value = sysconf(name);
7378 if (value == -1 && errno != 0)
7379 posix_error();
7380 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007381 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007382 }
7383 return result;
7384}
7385#endif
7386
7387
Fred Drakebec628d1999-12-15 18:31:10 +00007388/* This code is used to ensure that the tables of configuration value names
7389 * are in sorted order as required by conv_confname(), and also to build the
7390 * the exported dictionaries that are used to publish information about the
7391 * names available on the host platform.
7392 *
7393 * Sorting the table at runtime ensures that the table is properly ordered
7394 * when used, even for platforms we're not able to test on. It also makes
7395 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007396 */
Fred Drakebec628d1999-12-15 18:31:10 +00007397
7398static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007399cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007400{
7401 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007402 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007403 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007404 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007405
7406 return strcmp(c1->name, c2->name);
7407}
7408
7409static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007410setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007412{
Fred Drakebec628d1999-12-15 18:31:10 +00007413 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007414 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007415
7416 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7417 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007418 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007420
Barry Warsaw3155db32000-04-13 15:20:40 +00007421 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007422 PyObject *o = PyLong_FromLong(table[i].value);
7423 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7424 Py_XDECREF(o);
7425 Py_DECREF(d);
7426 return -1;
7427 }
7428 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007429 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007430 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007431}
7432
Fred Drakebec628d1999-12-15 18:31:10 +00007433/* Return -1 on failure, 0 on success. */
7434static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007435setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007436{
7437#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007438 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007439 sizeof(posix_constants_pathconf)
7440 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007441 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007442 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007443#endif
7444#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007445 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007446 sizeof(posix_constants_confstr)
7447 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007448 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007449 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007450#endif
7451#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007452 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007453 sizeof(posix_constants_sysconf)
7454 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007455 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007456 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007457#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007458 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007459}
Fred Draked86ed291999-12-15 15:34:33 +00007460
7461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007462PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007463"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007464Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007465in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007466
7467static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007468posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007469{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007470 abort();
7471 /*NOTREACHED*/
7472 Py_FatalError("abort() called from Python code didn't abort!");
7473 return NULL;
7474}
Fred Drakebec628d1999-12-15 18:31:10 +00007475
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007476#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007477PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007478"startfile(filepath [, operation]) - Start a file with its associated\n\
7479application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007480\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007481When \"operation\" is not specified or \"open\", this acts like\n\
7482double-clicking the file in Explorer, or giving the file name as an\n\
7483argument to the DOS \"start\" command: the file is opened with whatever\n\
7484application (if any) its extension is associated.\n\
7485When another \"operation\" is given, it specifies what should be done with\n\
7486the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007487\n\
7488startfile returns as soon as the associated application is launched.\n\
7489There is no option to wait for the application to close, and no way\n\
7490to retrieve the application's exit status.\n\
7491\n\
7492The filepath is relative to the current directory. If you want to use\n\
7493an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007494the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007495
7496static PyObject *
7497win32_startfile(PyObject *self, PyObject *args)
7498{
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 PyObject *ofilepath;
7500 char *filepath;
7501 char *operation = NULL;
7502 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007503
Victor Stinner8c62be82010-05-06 00:08:46 +00007504 PyObject *unipath, *woperation = NULL;
7505 if (!PyArg_ParseTuple(args, "U|s:startfile",
7506 &unipath, &operation)) {
7507 PyErr_Clear();
7508 goto normal;
7509 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007510
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 if (operation) {
7512 woperation = PyUnicode_DecodeASCII(operation,
7513 strlen(operation), NULL);
7514 if (!woperation) {
7515 PyErr_Clear();
7516 operation = NULL;
7517 goto normal;
7518 }
7519 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007520
Victor Stinner8c62be82010-05-06 00:08:46 +00007521 Py_BEGIN_ALLOW_THREADS
7522 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7523 PyUnicode_AS_UNICODE(unipath),
7524 NULL, NULL, SW_SHOWNORMAL);
7525 Py_END_ALLOW_THREADS
7526
7527 Py_XDECREF(woperation);
7528 if (rc <= (HINSTANCE)32) {
7529 PyObject *errval = win32_error_unicode("startfile",
7530 PyUnicode_AS_UNICODE(unipath));
7531 return errval;
7532 }
7533 Py_INCREF(Py_None);
7534 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007535
7536normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007537 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7538 PyUnicode_FSConverter, &ofilepath,
7539 &operation))
7540 return NULL;
7541 filepath = PyBytes_AsString(ofilepath);
7542 Py_BEGIN_ALLOW_THREADS
7543 rc = ShellExecute((HWND)0, operation, filepath,
7544 NULL, NULL, SW_SHOWNORMAL);
7545 Py_END_ALLOW_THREADS
7546 if (rc <= (HINSTANCE)32) {
7547 PyObject *errval = win32_error("startfile", filepath);
7548 Py_DECREF(ofilepath);
7549 return errval;
7550 }
7551 Py_DECREF(ofilepath);
7552 Py_INCREF(Py_None);
7553 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007554}
7555#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007556
Martin v. Löwis438b5342002-12-27 10:16:42 +00007557#ifdef HAVE_GETLOADAVG
7558PyDoc_STRVAR(posix_getloadavg__doc__,
7559"getloadavg() -> (float, float, float)\n\n\
7560Return the number of processes in the system run queue averaged over\n\
7561the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7562was unobtainable");
7563
7564static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007565posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007566{
7567 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007568 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007569 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7570 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007571 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007572 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007573}
7574#endif
7575
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007576#ifdef MS_WINDOWS
7577
7578PyDoc_STRVAR(win32_urandom__doc__,
7579"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007580Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007581
7582typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7583 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7584 DWORD dwFlags );
7585typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7586 BYTE *pbBuffer );
7587
7588static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007589/* This handle is never explicitly released. Instead, the operating
7590 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007591static HCRYPTPROV hCryptProv = 0;
7592
Tim Peters4ad82172004-08-30 17:02:04 +00007593static PyObject*
7594win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007595{
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 int howMany;
7597 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007598
Victor Stinner8c62be82010-05-06 00:08:46 +00007599 /* Read arguments */
7600 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7601 return NULL;
7602 if (howMany < 0)
7603 return PyErr_Format(PyExc_ValueError,
7604 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007605
Victor Stinner8c62be82010-05-06 00:08:46 +00007606 if (hCryptProv == 0) {
7607 HINSTANCE hAdvAPI32 = NULL;
7608 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007609
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 /* Obtain handle to the DLL containing CryptoAPI
7611 This should not fail */
7612 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7613 if(hAdvAPI32 == NULL)
7614 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007615
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 /* Obtain pointers to the CryptoAPI functions
7617 This will fail on some early versions of Win95 */
7618 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7619 hAdvAPI32,
7620 "CryptAcquireContextA");
7621 if (pCryptAcquireContext == NULL)
7622 return PyErr_Format(PyExc_NotImplementedError,
7623 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007624
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7626 hAdvAPI32, "CryptGenRandom");
7627 if (pCryptGenRandom == NULL)
7628 return PyErr_Format(PyExc_NotImplementedError,
7629 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007630
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 /* Acquire context */
7632 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7633 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7634 return win32_error("CryptAcquireContext", NULL);
7635 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007636
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 /* Allocate bytes */
7638 result = PyBytes_FromStringAndSize(NULL, howMany);
7639 if (result != NULL) {
7640 /* Get random data */
7641 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7642 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7643 PyBytes_AS_STRING(result))) {
7644 Py_DECREF(result);
7645 return win32_error("CryptGenRandom", NULL);
7646 }
7647 }
7648 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007649}
7650#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007651
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007652PyDoc_STRVAR(device_encoding__doc__,
7653"device_encoding(fd) -> str\n\n\
7654Return a string describing the encoding of the device\n\
7655if the output is a terminal; else return None.");
7656
7657static PyObject *
7658device_encoding(PyObject *self, PyObject *args)
7659{
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 int fd;
7661 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7662 return NULL;
7663 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7664 Py_INCREF(Py_None);
7665 return Py_None;
7666 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007667#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007668 if (fd == 0) {
7669 char buf[100];
7670 sprintf(buf, "cp%d", GetConsoleCP());
7671 return PyUnicode_FromString(buf);
7672 }
7673 if (fd == 1 || fd == 2) {
7674 char buf[100];
7675 sprintf(buf, "cp%d", GetConsoleOutputCP());
7676 return PyUnicode_FromString(buf);
7677 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007678#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007679 {
7680 char *codeset = nl_langinfo(CODESET);
7681 if (codeset != NULL && codeset[0] != 0)
7682 return PyUnicode_FromString(codeset);
7683 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007684#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007685 Py_INCREF(Py_None);
7686 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007687}
7688
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007689#ifdef __VMS
7690/* Use openssl random routine */
7691#include <openssl/rand.h>
7692PyDoc_STRVAR(vms_urandom__doc__,
7693"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007694Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007695
7696static PyObject*
7697vms_urandom(PyObject *self, PyObject *args)
7698{
Victor Stinner8c62be82010-05-06 00:08:46 +00007699 int howMany;
7700 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007701
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 /* Read arguments */
7703 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7704 return NULL;
7705 if (howMany < 0)
7706 return PyErr_Format(PyExc_ValueError,
7707 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007708
Victor Stinner8c62be82010-05-06 00:08:46 +00007709 /* Allocate bytes */
7710 result = PyBytes_FromStringAndSize(NULL, howMany);
7711 if (result != NULL) {
7712 /* Get random data */
7713 if (RAND_pseudo_bytes((unsigned char*)
7714 PyBytes_AS_STRING(result),
7715 howMany) < 0) {
7716 Py_DECREF(result);
7717 return PyErr_Format(PyExc_ValueError,
7718 "RAND_pseudo_bytes");
7719 }
7720 }
7721 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007722}
7723#endif
7724
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007725#ifdef HAVE_SETRESUID
7726PyDoc_STRVAR(posix_setresuid__doc__,
7727"setresuid(ruid, euid, suid)\n\n\
7728Set the current process's real, effective, and saved user ids.");
7729
7730static PyObject*
7731posix_setresuid (PyObject *self, PyObject *args)
7732{
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 /* We assume uid_t is no larger than a long. */
7734 long ruid, euid, suid;
7735 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7736 return NULL;
7737 if (setresuid(ruid, euid, suid) < 0)
7738 return posix_error();
7739 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007740}
7741#endif
7742
7743#ifdef HAVE_SETRESGID
7744PyDoc_STRVAR(posix_setresgid__doc__,
7745"setresgid(rgid, egid, sgid)\n\n\
7746Set the current process's real, effective, and saved group ids.");
7747
7748static PyObject*
7749posix_setresgid (PyObject *self, PyObject *args)
7750{
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 /* We assume uid_t is no larger than a long. */
7752 long rgid, egid, sgid;
7753 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7754 return NULL;
7755 if (setresgid(rgid, egid, sgid) < 0)
7756 return posix_error();
7757 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007758}
7759#endif
7760
7761#ifdef HAVE_GETRESUID
7762PyDoc_STRVAR(posix_getresuid__doc__,
7763"getresuid() -> (ruid, euid, suid)\n\n\
7764Get tuple of the current process's real, effective, and saved user ids.");
7765
7766static PyObject*
7767posix_getresuid (PyObject *self, PyObject *noargs)
7768{
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 uid_t ruid, euid, suid;
7770 long l_ruid, l_euid, l_suid;
7771 if (getresuid(&ruid, &euid, &suid) < 0)
7772 return posix_error();
7773 /* Force the values into long's as we don't know the size of uid_t. */
7774 l_ruid = ruid;
7775 l_euid = euid;
7776 l_suid = suid;
7777 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007778}
7779#endif
7780
7781#ifdef HAVE_GETRESGID
7782PyDoc_STRVAR(posix_getresgid__doc__,
7783"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007784Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007785
7786static PyObject*
7787posix_getresgid (PyObject *self, PyObject *noargs)
7788{
Victor Stinner8c62be82010-05-06 00:08:46 +00007789 uid_t rgid, egid, sgid;
7790 long l_rgid, l_egid, l_sgid;
7791 if (getresgid(&rgid, &egid, &sgid) < 0)
7792 return posix_error();
7793 /* Force the values into long's as we don't know the size of uid_t. */
7794 l_rgid = rgid;
7795 l_egid = egid;
7796 l_sgid = sgid;
7797 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007798}
7799#endif
7800
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007801static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007802 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007803#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007805#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007807#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007809#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007810 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007811#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007813#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007814#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007816#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007817#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007819#endif /* HAVE_LCHMOD */
7820#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007821 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007822#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007823#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007824 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007825#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007826#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007828#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007829#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007830 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007831#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007832#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007833 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007834#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007835#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7837 METH_NOARGS, posix_getcwd__doc__},
7838 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7839 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007840#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007841#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007843#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7845 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7846 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007847#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007849#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007850#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007852#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007853#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007854 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007855#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007856 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7857 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7858 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007859 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007860#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007862#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007863#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007864 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007865 win_symlink__doc__},
7866#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007867#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007868 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007869#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007871#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007872 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007873#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7875 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7876 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007877#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007879#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007881#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7883 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007884#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007885#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7887 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007888#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7890 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007891#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007892#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007893#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007894 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007895#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007896#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007898#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007899#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007900 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007901#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007902#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007903 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007904#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007905#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007907#endif /* HAVE_GETEGID */
7908#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007910#endif /* HAVE_GETEUID */
7911#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007913#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007914#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007916#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007917 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007918#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007919 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007920#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007921#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007923#endif /* HAVE_GETPPID */
7924#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007926#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007927#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007929#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007930#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007932#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007933#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007935#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007936#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007938#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007939#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7941 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007942 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007943#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007944#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007946#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007947#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007949#endif /* HAVE_SETEUID */
7950#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007952#endif /* HAVE_SETEGID */
7953#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007955#endif /* HAVE_SETREUID */
7956#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007958#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007959#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007961#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007962#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007964#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007965#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007966 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007967#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007968#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007970#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007971#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007973#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007974#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007976#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007977#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007979#endif /* HAVE_WAIT3 */
7980#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007982#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007983#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007985#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007986#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007988#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007989#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007991#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007992#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007993 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007994#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007995#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007996 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007997#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007998#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008000#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8002 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8003 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8004 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
8005 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8006 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8007 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8008 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8009 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8010 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8011 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008012#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008014#endif
8015#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008017#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008018#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00008019 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008020#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008021#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8023 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8024 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008025#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008026#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008028#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008029#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008031#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008032#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008034#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008035 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008036#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008037 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008038#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008039#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008041#endif
8042#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008044#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008045#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008046#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008047 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008048#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008049#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008051#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008052#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008054#endif /* WIFSTOPPED */
8055#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008057#endif /* WIFSIGNALED */
8058#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008060#endif /* WIFEXITED */
8061#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008062 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008063#endif /* WEXITSTATUS */
8064#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008065 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008066#endif /* WTERMSIG */
8067#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008068 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008069#endif /* WSTOPSIG */
8070#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008071#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008073#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008074#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008075 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008076#endif
Fred Drakec9680921999-12-13 16:37:25 +00008077#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008079#endif
8080#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
8083#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008089 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008090#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008091 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008092 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008093 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin9c669cc2011-06-08 18:17:18 -05008094 {"_isdir", posix__isdir, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008095#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008096#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008098#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008099 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008101 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008102 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008104 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008105#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008107#endif
8108#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008110#endif
8111#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008113#endif
8114#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008116#endif
8117
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008119};
8120
8121
Barry Warsaw4a342091996-12-19 23:50:02 +00008122static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008123ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008124{
Victor Stinner8c62be82010-05-06 00:08:46 +00008125 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008126}
8127
Guido van Rossumd48f2521997-12-05 22:19:34 +00008128#if defined(PYOS_OS2)
8129/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008130static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008131{
8132 APIRET rc;
8133 ULONG values[QSV_MAX+1];
8134 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008135 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008136
8137 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008138 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008139 Py_END_ALLOW_THREADS
8140
8141 if (rc != NO_ERROR) {
8142 os2_error(rc);
8143 return -1;
8144 }
8145
Fred Drake4d1e64b2002-04-15 19:40:07 +00008146 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8147 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8148 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8149 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8150 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8151 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8152 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008153
8154 switch (values[QSV_VERSION_MINOR]) {
8155 case 0: ver = "2.00"; break;
8156 case 10: ver = "2.10"; break;
8157 case 11: ver = "2.11"; break;
8158 case 30: ver = "3.00"; break;
8159 case 40: ver = "4.00"; break;
8160 case 50: ver = "5.00"; break;
8161 default:
Tim Peters885d4572001-11-28 20:27:42 +00008162 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008163 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008164 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008165 ver = &tmp[0];
8166 }
8167
8168 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008169 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008170 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008171
8172 /* Add Indicator of Which Drive was Used to Boot the System */
8173 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8174 tmp[1] = ':';
8175 tmp[2] = '\0';
8176
Fred Drake4d1e64b2002-04-15 19:40:07 +00008177 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008178}
8179#endif
8180
Brian Curtin52173d42010-12-02 18:29:18 +00008181#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008182static int
Brian Curtin52173d42010-12-02 18:29:18 +00008183enable_symlink()
8184{
8185 HANDLE tok;
8186 TOKEN_PRIVILEGES tok_priv;
8187 LUID luid;
8188 int meth_idx = 0;
8189
8190 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008191 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008192
8193 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008194 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008195
8196 tok_priv.PrivilegeCount = 1;
8197 tok_priv.Privileges[0].Luid = luid;
8198 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8199
8200 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8201 sizeof(TOKEN_PRIVILEGES),
8202 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008203 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008204
Brian Curtin3b4499c2010-12-28 14:31:47 +00008205 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8206 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008207}
8208#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8209
Barry Warsaw4a342091996-12-19 23:50:02 +00008210static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008211all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008212{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008213#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008215#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008216#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008218#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008219#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008221#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008222#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008224#endif
Fred Drakec9680921999-12-13 16:37:25 +00008225#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008227#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008228#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008230#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008231#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008233#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008234#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008236#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008237#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008239#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008240#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008242#endif
8243#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008245#endif
8246#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008248#endif
8249#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008251#endif
8252#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008253 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008254#endif
8255#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008256 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008257#endif
8258#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008260#endif
8261#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008263#endif
8264#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008266#endif
8267#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008269#endif
8270#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008272#endif
8273#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008275#endif
8276#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008278#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008279#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008281#endif
8282#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008284#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008285#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008287#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008288#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008290#endif
8291#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008293#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008294
Tim Peters5aa91602002-01-30 05:46:57 +00008295/* MS Windows */
8296#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008297 /* Don't inherit in child processes. */
8298 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008299#endif
8300#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008301 /* Optimize for short life (keep in memory). */
8302 /* MS forgot to define this one with a non-underscore form too. */
8303 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008304#endif
8305#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008306 /* Automatically delete when last handle is closed. */
8307 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008308#endif
8309#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 /* Optimize for random access. */
8311 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008312#endif
8313#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008314 /* Optimize for sequential access. */
8315 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008316#endif
8317
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008318/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008319#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008320 /* Send a SIGIO signal whenever input or output
8321 becomes available on file descriptor */
8322 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008323#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008324#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008325 /* Direct disk access. */
8326 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008327#endif
8328#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008329 /* Must be a directory. */
8330 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008331#endif
8332#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 /* Do not follow links. */
8334 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008335#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008336#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 /* Do not update the access time. */
8338 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008339#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008340
Victor Stinner8c62be82010-05-06 00:08:46 +00008341 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008342#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008343 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008344#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008345#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008347#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008348#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008349 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008350#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008351#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008353#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008354#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008355 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008356#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008357#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008358 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008359#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008360#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008361 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008362#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008363#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008364 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008365#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008366#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008367 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008368#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008369#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008371#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008372#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008373 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008374#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008375#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008376 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008377#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008378#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008379 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008380#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008381#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008382 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008383#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008384#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008386#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008387#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008389#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008390#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008391 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008392#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008393
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008394 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008395#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008396 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008397#endif /* ST_RDONLY */
8398#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008399 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008400#endif /* ST_NOSUID */
8401
Guido van Rossum246bc171999-02-01 23:54:31 +00008402#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008403#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008404 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8405 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8406 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8407 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8408 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8409 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8410 if (ins(d, "P_PM", (long)P_PM)) return -1;
8411 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8412 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8413 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8414 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8415 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8416 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8417 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8418 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8419 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8420 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8421 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8422 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8423 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008424#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008425 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8426 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8427 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8428 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8429 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008430#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008431#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008432
Guido van Rossumd48f2521997-12-05 22:19:34 +00008433#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008434 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008435#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008436 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008437}
8438
8439
Tim Peters5aa91602002-01-30 05:46:57 +00008440#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008441#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008442#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008443
8444#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008445#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008446#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008447
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008448#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008449#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008450#define MODNAME "posix"
8451#endif
8452
Martin v. Löwis1a214512008-06-11 05:26:20 +00008453static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008454 PyModuleDef_HEAD_INIT,
8455 MODNAME,
8456 posix__doc__,
8457 -1,
8458 posix_methods,
8459 NULL,
8460 NULL,
8461 NULL,
8462 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008463};
8464
8465
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008466PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008467INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008468{
Victor Stinner8c62be82010-05-06 00:08:46 +00008469 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008470
Brian Curtin52173d42010-12-02 18:29:18 +00008471#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008472 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008473#endif
8474
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 m = PyModule_Create(&posixmodule);
8476 if (m == NULL)
8477 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008478
Victor Stinner8c62be82010-05-06 00:08:46 +00008479 /* Initialize environ dictionary */
8480 v = convertenviron();
8481 Py_XINCREF(v);
8482 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8483 return NULL;
8484 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008485
Victor Stinner8c62be82010-05-06 00:08:46 +00008486 if (all_ins(m))
8487 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008488
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 if (setup_confname_tables(m))
8490 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008491
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 Py_INCREF(PyExc_OSError);
8493 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008494
Guido van Rossumb3d39562000-01-31 18:41:26 +00008495#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008496 if (posix_putenv_garbage == NULL)
8497 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008498#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008499
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 if (!initialized) {
8501 stat_result_desc.name = MODNAME ".stat_result";
8502 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8503 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8504 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8505 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8506 structseq_new = StatResultType.tp_new;
8507 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008508
Victor Stinner8c62be82010-05-06 00:08:46 +00008509 statvfs_result_desc.name = MODNAME ".statvfs_result";
8510 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008511#ifdef NEED_TICKS_PER_SECOND
8512# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008513 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008514# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008516# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008517 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008518# endif
8519#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008520 }
8521 Py_INCREF((PyObject*) &StatResultType);
8522 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8523 Py_INCREF((PyObject*) &StatVFSResultType);
8524 PyModule_AddObject(m, "statvfs_result",
8525 (PyObject*) &StatVFSResultType);
8526 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008527
8528#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008529 /*
8530 * Step 2 of weak-linking support on Mac OS X.
8531 *
8532 * The code below removes functions that are not available on the
8533 * currently active platform.
8534 *
8535 * This block allow one to use a python binary that was build on
8536 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8537 * OSX 10.4.
8538 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008539#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008540 if (fstatvfs == NULL) {
8541 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8542 return NULL;
8543 }
8544 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008545#endif /* HAVE_FSTATVFS */
8546
8547#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008548 if (statvfs == NULL) {
8549 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8550 return NULL;
8551 }
8552 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008553#endif /* HAVE_STATVFS */
8554
8555# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008556 if (lchown == NULL) {
8557 if (PyObject_DelAttrString(m, "lchown") == -1) {
8558 return NULL;
8559 }
8560 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008561#endif /* HAVE_LCHOWN */
8562
8563
8564#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008565 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008566
Guido van Rossumb6775db1994-08-01 11:34:53 +00008567}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008568
8569#ifdef __cplusplus
8570}
8571#endif