blob: 89d3f2f2d0b783d32eb2a9307692f9d6ce5bfe29 [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}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002822#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002823
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002824PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002825"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002826Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002827
Barry Warsaw53699e91996-12-10 23:23:01 +00002828static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002829posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002830{
Victor Stinner8c62be82010-05-06 00:08:46 +00002831 int res;
2832 PyObject *opath;
2833 char *path;
2834 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002835
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002836#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002837 PyUnicodeObject *po;
2838 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2839 Py_BEGIN_ALLOW_THREADS
2840 /* PyUnicode_AS_UNICODE OK without thread lock as
2841 it is a simple dereference. */
2842 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2843 Py_END_ALLOW_THREADS
2844 if (!res)
2845 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2846 Py_INCREF(Py_None);
2847 return Py_None;
2848 }
2849 /* Drop the argument parsing error as narrow strings
2850 are also valid. */
2851 PyErr_Clear();
2852 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2853 PyUnicode_FSConverter, &opath, &mode))
2854 return NULL;
2855 path = PyBytes_AsString(opath);
2856 Py_BEGIN_ALLOW_THREADS
2857 /* PyUnicode_AS_UNICODE OK without thread lock as
2858 it is a simple dereference. */
2859 res = CreateDirectoryA(path, NULL);
2860 Py_END_ALLOW_THREADS
2861 if (!res) {
2862 win32_error("mkdir", path);
2863 Py_DECREF(opath);
2864 return NULL;
2865 }
2866 Py_DECREF(opath);
2867 Py_INCREF(Py_None);
2868 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002869#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002870
Victor Stinner8c62be82010-05-06 00:08:46 +00002871 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2872 PyUnicode_FSConverter, &opath, &mode))
2873 return NULL;
2874 path = PyBytes_AsString(opath);
2875 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002876#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002878#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002880#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002881 Py_END_ALLOW_THREADS
2882 if (res < 0)
2883 return posix_error_with_allocated_filename(opath);
2884 Py_DECREF(opath);
2885 Py_INCREF(Py_None);
2886 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002887#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002888}
2889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002890
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002891/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2892#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002893#include <sys/resource.h>
2894#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002895
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002896
2897#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002898PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002899"nice(inc) -> new_priority\n\n\
2900Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002901
Barry Warsaw53699e91996-12-10 23:23:01 +00002902static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002903posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002904{
Victor Stinner8c62be82010-05-06 00:08:46 +00002905 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002906
Victor Stinner8c62be82010-05-06 00:08:46 +00002907 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2908 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002909
Victor Stinner8c62be82010-05-06 00:08:46 +00002910 /* There are two flavours of 'nice': one that returns the new
2911 priority (as required by almost all standards out there) and the
2912 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2913 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002914
Victor Stinner8c62be82010-05-06 00:08:46 +00002915 If we are of the nice family that returns the new priority, we
2916 need to clear errno before the call, and check if errno is filled
2917 before calling posix_error() on a returnvalue of -1, because the
2918 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002919
Victor Stinner8c62be82010-05-06 00:08:46 +00002920 errno = 0;
2921 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002922#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002923 if (value == 0)
2924 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002925#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002926 if (value == -1 && errno != 0)
2927 /* either nice() or getpriority() returned an error */
2928 return posix_error();
2929 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002930}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002931#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002932
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002933PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002934"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002935Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002936
Barry Warsaw53699e91996-12-10 23:23:01 +00002937static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002938posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002939{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002940#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002941 PyObject *o1, *o2;
2942 char *p1, *p2;
2943 BOOL result;
2944 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2945 goto error;
2946 if (!convert_to_unicode(&o1))
2947 goto error;
2948 if (!convert_to_unicode(&o2)) {
2949 Py_DECREF(o1);
2950 goto error;
2951 }
2952 Py_BEGIN_ALLOW_THREADS
2953 result = MoveFileW(PyUnicode_AsUnicode(o1),
2954 PyUnicode_AsUnicode(o2));
2955 Py_END_ALLOW_THREADS
2956 Py_DECREF(o1);
2957 Py_DECREF(o2);
2958 if (!result)
2959 return win32_error("rename", NULL);
2960 Py_INCREF(Py_None);
2961 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002962error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002963 PyErr_Clear();
2964 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2965 return NULL;
2966 Py_BEGIN_ALLOW_THREADS
2967 result = MoveFileA(p1, p2);
2968 Py_END_ALLOW_THREADS
2969 if (!result)
2970 return win32_error("rename", NULL);
2971 Py_INCREF(Py_None);
2972 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002973#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002974 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002975#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002976}
2977
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002979PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002980"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002981Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002982
Barry Warsaw53699e91996-12-10 23:23:01 +00002983static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002984posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002985{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002986#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002988#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002989 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002990#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002991}
2992
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002994PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002995"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002996Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002997
Barry Warsaw53699e91996-12-10 23:23:01 +00002998static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002999posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003000{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003001#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003002 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003003#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003004 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003005#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003006}
3007
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003008
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003009#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003011"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003012Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003013
Barry Warsaw53699e91996-12-10 23:23:01 +00003014static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003015posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003016{
Victor Stinner8c62be82010-05-06 00:08:46 +00003017 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003018#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003019 wchar_t *command;
3020 if (!PyArg_ParseTuple(args, "u:system", &command))
3021 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003022
Victor Stinner8c62be82010-05-06 00:08:46 +00003023 Py_BEGIN_ALLOW_THREADS
3024 sts = _wsystem(command);
3025 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003026#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003027 PyObject *command_obj;
3028 char *command;
3029 if (!PyArg_ParseTuple(args, "O&:system",
3030 PyUnicode_FSConverter, &command_obj))
3031 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003032
Victor Stinner8c62be82010-05-06 00:08:46 +00003033 command = PyBytes_AsString(command_obj);
3034 Py_BEGIN_ALLOW_THREADS
3035 sts = system(command);
3036 Py_END_ALLOW_THREADS
3037 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003038#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003039 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003040}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003041#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003042
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003044PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003045"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003046Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003047
Barry Warsaw53699e91996-12-10 23:23:01 +00003048static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003049posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003050{
Victor Stinner8c62be82010-05-06 00:08:46 +00003051 int i;
3052 if (!PyArg_ParseTuple(args, "i:umask", &i))
3053 return NULL;
3054 i = (int)umask(i);
3055 if (i < 0)
3056 return posix_error();
3057 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003058}
3059
Brian Curtind40e6f72010-07-08 21:39:08 +00003060#ifdef MS_WINDOWS
3061
3062/* override the default DeleteFileW behavior so that directory
3063symlinks can be removed with this function, the same as with
3064Unix symlinks */
3065BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3066{
3067 WIN32_FILE_ATTRIBUTE_DATA info;
3068 WIN32_FIND_DATAW find_data;
3069 HANDLE find_data_handle;
3070 int is_directory = 0;
3071 int is_link = 0;
3072
3073 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3074 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3075
3076 /* Get WIN32_FIND_DATA structure for the path to determine if
3077 it is a symlink */
3078 if(is_directory &&
3079 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3080 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3081
3082 if(find_data_handle != INVALID_HANDLE_VALUE) {
3083 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3084 FindClose(find_data_handle);
3085 }
3086 }
3087 }
3088
3089 if (is_directory && is_link)
3090 return RemoveDirectoryW(lpFileName);
3091
3092 return DeleteFileW(lpFileName);
3093}
3094#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003097"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003101"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003102Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003103
Barry Warsaw53699e91996-12-10 23:23:01 +00003104static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003105posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003106{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003107#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003108 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3109 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003110#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003111 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003112#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003113}
3114
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003115
Guido van Rossumb6775db1994-08-01 11:34:53 +00003116#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003117PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003118"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003119Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003120
Barry Warsaw53699e91996-12-10 23:23:01 +00003121static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003122posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003123{
Victor Stinner8c62be82010-05-06 00:08:46 +00003124 struct utsname u;
3125 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003126
Victor Stinner8c62be82010-05-06 00:08:46 +00003127 Py_BEGIN_ALLOW_THREADS
3128 res = uname(&u);
3129 Py_END_ALLOW_THREADS
3130 if (res < 0)
3131 return posix_error();
3132 return Py_BuildValue("(sssss)",
3133 u.sysname,
3134 u.nodename,
3135 u.release,
3136 u.version,
3137 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003138}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003139#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003140
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003141static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003142extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003143{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003144 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003145 if (PyFloat_Check(t)) {
3146 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003147 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 if (!intobj)
3149 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003150#if SIZEOF_TIME_T > SIZEOF_LONG
3151 intval = PyLong_AsUnsignedLongLongMask(intobj);
3152#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003154#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003155 Py_DECREF(intobj);
3156 if (intval == -1 && PyErr_Occurred())
3157 return -1;
3158 *sec = intval;
3159 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3160 if (*usec < 0)
3161 /* If rounding gave us a negative number,
3162 truncate. */
3163 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003164 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003165 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003166#if SIZEOF_TIME_T > SIZEOF_LONG
3167 intval = PyLong_AsUnsignedLongLongMask(t);
3168#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003169 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003170#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003171 if (intval == -1 && PyErr_Occurred())
3172 return -1;
3173 *sec = intval;
3174 *usec = 0;
3175 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003176}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003178PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003179"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003180utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003181Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003182second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003183
Barry Warsaw53699e91996-12-10 23:23:01 +00003184static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003185posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003186{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003187#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003188 PyObject *arg;
3189 PyUnicodeObject *obwpath;
3190 wchar_t *wpath = NULL;
3191 PyObject *oapath;
3192 char *apath;
3193 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003194 time_t atimesec, mtimesec;
3195 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003196 FILETIME atime, mtime;
3197 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003198
Victor Stinner8c62be82010-05-06 00:08:46 +00003199 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3200 wpath = PyUnicode_AS_UNICODE(obwpath);
3201 Py_BEGIN_ALLOW_THREADS
3202 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3203 NULL, OPEN_EXISTING,
3204 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3205 Py_END_ALLOW_THREADS
3206 if (hFile == INVALID_HANDLE_VALUE)
3207 return win32_error_unicode("utime", wpath);
3208 } else
3209 /* Drop the argument parsing error as narrow strings
3210 are also valid. */
3211 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003212
Victor Stinner8c62be82010-05-06 00:08:46 +00003213 if (!wpath) {
3214 if (!PyArg_ParseTuple(args, "O&O:utime",
3215 PyUnicode_FSConverter, &oapath, &arg))
3216 return NULL;
3217 apath = PyBytes_AsString(oapath);
3218 Py_BEGIN_ALLOW_THREADS
3219 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3220 NULL, OPEN_EXISTING,
3221 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3222 Py_END_ALLOW_THREADS
3223 if (hFile == INVALID_HANDLE_VALUE) {
3224 win32_error("utime", apath);
3225 Py_DECREF(oapath);
3226 return NULL;
3227 }
3228 Py_DECREF(oapath);
3229 }
3230
3231 if (arg == Py_None) {
3232 SYSTEMTIME now;
3233 GetSystemTime(&now);
3234 if (!SystemTimeToFileTime(&now, &mtime) ||
3235 !SystemTimeToFileTime(&now, &atime)) {
3236 win32_error("utime", NULL);
3237 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003238 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003239 }
3240 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3241 PyErr_SetString(PyExc_TypeError,
3242 "utime() arg 2 must be a tuple (atime, mtime)");
3243 goto done;
3244 }
3245 else {
3246 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3247 &atimesec, &ausec) == -1)
3248 goto done;
3249 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3250 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3251 &mtimesec, &musec) == -1)
3252 goto done;
3253 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3254 }
3255 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3256 /* Avoid putting the file name into the error here,
3257 as that may confuse the user into believing that
3258 something is wrong with the file, when it also
3259 could be the time stamp that gives a problem. */
3260 win32_error("utime", NULL);
Antoine Pitroue85da7a2011-01-06 18:25:55 +00003261 goto done;
Victor Stinner8c62be82010-05-06 00:08:46 +00003262 }
3263 Py_INCREF(Py_None);
3264 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003265done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003266 CloseHandle(hFile);
3267 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003268#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003269
Victor Stinner8c62be82010-05-06 00:08:46 +00003270 PyObject *opath;
3271 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003272 time_t atime, mtime;
3273 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003274 int res;
3275 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003276
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003277#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003278 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003279#define ATIME buf[0].tv_sec
3280#define MTIME buf[1].tv_sec
3281#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003282/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003283 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003284#define ATIME buf.actime
3285#define MTIME buf.modtime
3286#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003287#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003289#define ATIME buf[0]
3290#define MTIME buf[1]
3291#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003292#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003293
Mark Hammond817c9292003-12-03 01:22:38 +00003294
Victor Stinner8c62be82010-05-06 00:08:46 +00003295 if (!PyArg_ParseTuple(args, "O&O:utime",
3296 PyUnicode_FSConverter, &opath, &arg))
3297 return NULL;
3298 path = PyBytes_AsString(opath);
3299 if (arg == Py_None) {
3300 /* optional time values not given */
3301 Py_BEGIN_ALLOW_THREADS
3302 res = utime(path, NULL);
3303 Py_END_ALLOW_THREADS
3304 }
3305 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3306 PyErr_SetString(PyExc_TypeError,
3307 "utime() arg 2 must be a tuple (atime, mtime)");
3308 Py_DECREF(opath);
3309 return NULL;
3310 }
3311 else {
3312 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3313 &atime, &ausec) == -1) {
3314 Py_DECREF(opath);
3315 return NULL;
3316 }
3317 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3318 &mtime, &musec) == -1) {
3319 Py_DECREF(opath);
3320 return NULL;
3321 }
3322 ATIME = atime;
3323 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003324#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003325 buf[0].tv_usec = ausec;
3326 buf[1].tv_usec = musec;
3327 Py_BEGIN_ALLOW_THREADS
3328 res = utimes(path, buf);
3329 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003330#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003331 Py_BEGIN_ALLOW_THREADS
3332 res = utime(path, UTIME_ARG);
3333 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003334#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003335 }
3336 if (res < 0) {
3337 return posix_error_with_allocated_filename(opath);
3338 }
3339 Py_DECREF(opath);
3340 Py_INCREF(Py_None);
3341 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003342#undef UTIME_ARG
3343#undef ATIME
3344#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003345#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003346}
3347
Guido van Rossum85e3b011991-06-03 12:42:10 +00003348
Guido van Rossum3b066191991-06-04 19:40:25 +00003349/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003351PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003352"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003353Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003354
Barry Warsaw53699e91996-12-10 23:23:01 +00003355static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003356posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003357{
Victor Stinner8c62be82010-05-06 00:08:46 +00003358 int sts;
3359 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3360 return NULL;
3361 _exit(sts);
3362 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003363}
3364
Martin v. Löwis114619e2002-10-07 06:44:21 +00003365#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3366static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003367free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003368{
Victor Stinner8c62be82010-05-06 00:08:46 +00003369 Py_ssize_t i;
3370 for (i = 0; i < count; i++)
3371 PyMem_Free(array[i]);
3372 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003373}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003374
Antoine Pitrou69f71142009-05-24 21:25:49 +00003375static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003376int fsconvert_strdup(PyObject *o, char**out)
3377{
Victor Stinner8c62be82010-05-06 00:08:46 +00003378 PyObject *bytes;
3379 Py_ssize_t size;
3380 if (!PyUnicode_FSConverter(o, &bytes))
3381 return 0;
3382 size = PyBytes_GET_SIZE(bytes);
3383 *out = PyMem_Malloc(size+1);
3384 if (!*out)
3385 return 0;
3386 memcpy(*out, PyBytes_AsString(bytes), size+1);
3387 Py_DECREF(bytes);
3388 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003389}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003390#endif
3391
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003392
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003393#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003394PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003395"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003396Execute an executable path with arguments, replacing current process.\n\
3397\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003398 path: path of executable file\n\
3399 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003400
Barry Warsaw53699e91996-12-10 23:23:01 +00003401static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003402posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003403{
Victor Stinner8c62be82010-05-06 00:08:46 +00003404 PyObject *opath;
3405 char *path;
3406 PyObject *argv;
3407 char **argvlist;
3408 Py_ssize_t i, argc;
3409 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003410
Victor Stinner8c62be82010-05-06 00:08:46 +00003411 /* execv has two arguments: (path, argv), where
3412 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003413
Victor Stinner8c62be82010-05-06 00:08:46 +00003414 if (!PyArg_ParseTuple(args, "O&O:execv",
3415 PyUnicode_FSConverter,
3416 &opath, &argv))
3417 return NULL;
3418 path = PyBytes_AsString(opath);
3419 if (PyList_Check(argv)) {
3420 argc = PyList_Size(argv);
3421 getitem = PyList_GetItem;
3422 }
3423 else if (PyTuple_Check(argv)) {
3424 argc = PyTuple_Size(argv);
3425 getitem = PyTuple_GetItem;
3426 }
3427 else {
3428 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3429 Py_DECREF(opath);
3430 return NULL;
3431 }
3432 if (argc < 1) {
3433 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3434 Py_DECREF(opath);
3435 return NULL;
3436 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003437
Victor Stinner8c62be82010-05-06 00:08:46 +00003438 argvlist = PyMem_NEW(char *, argc+1);
3439 if (argvlist == NULL) {
3440 Py_DECREF(opath);
3441 return PyErr_NoMemory();
3442 }
3443 for (i = 0; i < argc; i++) {
3444 if (!fsconvert_strdup((*getitem)(argv, i),
3445 &argvlist[i])) {
3446 free_string_array(argvlist, i);
3447 PyErr_SetString(PyExc_TypeError,
3448 "execv() arg 2 must contain only strings");
3449 Py_DECREF(opath);
3450 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003451
Victor Stinner8c62be82010-05-06 00:08:46 +00003452 }
3453 }
3454 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003455
Victor Stinner8c62be82010-05-06 00:08:46 +00003456 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003457
Victor Stinner8c62be82010-05-06 00:08:46 +00003458 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003459
Victor Stinner8c62be82010-05-06 00:08:46 +00003460 free_string_array(argvlist, argc);
3461 Py_DECREF(opath);
3462 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003463}
3464
Victor Stinner13bb71c2010-04-23 21:41:56 +00003465static char**
3466parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3467{
Victor Stinner8c62be82010-05-06 00:08:46 +00003468 char **envlist;
3469 Py_ssize_t i, pos, envc;
3470 PyObject *keys=NULL, *vals=NULL;
3471 PyObject *key, *val, *key2, *val2;
3472 char *p, *k, *v;
3473 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003474
Victor Stinner8c62be82010-05-06 00:08:46 +00003475 i = PyMapping_Size(env);
3476 if (i < 0)
3477 return NULL;
3478 envlist = PyMem_NEW(char *, i + 1);
3479 if (envlist == NULL) {
3480 PyErr_NoMemory();
3481 return NULL;
3482 }
3483 envc = 0;
3484 keys = PyMapping_Keys(env);
3485 vals = PyMapping_Values(env);
3486 if (!keys || !vals)
3487 goto error;
3488 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3489 PyErr_Format(PyExc_TypeError,
3490 "env.keys() or env.values() is not a list");
3491 goto error;
3492 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003493
Victor Stinner8c62be82010-05-06 00:08:46 +00003494 for (pos = 0; pos < i; pos++) {
3495 key = PyList_GetItem(keys, pos);
3496 val = PyList_GetItem(vals, pos);
3497 if (!key || !val)
3498 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003499
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 if (PyUnicode_FSConverter(key, &key2) == 0)
3501 goto error;
3502 if (PyUnicode_FSConverter(val, &val2) == 0) {
3503 Py_DECREF(key2);
3504 goto error;
3505 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003506
3507#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003508 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3509 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003510#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003511 k = PyBytes_AsString(key2);
3512 v = PyBytes_AsString(val2);
3513 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003514
Victor Stinner8c62be82010-05-06 00:08:46 +00003515 p = PyMem_NEW(char, len);
3516 if (p == NULL) {
3517 PyErr_NoMemory();
3518 Py_DECREF(key2);
3519 Py_DECREF(val2);
3520 goto error;
3521 }
3522 PyOS_snprintf(p, len, "%s=%s", k, v);
3523 envlist[envc++] = p;
3524 Py_DECREF(key2);
3525 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003526#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003527 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003528#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003529 }
3530 Py_DECREF(vals);
3531 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003532
Victor Stinner8c62be82010-05-06 00:08:46 +00003533 envlist[envc] = 0;
3534 *envc_ptr = envc;
3535 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003536
3537error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003538 Py_XDECREF(keys);
3539 Py_XDECREF(vals);
3540 while (--envc >= 0)
3541 PyMem_DEL(envlist[envc]);
3542 PyMem_DEL(envlist);
3543 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003544}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003546PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003547"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003548Execute a path with arguments and environment, replacing current process.\n\
3549\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003550 path: path of executable file\n\
3551 args: tuple or list of arguments\n\
3552 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003553
Barry Warsaw53699e91996-12-10 23:23:01 +00003554static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003555posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003556{
Victor Stinner8c62be82010-05-06 00:08:46 +00003557 PyObject *opath;
3558 char *path;
3559 PyObject *argv, *env;
3560 char **argvlist;
3561 char **envlist;
3562 Py_ssize_t i, argc, envc;
3563 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3564 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003565
Victor Stinner8c62be82010-05-06 00:08:46 +00003566 /* execve has three arguments: (path, argv, env), where
3567 argv is a list or tuple of strings and env is a dictionary
3568 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003569
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 if (!PyArg_ParseTuple(args, "O&OO:execve",
3571 PyUnicode_FSConverter,
3572 &opath, &argv, &env))
3573 return NULL;
3574 path = PyBytes_AsString(opath);
3575 if (PyList_Check(argv)) {
3576 argc = PyList_Size(argv);
3577 getitem = PyList_GetItem;
3578 }
3579 else if (PyTuple_Check(argv)) {
3580 argc = PyTuple_Size(argv);
3581 getitem = PyTuple_GetItem;
3582 }
3583 else {
3584 PyErr_SetString(PyExc_TypeError,
3585 "execve() arg 2 must be a tuple or list");
3586 goto fail_0;
3587 }
3588 if (!PyMapping_Check(env)) {
3589 PyErr_SetString(PyExc_TypeError,
3590 "execve() arg 3 must be a mapping object");
3591 goto fail_0;
3592 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003593
Victor Stinner8c62be82010-05-06 00:08:46 +00003594 argvlist = PyMem_NEW(char *, argc+1);
3595 if (argvlist == NULL) {
3596 PyErr_NoMemory();
3597 goto fail_0;
3598 }
3599 for (i = 0; i < argc; i++) {
3600 if (!fsconvert_strdup((*getitem)(argv, i),
3601 &argvlist[i]))
3602 {
3603 lastarg = i;
3604 goto fail_1;
3605 }
3606 }
3607 lastarg = argc;
3608 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003609
Victor Stinner8c62be82010-05-06 00:08:46 +00003610 envlist = parse_envlist(env, &envc);
3611 if (envlist == NULL)
3612 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003613
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003615
Victor Stinner8c62be82010-05-06 00:08:46 +00003616 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003617
Victor Stinner8c62be82010-05-06 00:08:46 +00003618 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003619
Victor Stinner8c62be82010-05-06 00:08:46 +00003620 while (--envc >= 0)
3621 PyMem_DEL(envlist[envc]);
3622 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003623 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003624 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003625 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003626 Py_DECREF(opath);
3627 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003628}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003629#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003631
Guido van Rossuma1065681999-01-25 23:20:23 +00003632#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003633PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003634"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003635Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003636\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003637 mode: mode of process creation\n\
3638 path: path of executable file\n\
3639 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003640
3641static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003642posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003643{
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 PyObject *opath;
3645 char *path;
3646 PyObject *argv;
3647 char **argvlist;
3648 int mode, i;
3649 Py_ssize_t argc;
3650 Py_intptr_t spawnval;
3651 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003652
Victor Stinner8c62be82010-05-06 00:08:46 +00003653 /* spawnv has three arguments: (mode, path, argv), where
3654 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003655
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3657 PyUnicode_FSConverter,
3658 &opath, &argv))
3659 return NULL;
3660 path = PyBytes_AsString(opath);
3661 if (PyList_Check(argv)) {
3662 argc = PyList_Size(argv);
3663 getitem = PyList_GetItem;
3664 }
3665 else if (PyTuple_Check(argv)) {
3666 argc = PyTuple_Size(argv);
3667 getitem = PyTuple_GetItem;
3668 }
3669 else {
3670 PyErr_SetString(PyExc_TypeError,
3671 "spawnv() arg 2 must be a tuple or list");
3672 Py_DECREF(opath);
3673 return NULL;
3674 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003675
Victor Stinner8c62be82010-05-06 00:08:46 +00003676 argvlist = PyMem_NEW(char *, argc+1);
3677 if (argvlist == NULL) {
3678 Py_DECREF(opath);
3679 return PyErr_NoMemory();
3680 }
3681 for (i = 0; i < argc; i++) {
3682 if (!fsconvert_strdup((*getitem)(argv, i),
3683 &argvlist[i])) {
3684 free_string_array(argvlist, i);
3685 PyErr_SetString(
3686 PyExc_TypeError,
3687 "spawnv() arg 2 must contain only strings");
3688 Py_DECREF(opath);
3689 return NULL;
3690 }
3691 }
3692 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003693
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003694#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 Py_BEGIN_ALLOW_THREADS
3696 spawnval = spawnv(mode, path, argvlist);
3697 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003698#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 if (mode == _OLD_P_OVERLAY)
3700 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003701
Victor Stinner8c62be82010-05-06 00:08:46 +00003702 Py_BEGIN_ALLOW_THREADS
3703 spawnval = _spawnv(mode, path, argvlist);
3704 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003705#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003706
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 free_string_array(argvlist, argc);
3708 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003709
Victor Stinner8c62be82010-05-06 00:08:46 +00003710 if (spawnval == -1)
3711 return posix_error();
3712 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003713#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003714 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003716 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003717#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003718}
3719
3720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003721PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003722"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003723Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003724\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003725 mode: mode of process creation\n\
3726 path: path of executable file\n\
3727 args: tuple or list of arguments\n\
3728 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003729
3730static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003731posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003732{
Victor Stinner8c62be82010-05-06 00:08:46 +00003733 PyObject *opath;
3734 char *path;
3735 PyObject *argv, *env;
3736 char **argvlist;
3737 char **envlist;
3738 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003739 int mode;
3740 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003741 Py_intptr_t spawnval;
3742 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3743 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003744
Victor Stinner8c62be82010-05-06 00:08:46 +00003745 /* spawnve has four arguments: (mode, path, argv, env), where
3746 argv is a list or tuple of strings and env is a dictionary
3747 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003748
Victor Stinner8c62be82010-05-06 00:08:46 +00003749 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3750 PyUnicode_FSConverter,
3751 &opath, &argv, &env))
3752 return NULL;
3753 path = PyBytes_AsString(opath);
3754 if (PyList_Check(argv)) {
3755 argc = PyList_Size(argv);
3756 getitem = PyList_GetItem;
3757 }
3758 else if (PyTuple_Check(argv)) {
3759 argc = PyTuple_Size(argv);
3760 getitem = PyTuple_GetItem;
3761 }
3762 else {
3763 PyErr_SetString(PyExc_TypeError,
3764 "spawnve() arg 2 must be a tuple or list");
3765 goto fail_0;
3766 }
3767 if (!PyMapping_Check(env)) {
3768 PyErr_SetString(PyExc_TypeError,
3769 "spawnve() arg 3 must be a mapping object");
3770 goto fail_0;
3771 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003772
Victor Stinner8c62be82010-05-06 00:08:46 +00003773 argvlist = PyMem_NEW(char *, argc+1);
3774 if (argvlist == NULL) {
3775 PyErr_NoMemory();
3776 goto fail_0;
3777 }
3778 for (i = 0; i < argc; i++) {
3779 if (!fsconvert_strdup((*getitem)(argv, i),
3780 &argvlist[i]))
3781 {
3782 lastarg = i;
3783 goto fail_1;
3784 }
3785 }
3786 lastarg = argc;
3787 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003788
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 envlist = parse_envlist(env, &envc);
3790 if (envlist == NULL)
3791 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003792
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003793#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 Py_BEGIN_ALLOW_THREADS
3795 spawnval = spawnve(mode, path, argvlist, envlist);
3796 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003797#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 if (mode == _OLD_P_OVERLAY)
3799 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003800
Victor Stinner8c62be82010-05-06 00:08:46 +00003801 Py_BEGIN_ALLOW_THREADS
3802 spawnval = _spawnve(mode, path, argvlist, envlist);
3803 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003804#endif
Tim Peters25059d32001-12-07 20:35:43 +00003805
Victor Stinner8c62be82010-05-06 00:08:46 +00003806 if (spawnval == -1)
3807 (void) posix_error();
3808 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003809#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003810 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003811#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003812 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003813#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003814
Victor Stinner8c62be82010-05-06 00:08:46 +00003815 while (--envc >= 0)
3816 PyMem_DEL(envlist[envc]);
3817 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003818 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003819 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003820 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003821 Py_DECREF(opath);
3822 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003823}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003824
3825/* OS/2 supports spawnvp & spawnvpe natively */
3826#if defined(PYOS_OS2)
3827PyDoc_STRVAR(posix_spawnvp__doc__,
3828"spawnvp(mode, file, args)\n\n\
3829Execute the program 'file' in a new process, using the environment\n\
3830search path to find the file.\n\
3831\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003832 mode: mode of process creation\n\
3833 file: executable file name\n\
3834 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003835
3836static PyObject *
3837posix_spawnvp(PyObject *self, PyObject *args)
3838{
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 PyObject *opath;
3840 char *path;
3841 PyObject *argv;
3842 char **argvlist;
3843 int mode, i, argc;
3844 Py_intptr_t spawnval;
3845 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003846
Victor Stinner8c62be82010-05-06 00:08:46 +00003847 /* spawnvp has three arguments: (mode, path, argv), where
3848 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003849
Victor Stinner8c62be82010-05-06 00:08:46 +00003850 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3851 PyUnicode_FSConverter,
3852 &opath, &argv))
3853 return NULL;
3854 path = PyBytes_AsString(opath);
3855 if (PyList_Check(argv)) {
3856 argc = PyList_Size(argv);
3857 getitem = PyList_GetItem;
3858 }
3859 else if (PyTuple_Check(argv)) {
3860 argc = PyTuple_Size(argv);
3861 getitem = PyTuple_GetItem;
3862 }
3863 else {
3864 PyErr_SetString(PyExc_TypeError,
3865 "spawnvp() arg 2 must be a tuple or list");
3866 Py_DECREF(opath);
3867 return NULL;
3868 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003869
Victor Stinner8c62be82010-05-06 00:08:46 +00003870 argvlist = PyMem_NEW(char *, argc+1);
3871 if (argvlist == NULL) {
3872 Py_DECREF(opath);
3873 return PyErr_NoMemory();
3874 }
3875 for (i = 0; i < argc; i++) {
3876 if (!fsconvert_strdup((*getitem)(argv, i),
3877 &argvlist[i])) {
3878 free_string_array(argvlist, i);
3879 PyErr_SetString(
3880 PyExc_TypeError,
3881 "spawnvp() arg 2 must contain only strings");
3882 Py_DECREF(opath);
3883 return NULL;
3884 }
3885 }
3886 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003887
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003889#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003890 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003891#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003892 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003893#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003894 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003895
Victor Stinner8c62be82010-05-06 00:08:46 +00003896 free_string_array(argvlist, argc);
3897 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003898
Victor Stinner8c62be82010-05-06 00:08:46 +00003899 if (spawnval == -1)
3900 return posix_error();
3901 else
3902 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003903}
3904
3905
3906PyDoc_STRVAR(posix_spawnvpe__doc__,
3907"spawnvpe(mode, file, args, env)\n\n\
3908Execute the program 'file' in a new process, using the environment\n\
3909search path to find the file.\n\
3910\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003911 mode: mode of process creation\n\
3912 file: executable file name\n\
3913 args: tuple or list of arguments\n\
3914 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003915
3916static PyObject *
3917posix_spawnvpe(PyObject *self, PyObject *args)
3918{
Victor Stinner8c62be82010-05-06 00:08:46 +00003919 PyObject *opath
3920 char *path;
3921 PyObject *argv, *env;
3922 char **argvlist;
3923 char **envlist;
3924 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003925 int mode;
3926 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003927 Py_intptr_t spawnval;
3928 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3929 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003930
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 /* spawnvpe has four arguments: (mode, path, argv, env), where
3932 argv is a list or tuple of strings and env is a dictionary
3933 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003934
Victor Stinner8c62be82010-05-06 00:08:46 +00003935 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3936 PyUnicode_FSConverter,
3937 &opath, &argv, &env))
3938 return NULL;
3939 path = PyBytes_AsString(opath);
3940 if (PyList_Check(argv)) {
3941 argc = PyList_Size(argv);
3942 getitem = PyList_GetItem;
3943 }
3944 else if (PyTuple_Check(argv)) {
3945 argc = PyTuple_Size(argv);
3946 getitem = PyTuple_GetItem;
3947 }
3948 else {
3949 PyErr_SetString(PyExc_TypeError,
3950 "spawnvpe() arg 2 must be a tuple or list");
3951 goto fail_0;
3952 }
3953 if (!PyMapping_Check(env)) {
3954 PyErr_SetString(PyExc_TypeError,
3955 "spawnvpe() arg 3 must be a mapping object");
3956 goto fail_0;
3957 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003958
Victor Stinner8c62be82010-05-06 00:08:46 +00003959 argvlist = PyMem_NEW(char *, argc+1);
3960 if (argvlist == NULL) {
3961 PyErr_NoMemory();
3962 goto fail_0;
3963 }
3964 for (i = 0; i < argc; i++) {
3965 if (!fsconvert_strdup((*getitem)(argv, i),
3966 &argvlist[i]))
3967 {
3968 lastarg = i;
3969 goto fail_1;
3970 }
3971 }
3972 lastarg = argc;
3973 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003974
Victor Stinner8c62be82010-05-06 00:08:46 +00003975 envlist = parse_envlist(env, &envc);
3976 if (envlist == NULL)
3977 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003978
Victor Stinner8c62be82010-05-06 00:08:46 +00003979 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003980#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003981 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003982#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003983 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003984#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003985 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003986
Victor Stinner8c62be82010-05-06 00:08:46 +00003987 if (spawnval == -1)
3988 (void) posix_error();
3989 else
3990 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003991
Victor Stinner8c62be82010-05-06 00:08:46 +00003992 while (--envc >= 0)
3993 PyMem_DEL(envlist[envc]);
3994 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003995 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003996 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003997 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003998 Py_DECREF(opath);
3999 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00004000}
4001#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00004002#endif /* HAVE_SPAWNV */
4003
4004
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004005#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004006PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004007"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004008Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4009\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004010Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004011
4012static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004013posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004014{
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 pid_t pid;
4016 int result = 0;
4017 _PyImport_AcquireLock();
4018 pid = fork1();
4019 if (pid == 0) {
4020 /* child: this clobbers and resets the import lock. */
4021 PyOS_AfterFork();
4022 } else {
4023 /* parent: release the import lock. */
4024 result = _PyImport_ReleaseLock();
4025 }
4026 if (pid == -1)
4027 return posix_error();
4028 if (result < 0) {
4029 /* Don't clobber the OSError if the fork failed. */
4030 PyErr_SetString(PyExc_RuntimeError,
4031 "not holding the import lock");
4032 return NULL;
4033 }
4034 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004035}
4036#endif
4037
4038
Guido van Rossumad0ee831995-03-01 10:34:45 +00004039#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004040PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004041"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004042Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004043Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004044
Barry Warsaw53699e91996-12-10 23:23:01 +00004045static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004046posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004047{
Victor Stinner8c62be82010-05-06 00:08:46 +00004048 pid_t pid;
4049 int result = 0;
4050 _PyImport_AcquireLock();
4051 pid = fork();
4052 if (pid == 0) {
4053 /* child: this clobbers and resets the import lock. */
4054 PyOS_AfterFork();
4055 } else {
4056 /* parent: release the import lock. */
4057 result = _PyImport_ReleaseLock();
4058 }
4059 if (pid == -1)
4060 return posix_error();
4061 if (result < 0) {
4062 /* Don't clobber the OSError if the fork failed. */
4063 PyErr_SetString(PyExc_RuntimeError,
4064 "not holding the import lock");
4065 return NULL;
4066 }
4067 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004068}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004069#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004070
Neal Norwitzb59798b2003-03-21 01:43:31 +00004071/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004072/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4073#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004074#define DEV_PTY_FILE "/dev/ptc"
4075#define HAVE_DEV_PTMX
4076#else
4077#define DEV_PTY_FILE "/dev/ptmx"
4078#endif
4079
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004080#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004081#ifdef HAVE_PTY_H
4082#include <pty.h>
4083#else
4084#ifdef HAVE_LIBUTIL_H
4085#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004086#else
4087#ifdef HAVE_UTIL_H
4088#include <util.h>
4089#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004090#endif /* HAVE_LIBUTIL_H */
4091#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004092#ifdef HAVE_STROPTS_H
4093#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004094#endif
4095#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004096
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004097#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004098PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004099"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004100Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004101
4102static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004103posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004104{
Victor Stinner8c62be82010-05-06 00:08:46 +00004105 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004106#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004107 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004108#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004109#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004110 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004111#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004112 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004113#endif
4114#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004115
Thomas Wouters70c21a12000-07-14 14:28:33 +00004116#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004117 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4118 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004119#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004120 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4121 if (slave_name == NULL)
4122 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004123
Victor Stinner8c62be82010-05-06 00:08:46 +00004124 slave_fd = open(slave_name, O_RDWR);
4125 if (slave_fd < 0)
4126 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004127#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004128 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4129 if (master_fd < 0)
4130 return posix_error();
4131 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4132 /* change permission of slave */
4133 if (grantpt(master_fd) < 0) {
4134 PyOS_setsig(SIGCHLD, sig_saved);
4135 return posix_error();
4136 }
4137 /* unlock slave */
4138 if (unlockpt(master_fd) < 0) {
4139 PyOS_setsig(SIGCHLD, sig_saved);
4140 return posix_error();
4141 }
4142 PyOS_setsig(SIGCHLD, sig_saved);
4143 slave_name = ptsname(master_fd); /* get name of slave */
4144 if (slave_name == NULL)
4145 return posix_error();
4146 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4147 if (slave_fd < 0)
4148 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004149#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004150 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4151 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004152#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004153 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004154#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004155#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004156#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004157
Victor Stinner8c62be82010-05-06 00:08:46 +00004158 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004159
Fred Drake8cef4cf2000-06-28 16:40:38 +00004160}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004161#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004162
4163#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004164PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004165"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004166Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4167Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004168To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004169
4170static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004171posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004172{
Victor Stinner8c62be82010-05-06 00:08:46 +00004173 int master_fd = -1, result = 0;
4174 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004175
Victor Stinner8c62be82010-05-06 00:08:46 +00004176 _PyImport_AcquireLock();
4177 pid = forkpty(&master_fd, NULL, NULL, NULL);
4178 if (pid == 0) {
4179 /* child: this clobbers and resets the import lock. */
4180 PyOS_AfterFork();
4181 } else {
4182 /* parent: release the import lock. */
4183 result = _PyImport_ReleaseLock();
4184 }
4185 if (pid == -1)
4186 return posix_error();
4187 if (result < 0) {
4188 /* Don't clobber the OSError if the fork failed. */
4189 PyErr_SetString(PyExc_RuntimeError,
4190 "not holding the import lock");
4191 return NULL;
4192 }
4193 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004194}
4195#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004196
Guido van Rossumad0ee831995-03-01 10:34:45 +00004197#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004198PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004199"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004200Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004201
Barry Warsaw53699e91996-12-10 23:23:01 +00004202static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004203posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004204{
Victor Stinner8c62be82010-05-06 00:08:46 +00004205 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004206}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004207#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004209
Guido van Rossumad0ee831995-03-01 10:34:45 +00004210#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004211PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004212"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004213Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004214
Barry Warsaw53699e91996-12-10 23:23:01 +00004215static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004216posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004217{
Victor Stinner8c62be82010-05-06 00:08:46 +00004218 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004219}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004220#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004222
Guido van Rossumad0ee831995-03-01 10:34:45 +00004223#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004224PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004225"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004226Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004227
Barry Warsaw53699e91996-12-10 23:23:01 +00004228static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004229posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004230{
Victor Stinner8c62be82010-05-06 00:08:46 +00004231 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004232}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004233#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004234
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004236PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004237"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004238Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004239
Barry Warsaw53699e91996-12-10 23:23:01 +00004240static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004241posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004242{
Victor Stinner8c62be82010-05-06 00:08:46 +00004243 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004244}
4245
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004246
Fred Drakec9680921999-12-13 16:37:25 +00004247#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004248PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004249"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004250Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004251
4252static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004253posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004254{
4255 PyObject *result = NULL;
4256
Fred Drakec9680921999-12-13 16:37:25 +00004257#ifdef NGROUPS_MAX
4258#define MAX_GROUPS NGROUPS_MAX
4259#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004260 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004261#define MAX_GROUPS 64
4262#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004263 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004264
4265 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4266 * This is a helper variable to store the intermediate result when
4267 * that happens.
4268 *
4269 * To keep the code readable the OSX behaviour is unconditional,
4270 * according to the POSIX spec this should be safe on all unix-y
4271 * systems.
4272 */
4273 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004274 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004275
Victor Stinner8c62be82010-05-06 00:08:46 +00004276 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004277 if (n < 0) {
4278 if (errno == EINVAL) {
4279 n = getgroups(0, NULL);
4280 if (n == -1) {
4281 return posix_error();
4282 }
4283 if (n == 0) {
4284 /* Avoid malloc(0) */
4285 alt_grouplist = grouplist;
4286 } else {
4287 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4288 if (alt_grouplist == NULL) {
4289 errno = EINVAL;
4290 return posix_error();
4291 }
4292 n = getgroups(n, alt_grouplist);
4293 if (n == -1) {
4294 PyMem_Free(alt_grouplist);
4295 return posix_error();
4296 }
4297 }
4298 } else {
4299 return posix_error();
4300 }
4301 }
4302 result = PyList_New(n);
4303 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004304 int i;
4305 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004306 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004307 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004308 Py_DECREF(result);
4309 result = NULL;
4310 break;
Fred Drakec9680921999-12-13 16:37:25 +00004311 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004312 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004313 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004314 }
4315
4316 if (alt_grouplist != grouplist) {
4317 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004318 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004319
Fred Drakec9680921999-12-13 16:37:25 +00004320 return result;
4321}
4322#endif
4323
Antoine Pitroub7572f02009-12-02 20:46:48 +00004324#ifdef HAVE_INITGROUPS
4325PyDoc_STRVAR(posix_initgroups__doc__,
4326"initgroups(username, gid) -> None\n\n\
4327Call the system initgroups() to initialize the group access list with all of\n\
4328the groups of which the specified username is a member, plus the specified\n\
4329group id.");
4330
4331static PyObject *
4332posix_initgroups(PyObject *self, PyObject *args)
4333{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004334 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004335 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004336 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004337 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004338
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004339 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4340 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004341 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004342 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004343
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004344 res = initgroups(username, (gid_t) gid);
4345 Py_DECREF(oname);
4346 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004347 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004348
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 Py_INCREF(Py_None);
4350 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004351}
4352#endif
4353
Martin v. Löwis606edc12002-06-13 21:09:11 +00004354#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004355PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004356"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004357Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004358
4359static PyObject *
4360posix_getpgid(PyObject *self, PyObject *args)
4361{
Victor Stinner8c62be82010-05-06 00:08:46 +00004362 pid_t pid, pgid;
4363 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4364 return NULL;
4365 pgid = getpgid(pid);
4366 if (pgid < 0)
4367 return posix_error();
4368 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004369}
4370#endif /* HAVE_GETPGID */
4371
4372
Guido van Rossumb6775db1994-08-01 11:34:53 +00004373#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004374PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004375"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004376Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004377
Barry Warsaw53699e91996-12-10 23:23:01 +00004378static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004379posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004380{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004381#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004382 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004383#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004384 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004385#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004386}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004387#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004389
Guido van Rossumb6775db1994-08-01 11:34:53 +00004390#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004391PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004392"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004393Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004394
Barry Warsaw53699e91996-12-10 23:23:01 +00004395static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004396posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004397{
Guido van Rossum64933891994-10-20 21:56:42 +00004398#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004399 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004400#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004402#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004403 return posix_error();
4404 Py_INCREF(Py_None);
4405 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004406}
4407
Guido van Rossumb6775db1994-08-01 11:34:53 +00004408#endif /* HAVE_SETPGRP */
4409
Guido van Rossumad0ee831995-03-01 10:34:45 +00004410#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004411
4412#ifdef MS_WINDOWS
4413#include <tlhelp32.h>
4414
4415static PyObject*
4416win32_getppid()
4417{
4418 HANDLE snapshot;
4419 pid_t mypid;
4420 PyObject* result = NULL;
4421 BOOL have_record;
4422 PROCESSENTRY32 pe;
4423
4424 mypid = getpid(); /* This function never fails */
4425
4426 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4427 if (snapshot == INVALID_HANDLE_VALUE)
4428 return PyErr_SetFromWindowsErr(GetLastError());
4429
4430 pe.dwSize = sizeof(pe);
4431 have_record = Process32First(snapshot, &pe);
4432 while (have_record) {
4433 if (mypid == (pid_t)pe.th32ProcessID) {
4434 /* We could cache the ulong value in a static variable. */
4435 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4436 break;
4437 }
4438
4439 have_record = Process32Next(snapshot, &pe);
4440 }
4441
4442 /* If our loop exits and our pid was not found (result will be NULL)
4443 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4444 * error anyway, so let's raise it. */
4445 if (!result)
4446 result = PyErr_SetFromWindowsErr(GetLastError());
4447
4448 CloseHandle(snapshot);
4449
4450 return result;
4451}
4452#endif /*MS_WINDOWS*/
4453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004454PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004455"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004456Return the parent's process id. If the parent process has already exited,\n\
4457Windows machines will still return its id; others systems will return the id\n\
4458of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004459
Barry Warsaw53699e91996-12-10 23:23:01 +00004460static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004461posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004462{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004463#ifdef MS_WINDOWS
4464 return win32_getppid();
4465#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004467#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004468}
4469#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004471
Fred Drake12c6e2d1999-12-14 21:25:03 +00004472#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004473PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004474"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004475Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004476
4477static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004478posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004479{
Victor Stinner8c62be82010-05-06 00:08:46 +00004480 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004481#ifdef MS_WINDOWS
4482 wchar_t user_name[UNLEN + 1];
4483 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4484
4485 if (GetUserNameW(user_name, &num_chars)) {
4486 /* num_chars is the number of unicode chars plus null terminator */
4487 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4488 }
4489 else
4490 result = PyErr_SetFromWindowsErr(GetLastError());
4491#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004492 char *name;
4493 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004494
Victor Stinner8c62be82010-05-06 00:08:46 +00004495 errno = 0;
4496 name = getlogin();
4497 if (name == NULL) {
4498 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004499 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004500 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004501 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004502 }
4503 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004504 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004505 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004506#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004507 return result;
4508}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004509#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004510
Guido van Rossumad0ee831995-03-01 10:34:45 +00004511#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004512PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004513"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004514Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004515
Barry Warsaw53699e91996-12-10 23:23:01 +00004516static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004517posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004518{
Victor Stinner8c62be82010-05-06 00:08:46 +00004519 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004520}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004521#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004522
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004523
Guido van Rossumad0ee831995-03-01 10:34:45 +00004524#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004525PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004526"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004527Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004528
Barry Warsaw53699e91996-12-10 23:23:01 +00004529static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004530posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004531{
Victor Stinner8c62be82010-05-06 00:08:46 +00004532 pid_t pid;
4533 int sig;
4534 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4535 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004536#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004537 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4538 APIRET rc;
4539 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004540 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004541
4542 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4543 APIRET rc;
4544 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004545 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004546
4547 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004548 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004549#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004550 if (kill(pid, sig) == -1)
4551 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004552#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004553 Py_INCREF(Py_None);
4554 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004555}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004556#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004557
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004558#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004559PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004560"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004561Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004562
4563static PyObject *
4564posix_killpg(PyObject *self, PyObject *args)
4565{
Victor Stinner8c62be82010-05-06 00:08:46 +00004566 int sig;
4567 pid_t pgid;
4568 /* XXX some man pages make the `pgid` parameter an int, others
4569 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4570 take the same type. Moreover, pid_t is always at least as wide as
4571 int (else compilation of this module fails), which is safe. */
4572 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4573 return NULL;
4574 if (killpg(pgid, sig) == -1)
4575 return posix_error();
4576 Py_INCREF(Py_None);
4577 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004578}
4579#endif
4580
Brian Curtineb24d742010-04-12 17:16:38 +00004581#ifdef MS_WINDOWS
4582PyDoc_STRVAR(win32_kill__doc__,
4583"kill(pid, sig)\n\n\
4584Kill a process with a signal.");
4585
4586static PyObject *
4587win32_kill(PyObject *self, PyObject *args)
4588{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004589 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004590 DWORD pid, sig, err;
4591 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004592
Victor Stinner8c62be82010-05-06 00:08:46 +00004593 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4594 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004595
Victor Stinner8c62be82010-05-06 00:08:46 +00004596 /* Console processes which share a common console can be sent CTRL+C or
4597 CTRL+BREAK events, provided they handle said events. */
4598 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4599 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4600 err = GetLastError();
4601 PyErr_SetFromWindowsErr(err);
4602 }
4603 else
4604 Py_RETURN_NONE;
4605 }
Brian Curtineb24d742010-04-12 17:16:38 +00004606
Victor Stinner8c62be82010-05-06 00:08:46 +00004607 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4608 attempt to open and terminate the process. */
4609 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4610 if (handle == NULL) {
4611 err = GetLastError();
4612 return PyErr_SetFromWindowsErr(err);
4613 }
Brian Curtineb24d742010-04-12 17:16:38 +00004614
Victor Stinner8c62be82010-05-06 00:08:46 +00004615 if (TerminateProcess(handle, sig) == 0) {
4616 err = GetLastError();
4617 result = PyErr_SetFromWindowsErr(err);
4618 } else {
4619 Py_INCREF(Py_None);
4620 result = Py_None;
4621 }
Brian Curtineb24d742010-04-12 17:16:38 +00004622
Victor Stinner8c62be82010-05-06 00:08:46 +00004623 CloseHandle(handle);
4624 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004625}
4626#endif /* MS_WINDOWS */
4627
Guido van Rossumc0125471996-06-28 18:55:32 +00004628#ifdef HAVE_PLOCK
4629
4630#ifdef HAVE_SYS_LOCK_H
4631#include <sys/lock.h>
4632#endif
4633
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004634PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004635"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004636Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004637
Barry Warsaw53699e91996-12-10 23:23:01 +00004638static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004639posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004640{
Victor Stinner8c62be82010-05-06 00:08:46 +00004641 int op;
4642 if (!PyArg_ParseTuple(args, "i:plock", &op))
4643 return NULL;
4644 if (plock(op) == -1)
4645 return posix_error();
4646 Py_INCREF(Py_None);
4647 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004648}
4649#endif
4650
Guido van Rossumb6775db1994-08-01 11:34:53 +00004651#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004652PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004653"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004654Set the current process's user id.");
4655
Barry Warsaw53699e91996-12-10 23:23:01 +00004656static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004657posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004658{
Victor Stinner8c62be82010-05-06 00:08:46 +00004659 long uid_arg;
4660 uid_t uid;
4661 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4662 return NULL;
4663 uid = uid_arg;
4664 if (uid != uid_arg) {
4665 PyErr_SetString(PyExc_OverflowError, "user id too big");
4666 return NULL;
4667 }
4668 if (setuid(uid) < 0)
4669 return posix_error();
4670 Py_INCREF(Py_None);
4671 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004672}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004673#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004674
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004675
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004676#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004677PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004678"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004679Set the current process's effective user id.");
4680
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004681static PyObject *
4682posix_seteuid (PyObject *self, PyObject *args)
4683{
Victor Stinner8c62be82010-05-06 00:08:46 +00004684 long euid_arg;
4685 uid_t euid;
4686 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4687 return NULL;
4688 euid = euid_arg;
4689 if (euid != euid_arg) {
4690 PyErr_SetString(PyExc_OverflowError, "user id too big");
4691 return NULL;
4692 }
4693 if (seteuid(euid) < 0) {
4694 return posix_error();
4695 } else {
4696 Py_INCREF(Py_None);
4697 return Py_None;
4698 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004699}
4700#endif /* HAVE_SETEUID */
4701
4702#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004703PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004704"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004705Set the current process's effective group id.");
4706
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004707static PyObject *
4708posix_setegid (PyObject *self, PyObject *args)
4709{
Victor Stinner8c62be82010-05-06 00:08:46 +00004710 long egid_arg;
4711 gid_t egid;
4712 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4713 return NULL;
4714 egid = egid_arg;
4715 if (egid != egid_arg) {
4716 PyErr_SetString(PyExc_OverflowError, "group id too big");
4717 return NULL;
4718 }
4719 if (setegid(egid) < 0) {
4720 return posix_error();
4721 } else {
4722 Py_INCREF(Py_None);
4723 return Py_None;
4724 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004725}
4726#endif /* HAVE_SETEGID */
4727
4728#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004729PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004730"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004731Set the current process's real and effective user ids.");
4732
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004733static PyObject *
4734posix_setreuid (PyObject *self, PyObject *args)
4735{
Victor Stinner8c62be82010-05-06 00:08:46 +00004736 long ruid_arg, euid_arg;
4737 uid_t ruid, euid;
4738 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4739 return NULL;
4740 if (ruid_arg == -1)
4741 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4742 else
4743 ruid = ruid_arg; /* otherwise, assign from our long */
4744 if (euid_arg == -1)
4745 euid = (uid_t)-1;
4746 else
4747 euid = euid_arg;
4748 if ((euid_arg != -1 && euid != euid_arg) ||
4749 (ruid_arg != -1 && ruid != ruid_arg)) {
4750 PyErr_SetString(PyExc_OverflowError, "user id too big");
4751 return NULL;
4752 }
4753 if (setreuid(ruid, euid) < 0) {
4754 return posix_error();
4755 } else {
4756 Py_INCREF(Py_None);
4757 return Py_None;
4758 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004759}
4760#endif /* HAVE_SETREUID */
4761
4762#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004763PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004764"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004765Set the current process's real and effective group ids.");
4766
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004767static PyObject *
4768posix_setregid (PyObject *self, PyObject *args)
4769{
Victor Stinner8c62be82010-05-06 00:08:46 +00004770 long rgid_arg, egid_arg;
4771 gid_t rgid, egid;
4772 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4773 return NULL;
4774 if (rgid_arg == -1)
4775 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4776 else
4777 rgid = rgid_arg; /* otherwise, assign from our long */
4778 if (egid_arg == -1)
4779 egid = (gid_t)-1;
4780 else
4781 egid = egid_arg;
4782 if ((egid_arg != -1 && egid != egid_arg) ||
4783 (rgid_arg != -1 && rgid != rgid_arg)) {
4784 PyErr_SetString(PyExc_OverflowError, "group id too big");
4785 return NULL;
4786 }
4787 if (setregid(rgid, egid) < 0) {
4788 return posix_error();
4789 } else {
4790 Py_INCREF(Py_None);
4791 return Py_None;
4792 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004793}
4794#endif /* HAVE_SETREGID */
4795
Guido van Rossumb6775db1994-08-01 11:34:53 +00004796#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004797PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004798"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004799Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004800
Barry Warsaw53699e91996-12-10 23:23:01 +00004801static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004802posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004803{
Victor Stinner8c62be82010-05-06 00:08:46 +00004804 long gid_arg;
4805 gid_t gid;
4806 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4807 return NULL;
4808 gid = gid_arg;
4809 if (gid != gid_arg) {
4810 PyErr_SetString(PyExc_OverflowError, "group id too big");
4811 return NULL;
4812 }
4813 if (setgid(gid) < 0)
4814 return posix_error();
4815 Py_INCREF(Py_None);
4816 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004817}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004818#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004819
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004820#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004821PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004822"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004823Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004824
4825static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004826posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004827{
Victor Stinner8c62be82010-05-06 00:08:46 +00004828 int i, len;
4829 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004830
Victor Stinner8c62be82010-05-06 00:08:46 +00004831 if (!PySequence_Check(groups)) {
4832 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4833 return NULL;
4834 }
4835 len = PySequence_Size(groups);
4836 if (len > MAX_GROUPS) {
4837 PyErr_SetString(PyExc_ValueError, "too many groups");
4838 return NULL;
4839 }
4840 for(i = 0; i < len; i++) {
4841 PyObject *elem;
4842 elem = PySequence_GetItem(groups, i);
4843 if (!elem)
4844 return NULL;
4845 if (!PyLong_Check(elem)) {
4846 PyErr_SetString(PyExc_TypeError,
4847 "groups must be integers");
4848 Py_DECREF(elem);
4849 return NULL;
4850 } else {
4851 unsigned long x = PyLong_AsUnsignedLong(elem);
4852 if (PyErr_Occurred()) {
4853 PyErr_SetString(PyExc_TypeError,
4854 "group id too big");
4855 Py_DECREF(elem);
4856 return NULL;
4857 }
4858 grouplist[i] = x;
4859 /* read back the value to see if it fitted in gid_t */
4860 if (grouplist[i] != x) {
4861 PyErr_SetString(PyExc_TypeError,
4862 "group id too big");
4863 Py_DECREF(elem);
4864 return NULL;
4865 }
4866 }
4867 Py_DECREF(elem);
4868 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004869
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 if (setgroups(len, grouplist) < 0)
4871 return posix_error();
4872 Py_INCREF(Py_None);
4873 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004874}
4875#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004876
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004877#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4878static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004879wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004880{
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 PyObject *result;
4882 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004883
Victor Stinner8c62be82010-05-06 00:08:46 +00004884 if (pid == -1)
4885 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004886
Victor Stinner8c62be82010-05-06 00:08:46 +00004887 if (struct_rusage == NULL) {
4888 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4889 if (m == NULL)
4890 return NULL;
4891 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4892 Py_DECREF(m);
4893 if (struct_rusage == NULL)
4894 return NULL;
4895 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004896
Victor Stinner8c62be82010-05-06 00:08:46 +00004897 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4898 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4899 if (!result)
4900 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004901
4902#ifndef doubletime
4903#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4904#endif
4905
Victor Stinner8c62be82010-05-06 00:08:46 +00004906 PyStructSequence_SET_ITEM(result, 0,
4907 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4908 PyStructSequence_SET_ITEM(result, 1,
4909 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004910#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004911 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4912 SET_INT(result, 2, ru->ru_maxrss);
4913 SET_INT(result, 3, ru->ru_ixrss);
4914 SET_INT(result, 4, ru->ru_idrss);
4915 SET_INT(result, 5, ru->ru_isrss);
4916 SET_INT(result, 6, ru->ru_minflt);
4917 SET_INT(result, 7, ru->ru_majflt);
4918 SET_INT(result, 8, ru->ru_nswap);
4919 SET_INT(result, 9, ru->ru_inblock);
4920 SET_INT(result, 10, ru->ru_oublock);
4921 SET_INT(result, 11, ru->ru_msgsnd);
4922 SET_INT(result, 12, ru->ru_msgrcv);
4923 SET_INT(result, 13, ru->ru_nsignals);
4924 SET_INT(result, 14, ru->ru_nvcsw);
4925 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004926#undef SET_INT
4927
Victor Stinner8c62be82010-05-06 00:08:46 +00004928 if (PyErr_Occurred()) {
4929 Py_DECREF(result);
4930 return NULL;
4931 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004932
Victor Stinner8c62be82010-05-06 00:08:46 +00004933 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004934}
4935#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4936
4937#ifdef HAVE_WAIT3
4938PyDoc_STRVAR(posix_wait3__doc__,
4939"wait3(options) -> (pid, status, rusage)\n\n\
4940Wait for completion of a child process.");
4941
4942static PyObject *
4943posix_wait3(PyObject *self, PyObject *args)
4944{
Victor Stinner8c62be82010-05-06 00:08:46 +00004945 pid_t pid;
4946 int options;
4947 struct rusage ru;
4948 WAIT_TYPE status;
4949 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004950
Victor Stinner8c62be82010-05-06 00:08:46 +00004951 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4952 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004953
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 Py_BEGIN_ALLOW_THREADS
4955 pid = wait3(&status, options, &ru);
4956 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004957
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004959}
4960#endif /* HAVE_WAIT3 */
4961
4962#ifdef HAVE_WAIT4
4963PyDoc_STRVAR(posix_wait4__doc__,
4964"wait4(pid, options) -> (pid, status, rusage)\n\n\
4965Wait for completion of a given child process.");
4966
4967static PyObject *
4968posix_wait4(PyObject *self, PyObject *args)
4969{
Victor Stinner8c62be82010-05-06 00:08:46 +00004970 pid_t pid;
4971 int options;
4972 struct rusage ru;
4973 WAIT_TYPE status;
4974 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004975
Victor Stinner8c62be82010-05-06 00:08:46 +00004976 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4977 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004978
Victor Stinner8c62be82010-05-06 00:08:46 +00004979 Py_BEGIN_ALLOW_THREADS
4980 pid = wait4(pid, &status, options, &ru);
4981 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004982
Victor Stinner8c62be82010-05-06 00:08:46 +00004983 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004984}
4985#endif /* HAVE_WAIT4 */
4986
Guido van Rossumb6775db1994-08-01 11:34:53 +00004987#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004988PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004989"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004990Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004991
Barry Warsaw53699e91996-12-10 23:23:01 +00004992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004993posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004994{
Victor Stinner8c62be82010-05-06 00:08:46 +00004995 pid_t pid;
4996 int options;
4997 WAIT_TYPE status;
4998 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004999
Victor Stinner8c62be82010-05-06 00:08:46 +00005000 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5001 return NULL;
5002 Py_BEGIN_ALLOW_THREADS
5003 pid = waitpid(pid, &status, options);
5004 Py_END_ALLOW_THREADS
5005 if (pid == -1)
5006 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005007
Victor Stinner8c62be82010-05-06 00:08:46 +00005008 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005009}
5010
Tim Petersab034fa2002-02-01 11:27:43 +00005011#elif defined(HAVE_CWAIT)
5012
5013/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005014PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005015"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005016"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005017
5018static PyObject *
5019posix_waitpid(PyObject *self, PyObject *args)
5020{
Victor Stinner8c62be82010-05-06 00:08:46 +00005021 Py_intptr_t pid;
5022 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005023
Victor Stinner8c62be82010-05-06 00:08:46 +00005024 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5025 return NULL;
5026 Py_BEGIN_ALLOW_THREADS
5027 pid = _cwait(&status, pid, options);
5028 Py_END_ALLOW_THREADS
5029 if (pid == -1)
5030 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005031
Victor Stinner8c62be82010-05-06 00:08:46 +00005032 /* shift the status left a byte so this is more like the POSIX waitpid */
5033 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005034}
5035#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005036
Guido van Rossumad0ee831995-03-01 10:34:45 +00005037#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005038PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005039"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005040Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005041
Barry Warsaw53699e91996-12-10 23:23:01 +00005042static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005043posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005044{
Victor Stinner8c62be82010-05-06 00:08:46 +00005045 pid_t pid;
5046 WAIT_TYPE status;
5047 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005048
Victor Stinner8c62be82010-05-06 00:08:46 +00005049 Py_BEGIN_ALLOW_THREADS
5050 pid = wait(&status);
5051 Py_END_ALLOW_THREADS
5052 if (pid == -1)
5053 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005054
Victor Stinner8c62be82010-05-06 00:08:46 +00005055 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005056}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005057#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005058
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005060PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005061"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005062Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005063
Barry Warsaw53699e91996-12-10 23:23:01 +00005064static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005065posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005066{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005067#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005068 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005069#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005070#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005071 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5072 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005073#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005074 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005075#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005076#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005077}
5078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005079
Guido van Rossumb6775db1994-08-01 11:34:53 +00005080#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005081PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005082"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005083Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005084
Barry Warsaw53699e91996-12-10 23:23:01 +00005085static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005086posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005087{
Victor Stinner8c62be82010-05-06 00:08:46 +00005088 PyObject* v;
5089 char buf[MAXPATHLEN];
5090 PyObject *opath;
5091 char *path;
5092 int n;
5093 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005094
Victor Stinner8c62be82010-05-06 00:08:46 +00005095 if (!PyArg_ParseTuple(args, "O&:readlink",
5096 PyUnicode_FSConverter, &opath))
5097 return NULL;
5098 path = PyBytes_AsString(opath);
5099 v = PySequence_GetItem(args, 0);
5100 if (v == NULL) {
5101 Py_DECREF(opath);
5102 return NULL;
5103 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005104
Victor Stinner8c62be82010-05-06 00:08:46 +00005105 if (PyUnicode_Check(v)) {
5106 arg_is_unicode = 1;
5107 }
5108 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005109
Victor Stinner8c62be82010-05-06 00:08:46 +00005110 Py_BEGIN_ALLOW_THREADS
5111 n = readlink(path, buf, (int) sizeof buf);
5112 Py_END_ALLOW_THREADS
5113 if (n < 0)
5114 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005115
Victor Stinner8c62be82010-05-06 00:08:46 +00005116 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005117 if (arg_is_unicode)
5118 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5119 else
5120 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005121}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005122#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005123
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005124
Brian Curtin52173d42010-12-02 18:29:18 +00005125#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005126PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005127"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005128Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005129
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005131posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005132{
Victor Stinner8c62be82010-05-06 00:08:46 +00005133 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005134}
5135#endif /* HAVE_SYMLINK */
5136
Brian Curtind40e6f72010-07-08 21:39:08 +00005137#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5138
5139PyDoc_STRVAR(win_readlink__doc__,
5140"readlink(path) -> path\n\n\
5141Return a string representing the path to which the symbolic link points.");
5142
Brian Curtind40e6f72010-07-08 21:39:08 +00005143/* Windows readlink implementation */
5144static PyObject *
5145win_readlink(PyObject *self, PyObject *args)
5146{
5147 wchar_t *path;
5148 DWORD n_bytes_returned;
5149 DWORD io_result;
5150 PyObject *result;
5151 HANDLE reparse_point_handle;
5152
5153 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5154 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5155 wchar_t *print_name;
5156
5157 if (!PyArg_ParseTuple(args,
5158 "u:readlink",
5159 &path))
5160 return NULL;
5161
5162 /* First get a handle to the reparse point */
5163 Py_BEGIN_ALLOW_THREADS
5164 reparse_point_handle = CreateFileW(
5165 path,
5166 0,
5167 0,
5168 0,
5169 OPEN_EXISTING,
5170 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5171 0);
5172 Py_END_ALLOW_THREADS
5173
5174 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5175 {
5176 return win32_error_unicode("readlink", path);
5177 }
5178
5179 Py_BEGIN_ALLOW_THREADS
5180 /* New call DeviceIoControl to read the reparse point */
5181 io_result = DeviceIoControl(
5182 reparse_point_handle,
5183 FSCTL_GET_REPARSE_POINT,
5184 0, 0, /* in buffer */
5185 target_buffer, sizeof(target_buffer),
5186 &n_bytes_returned,
5187 0 /* we're not using OVERLAPPED_IO */
5188 );
5189 CloseHandle(reparse_point_handle);
5190 Py_END_ALLOW_THREADS
5191
5192 if (io_result==0)
5193 {
5194 return win32_error_unicode("readlink", path);
5195 }
5196
5197 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5198 {
5199 PyErr_SetString(PyExc_ValueError,
5200 "not a symbolic link");
5201 return NULL;
5202 }
Brian Curtin74e45612010-07-09 15:58:59 +00005203 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5204 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5205
5206 result = PyUnicode_FromWideChar(print_name,
5207 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005208 return result;
5209}
5210
5211#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5212
Brian Curtin52173d42010-12-02 18:29:18 +00005213#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005214
5215/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5216static int has_CreateSymbolicLinkW = 0;
5217static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5218static int
5219check_CreateSymbolicLinkW()
5220{
5221 HINSTANCE hKernel32;
5222 /* only recheck */
5223 if (has_CreateSymbolicLinkW)
5224 return has_CreateSymbolicLinkW;
5225 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005226 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5227 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005228 if (Py_CreateSymbolicLinkW)
5229 has_CreateSymbolicLinkW = 1;
5230 return has_CreateSymbolicLinkW;
5231}
5232
5233PyDoc_STRVAR(win_symlink__doc__,
5234"symlink(src, dst, target_is_directory=False)\n\n\
5235Create a symbolic link pointing to src named dst.\n\
5236target_is_directory is required if the target is to be interpreted as\n\
5237a directory.\n\
5238This function requires Windows 6.0 or greater, and raises a\n\
5239NotImplementedError otherwise.");
5240
5241static PyObject *
5242win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5243{
5244 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5245 PyObject *src, *dest;
5246 int target_is_directory = 0;
5247 DWORD res;
5248 WIN32_FILE_ATTRIBUTE_DATA src_info;
5249
5250 if (!check_CreateSymbolicLinkW())
5251 {
5252 /* raise NotImplementedError */
5253 return PyErr_Format(PyExc_NotImplementedError,
5254 "CreateSymbolicLinkW not found");
5255 }
5256 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5257 kwlist, &src, &dest, &target_is_directory))
5258 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005259
5260 if (win32_can_symlink == 0)
5261 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5262
Brian Curtind40e6f72010-07-08 21:39:08 +00005263 if (!convert_to_unicode(&src)) { return NULL; }
5264 if (!convert_to_unicode(&dest)) {
5265 Py_DECREF(src);
5266 return NULL;
5267 }
5268
5269 /* if src is a directory, ensure target_is_directory==1 */
5270 if(
5271 GetFileAttributesExW(
5272 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5273 ))
5274 {
5275 target_is_directory = target_is_directory ||
5276 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5277 }
5278
5279 Py_BEGIN_ALLOW_THREADS
5280 res = Py_CreateSymbolicLinkW(
5281 PyUnicode_AsUnicode(dest),
5282 PyUnicode_AsUnicode(src),
5283 target_is_directory);
5284 Py_END_ALLOW_THREADS
5285 Py_DECREF(src);
5286 Py_DECREF(dest);
5287 if (!res)
5288 {
5289 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5290 }
5291
5292 Py_INCREF(Py_None);
5293 return Py_None;
5294}
Brian Curtin52173d42010-12-02 18:29:18 +00005295#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005296
5297#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005298#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5299static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005300system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005301{
5302 ULONG value = 0;
5303
5304 Py_BEGIN_ALLOW_THREADS
5305 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5306 Py_END_ALLOW_THREADS
5307
5308 return value;
5309}
5310
5311static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005312posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005313{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005314 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005315 return Py_BuildValue("ddddd",
5316 (double)0 /* t.tms_utime / HZ */,
5317 (double)0 /* t.tms_stime / HZ */,
5318 (double)0 /* t.tms_cutime / HZ */,
5319 (double)0 /* t.tms_cstime / HZ */,
5320 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005321}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005322#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005323#define NEED_TICKS_PER_SECOND
5324static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005325static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005326posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005327{
Victor Stinner8c62be82010-05-06 00:08:46 +00005328 struct tms t;
5329 clock_t c;
5330 errno = 0;
5331 c = times(&t);
5332 if (c == (clock_t) -1)
5333 return posix_error();
5334 return Py_BuildValue("ddddd",
5335 (double)t.tms_utime / ticks_per_second,
5336 (double)t.tms_stime / ticks_per_second,
5337 (double)t.tms_cutime / ticks_per_second,
5338 (double)t.tms_cstime / ticks_per_second,
5339 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005340}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005341#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005342#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005343
5344
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005345#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005346#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005347static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005348posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005349{
Victor Stinner8c62be82010-05-06 00:08:46 +00005350 FILETIME create, exit, kernel, user;
5351 HANDLE hProc;
5352 hProc = GetCurrentProcess();
5353 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5354 /* The fields of a FILETIME structure are the hi and lo part
5355 of a 64-bit value expressed in 100 nanosecond units.
5356 1e7 is one second in such units; 1e-7 the inverse.
5357 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5358 */
5359 return Py_BuildValue(
5360 "ddddd",
5361 (double)(user.dwHighDateTime*429.4967296 +
5362 user.dwLowDateTime*1e-7),
5363 (double)(kernel.dwHighDateTime*429.4967296 +
5364 kernel.dwLowDateTime*1e-7),
5365 (double)0,
5366 (double)0,
5367 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005368}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005369#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005370
5371#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005372PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005373"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005374Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005375#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005376
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005377
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005378#ifdef HAVE_GETSID
5379PyDoc_STRVAR(posix_getsid__doc__,
5380"getsid(pid) -> sid\n\n\
5381Call the system call getsid().");
5382
5383static PyObject *
5384posix_getsid(PyObject *self, PyObject *args)
5385{
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 pid_t pid;
5387 int sid;
5388 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5389 return NULL;
5390 sid = getsid(pid);
5391 if (sid < 0)
5392 return posix_error();
5393 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005394}
5395#endif /* HAVE_GETSID */
5396
5397
Guido van Rossumb6775db1994-08-01 11:34:53 +00005398#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005399PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005400"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005401Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005402
Barry Warsaw53699e91996-12-10 23:23:01 +00005403static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005404posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005405{
Victor Stinner8c62be82010-05-06 00:08:46 +00005406 if (setsid() < 0)
5407 return posix_error();
5408 Py_INCREF(Py_None);
5409 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005410}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005411#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005412
Guido van Rossumb6775db1994-08-01 11:34:53 +00005413#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005414PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005415"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005416Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005417
Barry Warsaw53699e91996-12-10 23:23:01 +00005418static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005419posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005420{
Victor Stinner8c62be82010-05-06 00:08:46 +00005421 pid_t pid;
5422 int pgrp;
5423 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5424 return NULL;
5425 if (setpgid(pid, pgrp) < 0)
5426 return posix_error();
5427 Py_INCREF(Py_None);
5428 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005429}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005430#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005431
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005432
Guido van Rossumb6775db1994-08-01 11:34:53 +00005433#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005434PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005435"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005436Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005437
Barry Warsaw53699e91996-12-10 23:23:01 +00005438static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005439posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005440{
Victor Stinner8c62be82010-05-06 00:08:46 +00005441 int fd;
5442 pid_t pgid;
5443 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5444 return NULL;
5445 pgid = tcgetpgrp(fd);
5446 if (pgid < 0)
5447 return posix_error();
5448 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005449}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005450#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005451
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005452
Guido van Rossumb6775db1994-08-01 11:34:53 +00005453#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005454PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005455"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005457
Barry Warsaw53699e91996-12-10 23:23:01 +00005458static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005459posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005460{
Victor Stinner8c62be82010-05-06 00:08:46 +00005461 int fd;
5462 pid_t pgid;
5463 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5464 return NULL;
5465 if (tcsetpgrp(fd, pgid) < 0)
5466 return posix_error();
5467 Py_INCREF(Py_None);
5468 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005469}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005470#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005471
Guido van Rossum687dd131993-05-17 08:34:16 +00005472/* Functions acting on file descriptors */
5473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005474PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005475"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005476Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005477
Barry Warsaw53699e91996-12-10 23:23:01 +00005478static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005479posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005480{
Victor Stinner8c62be82010-05-06 00:08:46 +00005481 PyObject *ofile;
5482 char *file;
5483 int flag;
5484 int mode = 0777;
5485 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005486
5487#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005488 PyUnicodeObject *po;
5489 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5490 Py_BEGIN_ALLOW_THREADS
5491 /* PyUnicode_AS_UNICODE OK without thread
5492 lock as it is a simple dereference. */
5493 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5494 Py_END_ALLOW_THREADS
5495 if (fd < 0)
5496 return posix_error();
5497 return PyLong_FromLong((long)fd);
5498 }
5499 /* Drop the argument parsing error as narrow strings
5500 are also valid. */
5501 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005502#endif
5503
Victor Stinner8c62be82010-05-06 00:08:46 +00005504 if (!PyArg_ParseTuple(args, "O&i|i",
5505 PyUnicode_FSConverter, &ofile,
5506 &flag, &mode))
5507 return NULL;
5508 file = PyBytes_AsString(ofile);
5509 Py_BEGIN_ALLOW_THREADS
5510 fd = open(file, flag, mode);
5511 Py_END_ALLOW_THREADS
5512 if (fd < 0)
5513 return posix_error_with_allocated_filename(ofile);
5514 Py_DECREF(ofile);
5515 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005516}
5517
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005519PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005520"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005521Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005522
Barry Warsaw53699e91996-12-10 23:23:01 +00005523static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005524posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005525{
Victor Stinner8c62be82010-05-06 00:08:46 +00005526 int fd, res;
5527 if (!PyArg_ParseTuple(args, "i:close", &fd))
5528 return NULL;
5529 if (!_PyVerify_fd(fd))
5530 return posix_error();
5531 Py_BEGIN_ALLOW_THREADS
5532 res = close(fd);
5533 Py_END_ALLOW_THREADS
5534 if (res < 0)
5535 return posix_error();
5536 Py_INCREF(Py_None);
5537 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005538}
5539
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005540
Victor Stinner8c62be82010-05-06 00:08:46 +00005541PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005542"closerange(fd_low, fd_high)\n\n\
5543Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5544
5545static PyObject *
5546posix_closerange(PyObject *self, PyObject *args)
5547{
Victor Stinner8c62be82010-05-06 00:08:46 +00005548 int fd_from, fd_to, i;
5549 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5550 return NULL;
5551 Py_BEGIN_ALLOW_THREADS
5552 for (i = fd_from; i < fd_to; i++)
5553 if (_PyVerify_fd(i))
5554 close(i);
5555 Py_END_ALLOW_THREADS
5556 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005557}
5558
5559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005560PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005561"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005562Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005563
Barry Warsaw53699e91996-12-10 23:23:01 +00005564static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005565posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005566{
Victor Stinner8c62be82010-05-06 00:08:46 +00005567 int fd;
5568 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5569 return NULL;
5570 if (!_PyVerify_fd(fd))
5571 return posix_error();
5572 Py_BEGIN_ALLOW_THREADS
5573 fd = dup(fd);
5574 Py_END_ALLOW_THREADS
5575 if (fd < 0)
5576 return posix_error();
5577 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005578}
5579
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005581PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005582"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005583Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005584
Barry Warsaw53699e91996-12-10 23:23:01 +00005585static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005586posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005587{
Victor Stinner8c62be82010-05-06 00:08:46 +00005588 int fd, fd2, res;
5589 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5590 return NULL;
5591 if (!_PyVerify_fd_dup2(fd, fd2))
5592 return posix_error();
5593 Py_BEGIN_ALLOW_THREADS
5594 res = dup2(fd, fd2);
5595 Py_END_ALLOW_THREADS
5596 if (res < 0)
5597 return posix_error();
5598 Py_INCREF(Py_None);
5599 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005600}
5601
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005604"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005605Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005606
Barry Warsaw53699e91996-12-10 23:23:01 +00005607static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005608posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005609{
Victor Stinner8c62be82010-05-06 00:08:46 +00005610 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005611#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005612 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005613#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005615#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 PyObject *posobj;
5617 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5618 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005619#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005620 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5621 switch (how) {
5622 case 0: how = SEEK_SET; break;
5623 case 1: how = SEEK_CUR; break;
5624 case 2: how = SEEK_END; break;
5625 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005626#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005627
5628#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005629 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005630#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 pos = PyLong_Check(posobj) ?
5632 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005633#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005634 if (PyErr_Occurred())
5635 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005636
Victor Stinner8c62be82010-05-06 00:08:46 +00005637 if (!_PyVerify_fd(fd))
5638 return posix_error();
5639 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005640#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005641 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005642#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005643 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005644#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005645 Py_END_ALLOW_THREADS
5646 if (res < 0)
5647 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005648
5649#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005650 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005651#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005652 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005653#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005654}
5655
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005656
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005657PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005658"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005659Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005660
Barry Warsaw53699e91996-12-10 23:23:01 +00005661static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005662posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005663{
Victor Stinner8c62be82010-05-06 00:08:46 +00005664 int fd, size;
5665 Py_ssize_t n;
5666 PyObject *buffer;
5667 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5668 return NULL;
5669 if (size < 0) {
5670 errno = EINVAL;
5671 return posix_error();
5672 }
5673 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5674 if (buffer == NULL)
5675 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005676 if (!_PyVerify_fd(fd)) {
5677 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005679 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005680 Py_BEGIN_ALLOW_THREADS
5681 n = read(fd, PyBytes_AS_STRING(buffer), size);
5682 Py_END_ALLOW_THREADS
5683 if (n < 0) {
5684 Py_DECREF(buffer);
5685 return posix_error();
5686 }
5687 if (n != size)
5688 _PyBytes_Resize(&buffer, n);
5689 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005690}
5691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005692
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005693PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005694"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005695Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005696
Barry Warsaw53699e91996-12-10 23:23:01 +00005697static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005698posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005699{
Victor Stinner8c62be82010-05-06 00:08:46 +00005700 Py_buffer pbuf;
5701 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005702 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005703
Victor Stinner8c62be82010-05-06 00:08:46 +00005704 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5705 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005706 if (!_PyVerify_fd(fd)) {
5707 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005708 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005709 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005710 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005711 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005712#if defined(MS_WIN64) || defined(MS_WINDOWS)
5713 if (len > INT_MAX)
5714 len = INT_MAX;
5715 size = write(fd, pbuf.buf, (int)len);
5716#else
Victor Stinner72344792011-01-11 00:04:12 +00005717 size = write(fd, pbuf.buf, len);
Victor Stinnere6edec22011-01-04 00:29:35 +00005718#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005720 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005721 if (size < 0)
5722 return posix_error();
5723 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005724}
5725
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005727PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005728"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005729Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005730
Barry Warsaw53699e91996-12-10 23:23:01 +00005731static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005732posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005733{
Victor Stinner8c62be82010-05-06 00:08:46 +00005734 int fd;
5735 STRUCT_STAT st;
5736 int res;
5737 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5738 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005739#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005740 /* on OpenVMS we must ensure that all bytes are written to the file */
5741 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005742#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005743 if (!_PyVerify_fd(fd))
5744 return posix_error();
5745 Py_BEGIN_ALLOW_THREADS
5746 res = FSTAT(fd, &st);
5747 Py_END_ALLOW_THREADS
5748 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005749#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005750 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005751#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005752 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005753#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005754 }
Tim Peters5aa91602002-01-30 05:46:57 +00005755
Victor Stinner8c62be82010-05-06 00:08:46 +00005756 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005757}
5758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005759PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005760"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005761Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005762connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005763
5764static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005765posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005766{
Victor Stinner8c62be82010-05-06 00:08:46 +00005767 int fd;
5768 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5769 return NULL;
5770 if (!_PyVerify_fd(fd))
5771 return PyBool_FromLong(0);
5772 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005773}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005774
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005775#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005776PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005777"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005778Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005779
Barry Warsaw53699e91996-12-10 23:23:01 +00005780static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005781posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005782{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005783#if defined(PYOS_OS2)
5784 HFILE read, write;
5785 APIRET rc;
5786
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005788 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005789 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005790 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005791 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005792
5793 return Py_BuildValue("(ii)", read, write);
5794#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005795#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 int fds[2];
5797 int res;
5798 Py_BEGIN_ALLOW_THREADS
5799 res = pipe(fds);
5800 Py_END_ALLOW_THREADS
5801 if (res != 0)
5802 return posix_error();
5803 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005804#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005805 HANDLE read, write;
5806 int read_fd, write_fd;
5807 BOOL ok;
5808 Py_BEGIN_ALLOW_THREADS
5809 ok = CreatePipe(&read, &write, NULL, 0);
5810 Py_END_ALLOW_THREADS
5811 if (!ok)
5812 return win32_error("CreatePipe", NULL);
5813 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5814 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5815 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005816#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005817#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005818}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005819#endif /* HAVE_PIPE */
5820
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005821
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005822#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005823PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005824"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005825Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005826
Barry Warsaw53699e91996-12-10 23:23:01 +00005827static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005828posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005829{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005830 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005831 char *filename;
5832 int mode = 0666;
5833 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005834 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5835 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005836 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005837 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005838 Py_BEGIN_ALLOW_THREADS
5839 res = mkfifo(filename, mode);
5840 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005841 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005842 if (res < 0)
5843 return posix_error();
5844 Py_INCREF(Py_None);
5845 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005846}
5847#endif
5848
5849
Neal Norwitz11690112002-07-30 01:08:28 +00005850#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005851PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005852"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005853Create a filesystem node (file, device special file or named pipe)\n\
5854named filename. mode specifies both the permissions to use and the\n\
5855type of node to be created, being combined (bitwise OR) with one of\n\
5856S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005857device defines the newly created device special file (probably using\n\
5858os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005859
5860
5861static PyObject *
5862posix_mknod(PyObject *self, PyObject *args)
5863{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005864 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 char *filename;
5866 int mode = 0600;
5867 int device = 0;
5868 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005869 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5870 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005871 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005872 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 Py_BEGIN_ALLOW_THREADS
5874 res = mknod(filename, mode, device);
5875 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005876 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005877 if (res < 0)
5878 return posix_error();
5879 Py_INCREF(Py_None);
5880 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005881}
5882#endif
5883
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005884#ifdef HAVE_DEVICE_MACROS
5885PyDoc_STRVAR(posix_major__doc__,
5886"major(device) -> major number\n\
5887Extracts a device major number from a raw device number.");
5888
5889static PyObject *
5890posix_major(PyObject *self, PyObject *args)
5891{
Victor Stinner8c62be82010-05-06 00:08:46 +00005892 int device;
5893 if (!PyArg_ParseTuple(args, "i:major", &device))
5894 return NULL;
5895 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005896}
5897
5898PyDoc_STRVAR(posix_minor__doc__,
5899"minor(device) -> minor number\n\
5900Extracts a device minor number from a raw device number.");
5901
5902static PyObject *
5903posix_minor(PyObject *self, PyObject *args)
5904{
Victor Stinner8c62be82010-05-06 00:08:46 +00005905 int device;
5906 if (!PyArg_ParseTuple(args, "i:minor", &device))
5907 return NULL;
5908 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005909}
5910
5911PyDoc_STRVAR(posix_makedev__doc__,
5912"makedev(major, minor) -> device number\n\
5913Composes a raw device number from the major and minor device numbers.");
5914
5915static PyObject *
5916posix_makedev(PyObject *self, PyObject *args)
5917{
Victor Stinner8c62be82010-05-06 00:08:46 +00005918 int major, minor;
5919 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5920 return NULL;
5921 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005922}
5923#endif /* device macros */
5924
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005925
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005926#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005927PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005928"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005929Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005930
Barry Warsaw53699e91996-12-10 23:23:01 +00005931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005932posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005933{
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 int fd;
5935 off_t length;
5936 int res;
5937 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005938
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5940 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005941
5942#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005944#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005945 length = PyLong_Check(lenobj) ?
5946 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005947#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005948 if (PyErr_Occurred())
5949 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005950
Victor Stinner8c62be82010-05-06 00:08:46 +00005951 Py_BEGIN_ALLOW_THREADS
5952 res = ftruncate(fd, length);
5953 Py_END_ALLOW_THREADS
5954 if (res < 0)
5955 return posix_error();
5956 Py_INCREF(Py_None);
5957 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005958}
5959#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005960
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005961#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005962PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005963"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005964Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005965
Fred Drake762e2061999-08-26 17:23:54 +00005966/* Save putenv() parameters as values here, so we can collect them when they
5967 * get re-set with another call for the same key. */
5968static PyObject *posix_putenv_garbage;
5969
Tim Peters5aa91602002-01-30 05:46:57 +00005970static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005971posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005972{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005973#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005974 wchar_t *s1, *s2;
5975 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005976#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005977 PyObject *os1, *os2;
5978 char *s1, *s2;
5979 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005980#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005981 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005982 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005983
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005984#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005985 if (!PyArg_ParseTuple(args,
5986 "uu:putenv",
5987 &s1, &s2))
5988 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005989#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 if (!PyArg_ParseTuple(args,
5991 "O&O&:putenv",
5992 PyUnicode_FSConverter, &os1,
5993 PyUnicode_FSConverter, &os2))
5994 return NULL;
5995 s1 = PyBytes_AsString(os1);
5996 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005997#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005998
5999#if defined(PYOS_OS2)
6000 if (stricmp(s1, "BEGINLIBPATH") == 0) {
6001 APIRET rc;
6002
Guido van Rossumd48f2521997-12-05 22:19:34 +00006003 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006004 if (rc != NO_ERROR) {
6005 os2_error(rc);
6006 goto error;
6007 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006008
6009 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6010 APIRET rc;
6011
Guido van Rossumd48f2521997-12-05 22:19:34 +00006012 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006013 if (rc != NO_ERROR) {
6014 os2_error(rc);
6015 goto error;
6016 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006017 } else {
6018#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006019 /* XXX This can leak memory -- not easy to fix :-( */
6020 /* len includes space for a trailing \0; the size arg to
6021 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006022#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006023 len = wcslen(s1) + wcslen(s2) + 2;
6024 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006025#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006026 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006028#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006029 if (newstr == NULL) {
6030 PyErr_NoMemory();
6031 goto error;
6032 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006033#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006034 newenv = PyUnicode_AsUnicode(newstr);
6035 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6036 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006037 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006038 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006040#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006041 newenv = PyBytes_AS_STRING(newstr);
6042 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6043 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006044 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006045 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006047#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006048
Victor Stinner8c62be82010-05-06 00:08:46 +00006049 /* Install the first arg and newstr in posix_putenv_garbage;
6050 * this will cause previous value to be collected. This has to
6051 * happen after the real putenv() call because the old value
6052 * was still accessible until then. */
6053 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006054#ifdef MS_WINDOWS
6055 PyTuple_GET_ITEM(args, 0),
6056#else
6057 os1,
6058#endif
6059 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 /* really not much we can do; just leak */
6061 PyErr_Clear();
6062 }
6063 else {
6064 Py_DECREF(newstr);
6065 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006066
6067#if defined(PYOS_OS2)
6068 }
6069#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006070
Martin v. Löwis011e8422009-05-05 04:43:17 +00006071#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006072 Py_DECREF(os1);
6073 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006074#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006075 Py_RETURN_NONE;
6076
6077error:
6078#ifndef MS_WINDOWS
6079 Py_DECREF(os1);
6080 Py_DECREF(os2);
6081#endif
6082 Py_XDECREF(newstr);
6083 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006084}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006085#endif /* putenv */
6086
Guido van Rossumc524d952001-10-19 01:31:59 +00006087#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006088PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006089"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006090Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006091
6092static PyObject *
6093posix_unsetenv(PyObject *self, PyObject *args)
6094{
Victor Stinner84ae1182010-05-06 22:05:07 +00006095#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006096 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006097
Victor Stinner8c62be82010-05-06 00:08:46 +00006098 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6099 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006100#else
6101 PyObject *os1;
6102 char *s1;
6103
6104 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6105 PyUnicode_FSConverter, &os1))
6106 return NULL;
6107 s1 = PyBytes_AsString(os1);
6108#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006109
Victor Stinner8c62be82010-05-06 00:08:46 +00006110 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006111
Victor Stinner8c62be82010-05-06 00:08:46 +00006112 /* Remove the key from posix_putenv_garbage;
6113 * this will cause it to be collected. This has to
6114 * happen after the real unsetenv() call because the
6115 * old value was still accessible until then.
6116 */
6117 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006118#ifdef MS_WINDOWS
6119 PyTuple_GET_ITEM(args, 0)
6120#else
6121 os1
6122#endif
6123 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006124 /* really not much we can do; just leak */
6125 PyErr_Clear();
6126 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006127
Victor Stinner84ae1182010-05-06 22:05:07 +00006128#ifndef MS_WINDOWS
6129 Py_DECREF(os1);
6130#endif
6131 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006132}
6133#endif /* unsetenv */
6134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006135PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006136"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006137Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006138
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006140posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006141{
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 int code;
6143 char *message;
6144 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6145 return NULL;
6146 message = strerror(code);
6147 if (message == NULL) {
6148 PyErr_SetString(PyExc_ValueError,
6149 "strerror() argument out of range");
6150 return NULL;
6151 }
6152 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006153}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006154
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006155
Guido van Rossumc9641791998-08-04 15:26:23 +00006156#ifdef HAVE_SYS_WAIT_H
6157
Fred Drake106c1a02002-04-23 15:58:02 +00006158#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006159PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006160"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006161Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006162
6163static PyObject *
6164posix_WCOREDUMP(PyObject *self, PyObject *args)
6165{
Victor Stinner8c62be82010-05-06 00:08:46 +00006166 WAIT_TYPE status;
6167 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006168
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6170 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006171
Victor Stinner8c62be82010-05-06 00:08:46 +00006172 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006173}
6174#endif /* WCOREDUMP */
6175
6176#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006178"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006179Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006180job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006181
6182static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006183posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006184{
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 WAIT_TYPE status;
6186 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006187
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6189 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006190
Victor Stinner8c62be82010-05-06 00:08:46 +00006191 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006192}
6193#endif /* WIFCONTINUED */
6194
Guido van Rossumc9641791998-08-04 15:26:23 +00006195#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006196PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006197"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006198Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006199
6200static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006201posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006202{
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 WAIT_TYPE status;
6204 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006205
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6207 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006208
Victor Stinner8c62be82010-05-06 00:08:46 +00006209 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006210}
6211#endif /* WIFSTOPPED */
6212
6213#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006214PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006215"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006216Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006217
6218static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006219posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006220{
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 WAIT_TYPE status;
6222 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006223
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6225 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006226
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006228}
6229#endif /* WIFSIGNALED */
6230
6231#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006232PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006233"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006234Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006235system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006236
6237static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006238posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006239{
Victor Stinner8c62be82010-05-06 00:08:46 +00006240 WAIT_TYPE status;
6241 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006242
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6244 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006245
Victor Stinner8c62be82010-05-06 00:08:46 +00006246 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006247}
6248#endif /* WIFEXITED */
6249
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006250#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006251PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006252"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006253Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006254
6255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006256posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006257{
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 WAIT_TYPE status;
6259 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006260
Victor Stinner8c62be82010-05-06 00:08:46 +00006261 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6262 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006263
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006265}
6266#endif /* WEXITSTATUS */
6267
6268#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006269PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006270"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006271Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006272value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006273
6274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006275posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006276{
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 WAIT_TYPE status;
6278 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006279
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6281 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006282
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006284}
6285#endif /* WTERMSIG */
6286
6287#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006288PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006289"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006290Return the signal that stopped the process that provided\n\
6291the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006292
6293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006294posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006295{
Victor Stinner8c62be82010-05-06 00:08:46 +00006296 WAIT_TYPE status;
6297 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006298
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6300 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006301
Victor Stinner8c62be82010-05-06 00:08:46 +00006302 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006303}
6304#endif /* WSTOPSIG */
6305
6306#endif /* HAVE_SYS_WAIT_H */
6307
6308
Thomas Wouters477c8d52006-05-27 19:21:47 +00006309#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006310#ifdef _SCO_DS
6311/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6312 needed definitions in sys/statvfs.h */
6313#define _SVID3
6314#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006315#include <sys/statvfs.h>
6316
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006317static PyObject*
6318_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6320 if (v == NULL)
6321 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006322
6323#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006324 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6325 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6326 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6327 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6328 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6329 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6330 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6331 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6332 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6333 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006334#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006335 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6336 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6337 PyStructSequence_SET_ITEM(v, 2,
6338 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6339 PyStructSequence_SET_ITEM(v, 3,
6340 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6341 PyStructSequence_SET_ITEM(v, 4,
6342 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6343 PyStructSequence_SET_ITEM(v, 5,
6344 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6345 PyStructSequence_SET_ITEM(v, 6,
6346 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6347 PyStructSequence_SET_ITEM(v, 7,
6348 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6349 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6350 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006351#endif
6352
Victor Stinner8c62be82010-05-06 00:08:46 +00006353 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006354}
6355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006356PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006357"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006358Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006359
6360static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006361posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006362{
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 int fd, res;
6364 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006365
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6367 return NULL;
6368 Py_BEGIN_ALLOW_THREADS
6369 res = fstatvfs(fd, &st);
6370 Py_END_ALLOW_THREADS
6371 if (res != 0)
6372 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006373
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006375}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006376#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006377
6378
Thomas Wouters477c8d52006-05-27 19:21:47 +00006379#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006380#include <sys/statvfs.h>
6381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006382PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006383"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006384Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006385
6386static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006387posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006388{
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 char *path;
6390 int res;
6391 struct statvfs st;
6392 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6393 return NULL;
6394 Py_BEGIN_ALLOW_THREADS
6395 res = statvfs(path, &st);
6396 Py_END_ALLOW_THREADS
6397 if (res != 0)
6398 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006399
Victor Stinner8c62be82010-05-06 00:08:46 +00006400 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006401}
6402#endif /* HAVE_STATVFS */
6403
Fred Drakec9680921999-12-13 16:37:25 +00006404/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6405 * It maps strings representing configuration variable names to
6406 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006407 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006408 * rarely-used constants. There are three separate tables that use
6409 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006410 *
6411 * This code is always included, even if none of the interfaces that
6412 * need it are included. The #if hackery needed to avoid it would be
6413 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006414 */
6415struct constdef {
6416 char *name;
6417 long value;
6418};
6419
Fred Drake12c6e2d1999-12-14 21:25:03 +00006420static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006421conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006422 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006423{
Christian Heimes217cfd12007-12-02 14:31:20 +00006424 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006425 *valuep = PyLong_AS_LONG(arg);
6426 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006427 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006428 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006429 /* look up the value in the table using a binary search */
6430 size_t lo = 0;
6431 size_t mid;
6432 size_t hi = tablesize;
6433 int cmp;
6434 const char *confname;
6435 if (!PyUnicode_Check(arg)) {
6436 PyErr_SetString(PyExc_TypeError,
6437 "configuration names must be strings or integers");
6438 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006439 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006440 confname = _PyUnicode_AsString(arg);
6441 if (confname == NULL)
6442 return 0;
6443 while (lo < hi) {
6444 mid = (lo + hi) / 2;
6445 cmp = strcmp(confname, table[mid].name);
6446 if (cmp < 0)
6447 hi = mid;
6448 else if (cmp > 0)
6449 lo = mid + 1;
6450 else {
6451 *valuep = table[mid].value;
6452 return 1;
6453 }
6454 }
6455 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6456 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006457 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006458}
6459
6460
6461#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6462static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006463#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006464 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006465#endif
6466#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006468#endif
Fred Drakec9680921999-12-13 16:37:25 +00006469#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006471#endif
6472#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006474#endif
6475#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006477#endif
6478#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006480#endif
6481#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006483#endif
6484#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006486#endif
6487#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006489#endif
6490#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006491 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006492#endif
6493#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006494 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006495#endif
6496#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006498#endif
6499#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006501#endif
6502#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006504#endif
6505#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006507#endif
6508#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
6511#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006513#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006514#ifdef _PC_ACL_ENABLED
6515 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6516#endif
6517#ifdef _PC_MIN_HOLE_SIZE
6518 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6519#endif
6520#ifdef _PC_ALLOC_SIZE_MIN
6521 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6522#endif
6523#ifdef _PC_REC_INCR_XFER_SIZE
6524 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6525#endif
6526#ifdef _PC_REC_MAX_XFER_SIZE
6527 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6528#endif
6529#ifdef _PC_REC_MIN_XFER_SIZE
6530 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6531#endif
6532#ifdef _PC_REC_XFER_ALIGN
6533 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6534#endif
6535#ifdef _PC_SYMLINK_MAX
6536 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6537#endif
6538#ifdef _PC_XATTR_ENABLED
6539 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6540#endif
6541#ifdef _PC_XATTR_EXISTS
6542 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6543#endif
6544#ifdef _PC_TIMESTAMP_RESOLUTION
6545 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6546#endif
Fred Drakec9680921999-12-13 16:37:25 +00006547};
6548
Fred Drakec9680921999-12-13 16:37:25 +00006549static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006550conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006551{
6552 return conv_confname(arg, valuep, posix_constants_pathconf,
6553 sizeof(posix_constants_pathconf)
6554 / sizeof(struct constdef));
6555}
6556#endif
6557
6558#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006559PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006560"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006561Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006562If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006563
6564static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006565posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006566{
6567 PyObject *result = NULL;
6568 int name, fd;
6569
Fred Drake12c6e2d1999-12-14 21:25:03 +00006570 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6571 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006572 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006573
Stefan Krah0e803b32010-11-26 16:16:47 +00006574 errno = 0;
6575 limit = fpathconf(fd, name);
6576 if (limit == -1 && errno != 0)
6577 posix_error();
6578 else
6579 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006580 }
6581 return result;
6582}
6583#endif
6584
6585
6586#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006587PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006588"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006589Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006590If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006591
6592static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006593posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006594{
6595 PyObject *result = NULL;
6596 int name;
6597 char *path;
6598
6599 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6600 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006601 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006602
Victor Stinner8c62be82010-05-06 00:08:46 +00006603 errno = 0;
6604 limit = pathconf(path, name);
6605 if (limit == -1 && errno != 0) {
6606 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006607 /* could be a path or name problem */
6608 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006609 else
Stefan Krah99439262010-11-26 12:58:05 +00006610 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006611 }
6612 else
6613 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006614 }
6615 return result;
6616}
6617#endif
6618
6619#ifdef HAVE_CONFSTR
6620static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006621#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006622 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006623#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006624#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006626#endif
6627#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006629#endif
Fred Draked86ed291999-12-15 15:34:33 +00006630#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006632#endif
6633#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006635#endif
6636#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006638#endif
6639#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006641#endif
Fred Drakec9680921999-12-13 16:37:25 +00006642#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006644#endif
6645#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006647#endif
6648#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006650#endif
6651#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006653#endif
6654#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006656#endif
6657#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006659#endif
6660#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006662#endif
6663#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006665#endif
Fred Draked86ed291999-12-15 15:34:33 +00006666#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006668#endif
Fred Drakec9680921999-12-13 16:37:25 +00006669#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006671#endif
Fred Draked86ed291999-12-15 15:34:33 +00006672#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006674#endif
6675#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006677#endif
6678#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006680#endif
6681#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006683#endif
Fred Drakec9680921999-12-13 16:37:25 +00006684#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006686#endif
6687#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006689#endif
6690#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006692#endif
6693#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006695#endif
6696#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006698#endif
6699#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006701#endif
6702#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006704#endif
6705#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006707#endif
6708#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006710#endif
6711#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006713#endif
6714#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006716#endif
6717#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006719#endif
6720#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006722#endif
6723#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006725#endif
6726#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006728#endif
6729#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006731#endif
Fred Draked86ed291999-12-15 15:34:33 +00006732#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006734#endif
6735#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006736 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006737#endif
6738#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006740#endif
6741#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006742 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006743#endif
6744#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006746#endif
6747#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006748 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006749#endif
6750#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006751 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006752#endif
6753#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006754 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006755#endif
6756#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006758#endif
6759#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006760 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006761#endif
6762#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006764#endif
6765#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006767#endif
6768#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006769 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006770#endif
Fred Drakec9680921999-12-13 16:37:25 +00006771};
6772
6773static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006774conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006775{
6776 return conv_confname(arg, valuep, posix_constants_confstr,
6777 sizeof(posix_constants_confstr)
6778 / sizeof(struct constdef));
6779}
6780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006781PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006782"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006783Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006784
6785static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006786posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006787{
6788 PyObject *result = NULL;
6789 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006790 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006791 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006792
Victor Stinnercb043522010-09-10 23:49:04 +00006793 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6794 return NULL;
6795
6796 errno = 0;
6797 len = confstr(name, buffer, sizeof(buffer));
6798 if (len == 0) {
6799 if (errno) {
6800 posix_error();
6801 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006802 }
6803 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006804 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006805 }
6806 }
Victor Stinnercb043522010-09-10 23:49:04 +00006807
6808 if ((unsigned int)len >= sizeof(buffer)) {
6809 char *buf = PyMem_Malloc(len);
6810 if (buf == NULL)
6811 return PyErr_NoMemory();
6812 confstr(name, buf, len);
6813 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6814 PyMem_Free(buf);
6815 }
6816 else
6817 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006818 return result;
6819}
6820#endif
6821
6822
6823#ifdef HAVE_SYSCONF
6824static struct constdef posix_constants_sysconf[] = {
6825#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006827#endif
6828#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006830#endif
6831#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006833#endif
6834#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006836#endif
6837#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006839#endif
6840#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006841 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006842#endif
6843#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006845#endif
6846#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006848#endif
6849#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006851#endif
6852#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006854#endif
Fred Draked86ed291999-12-15 15:34:33 +00006855#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006857#endif
6858#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006860#endif
Fred Drakec9680921999-12-13 16:37:25 +00006861#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006863#endif
Fred Drakec9680921999-12-13 16:37:25 +00006864#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
6873#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006875#endif
6876#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006878#endif
Fred Draked86ed291999-12-15 15:34:33 +00006879#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006881#endif
Fred Drakec9680921999-12-13 16:37:25 +00006882#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
6891#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006893#endif
6894#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006896#endif
Fred Draked86ed291999-12-15 15:34:33 +00006897#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006899#endif
Fred Drakec9680921999-12-13 16:37:25 +00006900#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
6903#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
6909#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006911#endif
6912#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006914#endif
6915#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006917#endif
6918#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006920#endif
6921#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006923#endif
6924#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006926#endif
6927#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006929#endif
6930#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006932#endif
6933#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006935#endif
6936#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006938#endif
6939#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006941#endif
6942#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006944#endif
6945#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006947#endif
6948#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006950#endif
6951#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006953#endif
6954#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006956#endif
6957#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006959#endif
6960#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
6963#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006965#endif
6966#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006968#endif
Fred Draked86ed291999-12-15 15:34:33 +00006969#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006971#endif
Fred Drakec9680921999-12-13 16:37:25 +00006972#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
6978#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006980#endif
Fred Draked86ed291999-12-15 15:34:33 +00006981#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006983#endif
Fred Drakec9680921999-12-13 16:37:25 +00006984#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006986#endif
Fred Draked86ed291999-12-15 15:34:33 +00006987#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006989#endif
6990#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006992#endif
Fred Drakec9680921999-12-13 16:37:25 +00006993#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006995#endif
6996#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006998#endif
6999#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007001#endif
7002#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
Fred Draked86ed291999-12-15 15:34:33 +00007005#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007007#endif
Fred Drakec9680921999-12-13 16:37:25 +00007008#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
7017#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
7020#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
7023#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
7026#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007028#endif
Fred Draked86ed291999-12-15 15:34:33 +00007029#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007031#endif
Fred Drakec9680921999-12-13 16:37:25 +00007032#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007034#endif
7035#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007037#endif
Fred Draked86ed291999-12-15 15:34:33 +00007038#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007040#endif
Fred Drakec9680921999-12-13 16:37:25 +00007041#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
7044#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007046#endif
7047#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007049#endif
7050#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
7056#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007058#endif
7059#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
7065#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007067#endif
Fred Draked86ed291999-12-15 15:34:33 +00007068#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007070#endif
7071#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007073#endif
Fred Drakec9680921999-12-13 16:37:25 +00007074#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007076#endif
7077#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007079#endif
7080#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007103#endif
7104#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007106#endif
7107#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007109#endif
7110#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007112#endif
7113#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007115#endif
7116#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007118#endif
7119#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007121#endif
7122#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007124#endif
7125#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
7128#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007130#endif
7131#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007133#endif
7134#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007136#endif
7137#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007139#endif
7140#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007142#endif
7143#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
7146#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
7149#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
7155#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007157#endif
7158#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007160#endif
7161#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
7164#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007166#endif
7167#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007169#endif
7170#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007172#endif
7173#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007175#endif
7176#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007178#endif
Fred Draked86ed291999-12-15 15:34:33 +00007179#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007181#endif
Fred Drakec9680921999-12-13 16:37:25 +00007182#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007184#endif
7185#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
7194#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007196#endif
7197#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
7206#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007208#endif
7209#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
7212#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007214#endif
7215#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007217#endif
7218#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007220#endif
7221#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
7245#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007247#endif
7248#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007250#endif
7251#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007280#endif
7281#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007283#endif
7284#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007286#endif
7287#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007289#endif
7290#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007292#endif
7293#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007295#endif
7296#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007298#endif
7299#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007301#endif
7302#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007303 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007304#endif
7305#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007306 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007307#endif
7308#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007310#endif
7311#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007313#endif
7314#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007315 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007316#endif
7317};
7318
7319static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007320conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007321{
7322 return conv_confname(arg, valuep, posix_constants_sysconf,
7323 sizeof(posix_constants_sysconf)
7324 / sizeof(struct constdef));
7325}
7326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007327PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007328"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007329Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007330
7331static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007332posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007333{
7334 PyObject *result = NULL;
7335 int name;
7336
7337 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7338 int value;
7339
7340 errno = 0;
7341 value = sysconf(name);
7342 if (value == -1 && errno != 0)
7343 posix_error();
7344 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007345 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007346 }
7347 return result;
7348}
7349#endif
7350
7351
Fred Drakebec628d1999-12-15 18:31:10 +00007352/* This code is used to ensure that the tables of configuration value names
7353 * are in sorted order as required by conv_confname(), and also to build the
7354 * the exported dictionaries that are used to publish information about the
7355 * names available on the host platform.
7356 *
7357 * Sorting the table at runtime ensures that the table is properly ordered
7358 * when used, even for platforms we're not able to test on. It also makes
7359 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007360 */
Fred Drakebec628d1999-12-15 18:31:10 +00007361
7362static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007363cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007364{
7365 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007367 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007369
7370 return strcmp(c1->name, c2->name);
7371}
7372
7373static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007374setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007375 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007376{
Fred Drakebec628d1999-12-15 18:31:10 +00007377 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007378 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007379
7380 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7381 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007382 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007384
Barry Warsaw3155db32000-04-13 15:20:40 +00007385 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007386 PyObject *o = PyLong_FromLong(table[i].value);
7387 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7388 Py_XDECREF(o);
7389 Py_DECREF(d);
7390 return -1;
7391 }
7392 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007393 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007394 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007395}
7396
Fred Drakebec628d1999-12-15 18:31:10 +00007397/* Return -1 on failure, 0 on success. */
7398static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007399setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007400{
7401#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007402 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007403 sizeof(posix_constants_pathconf)
7404 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007405 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007406 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007407#endif
7408#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007409 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007410 sizeof(posix_constants_confstr)
7411 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007412 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007413 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007414#endif
7415#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007416 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007417 sizeof(posix_constants_sysconf)
7418 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007419 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007420 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007421#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007422 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007423}
Fred Draked86ed291999-12-15 15:34:33 +00007424
7425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007426PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007427"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007428Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007429in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007430
7431static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007432posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007433{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007434 abort();
7435 /*NOTREACHED*/
7436 Py_FatalError("abort() called from Python code didn't abort!");
7437 return NULL;
7438}
Fred Drakebec628d1999-12-15 18:31:10 +00007439
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007440#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007441PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007442"startfile(filepath [, operation]) - Start a file with its associated\n\
7443application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007444\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007445When \"operation\" is not specified or \"open\", this acts like\n\
7446double-clicking the file in Explorer, or giving the file name as an\n\
7447argument to the DOS \"start\" command: the file is opened with whatever\n\
7448application (if any) its extension is associated.\n\
7449When another \"operation\" is given, it specifies what should be done with\n\
7450the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007451\n\
7452startfile returns as soon as the associated application is launched.\n\
7453There is no option to wait for the application to close, and no way\n\
7454to retrieve the application's exit status.\n\
7455\n\
7456The filepath is relative to the current directory. If you want to use\n\
7457an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007458the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007459
7460static PyObject *
7461win32_startfile(PyObject *self, PyObject *args)
7462{
Victor Stinner8c62be82010-05-06 00:08:46 +00007463 PyObject *ofilepath;
7464 char *filepath;
7465 char *operation = NULL;
7466 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007467
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 PyObject *unipath, *woperation = NULL;
7469 if (!PyArg_ParseTuple(args, "U|s:startfile",
7470 &unipath, &operation)) {
7471 PyErr_Clear();
7472 goto normal;
7473 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007474
Victor Stinner8c62be82010-05-06 00:08:46 +00007475 if (operation) {
7476 woperation = PyUnicode_DecodeASCII(operation,
7477 strlen(operation), NULL);
7478 if (!woperation) {
7479 PyErr_Clear();
7480 operation = NULL;
7481 goto normal;
7482 }
7483 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007484
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 Py_BEGIN_ALLOW_THREADS
7486 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7487 PyUnicode_AS_UNICODE(unipath),
7488 NULL, NULL, SW_SHOWNORMAL);
7489 Py_END_ALLOW_THREADS
7490
7491 Py_XDECREF(woperation);
7492 if (rc <= (HINSTANCE)32) {
7493 PyObject *errval = win32_error_unicode("startfile",
7494 PyUnicode_AS_UNICODE(unipath));
7495 return errval;
7496 }
7497 Py_INCREF(Py_None);
7498 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007499
7500normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007501 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7502 PyUnicode_FSConverter, &ofilepath,
7503 &operation))
7504 return NULL;
7505 filepath = PyBytes_AsString(ofilepath);
7506 Py_BEGIN_ALLOW_THREADS
7507 rc = ShellExecute((HWND)0, operation, filepath,
7508 NULL, NULL, SW_SHOWNORMAL);
7509 Py_END_ALLOW_THREADS
7510 if (rc <= (HINSTANCE)32) {
7511 PyObject *errval = win32_error("startfile", filepath);
7512 Py_DECREF(ofilepath);
7513 return errval;
7514 }
7515 Py_DECREF(ofilepath);
7516 Py_INCREF(Py_None);
7517 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007518}
7519#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007520
Martin v. Löwis438b5342002-12-27 10:16:42 +00007521#ifdef HAVE_GETLOADAVG
7522PyDoc_STRVAR(posix_getloadavg__doc__,
7523"getloadavg() -> (float, float, float)\n\n\
7524Return the number of processes in the system run queue averaged over\n\
7525the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7526was unobtainable");
7527
7528static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007529posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007530{
7531 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007532 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007533 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7534 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007535 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007536 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007537}
7538#endif
7539
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007540#ifdef MS_WINDOWS
7541
7542PyDoc_STRVAR(win32_urandom__doc__,
7543"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007544Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007545
7546typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7547 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7548 DWORD dwFlags );
7549typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7550 BYTE *pbBuffer );
7551
7552static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007553/* This handle is never explicitly released. Instead, the operating
7554 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007555static HCRYPTPROV hCryptProv = 0;
7556
Tim Peters4ad82172004-08-30 17:02:04 +00007557static PyObject*
7558win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007559{
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 int howMany;
7561 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007562
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 /* Read arguments */
7564 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7565 return NULL;
7566 if (howMany < 0)
7567 return PyErr_Format(PyExc_ValueError,
7568 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007569
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 if (hCryptProv == 0) {
7571 HINSTANCE hAdvAPI32 = NULL;
7572 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007573
Victor Stinner8c62be82010-05-06 00:08:46 +00007574 /* Obtain handle to the DLL containing CryptoAPI
7575 This should not fail */
7576 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7577 if(hAdvAPI32 == NULL)
7578 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007579
Victor Stinner8c62be82010-05-06 00:08:46 +00007580 /* Obtain pointers to the CryptoAPI functions
7581 This will fail on some early versions of Win95 */
7582 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7583 hAdvAPI32,
7584 "CryptAcquireContextA");
7585 if (pCryptAcquireContext == NULL)
7586 return PyErr_Format(PyExc_NotImplementedError,
7587 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007588
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7590 hAdvAPI32, "CryptGenRandom");
7591 if (pCryptGenRandom == NULL)
7592 return PyErr_Format(PyExc_NotImplementedError,
7593 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007594
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 /* Acquire context */
7596 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7597 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7598 return win32_error("CryptAcquireContext", NULL);
7599 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007600
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 /* Allocate bytes */
7602 result = PyBytes_FromStringAndSize(NULL, howMany);
7603 if (result != NULL) {
7604 /* Get random data */
7605 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7606 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7607 PyBytes_AS_STRING(result))) {
7608 Py_DECREF(result);
7609 return win32_error("CryptGenRandom", NULL);
7610 }
7611 }
7612 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007613}
7614#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007615
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007616PyDoc_STRVAR(device_encoding__doc__,
7617"device_encoding(fd) -> str\n\n\
7618Return a string describing the encoding of the device\n\
7619if the output is a terminal; else return None.");
7620
7621static PyObject *
7622device_encoding(PyObject *self, PyObject *args)
7623{
Victor Stinner8c62be82010-05-06 00:08:46 +00007624 int fd;
7625 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7626 return NULL;
7627 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7628 Py_INCREF(Py_None);
7629 return Py_None;
7630 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007631#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 if (fd == 0) {
7633 char buf[100];
7634 sprintf(buf, "cp%d", GetConsoleCP());
7635 return PyUnicode_FromString(buf);
7636 }
7637 if (fd == 1 || fd == 2) {
7638 char buf[100];
7639 sprintf(buf, "cp%d", GetConsoleOutputCP());
7640 return PyUnicode_FromString(buf);
7641 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007642#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007643 {
7644 char *codeset = nl_langinfo(CODESET);
7645 if (codeset != NULL && codeset[0] != 0)
7646 return PyUnicode_FromString(codeset);
7647 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007648#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 Py_INCREF(Py_None);
7650 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007651}
7652
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007653#ifdef __VMS
7654/* Use openssl random routine */
7655#include <openssl/rand.h>
7656PyDoc_STRVAR(vms_urandom__doc__,
7657"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007658Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007659
7660static PyObject*
7661vms_urandom(PyObject *self, PyObject *args)
7662{
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 int howMany;
7664 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007665
Victor Stinner8c62be82010-05-06 00:08:46 +00007666 /* Read arguments */
7667 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7668 return NULL;
7669 if (howMany < 0)
7670 return PyErr_Format(PyExc_ValueError,
7671 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007672
Victor Stinner8c62be82010-05-06 00:08:46 +00007673 /* Allocate bytes */
7674 result = PyBytes_FromStringAndSize(NULL, howMany);
7675 if (result != NULL) {
7676 /* Get random data */
7677 if (RAND_pseudo_bytes((unsigned char*)
7678 PyBytes_AS_STRING(result),
7679 howMany) < 0) {
7680 Py_DECREF(result);
7681 return PyErr_Format(PyExc_ValueError,
7682 "RAND_pseudo_bytes");
7683 }
7684 }
7685 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007686}
7687#endif
7688
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007689#ifdef HAVE_SETRESUID
7690PyDoc_STRVAR(posix_setresuid__doc__,
7691"setresuid(ruid, euid, suid)\n\n\
7692Set the current process's real, effective, and saved user ids.");
7693
7694static PyObject*
7695posix_setresuid (PyObject *self, PyObject *args)
7696{
Victor Stinner8c62be82010-05-06 00:08:46 +00007697 /* We assume uid_t is no larger than a long. */
7698 long ruid, euid, suid;
7699 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7700 return NULL;
7701 if (setresuid(ruid, euid, suid) < 0)
7702 return posix_error();
7703 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007704}
7705#endif
7706
7707#ifdef HAVE_SETRESGID
7708PyDoc_STRVAR(posix_setresgid__doc__,
7709"setresgid(rgid, egid, sgid)\n\n\
7710Set the current process's real, effective, and saved group ids.");
7711
7712static PyObject*
7713posix_setresgid (PyObject *self, PyObject *args)
7714{
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 /* We assume uid_t is no larger than a long. */
7716 long rgid, egid, sgid;
7717 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7718 return NULL;
7719 if (setresgid(rgid, egid, sgid) < 0)
7720 return posix_error();
7721 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007722}
7723#endif
7724
7725#ifdef HAVE_GETRESUID
7726PyDoc_STRVAR(posix_getresuid__doc__,
7727"getresuid() -> (ruid, euid, suid)\n\n\
7728Get tuple of the current process's real, effective, and saved user ids.");
7729
7730static PyObject*
7731posix_getresuid (PyObject *self, PyObject *noargs)
7732{
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 uid_t ruid, euid, suid;
7734 long l_ruid, l_euid, l_suid;
7735 if (getresuid(&ruid, &euid, &suid) < 0)
7736 return posix_error();
7737 /* Force the values into long's as we don't know the size of uid_t. */
7738 l_ruid = ruid;
7739 l_euid = euid;
7740 l_suid = suid;
7741 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007742}
7743#endif
7744
7745#ifdef HAVE_GETRESGID
7746PyDoc_STRVAR(posix_getresgid__doc__,
7747"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007748Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007749
7750static PyObject*
7751posix_getresgid (PyObject *self, PyObject *noargs)
7752{
Victor Stinner8c62be82010-05-06 00:08:46 +00007753 uid_t rgid, egid, sgid;
7754 long l_rgid, l_egid, l_sgid;
7755 if (getresgid(&rgid, &egid, &sgid) < 0)
7756 return posix_error();
7757 /* Force the values into long's as we don't know the size of uid_t. */
7758 l_rgid = rgid;
7759 l_egid = egid;
7760 l_sgid = sgid;
7761 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007762}
7763#endif
7764
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007765static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007767#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007768 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007769#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007771#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007773#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007775#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007777#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007778#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007780#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007781#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007783#endif /* HAVE_LCHMOD */
7784#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007785 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007786#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007787#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007789#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007790#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007791 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007792#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007793#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007795#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007796#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007798#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007799#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7801 METH_NOARGS, posix_getcwd__doc__},
7802 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7803 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007804#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007805#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007807#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007808 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7809 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7810 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007811#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007813#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007814#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007816#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007817#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007818 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007819#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007820 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7821 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7822 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007824#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007825 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007826#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007827#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007828 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007829 win_symlink__doc__},
7830#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007831#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007833#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007835#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007837#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7839 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7840 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007841#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007843#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007845#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007846 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7847 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007848#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007849#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7851 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007852#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7854 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007855#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007856#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007857#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007859#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007860#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007862#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007863#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007865#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007866#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007868#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007869#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007871#endif /* HAVE_GETEGID */
7872#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007874#endif /* HAVE_GETEUID */
7875#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007877#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007878#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007879 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007880#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007882#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007884#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007885#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007887#endif /* HAVE_GETPPID */
7888#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007890#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007891#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007893#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007894#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007896#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007897#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007899#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007900#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007902#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007903#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007904 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7905 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007906 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007907#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007908#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007910#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007911#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007913#endif /* HAVE_SETEUID */
7914#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007916#endif /* HAVE_SETEGID */
7917#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007919#endif /* HAVE_SETREUID */
7920#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007922#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007923#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007925#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007926#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007927 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007928#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007929#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007931#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007932#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007933 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007934#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007935#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007937#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007938#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007940#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007941#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007942 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007943#endif /* HAVE_WAIT3 */
7944#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007946#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007947#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007949#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007950#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007952#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007953#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007955#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007956#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007958#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007959#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007961#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007962#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007964#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7966 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7967 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7968 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7969 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7970 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7971 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7972 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7973 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7974 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7975 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007976#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007978#endif
7979#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007981#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007982#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007984#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007985#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007986 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7987 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7988 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007989#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007990#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007992#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007993#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007995#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007996#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007998#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008000#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00008002#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008003#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008005#endif
8006#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008008#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008009#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008010#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008011 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008012#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008013#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008015#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008016#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008018#endif /* WIFSTOPPED */
8019#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008021#endif /* WIFSIGNALED */
8022#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008024#endif /* WIFEXITED */
8025#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008027#endif /* WEXITSTATUS */
8028#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008030#endif /* WTERMSIG */
8031#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008033#endif /* WSTOPSIG */
8034#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008035#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008037#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008038#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008040#endif
Fred Drakec9680921999-12-13 16:37:25 +00008041#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
8050#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008052#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008054#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008055 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008056 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008057 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008058#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008059#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008061#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008062 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008064 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008065 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008067 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008068#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008070#endif
8071#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008073#endif
8074#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008075 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008076#endif
8077#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008079#endif
8080
Victor Stinner8c62be82010-05-06 00:08:46 +00008081 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008082};
8083
8084
Barry Warsaw4a342091996-12-19 23:50:02 +00008085static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008086ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008087{
Victor Stinner8c62be82010-05-06 00:08:46 +00008088 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008089}
8090
Guido van Rossumd48f2521997-12-05 22:19:34 +00008091#if defined(PYOS_OS2)
8092/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008093static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008094{
8095 APIRET rc;
8096 ULONG values[QSV_MAX+1];
8097 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008098 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008099
8100 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008101 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008102 Py_END_ALLOW_THREADS
8103
8104 if (rc != NO_ERROR) {
8105 os2_error(rc);
8106 return -1;
8107 }
8108
Fred Drake4d1e64b2002-04-15 19:40:07 +00008109 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8110 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8111 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8112 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8113 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8114 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8115 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008116
8117 switch (values[QSV_VERSION_MINOR]) {
8118 case 0: ver = "2.00"; break;
8119 case 10: ver = "2.10"; break;
8120 case 11: ver = "2.11"; break;
8121 case 30: ver = "3.00"; break;
8122 case 40: ver = "4.00"; break;
8123 case 50: ver = "5.00"; break;
8124 default:
Tim Peters885d4572001-11-28 20:27:42 +00008125 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008127 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008128 ver = &tmp[0];
8129 }
8130
8131 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008132 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008133 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008134
8135 /* Add Indicator of Which Drive was Used to Boot the System */
8136 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8137 tmp[1] = ':';
8138 tmp[2] = '\0';
8139
Fred Drake4d1e64b2002-04-15 19:40:07 +00008140 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008141}
8142#endif
8143
Brian Curtin52173d42010-12-02 18:29:18 +00008144#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008145static int
Brian Curtin52173d42010-12-02 18:29:18 +00008146enable_symlink()
8147{
8148 HANDLE tok;
8149 TOKEN_PRIVILEGES tok_priv;
8150 LUID luid;
8151 int meth_idx = 0;
8152
8153 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008154 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008155
8156 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008157 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008158
8159 tok_priv.PrivilegeCount = 1;
8160 tok_priv.Privileges[0].Luid = luid;
8161 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8162
8163 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8164 sizeof(TOKEN_PRIVILEGES),
8165 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008166 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008167
Brian Curtin3b4499c2010-12-28 14:31:47 +00008168 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8169 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008170}
8171#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8172
Barry Warsaw4a342091996-12-19 23:50:02 +00008173static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008174all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008175{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008176#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008178#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008179#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008181#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008182#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008184#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008185#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008186 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008187#endif
Fred Drakec9680921999-12-13 16:37:25 +00008188#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008189 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008190#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008191#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008192 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008193#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008194#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008195 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008196#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008197#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008198 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008199#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008200#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008201 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008202#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008203#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008204 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008205#endif
8206#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008207 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008208#endif
8209#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008210 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008211#endif
8212#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008213 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008214#endif
8215#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008216 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008217#endif
8218#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008219 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008220#endif
8221#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008222 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008223#endif
8224#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008225 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008226#endif
8227#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008228 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008229#endif
8230#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008231 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008232#endif
8233#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008234 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008235#endif
8236#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008237 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008238#endif
8239#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008240 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008241#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008242#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008244#endif
8245#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008247#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008248#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008250#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008251#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008253#endif
8254#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008255 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008256#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008257
Tim Peters5aa91602002-01-30 05:46:57 +00008258/* MS Windows */
8259#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 /* Don't inherit in child processes. */
8261 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008262#endif
8263#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008264 /* Optimize for short life (keep in memory). */
8265 /* MS forgot to define this one with a non-underscore form too. */
8266 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008267#endif
8268#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 /* Automatically delete when last handle is closed. */
8270 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008271#endif
8272#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008273 /* Optimize for random access. */
8274 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008275#endif
8276#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 /* Optimize for sequential access. */
8278 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008279#endif
8280
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008281/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008282#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 /* Send a SIGIO signal whenever input or output
8284 becomes available on file descriptor */
8285 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008286#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008287#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008288 /* Direct disk access. */
8289 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008290#endif
8291#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 /* Must be a directory. */
8293 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008294#endif
8295#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008296 /* Do not follow links. */
8297 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008298#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008299#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008300 /* Do not update the access time. */
8301 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008302#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008303
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008305#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008306 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008307#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008308#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008309 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008310#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008311#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008312 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008313#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008314#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008315 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008316#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008317#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008318 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008319#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008320#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008321 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008322#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008323#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008324 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008325#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008326#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008327 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008328#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008329#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008330 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008331#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008332#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008334#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008335#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008336 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008337#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008338#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008340#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008341#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008343#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008344#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008346#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008347#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008349#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008350#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008351 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008352#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008353#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008354 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008355#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008356
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008357 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008358#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008359 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008360#endif /* ST_RDONLY */
8361#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008362 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008363#endif /* ST_NOSUID */
8364
Guido van Rossum246bc171999-02-01 23:54:31 +00008365#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008366#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008367 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8368 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8369 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8370 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8371 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8372 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8373 if (ins(d, "P_PM", (long)P_PM)) return -1;
8374 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8375 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8376 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8377 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8378 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8379 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8380 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8381 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8382 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8383 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8384 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8385 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8386 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008387#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008388 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8389 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8390 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8391 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8392 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008393#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008394#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008395
Guido van Rossumd48f2521997-12-05 22:19:34 +00008396#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008397 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008398#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008399 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008400}
8401
8402
Tim Peters5aa91602002-01-30 05:46:57 +00008403#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008404#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008405#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008406
8407#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008408#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008409#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008410
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008411#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008412#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008413#define MODNAME "posix"
8414#endif
8415
Martin v. Löwis1a214512008-06-11 05:26:20 +00008416static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 PyModuleDef_HEAD_INIT,
8418 MODNAME,
8419 posix__doc__,
8420 -1,
8421 posix_methods,
8422 NULL,
8423 NULL,
8424 NULL,
8425 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008426};
8427
8428
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008429PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008430INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008431{
Victor Stinner8c62be82010-05-06 00:08:46 +00008432 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008433
Brian Curtin52173d42010-12-02 18:29:18 +00008434#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008435 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008436#endif
8437
Victor Stinner8c62be82010-05-06 00:08:46 +00008438 m = PyModule_Create(&posixmodule);
8439 if (m == NULL)
8440 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008441
Victor Stinner8c62be82010-05-06 00:08:46 +00008442 /* Initialize environ dictionary */
8443 v = convertenviron();
8444 Py_XINCREF(v);
8445 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8446 return NULL;
8447 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008448
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 if (all_ins(m))
8450 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008451
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 if (setup_confname_tables(m))
8453 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008454
Victor Stinner8c62be82010-05-06 00:08:46 +00008455 Py_INCREF(PyExc_OSError);
8456 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008457
Guido van Rossumb3d39562000-01-31 18:41:26 +00008458#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008459 if (posix_putenv_garbage == NULL)
8460 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008461#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008462
Victor Stinner8c62be82010-05-06 00:08:46 +00008463 if (!initialized) {
8464 stat_result_desc.name = MODNAME ".stat_result";
8465 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8466 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8467 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8468 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8469 structseq_new = StatResultType.tp_new;
8470 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008471
Victor Stinner8c62be82010-05-06 00:08:46 +00008472 statvfs_result_desc.name = MODNAME ".statvfs_result";
8473 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008474#ifdef NEED_TICKS_PER_SECOND
8475# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008476 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008477# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008479# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008480 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008481# endif
8482#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008483 }
8484 Py_INCREF((PyObject*) &StatResultType);
8485 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8486 Py_INCREF((PyObject*) &StatVFSResultType);
8487 PyModule_AddObject(m, "statvfs_result",
8488 (PyObject*) &StatVFSResultType);
8489 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008490
8491#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008492 /*
8493 * Step 2 of weak-linking support on Mac OS X.
8494 *
8495 * The code below removes functions that are not available on the
8496 * currently active platform.
8497 *
8498 * This block allow one to use a python binary that was build on
8499 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8500 * OSX 10.4.
8501 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008502#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 if (fstatvfs == NULL) {
8504 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8505 return NULL;
8506 }
8507 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008508#endif /* HAVE_FSTATVFS */
8509
8510#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008511 if (statvfs == NULL) {
8512 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8513 return NULL;
8514 }
8515 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008516#endif /* HAVE_STATVFS */
8517
8518# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008519 if (lchown == NULL) {
8520 if (PyObject_DelAttrString(m, "lchown") == -1) {
8521 return NULL;
8522 }
8523 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008524#endif /* HAVE_LCHOWN */
8525
8526
8527#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008528 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008529
Guido van Rossumb6775db1994-08-01 11:34:53 +00008530}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008531
8532#ifdef __cplusplus
8533}
8534#endif