blob: eb2e412ebcfc4828bb3b874b9f4a21ea4f439c3d [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
Brian Curtin95d028f2011-06-09 09:10:38 -05002823PyDoc_STRVAR(posix__isdir__doc__,
2824"Return true if the pathname refers to an existing directory.");
2825
Brian Curtin9c669cc2011-06-08 18:17:18 -05002826static PyObject *
2827posix__isdir(PyObject *self, PyObject *args)
2828{
2829 PyObject *opath;
2830 char *path;
2831 PyUnicodeObject *po;
2832 DWORD attributes;
2833
2834 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
2835 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2836
2837 attributes = GetFileAttributesW(wpath);
2838 if (attributes == INVALID_FILE_ATTRIBUTES)
2839 Py_RETURN_FALSE;
2840 goto check;
2841 }
2842 /* Drop the argument parsing error as narrow strings
2843 are also valid. */
2844 PyErr_Clear();
2845
2846 if (!PyArg_ParseTuple(args, "O&:_isdir",
2847 PyUnicode_FSConverter, &opath))
2848 return NULL;
2849
2850 path = PyBytes_AsString(opath);
2851 attributes = GetFileAttributesA(path);
2852 if (attributes == INVALID_FILE_ATTRIBUTES)
2853 Py_RETURN_FALSE;
2854
2855check:
2856 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
2857 Py_RETURN_TRUE;
2858 else
2859 Py_RETURN_FALSE;
2860}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002861#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002863PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002864"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002865Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002866
Barry Warsaw53699e91996-12-10 23:23:01 +00002867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002868posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002869{
Victor Stinner8c62be82010-05-06 00:08:46 +00002870 int res;
2871 PyObject *opath;
2872 char *path;
2873 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002874
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002875#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002876 PyUnicodeObject *po;
2877 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2878 Py_BEGIN_ALLOW_THREADS
2879 /* PyUnicode_AS_UNICODE OK without thread lock as
2880 it is a simple dereference. */
2881 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2882 Py_END_ALLOW_THREADS
2883 if (!res)
2884 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2885 Py_INCREF(Py_None);
2886 return Py_None;
2887 }
2888 /* Drop the argument parsing error as narrow strings
2889 are also valid. */
2890 PyErr_Clear();
2891 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2892 PyUnicode_FSConverter, &opath, &mode))
2893 return NULL;
2894 path = PyBytes_AsString(opath);
2895 Py_BEGIN_ALLOW_THREADS
2896 /* PyUnicode_AS_UNICODE OK without thread lock as
2897 it is a simple dereference. */
2898 res = CreateDirectoryA(path, NULL);
2899 Py_END_ALLOW_THREADS
2900 if (!res) {
2901 win32_error("mkdir", path);
2902 Py_DECREF(opath);
2903 return NULL;
2904 }
2905 Py_DECREF(opath);
2906 Py_INCREF(Py_None);
2907 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002908#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002909
Victor Stinner8c62be82010-05-06 00:08:46 +00002910 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2911 PyUnicode_FSConverter, &opath, &mode))
2912 return NULL;
2913 path = PyBytes_AsString(opath);
2914 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002915#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002916 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002917#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002918 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002919#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002920 Py_END_ALLOW_THREADS
2921 if (res < 0)
2922 return posix_error_with_allocated_filename(opath);
2923 Py_DECREF(opath);
2924 Py_INCREF(Py_None);
2925 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002926#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002927}
2928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002929
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002930/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2931#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002932#include <sys/resource.h>
2933#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002934
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002935
2936#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002937PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002938"nice(inc) -> new_priority\n\n\
2939Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002940
Barry Warsaw53699e91996-12-10 23:23:01 +00002941static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002942posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002943{
Victor Stinner8c62be82010-05-06 00:08:46 +00002944 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002945
Victor Stinner8c62be82010-05-06 00:08:46 +00002946 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2947 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002948
Victor Stinner8c62be82010-05-06 00:08:46 +00002949 /* There are two flavours of 'nice': one that returns the new
2950 priority (as required by almost all standards out there) and the
2951 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2952 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002953
Victor Stinner8c62be82010-05-06 00:08:46 +00002954 If we are of the nice family that returns the new priority, we
2955 need to clear errno before the call, and check if errno is filled
2956 before calling posix_error() on a returnvalue of -1, because the
2957 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002958
Victor Stinner8c62be82010-05-06 00:08:46 +00002959 errno = 0;
2960 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002961#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002962 if (value == 0)
2963 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002964#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002965 if (value == -1 && errno != 0)
2966 /* either nice() or getpriority() returned an error */
2967 return posix_error();
2968 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002969}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002970#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002972PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002973"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002975
Barry Warsaw53699e91996-12-10 23:23:01 +00002976static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002977posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002978{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002979#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002980 PyObject *o1, *o2;
2981 char *p1, *p2;
2982 BOOL result;
2983 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2984 goto error;
2985 if (!convert_to_unicode(&o1))
2986 goto error;
2987 if (!convert_to_unicode(&o2)) {
2988 Py_DECREF(o1);
2989 goto error;
2990 }
2991 Py_BEGIN_ALLOW_THREADS
2992 result = MoveFileW(PyUnicode_AsUnicode(o1),
2993 PyUnicode_AsUnicode(o2));
2994 Py_END_ALLOW_THREADS
2995 Py_DECREF(o1);
2996 Py_DECREF(o2);
2997 if (!result)
2998 return win32_error("rename", NULL);
2999 Py_INCREF(Py_None);
3000 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003001error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003002 PyErr_Clear();
3003 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
3004 return NULL;
3005 Py_BEGIN_ALLOW_THREADS
3006 result = MoveFileA(p1, p2);
3007 Py_END_ALLOW_THREADS
3008 if (!result)
3009 return win32_error("rename", NULL);
3010 Py_INCREF(Py_None);
3011 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003012#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003013 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003014#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003015}
3016
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003018PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003019"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003020Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003021
Barry Warsaw53699e91996-12-10 23:23:01 +00003022static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003023posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003024{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003025#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003026 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003027#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003028 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003029#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003030}
3031
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003033PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003034"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003035Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003036
Barry Warsaw53699e91996-12-10 23:23:01 +00003037static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003038posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003039{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003040#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003041 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003042#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003043 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003044#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003045}
3046
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003047
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003048#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003049PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003050"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003051Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003052
Barry Warsaw53699e91996-12-10 23:23:01 +00003053static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003054posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003055{
Victor Stinner8c62be82010-05-06 00:08:46 +00003056 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003057#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003058 wchar_t *command;
3059 if (!PyArg_ParseTuple(args, "u:system", &command))
3060 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003061
Victor Stinner8c62be82010-05-06 00:08:46 +00003062 Py_BEGIN_ALLOW_THREADS
3063 sts = _wsystem(command);
3064 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003065#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003066 PyObject *command_obj;
3067 char *command;
3068 if (!PyArg_ParseTuple(args, "O&:system",
3069 PyUnicode_FSConverter, &command_obj))
3070 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003071
Victor Stinner8c62be82010-05-06 00:08:46 +00003072 command = PyBytes_AsString(command_obj);
3073 Py_BEGIN_ALLOW_THREADS
3074 sts = system(command);
3075 Py_END_ALLOW_THREADS
3076 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003077#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003078 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003079}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003080#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003081
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003082
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003083PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003084"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003085Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003086
Barry Warsaw53699e91996-12-10 23:23:01 +00003087static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003088posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003089{
Victor Stinner8c62be82010-05-06 00:08:46 +00003090 int i;
3091 if (!PyArg_ParseTuple(args, "i:umask", &i))
3092 return NULL;
3093 i = (int)umask(i);
3094 if (i < 0)
3095 return posix_error();
3096 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003097}
3098
Brian Curtind40e6f72010-07-08 21:39:08 +00003099#ifdef MS_WINDOWS
3100
3101/* override the default DeleteFileW behavior so that directory
3102symlinks can be removed with this function, the same as with
3103Unix symlinks */
3104BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3105{
3106 WIN32_FILE_ATTRIBUTE_DATA info;
3107 WIN32_FIND_DATAW find_data;
3108 HANDLE find_data_handle;
3109 int is_directory = 0;
3110 int is_link = 0;
3111
3112 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3113 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3114
3115 /* Get WIN32_FIND_DATA structure for the path to determine if
3116 it is a symlink */
3117 if(is_directory &&
3118 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3119 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3120
3121 if(find_data_handle != INVALID_HANDLE_VALUE) {
3122 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3123 FindClose(find_data_handle);
3124 }
3125 }
3126 }
3127
3128 if (is_directory && is_link)
3129 return RemoveDirectoryW(lpFileName);
3130
3131 return DeleteFileW(lpFileName);
3132}
3133#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003135PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003136"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003137Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003139PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003140"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003141Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003142
Barry Warsaw53699e91996-12-10 23:23:01 +00003143static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003144posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003145{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003146#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003147 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3148 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003149#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003150 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003151#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003152}
3153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003154
Guido van Rossumb6775db1994-08-01 11:34:53 +00003155#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003156PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003157"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003158Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003159
Barry Warsaw53699e91996-12-10 23:23:01 +00003160static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003161posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003162{
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 struct utsname u;
3164 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003165
Victor Stinner8c62be82010-05-06 00:08:46 +00003166 Py_BEGIN_ALLOW_THREADS
3167 res = uname(&u);
3168 Py_END_ALLOW_THREADS
3169 if (res < 0)
3170 return posix_error();
3171 return Py_BuildValue("(sssss)",
3172 u.sysname,
3173 u.nodename,
3174 u.release,
3175 u.version,
3176 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003177}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003178#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003179
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003180static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003181extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003182{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003183 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003184 if (PyFloat_Check(t)) {
3185 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003186 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003187 if (!intobj)
3188 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003189#if SIZEOF_TIME_T > SIZEOF_LONG
3190 intval = PyLong_AsUnsignedLongLongMask(intobj);
3191#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003192 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003193#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 Py_DECREF(intobj);
3195 if (intval == -1 && PyErr_Occurred())
3196 return -1;
3197 *sec = intval;
3198 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3199 if (*usec < 0)
3200 /* If rounding gave us a negative number,
3201 truncate. */
3202 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003203 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003204 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003205#if SIZEOF_TIME_T > SIZEOF_LONG
3206 intval = PyLong_AsUnsignedLongLongMask(t);
3207#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003208 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003209#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003210 if (intval == -1 && PyErr_Occurred())
3211 return -1;
3212 *sec = intval;
3213 *usec = 0;
3214 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003215}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003217PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003218"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003219utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003220Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003221second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222
Barry Warsaw53699e91996-12-10 23:23:01 +00003223static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003224posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003225{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003226#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003227 PyObject *arg;
3228 PyUnicodeObject *obwpath;
3229 wchar_t *wpath = NULL;
3230 PyObject *oapath;
3231 char *apath;
3232 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003233 time_t atimesec, mtimesec;
3234 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003235 FILETIME atime, mtime;
3236 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003237
Victor Stinner8c62be82010-05-06 00:08:46 +00003238 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3239 wpath = PyUnicode_AS_UNICODE(obwpath);
3240 Py_BEGIN_ALLOW_THREADS
3241 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3242 NULL, OPEN_EXISTING,
3243 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3244 Py_END_ALLOW_THREADS
3245 if (hFile == INVALID_HANDLE_VALUE)
3246 return win32_error_unicode("utime", wpath);
3247 } else
3248 /* Drop the argument parsing error as narrow strings
3249 are also valid. */
3250 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003251
Victor Stinner8c62be82010-05-06 00:08:46 +00003252 if (!wpath) {
3253 if (!PyArg_ParseTuple(args, "O&O:utime",
3254 PyUnicode_FSConverter, &oapath, &arg))
3255 return NULL;
3256 apath = PyBytes_AsString(oapath);
3257 Py_BEGIN_ALLOW_THREADS
3258 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3259 NULL, OPEN_EXISTING,
3260 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3261 Py_END_ALLOW_THREADS
3262 if (hFile == INVALID_HANDLE_VALUE) {
3263 win32_error("utime", apath);
3264 Py_DECREF(oapath);
3265 return NULL;
3266 }
3267 Py_DECREF(oapath);
3268 }
3269
3270 if (arg == Py_None) {
3271 SYSTEMTIME now;
3272 GetSystemTime(&now);
3273 if (!SystemTimeToFileTime(&now, &mtime) ||
3274 !SystemTimeToFileTime(&now, &atime)) {
3275 win32_error("utime", NULL);
3276 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003277 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003278 }
3279 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3280 PyErr_SetString(PyExc_TypeError,
3281 "utime() arg 2 must be a tuple (atime, mtime)");
3282 goto done;
3283 }
3284 else {
3285 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3286 &atimesec, &ausec) == -1)
3287 goto done;
3288 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3289 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3290 &mtimesec, &musec) == -1)
3291 goto done;
3292 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3293 }
3294 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3295 /* Avoid putting the file name into the error here,
3296 as that may confuse the user into believing that
3297 something is wrong with the file, when it also
3298 could be the time stamp that gives a problem. */
3299 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003300 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003301 }
3302 Py_INCREF(Py_None);
3303 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003304done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003305 CloseHandle(hFile);
3306 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003307#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003308
Victor Stinner8c62be82010-05-06 00:08:46 +00003309 PyObject *opath;
3310 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003311 time_t atime, mtime;
3312 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003313 int res;
3314 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003315
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003316#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003317 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003318#define ATIME buf[0].tv_sec
3319#define MTIME buf[1].tv_sec
3320#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003321/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003323#define ATIME buf.actime
3324#define MTIME buf.modtime
3325#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003326#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003327 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003328#define ATIME buf[0]
3329#define MTIME buf[1]
3330#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003331#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003332
Mark Hammond817c9292003-12-03 01:22:38 +00003333
Victor Stinner8c62be82010-05-06 00:08:46 +00003334 if (!PyArg_ParseTuple(args, "O&O:utime",
3335 PyUnicode_FSConverter, &opath, &arg))
3336 return NULL;
3337 path = PyBytes_AsString(opath);
3338 if (arg == Py_None) {
3339 /* optional time values not given */
3340 Py_BEGIN_ALLOW_THREADS
3341 res = utime(path, NULL);
3342 Py_END_ALLOW_THREADS
3343 }
3344 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3345 PyErr_SetString(PyExc_TypeError,
3346 "utime() arg 2 must be a tuple (atime, mtime)");
3347 Py_DECREF(opath);
3348 return NULL;
3349 }
3350 else {
3351 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3352 &atime, &ausec) == -1) {
3353 Py_DECREF(opath);
3354 return NULL;
3355 }
3356 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3357 &mtime, &musec) == -1) {
3358 Py_DECREF(opath);
3359 return NULL;
3360 }
3361 ATIME = atime;
3362 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003363#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003364 buf[0].tv_usec = ausec;
3365 buf[1].tv_usec = musec;
3366 Py_BEGIN_ALLOW_THREADS
3367 res = utimes(path, buf);
3368 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003369#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003370 Py_BEGIN_ALLOW_THREADS
3371 res = utime(path, UTIME_ARG);
3372 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003373#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003374 }
3375 if (res < 0) {
3376 return posix_error_with_allocated_filename(opath);
3377 }
3378 Py_DECREF(opath);
3379 Py_INCREF(Py_None);
3380 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003381#undef UTIME_ARG
3382#undef ATIME
3383#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003384#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003385}
3386
Guido van Rossum85e3b011991-06-03 12:42:10 +00003387
Guido van Rossum3b066191991-06-04 19:40:25 +00003388/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003390PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003391"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003392Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003393
Barry Warsaw53699e91996-12-10 23:23:01 +00003394static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003395posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003396{
Victor Stinner8c62be82010-05-06 00:08:46 +00003397 int sts;
3398 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3399 return NULL;
3400 _exit(sts);
3401 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003402}
3403
Martin v. Löwis114619e2002-10-07 06:44:21 +00003404#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3405static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003406free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003407{
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 Py_ssize_t i;
3409 for (i = 0; i < count; i++)
3410 PyMem_Free(array[i]);
3411 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003412}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003413
Antoine Pitrou69f71142009-05-24 21:25:49 +00003414static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003415int fsconvert_strdup(PyObject *o, char**out)
3416{
Victor Stinner8c62be82010-05-06 00:08:46 +00003417 PyObject *bytes;
3418 Py_ssize_t size;
3419 if (!PyUnicode_FSConverter(o, &bytes))
3420 return 0;
3421 size = PyBytes_GET_SIZE(bytes);
3422 *out = PyMem_Malloc(size+1);
3423 if (!*out)
3424 return 0;
3425 memcpy(*out, PyBytes_AsString(bytes), size+1);
3426 Py_DECREF(bytes);
3427 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003428}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003429#endif
3430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003431
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003432#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003433PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003434"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003435Execute an executable path with arguments, replacing current process.\n\
3436\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003437 path: path of executable file\n\
3438 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003439
Barry Warsaw53699e91996-12-10 23:23:01 +00003440static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003441posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003442{
Victor Stinner8c62be82010-05-06 00:08:46 +00003443 PyObject *opath;
3444 char *path;
3445 PyObject *argv;
3446 char **argvlist;
3447 Py_ssize_t i, argc;
3448 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003449
Victor Stinner8c62be82010-05-06 00:08:46 +00003450 /* execv has two arguments: (path, argv), where
3451 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003452
Victor Stinner8c62be82010-05-06 00:08:46 +00003453 if (!PyArg_ParseTuple(args, "O&O:execv",
3454 PyUnicode_FSConverter,
3455 &opath, &argv))
3456 return NULL;
3457 path = PyBytes_AsString(opath);
3458 if (PyList_Check(argv)) {
3459 argc = PyList_Size(argv);
3460 getitem = PyList_GetItem;
3461 }
3462 else if (PyTuple_Check(argv)) {
3463 argc = PyTuple_Size(argv);
3464 getitem = PyTuple_GetItem;
3465 }
3466 else {
3467 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3468 Py_DECREF(opath);
3469 return NULL;
3470 }
3471 if (argc < 1) {
3472 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3473 Py_DECREF(opath);
3474 return NULL;
3475 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003476
Victor Stinner8c62be82010-05-06 00:08:46 +00003477 argvlist = PyMem_NEW(char *, argc+1);
3478 if (argvlist == NULL) {
3479 Py_DECREF(opath);
3480 return PyErr_NoMemory();
3481 }
3482 for (i = 0; i < argc; i++) {
3483 if (!fsconvert_strdup((*getitem)(argv, i),
3484 &argvlist[i])) {
3485 free_string_array(argvlist, i);
3486 PyErr_SetString(PyExc_TypeError,
3487 "execv() arg 2 must contain only strings");
3488 Py_DECREF(opath);
3489 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003490
Victor Stinner8c62be82010-05-06 00:08:46 +00003491 }
3492 }
3493 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003494
Victor Stinner8c62be82010-05-06 00:08:46 +00003495 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003496
Victor Stinner8c62be82010-05-06 00:08:46 +00003497 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003498
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 free_string_array(argvlist, argc);
3500 Py_DECREF(opath);
3501 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003502}
3503
Victor Stinner13bb71c2010-04-23 21:41:56 +00003504static char**
3505parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3506{
Victor Stinner8c62be82010-05-06 00:08:46 +00003507 char **envlist;
3508 Py_ssize_t i, pos, envc;
3509 PyObject *keys=NULL, *vals=NULL;
3510 PyObject *key, *val, *key2, *val2;
3511 char *p, *k, *v;
3512 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003513
Victor Stinner8c62be82010-05-06 00:08:46 +00003514 i = PyMapping_Size(env);
3515 if (i < 0)
3516 return NULL;
3517 envlist = PyMem_NEW(char *, i + 1);
3518 if (envlist == NULL) {
3519 PyErr_NoMemory();
3520 return NULL;
3521 }
3522 envc = 0;
3523 keys = PyMapping_Keys(env);
3524 vals = PyMapping_Values(env);
3525 if (!keys || !vals)
3526 goto error;
3527 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3528 PyErr_Format(PyExc_TypeError,
3529 "env.keys() or env.values() is not a list");
3530 goto error;
3531 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003532
Victor Stinner8c62be82010-05-06 00:08:46 +00003533 for (pos = 0; pos < i; pos++) {
3534 key = PyList_GetItem(keys, pos);
3535 val = PyList_GetItem(vals, pos);
3536 if (!key || !val)
3537 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003538
Victor Stinner8c62be82010-05-06 00:08:46 +00003539 if (PyUnicode_FSConverter(key, &key2) == 0)
3540 goto error;
3541 if (PyUnicode_FSConverter(val, &val2) == 0) {
3542 Py_DECREF(key2);
3543 goto error;
3544 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003545
3546#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3548 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003549#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003550 k = PyBytes_AsString(key2);
3551 v = PyBytes_AsString(val2);
3552 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003553
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 p = PyMem_NEW(char, len);
3555 if (p == NULL) {
3556 PyErr_NoMemory();
3557 Py_DECREF(key2);
3558 Py_DECREF(val2);
3559 goto error;
3560 }
3561 PyOS_snprintf(p, len, "%s=%s", k, v);
3562 envlist[envc++] = p;
3563 Py_DECREF(key2);
3564 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003565#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003566 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003567#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003568 }
3569 Py_DECREF(vals);
3570 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003571
Victor Stinner8c62be82010-05-06 00:08:46 +00003572 envlist[envc] = 0;
3573 *envc_ptr = envc;
3574 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003575
3576error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003577 Py_XDECREF(keys);
3578 Py_XDECREF(vals);
3579 while (--envc >= 0)
3580 PyMem_DEL(envlist[envc]);
3581 PyMem_DEL(envlist);
3582 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003583}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003585PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003586"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003587Execute a path with arguments and environment, replacing current process.\n\
3588\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003589 path: path of executable file\n\
3590 args: tuple or list of arguments\n\
3591 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003592
Barry Warsaw53699e91996-12-10 23:23:01 +00003593static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003594posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003595{
Victor Stinner8c62be82010-05-06 00:08:46 +00003596 PyObject *opath;
3597 char *path;
3598 PyObject *argv, *env;
3599 char **argvlist;
3600 char **envlist;
3601 Py_ssize_t i, argc, envc;
3602 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3603 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003604
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 /* execve has three arguments: (path, argv, env), where
3606 argv is a list or tuple of strings and env is a dictionary
3607 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003608
Victor Stinner8c62be82010-05-06 00:08:46 +00003609 if (!PyArg_ParseTuple(args, "O&OO:execve",
3610 PyUnicode_FSConverter,
3611 &opath, &argv, &env))
3612 return NULL;
3613 path = PyBytes_AsString(opath);
3614 if (PyList_Check(argv)) {
3615 argc = PyList_Size(argv);
3616 getitem = PyList_GetItem;
3617 }
3618 else if (PyTuple_Check(argv)) {
3619 argc = PyTuple_Size(argv);
3620 getitem = PyTuple_GetItem;
3621 }
3622 else {
3623 PyErr_SetString(PyExc_TypeError,
3624 "execve() arg 2 must be a tuple or list");
3625 goto fail_0;
3626 }
3627 if (!PyMapping_Check(env)) {
3628 PyErr_SetString(PyExc_TypeError,
3629 "execve() arg 3 must be a mapping object");
3630 goto fail_0;
3631 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003632
Victor Stinner8c62be82010-05-06 00:08:46 +00003633 argvlist = PyMem_NEW(char *, argc+1);
3634 if (argvlist == NULL) {
3635 PyErr_NoMemory();
3636 goto fail_0;
3637 }
3638 for (i = 0; i < argc; i++) {
3639 if (!fsconvert_strdup((*getitem)(argv, i),
3640 &argvlist[i]))
3641 {
3642 lastarg = i;
3643 goto fail_1;
3644 }
3645 }
3646 lastarg = argc;
3647 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003648
Victor Stinner8c62be82010-05-06 00:08:46 +00003649 envlist = parse_envlist(env, &envc);
3650 if (envlist == NULL)
3651 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003652
Victor Stinner8c62be82010-05-06 00:08:46 +00003653 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003654
Victor Stinner8c62be82010-05-06 00:08:46 +00003655 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003656
Victor Stinner8c62be82010-05-06 00:08:46 +00003657 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003658
Victor Stinner8c62be82010-05-06 00:08:46 +00003659 while (--envc >= 0)
3660 PyMem_DEL(envlist[envc]);
3661 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003662 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003663 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003664 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003665 Py_DECREF(opath);
3666 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003667}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003668#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003669
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003670
Guido van Rossuma1065681999-01-25 23:20:23 +00003671#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003672PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003673"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003674Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003675\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003676 mode: mode of process creation\n\
3677 path: path of executable file\n\
3678 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003679
3680static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003681posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003682{
Victor Stinner8c62be82010-05-06 00:08:46 +00003683 PyObject *opath;
3684 char *path;
3685 PyObject *argv;
3686 char **argvlist;
3687 int mode, i;
3688 Py_ssize_t argc;
3689 Py_intptr_t spawnval;
3690 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003691
Victor Stinner8c62be82010-05-06 00:08:46 +00003692 /* spawnv has three arguments: (mode, path, argv), where
3693 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003694
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3696 PyUnicode_FSConverter,
3697 &opath, &argv))
3698 return NULL;
3699 path = PyBytes_AsString(opath);
3700 if (PyList_Check(argv)) {
3701 argc = PyList_Size(argv);
3702 getitem = PyList_GetItem;
3703 }
3704 else if (PyTuple_Check(argv)) {
3705 argc = PyTuple_Size(argv);
3706 getitem = PyTuple_GetItem;
3707 }
3708 else {
3709 PyErr_SetString(PyExc_TypeError,
3710 "spawnv() arg 2 must be a tuple or list");
3711 Py_DECREF(opath);
3712 return NULL;
3713 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003714
Victor Stinner8c62be82010-05-06 00:08:46 +00003715 argvlist = PyMem_NEW(char *, argc+1);
3716 if (argvlist == NULL) {
3717 Py_DECREF(opath);
3718 return PyErr_NoMemory();
3719 }
3720 for (i = 0; i < argc; i++) {
3721 if (!fsconvert_strdup((*getitem)(argv, i),
3722 &argvlist[i])) {
3723 free_string_array(argvlist, i);
3724 PyErr_SetString(
3725 PyExc_TypeError,
3726 "spawnv() arg 2 must contain only strings");
3727 Py_DECREF(opath);
3728 return NULL;
3729 }
3730 }
3731 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003732
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003733#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003734 Py_BEGIN_ALLOW_THREADS
3735 spawnval = spawnv(mode, path, argvlist);
3736 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003737#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 if (mode == _OLD_P_OVERLAY)
3739 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003740
Victor Stinner8c62be82010-05-06 00:08:46 +00003741 Py_BEGIN_ALLOW_THREADS
3742 spawnval = _spawnv(mode, path, argvlist);
3743 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003744#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003745
Victor Stinner8c62be82010-05-06 00:08:46 +00003746 free_string_array(argvlist, argc);
3747 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003748
Victor Stinner8c62be82010-05-06 00:08:46 +00003749 if (spawnval == -1)
3750 return posix_error();
3751 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003752#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003753 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003754#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003755 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003756#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003757}
3758
3759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003760PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003761"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003762Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003763\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003764 mode: mode of process creation\n\
3765 path: path of executable file\n\
3766 args: tuple or list of arguments\n\
3767 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003768
3769static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003770posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003771{
Victor Stinner8c62be82010-05-06 00:08:46 +00003772 PyObject *opath;
3773 char *path;
3774 PyObject *argv, *env;
3775 char **argvlist;
3776 char **envlist;
3777 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003778 int mode;
3779 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003780 Py_intptr_t spawnval;
3781 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3782 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003783
Victor Stinner8c62be82010-05-06 00:08:46 +00003784 /* spawnve has four arguments: (mode, path, argv, env), where
3785 argv is a list or tuple of strings and env is a dictionary
3786 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003787
Victor Stinner8c62be82010-05-06 00:08:46 +00003788 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3789 PyUnicode_FSConverter,
3790 &opath, &argv, &env))
3791 return NULL;
3792 path = PyBytes_AsString(opath);
3793 if (PyList_Check(argv)) {
3794 argc = PyList_Size(argv);
3795 getitem = PyList_GetItem;
3796 }
3797 else if (PyTuple_Check(argv)) {
3798 argc = PyTuple_Size(argv);
3799 getitem = PyTuple_GetItem;
3800 }
3801 else {
3802 PyErr_SetString(PyExc_TypeError,
3803 "spawnve() arg 2 must be a tuple or list");
3804 goto fail_0;
3805 }
3806 if (!PyMapping_Check(env)) {
3807 PyErr_SetString(PyExc_TypeError,
3808 "spawnve() arg 3 must be a mapping object");
3809 goto fail_0;
3810 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003811
Victor Stinner8c62be82010-05-06 00:08:46 +00003812 argvlist = PyMem_NEW(char *, argc+1);
3813 if (argvlist == NULL) {
3814 PyErr_NoMemory();
3815 goto fail_0;
3816 }
3817 for (i = 0; i < argc; i++) {
3818 if (!fsconvert_strdup((*getitem)(argv, i),
3819 &argvlist[i]))
3820 {
3821 lastarg = i;
3822 goto fail_1;
3823 }
3824 }
3825 lastarg = argc;
3826 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003827
Victor Stinner8c62be82010-05-06 00:08:46 +00003828 envlist = parse_envlist(env, &envc);
3829 if (envlist == NULL)
3830 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003831
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003832#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003833 Py_BEGIN_ALLOW_THREADS
3834 spawnval = spawnve(mode, path, argvlist, envlist);
3835 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003836#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003837 if (mode == _OLD_P_OVERLAY)
3838 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003839
Victor Stinner8c62be82010-05-06 00:08:46 +00003840 Py_BEGIN_ALLOW_THREADS
3841 spawnval = _spawnve(mode, path, argvlist, envlist);
3842 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003843#endif
Tim Peters25059d32001-12-07 20:35:43 +00003844
Victor Stinner8c62be82010-05-06 00:08:46 +00003845 if (spawnval == -1)
3846 (void) posix_error();
3847 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003848#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003849 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003850#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003851 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003852#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003853
Victor Stinner8c62be82010-05-06 00:08:46 +00003854 while (--envc >= 0)
3855 PyMem_DEL(envlist[envc]);
3856 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003857 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003858 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003859 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003860 Py_DECREF(opath);
3861 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003862}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003863
3864/* OS/2 supports spawnvp & spawnvpe natively */
3865#if defined(PYOS_OS2)
3866PyDoc_STRVAR(posix_spawnvp__doc__,
3867"spawnvp(mode, file, args)\n\n\
3868Execute the program 'file' in a new process, using the environment\n\
3869search path to find the file.\n\
3870\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003871 mode: mode of process creation\n\
3872 file: executable file name\n\
3873 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003874
3875static PyObject *
3876posix_spawnvp(PyObject *self, PyObject *args)
3877{
Victor Stinner8c62be82010-05-06 00:08:46 +00003878 PyObject *opath;
3879 char *path;
3880 PyObject *argv;
3881 char **argvlist;
3882 int mode, i, argc;
3883 Py_intptr_t spawnval;
3884 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003885
Victor Stinner8c62be82010-05-06 00:08:46 +00003886 /* spawnvp has three arguments: (mode, path, argv), where
3887 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003888
Victor Stinner8c62be82010-05-06 00:08:46 +00003889 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3890 PyUnicode_FSConverter,
3891 &opath, &argv))
3892 return NULL;
3893 path = PyBytes_AsString(opath);
3894 if (PyList_Check(argv)) {
3895 argc = PyList_Size(argv);
3896 getitem = PyList_GetItem;
3897 }
3898 else if (PyTuple_Check(argv)) {
3899 argc = PyTuple_Size(argv);
3900 getitem = PyTuple_GetItem;
3901 }
3902 else {
3903 PyErr_SetString(PyExc_TypeError,
3904 "spawnvp() arg 2 must be a tuple or list");
3905 Py_DECREF(opath);
3906 return NULL;
3907 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003908
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 argvlist = PyMem_NEW(char *, argc+1);
3910 if (argvlist == NULL) {
3911 Py_DECREF(opath);
3912 return PyErr_NoMemory();
3913 }
3914 for (i = 0; i < argc; i++) {
3915 if (!fsconvert_strdup((*getitem)(argv, i),
3916 &argvlist[i])) {
3917 free_string_array(argvlist, i);
3918 PyErr_SetString(
3919 PyExc_TypeError,
3920 "spawnvp() arg 2 must contain only strings");
3921 Py_DECREF(opath);
3922 return NULL;
3923 }
3924 }
3925 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003926
Victor Stinner8c62be82010-05-06 00:08:46 +00003927 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003928#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003929 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003930#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003932#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003933 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003934
Victor Stinner8c62be82010-05-06 00:08:46 +00003935 free_string_array(argvlist, argc);
3936 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003937
Victor Stinner8c62be82010-05-06 00:08:46 +00003938 if (spawnval == -1)
3939 return posix_error();
3940 else
3941 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003942}
3943
3944
3945PyDoc_STRVAR(posix_spawnvpe__doc__,
3946"spawnvpe(mode, file, args, env)\n\n\
3947Execute the program 'file' in a new process, using the environment\n\
3948search path to find the file.\n\
3949\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 mode: mode of process creation\n\
3951 file: executable file name\n\
3952 args: tuple or list of arguments\n\
3953 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003954
3955static PyObject *
3956posix_spawnvpe(PyObject *self, PyObject *args)
3957{
Victor Stinner8c62be82010-05-06 00:08:46 +00003958 PyObject *opath
3959 char *path;
3960 PyObject *argv, *env;
3961 char **argvlist;
3962 char **envlist;
3963 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003964 int mode;
3965 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003966 Py_intptr_t spawnval;
3967 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3968 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003969
Victor Stinner8c62be82010-05-06 00:08:46 +00003970 /* spawnvpe has four arguments: (mode, path, argv, env), where
3971 argv is a list or tuple of strings and env is a dictionary
3972 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003973
Victor Stinner8c62be82010-05-06 00:08:46 +00003974 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3975 PyUnicode_FSConverter,
3976 &opath, &argv, &env))
3977 return NULL;
3978 path = PyBytes_AsString(opath);
3979 if (PyList_Check(argv)) {
3980 argc = PyList_Size(argv);
3981 getitem = PyList_GetItem;
3982 }
3983 else if (PyTuple_Check(argv)) {
3984 argc = PyTuple_Size(argv);
3985 getitem = PyTuple_GetItem;
3986 }
3987 else {
3988 PyErr_SetString(PyExc_TypeError,
3989 "spawnvpe() arg 2 must be a tuple or list");
3990 goto fail_0;
3991 }
3992 if (!PyMapping_Check(env)) {
3993 PyErr_SetString(PyExc_TypeError,
3994 "spawnvpe() arg 3 must be a mapping object");
3995 goto fail_0;
3996 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003997
Victor Stinner8c62be82010-05-06 00:08:46 +00003998 argvlist = PyMem_NEW(char *, argc+1);
3999 if (argvlist == NULL) {
4000 PyErr_NoMemory();
4001 goto fail_0;
4002 }
4003 for (i = 0; i < argc; i++) {
4004 if (!fsconvert_strdup((*getitem)(argv, i),
4005 &argvlist[i]))
4006 {
4007 lastarg = i;
4008 goto fail_1;
4009 }
4010 }
4011 lastarg = argc;
4012 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004013
Victor Stinner8c62be82010-05-06 00:08:46 +00004014 envlist = parse_envlist(env, &envc);
4015 if (envlist == NULL)
4016 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004017
Victor Stinner8c62be82010-05-06 00:08:46 +00004018 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004019#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004020 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004021#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004022 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004023#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004024 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004025
Victor Stinner8c62be82010-05-06 00:08:46 +00004026 if (spawnval == -1)
4027 (void) posix_error();
4028 else
4029 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004030
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 while (--envc >= 0)
4032 PyMem_DEL(envlist[envc]);
4033 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004034 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004036 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 Py_DECREF(opath);
4038 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004039}
4040#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004041#endif /* HAVE_SPAWNV */
4042
4043
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004044#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004045PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004046"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004047Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4048\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004049Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004050
4051static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004052posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004053{
Victor Stinner8c62be82010-05-06 00:08:46 +00004054 pid_t pid;
4055 int result = 0;
4056 _PyImport_AcquireLock();
4057 pid = fork1();
4058 if (pid == 0) {
4059 /* child: this clobbers and resets the import lock. */
4060 PyOS_AfterFork();
4061 } else {
4062 /* parent: release the import lock. */
4063 result = _PyImport_ReleaseLock();
4064 }
4065 if (pid == -1)
4066 return posix_error();
4067 if (result < 0) {
4068 /* Don't clobber the OSError if the fork failed. */
4069 PyErr_SetString(PyExc_RuntimeError,
4070 "not holding the import lock");
4071 return NULL;
4072 }
4073 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004074}
4075#endif
4076
4077
Guido van Rossumad0ee831995-03-01 10:34:45 +00004078#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004079PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004080"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004081Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004082Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004083
Barry Warsaw53699e91996-12-10 23:23:01 +00004084static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004085posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004086{
Victor Stinner8c62be82010-05-06 00:08:46 +00004087 pid_t pid;
4088 int result = 0;
4089 _PyImport_AcquireLock();
4090 pid = fork();
4091 if (pid == 0) {
4092 /* child: this clobbers and resets the import lock. */
4093 PyOS_AfterFork();
4094 } else {
4095 /* parent: release the import lock. */
4096 result = _PyImport_ReleaseLock();
4097 }
4098 if (pid == -1)
4099 return posix_error();
4100 if (result < 0) {
4101 /* Don't clobber the OSError if the fork failed. */
4102 PyErr_SetString(PyExc_RuntimeError,
4103 "not holding the import lock");
4104 return NULL;
4105 }
4106 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004107}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004108#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004109
Neal Norwitzb59798b2003-03-21 01:43:31 +00004110/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004111/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4112#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004113#define DEV_PTY_FILE "/dev/ptc"
4114#define HAVE_DEV_PTMX
4115#else
4116#define DEV_PTY_FILE "/dev/ptmx"
4117#endif
4118
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004119#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004120#ifdef HAVE_PTY_H
4121#include <pty.h>
4122#else
4123#ifdef HAVE_LIBUTIL_H
4124#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004125#else
4126#ifdef HAVE_UTIL_H
4127#include <util.h>
4128#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004129#endif /* HAVE_LIBUTIL_H */
4130#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004131#ifdef HAVE_STROPTS_H
4132#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004133#endif
4134#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004135
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004136#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004137PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004138"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004139Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004140
4141static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004142posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004143{
Victor Stinner8c62be82010-05-06 00:08:46 +00004144 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004145#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004146 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004147#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004148#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004149 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004150#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004151 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004152#endif
4153#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004154
Thomas Wouters70c21a12000-07-14 14:28:33 +00004155#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004156 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4157 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004158#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004159 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4160 if (slave_name == NULL)
4161 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004162
Victor Stinner8c62be82010-05-06 00:08:46 +00004163 slave_fd = open(slave_name, O_RDWR);
4164 if (slave_fd < 0)
4165 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004166#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004167 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4168 if (master_fd < 0)
4169 return posix_error();
4170 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4171 /* change permission of slave */
4172 if (grantpt(master_fd) < 0) {
4173 PyOS_setsig(SIGCHLD, sig_saved);
4174 return posix_error();
4175 }
4176 /* unlock slave */
4177 if (unlockpt(master_fd) < 0) {
4178 PyOS_setsig(SIGCHLD, sig_saved);
4179 return posix_error();
4180 }
4181 PyOS_setsig(SIGCHLD, sig_saved);
4182 slave_name = ptsname(master_fd); /* get name of slave */
4183 if (slave_name == NULL)
4184 return posix_error();
4185 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4186 if (slave_fd < 0)
4187 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004188#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004189 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4190 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004191#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004193#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004194#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004195#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004196
Victor Stinner8c62be82010-05-06 00:08:46 +00004197 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004198
Fred Drake8cef4cf2000-06-28 16:40:38 +00004199}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004200#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004201
4202#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004203PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004204"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004205Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4206Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004207To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004208
4209static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004210posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004211{
Victor Stinner8c62be82010-05-06 00:08:46 +00004212 int master_fd = -1, result = 0;
4213 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004214
Victor Stinner8c62be82010-05-06 00:08:46 +00004215 _PyImport_AcquireLock();
4216 pid = forkpty(&master_fd, NULL, NULL, NULL);
4217 if (pid == 0) {
4218 /* child: this clobbers and resets the import lock. */
4219 PyOS_AfterFork();
4220 } else {
4221 /* parent: release the import lock. */
4222 result = _PyImport_ReleaseLock();
4223 }
4224 if (pid == -1)
4225 return posix_error();
4226 if (result < 0) {
4227 /* Don't clobber the OSError if the fork failed. */
4228 PyErr_SetString(PyExc_RuntimeError,
4229 "not holding the import lock");
4230 return NULL;
4231 }
4232 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004233}
4234#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004235
Guido van Rossumad0ee831995-03-01 10:34:45 +00004236#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004237PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004238"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004239Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004240
Barry Warsaw53699e91996-12-10 23:23:01 +00004241static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004242posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004243{
Victor Stinner8c62be82010-05-06 00:08:46 +00004244 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004245}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004246#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004247
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004248
Guido van Rossumad0ee831995-03-01 10:34:45 +00004249#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004250PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004251"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004252Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004253
Barry Warsaw53699e91996-12-10 23:23:01 +00004254static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004255posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004256{
Victor Stinner8c62be82010-05-06 00:08:46 +00004257 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004258}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004259#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004260
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004261
Guido van Rossumad0ee831995-03-01 10:34:45 +00004262#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004263PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004264"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004265Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004266
Barry Warsaw53699e91996-12-10 23:23:01 +00004267static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004268posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004269{
Victor Stinner8c62be82010-05-06 00:08:46 +00004270 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004271}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004272#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004273
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004274
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004275PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004276"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004277Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004278
Barry Warsaw53699e91996-12-10 23:23:01 +00004279static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004280posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004281{
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004283}
4284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004285
Fred Drakec9680921999-12-13 16:37:25 +00004286#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004287PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004288"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004289Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004290
4291static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004292posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004293{
4294 PyObject *result = NULL;
4295
Fred Drakec9680921999-12-13 16:37:25 +00004296#ifdef NGROUPS_MAX
4297#define MAX_GROUPS NGROUPS_MAX
4298#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004299 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004300#define MAX_GROUPS 64
4301#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004303
4304 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4305 * This is a helper variable to store the intermediate result when
4306 * that happens.
4307 *
4308 * To keep the code readable the OSX behaviour is unconditional,
4309 * according to the POSIX spec this should be safe on all unix-y
4310 * systems.
4311 */
4312 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004313 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004314
Victor Stinner8c62be82010-05-06 00:08:46 +00004315 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004316 if (n < 0) {
4317 if (errno == EINVAL) {
4318 n = getgroups(0, NULL);
4319 if (n == -1) {
4320 return posix_error();
4321 }
4322 if (n == 0) {
4323 /* Avoid malloc(0) */
4324 alt_grouplist = grouplist;
4325 } else {
4326 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4327 if (alt_grouplist == NULL) {
4328 errno = EINVAL;
4329 return posix_error();
4330 }
4331 n = getgroups(n, alt_grouplist);
4332 if (n == -1) {
4333 PyMem_Free(alt_grouplist);
4334 return posix_error();
4335 }
4336 }
4337 } else {
4338 return posix_error();
4339 }
4340 }
4341 result = PyList_New(n);
4342 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004343 int i;
4344 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004345 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004347 Py_DECREF(result);
4348 result = NULL;
4349 break;
Fred Drakec9680921999-12-13 16:37:25 +00004350 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004352 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004353 }
4354
4355 if (alt_grouplist != grouplist) {
4356 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004357 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004358
Fred Drakec9680921999-12-13 16:37:25 +00004359 return result;
4360}
4361#endif
4362
Antoine Pitroub7572f02009-12-02 20:46:48 +00004363#ifdef HAVE_INITGROUPS
4364PyDoc_STRVAR(posix_initgroups__doc__,
4365"initgroups(username, gid) -> None\n\n\
4366Call the system initgroups() to initialize the group access list with all of\n\
4367the groups of which the specified username is a member, plus the specified\n\
4368group id.");
4369
4370static PyObject *
4371posix_initgroups(PyObject *self, PyObject *args)
4372{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004373 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004374 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004375 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004376 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004377
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004378 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4379 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004380 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004381 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004382
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004383 res = initgroups(username, (gid_t) gid);
4384 Py_DECREF(oname);
4385 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004386 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004387
Victor Stinner8c62be82010-05-06 00:08:46 +00004388 Py_INCREF(Py_None);
4389 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004390}
4391#endif
4392
Martin v. Löwis606edc12002-06-13 21:09:11 +00004393#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004394PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004395"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004396Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004397
4398static PyObject *
4399posix_getpgid(PyObject *self, PyObject *args)
4400{
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 pid_t pid, pgid;
4402 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4403 return NULL;
4404 pgid = getpgid(pid);
4405 if (pgid < 0)
4406 return posix_error();
4407 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004408}
4409#endif /* HAVE_GETPGID */
4410
4411
Guido van Rossumb6775db1994-08-01 11:34:53 +00004412#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004413PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004414"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004415Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004416
Barry Warsaw53699e91996-12-10 23:23:01 +00004417static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004418posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004419{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004420#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004421 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004422#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004423 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004424#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004425}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004426#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004428
Guido van Rossumb6775db1994-08-01 11:34:53 +00004429#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004430PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004431"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004432Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004433
Barry Warsaw53699e91996-12-10 23:23:01 +00004434static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004435posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004436{
Guido van Rossum64933891994-10-20 21:56:42 +00004437#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004438 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004439#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004440 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004441#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004442 return posix_error();
4443 Py_INCREF(Py_None);
4444 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004445}
4446
Guido van Rossumb6775db1994-08-01 11:34:53 +00004447#endif /* HAVE_SETPGRP */
4448
Guido van Rossumad0ee831995-03-01 10:34:45 +00004449#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004450
4451#ifdef MS_WINDOWS
4452#include <tlhelp32.h>
4453
4454static PyObject*
4455win32_getppid()
4456{
4457 HANDLE snapshot;
4458 pid_t mypid;
4459 PyObject* result = NULL;
4460 BOOL have_record;
4461 PROCESSENTRY32 pe;
4462
4463 mypid = getpid(); /* This function never fails */
4464
4465 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4466 if (snapshot == INVALID_HANDLE_VALUE)
4467 return PyErr_SetFromWindowsErr(GetLastError());
4468
4469 pe.dwSize = sizeof(pe);
4470 have_record = Process32First(snapshot, &pe);
4471 while (have_record) {
4472 if (mypid == (pid_t)pe.th32ProcessID) {
4473 /* We could cache the ulong value in a static variable. */
4474 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4475 break;
4476 }
4477
4478 have_record = Process32Next(snapshot, &pe);
4479 }
4480
4481 /* If our loop exits and our pid was not found (result will be NULL)
4482 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4483 * error anyway, so let's raise it. */
4484 if (!result)
4485 result = PyErr_SetFromWindowsErr(GetLastError());
4486
4487 CloseHandle(snapshot);
4488
4489 return result;
4490}
4491#endif /*MS_WINDOWS*/
4492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004493PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004494"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004495Return the parent's process id. If the parent process has already exited,\n\
4496Windows machines will still return its id; others systems will return the id\n\
4497of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004498
Barry Warsaw53699e91996-12-10 23:23:01 +00004499static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004500posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004501{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004502#ifdef MS_WINDOWS
4503 return win32_getppid();
4504#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004505 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004506#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004507}
4508#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004509
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004510
Fred Drake12c6e2d1999-12-14 21:25:03 +00004511#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004512PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004513"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004514Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004515
4516static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004517posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004518{
Victor Stinner8c62be82010-05-06 00:08:46 +00004519 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004520#ifdef MS_WINDOWS
4521 wchar_t user_name[UNLEN + 1];
4522 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4523
4524 if (GetUserNameW(user_name, &num_chars)) {
4525 /* num_chars is the number of unicode chars plus null terminator */
4526 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4527 }
4528 else
4529 result = PyErr_SetFromWindowsErr(GetLastError());
4530#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004531 char *name;
4532 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004533
Victor Stinner8c62be82010-05-06 00:08:46 +00004534 errno = 0;
4535 name = getlogin();
4536 if (name == NULL) {
4537 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004538 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004539 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004540 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 }
4542 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004543 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004544 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004545#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004546 return result;
4547}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004548#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004549
Guido van Rossumad0ee831995-03-01 10:34:45 +00004550#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004551PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004552"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004553Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004554
Barry Warsaw53699e91996-12-10 23:23:01 +00004555static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004556posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004557{
Victor Stinner8c62be82010-05-06 00:08:46 +00004558 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004559}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004560#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004561
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004562
Guido van Rossumad0ee831995-03-01 10:34:45 +00004563#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004564PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004565"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004566Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004567
Barry Warsaw53699e91996-12-10 23:23:01 +00004568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004569posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004570{
Victor Stinner8c62be82010-05-06 00:08:46 +00004571 pid_t pid;
4572 int sig;
4573 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4574 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004575#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004576 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4577 APIRET rc;
4578 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004579 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004580
4581 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4582 APIRET rc;
4583 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004584 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004585
4586 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004587 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004588#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004589 if (kill(pid, sig) == -1)
4590 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004591#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004592 Py_INCREF(Py_None);
4593 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004594}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004595#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004596
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004597#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004598PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004599"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004600Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004601
4602static PyObject *
4603posix_killpg(PyObject *self, PyObject *args)
4604{
Victor Stinner8c62be82010-05-06 00:08:46 +00004605 int sig;
4606 pid_t pgid;
4607 /* XXX some man pages make the `pgid` parameter an int, others
4608 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4609 take the same type. Moreover, pid_t is always at least as wide as
4610 int (else compilation of this module fails), which is safe. */
4611 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4612 return NULL;
4613 if (killpg(pgid, sig) == -1)
4614 return posix_error();
4615 Py_INCREF(Py_None);
4616 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004617}
4618#endif
4619
Brian Curtineb24d742010-04-12 17:16:38 +00004620#ifdef MS_WINDOWS
4621PyDoc_STRVAR(win32_kill__doc__,
4622"kill(pid, sig)\n\n\
4623Kill a process with a signal.");
4624
4625static PyObject *
4626win32_kill(PyObject *self, PyObject *args)
4627{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004628 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004629 DWORD pid, sig, err;
4630 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004631
Victor Stinner8c62be82010-05-06 00:08:46 +00004632 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4633 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004634
Victor Stinner8c62be82010-05-06 00:08:46 +00004635 /* Console processes which share a common console can be sent CTRL+C or
4636 CTRL+BREAK events, provided they handle said events. */
4637 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4638 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4639 err = GetLastError();
4640 PyErr_SetFromWindowsErr(err);
4641 }
4642 else
4643 Py_RETURN_NONE;
4644 }
Brian Curtineb24d742010-04-12 17:16:38 +00004645
Victor Stinner8c62be82010-05-06 00:08:46 +00004646 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4647 attempt to open and terminate the process. */
4648 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4649 if (handle == NULL) {
4650 err = GetLastError();
4651 return PyErr_SetFromWindowsErr(err);
4652 }
Brian Curtineb24d742010-04-12 17:16:38 +00004653
Victor Stinner8c62be82010-05-06 00:08:46 +00004654 if (TerminateProcess(handle, sig) == 0) {
4655 err = GetLastError();
4656 result = PyErr_SetFromWindowsErr(err);
4657 } else {
4658 Py_INCREF(Py_None);
4659 result = Py_None;
4660 }
Brian Curtineb24d742010-04-12 17:16:38 +00004661
Victor Stinner8c62be82010-05-06 00:08:46 +00004662 CloseHandle(handle);
4663 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004664}
4665#endif /* MS_WINDOWS */
4666
Guido van Rossumc0125471996-06-28 18:55:32 +00004667#ifdef HAVE_PLOCK
4668
4669#ifdef HAVE_SYS_LOCK_H
4670#include <sys/lock.h>
4671#endif
4672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004673PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004674"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004675Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004676
Barry Warsaw53699e91996-12-10 23:23:01 +00004677static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004678posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004679{
Victor Stinner8c62be82010-05-06 00:08:46 +00004680 int op;
4681 if (!PyArg_ParseTuple(args, "i:plock", &op))
4682 return NULL;
4683 if (plock(op) == -1)
4684 return posix_error();
4685 Py_INCREF(Py_None);
4686 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004687}
4688#endif
4689
Guido van Rossumb6775db1994-08-01 11:34:53 +00004690#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004691PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004692"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004693Set the current process's user id.");
4694
Barry Warsaw53699e91996-12-10 23:23:01 +00004695static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004696posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004697{
Victor Stinner8c62be82010-05-06 00:08:46 +00004698 long uid_arg;
4699 uid_t uid;
4700 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4701 return NULL;
4702 uid = uid_arg;
4703 if (uid != uid_arg) {
4704 PyErr_SetString(PyExc_OverflowError, "user id too big");
4705 return NULL;
4706 }
4707 if (setuid(uid) < 0)
4708 return posix_error();
4709 Py_INCREF(Py_None);
4710 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004711}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004712#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004713
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004714
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004715#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004716PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004717"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004718Set the current process's effective user id.");
4719
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004720static PyObject *
4721posix_seteuid (PyObject *self, PyObject *args)
4722{
Victor Stinner8c62be82010-05-06 00:08:46 +00004723 long euid_arg;
4724 uid_t euid;
4725 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4726 return NULL;
4727 euid = euid_arg;
4728 if (euid != euid_arg) {
4729 PyErr_SetString(PyExc_OverflowError, "user id too big");
4730 return NULL;
4731 }
4732 if (seteuid(euid) < 0) {
4733 return posix_error();
4734 } else {
4735 Py_INCREF(Py_None);
4736 return Py_None;
4737 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004738}
4739#endif /* HAVE_SETEUID */
4740
4741#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004742PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004743"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004744Set the current process's effective group id.");
4745
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004746static PyObject *
4747posix_setegid (PyObject *self, PyObject *args)
4748{
Victor Stinner8c62be82010-05-06 00:08:46 +00004749 long egid_arg;
4750 gid_t egid;
4751 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4752 return NULL;
4753 egid = egid_arg;
4754 if (egid != egid_arg) {
4755 PyErr_SetString(PyExc_OverflowError, "group id too big");
4756 return NULL;
4757 }
4758 if (setegid(egid) < 0) {
4759 return posix_error();
4760 } else {
4761 Py_INCREF(Py_None);
4762 return Py_None;
4763 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004764}
4765#endif /* HAVE_SETEGID */
4766
4767#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004768PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004769"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004770Set the current process's real and effective user ids.");
4771
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004772static PyObject *
4773posix_setreuid (PyObject *self, PyObject *args)
4774{
Victor Stinner8c62be82010-05-06 00:08:46 +00004775 long ruid_arg, euid_arg;
4776 uid_t ruid, euid;
4777 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4778 return NULL;
4779 if (ruid_arg == -1)
4780 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4781 else
4782 ruid = ruid_arg; /* otherwise, assign from our long */
4783 if (euid_arg == -1)
4784 euid = (uid_t)-1;
4785 else
4786 euid = euid_arg;
4787 if ((euid_arg != -1 && euid != euid_arg) ||
4788 (ruid_arg != -1 && ruid != ruid_arg)) {
4789 PyErr_SetString(PyExc_OverflowError, "user id too big");
4790 return NULL;
4791 }
4792 if (setreuid(ruid, euid) < 0) {
4793 return posix_error();
4794 } else {
4795 Py_INCREF(Py_None);
4796 return Py_None;
4797 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004798}
4799#endif /* HAVE_SETREUID */
4800
4801#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004802PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004803"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004804Set the current process's real and effective group ids.");
4805
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004806static PyObject *
4807posix_setregid (PyObject *self, PyObject *args)
4808{
Victor Stinner8c62be82010-05-06 00:08:46 +00004809 long rgid_arg, egid_arg;
4810 gid_t rgid, egid;
4811 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4812 return NULL;
4813 if (rgid_arg == -1)
4814 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4815 else
4816 rgid = rgid_arg; /* otherwise, assign from our long */
4817 if (egid_arg == -1)
4818 egid = (gid_t)-1;
4819 else
4820 egid = egid_arg;
4821 if ((egid_arg != -1 && egid != egid_arg) ||
4822 (rgid_arg != -1 && rgid != rgid_arg)) {
4823 PyErr_SetString(PyExc_OverflowError, "group id too big");
4824 return NULL;
4825 }
4826 if (setregid(rgid, egid) < 0) {
4827 return posix_error();
4828 } else {
4829 Py_INCREF(Py_None);
4830 return Py_None;
4831 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004832}
4833#endif /* HAVE_SETREGID */
4834
Guido van Rossumb6775db1994-08-01 11:34:53 +00004835#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004836PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004837"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004838Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004839
Barry Warsaw53699e91996-12-10 23:23:01 +00004840static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004841posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004842{
Victor Stinner8c62be82010-05-06 00:08:46 +00004843 long gid_arg;
4844 gid_t gid;
4845 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4846 return NULL;
4847 gid = gid_arg;
4848 if (gid != gid_arg) {
4849 PyErr_SetString(PyExc_OverflowError, "group id too big");
4850 return NULL;
4851 }
4852 if (setgid(gid) < 0)
4853 return posix_error();
4854 Py_INCREF(Py_None);
4855 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004856}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004857#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004858
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004859#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004860PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004861"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004862Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004863
4864static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004865posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004866{
Victor Stinner8c62be82010-05-06 00:08:46 +00004867 int i, len;
4868 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004869
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 if (!PySequence_Check(groups)) {
4871 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4872 return NULL;
4873 }
4874 len = PySequence_Size(groups);
4875 if (len > MAX_GROUPS) {
4876 PyErr_SetString(PyExc_ValueError, "too many groups");
4877 return NULL;
4878 }
4879 for(i = 0; i < len; i++) {
4880 PyObject *elem;
4881 elem = PySequence_GetItem(groups, i);
4882 if (!elem)
4883 return NULL;
4884 if (!PyLong_Check(elem)) {
4885 PyErr_SetString(PyExc_TypeError,
4886 "groups must be integers");
4887 Py_DECREF(elem);
4888 return NULL;
4889 } else {
4890 unsigned long x = PyLong_AsUnsignedLong(elem);
4891 if (PyErr_Occurred()) {
4892 PyErr_SetString(PyExc_TypeError,
4893 "group id too big");
4894 Py_DECREF(elem);
4895 return NULL;
4896 }
4897 grouplist[i] = x;
4898 /* read back the value to see if it fitted in gid_t */
4899 if (grouplist[i] != x) {
4900 PyErr_SetString(PyExc_TypeError,
4901 "group id too big");
4902 Py_DECREF(elem);
4903 return NULL;
4904 }
4905 }
4906 Py_DECREF(elem);
4907 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004908
Victor Stinner8c62be82010-05-06 00:08:46 +00004909 if (setgroups(len, grouplist) < 0)
4910 return posix_error();
4911 Py_INCREF(Py_None);
4912 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004913}
4914#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004915
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004916#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4917static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004918wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004919{
Victor Stinner8c62be82010-05-06 00:08:46 +00004920 PyObject *result;
4921 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004922
Victor Stinner8c62be82010-05-06 00:08:46 +00004923 if (pid == -1)
4924 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004925
Victor Stinner8c62be82010-05-06 00:08:46 +00004926 if (struct_rusage == NULL) {
4927 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4928 if (m == NULL)
4929 return NULL;
4930 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4931 Py_DECREF(m);
4932 if (struct_rusage == NULL)
4933 return NULL;
4934 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004935
Victor Stinner8c62be82010-05-06 00:08:46 +00004936 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4937 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4938 if (!result)
4939 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004940
4941#ifndef doubletime
4942#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4943#endif
4944
Victor Stinner8c62be82010-05-06 00:08:46 +00004945 PyStructSequence_SET_ITEM(result, 0,
4946 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4947 PyStructSequence_SET_ITEM(result, 1,
4948 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004949#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004950 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4951 SET_INT(result, 2, ru->ru_maxrss);
4952 SET_INT(result, 3, ru->ru_ixrss);
4953 SET_INT(result, 4, ru->ru_idrss);
4954 SET_INT(result, 5, ru->ru_isrss);
4955 SET_INT(result, 6, ru->ru_minflt);
4956 SET_INT(result, 7, ru->ru_majflt);
4957 SET_INT(result, 8, ru->ru_nswap);
4958 SET_INT(result, 9, ru->ru_inblock);
4959 SET_INT(result, 10, ru->ru_oublock);
4960 SET_INT(result, 11, ru->ru_msgsnd);
4961 SET_INT(result, 12, ru->ru_msgrcv);
4962 SET_INT(result, 13, ru->ru_nsignals);
4963 SET_INT(result, 14, ru->ru_nvcsw);
4964 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004965#undef SET_INT
4966
Victor Stinner8c62be82010-05-06 00:08:46 +00004967 if (PyErr_Occurred()) {
4968 Py_DECREF(result);
4969 return NULL;
4970 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004971
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004973}
4974#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4975
4976#ifdef HAVE_WAIT3
4977PyDoc_STRVAR(posix_wait3__doc__,
4978"wait3(options) -> (pid, status, rusage)\n\n\
4979Wait for completion of a child process.");
4980
4981static PyObject *
4982posix_wait3(PyObject *self, PyObject *args)
4983{
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 pid_t pid;
4985 int options;
4986 struct rusage ru;
4987 WAIT_TYPE status;
4988 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004989
Victor Stinner8c62be82010-05-06 00:08:46 +00004990 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4991 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004992
Victor Stinner8c62be82010-05-06 00:08:46 +00004993 Py_BEGIN_ALLOW_THREADS
4994 pid = wait3(&status, options, &ru);
4995 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004996
Victor Stinner8c62be82010-05-06 00:08:46 +00004997 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004998}
4999#endif /* HAVE_WAIT3 */
5000
5001#ifdef HAVE_WAIT4
5002PyDoc_STRVAR(posix_wait4__doc__,
5003"wait4(pid, options) -> (pid, status, rusage)\n\n\
5004Wait for completion of a given child process.");
5005
5006static PyObject *
5007posix_wait4(PyObject *self, PyObject *args)
5008{
Victor Stinner8c62be82010-05-06 00:08:46 +00005009 pid_t pid;
5010 int options;
5011 struct rusage ru;
5012 WAIT_TYPE status;
5013 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005014
Victor Stinner8c62be82010-05-06 00:08:46 +00005015 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
5016 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005017
Victor Stinner8c62be82010-05-06 00:08:46 +00005018 Py_BEGIN_ALLOW_THREADS
5019 pid = wait4(pid, &status, options, &ru);
5020 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005021
Victor Stinner8c62be82010-05-06 00:08:46 +00005022 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005023}
5024#endif /* HAVE_WAIT4 */
5025
Guido van Rossumb6775db1994-08-01 11:34:53 +00005026#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005027PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005028"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005029Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005030
Barry Warsaw53699e91996-12-10 23:23:01 +00005031static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005032posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005033{
Victor Stinner8c62be82010-05-06 00:08:46 +00005034 pid_t pid;
5035 int options;
5036 WAIT_TYPE status;
5037 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005038
Victor Stinner8c62be82010-05-06 00:08:46 +00005039 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5040 return NULL;
5041 Py_BEGIN_ALLOW_THREADS
5042 pid = waitpid(pid, &status, options);
5043 Py_END_ALLOW_THREADS
5044 if (pid == -1)
5045 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005046
Victor Stinner8c62be82010-05-06 00:08:46 +00005047 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005048}
5049
Tim Petersab034fa2002-02-01 11:27:43 +00005050#elif defined(HAVE_CWAIT)
5051
5052/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005053PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005054"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005055"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005056
5057static PyObject *
5058posix_waitpid(PyObject *self, PyObject *args)
5059{
Victor Stinner8c62be82010-05-06 00:08:46 +00005060 Py_intptr_t pid;
5061 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005062
Victor Stinner8c62be82010-05-06 00:08:46 +00005063 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5064 return NULL;
5065 Py_BEGIN_ALLOW_THREADS
5066 pid = _cwait(&status, pid, options);
5067 Py_END_ALLOW_THREADS
5068 if (pid == -1)
5069 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005070
Victor Stinner8c62be82010-05-06 00:08:46 +00005071 /* shift the status left a byte so this is more like the POSIX waitpid */
5072 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005073}
5074#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005075
Guido van Rossumad0ee831995-03-01 10:34:45 +00005076#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005077PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005078"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005079Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005080
Barry Warsaw53699e91996-12-10 23:23:01 +00005081static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005082posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005083{
Victor Stinner8c62be82010-05-06 00:08:46 +00005084 pid_t pid;
5085 WAIT_TYPE status;
5086 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005087
Victor Stinner8c62be82010-05-06 00:08:46 +00005088 Py_BEGIN_ALLOW_THREADS
5089 pid = wait(&status);
5090 Py_END_ALLOW_THREADS
5091 if (pid == -1)
5092 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005093
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005095}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005096#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005098
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005099PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005100"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005101Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005102
Barry Warsaw53699e91996-12-10 23:23:01 +00005103static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005104posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005105{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005106#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005107 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005108#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005109#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005110 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5111 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005112#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005113 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005114#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005115#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005116}
5117
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005118
Guido van Rossumb6775db1994-08-01 11:34:53 +00005119#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005120PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005121"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005122Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005123
Barry Warsaw53699e91996-12-10 23:23:01 +00005124static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005125posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005126{
Victor Stinner8c62be82010-05-06 00:08:46 +00005127 PyObject* v;
5128 char buf[MAXPATHLEN];
5129 PyObject *opath;
5130 char *path;
5131 int n;
5132 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005133
Victor Stinner8c62be82010-05-06 00:08:46 +00005134 if (!PyArg_ParseTuple(args, "O&:readlink",
5135 PyUnicode_FSConverter, &opath))
5136 return NULL;
5137 path = PyBytes_AsString(opath);
5138 v = PySequence_GetItem(args, 0);
5139 if (v == NULL) {
5140 Py_DECREF(opath);
5141 return NULL;
5142 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005143
Victor Stinner8c62be82010-05-06 00:08:46 +00005144 if (PyUnicode_Check(v)) {
5145 arg_is_unicode = 1;
5146 }
5147 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005148
Victor Stinner8c62be82010-05-06 00:08:46 +00005149 Py_BEGIN_ALLOW_THREADS
5150 n = readlink(path, buf, (int) sizeof buf);
5151 Py_END_ALLOW_THREADS
5152 if (n < 0)
5153 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005154
Victor Stinner8c62be82010-05-06 00:08:46 +00005155 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005156 if (arg_is_unicode)
5157 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5158 else
5159 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005160}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005161#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005163
Brian Curtin52173d42010-12-02 18:29:18 +00005164#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005165PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005166"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005167Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005168
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005169static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005170posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005171{
Victor Stinner8c62be82010-05-06 00:08:46 +00005172 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005173}
5174#endif /* HAVE_SYMLINK */
5175
Brian Curtind40e6f72010-07-08 21:39:08 +00005176#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5177
5178PyDoc_STRVAR(win_readlink__doc__,
5179"readlink(path) -> path\n\n\
5180Return a string representing the path to which the symbolic link points.");
5181
Brian Curtind40e6f72010-07-08 21:39:08 +00005182/* Windows readlink implementation */
5183static PyObject *
5184win_readlink(PyObject *self, PyObject *args)
5185{
5186 wchar_t *path;
5187 DWORD n_bytes_returned;
5188 DWORD io_result;
5189 PyObject *result;
5190 HANDLE reparse_point_handle;
5191
5192 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5193 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5194 wchar_t *print_name;
5195
5196 if (!PyArg_ParseTuple(args,
5197 "u:readlink",
5198 &path))
5199 return NULL;
5200
5201 /* First get a handle to the reparse point */
5202 Py_BEGIN_ALLOW_THREADS
5203 reparse_point_handle = CreateFileW(
5204 path,
5205 0,
5206 0,
5207 0,
5208 OPEN_EXISTING,
5209 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5210 0);
5211 Py_END_ALLOW_THREADS
5212
5213 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5214 {
5215 return win32_error_unicode("readlink", path);
5216 }
5217
5218 Py_BEGIN_ALLOW_THREADS
5219 /* New call DeviceIoControl to read the reparse point */
5220 io_result = DeviceIoControl(
5221 reparse_point_handle,
5222 FSCTL_GET_REPARSE_POINT,
5223 0, 0, /* in buffer */
5224 target_buffer, sizeof(target_buffer),
5225 &n_bytes_returned,
5226 0 /* we're not using OVERLAPPED_IO */
5227 );
5228 CloseHandle(reparse_point_handle);
5229 Py_END_ALLOW_THREADS
5230
5231 if (io_result==0)
5232 {
5233 return win32_error_unicode("readlink", path);
5234 }
5235
5236 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5237 {
5238 PyErr_SetString(PyExc_ValueError,
5239 "not a symbolic link");
5240 return NULL;
5241 }
Brian Curtin74e45612010-07-09 15:58:59 +00005242 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5243 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5244
5245 result = PyUnicode_FromWideChar(print_name,
5246 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005247 return result;
5248}
5249
5250#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5251
Brian Curtin52173d42010-12-02 18:29:18 +00005252#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005253
5254/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5255static int has_CreateSymbolicLinkW = 0;
5256static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5257static int
5258check_CreateSymbolicLinkW()
5259{
5260 HINSTANCE hKernel32;
5261 /* only recheck */
5262 if (has_CreateSymbolicLinkW)
5263 return has_CreateSymbolicLinkW;
5264 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005265 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5266 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005267 if (Py_CreateSymbolicLinkW)
5268 has_CreateSymbolicLinkW = 1;
5269 return has_CreateSymbolicLinkW;
5270}
5271
5272PyDoc_STRVAR(win_symlink__doc__,
5273"symlink(src, dst, target_is_directory=False)\n\n\
5274Create a symbolic link pointing to src named dst.\n\
5275target_is_directory is required if the target is to be interpreted as\n\
5276a directory.\n\
5277This function requires Windows 6.0 or greater, and raises a\n\
5278NotImplementedError otherwise.");
5279
5280static PyObject *
5281win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5282{
5283 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5284 PyObject *src, *dest;
5285 int target_is_directory = 0;
5286 DWORD res;
5287 WIN32_FILE_ATTRIBUTE_DATA src_info;
5288
5289 if (!check_CreateSymbolicLinkW())
5290 {
5291 /* raise NotImplementedError */
5292 return PyErr_Format(PyExc_NotImplementedError,
5293 "CreateSymbolicLinkW not found");
5294 }
5295 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5296 kwlist, &src, &dest, &target_is_directory))
5297 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005298
5299 if (win32_can_symlink == 0)
5300 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5301
Brian Curtind40e6f72010-07-08 21:39:08 +00005302 if (!convert_to_unicode(&src)) { return NULL; }
5303 if (!convert_to_unicode(&dest)) {
5304 Py_DECREF(src);
5305 return NULL;
5306 }
5307
5308 /* if src is a directory, ensure target_is_directory==1 */
5309 if(
5310 GetFileAttributesExW(
5311 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5312 ))
5313 {
5314 target_is_directory = target_is_directory ||
5315 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5316 }
5317
5318 Py_BEGIN_ALLOW_THREADS
5319 res = Py_CreateSymbolicLinkW(
5320 PyUnicode_AsUnicode(dest),
5321 PyUnicode_AsUnicode(src),
5322 target_is_directory);
5323 Py_END_ALLOW_THREADS
5324 Py_DECREF(src);
5325 Py_DECREF(dest);
5326 if (!res)
5327 {
5328 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5329 }
5330
5331 Py_INCREF(Py_None);
5332 return Py_None;
5333}
Brian Curtin52173d42010-12-02 18:29:18 +00005334#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005335
5336#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005337#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5338static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005339system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005340{
5341 ULONG value = 0;
5342
5343 Py_BEGIN_ALLOW_THREADS
5344 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5345 Py_END_ALLOW_THREADS
5346
5347 return value;
5348}
5349
5350static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005351posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005352{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005353 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005354 return Py_BuildValue("ddddd",
5355 (double)0 /* t.tms_utime / HZ */,
5356 (double)0 /* t.tms_stime / HZ */,
5357 (double)0 /* t.tms_cutime / HZ */,
5358 (double)0 /* t.tms_cstime / HZ */,
5359 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005360}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005361#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005362#define NEED_TICKS_PER_SECOND
5363static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005364static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005365posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005366{
Victor Stinner8c62be82010-05-06 00:08:46 +00005367 struct tms t;
5368 clock_t c;
5369 errno = 0;
5370 c = times(&t);
5371 if (c == (clock_t) -1)
5372 return posix_error();
5373 return Py_BuildValue("ddddd",
5374 (double)t.tms_utime / ticks_per_second,
5375 (double)t.tms_stime / ticks_per_second,
5376 (double)t.tms_cutime / ticks_per_second,
5377 (double)t.tms_cstime / ticks_per_second,
5378 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005379}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005380#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005381#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005382
5383
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005384#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005385#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005386static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005387posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005388{
Victor Stinner8c62be82010-05-06 00:08:46 +00005389 FILETIME create, exit, kernel, user;
5390 HANDLE hProc;
5391 hProc = GetCurrentProcess();
5392 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5393 /* The fields of a FILETIME structure are the hi and lo part
5394 of a 64-bit value expressed in 100 nanosecond units.
5395 1e7 is one second in such units; 1e-7 the inverse.
5396 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5397 */
5398 return Py_BuildValue(
5399 "ddddd",
5400 (double)(user.dwHighDateTime*429.4967296 +
5401 user.dwLowDateTime*1e-7),
5402 (double)(kernel.dwHighDateTime*429.4967296 +
5403 kernel.dwLowDateTime*1e-7),
5404 (double)0,
5405 (double)0,
5406 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005407}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005408#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005409
5410#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005412"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005413Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005414#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005415
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005416
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005417#ifdef HAVE_GETSID
5418PyDoc_STRVAR(posix_getsid__doc__,
5419"getsid(pid) -> sid\n\n\
5420Call the system call getsid().");
5421
5422static PyObject *
5423posix_getsid(PyObject *self, PyObject *args)
5424{
Victor Stinner8c62be82010-05-06 00:08:46 +00005425 pid_t pid;
5426 int sid;
5427 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5428 return NULL;
5429 sid = getsid(pid);
5430 if (sid < 0)
5431 return posix_error();
5432 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005433}
5434#endif /* HAVE_GETSID */
5435
5436
Guido van Rossumb6775db1994-08-01 11:34:53 +00005437#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005438PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005439"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005440Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005441
Barry Warsaw53699e91996-12-10 23:23:01 +00005442static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005443posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005444{
Victor Stinner8c62be82010-05-06 00:08:46 +00005445 if (setsid() < 0)
5446 return posix_error();
5447 Py_INCREF(Py_None);
5448 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005449}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005450#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005451
Guido van Rossumb6775db1994-08-01 11:34:53 +00005452#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005453PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005454"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005455Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005456
Barry Warsaw53699e91996-12-10 23:23:01 +00005457static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005458posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005459{
Victor Stinner8c62be82010-05-06 00:08:46 +00005460 pid_t pid;
5461 int pgrp;
5462 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5463 return NULL;
5464 if (setpgid(pid, pgrp) < 0)
5465 return posix_error();
5466 Py_INCREF(Py_None);
5467 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005468}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005469#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005471
Guido van Rossumb6775db1994-08-01 11:34:53 +00005472#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005473PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005474"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005475Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005476
Barry Warsaw53699e91996-12-10 23:23:01 +00005477static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005478posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005479{
Victor Stinner8c62be82010-05-06 00:08:46 +00005480 int fd;
5481 pid_t pgid;
5482 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5483 return NULL;
5484 pgid = tcgetpgrp(fd);
5485 if (pgid < 0)
5486 return posix_error();
5487 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005488}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005489#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005490
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005491
Guido van Rossumb6775db1994-08-01 11:34:53 +00005492#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005493PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005494"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005495Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005496
Barry Warsaw53699e91996-12-10 23:23:01 +00005497static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005498posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005499{
Victor Stinner8c62be82010-05-06 00:08:46 +00005500 int fd;
5501 pid_t pgid;
5502 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5503 return NULL;
5504 if (tcsetpgrp(fd, pgid) < 0)
5505 return posix_error();
5506 Py_INCREF(Py_None);
5507 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005508}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005509#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005510
Guido van Rossum687dd131993-05-17 08:34:16 +00005511/* Functions acting on file descriptors */
5512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005513PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005514"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005515Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005516
Barry Warsaw53699e91996-12-10 23:23:01 +00005517static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005518posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005519{
Victor Stinner8c62be82010-05-06 00:08:46 +00005520 PyObject *ofile;
5521 char *file;
5522 int flag;
5523 int mode = 0777;
5524 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005525
5526#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005527 PyUnicodeObject *po;
5528 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5529 Py_BEGIN_ALLOW_THREADS
5530 /* PyUnicode_AS_UNICODE OK without thread
5531 lock as it is a simple dereference. */
5532 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5533 Py_END_ALLOW_THREADS
5534 if (fd < 0)
5535 return posix_error();
5536 return PyLong_FromLong((long)fd);
5537 }
5538 /* Drop the argument parsing error as narrow strings
5539 are also valid. */
5540 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005541#endif
5542
Victor Stinner8c62be82010-05-06 00:08:46 +00005543 if (!PyArg_ParseTuple(args, "O&i|i",
5544 PyUnicode_FSConverter, &ofile,
5545 &flag, &mode))
5546 return NULL;
5547 file = PyBytes_AsString(ofile);
5548 Py_BEGIN_ALLOW_THREADS
5549 fd = open(file, flag, mode);
5550 Py_END_ALLOW_THREADS
5551 if (fd < 0)
5552 return posix_error_with_allocated_filename(ofile);
5553 Py_DECREF(ofile);
5554 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005555}
5556
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005558PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005559"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005561
Barry Warsaw53699e91996-12-10 23:23:01 +00005562static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005563posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005564{
Victor Stinner8c62be82010-05-06 00:08:46 +00005565 int fd, res;
5566 if (!PyArg_ParseTuple(args, "i:close", &fd))
5567 return NULL;
5568 if (!_PyVerify_fd(fd))
5569 return posix_error();
5570 Py_BEGIN_ALLOW_THREADS
5571 res = close(fd);
5572 Py_END_ALLOW_THREADS
5573 if (res < 0)
5574 return posix_error();
5575 Py_INCREF(Py_None);
5576 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005577}
5578
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005579
Victor Stinner8c62be82010-05-06 00:08:46 +00005580PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005581"closerange(fd_low, fd_high)\n\n\
5582Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5583
5584static PyObject *
5585posix_closerange(PyObject *self, PyObject *args)
5586{
Victor Stinner8c62be82010-05-06 00:08:46 +00005587 int fd_from, fd_to, i;
5588 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5589 return NULL;
5590 Py_BEGIN_ALLOW_THREADS
5591 for (i = fd_from; i < fd_to; i++)
5592 if (_PyVerify_fd(i))
5593 close(i);
5594 Py_END_ALLOW_THREADS
5595 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005596}
5597
5598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005599PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005600"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005601Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005602
Barry Warsaw53699e91996-12-10 23:23:01 +00005603static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005604posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005605{
Victor Stinner8c62be82010-05-06 00:08:46 +00005606 int fd;
5607 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5608 return NULL;
5609 if (!_PyVerify_fd(fd))
5610 return posix_error();
5611 Py_BEGIN_ALLOW_THREADS
5612 fd = dup(fd);
5613 Py_END_ALLOW_THREADS
5614 if (fd < 0)
5615 return posix_error();
5616 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005617}
5618
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005619
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005621"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005622Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005623
Barry Warsaw53699e91996-12-10 23:23:01 +00005624static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005625posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005626{
Victor Stinner8c62be82010-05-06 00:08:46 +00005627 int fd, fd2, res;
5628 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5629 return NULL;
5630 if (!_PyVerify_fd_dup2(fd, fd2))
5631 return posix_error();
5632 Py_BEGIN_ALLOW_THREADS
5633 res = dup2(fd, fd2);
5634 Py_END_ALLOW_THREADS
5635 if (res < 0)
5636 return posix_error();
5637 Py_INCREF(Py_None);
5638 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005639}
5640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005643"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005644Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005645
Barry Warsaw53699e91996-12-10 23:23:01 +00005646static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005647posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005648{
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005650#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005651 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005652#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005653 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005654#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005655 PyObject *posobj;
5656 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5657 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005658#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005659 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5660 switch (how) {
5661 case 0: how = SEEK_SET; break;
5662 case 1: how = SEEK_CUR; break;
5663 case 2: how = SEEK_END; break;
5664 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005665#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005666
5667#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005668 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005669#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005670 pos = PyLong_Check(posobj) ?
5671 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005672#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005673 if (PyErr_Occurred())
5674 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005675
Victor Stinner8c62be82010-05-06 00:08:46 +00005676 if (!_PyVerify_fd(fd))
5677 return posix_error();
5678 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005679#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005680 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005681#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005682 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005683#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005684 Py_END_ALLOW_THREADS
5685 if (res < 0)
5686 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005687
5688#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005689 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005690#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005691 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005692#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005693}
5694
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005695
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005696PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005697"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005698Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005699
Barry Warsaw53699e91996-12-10 23:23:01 +00005700static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005701posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005702{
Victor Stinner8c62be82010-05-06 00:08:46 +00005703 int fd, size;
5704 Py_ssize_t n;
5705 PyObject *buffer;
5706 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5707 return NULL;
5708 if (size < 0) {
5709 errno = EINVAL;
5710 return posix_error();
5711 }
5712 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5713 if (buffer == NULL)
5714 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005715 if (!_PyVerify_fd(fd)) {
5716 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005717 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005718 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 Py_BEGIN_ALLOW_THREADS
5720 n = read(fd, PyBytes_AS_STRING(buffer), size);
5721 Py_END_ALLOW_THREADS
5722 if (n < 0) {
5723 Py_DECREF(buffer);
5724 return posix_error();
5725 }
5726 if (n != size)
5727 _PyBytes_Resize(&buffer, n);
5728 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005729}
5730
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005732PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005733"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005734Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005735
Barry Warsaw53699e91996-12-10 23:23:01 +00005736static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005737posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005738{
Victor Stinner8c62be82010-05-06 00:08:46 +00005739 Py_buffer pbuf;
5740 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005741 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005742
Victor Stinner8c62be82010-05-06 00:08:46 +00005743 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5744 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005745 if (!_PyVerify_fd(fd)) {
5746 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005747 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005748 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005749 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005751#if defined(MS_WIN64) || defined(MS_WINDOWS)
5752 if (len > INT_MAX)
5753 len = INT_MAX;
5754 size = write(fd, pbuf.buf, (int)len);
5755#else
Victor Stinner72344792011-01-11 00:04:12 +00005756 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005757#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005758 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005759 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005760 if (size < 0)
5761 return posix_error();
5762 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005763}
5764
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005765
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005766PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005767"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005768Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005769
Barry Warsaw53699e91996-12-10 23:23:01 +00005770static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005771posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005772{
Victor Stinner8c62be82010-05-06 00:08:46 +00005773 int fd;
5774 STRUCT_STAT st;
5775 int res;
5776 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5777 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005778#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005779 /* on OpenVMS we must ensure that all bytes are written to the file */
5780 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005781#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005782 if (!_PyVerify_fd(fd))
5783 return posix_error();
5784 Py_BEGIN_ALLOW_THREADS
5785 res = FSTAT(fd, &st);
5786 Py_END_ALLOW_THREADS
5787 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005788#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005789 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005790#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005792#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005793 }
Tim Peters5aa91602002-01-30 05:46:57 +00005794
Victor Stinner8c62be82010-05-06 00:08:46 +00005795 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005796}
5797
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005798PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005799"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005800Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005801connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005802
5803static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005804posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005805{
Victor Stinner8c62be82010-05-06 00:08:46 +00005806 int fd;
5807 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5808 return NULL;
5809 if (!_PyVerify_fd(fd))
5810 return PyBool_FromLong(0);
5811 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005812}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005813
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005814#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005816"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005817Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005818
Barry Warsaw53699e91996-12-10 23:23:01 +00005819static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005820posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005821{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005822#if defined(PYOS_OS2)
5823 HFILE read, write;
5824 APIRET rc;
5825
Victor Stinner8c62be82010-05-06 00:08:46 +00005826 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005827 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005829 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005830 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005831
5832 return Py_BuildValue("(ii)", read, write);
5833#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005834#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005835 int fds[2];
5836 int res;
5837 Py_BEGIN_ALLOW_THREADS
5838 res = pipe(fds);
5839 Py_END_ALLOW_THREADS
5840 if (res != 0)
5841 return posix_error();
5842 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005843#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 HANDLE read, write;
5845 int read_fd, write_fd;
5846 BOOL ok;
5847 Py_BEGIN_ALLOW_THREADS
5848 ok = CreatePipe(&read, &write, NULL, 0);
5849 Py_END_ALLOW_THREADS
5850 if (!ok)
5851 return win32_error("CreatePipe", NULL);
5852 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5853 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5854 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005855#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005856#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005857}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005858#endif /* HAVE_PIPE */
5859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005860
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005861#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005862PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005863"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005864Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005865
Barry Warsaw53699e91996-12-10 23:23:01 +00005866static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005867posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005868{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005869 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 char *filename;
5871 int mode = 0666;
5872 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005873 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5874 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005875 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005876 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005877 Py_BEGIN_ALLOW_THREADS
5878 res = mkfifo(filename, mode);
5879 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005880 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005881 if (res < 0)
5882 return posix_error();
5883 Py_INCREF(Py_None);
5884 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005885}
5886#endif
5887
5888
Neal Norwitz11690112002-07-30 01:08:28 +00005889#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005890PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005891"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005892Create a filesystem node (file, device special file or named pipe)\n\
5893named filename. mode specifies both the permissions to use and the\n\
5894type of node to be created, being combined (bitwise OR) with one of\n\
5895S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005896device defines the newly created device special file (probably using\n\
5897os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005898
5899
5900static PyObject *
5901posix_mknod(PyObject *self, PyObject *args)
5902{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005903 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005904 char *filename;
5905 int mode = 0600;
5906 int device = 0;
5907 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005908 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5909 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005911 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 Py_BEGIN_ALLOW_THREADS
5913 res = mknod(filename, mode, device);
5914 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005915 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005916 if (res < 0)
5917 return posix_error();
5918 Py_INCREF(Py_None);
5919 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005920}
5921#endif
5922
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005923#ifdef HAVE_DEVICE_MACROS
5924PyDoc_STRVAR(posix_major__doc__,
5925"major(device) -> major number\n\
5926Extracts a device major number from a raw device number.");
5927
5928static PyObject *
5929posix_major(PyObject *self, PyObject *args)
5930{
Victor Stinner8c62be82010-05-06 00:08:46 +00005931 int device;
5932 if (!PyArg_ParseTuple(args, "i:major", &device))
5933 return NULL;
5934 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005935}
5936
5937PyDoc_STRVAR(posix_minor__doc__,
5938"minor(device) -> minor number\n\
5939Extracts a device minor number from a raw device number.");
5940
5941static PyObject *
5942posix_minor(PyObject *self, PyObject *args)
5943{
Victor Stinner8c62be82010-05-06 00:08:46 +00005944 int device;
5945 if (!PyArg_ParseTuple(args, "i:minor", &device))
5946 return NULL;
5947 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005948}
5949
5950PyDoc_STRVAR(posix_makedev__doc__,
5951"makedev(major, minor) -> device number\n\
5952Composes a raw device number from the major and minor device numbers.");
5953
5954static PyObject *
5955posix_makedev(PyObject *self, PyObject *args)
5956{
Victor Stinner8c62be82010-05-06 00:08:46 +00005957 int major, minor;
5958 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5959 return NULL;
5960 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005961}
5962#endif /* device macros */
5963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005964
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005965#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005966PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005967"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005968Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005969
Barry Warsaw53699e91996-12-10 23:23:01 +00005970static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005971posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005972{
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 int fd;
5974 off_t length;
5975 int res;
5976 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005977
Victor Stinner8c62be82010-05-06 00:08:46 +00005978 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5979 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005980
5981#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005982 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005983#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005984 length = PyLong_Check(lenobj) ?
5985 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005986#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 if (PyErr_Occurred())
5988 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005989
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 Py_BEGIN_ALLOW_THREADS
5991 res = ftruncate(fd, length);
5992 Py_END_ALLOW_THREADS
5993 if (res < 0)
5994 return posix_error();
5995 Py_INCREF(Py_None);
5996 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005997}
5998#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005999
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006000#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006001PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006002"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006003Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006004
Fred Drake762e2061999-08-26 17:23:54 +00006005/* Save putenv() parameters as values here, so we can collect them when they
6006 * get re-set with another call for the same key. */
6007static PyObject *posix_putenv_garbage;
6008
Tim Peters5aa91602002-01-30 05:46:57 +00006009static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006010posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006011{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006012#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006013 wchar_t *s1, *s2;
6014 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006015#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006016 PyObject *os1, *os2;
6017 char *s1, *s2;
6018 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006019#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006020 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006021 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006022
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006023#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 if (!PyArg_ParseTuple(args,
6025 "uu:putenv",
6026 &s1, &s2))
6027 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00006028#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006029 if (!PyArg_ParseTuple(args,
6030 "O&O&:putenv",
6031 PyUnicode_FSConverter, &os1,
6032 PyUnicode_FSConverter, &os2))
6033 return NULL;
6034 s1 = PyBytes_AsString(os1);
6035 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006036#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00006037
6038#if defined(PYOS_OS2)
6039 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6040 APIRET rc;
6041
Guido van Rossumd48f2521997-12-05 22:19:34 +00006042 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006043 if (rc != NO_ERROR) {
6044 os2_error(rc);
6045 goto error;
6046 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006047
6048 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6049 APIRET rc;
6050
Guido van Rossumd48f2521997-12-05 22:19:34 +00006051 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006052 if (rc != NO_ERROR) {
6053 os2_error(rc);
6054 goto error;
6055 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006056 } else {
6057#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 /* XXX This can leak memory -- not easy to fix :-( */
6059 /* len includes space for a trailing \0; the size arg to
6060 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006061#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006062 len = wcslen(s1) + wcslen(s2) + 2;
6063 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006064#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006065 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006066 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006067#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006068 if (newstr == NULL) {
6069 PyErr_NoMemory();
6070 goto error;
6071 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006072#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006073 newenv = PyUnicode_AsUnicode(newstr);
6074 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6075 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006076 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006077 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006078 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006079#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 newenv = PyBytes_AS_STRING(newstr);
6081 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6082 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006083 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006084 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006085 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006086#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006087
Victor Stinner8c62be82010-05-06 00:08:46 +00006088 /* Install the first arg and newstr in posix_putenv_garbage;
6089 * this will cause previous value to be collected. This has to
6090 * happen after the real putenv() call because the old value
6091 * was still accessible until then. */
6092 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006093#ifdef MS_WINDOWS
6094 PyTuple_GET_ITEM(args, 0),
6095#else
6096 os1,
6097#endif
6098 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 /* really not much we can do; just leak */
6100 PyErr_Clear();
6101 }
6102 else {
6103 Py_DECREF(newstr);
6104 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006105
6106#if defined(PYOS_OS2)
6107 }
6108#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006109
Martin v. Löwis011e8422009-05-05 04:43:17 +00006110#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 Py_DECREF(os1);
6112 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006113#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006114 Py_RETURN_NONE;
6115
6116error:
6117#ifndef MS_WINDOWS
6118 Py_DECREF(os1);
6119 Py_DECREF(os2);
6120#endif
6121 Py_XDECREF(newstr);
6122 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006123}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006124#endif /* putenv */
6125
Guido van Rossumc524d952001-10-19 01:31:59 +00006126#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006127PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006128"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006129Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006130
6131static PyObject *
6132posix_unsetenv(PyObject *self, PyObject *args)
6133{
Victor Stinner84ae1182010-05-06 22:05:07 +00006134#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006136
Victor Stinner8c62be82010-05-06 00:08:46 +00006137 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6138 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006139#else
6140 PyObject *os1;
6141 char *s1;
6142
6143 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6144 PyUnicode_FSConverter, &os1))
6145 return NULL;
6146 s1 = PyBytes_AsString(os1);
6147#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006148
Victor Stinner8c62be82010-05-06 00:08:46 +00006149 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006150
Victor Stinner8c62be82010-05-06 00:08:46 +00006151 /* Remove the key from posix_putenv_garbage;
6152 * this will cause it to be collected. This has to
6153 * happen after the real unsetenv() call because the
6154 * old value was still accessible until then.
6155 */
6156 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006157#ifdef MS_WINDOWS
6158 PyTuple_GET_ITEM(args, 0)
6159#else
6160 os1
6161#endif
6162 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 /* really not much we can do; just leak */
6164 PyErr_Clear();
6165 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006166
Victor Stinner84ae1182010-05-06 22:05:07 +00006167#ifndef MS_WINDOWS
6168 Py_DECREF(os1);
6169#endif
6170 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006171}
6172#endif /* unsetenv */
6173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006174PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006175"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006176Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006177
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006178static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006179posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006180{
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 int code;
6182 char *message;
6183 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6184 return NULL;
6185 message = strerror(code);
6186 if (message == NULL) {
6187 PyErr_SetString(PyExc_ValueError,
6188 "strerror() argument out of range");
6189 return NULL;
6190 }
6191 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006192}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006193
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006194
Guido van Rossumc9641791998-08-04 15:26:23 +00006195#ifdef HAVE_SYS_WAIT_H
6196
Fred Drake106c1a02002-04-23 15:58:02 +00006197#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006198PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006199"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006200Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006201
6202static PyObject *
6203posix_WCOREDUMP(PyObject *self, PyObject *args)
6204{
Victor Stinner8c62be82010-05-06 00:08:46 +00006205 WAIT_TYPE status;
6206 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006207
Victor Stinner8c62be82010-05-06 00:08:46 +00006208 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6209 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006210
Victor Stinner8c62be82010-05-06 00:08:46 +00006211 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006212}
6213#endif /* WCOREDUMP */
6214
6215#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006216PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006217"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006218Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006219job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006220
6221static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006222posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006223{
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 WAIT_TYPE status;
6225 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006226
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6228 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006229
Victor Stinner8c62be82010-05-06 00:08:46 +00006230 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006231}
6232#endif /* WIFCONTINUED */
6233
Guido van Rossumc9641791998-08-04 15:26:23 +00006234#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006235PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006236"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006237Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006238
6239static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006240posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006241{
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 WAIT_TYPE status;
6243 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006244
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6246 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006247
Victor Stinner8c62be82010-05-06 00:08:46 +00006248 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006249}
6250#endif /* WIFSTOPPED */
6251
6252#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006253PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006254"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006255Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006256
6257static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006258posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006259{
Victor Stinner8c62be82010-05-06 00:08:46 +00006260 WAIT_TYPE status;
6261 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006262
Victor Stinner8c62be82010-05-06 00:08:46 +00006263 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6264 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006265
Victor Stinner8c62be82010-05-06 00:08:46 +00006266 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006267}
6268#endif /* WIFSIGNALED */
6269
6270#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006271PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006272"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006273Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006274system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006275
6276static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006277posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006278{
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 WAIT_TYPE status;
6280 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006281
Victor Stinner8c62be82010-05-06 00:08:46 +00006282 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6283 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006284
Victor Stinner8c62be82010-05-06 00:08:46 +00006285 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006286}
6287#endif /* WIFEXITED */
6288
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006289#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006290PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006291"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006292Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006293
6294static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006295posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006296{
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 WAIT_TYPE status;
6298 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006299
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6301 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006302
Victor Stinner8c62be82010-05-06 00:08:46 +00006303 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006304}
6305#endif /* WEXITSTATUS */
6306
6307#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006308PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006309"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006310Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006311value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006312
6313static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006314posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006315{
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 WAIT_TYPE status;
6317 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006318
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6320 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006321
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006323}
6324#endif /* WTERMSIG */
6325
6326#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006327PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006328"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006329Return the signal that stopped the process that provided\n\
6330the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006331
6332static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006333posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006334{
Victor Stinner8c62be82010-05-06 00:08:46 +00006335 WAIT_TYPE status;
6336 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006337
Victor Stinner8c62be82010-05-06 00:08:46 +00006338 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6339 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006340
Victor Stinner8c62be82010-05-06 00:08:46 +00006341 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006342}
6343#endif /* WSTOPSIG */
6344
6345#endif /* HAVE_SYS_WAIT_H */
6346
6347
Thomas Wouters477c8d52006-05-27 19:21:47 +00006348#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006349#ifdef _SCO_DS
6350/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6351 needed definitions in sys/statvfs.h */
6352#define _SVID3
6353#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006354#include <sys/statvfs.h>
6355
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006356static PyObject*
6357_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6359 if (v == NULL)
6360 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006361
6362#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6364 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6365 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6366 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6367 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6368 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6369 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6370 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6371 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6372 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006373#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6375 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6376 PyStructSequence_SET_ITEM(v, 2,
6377 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6378 PyStructSequence_SET_ITEM(v, 3,
6379 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6380 PyStructSequence_SET_ITEM(v, 4,
6381 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6382 PyStructSequence_SET_ITEM(v, 5,
6383 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6384 PyStructSequence_SET_ITEM(v, 6,
6385 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6386 PyStructSequence_SET_ITEM(v, 7,
6387 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6388 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6389 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006390#endif
6391
Victor Stinner8c62be82010-05-06 00:08:46 +00006392 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006393}
6394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006395PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006396"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006397Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006398
6399static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006400posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006401{
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 int fd, res;
6403 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006404
Victor Stinner8c62be82010-05-06 00:08:46 +00006405 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6406 return NULL;
6407 Py_BEGIN_ALLOW_THREADS
6408 res = fstatvfs(fd, &st);
6409 Py_END_ALLOW_THREADS
6410 if (res != 0)
6411 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006412
Victor Stinner8c62be82010-05-06 00:08:46 +00006413 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006414}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006415#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006416
6417
Thomas Wouters477c8d52006-05-27 19:21:47 +00006418#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006419#include <sys/statvfs.h>
6420
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006421PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006422"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006423Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006424
6425static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006426posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006427{
Victor Stinner8c62be82010-05-06 00:08:46 +00006428 char *path;
6429 int res;
6430 struct statvfs st;
6431 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6432 return NULL;
6433 Py_BEGIN_ALLOW_THREADS
6434 res = statvfs(path, &st);
6435 Py_END_ALLOW_THREADS
6436 if (res != 0)
6437 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006438
Victor Stinner8c62be82010-05-06 00:08:46 +00006439 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006440}
6441#endif /* HAVE_STATVFS */
6442
Fred Drakec9680921999-12-13 16:37:25 +00006443/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6444 * It maps strings representing configuration variable names to
6445 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006446 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006447 * rarely-used constants. There are three separate tables that use
6448 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006449 *
6450 * This code is always included, even if none of the interfaces that
6451 * need it are included. The #if hackery needed to avoid it would be
6452 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006453 */
6454struct constdef {
6455 char *name;
6456 long value;
6457};
6458
Fred Drake12c6e2d1999-12-14 21:25:03 +00006459static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006460conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006461 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006462{
Christian Heimes217cfd12007-12-02 14:31:20 +00006463 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006464 *valuep = PyLong_AS_LONG(arg);
6465 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006466 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006467 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006468 /* look up the value in the table using a binary search */
6469 size_t lo = 0;
6470 size_t mid;
6471 size_t hi = tablesize;
6472 int cmp;
6473 const char *confname;
6474 if (!PyUnicode_Check(arg)) {
6475 PyErr_SetString(PyExc_TypeError,
6476 "configuration names must be strings or integers");
6477 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006479 confname = _PyUnicode_AsString(arg);
6480 if (confname == NULL)
6481 return 0;
6482 while (lo < hi) {
6483 mid = (lo + hi) / 2;
6484 cmp = strcmp(confname, table[mid].name);
6485 if (cmp < 0)
6486 hi = mid;
6487 else if (cmp > 0)
6488 lo = mid + 1;
6489 else {
6490 *valuep = table[mid].value;
6491 return 1;
6492 }
6493 }
6494 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6495 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006496 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006497}
6498
6499
6500#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6501static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006502#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006504#endif
6505#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006507#endif
Fred Drakec9680921999-12-13 16:37:25 +00006508#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
6511#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006513#endif
6514#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006515 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006516#endif
6517#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006518 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006519#endif
6520#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006522#endif
6523#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006524 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006525#endif
6526#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006527 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006528#endif
6529#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006530 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006531#endif
6532#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006533 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006534#endif
6535#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006536 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006537#endif
6538#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006540#endif
6541#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006542 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006543#endif
6544#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006545 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006546#endif
6547#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006549#endif
6550#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006551 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006552#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006553#ifdef _PC_ACL_ENABLED
6554 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6555#endif
6556#ifdef _PC_MIN_HOLE_SIZE
6557 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6558#endif
6559#ifdef _PC_ALLOC_SIZE_MIN
6560 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6561#endif
6562#ifdef _PC_REC_INCR_XFER_SIZE
6563 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6564#endif
6565#ifdef _PC_REC_MAX_XFER_SIZE
6566 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6567#endif
6568#ifdef _PC_REC_MIN_XFER_SIZE
6569 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6570#endif
6571#ifdef _PC_REC_XFER_ALIGN
6572 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6573#endif
6574#ifdef _PC_SYMLINK_MAX
6575 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6576#endif
6577#ifdef _PC_XATTR_ENABLED
6578 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6579#endif
6580#ifdef _PC_XATTR_EXISTS
6581 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6582#endif
6583#ifdef _PC_TIMESTAMP_RESOLUTION
6584 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6585#endif
Fred Drakec9680921999-12-13 16:37:25 +00006586};
6587
Fred Drakec9680921999-12-13 16:37:25 +00006588static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006589conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006590{
6591 return conv_confname(arg, valuep, posix_constants_pathconf,
6592 sizeof(posix_constants_pathconf)
6593 / sizeof(struct constdef));
6594}
6595#endif
6596
6597#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006598PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006599"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006600Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006601If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006602
6603static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006604posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006605{
6606 PyObject *result = NULL;
6607 int name, fd;
6608
Fred Drake12c6e2d1999-12-14 21:25:03 +00006609 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6610 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006611 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006612
Stefan Krah0e803b32010-11-26 16:16:47 +00006613 errno = 0;
6614 limit = fpathconf(fd, name);
6615 if (limit == -1 && errno != 0)
6616 posix_error();
6617 else
6618 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006619 }
6620 return result;
6621}
6622#endif
6623
6624
6625#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006626PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006627"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006628Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006629If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006630
6631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006632posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006633{
6634 PyObject *result = NULL;
6635 int name;
6636 char *path;
6637
6638 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6639 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006641
Victor Stinner8c62be82010-05-06 00:08:46 +00006642 errno = 0;
6643 limit = pathconf(path, name);
6644 if (limit == -1 && errno != 0) {
6645 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006646 /* could be a path or name problem */
6647 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006648 else
Stefan Krah99439262010-11-26 12:58:05 +00006649 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006650 }
6651 else
6652 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006653 }
6654 return result;
6655}
6656#endif
6657
6658#ifdef HAVE_CONFSTR
6659static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006660#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006662#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006663#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006665#endif
6666#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006668#endif
Fred Draked86ed291999-12-15 15:34:33 +00006669#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006671#endif
6672#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006674#endif
6675#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006677#endif
6678#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006680#endif
Fred Drakec9680921999-12-13 16:37:25 +00006681#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006683#endif
6684#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006686#endif
6687#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006689#endif
6690#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006692#endif
6693#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006695#endif
6696#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006698#endif
6699#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006701#endif
6702#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006704#endif
Fred Draked86ed291999-12-15 15:34:33 +00006705#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006707#endif
Fred Drakec9680921999-12-13 16:37:25 +00006708#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006710#endif
Fred Draked86ed291999-12-15 15:34:33 +00006711#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006713#endif
6714#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006716#endif
6717#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006719#endif
6720#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006722#endif
Fred Drakec9680921999-12-13 16:37:25 +00006723#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006725#endif
6726#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006728#endif
6729#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006731#endif
6732#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006734#endif
6735#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006736 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006737#endif
6738#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006740#endif
6741#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006742 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006743#endif
6744#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006746#endif
6747#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006748 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006749#endif
6750#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006751 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006752#endif
6753#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006754 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006755#endif
6756#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006758#endif
6759#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006760 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006761#endif
6762#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006764#endif
6765#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006767#endif
6768#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006769 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006770#endif
Fred Draked86ed291999-12-15 15:34:33 +00006771#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006772 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006773#endif
6774#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006775 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006776#endif
6777#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006779#endif
6780#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006781 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006782#endif
6783#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006784 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006785#endif
6786#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006788#endif
6789#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006791#endif
6792#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006794#endif
6795#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006797#endif
6798#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006799 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006800#endif
6801#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006803#endif
6804#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006806#endif
6807#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006809#endif
Fred Drakec9680921999-12-13 16:37:25 +00006810};
6811
6812static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006813conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006814{
6815 return conv_confname(arg, valuep, posix_constants_confstr,
6816 sizeof(posix_constants_confstr)
6817 / sizeof(struct constdef));
6818}
6819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006820PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006821"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006822Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006823
6824static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006825posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006826{
6827 PyObject *result = NULL;
6828 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006829 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006830 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006831
Victor Stinnercb043522010-09-10 23:49:04 +00006832 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6833 return NULL;
6834
6835 errno = 0;
6836 len = confstr(name, buffer, sizeof(buffer));
6837 if (len == 0) {
6838 if (errno) {
6839 posix_error();
6840 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006841 }
6842 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006843 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006844 }
6845 }
Victor Stinnercb043522010-09-10 23:49:04 +00006846
6847 if ((unsigned int)len >= sizeof(buffer)) {
6848 char *buf = PyMem_Malloc(len);
6849 if (buf == NULL)
6850 return PyErr_NoMemory();
6851 confstr(name, buf, len);
6852 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6853 PyMem_Free(buf);
6854 }
6855 else
6856 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006857 return result;
6858}
6859#endif
6860
6861
6862#ifdef HAVE_SYSCONF
6863static struct constdef posix_constants_sysconf[] = {
6864#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
6873#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006875#endif
6876#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006878#endif
6879#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
6882#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
6891#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006893#endif
Fred Draked86ed291999-12-15 15:34:33 +00006894#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006896#endif
6897#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006899#endif
Fred Drakec9680921999-12-13 16:37:25 +00006900#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
Fred Drakec9680921999-12-13 16:37:25 +00006903#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
6909#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006911#endif
6912#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006914#endif
6915#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006917#endif
Fred Draked86ed291999-12-15 15:34:33 +00006918#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006920#endif
Fred Drakec9680921999-12-13 16:37:25 +00006921#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006923#endif
6924#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006926#endif
6927#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006929#endif
6930#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006932#endif
6933#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006935#endif
Fred Draked86ed291999-12-15 15:34:33 +00006936#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006938#endif
Fred Drakec9680921999-12-13 16:37:25 +00006939#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006941#endif
6942#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006944#endif
6945#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006947#endif
6948#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006950#endif
6951#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006953#endif
6954#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006956#endif
6957#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006959#endif
6960#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
6963#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006965#endif
6966#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006968#endif
6969#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
6972#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
6978#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006980#endif
6981#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006983#endif
6984#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006986#endif
6987#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006989#endif
6990#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006992#endif
6993#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006995#endif
6996#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006998#endif
6999#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00007001#endif
7002#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
7005#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00007007#endif
Fred Draked86ed291999-12-15 15:34:33 +00007008#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00007010#endif
Fred Drakec9680921999-12-13 16:37:25 +00007011#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
7017#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
Fred Draked86ed291999-12-15 15:34:33 +00007020#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00007022#endif
Fred Drakec9680921999-12-13 16:37:25 +00007023#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
Fred Draked86ed291999-12-15 15:34:33 +00007026#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00007028#endif
7029#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00007031#endif
Fred Drakec9680921999-12-13 16:37:25 +00007032#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007034#endif
7035#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007037#endif
7038#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
7041#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
Fred Draked86ed291999-12-15 15:34:33 +00007044#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007046#endif
Fred Drakec9680921999-12-13 16:37:25 +00007047#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007049#endif
7050#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
7056#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007058#endif
7059#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
7065#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007067#endif
Fred Draked86ed291999-12-15 15:34:33 +00007068#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007070#endif
Fred Drakec9680921999-12-13 16:37:25 +00007071#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007073#endif
7074#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007076#endif
Fred Draked86ed291999-12-15 15:34:33 +00007077#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007079#endif
Fred Drakec9680921999-12-13 16:37:25 +00007080#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007103#endif
7104#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007106#endif
Fred Draked86ed291999-12-15 15:34:33 +00007107#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007109#endif
7110#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007112#endif
Fred Drakec9680921999-12-13 16:37:25 +00007113#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007115#endif
7116#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007118#endif
7119#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007121#endif
7122#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007124#endif
7125#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
7128#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007130#endif
7131#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007133#endif
7134#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007136#endif
7137#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007139#endif
7140#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007142#endif
7143#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
7146#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
7149#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
7155#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007157#endif
7158#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007160#endif
7161#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
7164#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007166#endif
7167#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007169#endif
7170#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007172#endif
7173#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007175#endif
7176#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007178#endif
7179#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007181#endif
7182#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007184#endif
7185#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
7194#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007196#endif
7197#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
7206#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007208#endif
7209#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
7212#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007214#endif
7215#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007217#endif
Fred Draked86ed291999-12-15 15:34:33 +00007218#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007220#endif
Fred Drakec9680921999-12-13 16:37:25 +00007221#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
7245#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007247#endif
7248#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007250#endif
7251#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007280#endif
7281#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007283#endif
7284#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007286#endif
7287#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007289#endif
7290#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007292#endif
7293#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007295#endif
7296#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007298#endif
7299#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007301#endif
7302#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007303 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007304#endif
7305#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007306 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007307#endif
7308#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007310#endif
7311#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007313#endif
7314#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007316#endif
7317#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007319#endif
7320#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007321 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007322#endif
7323#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007324 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007325#endif
7326#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007327 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007328#endif
7329#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007331#endif
7332#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007334#endif
7335#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007337#endif
7338#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007339 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007340#endif
7341#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007342 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007343#endif
7344#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007345 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007346#endif
7347#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007348 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007349#endif
7350#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007351 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007352#endif
7353#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007355#endif
7356};
7357
7358static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007359conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007360{
7361 return conv_confname(arg, valuep, posix_constants_sysconf,
7362 sizeof(posix_constants_sysconf)
7363 / sizeof(struct constdef));
7364}
7365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007366PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007367"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007368Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007369
7370static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007371posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007372{
7373 PyObject *result = NULL;
7374 int name;
7375
7376 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7377 int value;
7378
7379 errno = 0;
7380 value = sysconf(name);
7381 if (value == -1 && errno != 0)
7382 posix_error();
7383 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007384 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007385 }
7386 return result;
7387}
7388#endif
7389
7390
Fred Drakebec628d1999-12-15 18:31:10 +00007391/* This code is used to ensure that the tables of configuration value names
7392 * are in sorted order as required by conv_confname(), and also to build the
7393 * the exported dictionaries that are used to publish information about the
7394 * names available on the host platform.
7395 *
7396 * Sorting the table at runtime ensures that the table is properly ordered
7397 * when used, even for platforms we're not able to test on. It also makes
7398 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007399 */
Fred Drakebec628d1999-12-15 18:31:10 +00007400
7401static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007402cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007403{
7404 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007405 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007406 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007407 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007408
7409 return strcmp(c1->name, c2->name);
7410}
7411
7412static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007413setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007415{
Fred Drakebec628d1999-12-15 18:31:10 +00007416 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007417 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007418
7419 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7420 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007421 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007422 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007423
Barry Warsaw3155db32000-04-13 15:20:40 +00007424 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007425 PyObject *o = PyLong_FromLong(table[i].value);
7426 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7427 Py_XDECREF(o);
7428 Py_DECREF(d);
7429 return -1;
7430 }
7431 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007432 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007433 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007434}
7435
Fred Drakebec628d1999-12-15 18:31:10 +00007436/* Return -1 on failure, 0 on success. */
7437static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007438setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007439{
7440#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007441 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007442 sizeof(posix_constants_pathconf)
7443 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007444 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007445 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007446#endif
7447#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007448 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007449 sizeof(posix_constants_confstr)
7450 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007451 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007452 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007453#endif
7454#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007455 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007456 sizeof(posix_constants_sysconf)
7457 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007458 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007459 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007460#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007461 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007462}
Fred Draked86ed291999-12-15 15:34:33 +00007463
7464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007465PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007466"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007467Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007468in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007469
7470static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007471posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007472{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007473 abort();
7474 /*NOTREACHED*/
7475 Py_FatalError("abort() called from Python code didn't abort!");
7476 return NULL;
7477}
Fred Drakebec628d1999-12-15 18:31:10 +00007478
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007479#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007480PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007481"startfile(filepath [, operation]) - Start a file with its associated\n\
7482application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007483\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007484When \"operation\" is not specified or \"open\", this acts like\n\
7485double-clicking the file in Explorer, or giving the file name as an\n\
7486argument to the DOS \"start\" command: the file is opened with whatever\n\
7487application (if any) its extension is associated.\n\
7488When another \"operation\" is given, it specifies what should be done with\n\
7489the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007490\n\
7491startfile returns as soon as the associated application is launched.\n\
7492There is no option to wait for the application to close, and no way\n\
7493to retrieve the application's exit status.\n\
7494\n\
7495The filepath is relative to the current directory. If you want to use\n\
7496an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007497the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007498
7499static PyObject *
7500win32_startfile(PyObject *self, PyObject *args)
7501{
Victor Stinner8c62be82010-05-06 00:08:46 +00007502 PyObject *ofilepath;
7503 char *filepath;
7504 char *operation = NULL;
7505 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007506
Victor Stinner8c62be82010-05-06 00:08:46 +00007507 PyObject *unipath, *woperation = NULL;
7508 if (!PyArg_ParseTuple(args, "U|s:startfile",
7509 &unipath, &operation)) {
7510 PyErr_Clear();
7511 goto normal;
7512 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007513
Victor Stinner8c62be82010-05-06 00:08:46 +00007514 if (operation) {
7515 woperation = PyUnicode_DecodeASCII(operation,
7516 strlen(operation), NULL);
7517 if (!woperation) {
7518 PyErr_Clear();
7519 operation = NULL;
7520 goto normal;
7521 }
7522 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007523
Victor Stinner8c62be82010-05-06 00:08:46 +00007524 Py_BEGIN_ALLOW_THREADS
7525 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7526 PyUnicode_AS_UNICODE(unipath),
7527 NULL, NULL, SW_SHOWNORMAL);
7528 Py_END_ALLOW_THREADS
7529
7530 Py_XDECREF(woperation);
7531 if (rc <= (HINSTANCE)32) {
7532 PyObject *errval = win32_error_unicode("startfile",
7533 PyUnicode_AS_UNICODE(unipath));
7534 return errval;
7535 }
7536 Py_INCREF(Py_None);
7537 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007538
7539normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7541 PyUnicode_FSConverter, &ofilepath,
7542 &operation))
7543 return NULL;
7544 filepath = PyBytes_AsString(ofilepath);
7545 Py_BEGIN_ALLOW_THREADS
7546 rc = ShellExecute((HWND)0, operation, filepath,
7547 NULL, NULL, SW_SHOWNORMAL);
7548 Py_END_ALLOW_THREADS
7549 if (rc <= (HINSTANCE)32) {
7550 PyObject *errval = win32_error("startfile", filepath);
7551 Py_DECREF(ofilepath);
7552 return errval;
7553 }
7554 Py_DECREF(ofilepath);
7555 Py_INCREF(Py_None);
7556 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007557}
7558#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007559
Martin v. Löwis438b5342002-12-27 10:16:42 +00007560#ifdef HAVE_GETLOADAVG
7561PyDoc_STRVAR(posix_getloadavg__doc__,
7562"getloadavg() -> (float, float, float)\n\n\
7563Return the number of processes in the system run queue averaged over\n\
7564the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7565was unobtainable");
7566
7567static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007568posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007569{
7570 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007571 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007572 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7573 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007574 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007575 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007576}
7577#endif
7578
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007579#ifdef MS_WINDOWS
7580
7581PyDoc_STRVAR(win32_urandom__doc__,
7582"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007583Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007584
7585typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7586 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7587 DWORD dwFlags );
7588typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7589 BYTE *pbBuffer );
7590
7591static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007592/* This handle is never explicitly released. Instead, the operating
7593 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007594static HCRYPTPROV hCryptProv = 0;
7595
Tim Peters4ad82172004-08-30 17:02:04 +00007596static PyObject*
7597win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007598{
Victor Stinner8c62be82010-05-06 00:08:46 +00007599 int howMany;
7600 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007601
Victor Stinner8c62be82010-05-06 00:08:46 +00007602 /* Read arguments */
7603 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7604 return NULL;
7605 if (howMany < 0)
7606 return PyErr_Format(PyExc_ValueError,
7607 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007608
Victor Stinner8c62be82010-05-06 00:08:46 +00007609 if (hCryptProv == 0) {
7610 HINSTANCE hAdvAPI32 = NULL;
7611 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007612
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 /* Obtain handle to the DLL containing CryptoAPI
7614 This should not fail */
7615 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7616 if(hAdvAPI32 == NULL)
7617 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007618
Victor Stinner8c62be82010-05-06 00:08:46 +00007619 /* Obtain pointers to the CryptoAPI functions
7620 This will fail on some early versions of Win95 */
7621 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7622 hAdvAPI32,
7623 "CryptAcquireContextA");
7624 if (pCryptAcquireContext == NULL)
7625 return PyErr_Format(PyExc_NotImplementedError,
7626 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007627
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7629 hAdvAPI32, "CryptGenRandom");
7630 if (pCryptGenRandom == NULL)
7631 return PyErr_Format(PyExc_NotImplementedError,
7632 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007633
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 /* Acquire context */
7635 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7636 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7637 return win32_error("CryptAcquireContext", NULL);
7638 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007639
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 /* Allocate bytes */
7641 result = PyBytes_FromStringAndSize(NULL, howMany);
7642 if (result != NULL) {
7643 /* Get random data */
7644 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7645 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7646 PyBytes_AS_STRING(result))) {
7647 Py_DECREF(result);
7648 return win32_error("CryptGenRandom", NULL);
7649 }
7650 }
7651 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007652}
7653#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007654
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007655PyDoc_STRVAR(device_encoding__doc__,
7656"device_encoding(fd) -> str\n\n\
7657Return a string describing the encoding of the device\n\
7658if the output is a terminal; else return None.");
7659
7660static PyObject *
7661device_encoding(PyObject *self, PyObject *args)
7662{
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 int fd;
7664 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7665 return NULL;
7666 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7667 Py_INCREF(Py_None);
7668 return Py_None;
7669 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007670#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007671 if (fd == 0) {
7672 char buf[100];
7673 sprintf(buf, "cp%d", GetConsoleCP());
7674 return PyUnicode_FromString(buf);
7675 }
7676 if (fd == 1 || fd == 2) {
7677 char buf[100];
7678 sprintf(buf, "cp%d", GetConsoleOutputCP());
7679 return PyUnicode_FromString(buf);
7680 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007681#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007682 {
7683 char *codeset = nl_langinfo(CODESET);
7684 if (codeset != NULL && codeset[0] != 0)
7685 return PyUnicode_FromString(codeset);
7686 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007687#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007688 Py_INCREF(Py_None);
7689 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007690}
7691
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007692#ifdef __VMS
7693/* Use openssl random routine */
7694#include <openssl/rand.h>
7695PyDoc_STRVAR(vms_urandom__doc__,
7696"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007697Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007698
7699static PyObject*
7700vms_urandom(PyObject *self, PyObject *args)
7701{
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 int howMany;
7703 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007704
Victor Stinner8c62be82010-05-06 00:08:46 +00007705 /* Read arguments */
7706 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7707 return NULL;
7708 if (howMany < 0)
7709 return PyErr_Format(PyExc_ValueError,
7710 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007711
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 /* Allocate bytes */
7713 result = PyBytes_FromStringAndSize(NULL, howMany);
7714 if (result != NULL) {
7715 /* Get random data */
7716 if (RAND_pseudo_bytes((unsigned char*)
7717 PyBytes_AS_STRING(result),
7718 howMany) < 0) {
7719 Py_DECREF(result);
7720 return PyErr_Format(PyExc_ValueError,
7721 "RAND_pseudo_bytes");
7722 }
7723 }
7724 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007725}
7726#endif
7727
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007728#ifdef HAVE_SETRESUID
7729PyDoc_STRVAR(posix_setresuid__doc__,
7730"setresuid(ruid, euid, suid)\n\n\
7731Set the current process's real, effective, and saved user ids.");
7732
7733static PyObject*
7734posix_setresuid (PyObject *self, PyObject *args)
7735{
Victor Stinner8c62be82010-05-06 00:08:46 +00007736 /* We assume uid_t is no larger than a long. */
7737 long ruid, euid, suid;
7738 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7739 return NULL;
7740 if (setresuid(ruid, euid, suid) < 0)
7741 return posix_error();
7742 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007743}
7744#endif
7745
7746#ifdef HAVE_SETRESGID
7747PyDoc_STRVAR(posix_setresgid__doc__,
7748"setresgid(rgid, egid, sgid)\n\n\
7749Set the current process's real, effective, and saved group ids.");
7750
7751static PyObject*
7752posix_setresgid (PyObject *self, PyObject *args)
7753{
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 /* We assume uid_t is no larger than a long. */
7755 long rgid, egid, sgid;
7756 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7757 return NULL;
7758 if (setresgid(rgid, egid, sgid) < 0)
7759 return posix_error();
7760 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007761}
7762#endif
7763
7764#ifdef HAVE_GETRESUID
7765PyDoc_STRVAR(posix_getresuid__doc__,
7766"getresuid() -> (ruid, euid, suid)\n\n\
7767Get tuple of the current process's real, effective, and saved user ids.");
7768
7769static PyObject*
7770posix_getresuid (PyObject *self, PyObject *noargs)
7771{
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 uid_t ruid, euid, suid;
7773 long l_ruid, l_euid, l_suid;
7774 if (getresuid(&ruid, &euid, &suid) < 0)
7775 return posix_error();
7776 /* Force the values into long's as we don't know the size of uid_t. */
7777 l_ruid = ruid;
7778 l_euid = euid;
7779 l_suid = suid;
7780 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007781}
7782#endif
7783
7784#ifdef HAVE_GETRESGID
7785PyDoc_STRVAR(posix_getresgid__doc__,
7786"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007787Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007788
7789static PyObject*
7790posix_getresgid (PyObject *self, PyObject *noargs)
7791{
Victor Stinner8c62be82010-05-06 00:08:46 +00007792 uid_t rgid, egid, sgid;
7793 long l_rgid, l_egid, l_sgid;
7794 if (getresgid(&rgid, &egid, &sgid) < 0)
7795 return posix_error();
7796 /* Force the values into long's as we don't know the size of uid_t. */
7797 l_rgid = rgid;
7798 l_egid = egid;
7799 l_sgid = sgid;
7800 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007801}
7802#endif
7803
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007804static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007806#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007807 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007808#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007810#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007811 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007812#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007813 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007814#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007816#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007817#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007818 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007819#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007820#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007821 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007822#endif /* HAVE_LCHMOD */
7823#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007824 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007825#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007826#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007828#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007829#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007830 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007831#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007832#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007833 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007834#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007835#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007837#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007838#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7840 METH_NOARGS, posix_getcwd__doc__},
7841 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7842 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007843#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007844#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007846#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7848 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7849 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007850#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007852#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007853#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007855#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007856#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007857 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007858#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007859 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7860 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7861 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007862 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007863#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007865#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007866#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007867 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007868 win_symlink__doc__},
7869#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007870#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007872#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007874#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007875 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007876#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7878 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7879 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007880#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007882#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007884#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007885 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7886 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007887#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007888#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7890 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007891#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7893 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007894#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007895#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007896#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007898#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007899#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007900 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007901#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007902#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007903 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007904#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007905#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007907#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007908#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007910#endif /* HAVE_GETEGID */
7911#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007913#endif /* HAVE_GETEUID */
7914#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007916#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007917#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007919#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007920 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007921#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007922 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007923#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007924#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007926#endif /* HAVE_GETPPID */
7927#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007929#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007930#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007932#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007933#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007934 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007935#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007936#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007937 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007938#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007939#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007940 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007941#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007942#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7944 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007945 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007946#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007947#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007949#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007950#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007952#endif /* HAVE_SETEUID */
7953#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007955#endif /* HAVE_SETEGID */
7956#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007958#endif /* HAVE_SETREUID */
7959#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007961#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007962#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007964#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007965#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007966 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007967#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007968#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007970#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007971#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007973#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007974#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007976#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007977#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007979#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007980#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007982#endif /* HAVE_WAIT3 */
7983#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007985#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007986#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007988#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007989#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007991#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007992#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007993 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007994#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007995#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007996 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007997#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007998#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008000#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00008001#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008003#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"open", posix_open, METH_VARARGS, posix_open__doc__},
8005 {"close", posix_close, METH_VARARGS, posix_close__doc__},
8006 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
8007 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
8008 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
8009 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
8010 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
8011 {"read", posix_read, METH_VARARGS, posix_read__doc__},
8012 {"write", posix_write, METH_VARARGS, posix_write__doc__},
8013 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
8014 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008015#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008017#endif
8018#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00008019 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008020#endif
Neal Norwitz11690112002-07-30 01:08:28 +00008021#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008023#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008024#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00008025 {"major", posix_major, METH_VARARGS, posix_major__doc__},
8026 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
8027 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008028#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008029#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008031#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008032#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008034#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008035#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00008037#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008038 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008039#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008041#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008042#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008044#endif
8045#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008046 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008047#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008048#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008049#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008051#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008052#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008054#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008055#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008057#endif /* WIFSTOPPED */
8058#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008060#endif /* WIFSIGNALED */
8061#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008062 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008063#endif /* WIFEXITED */
8064#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008065 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008066#endif /* WEXITSTATUS */
8067#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008068 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008069#endif /* WTERMSIG */
8070#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008072#endif /* WSTOPSIG */
8073#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008074#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008075 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008076#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008077#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008079#endif
Fred Drakec9680921999-12-13 16:37:25 +00008080#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008082#endif
8083#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008085#endif
8086#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008088#endif
8089#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008091#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008092 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008093#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008094 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008095 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008096 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -05008097 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +00008098#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008099#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008101#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008102 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008104 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008105 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008107 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008108#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008110#endif
8111#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008113#endif
8114#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008116#endif
8117#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008119#endif
8120
Victor Stinner8c62be82010-05-06 00:08:46 +00008121 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008122};
8123
8124
Barry Warsaw4a342091996-12-19 23:50:02 +00008125static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008126ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008127{
Victor Stinner8c62be82010-05-06 00:08:46 +00008128 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008129}
8130
Guido van Rossumd48f2521997-12-05 22:19:34 +00008131#if defined(PYOS_OS2)
8132/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008133static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008134{
8135 APIRET rc;
8136 ULONG values[QSV_MAX+1];
8137 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008138 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008139
8140 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008141 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008142 Py_END_ALLOW_THREADS
8143
8144 if (rc != NO_ERROR) {
8145 os2_error(rc);
8146 return -1;
8147 }
8148
Fred Drake4d1e64b2002-04-15 19:40:07 +00008149 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8150 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8151 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8152 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8153 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8154 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8155 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008156
8157 switch (values[QSV_VERSION_MINOR]) {
8158 case 0: ver = "2.00"; break;
8159 case 10: ver = "2.10"; break;
8160 case 11: ver = "2.11"; break;
8161 case 30: ver = "3.00"; break;
8162 case 40: ver = "4.00"; break;
8163 case 50: ver = "5.00"; break;
8164 default:
Tim Peters885d4572001-11-28 20:27:42 +00008165 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008166 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008167 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008168 ver = &tmp[0];
8169 }
8170
8171 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008172 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008173 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008174
8175 /* Add Indicator of Which Drive was Used to Boot the System */
8176 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8177 tmp[1] = ':';
8178 tmp[2] = '\0';
8179
Fred Drake4d1e64b2002-04-15 19:40:07 +00008180 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008181}
8182#endif
8183
Brian Curtin52173d42010-12-02 18:29:18 +00008184#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008185static int
Brian Curtin52173d42010-12-02 18:29:18 +00008186enable_symlink()
8187{
8188 HANDLE tok;
8189 TOKEN_PRIVILEGES tok_priv;
8190 LUID luid;
8191 int meth_idx = 0;
8192
8193 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008194 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008195
8196 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008197 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008198
8199 tok_priv.PrivilegeCount = 1;
8200 tok_priv.Privileges[0].Luid = luid;
8201 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8202
8203 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8204 sizeof(TOKEN_PRIVILEGES),
8205 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008206 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008207
Brian Curtin3b4499c2010-12-28 14:31:47 +00008208 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8209 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008210}
8211#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8212
Barry Warsaw4a342091996-12-19 23:50:02 +00008213static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008214all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008215{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008216#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008218#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008219#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008221#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008222#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008224#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008225#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008226 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008227#endif
Fred Drakec9680921999-12-13 16:37:25 +00008228#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008230#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008231#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008232 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008233#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008234#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008236#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008237#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008239#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008240#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008241 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008242#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008243#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008245#endif
8246#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008248#endif
8249#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008250 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008251#endif
8252#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008253 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008254#endif
8255#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008256 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008257#endif
8258#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008260#endif
8261#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008262 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008263#endif
8264#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008266#endif
8267#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008269#endif
8270#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008272#endif
8273#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008275#endif
8276#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008278#endif
8279#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008281#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008282#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008284#endif
8285#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008286 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008287#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008288#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008290#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008291#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008293#endif
8294#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008296#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008297
Tim Peters5aa91602002-01-30 05:46:57 +00008298/* MS Windows */
8299#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008300 /* Don't inherit in child processes. */
8301 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008302#endif
8303#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 /* Optimize for short life (keep in memory). */
8305 /* MS forgot to define this one with a non-underscore form too. */
8306 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008307#endif
8308#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008309 /* Automatically delete when last handle is closed. */
8310 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008311#endif
8312#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 /* Optimize for random access. */
8314 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008315#endif
8316#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008317 /* Optimize for sequential access. */
8318 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008319#endif
8320
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008321/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008322#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008323 /* Send a SIGIO signal whenever input or output
8324 becomes available on file descriptor */
8325 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008326#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008327#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 /* Direct disk access. */
8329 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008330#endif
8331#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008332 /* Must be a directory. */
8333 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008334#endif
8335#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008336 /* Do not follow links. */
8337 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008338#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008339#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 /* Do not update the access time. */
8341 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008342#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008343
Victor Stinner8c62be82010-05-06 00:08:46 +00008344 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008345#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008346 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008347#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008348#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008349 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008350#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008351#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008352 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008353#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008354#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008355 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008356#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008357#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008358 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008359#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008360#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008361 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008362#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008363#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008364 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008365#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008366#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008367 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008368#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008369#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008370 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008371#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008372#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008373 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008374#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008375#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008376 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008377#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008378#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008379 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008380#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008381#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008382 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008383#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008384#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008386#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008387#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008389#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008390#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008391 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008392#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008393#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008394 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008395#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008396
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008397 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008398#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008399 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008400#endif /* ST_RDONLY */
8401#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008402 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008403#endif /* ST_NOSUID */
8404
Guido van Rossum246bc171999-02-01 23:54:31 +00008405#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008406#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008407 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8408 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8409 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8410 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8411 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8412 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8413 if (ins(d, "P_PM", (long)P_PM)) return -1;
8414 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8415 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8416 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8417 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8418 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8419 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8420 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8421 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8422 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8423 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8424 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8425 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8426 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008427#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008428 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8429 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8430 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8431 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8432 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008433#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008434#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008435
Guido van Rossumd48f2521997-12-05 22:19:34 +00008436#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008437 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008438#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008439 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008440}
8441
8442
Tim Peters5aa91602002-01-30 05:46:57 +00008443#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008444#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008445#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008446
8447#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008448#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008449#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008450
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008451#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008452#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008453#define MODNAME "posix"
8454#endif
8455
Martin v. Löwis1a214512008-06-11 05:26:20 +00008456static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008457 PyModuleDef_HEAD_INIT,
8458 MODNAME,
8459 posix__doc__,
8460 -1,
8461 posix_methods,
8462 NULL,
8463 NULL,
8464 NULL,
8465 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008466};
8467
8468
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008469PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008470INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008471{
Victor Stinner8c62be82010-05-06 00:08:46 +00008472 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008473
Brian Curtin52173d42010-12-02 18:29:18 +00008474#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008475 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008476#endif
8477
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 m = PyModule_Create(&posixmodule);
8479 if (m == NULL)
8480 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008481
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 /* Initialize environ dictionary */
8483 v = convertenviron();
8484 Py_XINCREF(v);
8485 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8486 return NULL;
8487 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008488
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 if (all_ins(m))
8490 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008491
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 if (setup_confname_tables(m))
8493 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008494
Victor Stinner8c62be82010-05-06 00:08:46 +00008495 Py_INCREF(PyExc_OSError);
8496 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008497
Guido van Rossumb3d39562000-01-31 18:41:26 +00008498#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008499 if (posix_putenv_garbage == NULL)
8500 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008501#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008502
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 if (!initialized) {
8504 stat_result_desc.name = MODNAME ".stat_result";
8505 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8506 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8507 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8508 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8509 structseq_new = StatResultType.tp_new;
8510 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008511
Victor Stinner8c62be82010-05-06 00:08:46 +00008512 statvfs_result_desc.name = MODNAME ".statvfs_result";
8513 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008514#ifdef NEED_TICKS_PER_SECOND
8515# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008516 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008517# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008519# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008520 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008521# endif
8522#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008523 }
8524 Py_INCREF((PyObject*) &StatResultType);
8525 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8526 Py_INCREF((PyObject*) &StatVFSResultType);
8527 PyModule_AddObject(m, "statvfs_result",
8528 (PyObject*) &StatVFSResultType);
8529 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008530
8531#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008532 /*
8533 * Step 2 of weak-linking support on Mac OS X.
8534 *
8535 * The code below removes functions that are not available on the
8536 * currently active platform.
8537 *
8538 * This block allow one to use a python binary that was build on
8539 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8540 * OSX 10.4.
8541 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008542#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008543 if (fstatvfs == NULL) {
8544 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8545 return NULL;
8546 }
8547 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008548#endif /* HAVE_FSTATVFS */
8549
8550#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008551 if (statvfs == NULL) {
8552 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8553 return NULL;
8554 }
8555 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008556#endif /* HAVE_STATVFS */
8557
8558# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008559 if (lchown == NULL) {
8560 if (PyObject_DelAttrString(m, "lchown") == -1) {
8561 return NULL;
8562 }
8563 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008564#endif /* HAVE_LCHOWN */
8565
8566
8567#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008568 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008569
Guido van Rossumb6775db1994-08-01 11:34:53 +00008570}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008571
8572#ifdef __cplusplus
8573}
8574#endif