blob: 0cd6340cd48f1ea8762b1c1bb2ab08470ffc496a [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
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000329#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000330# define STAT win32_stat
331# define FSTAT win32_fstat
332# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000333#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000334# define STAT stat
335# define FSTAT fstat
336# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000337#endif
338
Tim Peters11b23062003-04-23 02:39:17 +0000339#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000340#include <sys/mkdev.h>
341#else
342#if defined(MAJOR_IN_SYSMACROS)
343#include <sys/sysmacros.h>
344#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000345#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
346#include <sys/mkdev.h>
347#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000348#endif
Fred Drake699f3522000-06-29 21:12:41 +0000349
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000350#if defined _MSC_VER && _MSC_VER >= 1400
351/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
352 * valid and throw an assertion if it isn't.
353 * Normally, an invalid fd is likely to be a C program error and therefore
354 * an assertion can be useful, but it does contradict the POSIX standard
355 * which for write(2) states:
356 * "Otherwise, -1 shall be returned and errno set to indicate the error."
357 * "[EBADF] The fildes argument is not a valid file descriptor open for
358 * writing."
359 * Furthermore, python allows the user to enter any old integer
360 * as a fd and should merely raise a python exception on error.
361 * The Microsoft CRT doesn't provide an official way to check for the
362 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000363 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000364 * internal structures involved.
365 * The structures below must be updated for each version of visual studio
366 * according to the file internal.h in the CRT source, until MS comes
367 * up with a less hacky way to do this.
368 * (all of this is to avoid globally modifying the CRT behaviour using
369 * _set_invalid_parameter_handler() and _CrtSetReportMode())
370 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000371/* The actual size of the structure is determined at runtime.
372 * Only the first items must be present.
373 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000374typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000375 intptr_t osfhnd;
376 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000377} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000378
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000379extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380#define IOINFO_L2E 5
381#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
382#define IOINFO_ARRAYS 64
383#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
384#define FOPEN 0x01
385#define _NO_CONSOLE_FILENO (intptr_t)-2
386
387/* This function emulates what the windows CRT does to validate file handles */
388int
389_PyVerify_fd(int fd)
390{
Victor Stinner8c62be82010-05-06 00:08:46 +0000391 const int i1 = fd >> IOINFO_L2E;
392 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000393
Antoine Pitrou22e41552010-08-15 18:07:50 +0000394 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000395
Victor Stinner8c62be82010-05-06 00:08:46 +0000396 /* Determine the actual size of the ioinfo structure,
397 * as used by the CRT loaded in memory
398 */
399 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
400 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
401 }
402 if (sizeof_ioinfo == 0) {
403 /* This should not happen... */
404 goto fail;
405 }
406
407 /* See that it isn't a special CLEAR fileno */
408 if (fd != _NO_CONSOLE_FILENO) {
409 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
410 * we check pointer validity and other info
411 */
412 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
413 /* finally, check that the file is open */
414 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
415 if (info->osfile & FOPEN) {
416 return 1;
417 }
418 }
419 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000420 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000421 errno = EBADF;
422 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000423}
424
425/* the special case of checking dup2. The target fd must be in a sensible range */
426static int
427_PyVerify_fd_dup2(int fd1, int fd2)
428{
Victor Stinner8c62be82010-05-06 00:08:46 +0000429 if (!_PyVerify_fd(fd1))
430 return 0;
431 if (fd2 == _NO_CONSOLE_FILENO)
432 return 0;
433 if ((unsigned)fd2 < _NHANDLE_)
434 return 1;
435 else
436 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000437}
438#else
439/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
440#define _PyVerify_fd_dup2(A, B) (1)
441#endif
442
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000443#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +0000444/* The following structure was copied from
445 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
446 include doesn't seem to be present in the Windows SDK (at least as included
447 with Visual Studio Express). */
448typedef struct _REPARSE_DATA_BUFFER {
449 ULONG ReparseTag;
450 USHORT ReparseDataLength;
451 USHORT Reserved;
452 union {
453 struct {
454 USHORT SubstituteNameOffset;
455 USHORT SubstituteNameLength;
456 USHORT PrintNameOffset;
457 USHORT PrintNameLength;
458 ULONG Flags;
459 WCHAR PathBuffer[1];
460 } SymbolicLinkReparseBuffer;
461
462 struct {
463 USHORT SubstituteNameOffset;
464 USHORT SubstituteNameLength;
465 USHORT PrintNameOffset;
466 USHORT PrintNameLength;
467 WCHAR PathBuffer[1];
468 } MountPointReparseBuffer;
469
470 struct {
471 UCHAR DataBuffer[1];
472 } GenericReparseBuffer;
473 };
474} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
475
476#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
477 GenericReparseBuffer)
478#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
479
480static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000481win32_read_link(HANDLE reparse_point_handle, ULONG *reparse_tag, wchar_t **target_path)
Brian Curtinf5e76d02010-11-24 13:14:05 +0000482{
483 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
484 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
485 DWORD n_bytes_returned;
486 const wchar_t *ptr;
487 wchar_t *buf;
488 size_t len;
489
490 if (0 == DeviceIoControl(
491 reparse_point_handle,
492 FSCTL_GET_REPARSE_POINT,
493 NULL, 0, /* in buffer */
494 target_buffer, sizeof(target_buffer),
495 &n_bytes_returned,
496 NULL)) /* we're not using OVERLAPPED_IO */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000497 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000498
499 if (reparse_tag)
500 *reparse_tag = rdb->ReparseTag;
501
502 if (target_path) {
503 switch (rdb->ReparseTag) {
504 case IO_REPARSE_TAG_SYMLINK:
505 /* XXX: Maybe should use SubstituteName? */
506 ptr = rdb->SymbolicLinkReparseBuffer.PathBuffer +
507 rdb->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR);
508 len = rdb->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR);
509 break;
510 case IO_REPARSE_TAG_MOUNT_POINT:
511 ptr = rdb->MountPointReparseBuffer.PathBuffer +
512 rdb->MountPointReparseBuffer.SubstituteNameOffset/sizeof(WCHAR);
513 len = rdb->MountPointReparseBuffer.SubstituteNameLength/sizeof(WCHAR);
514 break;
515 default:
516 SetLastError(ERROR_REPARSE_TAG_MISMATCH); /* XXX: Proper error code? */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000517 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000518 }
519 buf = (wchar_t *)malloc(sizeof(wchar_t)*(len+1));
520 if (!buf) {
521 SetLastError(ERROR_OUTOFMEMORY);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000522 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000523 }
524 wcsncpy(buf, ptr, len);
525 buf[len] = L'\0';
526 if (wcsncmp(buf, L"\\??\\", 4) == 0)
527 buf[1] = L'\\';
528 *target_path = buf;
529 }
530
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +0000531 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +0000532}
Brian Curtinfc1be6d2010-11-24 13:23:18 +0000533#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +0000534
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000536#ifdef WITH_NEXT_FRAMEWORK
537/* On Darwin/MacOSX a shared library or framework has no access to
538** environ directly, we must obtain it with _NSGetEnviron().
539*/
540#include <crt_externs.h>
541static char **environ;
542#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000544#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545
Barry Warsaw53699e91996-12-10 23:23:01 +0000546static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000547convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548{
Victor Stinner8c62be82010-05-06 00:08:46 +0000549 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000550#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000551 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000552#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000553 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000554#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000555#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 APIRET rc;
557 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
558#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000559
Victor Stinner8c62be82010-05-06 00:08:46 +0000560 d = PyDict_New();
561 if (d == NULL)
562 return NULL;
563#ifdef WITH_NEXT_FRAMEWORK
564 if (environ == NULL)
565 environ = *_NSGetEnviron();
566#endif
567#ifdef MS_WINDOWS
568 /* _wenviron must be initialized in this way if the program is started
569 through main() instead of wmain(). */
570 _wgetenv(L"");
571 if (_wenviron == NULL)
572 return d;
573 /* This part ignores errors */
574 for (e = _wenviron; *e != NULL; e++) {
575 PyObject *k;
576 PyObject *v;
577 wchar_t *p = wcschr(*e, L'=');
578 if (p == NULL)
579 continue;
580 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
581 if (k == NULL) {
582 PyErr_Clear();
583 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000584 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000585 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
586 if (v == NULL) {
587 PyErr_Clear();
588 Py_DECREF(k);
589 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000590 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000591 if (PyDict_GetItem(d, k) == NULL) {
592 if (PyDict_SetItem(d, k, v) != 0)
593 PyErr_Clear();
594 }
595 Py_DECREF(k);
596 Py_DECREF(v);
597 }
598#else
599 if (environ == NULL)
600 return d;
601 /* This part ignores errors */
602 for (e = environ; *e != NULL; e++) {
603 PyObject *k;
604 PyObject *v;
605 char *p = strchr(*e, '=');
606 if (p == NULL)
607 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000608 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000609 if (k == NULL) {
610 PyErr_Clear();
611 continue;
612 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000613 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000614 if (v == NULL) {
615 PyErr_Clear();
616 Py_DECREF(k);
617 continue;
618 }
619 if (PyDict_GetItem(d, k) == NULL) {
620 if (PyDict_SetItem(d, k, v) != 0)
621 PyErr_Clear();
622 }
623 Py_DECREF(k);
624 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000625 }
626#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000627#if defined(PYOS_OS2)
628 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
629 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
630 PyObject *v = PyBytes_FromString(buffer);
631 PyDict_SetItemString(d, "BEGINLIBPATH", v);
632 Py_DECREF(v);
633 }
634 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
635 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
636 PyObject *v = PyBytes_FromString(buffer);
637 PyDict_SetItemString(d, "ENDLIBPATH", v);
638 Py_DECREF(v);
639 }
640#endif
641 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644/* Set a POSIX-specific error from errno, and return NULL */
645
Barry Warsawd58d7641998-07-23 16:14:40 +0000646static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000647posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000648{
Victor Stinner8c62be82010-05-06 00:08:46 +0000649 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000650}
Barry Warsawd58d7641998-07-23 16:14:40 +0000651static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000652posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000653{
Victor Stinner8c62be82010-05-06 00:08:46 +0000654 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000655}
656
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000657
Mark Hammondef8b6542001-05-13 08:04:26 +0000658static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000659posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000660{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000661 PyObject *name_str, *rc;
662 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
663 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000664 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000665 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
666 name_str);
667 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000669}
670
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000671#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000672static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000673win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000674{
Victor Stinner8c62be82010-05-06 00:08:46 +0000675 /* XXX We should pass the function name along in the future.
676 (winreg.c also wants to pass the function name.)
677 This would however require an additional param to the
678 Windows error object, which is non-trivial.
679 */
680 errno = GetLastError();
681 if (filename)
682 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
683 else
684 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000685}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000686
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000687static PyObject *
688win32_error_unicode(char* function, Py_UNICODE* filename)
689{
Victor Stinner8c62be82010-05-06 00:08:46 +0000690 /* XXX - see win32_error for comments on 'function' */
691 errno = GetLastError();
692 if (filename)
693 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
694 else
695 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000696}
697
Thomas Wouters477c8d52006-05-27 19:21:47 +0000698static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000699convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700{
Victor Stinner8c62be82010-05-06 00:08:46 +0000701 if (PyUnicode_CheckExact(*param))
702 Py_INCREF(*param);
703 else if (PyUnicode_Check(*param))
704 /* For a Unicode subtype that's not a Unicode object,
705 return a true Unicode object with the same data. */
706 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
707 PyUnicode_GET_SIZE(*param));
708 else
709 *param = PyUnicode_FromEncodedObject(*param,
710 Py_FileSystemDefaultEncoding,
711 "strict");
712 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000713}
714
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000715#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716
Guido van Rossumd48f2521997-12-05 22:19:34 +0000717#if defined(PYOS_OS2)
718/**********************************************************************
719 * Helper Function to Trim and Format OS/2 Messages
720 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000721static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000722os2_formatmsg(char *msgbuf, int msglen, char *reason)
723{
724 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
725
726 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
727 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
728
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000729 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000730 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
731 }
732
733 /* Add Optional Reason Text */
734 if (reason) {
735 strcat(msgbuf, " : ");
736 strcat(msgbuf, reason);
737 }
738}
739
740/**********************************************************************
741 * Decode an OS/2 Operating System Error Code
742 *
743 * A convenience function to lookup an OS/2 error code and return a
744 * text message we can use to raise a Python exception.
745 *
746 * Notes:
747 * The messages for errors returned from the OS/2 kernel reside in
748 * the file OSO001.MSG in the \OS2 directory hierarchy.
749 *
750 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000751static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000752os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
753{
754 APIRET rc;
755 ULONG msglen;
756
757 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
758 Py_BEGIN_ALLOW_THREADS
759 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
760 errorcode, "oso001.msg", &msglen);
761 Py_END_ALLOW_THREADS
762
763 if (rc == NO_ERROR)
764 os2_formatmsg(msgbuf, msglen, reason);
765 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000766 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000767 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000768
769 return msgbuf;
770}
771
772/* Set an OS/2-specific error and return NULL. OS/2 kernel
773 errors are not in a global variable e.g. 'errno' nor are
774 they congruent with posix error numbers. */
775
Victor Stinner8c62be82010-05-06 00:08:46 +0000776static PyObject *
777os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000778{
779 char text[1024];
780 PyObject *v;
781
782 os2_strerror(text, sizeof(text), code, "");
783
784 v = Py_BuildValue("(is)", code, text);
785 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000786 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000787 Py_DECREF(v);
788 }
789 return NULL; /* Signal to Python that an Exception is Pending */
790}
791
792#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000793
794/* POSIX generic methods */
795
Barry Warsaw53699e91996-12-10 23:23:01 +0000796static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000797posix_fildes(PyObject *fdobj, int (*func)(int))
798{
Victor Stinner8c62be82010-05-06 00:08:46 +0000799 int fd;
800 int res;
801 fd = PyObject_AsFileDescriptor(fdobj);
802 if (fd < 0)
803 return NULL;
804 if (!_PyVerify_fd(fd))
805 return posix_error();
806 Py_BEGIN_ALLOW_THREADS
807 res = (*func)(fd);
808 Py_END_ALLOW_THREADS
809 if (res < 0)
810 return posix_error();
811 Py_INCREF(Py_None);
812 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000813}
Guido van Rossum21142a01999-01-08 21:05:37 +0000814
815static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817{
Victor Stinner8c62be82010-05-06 00:08:46 +0000818 PyObject *opath1 = NULL;
819 char *path1;
820 int res;
821 if (!PyArg_ParseTuple(args, format,
822 PyUnicode_FSConverter, &opath1))
823 return NULL;
824 path1 = PyBytes_AsString(opath1);
825 Py_BEGIN_ALLOW_THREADS
826 res = (*func)(path1);
827 Py_END_ALLOW_THREADS
828 if (res < 0)
829 return posix_error_with_allocated_filename(opath1);
830 Py_DECREF(opath1);
831 Py_INCREF(Py_None);
832 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833}
834
Barry Warsaw53699e91996-12-10 23:23:01 +0000835static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000836posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000837 char *format,
838 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839{
Victor Stinner8c62be82010-05-06 00:08:46 +0000840 PyObject *opath1 = NULL, *opath2 = NULL;
841 char *path1, *path2;
842 int res;
843 if (!PyArg_ParseTuple(args, format,
844 PyUnicode_FSConverter, &opath1,
845 PyUnicode_FSConverter, &opath2)) {
846 return NULL;
847 }
848 path1 = PyBytes_AsString(opath1);
849 path2 = PyBytes_AsString(opath2);
850 Py_BEGIN_ALLOW_THREADS
851 res = (*func)(path1, path2);
852 Py_END_ALLOW_THREADS
853 Py_DECREF(opath1);
854 Py_DECREF(opath2);
855 if (res != 0)
856 /* XXX how to report both path1 and path2??? */
857 return posix_error();
858 Py_INCREF(Py_None);
859 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860}
861
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000862#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000863static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000864win32_1str(PyObject* args, char* func,
865 char* format, BOOL (__stdcall *funcA)(LPCSTR),
866 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867{
Victor Stinner8c62be82010-05-06 00:08:46 +0000868 PyObject *uni;
869 char *ansi;
870 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000871
Victor Stinner8c62be82010-05-06 00:08:46 +0000872 if (!PyArg_ParseTuple(args, wformat, &uni))
873 PyErr_Clear();
874 else {
875 Py_BEGIN_ALLOW_THREADS
876 result = funcW(PyUnicode_AsUnicode(uni));
877 Py_END_ALLOW_THREADS
878 if (!result)
879 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
880 Py_INCREF(Py_None);
881 return Py_None;
882 }
883 if (!PyArg_ParseTuple(args, format, &ansi))
884 return NULL;
885 Py_BEGIN_ALLOW_THREADS
886 result = funcA(ansi);
887 Py_END_ALLOW_THREADS
888 if (!result)
889 return win32_error(func, ansi);
890 Py_INCREF(Py_None);
891 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892
893}
894
895/* This is a reimplementation of the C library's chdir function,
896 but one that produces Win32 errors instead of DOS error codes.
897 chdir is essentially a wrapper around SetCurrentDirectory; however,
898 it also needs to set "magic" environment variables indicating
899 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000900static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901win32_chdir(LPCSTR path)
902{
Victor Stinner8c62be82010-05-06 00:08:46 +0000903 char new_path[MAX_PATH+1];
904 int result;
905 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906
Victor Stinner8c62be82010-05-06 00:08:46 +0000907 if(!SetCurrentDirectoryA(path))
908 return FALSE;
909 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
910 if (!result)
911 return FALSE;
912 /* In the ANSI API, there should not be any paths longer
913 than MAX_PATH. */
914 assert(result <= MAX_PATH+1);
915 if (strncmp(new_path, "\\\\", 2) == 0 ||
916 strncmp(new_path, "//", 2) == 0)
917 /* UNC path, nothing to do. */
918 return TRUE;
919 env[1] = new_path[0];
920 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921}
922
923/* The Unicode version differs from the ANSI version
924 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000925static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000926win32_wchdir(LPCWSTR path)
927{
Victor Stinner8c62be82010-05-06 00:08:46 +0000928 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
929 int result;
930 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000931
Victor Stinner8c62be82010-05-06 00:08:46 +0000932 if(!SetCurrentDirectoryW(path))
933 return FALSE;
934 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
935 if (!result)
936 return FALSE;
937 if (result > MAX_PATH+1) {
938 new_path = malloc(result * sizeof(wchar_t));
939 if (!new_path) {
940 SetLastError(ERROR_OUTOFMEMORY);
941 return FALSE;
942 }
943 result = GetCurrentDirectoryW(result, new_path);
944 if (!result) {
945 free(new_path);
946 return FALSE;
947 }
948 }
949 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
950 wcsncmp(new_path, L"//", 2) == 0)
951 /* UNC path, nothing to do. */
952 return TRUE;
953 env[1] = new_path[0];
954 result = SetEnvironmentVariableW(env, new_path);
955 if (new_path != _new_path)
956 free(new_path);
957 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958}
959#endif
960
Martin v. Löwis14694662006-02-03 12:54:16 +0000961#ifdef MS_WINDOWS
962/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
963 - time stamps are restricted to second resolution
964 - file modification times suffer from forth-and-back conversions between
965 UTC and local time
966 Therefore, we implement our own stat, based on the Win32 API directly.
967*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000968#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000969
970struct win32_stat{
971 int st_dev;
972 __int64 st_ino;
973 unsigned short st_mode;
974 int st_nlink;
975 int st_uid;
976 int st_gid;
977 int st_rdev;
978 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000979 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000980 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000981 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000982 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000983 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +0000984 int st_ctime_nsec;
985};
986
987static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
988
989static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000990FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +0000991{
Victor Stinner8c62be82010-05-06 00:08:46 +0000992 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
993 /* Cannot simply cast and dereference in_ptr,
994 since it might not be aligned properly */
995 __int64 in;
996 memcpy(&in, in_ptr, sizeof(in));
997 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +0000998 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +0000999}
1000
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001002time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001003{
Victor Stinner8c62be82010-05-06 00:08:46 +00001004 /* XXX endianness */
1005 __int64 out;
1006 out = time_in + secs_between_epochs;
1007 out = out * 10000000 + nsec_in / 100;
1008 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001009}
1010
Martin v. Löwis14694662006-02-03 12:54:16 +00001011/* Below, we *know* that ugo+r is 0444 */
1012#if _S_IREAD != 0400
1013#error Unsupported C library
1014#endif
1015static int
1016attributes_to_mode(DWORD attr)
1017{
Victor Stinner8c62be82010-05-06 00:08:46 +00001018 int m = 0;
1019 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1020 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1021 else
1022 m |= _S_IFREG;
1023 if (attr & FILE_ATTRIBUTE_READONLY)
1024 m |= 0444;
1025 else
1026 m |= 0666;
1027 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001028}
1029
1030static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001031attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001032{
Victor Stinner8c62be82010-05-06 00:08:46 +00001033 memset(result, 0, sizeof(*result));
1034 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1035 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
1036 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1037 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1038 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001039 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001040 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001041 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1042 /* first clear the S_IFMT bits */
1043 result->st_mode ^= (result->st_mode & 0170000);
1044 /* now set the bits that make this a symlink */
1045 result->st_mode |= 0120000;
1046 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001047
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001049}
1050
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001052attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001053{
Victor Stinner8c62be82010-05-06 00:08:46 +00001054 HANDLE hFindFile;
1055 WIN32_FIND_DATAA FileData;
1056 hFindFile = FindFirstFileA(pszFile, &FileData);
1057 if (hFindFile == INVALID_HANDLE_VALUE)
1058 return FALSE;
1059 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001060 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001061 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001062 info->dwFileAttributes = FileData.dwFileAttributes;
1063 info->ftCreationTime = FileData.ftCreationTime;
1064 info->ftLastAccessTime = FileData.ftLastAccessTime;
1065 info->ftLastWriteTime = FileData.ftLastWriteTime;
1066 info->nFileSizeHigh = FileData.nFileSizeHigh;
1067 info->nFileSizeLow = FileData.nFileSizeLow;
1068/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001069 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1070 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001071 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001072}
1073
1074static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001075attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001076{
Victor Stinner8c62be82010-05-06 00:08:46 +00001077 HANDLE hFindFile;
1078 WIN32_FIND_DATAW FileData;
1079 hFindFile = FindFirstFileW(pszFile, &FileData);
1080 if (hFindFile == INVALID_HANDLE_VALUE)
1081 return FALSE;
1082 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001083 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001084 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001085 info->dwFileAttributes = FileData.dwFileAttributes;
1086 info->ftCreationTime = FileData.ftCreationTime;
1087 info->ftLastAccessTime = FileData.ftLastAccessTime;
1088 info->ftLastWriteTime = FileData.ftLastWriteTime;
1089 info->nFileSizeHigh = FileData.nFileSizeHigh;
1090 info->nFileSizeLow = FileData.nFileSizeLow;
1091/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001092 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1093 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001094 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001095}
1096
Brian Curtinf5e76d02010-11-24 13:14:05 +00001097#ifndef SYMLOOP_MAX
1098#define SYMLOOP_MAX ( 88 )
1099#endif
1100
1101static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001102win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001103
1104static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001105win32_xstat_impl(const char *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001106{
1107 int code;
1108 HANDLE hFile;
1109 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001110 ULONG reparse_tag = 0;
Hirokazu Yamamoto74673512010-12-05 02:48:08 +00001111 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001112 const char *dot;
1113
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001114 if (depth > SYMLOOP_MAX) {
1115 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1116 return -1;
1117 }
1118
Brian Curtinf5e76d02010-11-24 13:14:05 +00001119 hFile = CreateFileA(
1120 path,
1121 0, /* desired access */
1122 0, /* share mode */
1123 NULL, /* security attributes */
1124 OPEN_EXISTING,
1125 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1126 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1127 NULL);
1128
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001129 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001130 /* Either the target doesn't exist, or we don't have access to
1131 get a handle to it. If the former, we need to return an error.
1132 If the latter, we can use attributes_from_dir. */
1133 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001134 return -1;
1135 /* Could not get attributes on open file. Fall back to
1136 reading the directory. */
1137 if (!attributes_from_dir(path, &info, &reparse_tag))
1138 /* Very strange. This should not fail now */
1139 return -1;
1140 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1141 if (traverse) {
1142 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001143 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001144 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001145 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001146 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001147 } else {
1148 if (!GetFileInformationByHandle(hFile, &info)) {
1149 CloseHandle(hFile);
1150 return -1;;
1151 }
1152 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1153 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1154 CloseHandle(hFile);
1155 if (code < 0)
1156 return code;
1157 if (traverse) {
1158 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1159 free(target_path);
1160 return code;
1161 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001162 } else
1163 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001164 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001165 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001166
1167 /* Set S_IEXEC if it is an .exe, .bat, ... */
1168 dot = strrchr(path, '.');
1169 if (dot) {
1170 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1171 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1172 result->st_mode |= 0111;
1173 }
1174 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001175}
1176
1177static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001178win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result, BOOL traverse, int depth)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001179{
1180 int code;
1181 HANDLE hFile;
1182 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001183 ULONG reparse_tag = 0;
1184 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001185 const wchar_t *dot;
1186
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001187 if (depth > SYMLOOP_MAX) {
1188 SetLastError(ERROR_CANT_RESOLVE_FILENAME); /* XXX: ELOOP? */
1189 return -1;
1190 }
1191
Brian Curtinf5e76d02010-11-24 13:14:05 +00001192 hFile = CreateFileW(
1193 path,
1194 0, /* desired access */
1195 0, /* share mode */
1196 NULL, /* security attributes */
1197 OPEN_EXISTING,
1198 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1199 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OPEN_REPARSE_POINT,
1200 NULL);
1201
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001202 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001203 /* Either the target doesn't exist, or we don't have access to
1204 get a handle to it. If the former, we need to return an error.
1205 If the latter, we can use attributes_from_dir. */
1206 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001207 return -1;
1208 /* Could not get attributes on open file. Fall back to
1209 reading the directory. */
1210 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1211 /* Very strange. This should not fail now */
1212 return -1;
1213 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1214 if (traverse) {
1215 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001216 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001217 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001218 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001219 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001220 } else {
1221 if (!GetFileInformationByHandle(hFile, &info)) {
1222 CloseHandle(hFile);
1223 return -1;;
1224 }
1225 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1226 code = win32_read_link(hFile, &reparse_tag, traverse ? &target_path : NULL);
1227 CloseHandle(hFile);
1228 if (code < 0)
1229 return code;
1230 if (traverse) {
1231 code = win32_xstat_impl_w(target_path, result, traverse, depth + 1);
1232 free(target_path);
1233 return code;
1234 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001235 } else
1236 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001237 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001238 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001239
1240 /* Set S_IEXEC if it is an .exe, .bat, ... */
1241 dot = wcsrchr(path, '.');
1242 if (dot) {
1243 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1244 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1245 result->st_mode |= 0111;
1246 }
1247 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001248}
1249
1250static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001251win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001252{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001253 /* Protocol violation: we explicitly clear errno, instead of
1254 setting it to a POSIX error. Callers should use GetLastError. */
1255 int code = win32_xstat_impl(path, result, traverse, 0);
1256 errno = 0;
1257 return code;
1258}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001259
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001260static int
1261win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1262{
1263 /* Protocol violation: we explicitly clear errno, instead of
1264 setting it to a POSIX error. Callers should use GetLastError. */
1265 int code = win32_xstat_impl_w(path, result, traverse, 0);
1266 errno = 0;
1267 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001268}
1269
Brian Curtind40e6f72010-07-08 21:39:08 +00001270/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
1271 win32_stat_w
1272
1273 In Posix, stat automatically traverses symlinks and returns the stat
1274 structure for the target. In Windows, the equivalent GetFileAttributes by
1275 default does not traverse symlinks and instead returns attributes for
1276 the symlink.
1277
1278 Therefore, win32_lstat will get the attributes traditionally, and
1279 win32_stat will first explicitly resolve the symlink target and then will
1280 call win32_lstat on that result.
1281
1282 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1283
1284static int
1285win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001286{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001287 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001288}
1289
Victor Stinner8c62be82010-05-06 00:08:46 +00001290static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001291win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001292{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001293 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001294}
1295
1296static int
1297win32_stat(const char* path, struct win32_stat *result)
1298{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001299 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001300}
1301
1302static int
1303win32_stat_w(const wchar_t* path, struct win32_stat *result)
1304{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001305 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001306}
1307
1308static int
1309win32_fstat(int file_number, struct win32_stat *result)
1310{
Victor Stinner8c62be82010-05-06 00:08:46 +00001311 BY_HANDLE_FILE_INFORMATION info;
1312 HANDLE h;
1313 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001314
Victor Stinner8c62be82010-05-06 00:08:46 +00001315 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001316
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 /* Protocol violation: we explicitly clear errno, instead of
1318 setting it to a POSIX error. Callers should use GetLastError. */
1319 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001320
Victor Stinner8c62be82010-05-06 00:08:46 +00001321 if (h == INVALID_HANDLE_VALUE) {
1322 /* This is really a C library error (invalid file handle).
1323 We set the Win32 error to the closes one matching. */
1324 SetLastError(ERROR_INVALID_HANDLE);
1325 return -1;
1326 }
1327 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001328
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 type = GetFileType(h);
1330 if (type == FILE_TYPE_UNKNOWN) {
1331 DWORD error = GetLastError();
1332 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001333 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001334 }
1335 /* else: valid but unknown file */
1336 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001337
Victor Stinner8c62be82010-05-06 00:08:46 +00001338 if (type != FILE_TYPE_DISK) {
1339 if (type == FILE_TYPE_CHAR)
1340 result->st_mode = _S_IFCHR;
1341 else if (type == FILE_TYPE_PIPE)
1342 result->st_mode = _S_IFIFO;
1343 return 0;
1344 }
1345
1346 if (!GetFileInformationByHandle(h, &info)) {
1347 return -1;
1348 }
1349
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001350 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001351 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1353 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001354}
1355
1356#endif /* MS_WINDOWS */
1357
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001358PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359"stat_result: Result from stat or lstat.\n\n\
1360This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001361 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001362or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1363\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001364Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1365or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001366\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001367See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001368
1369static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 {"st_mode", "protection bits"},
1371 {"st_ino", "inode"},
1372 {"st_dev", "device"},
1373 {"st_nlink", "number of hard links"},
1374 {"st_uid", "user ID of owner"},
1375 {"st_gid", "group ID of owner"},
1376 {"st_size", "total size, in bytes"},
1377 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1378 {NULL, "integer time of last access"},
1379 {NULL, "integer time of last modification"},
1380 {NULL, "integer time of last change"},
1381 {"st_atime", "time of last access"},
1382 {"st_mtime", "time of last modification"},
1383 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001384#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001385 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001386#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001387#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001388 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001389#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001390#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001391 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001392#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001393#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001394 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001395#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001396#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001397 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001398#endif
1399#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001400 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001401#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001402 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001403};
1404
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001405#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001406#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001407#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001408#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001409#endif
1410
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001411#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001412#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1413#else
1414#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1415#endif
1416
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001417#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001418#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1419#else
1420#define ST_RDEV_IDX ST_BLOCKS_IDX
1421#endif
1422
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001423#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1424#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1425#else
1426#define ST_FLAGS_IDX ST_RDEV_IDX
1427#endif
1428
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001429#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001430#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001431#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001432#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001433#endif
1434
1435#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1436#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1437#else
1438#define ST_BIRTHTIME_IDX ST_GEN_IDX
1439#endif
1440
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001441static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001442 "stat_result", /* name */
1443 stat_result__doc__, /* doc */
1444 stat_result_fields,
1445 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001446};
1447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001448PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001449"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1450This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001451 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001452or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001453\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001454See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001455
1456static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001457 {"f_bsize", },
1458 {"f_frsize", },
1459 {"f_blocks", },
1460 {"f_bfree", },
1461 {"f_bavail", },
1462 {"f_files", },
1463 {"f_ffree", },
1464 {"f_favail", },
1465 {"f_flag", },
1466 {"f_namemax",},
1467 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001468};
1469
1470static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001471 "statvfs_result", /* name */
1472 statvfs_result__doc__, /* doc */
1473 statvfs_result_fields,
1474 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001475};
1476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001478static PyTypeObject StatResultType;
1479static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001480static newfunc structseq_new;
1481
1482static PyObject *
1483statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1484{
Victor Stinner8c62be82010-05-06 00:08:46 +00001485 PyStructSequence *result;
1486 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001487
Victor Stinner8c62be82010-05-06 00:08:46 +00001488 result = (PyStructSequence*)structseq_new(type, args, kwds);
1489 if (!result)
1490 return NULL;
1491 /* If we have been initialized from a tuple,
1492 st_?time might be set to None. Initialize it
1493 from the int slots. */
1494 for (i = 7; i <= 9; i++) {
1495 if (result->ob_item[i+3] == Py_None) {
1496 Py_DECREF(Py_None);
1497 Py_INCREF(result->ob_item[i]);
1498 result->ob_item[i+3] = result->ob_item[i];
1499 }
1500 }
1501 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001502}
1503
1504
1505
1506/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001507static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001508
1509PyDoc_STRVAR(stat_float_times__doc__,
1510"stat_float_times([newval]) -> oldval\n\n\
1511Determine whether os.[lf]stat represents time stamps as float objects.\n\
1512If newval is True, future calls to stat() return floats, if it is False,\n\
1513future calls return ints. \n\
1514If newval is omitted, return the current setting.\n");
1515
1516static PyObject*
1517stat_float_times(PyObject* self, PyObject *args)
1518{
Victor Stinner8c62be82010-05-06 00:08:46 +00001519 int newval = -1;
1520 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1521 return NULL;
1522 if (newval == -1)
1523 /* Return old value */
1524 return PyBool_FromLong(_stat_float_times);
1525 _stat_float_times = newval;
1526 Py_INCREF(Py_None);
1527 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001528}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001529
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001530static void
1531fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1532{
Victor Stinner8c62be82010-05-06 00:08:46 +00001533 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001534#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001536#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001537 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001538#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001539 if (!ival)
1540 return;
1541 if (_stat_float_times) {
1542 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1543 } else {
1544 fval = ival;
1545 Py_INCREF(fval);
1546 }
1547 PyStructSequence_SET_ITEM(v, index, ival);
1548 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001549}
1550
Tim Peters5aa91602002-01-30 05:46:57 +00001551/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001552 (used by posix_stat() and posix_fstat()) */
1553static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001554_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001555{
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 unsigned long ansec, mnsec, cnsec;
1557 PyObject *v = PyStructSequence_New(&StatResultType);
1558 if (v == NULL)
1559 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001560
Victor Stinner8c62be82010-05-06 00:08:46 +00001561 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001562#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 PyStructSequence_SET_ITEM(v, 1,
1564 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001565#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001566 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001567#endif
1568#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001569 PyStructSequence_SET_ITEM(v, 2,
1570 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001571#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001573#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1575 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1576 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001577#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 PyStructSequence_SET_ITEM(v, 6,
1579 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001580#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001581 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001582#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001583
Martin v. Löwis14694662006-02-03 12:54:16 +00001584#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001585 ansec = st->st_atim.tv_nsec;
1586 mnsec = st->st_mtim.tv_nsec;
1587 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001588#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001589 ansec = st->st_atimespec.tv_nsec;
1590 mnsec = st->st_mtimespec.tv_nsec;
1591 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001592#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001593 ansec = st->st_atime_nsec;
1594 mnsec = st->st_mtime_nsec;
1595 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001596#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001597 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001598#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 fill_time(v, 7, st->st_atime, ansec);
1600 fill_time(v, 8, st->st_mtime, mnsec);
1601 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001602
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001603#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1605 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001606#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001607#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1609 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001610#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001611#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001612 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1613 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001614#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001615#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001616 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1617 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001618#endif
1619#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001620 {
1621 PyObject *val;
1622 unsigned long bsec,bnsec;
1623 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001624#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001625 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001626#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001628#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001629 if (_stat_float_times) {
1630 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1631 } else {
1632 val = PyLong_FromLong((long)bsec);
1633 }
1634 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1635 val);
1636 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001637#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001638#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001639 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1640 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001641#endif
Fred Drake699f3522000-06-29 21:12:41 +00001642
Victor Stinner8c62be82010-05-06 00:08:46 +00001643 if (PyErr_Occurred()) {
1644 Py_DECREF(v);
1645 return NULL;
1646 }
Fred Drake699f3522000-06-29 21:12:41 +00001647
Victor Stinner8c62be82010-05-06 00:08:46 +00001648 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001649}
1650
Barry Warsaw53699e91996-12-10 23:23:01 +00001651static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001652posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001653 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001654#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001656#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001657 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001658#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001659 char *wformat,
1660 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001661{
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 STRUCT_STAT st;
1663 PyObject *opath;
1664 char *path;
1665 int res;
1666 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001667
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001668#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001669 PyUnicodeObject *po;
1670 if (PyArg_ParseTuple(args, wformat, &po)) {
1671 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001672
Victor Stinner8c62be82010-05-06 00:08:46 +00001673 Py_BEGIN_ALLOW_THREADS
1674 /* PyUnicode_AS_UNICODE result OK without
1675 thread lock as it is a simple dereference. */
1676 res = wstatfunc(wpath, &st);
1677 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001678
Victor Stinner8c62be82010-05-06 00:08:46 +00001679 if (res != 0)
1680 return win32_error_unicode("stat", wpath);
1681 return _pystat_fromstructstat(&st);
1682 }
1683 /* Drop the argument parsing error as narrow strings
1684 are also valid. */
1685 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001686#endif
1687
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 if (!PyArg_ParseTuple(args, format,
1689 PyUnicode_FSConverter, &opath))
1690 return NULL;
1691 path = PyBytes_AsString(opath);
1692 Py_BEGIN_ALLOW_THREADS
1693 res = (*statfunc)(path, &st);
1694 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001695
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001697#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001699#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001700 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001701#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001702 }
1703 else
1704 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001705
Victor Stinner8c62be82010-05-06 00:08:46 +00001706 Py_DECREF(opath);
1707 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001708}
1709
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710/* POSIX methods */
1711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001712PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001713"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001714Use the real uid/gid to test for access to a path. Note that most\n\
1715operations will use the effective uid/gid, therefore this routine can\n\
1716be used in a suid/sgid environment to test if the invoking user has the\n\
1717specified access to the path. The mode argument can be F_OK to test\n\
1718existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001719
1720static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001721posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001722{
Victor Stinner8c62be82010-05-06 00:08:46 +00001723 PyObject *opath;
1724 char *path;
1725 int mode;
1726
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001727#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 DWORD attr;
1729 PyUnicodeObject *po;
1730 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1731 Py_BEGIN_ALLOW_THREADS
1732 /* PyUnicode_AS_UNICODE OK without thread lock as
1733 it is a simple dereference. */
1734 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1735 Py_END_ALLOW_THREADS
1736 goto finish;
1737 }
1738 /* Drop the argument parsing error as narrow strings
1739 are also valid. */
1740 PyErr_Clear();
1741 if (!PyArg_ParseTuple(args, "O&i:access",
1742 PyUnicode_FSConverter, &opath, &mode))
1743 return NULL;
1744 path = PyBytes_AsString(opath);
1745 Py_BEGIN_ALLOW_THREADS
1746 attr = GetFileAttributesA(path);
1747 Py_END_ALLOW_THREADS
1748 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001749finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001750 if (attr == 0xFFFFFFFF)
1751 /* File does not exist, or cannot read attributes */
1752 return PyBool_FromLong(0);
1753 /* Access is possible if either write access wasn't requested, or
1754 the file isn't read-only, or if it's a directory, as there are
1755 no read-only directories on Windows. */
1756 return PyBool_FromLong(!(mode & 2)
1757 || !(attr & FILE_ATTRIBUTE_READONLY)
1758 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001759#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001760 int res;
1761 if (!PyArg_ParseTuple(args, "O&i:access",
1762 PyUnicode_FSConverter, &opath, &mode))
1763 return NULL;
1764 path = PyBytes_AsString(opath);
1765 Py_BEGIN_ALLOW_THREADS
1766 res = access(path, mode);
1767 Py_END_ALLOW_THREADS
1768 Py_DECREF(opath);
1769 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001770#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001771}
1772
Guido van Rossumd371ff11999-01-25 16:12:23 +00001773#ifndef F_OK
1774#define F_OK 0
1775#endif
1776#ifndef R_OK
1777#define R_OK 4
1778#endif
1779#ifndef W_OK
1780#define W_OK 2
1781#endif
1782#ifndef X_OK
1783#define X_OK 1
1784#endif
1785
1786#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001787PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001788"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001790
1791static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001792posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001793{
Victor Stinner8c62be82010-05-06 00:08:46 +00001794 int id;
1795 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001796
Victor Stinner8c62be82010-05-06 00:08:46 +00001797 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1798 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001799
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001800#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001801 /* file descriptor 0 only, the default input device (stdin) */
1802 if (id == 0) {
1803 ret = ttyname();
1804 }
1805 else {
1806 ret = NULL;
1807 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001808#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001810#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001811 if (ret == NULL)
1812 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001813 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001814}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001815#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001816
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001817#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001818PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001819"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001820Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001821
1822static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001823posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001824{
Victor Stinner8c62be82010-05-06 00:08:46 +00001825 char *ret;
1826 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001827
Greg Wardb48bc172000-03-01 21:51:56 +00001828#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001829 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001830#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001832#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 if (ret == NULL)
1834 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001835 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001836}
1837#endif
1838
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001839PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001840"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001841Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
Barry Warsaw53699e91996-12-10 23:23:01 +00001843static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001844posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001845{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001846#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001847 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001848#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001849 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001850#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001851 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001852#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001853 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001854#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001855}
1856
Fred Drake4d1e64b2002-04-15 19:40:07 +00001857#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001858PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001859"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001860Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001861opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001862
1863static PyObject *
1864posix_fchdir(PyObject *self, PyObject *fdobj)
1865{
Victor Stinner8c62be82010-05-06 00:08:46 +00001866 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001867}
1868#endif /* HAVE_FCHDIR */
1869
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001872"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001873Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001874
Barry Warsaw53699e91996-12-10 23:23:01 +00001875static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001876posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001877{
Victor Stinner8c62be82010-05-06 00:08:46 +00001878 PyObject *opath = NULL;
1879 char *path = NULL;
1880 int i;
1881 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001882#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 DWORD attr;
1884 PyUnicodeObject *po;
1885 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1886 Py_BEGIN_ALLOW_THREADS
1887 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1888 if (attr != 0xFFFFFFFF) {
1889 if (i & _S_IWRITE)
1890 attr &= ~FILE_ATTRIBUTE_READONLY;
1891 else
1892 attr |= FILE_ATTRIBUTE_READONLY;
1893 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1894 }
1895 else
1896 res = 0;
1897 Py_END_ALLOW_THREADS
1898 if (!res)
1899 return win32_error_unicode("chmod",
1900 PyUnicode_AS_UNICODE(po));
1901 Py_INCREF(Py_None);
1902 return Py_None;
1903 }
1904 /* Drop the argument parsing error as narrow strings
1905 are also valid. */
1906 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001907
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1909 &opath, &i))
1910 return NULL;
1911 path = PyBytes_AsString(opath);
1912 Py_BEGIN_ALLOW_THREADS
1913 attr = GetFileAttributesA(path);
1914 if (attr != 0xFFFFFFFF) {
1915 if (i & _S_IWRITE)
1916 attr &= ~FILE_ATTRIBUTE_READONLY;
1917 else
1918 attr |= FILE_ATTRIBUTE_READONLY;
1919 res = SetFileAttributesA(path, attr);
1920 }
1921 else
1922 res = 0;
1923 Py_END_ALLOW_THREADS
1924 if (!res) {
1925 win32_error("chmod", path);
1926 Py_DECREF(opath);
1927 return NULL;
1928 }
1929 Py_DECREF(opath);
1930 Py_INCREF(Py_None);
1931 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001932#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001933 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1934 &opath, &i))
1935 return NULL;
1936 path = PyBytes_AsString(opath);
1937 Py_BEGIN_ALLOW_THREADS
1938 res = chmod(path, i);
1939 Py_END_ALLOW_THREADS
1940 if (res < 0)
1941 return posix_error_with_allocated_filename(opath);
1942 Py_DECREF(opath);
1943 Py_INCREF(Py_None);
1944 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001945#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001946}
1947
Christian Heimes4e30a842007-11-30 22:12:06 +00001948#ifdef HAVE_FCHMOD
1949PyDoc_STRVAR(posix_fchmod__doc__,
1950"fchmod(fd, mode)\n\n\
1951Change the access permissions of the file given by file\n\
1952descriptor fd.");
1953
1954static PyObject *
1955posix_fchmod(PyObject *self, PyObject *args)
1956{
Victor Stinner8c62be82010-05-06 00:08:46 +00001957 int fd, mode, res;
1958 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1959 return NULL;
1960 Py_BEGIN_ALLOW_THREADS
1961 res = fchmod(fd, mode);
1962 Py_END_ALLOW_THREADS
1963 if (res < 0)
1964 return posix_error();
1965 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001966}
1967#endif /* HAVE_FCHMOD */
1968
1969#ifdef HAVE_LCHMOD
1970PyDoc_STRVAR(posix_lchmod__doc__,
1971"lchmod(path, mode)\n\n\
1972Change the access permissions of a file. If path is a symlink, this\n\
1973affects the link itself rather than the target.");
1974
1975static PyObject *
1976posix_lchmod(PyObject *self, PyObject *args)
1977{
Victor Stinner8c62be82010-05-06 00:08:46 +00001978 PyObject *opath;
1979 char *path;
1980 int i;
1981 int res;
1982 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1983 &opath, &i))
1984 return NULL;
1985 path = PyBytes_AsString(opath);
1986 Py_BEGIN_ALLOW_THREADS
1987 res = lchmod(path, i);
1988 Py_END_ALLOW_THREADS
1989 if (res < 0)
1990 return posix_error_with_allocated_filename(opath);
1991 Py_DECREF(opath);
1992 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001993}
1994#endif /* HAVE_LCHMOD */
1995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001996
Thomas Wouterscf297e42007-02-23 15:07:44 +00001997#ifdef HAVE_CHFLAGS
1998PyDoc_STRVAR(posix_chflags__doc__,
1999"chflags(path, flags)\n\n\
2000Set file flags.");
2001
2002static PyObject *
2003posix_chflags(PyObject *self, PyObject *args)
2004{
Victor Stinner8c62be82010-05-06 00:08:46 +00002005 PyObject *opath;
2006 char *path;
2007 unsigned long flags;
2008 int res;
2009 if (!PyArg_ParseTuple(args, "O&k:chflags",
2010 PyUnicode_FSConverter, &opath, &flags))
2011 return NULL;
2012 path = PyBytes_AsString(opath);
2013 Py_BEGIN_ALLOW_THREADS
2014 res = chflags(path, flags);
2015 Py_END_ALLOW_THREADS
2016 if (res < 0)
2017 return posix_error_with_allocated_filename(opath);
2018 Py_DECREF(opath);
2019 Py_INCREF(Py_None);
2020 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002021}
2022#endif /* HAVE_CHFLAGS */
2023
2024#ifdef HAVE_LCHFLAGS
2025PyDoc_STRVAR(posix_lchflags__doc__,
2026"lchflags(path, flags)\n\n\
2027Set file flags.\n\
2028This function will not follow symbolic links.");
2029
2030static PyObject *
2031posix_lchflags(PyObject *self, PyObject *args)
2032{
Victor Stinner8c62be82010-05-06 00:08:46 +00002033 PyObject *opath;
2034 char *path;
2035 unsigned long flags;
2036 int res;
2037 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2038 PyUnicode_FSConverter, &opath, &flags))
2039 return NULL;
2040 path = PyBytes_AsString(opath);
2041 Py_BEGIN_ALLOW_THREADS
2042 res = lchflags(path, flags);
2043 Py_END_ALLOW_THREADS
2044 if (res < 0)
2045 return posix_error_with_allocated_filename(opath);
2046 Py_DECREF(opath);
2047 Py_INCREF(Py_None);
2048 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002049}
2050#endif /* HAVE_LCHFLAGS */
2051
Martin v. Löwis244edc82001-10-04 22:44:26 +00002052#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002053PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002054"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002055Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002056
2057static PyObject *
2058posix_chroot(PyObject *self, PyObject *args)
2059{
Victor Stinner8c62be82010-05-06 00:08:46 +00002060 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002061}
2062#endif
2063
Guido van Rossum21142a01999-01-08 21:05:37 +00002064#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002065PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002066"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002068
2069static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002070posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002071{
Stefan Krah0e803b32010-11-26 16:16:47 +00002072 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002073}
2074#endif /* HAVE_FSYNC */
2075
2076#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002077
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002078#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002079extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2080#endif
2081
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002082PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002083"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002084force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002086
2087static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002088posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002089{
Stefan Krah0e803b32010-11-26 16:16:47 +00002090 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002091}
2092#endif /* HAVE_FDATASYNC */
2093
2094
Fredrik Lundh10723342000-07-10 16:38:09 +00002095#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002096PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002097"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002098Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002099
Barry Warsaw53699e91996-12-10 23:23:01 +00002100static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002101posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002102{
Victor Stinner8c62be82010-05-06 00:08:46 +00002103 PyObject *opath;
2104 char *path;
2105 long uid, gid;
2106 int res;
2107 if (!PyArg_ParseTuple(args, "O&ll:chown",
2108 PyUnicode_FSConverter, &opath,
2109 &uid, &gid))
2110 return NULL;
2111 path = PyBytes_AsString(opath);
2112 Py_BEGIN_ALLOW_THREADS
2113 res = chown(path, (uid_t) uid, (gid_t) gid);
2114 Py_END_ALLOW_THREADS
2115 if (res < 0)
2116 return posix_error_with_allocated_filename(opath);
2117 Py_DECREF(opath);
2118 Py_INCREF(Py_None);
2119 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002120}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002121#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122
Christian Heimes4e30a842007-11-30 22:12:06 +00002123#ifdef HAVE_FCHOWN
2124PyDoc_STRVAR(posix_fchown__doc__,
2125"fchown(fd, uid, gid)\n\n\
2126Change the owner and group id of the file given by file descriptor\n\
2127fd to the numeric uid and gid.");
2128
2129static PyObject *
2130posix_fchown(PyObject *self, PyObject *args)
2131{
Victor Stinner8c62be82010-05-06 00:08:46 +00002132 int fd;
2133 long uid, gid;
2134 int res;
2135 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2136 return NULL;
2137 Py_BEGIN_ALLOW_THREADS
2138 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2139 Py_END_ALLOW_THREADS
2140 if (res < 0)
2141 return posix_error();
2142 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002143}
2144#endif /* HAVE_FCHOWN */
2145
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002146#ifdef HAVE_LCHOWN
2147PyDoc_STRVAR(posix_lchown__doc__,
2148"lchown(path, uid, gid)\n\n\
2149Change the owner and group id of path to the numeric uid and gid.\n\
2150This function will not follow symbolic links.");
2151
2152static PyObject *
2153posix_lchown(PyObject *self, PyObject *args)
2154{
Victor Stinner8c62be82010-05-06 00:08:46 +00002155 PyObject *opath;
2156 char *path;
2157 long uid, gid;
2158 int res;
2159 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2160 PyUnicode_FSConverter, &opath,
2161 &uid, &gid))
2162 return NULL;
2163 path = PyBytes_AsString(opath);
2164 Py_BEGIN_ALLOW_THREADS
2165 res = lchown(path, (uid_t) uid, (gid_t) gid);
2166 Py_END_ALLOW_THREADS
2167 if (res < 0)
2168 return posix_error_with_allocated_filename(opath);
2169 Py_DECREF(opath);
2170 Py_INCREF(Py_None);
2171 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002172}
2173#endif /* HAVE_LCHOWN */
2174
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002175
Guido van Rossum36bc6801995-06-14 22:54:23 +00002176#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002177static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002178posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002179{
Victor Stinner8c62be82010-05-06 00:08:46 +00002180 char buf[1026];
2181 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002182
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002183#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002184 if (!use_bytes) {
2185 wchar_t wbuf[1026];
2186 wchar_t *wbuf2 = wbuf;
2187 PyObject *resobj;
2188 DWORD len;
2189 Py_BEGIN_ALLOW_THREADS
2190 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2191 /* If the buffer is large enough, len does not include the
2192 terminating \0. If the buffer is too small, len includes
2193 the space needed for the terminator. */
2194 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2195 wbuf2 = malloc(len * sizeof(wchar_t));
2196 if (wbuf2)
2197 len = GetCurrentDirectoryW(len, wbuf2);
2198 }
2199 Py_END_ALLOW_THREADS
2200 if (!wbuf2) {
2201 PyErr_NoMemory();
2202 return NULL;
2203 }
2204 if (!len) {
2205 if (wbuf2 != wbuf) free(wbuf2);
2206 return win32_error("getcwdu", NULL);
2207 }
2208 resobj = PyUnicode_FromWideChar(wbuf2, len);
2209 if (wbuf2 != wbuf) free(wbuf2);
2210 return resobj;
2211 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002212#endif
2213
Victor Stinner8c62be82010-05-06 00:08:46 +00002214 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002215#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002216 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002217#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002218 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002219#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002220 Py_END_ALLOW_THREADS
2221 if (res == NULL)
2222 return posix_error();
2223 if (use_bytes)
2224 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002225 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002226}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002227
2228PyDoc_STRVAR(posix_getcwd__doc__,
2229"getcwd() -> path\n\n\
2230Return a unicode string representing the current working directory.");
2231
2232static PyObject *
2233posix_getcwd_unicode(PyObject *self)
2234{
2235 return posix_getcwd(0);
2236}
2237
2238PyDoc_STRVAR(posix_getcwdb__doc__,
2239"getcwdb() -> path\n\n\
2240Return a bytes string representing the current working directory.");
2241
2242static PyObject *
2243posix_getcwd_bytes(PyObject *self)
2244{
2245 return posix_getcwd(1);
2246}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002247#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002248
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002249
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002251PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002252"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002254
Barry Warsaw53699e91996-12-10 23:23:01 +00002255static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002256posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002257{
Victor Stinner8c62be82010-05-06 00:08:46 +00002258 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002259}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002260#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002261
Brian Curtin1b9df392010-11-24 20:24:31 +00002262#ifdef MS_WINDOWS
2263PyDoc_STRVAR(win32_link__doc__,
2264"link(src, dst)\n\n\
2265Create a hard link to a file.");
2266
2267static PyObject *
2268win32_link(PyObject *self, PyObject *args)
2269{
2270 PyObject *osrc, *odst;
2271 char *src, *dst;
2272 BOOL rslt;
2273
Brian Curtinfc889c42010-11-28 23:59:46 +00002274 PyUnicodeObject *usrc, *udst;
2275 if (PyArg_ParseTuple(args, "UU:link", &usrc, &udst)) {
2276 Py_BEGIN_ALLOW_THREADS
2277 rslt = CreateHardLinkW(PyUnicode_AS_UNICODE(udst),
2278 PyUnicode_AS_UNICODE(usrc), NULL);
2279 Py_END_ALLOW_THREADS
2280
2281 if (rslt == 0)
2282 return win32_error("link", NULL);
2283
2284 Py_RETURN_NONE;
2285 }
2286
2287 /* Narrow strings also valid. */
2288 PyErr_Clear();
2289
Brian Curtin1b9df392010-11-24 20:24:31 +00002290 if (!PyArg_ParseTuple(args, "O&O&:link", PyUnicode_FSConverter, &osrc,
2291 PyUnicode_FSConverter, &odst))
2292 return NULL;
2293
2294 src = PyBytes_AsString(osrc);
2295 dst = PyBytes_AsString(odst);
2296
2297 Py_BEGIN_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00002298 rslt = CreateHardLinkA(dst, src, NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002299 Py_END_ALLOW_THREADS
2300
Stefan Krah30b341f2010-11-27 11:44:18 +00002301 Py_DECREF(osrc);
2302 Py_DECREF(odst);
Brian Curtin1b9df392010-11-24 20:24:31 +00002303 if (rslt == 0)
Brian Curtinfc889c42010-11-28 23:59:46 +00002304 return win32_error("link", NULL);
Brian Curtin1b9df392010-11-24 20:24:31 +00002305
2306 Py_RETURN_NONE;
2307}
2308#endif /* MS_WINDOWS */
2309
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002311PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002312"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002313Return a list containing the names of the entries in the directory.\n\
2314\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002315 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002316\n\
2317The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002318entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002319
Barry Warsaw53699e91996-12-10 23:23:01 +00002320static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002321posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002322{
Victor Stinner8c62be82010-05-06 00:08:46 +00002323 /* XXX Should redo this putting the (now four) versions of opendir
2324 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002325#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002326
Victor Stinner8c62be82010-05-06 00:08:46 +00002327 PyObject *d, *v;
2328 HANDLE hFindFile;
2329 BOOL result;
2330 WIN32_FIND_DATA FileData;
2331 PyObject *opath;
2332 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2333 char *bufptr = namebuf;
2334 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002335
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002336 PyObject *po = NULL;
2337 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002338 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002339 Py_UNICODE *wnamebuf, *po_wchars;
2340
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002341 if (po == NULL) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002342 po_wchars = L".";
2343 len = 1;
2344 } else {
2345 po_wchars = PyUnicode_AS_UNICODE(po);
2346 len = PyUnicode_GET_SIZE(po);
2347 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002348 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002349 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2350 if (!wnamebuf) {
2351 PyErr_NoMemory();
2352 return NULL;
2353 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002354 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002355 if (len > 0) {
2356 Py_UNICODE wch = wnamebuf[len-1];
2357 if (wch != L'/' && wch != L'\\' && wch != L':')
2358 wnamebuf[len++] = L'\\';
2359 wcscpy(wnamebuf + len, L"*.*");
2360 }
2361 if ((d = PyList_New(0)) == NULL) {
2362 free(wnamebuf);
2363 return NULL;
2364 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002365 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002366 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002367 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 if (hFindFile == INVALID_HANDLE_VALUE) {
2369 int error = GetLastError();
2370 if (error == ERROR_FILE_NOT_FOUND) {
2371 free(wnamebuf);
2372 return d;
2373 }
2374 Py_DECREF(d);
2375 win32_error_unicode("FindFirstFileW", wnamebuf);
2376 free(wnamebuf);
2377 return NULL;
2378 }
2379 do {
2380 /* Skip over . and .. */
2381 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2382 wcscmp(wFileData.cFileName, L"..") != 0) {
2383 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2384 if (v == NULL) {
2385 Py_DECREF(d);
2386 d = NULL;
2387 break;
2388 }
2389 if (PyList_Append(d, v) != 0) {
2390 Py_DECREF(v);
2391 Py_DECREF(d);
2392 d = NULL;
2393 break;
2394 }
2395 Py_DECREF(v);
2396 }
2397 Py_BEGIN_ALLOW_THREADS
2398 result = FindNextFileW(hFindFile, &wFileData);
2399 Py_END_ALLOW_THREADS
2400 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2401 it got to the end of the directory. */
2402 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2403 Py_DECREF(d);
2404 win32_error_unicode("FindNextFileW", wnamebuf);
2405 FindClose(hFindFile);
2406 free(wnamebuf);
2407 return NULL;
2408 }
2409 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002410
Victor Stinner8c62be82010-05-06 00:08:46 +00002411 if (FindClose(hFindFile) == FALSE) {
2412 Py_DECREF(d);
2413 win32_error_unicode("FindClose", wnamebuf);
2414 free(wnamebuf);
2415 return NULL;
2416 }
2417 free(wnamebuf);
2418 return d;
2419 }
2420 /* Drop the argument parsing error as narrow strings
2421 are also valid. */
2422 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002423
Victor Stinner8c62be82010-05-06 00:08:46 +00002424 if (!PyArg_ParseTuple(args, "O&:listdir",
2425 PyUnicode_FSConverter, &opath))
2426 return NULL;
2427 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2428 PyErr_SetString(PyExc_ValueError, "path too long");
2429 Py_DECREF(opath);
2430 return NULL;
2431 }
2432 strcpy(namebuf, PyBytes_AsString(opath));
2433 len = PyObject_Size(opath);
Stefan Krah2a7feee2010-11-27 22:06:49 +00002434 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00002435 if (len > 0) {
2436 char ch = namebuf[len-1];
2437 if (ch != SEP && ch != ALTSEP && ch != ':')
2438 namebuf[len++] = '/';
2439 strcpy(namebuf + len, "*.*");
2440 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002441
Victor Stinner8c62be82010-05-06 00:08:46 +00002442 if ((d = PyList_New(0)) == NULL)
2443 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002444
Antoine Pitroub73caab2010-08-09 23:39:31 +00002445 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002446 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002447 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002448 if (hFindFile == INVALID_HANDLE_VALUE) {
2449 int error = GetLastError();
2450 if (error == ERROR_FILE_NOT_FOUND)
2451 return d;
2452 Py_DECREF(d);
2453 return win32_error("FindFirstFile", namebuf);
2454 }
2455 do {
2456 /* Skip over . and .. */
2457 if (strcmp(FileData.cFileName, ".") != 0 &&
2458 strcmp(FileData.cFileName, "..") != 0) {
2459 v = PyBytes_FromString(FileData.cFileName);
2460 if (v == NULL) {
2461 Py_DECREF(d);
2462 d = NULL;
2463 break;
2464 }
2465 if (PyList_Append(d, v) != 0) {
2466 Py_DECREF(v);
2467 Py_DECREF(d);
2468 d = NULL;
2469 break;
2470 }
2471 Py_DECREF(v);
2472 }
2473 Py_BEGIN_ALLOW_THREADS
2474 result = FindNextFile(hFindFile, &FileData);
2475 Py_END_ALLOW_THREADS
2476 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2477 it got to the end of the directory. */
2478 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2479 Py_DECREF(d);
2480 win32_error("FindNextFile", namebuf);
2481 FindClose(hFindFile);
2482 return NULL;
2483 }
2484 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002485
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 if (FindClose(hFindFile) == FALSE) {
2487 Py_DECREF(d);
2488 return win32_error("FindClose", namebuf);
2489 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002490
Victor Stinner8c62be82010-05-06 00:08:46 +00002491 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002492
Tim Peters0bb44a42000-09-15 07:44:49 +00002493#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002494
2495#ifndef MAX_PATH
2496#define MAX_PATH CCHMAXPATH
2497#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002498 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002499 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002500 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002501 PyObject *d, *v;
2502 char namebuf[MAX_PATH+5];
2503 HDIR hdir = 1;
2504 ULONG srchcnt = 1;
2505 FILEFINDBUF3 ep;
2506 APIRET rc;
2507
Victor Stinner8c62be82010-05-06 00:08:46 +00002508 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002509 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002510 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002511 name = PyBytes_AsString(oname);
2512 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002513 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002514 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002515 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002516 return NULL;
2517 }
2518 strcpy(namebuf, name);
2519 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002520 if (*pt == ALTSEP)
2521 *pt = SEP;
2522 if (namebuf[len-1] != SEP)
2523 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002524 strcpy(namebuf + len, "*.*");
2525
Neal Norwitz6c913782007-10-14 03:23:09 +00002526 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002527 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002528 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002529 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002530
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002531 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2532 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002533 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002534 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2535 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2536 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002537
2538 if (rc != NO_ERROR) {
2539 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002540 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002541 }
2542
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002543 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002544 do {
2545 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002546 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002547 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002548
2549 strcpy(namebuf, ep.achName);
2550
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002551 /* Leave Case of Name Alone -- In Native Form */
2552 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002553
Christian Heimes72b710a2008-05-26 13:28:38 +00002554 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002555 if (v == NULL) {
2556 Py_DECREF(d);
2557 d = NULL;
2558 break;
2559 }
2560 if (PyList_Append(d, v) != 0) {
2561 Py_DECREF(v);
2562 Py_DECREF(d);
2563 d = NULL;
2564 break;
2565 }
2566 Py_DECREF(v);
2567 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2568 }
2569
Victor Stinnerdcb24032010-04-22 12:08:36 +00002570 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002571 return d;
2572#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002573 PyObject *oname;
2574 char *name;
2575 PyObject *d, *v;
2576 DIR *dirp;
2577 struct dirent *ep;
2578 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002579
Victor Stinner8c62be82010-05-06 00:08:46 +00002580 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002581 /* v is never read, so it does not need to be initialized yet. */
2582 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002583 arg_is_unicode = 0;
2584 PyErr_Clear();
2585 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002586 oname = NULL;
2587 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002588 return NULL;
Antoine Pitrou3d330ad2010-09-14 10:08:08 +00002589 if (oname == NULL) { /* Default arg: "." */
Stefan Krah0e803b32010-11-26 16:16:47 +00002590 oname = PyBytes_FromString(".");
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002591 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002592 name = PyBytes_AsString(oname);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002593 Py_BEGIN_ALLOW_THREADS
2594 dirp = opendir(name);
2595 Py_END_ALLOW_THREADS
2596 if (dirp == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002597 return posix_error_with_allocated_filename(oname);
2598 }
2599 if ((d = PyList_New(0)) == NULL) {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002600 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002601 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002602 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002603 Py_DECREF(oname);
2604 return NULL;
2605 }
2606 for (;;) {
2607 errno = 0;
2608 Py_BEGIN_ALLOW_THREADS
2609 ep = readdir(dirp);
2610 Py_END_ALLOW_THREADS
2611 if (ep == NULL) {
2612 if (errno == 0) {
2613 break;
2614 } else {
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002615 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002616 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002617 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002618 Py_DECREF(d);
2619 return posix_error_with_allocated_filename(oname);
2620 }
2621 }
2622 if (ep->d_name[0] == '.' &&
2623 (NAMLEN(ep) == 1 ||
2624 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2625 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002626 if (arg_is_unicode)
2627 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2628 else
2629 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002630 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002631 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 break;
2633 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 if (PyList_Append(d, v) != 0) {
2635 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002636 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002637 break;
2638 }
2639 Py_DECREF(v);
2640 }
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002641 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002642 closedir(dirp);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00002643 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002644 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002645
Victor Stinner8c62be82010-05-06 00:08:46 +00002646 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002647
Tim Peters0bb44a42000-09-15 07:44:49 +00002648#endif /* which OS */
2649} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002650
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002651#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002652/* A helper function for abspath on win32 */
2653static PyObject *
2654posix__getfullpathname(PyObject *self, PyObject *args)
2655{
Victor Stinner8c62be82010-05-06 00:08:46 +00002656 PyObject *opath;
2657 char *path;
2658 char outbuf[MAX_PATH*2];
2659 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002660#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002661 PyUnicodeObject *po;
2662 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2663 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2664 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2665 Py_UNICODE *wtemp;
2666 DWORD result;
2667 PyObject *v;
2668 result = GetFullPathNameW(wpath,
2669 sizeof(woutbuf)/sizeof(woutbuf[0]),
2670 woutbuf, &wtemp);
2671 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2672 woutbufp = malloc(result * sizeof(Py_UNICODE));
2673 if (!woutbufp)
2674 return PyErr_NoMemory();
2675 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2676 }
2677 if (result)
2678 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2679 else
2680 v = win32_error_unicode("GetFullPathNameW", wpath);
2681 if (woutbufp != woutbuf)
2682 free(woutbufp);
2683 return v;
2684 }
2685 /* Drop the argument parsing error as narrow strings
2686 are also valid. */
2687 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002688
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002689#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002690 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2691 PyUnicode_FSConverter, &opath))
2692 return NULL;
2693 path = PyBytes_AsString(opath);
2694 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2695 outbuf, &temp)) {
2696 win32_error("GetFullPathName", path);
2697 Py_DECREF(opath);
2698 return NULL;
2699 }
2700 Py_DECREF(opath);
2701 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2702 return PyUnicode_Decode(outbuf, strlen(outbuf),
2703 Py_FileSystemDefaultEncoding, NULL);
2704 }
2705 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002706} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002707
Brian Curtinf5e76d02010-11-24 13:14:05 +00002708/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
2709static int has_GetFinalPathNameByHandle = 0;
2710static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
2711 DWORD);
2712static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
2713 DWORD);
2714static int
2715check_GetFinalPathNameByHandle()
2716{
2717 HINSTANCE hKernel32;
2718 /* only recheck */
2719 if (!has_GetFinalPathNameByHandle)
2720 {
2721 hKernel32 = GetModuleHandle("KERNEL32");
2722 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
2723 "GetFinalPathNameByHandleA");
2724 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
2725 "GetFinalPathNameByHandleW");
2726 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
2727 Py_GetFinalPathNameByHandleW;
2728 }
2729 return has_GetFinalPathNameByHandle;
2730}
2731
Brian Curtind40e6f72010-07-08 21:39:08 +00002732/* A helper function for samepath on windows */
2733static PyObject *
2734posix__getfinalpathname(PyObject *self, PyObject *args)
2735{
2736 HANDLE hFile;
2737 int buf_size;
2738 wchar_t *target_path;
2739 int result_length;
2740 PyObject *result;
2741 wchar_t *path;
2742
Brian Curtin94622b02010-09-24 00:03:39 +00002743 if (!PyArg_ParseTuple(args, "u|:_getfinalpathname", &path)) {
Brian Curtind40e6f72010-07-08 21:39:08 +00002744 return NULL;
2745 }
2746
2747 if(!check_GetFinalPathNameByHandle()) {
2748 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2749 NotImplementedError. */
2750 return PyErr_Format(PyExc_NotImplementedError,
2751 "GetFinalPathNameByHandle not available on this platform");
2752 }
2753
2754 hFile = CreateFileW(
2755 path,
2756 0, /* desired access */
2757 0, /* share mode */
2758 NULL, /* security attributes */
2759 OPEN_EXISTING,
2760 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2761 FILE_FLAG_BACKUP_SEMANTICS,
2762 NULL);
2763
2764 if(hFile == INVALID_HANDLE_VALUE) {
2765 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002766 return PyErr_Format(PyExc_RuntimeError,
2767 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002768 }
2769
2770 /* We have a good handle to the target, use it to determine the
2771 target path name. */
2772 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2773
2774 if(!buf_size)
2775 return win32_error_unicode("GetFinalPathNameByHandle", path);
2776
2777 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2778 if(!target_path)
2779 return PyErr_NoMemory();
2780
2781 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2782 buf_size, VOLUME_NAME_DOS);
2783 if(!result_length)
2784 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2785
2786 if(!CloseHandle(hFile))
2787 return win32_error_unicode("GetFinalPathNameByHandle", path);
2788
2789 target_path[result_length] = 0;
2790 result = PyUnicode_FromUnicode(target_path, result_length);
2791 free(target_path);
2792 return result;
2793
2794} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00002795
2796static PyObject *
2797posix__getfileinformation(PyObject *self, PyObject *args)
2798{
2799 HANDLE hFile;
2800 BY_HANDLE_FILE_INFORMATION info;
2801 int fd;
2802
2803 if (!PyArg_ParseTuple(args, "i:_getfileinformation", &fd))
2804 return NULL;
2805
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002806 if (!_PyVerify_fd(fd))
2807 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002808
2809 hFile = (HANDLE)_get_osfhandle(fd);
2810 if (hFile == INVALID_HANDLE_VALUE)
Hirokazu Yamamoto26253bb2010-12-05 04:16:47 +00002811 return posix_error();
Brian Curtin62857742010-09-06 17:07:27 +00002812
2813 if (!GetFileInformationByHandle(hFile, &info))
2814 return win32_error("_getfileinformation", NULL);
2815
2816 return Py_BuildValue("iii", info.dwVolumeSerialNumber,
2817 info.nFileIndexHigh,
2818 info.nFileIndexLow);
2819}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002820#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002822PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002823"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002824Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002825
Barry Warsaw53699e91996-12-10 23:23:01 +00002826static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002827posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002828{
Victor Stinner8c62be82010-05-06 00:08:46 +00002829 int res;
2830 PyObject *opath;
2831 char *path;
2832 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002833
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002834#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002835 PyUnicodeObject *po;
2836 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2837 Py_BEGIN_ALLOW_THREADS
2838 /* PyUnicode_AS_UNICODE OK without thread lock as
2839 it is a simple dereference. */
2840 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2841 Py_END_ALLOW_THREADS
2842 if (!res)
2843 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2844 Py_INCREF(Py_None);
2845 return Py_None;
2846 }
2847 /* Drop the argument parsing error as narrow strings
2848 are also valid. */
2849 PyErr_Clear();
2850 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2851 PyUnicode_FSConverter, &opath, &mode))
2852 return NULL;
2853 path = PyBytes_AsString(opath);
2854 Py_BEGIN_ALLOW_THREADS
2855 /* PyUnicode_AS_UNICODE OK without thread lock as
2856 it is a simple dereference. */
2857 res = CreateDirectoryA(path, NULL);
2858 Py_END_ALLOW_THREADS
2859 if (!res) {
2860 win32_error("mkdir", path);
2861 Py_DECREF(opath);
2862 return NULL;
2863 }
2864 Py_DECREF(opath);
2865 Py_INCREF(Py_None);
2866 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002867#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002868
Victor Stinner8c62be82010-05-06 00:08:46 +00002869 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2870 PyUnicode_FSConverter, &opath, &mode))
2871 return NULL;
2872 path = PyBytes_AsString(opath);
2873 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002874#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002875 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002876#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002877 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002878#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 Py_END_ALLOW_THREADS
2880 if (res < 0)
2881 return posix_error_with_allocated_filename(opath);
2882 Py_DECREF(opath);
2883 Py_INCREF(Py_None);
2884 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002885#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002886}
2887
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002888
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002889/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2890#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002891#include <sys/resource.h>
2892#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002893
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002894
2895#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002896PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002897"nice(inc) -> new_priority\n\n\
2898Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002899
Barry Warsaw53699e91996-12-10 23:23:01 +00002900static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002901posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002902{
Victor Stinner8c62be82010-05-06 00:08:46 +00002903 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002904
Victor Stinner8c62be82010-05-06 00:08:46 +00002905 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2906 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002907
Victor Stinner8c62be82010-05-06 00:08:46 +00002908 /* There are two flavours of 'nice': one that returns the new
2909 priority (as required by almost all standards out there) and the
2910 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2911 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002912
Victor Stinner8c62be82010-05-06 00:08:46 +00002913 If we are of the nice family that returns the new priority, we
2914 need to clear errno before the call, and check if errno is filled
2915 before calling posix_error() on a returnvalue of -1, because the
2916 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002917
Victor Stinner8c62be82010-05-06 00:08:46 +00002918 errno = 0;
2919 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002920#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002921 if (value == 0)
2922 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002923#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002924 if (value == -1 && errno != 0)
2925 /* either nice() or getpriority() returned an error */
2926 return posix_error();
2927 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002928}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002929#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002930
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002931PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002932"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002933Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002934
Barry Warsaw53699e91996-12-10 23:23:01 +00002935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002936posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002937{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002938#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002939 PyObject *o1, *o2;
2940 char *p1, *p2;
2941 BOOL result;
2942 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2943 goto error;
2944 if (!convert_to_unicode(&o1))
2945 goto error;
2946 if (!convert_to_unicode(&o2)) {
2947 Py_DECREF(o1);
2948 goto error;
2949 }
2950 Py_BEGIN_ALLOW_THREADS
2951 result = MoveFileW(PyUnicode_AsUnicode(o1),
2952 PyUnicode_AsUnicode(o2));
2953 Py_END_ALLOW_THREADS
2954 Py_DECREF(o1);
2955 Py_DECREF(o2);
2956 if (!result)
2957 return win32_error("rename", NULL);
2958 Py_INCREF(Py_None);
2959 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002960error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002961 PyErr_Clear();
2962 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2963 return NULL;
2964 Py_BEGIN_ALLOW_THREADS
2965 result = MoveFileA(p1, p2);
2966 Py_END_ALLOW_THREADS
2967 if (!result)
2968 return win32_error("rename", NULL);
2969 Py_INCREF(Py_None);
2970 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002971#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002972 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002973#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002974}
2975
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002977PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002978"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002979Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002980
Barry Warsaw53699e91996-12-10 23:23:01 +00002981static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002982posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002983{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002984#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002985 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002986#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002988#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002989}
2990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002992PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002993"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002994Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002995
Barry Warsaw53699e91996-12-10 23:23:01 +00002996static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002997posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002998{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002999#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00003000 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003001#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003002 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003003#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003004}
3005
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003006
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003007#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003008PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003009"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003010Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003011
Barry Warsaw53699e91996-12-10 23:23:01 +00003012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003013posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003014{
Victor Stinner8c62be82010-05-06 00:08:46 +00003015 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00003016#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003017 wchar_t *command;
3018 if (!PyArg_ParseTuple(args, "u:system", &command))
3019 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003020
Victor Stinner8c62be82010-05-06 00:08:46 +00003021 Py_BEGIN_ALLOW_THREADS
3022 sts = _wsystem(command);
3023 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00003024#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003025 PyObject *command_obj;
3026 char *command;
3027 if (!PyArg_ParseTuple(args, "O&:system",
3028 PyUnicode_FSConverter, &command_obj))
3029 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00003030
Victor Stinner8c62be82010-05-06 00:08:46 +00003031 command = PyBytes_AsString(command_obj);
3032 Py_BEGIN_ALLOW_THREADS
3033 sts = system(command);
3034 Py_END_ALLOW_THREADS
3035 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00003036#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003037 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003038}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003039#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003040
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003042PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003043"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003044Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003045
Barry Warsaw53699e91996-12-10 23:23:01 +00003046static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003047posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003048{
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 int i;
3050 if (!PyArg_ParseTuple(args, "i:umask", &i))
3051 return NULL;
3052 i = (int)umask(i);
3053 if (i < 0)
3054 return posix_error();
3055 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003056}
3057
Brian Curtind40e6f72010-07-08 21:39:08 +00003058#ifdef MS_WINDOWS
3059
3060/* override the default DeleteFileW behavior so that directory
3061symlinks can be removed with this function, the same as with
3062Unix symlinks */
3063BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
3064{
3065 WIN32_FILE_ATTRIBUTE_DATA info;
3066 WIN32_FIND_DATAW find_data;
3067 HANDLE find_data_handle;
3068 int is_directory = 0;
3069 int is_link = 0;
3070
3071 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3072 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3073
3074 /* Get WIN32_FIND_DATA structure for the path to determine if
3075 it is a symlink */
3076 if(is_directory &&
3077 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3078 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3079
3080 if(find_data_handle != INVALID_HANDLE_VALUE) {
3081 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3082 FindClose(find_data_handle);
3083 }
3084 }
3085 }
3086
3087 if (is_directory && is_link)
3088 return RemoveDirectoryW(lpFileName);
3089
3090 return DeleteFileW(lpFileName);
3091}
3092#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003093
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003094PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003095"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003096Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003097
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003098PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003099"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003101
Barry Warsaw53699e91996-12-10 23:23:01 +00003102static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003103posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003104{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003105#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003106 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3107 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003108#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003109 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003110#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003111}
3112
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003113
Guido van Rossumb6775db1994-08-01 11:34:53 +00003114#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003115PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003116"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003117Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003118
Barry Warsaw53699e91996-12-10 23:23:01 +00003119static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003120posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003121{
Victor Stinner8c62be82010-05-06 00:08:46 +00003122 struct utsname u;
3123 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003124
Victor Stinner8c62be82010-05-06 00:08:46 +00003125 Py_BEGIN_ALLOW_THREADS
3126 res = uname(&u);
3127 Py_END_ALLOW_THREADS
3128 if (res < 0)
3129 return posix_error();
3130 return Py_BuildValue("(sssss)",
3131 u.sysname,
3132 u.nodename,
3133 u.release,
3134 u.version,
3135 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003136}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003137#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003138
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003139static int
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003140extract_time(PyObject *t, time_t* sec, long* usec)
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003141{
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003142 time_t intval;
Victor Stinner8c62be82010-05-06 00:08:46 +00003143 if (PyFloat_Check(t)) {
3144 double tval = PyFloat_AsDouble(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003145 PyObject *intobj = PyNumber_Long(t);
Victor Stinner8c62be82010-05-06 00:08:46 +00003146 if (!intobj)
3147 return -1;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003148#if SIZEOF_TIME_T > SIZEOF_LONG
3149 intval = PyLong_AsUnsignedLongLongMask(intobj);
3150#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003151 intval = PyLong_AsLong(intobj);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003152#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 Py_DECREF(intobj);
3154 if (intval == -1 && PyErr_Occurred())
3155 return -1;
3156 *sec = intval;
3157 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3158 if (*usec < 0)
3159 /* If rounding gave us a negative number,
3160 truncate. */
3161 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003162 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003163 }
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003164#if SIZEOF_TIME_T > SIZEOF_LONG
3165 intval = PyLong_AsUnsignedLongLongMask(t);
3166#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003167 intval = PyLong_AsLong(t);
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003168#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003169 if (intval == -1 && PyErr_Occurred())
3170 return -1;
3171 *sec = intval;
3172 *usec = 0;
3173 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003174}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003176PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003177"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003178utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003179Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003180second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003181
Barry Warsaw53699e91996-12-10 23:23:01 +00003182static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003183posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003184{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003185#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003186 PyObject *arg;
3187 PyUnicodeObject *obwpath;
3188 wchar_t *wpath = NULL;
3189 PyObject *oapath;
3190 char *apath;
3191 HANDLE hFile;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003192 time_t atimesec, mtimesec;
3193 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 FILETIME atime, mtime;
3195 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003196
Victor Stinner8c62be82010-05-06 00:08:46 +00003197 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3198 wpath = PyUnicode_AS_UNICODE(obwpath);
3199 Py_BEGIN_ALLOW_THREADS
3200 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3201 NULL, OPEN_EXISTING,
3202 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3203 Py_END_ALLOW_THREADS
3204 if (hFile == INVALID_HANDLE_VALUE)
3205 return win32_error_unicode("utime", wpath);
3206 } else
3207 /* Drop the argument parsing error as narrow strings
3208 are also valid. */
3209 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003210
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 if (!wpath) {
3212 if (!PyArg_ParseTuple(args, "O&O:utime",
3213 PyUnicode_FSConverter, &oapath, &arg))
3214 return NULL;
3215 apath = PyBytes_AsString(oapath);
3216 Py_BEGIN_ALLOW_THREADS
3217 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3218 NULL, OPEN_EXISTING,
3219 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3220 Py_END_ALLOW_THREADS
3221 if (hFile == INVALID_HANDLE_VALUE) {
3222 win32_error("utime", apath);
3223 Py_DECREF(oapath);
3224 return NULL;
3225 }
3226 Py_DECREF(oapath);
3227 }
3228
3229 if (arg == Py_None) {
3230 SYSTEMTIME now;
3231 GetSystemTime(&now);
3232 if (!SystemTimeToFileTime(&now, &mtime) ||
3233 !SystemTimeToFileTime(&now, &atime)) {
3234 win32_error("utime", NULL);
3235 goto done;
Stefan Krah0e803b32010-11-26 16:16:47 +00003236 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003237 }
3238 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3239 PyErr_SetString(PyExc_TypeError,
3240 "utime() arg 2 must be a tuple (atime, mtime)");
3241 goto done;
3242 }
3243 else {
3244 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3245 &atimesec, &ausec) == -1)
3246 goto done;
3247 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3248 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3249 &mtimesec, &musec) == -1)
3250 goto done;
3251 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3252 }
3253 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3254 /* Avoid putting the file name into the error here,
3255 as that may confuse the user into believing that
3256 something is wrong with the file, when it also
3257 could be the time stamp that gives a problem. */
3258 win32_error("utime", NULL);
3259 }
3260 Py_INCREF(Py_None);
3261 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003262done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003263 CloseHandle(hFile);
3264 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003265#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003266
Victor Stinner8c62be82010-05-06 00:08:46 +00003267 PyObject *opath;
3268 char *path;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00003269 time_t atime, mtime;
3270 long ausec, musec;
Victor Stinner8c62be82010-05-06 00:08:46 +00003271 int res;
3272 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003273
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003274#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003275 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003276#define ATIME buf[0].tv_sec
3277#define MTIME buf[1].tv_sec
3278#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003279/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003280 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003281#define ATIME buf.actime
3282#define MTIME buf.modtime
3283#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003284#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003285 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003286#define ATIME buf[0]
3287#define MTIME buf[1]
3288#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003289#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003290
Mark Hammond817c9292003-12-03 01:22:38 +00003291
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 if (!PyArg_ParseTuple(args, "O&O:utime",
3293 PyUnicode_FSConverter, &opath, &arg))
3294 return NULL;
3295 path = PyBytes_AsString(opath);
3296 if (arg == Py_None) {
3297 /* optional time values not given */
3298 Py_BEGIN_ALLOW_THREADS
3299 res = utime(path, NULL);
3300 Py_END_ALLOW_THREADS
3301 }
3302 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3303 PyErr_SetString(PyExc_TypeError,
3304 "utime() arg 2 must be a tuple (atime, mtime)");
3305 Py_DECREF(opath);
3306 return NULL;
3307 }
3308 else {
3309 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3310 &atime, &ausec) == -1) {
3311 Py_DECREF(opath);
3312 return NULL;
3313 }
3314 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3315 &mtime, &musec) == -1) {
3316 Py_DECREF(opath);
3317 return NULL;
3318 }
3319 ATIME = atime;
3320 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003321#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 buf[0].tv_usec = ausec;
3323 buf[1].tv_usec = musec;
3324 Py_BEGIN_ALLOW_THREADS
3325 res = utimes(path, buf);
3326 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003327#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003328 Py_BEGIN_ALLOW_THREADS
3329 res = utime(path, UTIME_ARG);
3330 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003331#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003332 }
3333 if (res < 0) {
3334 return posix_error_with_allocated_filename(opath);
3335 }
3336 Py_DECREF(opath);
3337 Py_INCREF(Py_None);
3338 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003339#undef UTIME_ARG
3340#undef ATIME
3341#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003342#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003343}
3344
Guido van Rossum85e3b011991-06-03 12:42:10 +00003345
Guido van Rossum3b066191991-06-04 19:40:25 +00003346/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003347
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003348PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003349"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003350Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003351
Barry Warsaw53699e91996-12-10 23:23:01 +00003352static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003353posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003354{
Victor Stinner8c62be82010-05-06 00:08:46 +00003355 int sts;
3356 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3357 return NULL;
3358 _exit(sts);
3359 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003360}
3361
Martin v. Löwis114619e2002-10-07 06:44:21 +00003362#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3363static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003364free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003365{
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 Py_ssize_t i;
3367 for (i = 0; i < count; i++)
3368 PyMem_Free(array[i]);
3369 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003370}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003371
Antoine Pitrou69f71142009-05-24 21:25:49 +00003372static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003373int fsconvert_strdup(PyObject *o, char**out)
3374{
Victor Stinner8c62be82010-05-06 00:08:46 +00003375 PyObject *bytes;
3376 Py_ssize_t size;
3377 if (!PyUnicode_FSConverter(o, &bytes))
3378 return 0;
3379 size = PyBytes_GET_SIZE(bytes);
3380 *out = PyMem_Malloc(size+1);
3381 if (!*out)
3382 return 0;
3383 memcpy(*out, PyBytes_AsString(bytes), size+1);
3384 Py_DECREF(bytes);
3385 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003386}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003387#endif
3388
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003389
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003390#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003391PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003392"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003393Execute an executable path with arguments, replacing current process.\n\
3394\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003395 path: path of executable file\n\
3396 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003397
Barry Warsaw53699e91996-12-10 23:23:01 +00003398static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003399posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003400{
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 PyObject *opath;
3402 char *path;
3403 PyObject *argv;
3404 char **argvlist;
3405 Py_ssize_t i, argc;
3406 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003407
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 /* execv has two arguments: (path, argv), where
3409 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003410
Victor Stinner8c62be82010-05-06 00:08:46 +00003411 if (!PyArg_ParseTuple(args, "O&O:execv",
3412 PyUnicode_FSConverter,
3413 &opath, &argv))
3414 return NULL;
3415 path = PyBytes_AsString(opath);
3416 if (PyList_Check(argv)) {
3417 argc = PyList_Size(argv);
3418 getitem = PyList_GetItem;
3419 }
3420 else if (PyTuple_Check(argv)) {
3421 argc = PyTuple_Size(argv);
3422 getitem = PyTuple_GetItem;
3423 }
3424 else {
3425 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3426 Py_DECREF(opath);
3427 return NULL;
3428 }
3429 if (argc < 1) {
3430 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3431 Py_DECREF(opath);
3432 return NULL;
3433 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003434
Victor Stinner8c62be82010-05-06 00:08:46 +00003435 argvlist = PyMem_NEW(char *, argc+1);
3436 if (argvlist == NULL) {
3437 Py_DECREF(opath);
3438 return PyErr_NoMemory();
3439 }
3440 for (i = 0; i < argc; i++) {
3441 if (!fsconvert_strdup((*getitem)(argv, i),
3442 &argvlist[i])) {
3443 free_string_array(argvlist, i);
3444 PyErr_SetString(PyExc_TypeError,
3445 "execv() arg 2 must contain only strings");
3446 Py_DECREF(opath);
3447 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003448
Victor Stinner8c62be82010-05-06 00:08:46 +00003449 }
3450 }
3451 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003452
Victor Stinner8c62be82010-05-06 00:08:46 +00003453 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003454
Victor Stinner8c62be82010-05-06 00:08:46 +00003455 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003456
Victor Stinner8c62be82010-05-06 00:08:46 +00003457 free_string_array(argvlist, argc);
3458 Py_DECREF(opath);
3459 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003460}
3461
Victor Stinner13bb71c2010-04-23 21:41:56 +00003462static char**
3463parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3464{
Victor Stinner8c62be82010-05-06 00:08:46 +00003465 char **envlist;
3466 Py_ssize_t i, pos, envc;
3467 PyObject *keys=NULL, *vals=NULL;
3468 PyObject *key, *val, *key2, *val2;
3469 char *p, *k, *v;
3470 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003471
Victor Stinner8c62be82010-05-06 00:08:46 +00003472 i = PyMapping_Size(env);
3473 if (i < 0)
3474 return NULL;
3475 envlist = PyMem_NEW(char *, i + 1);
3476 if (envlist == NULL) {
3477 PyErr_NoMemory();
3478 return NULL;
3479 }
3480 envc = 0;
3481 keys = PyMapping_Keys(env);
3482 vals = PyMapping_Values(env);
3483 if (!keys || !vals)
3484 goto error;
3485 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3486 PyErr_Format(PyExc_TypeError,
3487 "env.keys() or env.values() is not a list");
3488 goto error;
3489 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003490
Victor Stinner8c62be82010-05-06 00:08:46 +00003491 for (pos = 0; pos < i; pos++) {
3492 key = PyList_GetItem(keys, pos);
3493 val = PyList_GetItem(vals, pos);
3494 if (!key || !val)
3495 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003496
Victor Stinner8c62be82010-05-06 00:08:46 +00003497 if (PyUnicode_FSConverter(key, &key2) == 0)
3498 goto error;
3499 if (PyUnicode_FSConverter(val, &val2) == 0) {
3500 Py_DECREF(key2);
3501 goto error;
3502 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003503
3504#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003505 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3506 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003507#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003508 k = PyBytes_AsString(key2);
3509 v = PyBytes_AsString(val2);
3510 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003511
Victor Stinner8c62be82010-05-06 00:08:46 +00003512 p = PyMem_NEW(char, len);
3513 if (p == NULL) {
3514 PyErr_NoMemory();
3515 Py_DECREF(key2);
3516 Py_DECREF(val2);
3517 goto error;
3518 }
3519 PyOS_snprintf(p, len, "%s=%s", k, v);
3520 envlist[envc++] = p;
3521 Py_DECREF(key2);
3522 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003523#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003524 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003525#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003526 }
3527 Py_DECREF(vals);
3528 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003529
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 envlist[envc] = 0;
3531 *envc_ptr = envc;
3532 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003533
3534error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003535 Py_XDECREF(keys);
3536 Py_XDECREF(vals);
3537 while (--envc >= 0)
3538 PyMem_DEL(envlist[envc]);
3539 PyMem_DEL(envlist);
3540 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003541}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003543PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003544"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003545Execute a path with arguments and environment, replacing current process.\n\
3546\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003547 path: path of executable file\n\
3548 args: tuple or list of arguments\n\
3549 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003550
Barry Warsaw53699e91996-12-10 23:23:01 +00003551static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003552posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003553{
Victor Stinner8c62be82010-05-06 00:08:46 +00003554 PyObject *opath;
3555 char *path;
3556 PyObject *argv, *env;
3557 char **argvlist;
3558 char **envlist;
3559 Py_ssize_t i, argc, envc;
3560 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3561 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003562
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 /* execve has three arguments: (path, argv, env), where
3564 argv is a list or tuple of strings and env is a dictionary
3565 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003566
Victor Stinner8c62be82010-05-06 00:08:46 +00003567 if (!PyArg_ParseTuple(args, "O&OO:execve",
3568 PyUnicode_FSConverter,
3569 &opath, &argv, &env))
3570 return NULL;
3571 path = PyBytes_AsString(opath);
3572 if (PyList_Check(argv)) {
3573 argc = PyList_Size(argv);
3574 getitem = PyList_GetItem;
3575 }
3576 else if (PyTuple_Check(argv)) {
3577 argc = PyTuple_Size(argv);
3578 getitem = PyTuple_GetItem;
3579 }
3580 else {
3581 PyErr_SetString(PyExc_TypeError,
3582 "execve() arg 2 must be a tuple or list");
3583 goto fail_0;
3584 }
3585 if (!PyMapping_Check(env)) {
3586 PyErr_SetString(PyExc_TypeError,
3587 "execve() arg 3 must be a mapping object");
3588 goto fail_0;
3589 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003590
Victor Stinner8c62be82010-05-06 00:08:46 +00003591 argvlist = PyMem_NEW(char *, argc+1);
3592 if (argvlist == NULL) {
3593 PyErr_NoMemory();
3594 goto fail_0;
3595 }
3596 for (i = 0; i < argc; i++) {
3597 if (!fsconvert_strdup((*getitem)(argv, i),
3598 &argvlist[i]))
3599 {
3600 lastarg = i;
3601 goto fail_1;
3602 }
3603 }
3604 lastarg = argc;
3605 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003606
Victor Stinner8c62be82010-05-06 00:08:46 +00003607 envlist = parse_envlist(env, &envc);
3608 if (envlist == NULL)
3609 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003610
Victor Stinner8c62be82010-05-06 00:08:46 +00003611 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003612
Victor Stinner8c62be82010-05-06 00:08:46 +00003613 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003614
Victor Stinner8c62be82010-05-06 00:08:46 +00003615 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003616
Victor Stinner8c62be82010-05-06 00:08:46 +00003617 while (--envc >= 0)
3618 PyMem_DEL(envlist[envc]);
3619 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003620 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003621 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003622 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003623 Py_DECREF(opath);
3624 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003625}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003626#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003627
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003628
Guido van Rossuma1065681999-01-25 23:20:23 +00003629#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003630PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003631"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003632Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003633\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003634 mode: mode of process creation\n\
3635 path: path of executable file\n\
3636 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003637
3638static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003639posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003640{
Victor Stinner8c62be82010-05-06 00:08:46 +00003641 PyObject *opath;
3642 char *path;
3643 PyObject *argv;
3644 char **argvlist;
3645 int mode, i;
3646 Py_ssize_t argc;
3647 Py_intptr_t spawnval;
3648 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003649
Victor Stinner8c62be82010-05-06 00:08:46 +00003650 /* spawnv has three arguments: (mode, path, argv), where
3651 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003652
Victor Stinner8c62be82010-05-06 00:08:46 +00003653 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3654 PyUnicode_FSConverter,
3655 &opath, &argv))
3656 return NULL;
3657 path = PyBytes_AsString(opath);
3658 if (PyList_Check(argv)) {
3659 argc = PyList_Size(argv);
3660 getitem = PyList_GetItem;
3661 }
3662 else if (PyTuple_Check(argv)) {
3663 argc = PyTuple_Size(argv);
3664 getitem = PyTuple_GetItem;
3665 }
3666 else {
3667 PyErr_SetString(PyExc_TypeError,
3668 "spawnv() arg 2 must be a tuple or list");
3669 Py_DECREF(opath);
3670 return NULL;
3671 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003672
Victor Stinner8c62be82010-05-06 00:08:46 +00003673 argvlist = PyMem_NEW(char *, argc+1);
3674 if (argvlist == NULL) {
3675 Py_DECREF(opath);
3676 return PyErr_NoMemory();
3677 }
3678 for (i = 0; i < argc; i++) {
3679 if (!fsconvert_strdup((*getitem)(argv, i),
3680 &argvlist[i])) {
3681 free_string_array(argvlist, i);
3682 PyErr_SetString(
3683 PyExc_TypeError,
3684 "spawnv() arg 2 must contain only strings");
3685 Py_DECREF(opath);
3686 return NULL;
3687 }
3688 }
3689 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003690
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003691#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003692 Py_BEGIN_ALLOW_THREADS
3693 spawnval = spawnv(mode, path, argvlist);
3694 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003695#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003696 if (mode == _OLD_P_OVERLAY)
3697 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003698
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 Py_BEGIN_ALLOW_THREADS
3700 spawnval = _spawnv(mode, path, argvlist);
3701 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003702#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003703
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 free_string_array(argvlist, argc);
3705 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003706
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 if (spawnval == -1)
3708 return posix_error();
3709 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003710#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003711 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003712#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003713 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003714#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003715}
3716
3717
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003718PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003719"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003720Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003721\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003722 mode: mode of process creation\n\
3723 path: path of executable file\n\
3724 args: tuple or list of arguments\n\
3725 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003726
3727static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003728posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003729{
Victor Stinner8c62be82010-05-06 00:08:46 +00003730 PyObject *opath;
3731 char *path;
3732 PyObject *argv, *env;
3733 char **argvlist;
3734 char **envlist;
3735 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003736 int mode;
3737 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 Py_intptr_t spawnval;
3739 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3740 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003741
Victor Stinner8c62be82010-05-06 00:08:46 +00003742 /* spawnve has four arguments: (mode, path, argv, env), where
3743 argv is a list or tuple of strings and env is a dictionary
3744 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003745
Victor Stinner8c62be82010-05-06 00:08:46 +00003746 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3747 PyUnicode_FSConverter,
3748 &opath, &argv, &env))
3749 return NULL;
3750 path = PyBytes_AsString(opath);
3751 if (PyList_Check(argv)) {
3752 argc = PyList_Size(argv);
3753 getitem = PyList_GetItem;
3754 }
3755 else if (PyTuple_Check(argv)) {
3756 argc = PyTuple_Size(argv);
3757 getitem = PyTuple_GetItem;
3758 }
3759 else {
3760 PyErr_SetString(PyExc_TypeError,
3761 "spawnve() arg 2 must be a tuple or list");
3762 goto fail_0;
3763 }
3764 if (!PyMapping_Check(env)) {
3765 PyErr_SetString(PyExc_TypeError,
3766 "spawnve() arg 3 must be a mapping object");
3767 goto fail_0;
3768 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003769
Victor Stinner8c62be82010-05-06 00:08:46 +00003770 argvlist = PyMem_NEW(char *, argc+1);
3771 if (argvlist == NULL) {
3772 PyErr_NoMemory();
3773 goto fail_0;
3774 }
3775 for (i = 0; i < argc; i++) {
3776 if (!fsconvert_strdup((*getitem)(argv, i),
3777 &argvlist[i]))
3778 {
3779 lastarg = i;
3780 goto fail_1;
3781 }
3782 }
3783 lastarg = argc;
3784 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003785
Victor Stinner8c62be82010-05-06 00:08:46 +00003786 envlist = parse_envlist(env, &envc);
3787 if (envlist == NULL)
3788 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003789
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003790#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003791 Py_BEGIN_ALLOW_THREADS
3792 spawnval = spawnve(mode, path, argvlist, envlist);
3793 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003794#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003795 if (mode == _OLD_P_OVERLAY)
3796 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003797
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 Py_BEGIN_ALLOW_THREADS
3799 spawnval = _spawnve(mode, path, argvlist, envlist);
3800 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003801#endif
Tim Peters25059d32001-12-07 20:35:43 +00003802
Victor Stinner8c62be82010-05-06 00:08:46 +00003803 if (spawnval == -1)
3804 (void) posix_error();
3805 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003806#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003808#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003809 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003810#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003811
Victor Stinner8c62be82010-05-06 00:08:46 +00003812 while (--envc >= 0)
3813 PyMem_DEL(envlist[envc]);
3814 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003815 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003816 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003817 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003818 Py_DECREF(opath);
3819 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003820}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003821
3822/* OS/2 supports spawnvp & spawnvpe natively */
3823#if defined(PYOS_OS2)
3824PyDoc_STRVAR(posix_spawnvp__doc__,
3825"spawnvp(mode, file, args)\n\n\
3826Execute the program 'file' in a new process, using the environment\n\
3827search path to find the file.\n\
3828\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003829 mode: mode of process creation\n\
3830 file: executable file name\n\
3831 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003832
3833static PyObject *
3834posix_spawnvp(PyObject *self, PyObject *args)
3835{
Victor Stinner8c62be82010-05-06 00:08:46 +00003836 PyObject *opath;
3837 char *path;
3838 PyObject *argv;
3839 char **argvlist;
3840 int mode, i, argc;
3841 Py_intptr_t spawnval;
3842 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003843
Victor Stinner8c62be82010-05-06 00:08:46 +00003844 /* spawnvp has three arguments: (mode, path, argv), where
3845 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003846
Victor Stinner8c62be82010-05-06 00:08:46 +00003847 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3848 PyUnicode_FSConverter,
3849 &opath, &argv))
3850 return NULL;
3851 path = PyBytes_AsString(opath);
3852 if (PyList_Check(argv)) {
3853 argc = PyList_Size(argv);
3854 getitem = PyList_GetItem;
3855 }
3856 else if (PyTuple_Check(argv)) {
3857 argc = PyTuple_Size(argv);
3858 getitem = PyTuple_GetItem;
3859 }
3860 else {
3861 PyErr_SetString(PyExc_TypeError,
3862 "spawnvp() arg 2 must be a tuple or list");
3863 Py_DECREF(opath);
3864 return NULL;
3865 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003866
Victor Stinner8c62be82010-05-06 00:08:46 +00003867 argvlist = PyMem_NEW(char *, argc+1);
3868 if (argvlist == NULL) {
3869 Py_DECREF(opath);
3870 return PyErr_NoMemory();
3871 }
3872 for (i = 0; i < argc; i++) {
3873 if (!fsconvert_strdup((*getitem)(argv, i),
3874 &argvlist[i])) {
3875 free_string_array(argvlist, i);
3876 PyErr_SetString(
3877 PyExc_TypeError,
3878 "spawnvp() arg 2 must contain only strings");
3879 Py_DECREF(opath);
3880 return NULL;
3881 }
3882 }
3883 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003884
Victor Stinner8c62be82010-05-06 00:08:46 +00003885 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003886#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003887 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003888#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003889 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003890#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003891 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003892
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 free_string_array(argvlist, argc);
3894 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003895
Victor Stinner8c62be82010-05-06 00:08:46 +00003896 if (spawnval == -1)
3897 return posix_error();
3898 else
3899 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003900}
3901
3902
3903PyDoc_STRVAR(posix_spawnvpe__doc__,
3904"spawnvpe(mode, file, args, env)\n\n\
3905Execute the program 'file' in a new process, using the environment\n\
3906search path to find the file.\n\
3907\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003908 mode: mode of process creation\n\
3909 file: executable file name\n\
3910 args: tuple or list of arguments\n\
3911 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003912
3913static PyObject *
3914posix_spawnvpe(PyObject *self, PyObject *args)
3915{
Victor Stinner8c62be82010-05-06 00:08:46 +00003916 PyObject *opath
3917 char *path;
3918 PyObject *argv, *env;
3919 char **argvlist;
3920 char **envlist;
3921 PyObject *res=NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00003922 int mode;
3923 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00003924 Py_intptr_t spawnval;
3925 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3926 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003927
Victor Stinner8c62be82010-05-06 00:08:46 +00003928 /* spawnvpe has four arguments: (mode, path, argv, env), where
3929 argv is a list or tuple of strings and env is a dictionary
3930 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003931
Victor Stinner8c62be82010-05-06 00:08:46 +00003932 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3933 PyUnicode_FSConverter,
3934 &opath, &argv, &env))
3935 return NULL;
3936 path = PyBytes_AsString(opath);
3937 if (PyList_Check(argv)) {
3938 argc = PyList_Size(argv);
3939 getitem = PyList_GetItem;
3940 }
3941 else if (PyTuple_Check(argv)) {
3942 argc = PyTuple_Size(argv);
3943 getitem = PyTuple_GetItem;
3944 }
3945 else {
3946 PyErr_SetString(PyExc_TypeError,
3947 "spawnvpe() arg 2 must be a tuple or list");
3948 goto fail_0;
3949 }
3950 if (!PyMapping_Check(env)) {
3951 PyErr_SetString(PyExc_TypeError,
3952 "spawnvpe() arg 3 must be a mapping object");
3953 goto fail_0;
3954 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003955
Victor Stinner8c62be82010-05-06 00:08:46 +00003956 argvlist = PyMem_NEW(char *, argc+1);
3957 if (argvlist == NULL) {
3958 PyErr_NoMemory();
3959 goto fail_0;
3960 }
3961 for (i = 0; i < argc; i++) {
3962 if (!fsconvert_strdup((*getitem)(argv, i),
3963 &argvlist[i]))
3964 {
3965 lastarg = i;
3966 goto fail_1;
3967 }
3968 }
3969 lastarg = argc;
3970 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003971
Victor Stinner8c62be82010-05-06 00:08:46 +00003972 envlist = parse_envlist(env, &envc);
3973 if (envlist == NULL)
3974 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003975
Victor Stinner8c62be82010-05-06 00:08:46 +00003976 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003977#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003978 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003979#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003980 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003981#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003982 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003983
Victor Stinner8c62be82010-05-06 00:08:46 +00003984 if (spawnval == -1)
3985 (void) posix_error();
3986 else
3987 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003988
Victor Stinner8c62be82010-05-06 00:08:46 +00003989 while (--envc >= 0)
3990 PyMem_DEL(envlist[envc]);
3991 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003992 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003993 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003994 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 Py_DECREF(opath);
3996 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003997}
3998#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003999#endif /* HAVE_SPAWNV */
4000
4001
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004002#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004003PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004004"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004005Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
4006\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004007Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004008
4009static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004010posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004011{
Victor Stinner8c62be82010-05-06 00:08:46 +00004012 pid_t pid;
4013 int result = 0;
4014 _PyImport_AcquireLock();
4015 pid = fork1();
4016 if (pid == 0) {
4017 /* child: this clobbers and resets the import lock. */
4018 PyOS_AfterFork();
4019 } else {
4020 /* parent: release the import lock. */
4021 result = _PyImport_ReleaseLock();
4022 }
4023 if (pid == -1)
4024 return posix_error();
4025 if (result < 0) {
4026 /* Don't clobber the OSError if the fork failed. */
4027 PyErr_SetString(PyExc_RuntimeError,
4028 "not holding the import lock");
4029 return NULL;
4030 }
4031 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004032}
4033#endif
4034
4035
Guido van Rossumad0ee831995-03-01 10:34:45 +00004036#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004037PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004038"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004039Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004040Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004041
Barry Warsaw53699e91996-12-10 23:23:01 +00004042static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004043posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004044{
Victor Stinner8c62be82010-05-06 00:08:46 +00004045 pid_t pid;
4046 int result = 0;
4047 _PyImport_AcquireLock();
4048 pid = fork();
4049 if (pid == 0) {
4050 /* child: this clobbers and resets the import lock. */
4051 PyOS_AfterFork();
4052 } else {
4053 /* parent: release the import lock. */
4054 result = _PyImport_ReleaseLock();
4055 }
4056 if (pid == -1)
4057 return posix_error();
4058 if (result < 0) {
4059 /* Don't clobber the OSError if the fork failed. */
4060 PyErr_SetString(PyExc_RuntimeError,
4061 "not holding the import lock");
4062 return NULL;
4063 }
4064 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00004065}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004066#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004067
Neal Norwitzb59798b2003-03-21 01:43:31 +00004068/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00004069/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
4070#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00004071#define DEV_PTY_FILE "/dev/ptc"
4072#define HAVE_DEV_PTMX
4073#else
4074#define DEV_PTY_FILE "/dev/ptmx"
4075#endif
4076
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004077#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004078#ifdef HAVE_PTY_H
4079#include <pty.h>
4080#else
4081#ifdef HAVE_LIBUTIL_H
4082#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004083#else
4084#ifdef HAVE_UTIL_H
4085#include <util.h>
4086#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004087#endif /* HAVE_LIBUTIL_H */
4088#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004089#ifdef HAVE_STROPTS_H
4090#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004091#endif
4092#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004093
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004094#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004095PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004096"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004097Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004098
4099static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004100posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004101{
Victor Stinner8c62be82010-05-06 00:08:46 +00004102 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004103#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004104 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004105#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004106#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004107 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004108#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004109 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004110#endif
4111#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004112
Thomas Wouters70c21a12000-07-14 14:28:33 +00004113#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004114 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4115 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004116#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004117 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4118 if (slave_name == NULL)
4119 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004120
Victor Stinner8c62be82010-05-06 00:08:46 +00004121 slave_fd = open(slave_name, O_RDWR);
4122 if (slave_fd < 0)
4123 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004124#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004125 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4126 if (master_fd < 0)
4127 return posix_error();
4128 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4129 /* change permission of slave */
4130 if (grantpt(master_fd) < 0) {
4131 PyOS_setsig(SIGCHLD, sig_saved);
4132 return posix_error();
4133 }
4134 /* unlock slave */
4135 if (unlockpt(master_fd) < 0) {
4136 PyOS_setsig(SIGCHLD, sig_saved);
4137 return posix_error();
4138 }
4139 PyOS_setsig(SIGCHLD, sig_saved);
4140 slave_name = ptsname(master_fd); /* get name of slave */
4141 if (slave_name == NULL)
4142 return posix_error();
4143 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4144 if (slave_fd < 0)
4145 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004146#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004147 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4148 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004149#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004150 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004151#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004152#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004153#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004154
Victor Stinner8c62be82010-05-06 00:08:46 +00004155 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004156
Fred Drake8cef4cf2000-06-28 16:40:38 +00004157}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004158#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004159
4160#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004161PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004162"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004163Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4164Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004165To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004166
4167static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004168posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004169{
Victor Stinner8c62be82010-05-06 00:08:46 +00004170 int master_fd = -1, result = 0;
4171 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004172
Victor Stinner8c62be82010-05-06 00:08:46 +00004173 _PyImport_AcquireLock();
4174 pid = forkpty(&master_fd, NULL, NULL, NULL);
4175 if (pid == 0) {
4176 /* child: this clobbers and resets the import lock. */
4177 PyOS_AfterFork();
4178 } else {
4179 /* parent: release the import lock. */
4180 result = _PyImport_ReleaseLock();
4181 }
4182 if (pid == -1)
4183 return posix_error();
4184 if (result < 0) {
4185 /* Don't clobber the OSError if the fork failed. */
4186 PyErr_SetString(PyExc_RuntimeError,
4187 "not holding the import lock");
4188 return NULL;
4189 }
4190 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004191}
4192#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004193
Guido van Rossumad0ee831995-03-01 10:34:45 +00004194#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004195PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004196"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004197Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004198
Barry Warsaw53699e91996-12-10 23:23:01 +00004199static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004200posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004201{
Victor Stinner8c62be82010-05-06 00:08:46 +00004202 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004203}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004204#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004206
Guido van Rossumad0ee831995-03-01 10:34:45 +00004207#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004208PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004209"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004210Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004211
Barry Warsaw53699e91996-12-10 23:23:01 +00004212static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004213posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004214{
Victor Stinner8c62be82010-05-06 00:08:46 +00004215 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004216}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004217#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004218
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004219
Guido van Rossumad0ee831995-03-01 10:34:45 +00004220#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004221PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004222"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004223Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004224
Barry Warsaw53699e91996-12-10 23:23:01 +00004225static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004226posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004227{
Victor Stinner8c62be82010-05-06 00:08:46 +00004228 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004229}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004230#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004231
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004232
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004233PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004234"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004235Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004236
Barry Warsaw53699e91996-12-10 23:23:01 +00004237static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004238posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004239{
Victor Stinner8c62be82010-05-06 00:08:46 +00004240 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004241}
4242
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004243
Fred Drakec9680921999-12-13 16:37:25 +00004244#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004245PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004246"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004247Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004248
4249static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004250posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004251{
4252 PyObject *result = NULL;
4253
Fred Drakec9680921999-12-13 16:37:25 +00004254#ifdef NGROUPS_MAX
4255#define MAX_GROUPS NGROUPS_MAX
4256#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004257 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004258#define MAX_GROUPS 64
4259#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004260 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004261
4262 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4263 * This is a helper variable to store the intermediate result when
4264 * that happens.
4265 *
4266 * To keep the code readable the OSX behaviour is unconditional,
4267 * according to the POSIX spec this should be safe on all unix-y
4268 * systems.
4269 */
4270 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004271 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004272
Victor Stinner8c62be82010-05-06 00:08:46 +00004273 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004274 if (n < 0) {
4275 if (errno == EINVAL) {
4276 n = getgroups(0, NULL);
4277 if (n == -1) {
4278 return posix_error();
4279 }
4280 if (n == 0) {
4281 /* Avoid malloc(0) */
4282 alt_grouplist = grouplist;
4283 } else {
4284 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4285 if (alt_grouplist == NULL) {
4286 errno = EINVAL;
4287 return posix_error();
4288 }
4289 n = getgroups(n, alt_grouplist);
4290 if (n == -1) {
4291 PyMem_Free(alt_grouplist);
4292 return posix_error();
4293 }
4294 }
4295 } else {
4296 return posix_error();
4297 }
4298 }
4299 result = PyList_New(n);
4300 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004301 int i;
4302 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004303 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004304 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00004305 Py_DECREF(result);
4306 result = NULL;
4307 break;
Fred Drakec9680921999-12-13 16:37:25 +00004308 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004309 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004310 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004311 }
4312
4313 if (alt_grouplist != grouplist) {
4314 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004315 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004316
Fred Drakec9680921999-12-13 16:37:25 +00004317 return result;
4318}
4319#endif
4320
Antoine Pitroub7572f02009-12-02 20:46:48 +00004321#ifdef HAVE_INITGROUPS
4322PyDoc_STRVAR(posix_initgroups__doc__,
4323"initgroups(username, gid) -> None\n\n\
4324Call the system initgroups() to initialize the group access list with all of\n\
4325the groups of which the specified username is a member, plus the specified\n\
4326group id.");
4327
4328static PyObject *
4329posix_initgroups(PyObject *self, PyObject *args)
4330{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004331 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004332 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004333 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004334 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004335
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004336 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4337 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004338 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004339 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004340
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004341 res = initgroups(username, (gid_t) gid);
4342 Py_DECREF(oname);
4343 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004344 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004345
Victor Stinner8c62be82010-05-06 00:08:46 +00004346 Py_INCREF(Py_None);
4347 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004348}
4349#endif
4350
Martin v. Löwis606edc12002-06-13 21:09:11 +00004351#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004352PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004353"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004354Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004355
4356static PyObject *
4357posix_getpgid(PyObject *self, PyObject *args)
4358{
Victor Stinner8c62be82010-05-06 00:08:46 +00004359 pid_t pid, pgid;
4360 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4361 return NULL;
4362 pgid = getpgid(pid);
4363 if (pgid < 0)
4364 return posix_error();
4365 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004366}
4367#endif /* HAVE_GETPGID */
4368
4369
Guido van Rossumb6775db1994-08-01 11:34:53 +00004370#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004371PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004372"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004373Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004374
Barry Warsaw53699e91996-12-10 23:23:01 +00004375static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004376posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004377{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004378#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004379 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004380#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004381 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004382#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004383}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004384#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004385
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004386
Guido van Rossumb6775db1994-08-01 11:34:53 +00004387#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004388PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004389"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004390Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004391
Barry Warsaw53699e91996-12-10 23:23:01 +00004392static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004393posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004394{
Guido van Rossum64933891994-10-20 21:56:42 +00004395#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004396 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004397#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004398 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004399#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004400 return posix_error();
4401 Py_INCREF(Py_None);
4402 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004403}
4404
Guido van Rossumb6775db1994-08-01 11:34:53 +00004405#endif /* HAVE_SETPGRP */
4406
Guido van Rossumad0ee831995-03-01 10:34:45 +00004407#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004408
4409#ifdef MS_WINDOWS
4410#include <tlhelp32.h>
4411
4412static PyObject*
4413win32_getppid()
4414{
4415 HANDLE snapshot;
4416 pid_t mypid;
4417 PyObject* result = NULL;
4418 BOOL have_record;
4419 PROCESSENTRY32 pe;
4420
4421 mypid = getpid(); /* This function never fails */
4422
4423 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
4424 if (snapshot == INVALID_HANDLE_VALUE)
4425 return PyErr_SetFromWindowsErr(GetLastError());
4426
4427 pe.dwSize = sizeof(pe);
4428 have_record = Process32First(snapshot, &pe);
4429 while (have_record) {
4430 if (mypid == (pid_t)pe.th32ProcessID) {
4431 /* We could cache the ulong value in a static variable. */
4432 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
4433 break;
4434 }
4435
4436 have_record = Process32Next(snapshot, &pe);
4437 }
4438
4439 /* If our loop exits and our pid was not found (result will be NULL)
4440 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
4441 * error anyway, so let's raise it. */
4442 if (!result)
4443 result = PyErr_SetFromWindowsErr(GetLastError());
4444
4445 CloseHandle(snapshot);
4446
4447 return result;
4448}
4449#endif /*MS_WINDOWS*/
4450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004451PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004452"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004453Return the parent's process id. If the parent process has already exited,\n\
4454Windows machines will still return its id; others systems will return the id\n\
4455of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004456
Barry Warsaw53699e91996-12-10 23:23:01 +00004457static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004458posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004459{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004460#ifdef MS_WINDOWS
4461 return win32_getppid();
4462#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004463 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00004464#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00004465}
4466#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004468
Fred Drake12c6e2d1999-12-14 21:25:03 +00004469#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004470PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004471"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004472Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004473
4474static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004475posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004476{
Victor Stinner8c62be82010-05-06 00:08:46 +00004477 PyObject *result = NULL;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004478#ifdef MS_WINDOWS
4479 wchar_t user_name[UNLEN + 1];
4480 DWORD num_chars = sizeof(user_name)/sizeof(user_name[0]);
4481
4482 if (GetUserNameW(user_name, &num_chars)) {
4483 /* num_chars is the number of unicode chars plus null terminator */
4484 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
4485 }
4486 else
4487 result = PyErr_SetFromWindowsErr(GetLastError());
4488#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004489 char *name;
4490 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004491
Victor Stinner8c62be82010-05-06 00:08:46 +00004492 errno = 0;
4493 name = getlogin();
4494 if (name == NULL) {
4495 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004496 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004497 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004498 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004499 }
4500 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004501 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004502 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004503#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00004504 return result;
4505}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00004506#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00004507
Guido van Rossumad0ee831995-03-01 10:34:45 +00004508#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004509PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004510"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004511Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004512
Barry Warsaw53699e91996-12-10 23:23:01 +00004513static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004514posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004515{
Victor Stinner8c62be82010-05-06 00:08:46 +00004516 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004517}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004518#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004519
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004520
Guido van Rossumad0ee831995-03-01 10:34:45 +00004521#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004522PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004523"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004524Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004525
Barry Warsaw53699e91996-12-10 23:23:01 +00004526static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004527posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004528{
Victor Stinner8c62be82010-05-06 00:08:46 +00004529 pid_t pid;
4530 int sig;
4531 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4532 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004533#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004534 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4535 APIRET rc;
4536 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004537 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004538
4539 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4540 APIRET rc;
4541 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004542 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004543
4544 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004545 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004546#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004547 if (kill(pid, sig) == -1)
4548 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004549#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004550 Py_INCREF(Py_None);
4551 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004552}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004553#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004554
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004555#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004556PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004557"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004558Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004559
4560static PyObject *
4561posix_killpg(PyObject *self, PyObject *args)
4562{
Victor Stinner8c62be82010-05-06 00:08:46 +00004563 int sig;
4564 pid_t pgid;
4565 /* XXX some man pages make the `pgid` parameter an int, others
4566 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4567 take the same type. Moreover, pid_t is always at least as wide as
4568 int (else compilation of this module fails), which is safe. */
4569 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4570 return NULL;
4571 if (killpg(pgid, sig) == -1)
4572 return posix_error();
4573 Py_INCREF(Py_None);
4574 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004575}
4576#endif
4577
Brian Curtineb24d742010-04-12 17:16:38 +00004578#ifdef MS_WINDOWS
4579PyDoc_STRVAR(win32_kill__doc__,
4580"kill(pid, sig)\n\n\
4581Kill a process with a signal.");
4582
4583static PyObject *
4584win32_kill(PyObject *self, PyObject *args)
4585{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004586 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004587 DWORD pid, sig, err;
4588 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004589
Victor Stinner8c62be82010-05-06 00:08:46 +00004590 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4591 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004592
Victor Stinner8c62be82010-05-06 00:08:46 +00004593 /* Console processes which share a common console can be sent CTRL+C or
4594 CTRL+BREAK events, provided they handle said events. */
4595 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4596 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4597 err = GetLastError();
4598 PyErr_SetFromWindowsErr(err);
4599 }
4600 else
4601 Py_RETURN_NONE;
4602 }
Brian Curtineb24d742010-04-12 17:16:38 +00004603
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4605 attempt to open and terminate the process. */
4606 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4607 if (handle == NULL) {
4608 err = GetLastError();
4609 return PyErr_SetFromWindowsErr(err);
4610 }
Brian Curtineb24d742010-04-12 17:16:38 +00004611
Victor Stinner8c62be82010-05-06 00:08:46 +00004612 if (TerminateProcess(handle, sig) == 0) {
4613 err = GetLastError();
4614 result = PyErr_SetFromWindowsErr(err);
4615 } else {
4616 Py_INCREF(Py_None);
4617 result = Py_None;
4618 }
Brian Curtineb24d742010-04-12 17:16:38 +00004619
Victor Stinner8c62be82010-05-06 00:08:46 +00004620 CloseHandle(handle);
4621 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004622}
4623#endif /* MS_WINDOWS */
4624
Guido van Rossumc0125471996-06-28 18:55:32 +00004625#ifdef HAVE_PLOCK
4626
4627#ifdef HAVE_SYS_LOCK_H
4628#include <sys/lock.h>
4629#endif
4630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004631PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004632"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004633Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004634
Barry Warsaw53699e91996-12-10 23:23:01 +00004635static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004636posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004637{
Victor Stinner8c62be82010-05-06 00:08:46 +00004638 int op;
4639 if (!PyArg_ParseTuple(args, "i:plock", &op))
4640 return NULL;
4641 if (plock(op) == -1)
4642 return posix_error();
4643 Py_INCREF(Py_None);
4644 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004645}
4646#endif
4647
Guido van Rossumb6775db1994-08-01 11:34:53 +00004648#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004649PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004650"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004651Set the current process's user id.");
4652
Barry Warsaw53699e91996-12-10 23:23:01 +00004653static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004654posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004655{
Victor Stinner8c62be82010-05-06 00:08:46 +00004656 long uid_arg;
4657 uid_t uid;
4658 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4659 return NULL;
4660 uid = uid_arg;
4661 if (uid != uid_arg) {
4662 PyErr_SetString(PyExc_OverflowError, "user id too big");
4663 return NULL;
4664 }
4665 if (setuid(uid) < 0)
4666 return posix_error();
4667 Py_INCREF(Py_None);
4668 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004669}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004670#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004671
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004672
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004673#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004674PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004675"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004676Set the current process's effective user id.");
4677
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004678static PyObject *
4679posix_seteuid (PyObject *self, PyObject *args)
4680{
Victor Stinner8c62be82010-05-06 00:08:46 +00004681 long euid_arg;
4682 uid_t euid;
4683 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4684 return NULL;
4685 euid = euid_arg;
4686 if (euid != euid_arg) {
4687 PyErr_SetString(PyExc_OverflowError, "user id too big");
4688 return NULL;
4689 }
4690 if (seteuid(euid) < 0) {
4691 return posix_error();
4692 } else {
4693 Py_INCREF(Py_None);
4694 return Py_None;
4695 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004696}
4697#endif /* HAVE_SETEUID */
4698
4699#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004700PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004701"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004702Set the current process's effective group id.");
4703
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004704static PyObject *
4705posix_setegid (PyObject *self, PyObject *args)
4706{
Victor Stinner8c62be82010-05-06 00:08:46 +00004707 long egid_arg;
4708 gid_t egid;
4709 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4710 return NULL;
4711 egid = egid_arg;
4712 if (egid != egid_arg) {
4713 PyErr_SetString(PyExc_OverflowError, "group id too big");
4714 return NULL;
4715 }
4716 if (setegid(egid) < 0) {
4717 return posix_error();
4718 } else {
4719 Py_INCREF(Py_None);
4720 return Py_None;
4721 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004722}
4723#endif /* HAVE_SETEGID */
4724
4725#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004726PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004727"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004728Set the current process's real and effective user ids.");
4729
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004730static PyObject *
4731posix_setreuid (PyObject *self, PyObject *args)
4732{
Victor Stinner8c62be82010-05-06 00:08:46 +00004733 long ruid_arg, euid_arg;
4734 uid_t ruid, euid;
4735 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4736 return NULL;
4737 if (ruid_arg == -1)
4738 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4739 else
4740 ruid = ruid_arg; /* otherwise, assign from our long */
4741 if (euid_arg == -1)
4742 euid = (uid_t)-1;
4743 else
4744 euid = euid_arg;
4745 if ((euid_arg != -1 && euid != euid_arg) ||
4746 (ruid_arg != -1 && ruid != ruid_arg)) {
4747 PyErr_SetString(PyExc_OverflowError, "user id too big");
4748 return NULL;
4749 }
4750 if (setreuid(ruid, euid) < 0) {
4751 return posix_error();
4752 } else {
4753 Py_INCREF(Py_None);
4754 return Py_None;
4755 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004756}
4757#endif /* HAVE_SETREUID */
4758
4759#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004760PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004761"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004762Set the current process's real and effective group ids.");
4763
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004764static PyObject *
4765posix_setregid (PyObject *self, PyObject *args)
4766{
Victor Stinner8c62be82010-05-06 00:08:46 +00004767 long rgid_arg, egid_arg;
4768 gid_t rgid, egid;
4769 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4770 return NULL;
4771 if (rgid_arg == -1)
4772 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4773 else
4774 rgid = rgid_arg; /* otherwise, assign from our long */
4775 if (egid_arg == -1)
4776 egid = (gid_t)-1;
4777 else
4778 egid = egid_arg;
4779 if ((egid_arg != -1 && egid != egid_arg) ||
4780 (rgid_arg != -1 && rgid != rgid_arg)) {
4781 PyErr_SetString(PyExc_OverflowError, "group id too big");
4782 return NULL;
4783 }
4784 if (setregid(rgid, egid) < 0) {
4785 return posix_error();
4786 } else {
4787 Py_INCREF(Py_None);
4788 return Py_None;
4789 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004790}
4791#endif /* HAVE_SETREGID */
4792
Guido van Rossumb6775db1994-08-01 11:34:53 +00004793#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004794PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004795"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004796Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004797
Barry Warsaw53699e91996-12-10 23:23:01 +00004798static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004799posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004800{
Victor Stinner8c62be82010-05-06 00:08:46 +00004801 long gid_arg;
4802 gid_t gid;
4803 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4804 return NULL;
4805 gid = gid_arg;
4806 if (gid != gid_arg) {
4807 PyErr_SetString(PyExc_OverflowError, "group id too big");
4808 return NULL;
4809 }
4810 if (setgid(gid) < 0)
4811 return posix_error();
4812 Py_INCREF(Py_None);
4813 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004814}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004815#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004816
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004817#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004818PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004819"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004820Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004821
4822static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004823posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004824{
Victor Stinner8c62be82010-05-06 00:08:46 +00004825 int i, len;
4826 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004827
Victor Stinner8c62be82010-05-06 00:08:46 +00004828 if (!PySequence_Check(groups)) {
4829 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4830 return NULL;
4831 }
4832 len = PySequence_Size(groups);
4833 if (len > MAX_GROUPS) {
4834 PyErr_SetString(PyExc_ValueError, "too many groups");
4835 return NULL;
4836 }
4837 for(i = 0; i < len; i++) {
4838 PyObject *elem;
4839 elem = PySequence_GetItem(groups, i);
4840 if (!elem)
4841 return NULL;
4842 if (!PyLong_Check(elem)) {
4843 PyErr_SetString(PyExc_TypeError,
4844 "groups must be integers");
4845 Py_DECREF(elem);
4846 return NULL;
4847 } else {
4848 unsigned long x = PyLong_AsUnsignedLong(elem);
4849 if (PyErr_Occurred()) {
4850 PyErr_SetString(PyExc_TypeError,
4851 "group id too big");
4852 Py_DECREF(elem);
4853 return NULL;
4854 }
4855 grouplist[i] = x;
4856 /* read back the value to see if it fitted in gid_t */
4857 if (grouplist[i] != x) {
4858 PyErr_SetString(PyExc_TypeError,
4859 "group id too big");
4860 Py_DECREF(elem);
4861 return NULL;
4862 }
4863 }
4864 Py_DECREF(elem);
4865 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004866
Victor Stinner8c62be82010-05-06 00:08:46 +00004867 if (setgroups(len, grouplist) < 0)
4868 return posix_error();
4869 Py_INCREF(Py_None);
4870 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004871}
4872#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004873
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004874#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4875static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004876wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004877{
Victor Stinner8c62be82010-05-06 00:08:46 +00004878 PyObject *result;
4879 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004880
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 if (pid == -1)
4882 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004883
Victor Stinner8c62be82010-05-06 00:08:46 +00004884 if (struct_rusage == NULL) {
4885 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4886 if (m == NULL)
4887 return NULL;
4888 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4889 Py_DECREF(m);
4890 if (struct_rusage == NULL)
4891 return NULL;
4892 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004893
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4895 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4896 if (!result)
4897 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004898
4899#ifndef doubletime
4900#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4901#endif
4902
Victor Stinner8c62be82010-05-06 00:08:46 +00004903 PyStructSequence_SET_ITEM(result, 0,
4904 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4905 PyStructSequence_SET_ITEM(result, 1,
4906 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004907#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004908 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4909 SET_INT(result, 2, ru->ru_maxrss);
4910 SET_INT(result, 3, ru->ru_ixrss);
4911 SET_INT(result, 4, ru->ru_idrss);
4912 SET_INT(result, 5, ru->ru_isrss);
4913 SET_INT(result, 6, ru->ru_minflt);
4914 SET_INT(result, 7, ru->ru_majflt);
4915 SET_INT(result, 8, ru->ru_nswap);
4916 SET_INT(result, 9, ru->ru_inblock);
4917 SET_INT(result, 10, ru->ru_oublock);
4918 SET_INT(result, 11, ru->ru_msgsnd);
4919 SET_INT(result, 12, ru->ru_msgrcv);
4920 SET_INT(result, 13, ru->ru_nsignals);
4921 SET_INT(result, 14, ru->ru_nvcsw);
4922 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004923#undef SET_INT
4924
Victor Stinner8c62be82010-05-06 00:08:46 +00004925 if (PyErr_Occurred()) {
4926 Py_DECREF(result);
4927 return NULL;
4928 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004929
Victor Stinner8c62be82010-05-06 00:08:46 +00004930 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004931}
4932#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4933
4934#ifdef HAVE_WAIT3
4935PyDoc_STRVAR(posix_wait3__doc__,
4936"wait3(options) -> (pid, status, rusage)\n\n\
4937Wait for completion of a child process.");
4938
4939static PyObject *
4940posix_wait3(PyObject *self, PyObject *args)
4941{
Victor Stinner8c62be82010-05-06 00:08:46 +00004942 pid_t pid;
4943 int options;
4944 struct rusage ru;
4945 WAIT_TYPE status;
4946 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004947
Victor Stinner8c62be82010-05-06 00:08:46 +00004948 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4949 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004950
Victor Stinner8c62be82010-05-06 00:08:46 +00004951 Py_BEGIN_ALLOW_THREADS
4952 pid = wait3(&status, options, &ru);
4953 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004954
Victor Stinner8c62be82010-05-06 00:08:46 +00004955 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004956}
4957#endif /* HAVE_WAIT3 */
4958
4959#ifdef HAVE_WAIT4
4960PyDoc_STRVAR(posix_wait4__doc__,
4961"wait4(pid, options) -> (pid, status, rusage)\n\n\
4962Wait for completion of a given child process.");
4963
4964static PyObject *
4965posix_wait4(PyObject *self, PyObject *args)
4966{
Victor Stinner8c62be82010-05-06 00:08:46 +00004967 pid_t pid;
4968 int options;
4969 struct rusage ru;
4970 WAIT_TYPE status;
4971 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004972
Victor Stinner8c62be82010-05-06 00:08:46 +00004973 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4974 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004975
Victor Stinner8c62be82010-05-06 00:08:46 +00004976 Py_BEGIN_ALLOW_THREADS
4977 pid = wait4(pid, &status, options, &ru);
4978 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004979
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004981}
4982#endif /* HAVE_WAIT4 */
4983
Guido van Rossumb6775db1994-08-01 11:34:53 +00004984#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004985PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004986"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004987Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004988
Barry Warsaw53699e91996-12-10 23:23:01 +00004989static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004990posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004991{
Victor Stinner8c62be82010-05-06 00:08:46 +00004992 pid_t pid;
4993 int options;
4994 WAIT_TYPE status;
4995 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004996
Victor Stinner8c62be82010-05-06 00:08:46 +00004997 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4998 return NULL;
4999 Py_BEGIN_ALLOW_THREADS
5000 pid = waitpid(pid, &status, options);
5001 Py_END_ALLOW_THREADS
5002 if (pid == -1)
5003 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005004
Victor Stinner8c62be82010-05-06 00:08:46 +00005005 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00005006}
5007
Tim Petersab034fa2002-02-01 11:27:43 +00005008#elif defined(HAVE_CWAIT)
5009
5010/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005011PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005012"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005013"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00005014
5015static PyObject *
5016posix_waitpid(PyObject *self, PyObject *args)
5017{
Victor Stinner8c62be82010-05-06 00:08:46 +00005018 Py_intptr_t pid;
5019 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00005020
Victor Stinner8c62be82010-05-06 00:08:46 +00005021 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
5022 return NULL;
5023 Py_BEGIN_ALLOW_THREADS
5024 pid = _cwait(&status, pid, options);
5025 Py_END_ALLOW_THREADS
5026 if (pid == -1)
5027 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005028
Victor Stinner8c62be82010-05-06 00:08:46 +00005029 /* shift the status left a byte so this is more like the POSIX waitpid */
5030 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00005031}
5032#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005033
Guido van Rossumad0ee831995-03-01 10:34:45 +00005034#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005035PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005036"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005037Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005038
Barry Warsaw53699e91996-12-10 23:23:01 +00005039static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005040posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00005041{
Victor Stinner8c62be82010-05-06 00:08:46 +00005042 pid_t pid;
5043 WAIT_TYPE status;
5044 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00005045
Victor Stinner8c62be82010-05-06 00:08:46 +00005046 Py_BEGIN_ALLOW_THREADS
5047 pid = wait(&status);
5048 Py_END_ALLOW_THREADS
5049 if (pid == -1)
5050 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00005051
Victor Stinner8c62be82010-05-06 00:08:46 +00005052 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00005053}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005054#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005055
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005056
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005057PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005058"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005059Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005060
Barry Warsaw53699e91996-12-10 23:23:01 +00005061static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005062posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005063{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005064#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00005065 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00005066#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005067#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00005068 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
5069 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005070#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005071 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005072#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00005073#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005074}
5075
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005076
Guido van Rossumb6775db1994-08-01 11:34:53 +00005077#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005078PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005079"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005080Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005081
Barry Warsaw53699e91996-12-10 23:23:01 +00005082static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005083posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005084{
Victor Stinner8c62be82010-05-06 00:08:46 +00005085 PyObject* v;
5086 char buf[MAXPATHLEN];
5087 PyObject *opath;
5088 char *path;
5089 int n;
5090 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00005091
Victor Stinner8c62be82010-05-06 00:08:46 +00005092 if (!PyArg_ParseTuple(args, "O&:readlink",
5093 PyUnicode_FSConverter, &opath))
5094 return NULL;
5095 path = PyBytes_AsString(opath);
5096 v = PySequence_GetItem(args, 0);
5097 if (v == NULL) {
5098 Py_DECREF(opath);
5099 return NULL;
5100 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005101
Victor Stinner8c62be82010-05-06 00:08:46 +00005102 if (PyUnicode_Check(v)) {
5103 arg_is_unicode = 1;
5104 }
5105 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005106
Victor Stinner8c62be82010-05-06 00:08:46 +00005107 Py_BEGIN_ALLOW_THREADS
5108 n = readlink(path, buf, (int) sizeof buf);
5109 Py_END_ALLOW_THREADS
5110 if (n < 0)
5111 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00005112
Victor Stinner8c62be82010-05-06 00:08:46 +00005113 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00005114 if (arg_is_unicode)
5115 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
5116 else
5117 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005118}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005119#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005120
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005121
Brian Curtin52173d42010-12-02 18:29:18 +00005122#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005123PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005124"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00005125Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005126
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005127static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005128posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005129{
Victor Stinner8c62be82010-05-06 00:08:46 +00005130 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005131}
5132#endif /* HAVE_SYMLINK */
5133
Brian Curtind40e6f72010-07-08 21:39:08 +00005134#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
5135
5136PyDoc_STRVAR(win_readlink__doc__,
5137"readlink(path) -> path\n\n\
5138Return a string representing the path to which the symbolic link points.");
5139
Brian Curtind40e6f72010-07-08 21:39:08 +00005140/* Windows readlink implementation */
5141static PyObject *
5142win_readlink(PyObject *self, PyObject *args)
5143{
5144 wchar_t *path;
5145 DWORD n_bytes_returned;
5146 DWORD io_result;
5147 PyObject *result;
5148 HANDLE reparse_point_handle;
5149
5150 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5151 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5152 wchar_t *print_name;
5153
5154 if (!PyArg_ParseTuple(args,
5155 "u:readlink",
5156 &path))
5157 return NULL;
5158
5159 /* First get a handle to the reparse point */
5160 Py_BEGIN_ALLOW_THREADS
5161 reparse_point_handle = CreateFileW(
5162 path,
5163 0,
5164 0,
5165 0,
5166 OPEN_EXISTING,
5167 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5168 0);
5169 Py_END_ALLOW_THREADS
5170
5171 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5172 {
5173 return win32_error_unicode("readlink", path);
5174 }
5175
5176 Py_BEGIN_ALLOW_THREADS
5177 /* New call DeviceIoControl to read the reparse point */
5178 io_result = DeviceIoControl(
5179 reparse_point_handle,
5180 FSCTL_GET_REPARSE_POINT,
5181 0, 0, /* in buffer */
5182 target_buffer, sizeof(target_buffer),
5183 &n_bytes_returned,
5184 0 /* we're not using OVERLAPPED_IO */
5185 );
5186 CloseHandle(reparse_point_handle);
5187 Py_END_ALLOW_THREADS
5188
5189 if (io_result==0)
5190 {
5191 return win32_error_unicode("readlink", path);
5192 }
5193
5194 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5195 {
5196 PyErr_SetString(PyExc_ValueError,
5197 "not a symbolic link");
5198 return NULL;
5199 }
Brian Curtin74e45612010-07-09 15:58:59 +00005200 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5201 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5202
5203 result = PyUnicode_FromWideChar(print_name,
5204 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005205 return result;
5206}
5207
5208#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5209
Brian Curtin52173d42010-12-02 18:29:18 +00005210#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtind40e6f72010-07-08 21:39:08 +00005211
5212/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5213static int has_CreateSymbolicLinkW = 0;
5214static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5215static int
5216check_CreateSymbolicLinkW()
5217{
5218 HINSTANCE hKernel32;
5219 /* only recheck */
5220 if (has_CreateSymbolicLinkW)
5221 return has_CreateSymbolicLinkW;
5222 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005223 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5224 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005225 if (Py_CreateSymbolicLinkW)
5226 has_CreateSymbolicLinkW = 1;
5227 return has_CreateSymbolicLinkW;
5228}
5229
5230PyDoc_STRVAR(win_symlink__doc__,
5231"symlink(src, dst, target_is_directory=False)\n\n\
5232Create a symbolic link pointing to src named dst.\n\
5233target_is_directory is required if the target is to be interpreted as\n\
5234a directory.\n\
5235This function requires Windows 6.0 or greater, and raises a\n\
5236NotImplementedError otherwise.");
5237
5238static PyObject *
5239win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5240{
5241 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5242 PyObject *src, *dest;
5243 int target_is_directory = 0;
5244 DWORD res;
5245 WIN32_FILE_ATTRIBUTE_DATA src_info;
5246
5247 if (!check_CreateSymbolicLinkW())
5248 {
5249 /* raise NotImplementedError */
5250 return PyErr_Format(PyExc_NotImplementedError,
5251 "CreateSymbolicLinkW not found");
5252 }
5253 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5254 kwlist, &src, &dest, &target_is_directory))
5255 return NULL;
Brian Curtin3b4499c2010-12-28 14:31:47 +00005256
5257 if (win32_can_symlink == 0)
5258 return PyErr_Format(PyExc_OSError, "symbolic link privilege not held");
5259
Brian Curtind40e6f72010-07-08 21:39:08 +00005260 if (!convert_to_unicode(&src)) { return NULL; }
5261 if (!convert_to_unicode(&dest)) {
5262 Py_DECREF(src);
5263 return NULL;
5264 }
5265
5266 /* if src is a directory, ensure target_is_directory==1 */
5267 if(
5268 GetFileAttributesExW(
5269 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5270 ))
5271 {
5272 target_is_directory = target_is_directory ||
5273 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5274 }
5275
5276 Py_BEGIN_ALLOW_THREADS
5277 res = Py_CreateSymbolicLinkW(
5278 PyUnicode_AsUnicode(dest),
5279 PyUnicode_AsUnicode(src),
5280 target_is_directory);
5281 Py_END_ALLOW_THREADS
5282 Py_DECREF(src);
5283 Py_DECREF(dest);
5284 if (!res)
5285 {
5286 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5287 }
5288
5289 Py_INCREF(Py_None);
5290 return Py_None;
5291}
Brian Curtin52173d42010-12-02 18:29:18 +00005292#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005293
5294#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005295#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5296static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005297system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005298{
5299 ULONG value = 0;
5300
5301 Py_BEGIN_ALLOW_THREADS
5302 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5303 Py_END_ALLOW_THREADS
5304
5305 return value;
5306}
5307
5308static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005309posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005310{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005311 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005312 return Py_BuildValue("ddddd",
5313 (double)0 /* t.tms_utime / HZ */,
5314 (double)0 /* t.tms_stime / HZ */,
5315 (double)0 /* t.tms_cutime / HZ */,
5316 (double)0 /* t.tms_cstime / HZ */,
5317 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005318}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005319#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005320#define NEED_TICKS_PER_SECOND
5321static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005322static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005323posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005324{
Victor Stinner8c62be82010-05-06 00:08:46 +00005325 struct tms t;
5326 clock_t c;
5327 errno = 0;
5328 c = times(&t);
5329 if (c == (clock_t) -1)
5330 return posix_error();
5331 return Py_BuildValue("ddddd",
5332 (double)t.tms_utime / ticks_per_second,
5333 (double)t.tms_stime / ticks_per_second,
5334 (double)t.tms_cutime / ticks_per_second,
5335 (double)t.tms_cstime / ticks_per_second,
5336 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005337}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005338#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005339#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005340
5341
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005342#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005343#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005344static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005345posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005346{
Victor Stinner8c62be82010-05-06 00:08:46 +00005347 FILETIME create, exit, kernel, user;
5348 HANDLE hProc;
5349 hProc = GetCurrentProcess();
5350 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5351 /* The fields of a FILETIME structure are the hi and lo part
5352 of a 64-bit value expressed in 100 nanosecond units.
5353 1e7 is one second in such units; 1e-7 the inverse.
5354 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5355 */
5356 return Py_BuildValue(
5357 "ddddd",
5358 (double)(user.dwHighDateTime*429.4967296 +
5359 user.dwLowDateTime*1e-7),
5360 (double)(kernel.dwHighDateTime*429.4967296 +
5361 kernel.dwLowDateTime*1e-7),
5362 (double)0,
5363 (double)0,
5364 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005365}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005366#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005367
5368#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005369PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005370"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005371Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005372#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005373
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005374
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005375#ifdef HAVE_GETSID
5376PyDoc_STRVAR(posix_getsid__doc__,
5377"getsid(pid) -> sid\n\n\
5378Call the system call getsid().");
5379
5380static PyObject *
5381posix_getsid(PyObject *self, PyObject *args)
5382{
Victor Stinner8c62be82010-05-06 00:08:46 +00005383 pid_t pid;
5384 int sid;
5385 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5386 return NULL;
5387 sid = getsid(pid);
5388 if (sid < 0)
5389 return posix_error();
5390 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005391}
5392#endif /* HAVE_GETSID */
5393
5394
Guido van Rossumb6775db1994-08-01 11:34:53 +00005395#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005396PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005397"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005398Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005399
Barry Warsaw53699e91996-12-10 23:23:01 +00005400static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005401posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005402{
Victor Stinner8c62be82010-05-06 00:08:46 +00005403 if (setsid() < 0)
5404 return posix_error();
5405 Py_INCREF(Py_None);
5406 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005407}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005408#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005409
Guido van Rossumb6775db1994-08-01 11:34:53 +00005410#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005411PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005412"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005413Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005414
Barry Warsaw53699e91996-12-10 23:23:01 +00005415static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005416posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005417{
Victor Stinner8c62be82010-05-06 00:08:46 +00005418 pid_t pid;
5419 int pgrp;
5420 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5421 return NULL;
5422 if (setpgid(pid, pgrp) < 0)
5423 return posix_error();
5424 Py_INCREF(Py_None);
5425 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005426}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005427#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005428
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005429
Guido van Rossumb6775db1994-08-01 11:34:53 +00005430#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005431PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005432"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005433Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005434
Barry Warsaw53699e91996-12-10 23:23:01 +00005435static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005436posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005437{
Victor Stinner8c62be82010-05-06 00:08:46 +00005438 int fd;
5439 pid_t pgid;
5440 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5441 return NULL;
5442 pgid = tcgetpgrp(fd);
5443 if (pgid < 0)
5444 return posix_error();
5445 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005446}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005447#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005448
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005449
Guido van Rossumb6775db1994-08-01 11:34:53 +00005450#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005451PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005452"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005453Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005454
Barry Warsaw53699e91996-12-10 23:23:01 +00005455static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005456posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005457{
Victor Stinner8c62be82010-05-06 00:08:46 +00005458 int fd;
5459 pid_t pgid;
5460 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5461 return NULL;
5462 if (tcsetpgrp(fd, pgid) < 0)
5463 return posix_error();
5464 Py_INCREF(Py_None);
5465 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005466}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005467#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005468
Guido van Rossum687dd131993-05-17 08:34:16 +00005469/* Functions acting on file descriptors */
5470
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005471PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005472"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005473Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005474
Barry Warsaw53699e91996-12-10 23:23:01 +00005475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005476posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005477{
Victor Stinner8c62be82010-05-06 00:08:46 +00005478 PyObject *ofile;
5479 char *file;
5480 int flag;
5481 int mode = 0777;
5482 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005483
5484#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005485 PyUnicodeObject *po;
5486 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5487 Py_BEGIN_ALLOW_THREADS
5488 /* PyUnicode_AS_UNICODE OK without thread
5489 lock as it is a simple dereference. */
5490 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5491 Py_END_ALLOW_THREADS
5492 if (fd < 0)
5493 return posix_error();
5494 return PyLong_FromLong((long)fd);
5495 }
5496 /* Drop the argument parsing error as narrow strings
5497 are also valid. */
5498 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005499#endif
5500
Victor Stinner8c62be82010-05-06 00:08:46 +00005501 if (!PyArg_ParseTuple(args, "O&i|i",
5502 PyUnicode_FSConverter, &ofile,
5503 &flag, &mode))
5504 return NULL;
5505 file = PyBytes_AsString(ofile);
5506 Py_BEGIN_ALLOW_THREADS
5507 fd = open(file, flag, mode);
5508 Py_END_ALLOW_THREADS
5509 if (fd < 0)
5510 return posix_error_with_allocated_filename(ofile);
5511 Py_DECREF(ofile);
5512 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005513}
5514
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005516PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005517"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005518Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005519
Barry Warsaw53699e91996-12-10 23:23:01 +00005520static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005521posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005522{
Victor Stinner8c62be82010-05-06 00:08:46 +00005523 int fd, res;
5524 if (!PyArg_ParseTuple(args, "i:close", &fd))
5525 return NULL;
5526 if (!_PyVerify_fd(fd))
5527 return posix_error();
5528 Py_BEGIN_ALLOW_THREADS
5529 res = close(fd);
5530 Py_END_ALLOW_THREADS
5531 if (res < 0)
5532 return posix_error();
5533 Py_INCREF(Py_None);
5534 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005535}
5536
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005537
Victor Stinner8c62be82010-05-06 00:08:46 +00005538PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005539"closerange(fd_low, fd_high)\n\n\
5540Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5541
5542static PyObject *
5543posix_closerange(PyObject *self, PyObject *args)
5544{
Victor Stinner8c62be82010-05-06 00:08:46 +00005545 int fd_from, fd_to, i;
5546 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5547 return NULL;
5548 Py_BEGIN_ALLOW_THREADS
5549 for (i = fd_from; i < fd_to; i++)
5550 if (_PyVerify_fd(i))
5551 close(i);
5552 Py_END_ALLOW_THREADS
5553 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005554}
5555
5556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005557PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005558"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005560
Barry Warsaw53699e91996-12-10 23:23:01 +00005561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005562posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005563{
Victor Stinner8c62be82010-05-06 00:08:46 +00005564 int fd;
5565 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5566 return NULL;
5567 if (!_PyVerify_fd(fd))
5568 return posix_error();
5569 Py_BEGIN_ALLOW_THREADS
5570 fd = dup(fd);
5571 Py_END_ALLOW_THREADS
5572 if (fd < 0)
5573 return posix_error();
5574 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005575}
5576
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005579"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005581
Barry Warsaw53699e91996-12-10 23:23:01 +00005582static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005583posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005584{
Victor Stinner8c62be82010-05-06 00:08:46 +00005585 int fd, fd2, res;
5586 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5587 return NULL;
5588 if (!_PyVerify_fd_dup2(fd, fd2))
5589 return posix_error();
5590 Py_BEGIN_ALLOW_THREADS
5591 res = dup2(fd, fd2);
5592 Py_END_ALLOW_THREADS
5593 if (res < 0)
5594 return posix_error();
5595 Py_INCREF(Py_None);
5596 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005597}
5598
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005599
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005600PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005601"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005602Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005603
Barry Warsaw53699e91996-12-10 23:23:01 +00005604static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005605posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005606{
Victor Stinner8c62be82010-05-06 00:08:46 +00005607 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005608#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005609 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005610#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005611 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005612#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005613 PyObject *posobj;
5614 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5615 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005616#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005617 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5618 switch (how) {
5619 case 0: how = SEEK_SET; break;
5620 case 1: how = SEEK_CUR; break;
5621 case 2: how = SEEK_END; break;
5622 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005623#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005624
5625#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005626 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005627#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005628 pos = PyLong_Check(posobj) ?
5629 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005630#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 if (PyErr_Occurred())
5632 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005633
Victor Stinner8c62be82010-05-06 00:08:46 +00005634 if (!_PyVerify_fd(fd))
5635 return posix_error();
5636 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005637#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005638 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005639#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005640 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005641#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005642 Py_END_ALLOW_THREADS
5643 if (res < 0)
5644 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005645
5646#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005647 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005648#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005650#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005651}
5652
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005654PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005655"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005656Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005657
Barry Warsaw53699e91996-12-10 23:23:01 +00005658static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005659posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005660{
Victor Stinner8c62be82010-05-06 00:08:46 +00005661 int fd, size;
5662 Py_ssize_t n;
5663 PyObject *buffer;
5664 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5665 return NULL;
5666 if (size < 0) {
5667 errno = EINVAL;
5668 return posix_error();
5669 }
5670 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5671 if (buffer == NULL)
5672 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005673 if (!_PyVerify_fd(fd)) {
5674 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00005675 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005676 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005677 Py_BEGIN_ALLOW_THREADS
5678 n = read(fd, PyBytes_AS_STRING(buffer), size);
5679 Py_END_ALLOW_THREADS
5680 if (n < 0) {
5681 Py_DECREF(buffer);
5682 return posix_error();
5683 }
5684 if (n != size)
5685 _PyBytes_Resize(&buffer, n);
5686 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005687}
5688
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005690PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005691"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005692Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005693
Barry Warsaw53699e91996-12-10 23:23:01 +00005694static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005695posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005696{
Victor Stinner8c62be82010-05-06 00:08:46 +00005697 Py_buffer pbuf;
5698 int fd;
Victor Stinnere6edec22011-01-04 00:29:35 +00005699 Py_ssize_t size, len;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005700
Victor Stinner8c62be82010-05-06 00:08:46 +00005701 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5702 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00005703 if (!_PyVerify_fd(fd)) {
5704 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005705 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00005706 }
Victor Stinnere6edec22011-01-04 00:29:35 +00005707 len = pbuf.len;
Victor Stinner8c62be82010-05-06 00:08:46 +00005708 Py_BEGIN_ALLOW_THREADS
Victor Stinnere6edec22011-01-04 00:29:35 +00005709#if defined(MS_WIN64) || defined(MS_WINDOWS)
5710 if (len > INT_MAX)
5711 len = INT_MAX;
5712 size = write(fd, pbuf.buf, (int)len);
5713#else
5714 size = write(fd, pbuf.buf, (size_t)len);
5715#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005716 Py_END_ALLOW_THREADS
Stefan Krah99439262010-11-26 12:58:05 +00005717 PyBuffer_Release(&pbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00005718 if (size < 0)
5719 return posix_error();
5720 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005721}
5722
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005724PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005725"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005726Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005727
Barry Warsaw53699e91996-12-10 23:23:01 +00005728static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005729posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005730{
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 int fd;
5732 STRUCT_STAT st;
5733 int res;
5734 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5735 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005736#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005737 /* on OpenVMS we must ensure that all bytes are written to the file */
5738 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005739#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005740 if (!_PyVerify_fd(fd))
5741 return posix_error();
5742 Py_BEGIN_ALLOW_THREADS
5743 res = FSTAT(fd, &st);
5744 Py_END_ALLOW_THREADS
5745 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005746#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005747 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005748#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005749 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005750#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005751 }
Tim Peters5aa91602002-01-30 05:46:57 +00005752
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005754}
5755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005756PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005757"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005758Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005759connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005760
5761static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005762posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005763{
Victor Stinner8c62be82010-05-06 00:08:46 +00005764 int fd;
5765 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5766 return NULL;
5767 if (!_PyVerify_fd(fd))
5768 return PyBool_FromLong(0);
5769 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005770}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005771
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005772#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005773PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005774"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005775Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005776
Barry Warsaw53699e91996-12-10 23:23:01 +00005777static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005778posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005779{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005780#if defined(PYOS_OS2)
5781 HFILE read, write;
5782 APIRET rc;
5783
Victor Stinner8c62be82010-05-06 00:08:46 +00005784 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005785 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005786 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005787 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005788 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005789
5790 return Py_BuildValue("(ii)", read, write);
5791#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005792#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005793 int fds[2];
5794 int res;
5795 Py_BEGIN_ALLOW_THREADS
5796 res = pipe(fds);
5797 Py_END_ALLOW_THREADS
5798 if (res != 0)
5799 return posix_error();
5800 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005801#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005802 HANDLE read, write;
5803 int read_fd, write_fd;
5804 BOOL ok;
5805 Py_BEGIN_ALLOW_THREADS
5806 ok = CreatePipe(&read, &write, NULL, 0);
5807 Py_END_ALLOW_THREADS
5808 if (!ok)
5809 return win32_error("CreatePipe", NULL);
5810 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5811 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5812 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005813#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005814#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005815}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005816#endif /* HAVE_PIPE */
5817
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005818
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005819#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005820PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005821"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005822Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005823
Barry Warsaw53699e91996-12-10 23:23:01 +00005824static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005825posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005826{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005827 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 char *filename;
5829 int mode = 0666;
5830 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005831 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5832 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005833 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005834 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005835 Py_BEGIN_ALLOW_THREADS
5836 res = mkfifo(filename, mode);
5837 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005838 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005839 if (res < 0)
5840 return posix_error();
5841 Py_INCREF(Py_None);
5842 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005843}
5844#endif
5845
5846
Neal Norwitz11690112002-07-30 01:08:28 +00005847#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005848PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005849"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005850Create a filesystem node (file, device special file or named pipe)\n\
5851named filename. mode specifies both the permissions to use and the\n\
5852type of node to be created, being combined (bitwise OR) with one of\n\
5853S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005854device defines the newly created device special file (probably using\n\
5855os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005856
5857
5858static PyObject *
5859posix_mknod(PyObject *self, PyObject *args)
5860{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005861 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005862 char *filename;
5863 int mode = 0600;
5864 int device = 0;
5865 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005866 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5867 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005868 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005869 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 Py_BEGIN_ALLOW_THREADS
5871 res = mknod(filename, mode, device);
5872 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005873 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005874 if (res < 0)
5875 return posix_error();
5876 Py_INCREF(Py_None);
5877 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005878}
5879#endif
5880
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005881#ifdef HAVE_DEVICE_MACROS
5882PyDoc_STRVAR(posix_major__doc__,
5883"major(device) -> major number\n\
5884Extracts a device major number from a raw device number.");
5885
5886static PyObject *
5887posix_major(PyObject *self, PyObject *args)
5888{
Victor Stinner8c62be82010-05-06 00:08:46 +00005889 int device;
5890 if (!PyArg_ParseTuple(args, "i:major", &device))
5891 return NULL;
5892 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005893}
5894
5895PyDoc_STRVAR(posix_minor__doc__,
5896"minor(device) -> minor number\n\
5897Extracts a device minor number from a raw device number.");
5898
5899static PyObject *
5900posix_minor(PyObject *self, PyObject *args)
5901{
Victor Stinner8c62be82010-05-06 00:08:46 +00005902 int device;
5903 if (!PyArg_ParseTuple(args, "i:minor", &device))
5904 return NULL;
5905 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005906}
5907
5908PyDoc_STRVAR(posix_makedev__doc__,
5909"makedev(major, minor) -> device number\n\
5910Composes a raw device number from the major and minor device numbers.");
5911
5912static PyObject *
5913posix_makedev(PyObject *self, PyObject *args)
5914{
Victor Stinner8c62be82010-05-06 00:08:46 +00005915 int major, minor;
5916 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5917 return NULL;
5918 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005919}
5920#endif /* device macros */
5921
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005922
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005923#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005924PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005925"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005926Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005927
Barry Warsaw53699e91996-12-10 23:23:01 +00005928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005929posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005930{
Victor Stinner8c62be82010-05-06 00:08:46 +00005931 int fd;
5932 off_t length;
5933 int res;
5934 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005935
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5937 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005938
5939#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005941#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005942 length = PyLong_Check(lenobj) ?
5943 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005944#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005945 if (PyErr_Occurred())
5946 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005947
Victor Stinner8c62be82010-05-06 00:08:46 +00005948 Py_BEGIN_ALLOW_THREADS
5949 res = ftruncate(fd, length);
5950 Py_END_ALLOW_THREADS
5951 if (res < 0)
5952 return posix_error();
5953 Py_INCREF(Py_None);
5954 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005955}
5956#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005957
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005958#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005959PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005960"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005961Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005962
Fred Drake762e2061999-08-26 17:23:54 +00005963/* Save putenv() parameters as values here, so we can collect them when they
5964 * get re-set with another call for the same key. */
5965static PyObject *posix_putenv_garbage;
5966
Tim Peters5aa91602002-01-30 05:46:57 +00005967static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005968posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005969{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005970#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 wchar_t *s1, *s2;
5972 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005973#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005974 PyObject *os1, *os2;
5975 char *s1, *s2;
5976 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005977#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005978 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005980
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005981#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005982 if (!PyArg_ParseTuple(args,
5983 "uu:putenv",
5984 &s1, &s2))
5985 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005986#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 if (!PyArg_ParseTuple(args,
5988 "O&O&:putenv",
5989 PyUnicode_FSConverter, &os1,
5990 PyUnicode_FSConverter, &os2))
5991 return NULL;
5992 s1 = PyBytes_AsString(os1);
5993 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005994#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005995
5996#if defined(PYOS_OS2)
5997 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5998 APIRET rc;
5999
Guido van Rossumd48f2521997-12-05 22:19:34 +00006000 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006001 if (rc != NO_ERROR) {
6002 os2_error(rc);
6003 goto error;
6004 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006005
6006 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
6007 APIRET rc;
6008
Guido van Rossumd48f2521997-12-05 22:19:34 +00006009 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00006010 if (rc != NO_ERROR) {
6011 os2_error(rc);
6012 goto error;
6013 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006014 } else {
6015#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006016 /* XXX This can leak memory -- not easy to fix :-( */
6017 /* len includes space for a trailing \0; the size arg to
6018 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006019#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 len = wcslen(s1) + wcslen(s2) + 2;
6021 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006022#else
Victor Stinner84ae1182010-05-06 22:05:07 +00006023 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00006024 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006025#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006026 if (newstr == NULL) {
6027 PyErr_NoMemory();
6028 goto error;
6029 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006030#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006031 newenv = PyUnicode_AsUnicode(newstr);
6032 _snwprintf(newenv, len, L"%s=%s", s1, s2);
6033 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006034 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006035 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006037#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006038 newenv = PyBytes_AS_STRING(newstr);
6039 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
6040 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006041 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00006042 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006043 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00006044#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006045
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 /* Install the first arg and newstr in posix_putenv_garbage;
6047 * this will cause previous value to be collected. This has to
6048 * happen after the real putenv() call because the old value
6049 * was still accessible until then. */
6050 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006051#ifdef MS_WINDOWS
6052 PyTuple_GET_ITEM(args, 0),
6053#else
6054 os1,
6055#endif
6056 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 /* really not much we can do; just leak */
6058 PyErr_Clear();
6059 }
6060 else {
6061 Py_DECREF(newstr);
6062 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00006063
6064#if defined(PYOS_OS2)
6065 }
6066#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006067
Martin v. Löwis011e8422009-05-05 04:43:17 +00006068#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006069 Py_DECREF(os1);
6070 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00006071#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00006072 Py_RETURN_NONE;
6073
6074error:
6075#ifndef MS_WINDOWS
6076 Py_DECREF(os1);
6077 Py_DECREF(os2);
6078#endif
6079 Py_XDECREF(newstr);
6080 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006081}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006082#endif /* putenv */
6083
Guido van Rossumc524d952001-10-19 01:31:59 +00006084#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006085PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006086"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006087Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00006088
6089static PyObject *
6090posix_unsetenv(PyObject *self, PyObject *args)
6091{
Victor Stinner84ae1182010-05-06 22:05:07 +00006092#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00006093 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00006094
Victor Stinner8c62be82010-05-06 00:08:46 +00006095 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
6096 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00006097#else
6098 PyObject *os1;
6099 char *s1;
6100
6101 if (!PyArg_ParseTuple(args, "O&:unsetenv",
6102 PyUnicode_FSConverter, &os1))
6103 return NULL;
6104 s1 = PyBytes_AsString(os1);
6105#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00006106
Victor Stinner8c62be82010-05-06 00:08:46 +00006107 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00006108
Victor Stinner8c62be82010-05-06 00:08:46 +00006109 /* Remove the key from posix_putenv_garbage;
6110 * this will cause it to be collected. This has to
6111 * happen after the real unsetenv() call because the
6112 * old value was still accessible until then.
6113 */
6114 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00006115#ifdef MS_WINDOWS
6116 PyTuple_GET_ITEM(args, 0)
6117#else
6118 os1
6119#endif
6120 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 /* really not much we can do; just leak */
6122 PyErr_Clear();
6123 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006124
Victor Stinner84ae1182010-05-06 22:05:07 +00006125#ifndef MS_WINDOWS
6126 Py_DECREF(os1);
6127#endif
6128 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006129}
6130#endif /* unsetenv */
6131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006132PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006133"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006134Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006135
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006136static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006137posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006138{
Victor Stinner8c62be82010-05-06 00:08:46 +00006139 int code;
6140 char *message;
6141 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6142 return NULL;
6143 message = strerror(code);
6144 if (message == NULL) {
6145 PyErr_SetString(PyExc_ValueError,
6146 "strerror() argument out of range");
6147 return NULL;
6148 }
6149 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006150}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006151
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006152
Guido van Rossumc9641791998-08-04 15:26:23 +00006153#ifdef HAVE_SYS_WAIT_H
6154
Fred Drake106c1a02002-04-23 15:58:02 +00006155#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006156PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006157"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006158Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006159
6160static PyObject *
6161posix_WCOREDUMP(PyObject *self, PyObject *args)
6162{
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 WAIT_TYPE status;
6164 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006165
Victor Stinner8c62be82010-05-06 00:08:46 +00006166 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6167 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006168
Victor Stinner8c62be82010-05-06 00:08:46 +00006169 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006170}
6171#endif /* WCOREDUMP */
6172
6173#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006174PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006175"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006176Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006178
6179static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006180posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006181{
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 WAIT_TYPE status;
6183 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006184
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6186 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006187
Victor Stinner8c62be82010-05-06 00:08:46 +00006188 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006189}
6190#endif /* WIFCONTINUED */
6191
Guido van Rossumc9641791998-08-04 15:26:23 +00006192#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006193PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006194"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006195Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006196
6197static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006198posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006199{
Victor Stinner8c62be82010-05-06 00:08:46 +00006200 WAIT_TYPE status;
6201 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006202
Victor Stinner8c62be82010-05-06 00:08:46 +00006203 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6204 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006205
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006207}
6208#endif /* WIFSTOPPED */
6209
6210#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006211PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006212"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006213Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006214
6215static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006216posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006217{
Victor Stinner8c62be82010-05-06 00:08:46 +00006218 WAIT_TYPE status;
6219 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006220
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6222 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006223
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006225}
6226#endif /* WIFSIGNALED */
6227
6228#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006229PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006230"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006231Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006232system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006233
6234static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006235posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006236{
Victor Stinner8c62be82010-05-06 00:08:46 +00006237 WAIT_TYPE status;
6238 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006239
Victor Stinner8c62be82010-05-06 00:08:46 +00006240 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6241 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006242
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006244}
6245#endif /* WIFEXITED */
6246
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006247#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006248PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006249"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006250Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006251
6252static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006253posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006254{
Victor Stinner8c62be82010-05-06 00:08:46 +00006255 WAIT_TYPE status;
6256 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006257
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6259 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006260
Victor Stinner8c62be82010-05-06 00:08:46 +00006261 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006262}
6263#endif /* WEXITSTATUS */
6264
6265#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006266PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006267"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006268Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006269value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006270
6271static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006272posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006273{
Victor Stinner8c62be82010-05-06 00:08:46 +00006274 WAIT_TYPE status;
6275 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006276
Victor Stinner8c62be82010-05-06 00:08:46 +00006277 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6278 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006279
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006281}
6282#endif /* WTERMSIG */
6283
6284#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006285PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006286"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006287Return the signal that stopped the process that provided\n\
6288the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006289
6290static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006291posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006292{
Victor Stinner8c62be82010-05-06 00:08:46 +00006293 WAIT_TYPE status;
6294 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006295
Victor Stinner8c62be82010-05-06 00:08:46 +00006296 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6297 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006298
Victor Stinner8c62be82010-05-06 00:08:46 +00006299 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006300}
6301#endif /* WSTOPSIG */
6302
6303#endif /* HAVE_SYS_WAIT_H */
6304
6305
Thomas Wouters477c8d52006-05-27 19:21:47 +00006306#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006307#ifdef _SCO_DS
6308/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6309 needed definitions in sys/statvfs.h */
6310#define _SVID3
6311#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006312#include <sys/statvfs.h>
6313
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006314static PyObject*
6315_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6317 if (v == NULL)
6318 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006319
6320#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006321 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6322 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6323 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6324 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6325 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6326 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6327 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6328 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6329 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6330 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006331#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006332 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6333 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6334 PyStructSequence_SET_ITEM(v, 2,
6335 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6336 PyStructSequence_SET_ITEM(v, 3,
6337 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6338 PyStructSequence_SET_ITEM(v, 4,
6339 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6340 PyStructSequence_SET_ITEM(v, 5,
6341 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6342 PyStructSequence_SET_ITEM(v, 6,
6343 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6344 PyStructSequence_SET_ITEM(v, 7,
6345 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6346 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6347 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006348#endif
6349
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006351}
6352
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006353PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006354"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006355Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006356
6357static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006358posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006359{
Victor Stinner8c62be82010-05-06 00:08:46 +00006360 int fd, res;
6361 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006362
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6364 return NULL;
6365 Py_BEGIN_ALLOW_THREADS
6366 res = fstatvfs(fd, &st);
6367 Py_END_ALLOW_THREADS
6368 if (res != 0)
6369 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006370
Victor Stinner8c62be82010-05-06 00:08:46 +00006371 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006372}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006373#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006374
6375
Thomas Wouters477c8d52006-05-27 19:21:47 +00006376#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006377#include <sys/statvfs.h>
6378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006379PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006380"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006381Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006382
6383static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006384posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006385{
Victor Stinner8c62be82010-05-06 00:08:46 +00006386 char *path;
6387 int res;
6388 struct statvfs st;
6389 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6390 return NULL;
6391 Py_BEGIN_ALLOW_THREADS
6392 res = statvfs(path, &st);
6393 Py_END_ALLOW_THREADS
6394 if (res != 0)
6395 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006396
Victor Stinner8c62be82010-05-06 00:08:46 +00006397 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006398}
6399#endif /* HAVE_STATVFS */
6400
Fred Drakec9680921999-12-13 16:37:25 +00006401/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6402 * It maps strings representing configuration variable names to
6403 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006404 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006405 * rarely-used constants. There are three separate tables that use
6406 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006407 *
6408 * This code is always included, even if none of the interfaces that
6409 * need it are included. The #if hackery needed to avoid it would be
6410 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006411 */
6412struct constdef {
6413 char *name;
6414 long value;
6415};
6416
Fred Drake12c6e2d1999-12-14 21:25:03 +00006417static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006418conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006419 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006420{
Christian Heimes217cfd12007-12-02 14:31:20 +00006421 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006422 *valuep = PyLong_AS_LONG(arg);
6423 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006424 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006425 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00006426 /* look up the value in the table using a binary search */
6427 size_t lo = 0;
6428 size_t mid;
6429 size_t hi = tablesize;
6430 int cmp;
6431 const char *confname;
6432 if (!PyUnicode_Check(arg)) {
6433 PyErr_SetString(PyExc_TypeError,
6434 "configuration names must be strings or integers");
6435 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006436 }
Stefan Krah0e803b32010-11-26 16:16:47 +00006437 confname = _PyUnicode_AsString(arg);
6438 if (confname == NULL)
6439 return 0;
6440 while (lo < hi) {
6441 mid = (lo + hi) / 2;
6442 cmp = strcmp(confname, table[mid].name);
6443 if (cmp < 0)
6444 hi = mid;
6445 else if (cmp > 0)
6446 lo = mid + 1;
6447 else {
6448 *valuep = table[mid].value;
6449 return 1;
6450 }
6451 }
6452 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6453 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006454 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006455}
6456
6457
6458#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6459static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006460#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006462#endif
6463#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006464 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006465#endif
Fred Drakec9680921999-12-13 16:37:25 +00006466#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006468#endif
6469#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006471#endif
6472#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006474#endif
6475#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006477#endif
6478#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006480#endif
6481#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006483#endif
6484#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006486#endif
6487#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006489#endif
6490#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006491 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006492#endif
6493#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006494 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006495#endif
6496#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006498#endif
6499#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006501#endif
6502#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006504#endif
6505#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006507#endif
6508#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00006511#ifdef _PC_ACL_ENABLED
6512 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
6513#endif
6514#ifdef _PC_MIN_HOLE_SIZE
6515 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
6516#endif
6517#ifdef _PC_ALLOC_SIZE_MIN
6518 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
6519#endif
6520#ifdef _PC_REC_INCR_XFER_SIZE
6521 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
6522#endif
6523#ifdef _PC_REC_MAX_XFER_SIZE
6524 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
6525#endif
6526#ifdef _PC_REC_MIN_XFER_SIZE
6527 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
6528#endif
6529#ifdef _PC_REC_XFER_ALIGN
6530 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
6531#endif
6532#ifdef _PC_SYMLINK_MAX
6533 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
6534#endif
6535#ifdef _PC_XATTR_ENABLED
6536 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
6537#endif
6538#ifdef _PC_XATTR_EXISTS
6539 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
6540#endif
6541#ifdef _PC_TIMESTAMP_RESOLUTION
6542 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
6543#endif
Fred Drakec9680921999-12-13 16:37:25 +00006544};
6545
Fred Drakec9680921999-12-13 16:37:25 +00006546static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006547conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006548{
6549 return conv_confname(arg, valuep, posix_constants_pathconf,
6550 sizeof(posix_constants_pathconf)
6551 / sizeof(struct constdef));
6552}
6553#endif
6554
6555#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006556PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006557"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006558Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006559If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006560
6561static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006562posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006563{
6564 PyObject *result = NULL;
6565 int name, fd;
6566
Fred Drake12c6e2d1999-12-14 21:25:03 +00006567 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6568 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006569 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006570
Stefan Krah0e803b32010-11-26 16:16:47 +00006571 errno = 0;
6572 limit = fpathconf(fd, name);
6573 if (limit == -1 && errno != 0)
6574 posix_error();
6575 else
6576 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006577 }
6578 return result;
6579}
6580#endif
6581
6582
6583#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006584PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006585"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006586Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006587If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006588
6589static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006590posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006591{
6592 PyObject *result = NULL;
6593 int name;
6594 char *path;
6595
6596 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6597 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006599
Victor Stinner8c62be82010-05-06 00:08:46 +00006600 errno = 0;
6601 limit = pathconf(path, name);
6602 if (limit == -1 && errno != 0) {
6603 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00006604 /* could be a path or name problem */
6605 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006606 else
Stefan Krah99439262010-11-26 12:58:05 +00006607 posix_error_with_filename(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00006608 }
6609 else
6610 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006611 }
6612 return result;
6613}
6614#endif
6615
6616#ifdef HAVE_CONFSTR
6617static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006618#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006620#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006621#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006622 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006623#endif
6624#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006626#endif
Fred Draked86ed291999-12-15 15:34:33 +00006627#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006629#endif
6630#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006632#endif
6633#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006635#endif
6636#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006638#endif
Fred Drakec9680921999-12-13 16:37:25 +00006639#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006641#endif
6642#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006644#endif
6645#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006647#endif
6648#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006650#endif
6651#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006653#endif
6654#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006656#endif
6657#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006659#endif
6660#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006662#endif
Fred Draked86ed291999-12-15 15:34:33 +00006663#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006665#endif
Fred Drakec9680921999-12-13 16:37:25 +00006666#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006668#endif
Fred Draked86ed291999-12-15 15:34:33 +00006669#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006671#endif
6672#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006674#endif
6675#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006677#endif
6678#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006680#endif
Fred Drakec9680921999-12-13 16:37:25 +00006681#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006683#endif
6684#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006686#endif
6687#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006689#endif
6690#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006692#endif
6693#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006695#endif
6696#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006698#endif
6699#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006701#endif
6702#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006704#endif
6705#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006707#endif
6708#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006710#endif
6711#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006713#endif
6714#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006716#endif
6717#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006719#endif
6720#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006722#endif
6723#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006725#endif
6726#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006728#endif
Fred Draked86ed291999-12-15 15:34:33 +00006729#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006731#endif
6732#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006734#endif
6735#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006736 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006737#endif
6738#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006740#endif
6741#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006742 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006743#endif
6744#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006746#endif
6747#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006748 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006749#endif
6750#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006751 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006752#endif
6753#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006754 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006755#endif
6756#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006758#endif
6759#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006760 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006761#endif
6762#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006764#endif
6765#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006767#endif
Fred Drakec9680921999-12-13 16:37:25 +00006768};
6769
6770static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006771conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006772{
6773 return conv_confname(arg, valuep, posix_constants_confstr,
6774 sizeof(posix_constants_confstr)
6775 / sizeof(struct constdef));
6776}
6777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006778PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006779"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006780Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006781
6782static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006783posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006784{
6785 PyObject *result = NULL;
6786 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00006787 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00006788 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006789
Victor Stinnercb043522010-09-10 23:49:04 +00006790 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
6791 return NULL;
6792
6793 errno = 0;
6794 len = confstr(name, buffer, sizeof(buffer));
6795 if (len == 0) {
6796 if (errno) {
6797 posix_error();
6798 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00006799 }
6800 else {
Victor Stinnercb043522010-09-10 23:49:04 +00006801 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00006802 }
6803 }
Victor Stinnercb043522010-09-10 23:49:04 +00006804
6805 if ((unsigned int)len >= sizeof(buffer)) {
6806 char *buf = PyMem_Malloc(len);
6807 if (buf == NULL)
6808 return PyErr_NoMemory();
6809 confstr(name, buf, len);
6810 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
6811 PyMem_Free(buf);
6812 }
6813 else
6814 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006815 return result;
6816}
6817#endif
6818
6819
6820#ifdef HAVE_SYSCONF
6821static struct constdef posix_constants_sysconf[] = {
6822#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006824#endif
6825#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006827#endif
6828#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006830#endif
6831#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006833#endif
6834#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006836#endif
6837#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006839#endif
6840#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006841 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006842#endif
6843#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006845#endif
6846#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006848#endif
6849#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006851#endif
Fred Draked86ed291999-12-15 15:34:33 +00006852#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006854#endif
6855#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006857#endif
Fred Drakec9680921999-12-13 16:37:25 +00006858#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006860#endif
Fred Drakec9680921999-12-13 16:37:25 +00006861#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006863#endif
6864#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
6873#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006875#endif
Fred Draked86ed291999-12-15 15:34:33 +00006876#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006878#endif
Fred Drakec9680921999-12-13 16:37:25 +00006879#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
6882#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
6891#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006893#endif
Fred Draked86ed291999-12-15 15:34:33 +00006894#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006896#endif
Fred Drakec9680921999-12-13 16:37:25 +00006897#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006899#endif
6900#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
6903#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
6909#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006911#endif
6912#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006914#endif
6915#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006917#endif
6918#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006920#endif
6921#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006923#endif
6924#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006926#endif
6927#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006929#endif
6930#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006932#endif
6933#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006935#endif
6936#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006938#endif
6939#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006941#endif
6942#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006944#endif
6945#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006947#endif
6948#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006950#endif
6951#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006953#endif
6954#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006956#endif
6957#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006959#endif
6960#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
6963#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006965#endif
Fred Draked86ed291999-12-15 15:34:33 +00006966#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006968#endif
Fred Drakec9680921999-12-13 16:37:25 +00006969#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
6972#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
Fred Draked86ed291999-12-15 15:34:33 +00006978#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006980#endif
Fred Drakec9680921999-12-13 16:37:25 +00006981#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006983#endif
Fred Draked86ed291999-12-15 15:34:33 +00006984#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006986#endif
6987#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006989#endif
Fred Drakec9680921999-12-13 16:37:25 +00006990#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006992#endif
6993#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006995#endif
6996#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006998#endif
6999#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007001#endif
Fred Draked86ed291999-12-15 15:34:33 +00007002#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00007004#endif
Fred Drakec9680921999-12-13 16:37:25 +00007005#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00007007#endif
7008#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
7017#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
7020#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
7023#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
Fred Draked86ed291999-12-15 15:34:33 +00007026#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00007028#endif
Fred Drakec9680921999-12-13 16:37:25 +00007029#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007031#endif
7032#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007034#endif
Fred Draked86ed291999-12-15 15:34:33 +00007035#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00007037#endif
Fred Drakec9680921999-12-13 16:37:25 +00007038#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
7041#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
7044#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007046#endif
7047#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007049#endif
7050#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
7056#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00007058#endif
7059#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
Fred Draked86ed291999-12-15 15:34:33 +00007065#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00007067#endif
7068#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00007070#endif
Fred Drakec9680921999-12-13 16:37:25 +00007071#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00007073#endif
7074#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007076#endif
7077#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007079#endif
7080#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00007102 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00007103#endif
7104#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007105 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00007106#endif
7107#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007108 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00007109#endif
7110#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00007111 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00007112#endif
7113#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00007114 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00007115#endif
7116#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00007117 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00007118#endif
7119#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00007120 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00007121#endif
7122#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007124#endif
7125#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007126 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007127#endif
7128#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00007130#endif
7131#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007132 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007133#endif
7134#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007136#endif
7137#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00007138 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00007139#endif
7140#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007141 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007142#endif
7143#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007144 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007145#endif
7146#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007147 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00007148#endif
7149#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00007151#endif
7152#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007154#endif
7155#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007157#endif
7158#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007160#endif
7161#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007163#endif
7164#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007166#endif
7167#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007169#endif
7170#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007172#endif
7173#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007175#endif
Fred Draked86ed291999-12-15 15:34:33 +00007176#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007178#endif
Fred Drakec9680921999-12-13 16:37:25 +00007179#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007181#endif
7182#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007184#endif
7185#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007187#endif
7188#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007190#endif
7191#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007193#endif
7194#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007196#endif
7197#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007199#endif
7200#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007202#endif
7203#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007205#endif
7206#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007208#endif
7209#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007211#endif
7212#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007214#endif
7215#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007216 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007217#endif
7218#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007219 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007220#endif
7221#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007222 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007223#endif
7224#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007226#endif
7227#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007229#endif
7230#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007232#endif
7233#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007235#endif
7236#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007237 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007238#endif
7239#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007240 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007241#endif
7242#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007243 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007244#endif
7245#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007247#endif
7248#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007249 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007250#endif
7251#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007253#endif
7254#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007256#endif
7257#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007259#endif
7260#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007262#endif
7263#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007265#endif
7266#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007268#endif
7269#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007271#endif
7272#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007274#endif
7275#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007277#endif
7278#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007280#endif
7281#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007283#endif
7284#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007286#endif
7287#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007288 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007289#endif
7290#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007291 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007292#endif
7293#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007295#endif
7296#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007298#endif
7299#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007301#endif
7302#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007303 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007304#endif
7305#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007306 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007307#endif
7308#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007310#endif
7311#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007313#endif
7314};
7315
7316static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007317conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007318{
7319 return conv_confname(arg, valuep, posix_constants_sysconf,
7320 sizeof(posix_constants_sysconf)
7321 / sizeof(struct constdef));
7322}
7323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007324PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007325"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007326Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007327
7328static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007329posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007330{
7331 PyObject *result = NULL;
7332 int name;
7333
7334 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7335 int value;
7336
7337 errno = 0;
7338 value = sysconf(name);
7339 if (value == -1 && errno != 0)
7340 posix_error();
7341 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007342 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007343 }
7344 return result;
7345}
7346#endif
7347
7348
Fred Drakebec628d1999-12-15 18:31:10 +00007349/* This code is used to ensure that the tables of configuration value names
7350 * are in sorted order as required by conv_confname(), and also to build the
7351 * the exported dictionaries that are used to publish information about the
7352 * names available on the host platform.
7353 *
7354 * Sorting the table at runtime ensures that the table is properly ordered
7355 * when used, even for platforms we're not able to test on. It also makes
7356 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007357 */
Fred Drakebec628d1999-12-15 18:31:10 +00007358
7359static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007360cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007361{
7362 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007363 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007364 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007365 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007366
7367 return strcmp(c1->name, c2->name);
7368}
7369
7370static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007371setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007372 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007373{
Fred Drakebec628d1999-12-15 18:31:10 +00007374 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007375 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007376
7377 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7378 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007379 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007380 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007381
Barry Warsaw3155db32000-04-13 15:20:40 +00007382 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 PyObject *o = PyLong_FromLong(table[i].value);
7384 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7385 Py_XDECREF(o);
7386 Py_DECREF(d);
7387 return -1;
7388 }
7389 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007390 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007391 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007392}
7393
Fred Drakebec628d1999-12-15 18:31:10 +00007394/* Return -1 on failure, 0 on success. */
7395static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007396setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007397{
7398#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007399 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007400 sizeof(posix_constants_pathconf)
7401 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007402 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007403 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007404#endif
7405#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007406 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007407 sizeof(posix_constants_confstr)
7408 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007409 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007410 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007411#endif
7412#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007413 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007414 sizeof(posix_constants_sysconf)
7415 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007416 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00007417 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007418#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007419 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007420}
Fred Draked86ed291999-12-15 15:34:33 +00007421
7422
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007423PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007424"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007425Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007426in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007427
7428static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007429posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007430{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007431 abort();
7432 /*NOTREACHED*/
7433 Py_FatalError("abort() called from Python code didn't abort!");
7434 return NULL;
7435}
Fred Drakebec628d1999-12-15 18:31:10 +00007436
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007437#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007438PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007439"startfile(filepath [, operation]) - Start a file with its associated\n\
7440application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007441\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007442When \"operation\" is not specified or \"open\", this acts like\n\
7443double-clicking the file in Explorer, or giving the file name as an\n\
7444argument to the DOS \"start\" command: the file is opened with whatever\n\
7445application (if any) its extension is associated.\n\
7446When another \"operation\" is given, it specifies what should be done with\n\
7447the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007448\n\
7449startfile returns as soon as the associated application is launched.\n\
7450There is no option to wait for the application to close, and no way\n\
7451to retrieve the application's exit status.\n\
7452\n\
7453The filepath is relative to the current directory. If you want to use\n\
7454an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007455the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007456
7457static PyObject *
7458win32_startfile(PyObject *self, PyObject *args)
7459{
Victor Stinner8c62be82010-05-06 00:08:46 +00007460 PyObject *ofilepath;
7461 char *filepath;
7462 char *operation = NULL;
7463 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007464
Victor Stinner8c62be82010-05-06 00:08:46 +00007465 PyObject *unipath, *woperation = NULL;
7466 if (!PyArg_ParseTuple(args, "U|s:startfile",
7467 &unipath, &operation)) {
7468 PyErr_Clear();
7469 goto normal;
7470 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007471
Victor Stinner8c62be82010-05-06 00:08:46 +00007472 if (operation) {
7473 woperation = PyUnicode_DecodeASCII(operation,
7474 strlen(operation), NULL);
7475 if (!woperation) {
7476 PyErr_Clear();
7477 operation = NULL;
7478 goto normal;
7479 }
7480 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007481
Victor Stinner8c62be82010-05-06 00:08:46 +00007482 Py_BEGIN_ALLOW_THREADS
7483 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7484 PyUnicode_AS_UNICODE(unipath),
7485 NULL, NULL, SW_SHOWNORMAL);
7486 Py_END_ALLOW_THREADS
7487
7488 Py_XDECREF(woperation);
7489 if (rc <= (HINSTANCE)32) {
7490 PyObject *errval = win32_error_unicode("startfile",
7491 PyUnicode_AS_UNICODE(unipath));
7492 return errval;
7493 }
7494 Py_INCREF(Py_None);
7495 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007496
7497normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007498 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7499 PyUnicode_FSConverter, &ofilepath,
7500 &operation))
7501 return NULL;
7502 filepath = PyBytes_AsString(ofilepath);
7503 Py_BEGIN_ALLOW_THREADS
7504 rc = ShellExecute((HWND)0, operation, filepath,
7505 NULL, NULL, SW_SHOWNORMAL);
7506 Py_END_ALLOW_THREADS
7507 if (rc <= (HINSTANCE)32) {
7508 PyObject *errval = win32_error("startfile", filepath);
7509 Py_DECREF(ofilepath);
7510 return errval;
7511 }
7512 Py_DECREF(ofilepath);
7513 Py_INCREF(Py_None);
7514 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007515}
7516#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007517
Martin v. Löwis438b5342002-12-27 10:16:42 +00007518#ifdef HAVE_GETLOADAVG
7519PyDoc_STRVAR(posix_getloadavg__doc__,
7520"getloadavg() -> (float, float, float)\n\n\
7521Return the number of processes in the system run queue averaged over\n\
7522the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7523was unobtainable");
7524
7525static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007526posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007527{
7528 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007529 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007530 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7531 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007532 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00007533 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007534}
7535#endif
7536
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007537#ifdef MS_WINDOWS
7538
7539PyDoc_STRVAR(win32_urandom__doc__,
7540"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007541Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007542
7543typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7544 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7545 DWORD dwFlags );
7546typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7547 BYTE *pbBuffer );
7548
7549static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007550/* This handle is never explicitly released. Instead, the operating
7551 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007552static HCRYPTPROV hCryptProv = 0;
7553
Tim Peters4ad82172004-08-30 17:02:04 +00007554static PyObject*
7555win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007556{
Victor Stinner8c62be82010-05-06 00:08:46 +00007557 int howMany;
7558 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007559
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 /* Read arguments */
7561 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7562 return NULL;
7563 if (howMany < 0)
7564 return PyErr_Format(PyExc_ValueError,
7565 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007566
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 if (hCryptProv == 0) {
7568 HINSTANCE hAdvAPI32 = NULL;
7569 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007570
Victor Stinner8c62be82010-05-06 00:08:46 +00007571 /* Obtain handle to the DLL containing CryptoAPI
7572 This should not fail */
7573 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7574 if(hAdvAPI32 == NULL)
7575 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007576
Victor Stinner8c62be82010-05-06 00:08:46 +00007577 /* Obtain pointers to the CryptoAPI functions
7578 This will fail on some early versions of Win95 */
7579 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7580 hAdvAPI32,
7581 "CryptAcquireContextA");
7582 if (pCryptAcquireContext == NULL)
7583 return PyErr_Format(PyExc_NotImplementedError,
7584 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007585
Victor Stinner8c62be82010-05-06 00:08:46 +00007586 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7587 hAdvAPI32, "CryptGenRandom");
7588 if (pCryptGenRandom == NULL)
7589 return PyErr_Format(PyExc_NotImplementedError,
7590 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007591
Victor Stinner8c62be82010-05-06 00:08:46 +00007592 /* Acquire context */
7593 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7594 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7595 return win32_error("CryptAcquireContext", NULL);
7596 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007597
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 /* Allocate bytes */
7599 result = PyBytes_FromStringAndSize(NULL, howMany);
7600 if (result != NULL) {
7601 /* Get random data */
7602 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7603 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7604 PyBytes_AS_STRING(result))) {
7605 Py_DECREF(result);
7606 return win32_error("CryptGenRandom", NULL);
7607 }
7608 }
7609 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007610}
7611#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007612
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007613PyDoc_STRVAR(device_encoding__doc__,
7614"device_encoding(fd) -> str\n\n\
7615Return a string describing the encoding of the device\n\
7616if the output is a terminal; else return None.");
7617
7618static PyObject *
7619device_encoding(PyObject *self, PyObject *args)
7620{
Victor Stinner8c62be82010-05-06 00:08:46 +00007621 int fd;
7622 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7623 return NULL;
7624 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7625 Py_INCREF(Py_None);
7626 return Py_None;
7627 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007628#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007629 if (fd == 0) {
7630 char buf[100];
7631 sprintf(buf, "cp%d", GetConsoleCP());
7632 return PyUnicode_FromString(buf);
7633 }
7634 if (fd == 1 || fd == 2) {
7635 char buf[100];
7636 sprintf(buf, "cp%d", GetConsoleOutputCP());
7637 return PyUnicode_FromString(buf);
7638 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007639#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 {
7641 char *codeset = nl_langinfo(CODESET);
7642 if (codeset != NULL && codeset[0] != 0)
7643 return PyUnicode_FromString(codeset);
7644 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007645#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007646 Py_INCREF(Py_None);
7647 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007648}
7649
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007650#ifdef __VMS
7651/* Use openssl random routine */
7652#include <openssl/rand.h>
7653PyDoc_STRVAR(vms_urandom__doc__,
7654"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007655Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007656
7657static PyObject*
7658vms_urandom(PyObject *self, PyObject *args)
7659{
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 int howMany;
7661 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007662
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 /* Read arguments */
7664 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7665 return NULL;
7666 if (howMany < 0)
7667 return PyErr_Format(PyExc_ValueError,
7668 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007669
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 /* Allocate bytes */
7671 result = PyBytes_FromStringAndSize(NULL, howMany);
7672 if (result != NULL) {
7673 /* Get random data */
7674 if (RAND_pseudo_bytes((unsigned char*)
7675 PyBytes_AS_STRING(result),
7676 howMany) < 0) {
7677 Py_DECREF(result);
7678 return PyErr_Format(PyExc_ValueError,
7679 "RAND_pseudo_bytes");
7680 }
7681 }
7682 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007683}
7684#endif
7685
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007686#ifdef HAVE_SETRESUID
7687PyDoc_STRVAR(posix_setresuid__doc__,
7688"setresuid(ruid, euid, suid)\n\n\
7689Set the current process's real, effective, and saved user ids.");
7690
7691static PyObject*
7692posix_setresuid (PyObject *self, PyObject *args)
7693{
Victor Stinner8c62be82010-05-06 00:08:46 +00007694 /* We assume uid_t is no larger than a long. */
7695 long ruid, euid, suid;
7696 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7697 return NULL;
7698 if (setresuid(ruid, euid, suid) < 0)
7699 return posix_error();
7700 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007701}
7702#endif
7703
7704#ifdef HAVE_SETRESGID
7705PyDoc_STRVAR(posix_setresgid__doc__,
7706"setresgid(rgid, egid, sgid)\n\n\
7707Set the current process's real, effective, and saved group ids.");
7708
7709static PyObject*
7710posix_setresgid (PyObject *self, PyObject *args)
7711{
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 /* We assume uid_t is no larger than a long. */
7713 long rgid, egid, sgid;
7714 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7715 return NULL;
7716 if (setresgid(rgid, egid, sgid) < 0)
7717 return posix_error();
7718 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007719}
7720#endif
7721
7722#ifdef HAVE_GETRESUID
7723PyDoc_STRVAR(posix_getresuid__doc__,
7724"getresuid() -> (ruid, euid, suid)\n\n\
7725Get tuple of the current process's real, effective, and saved user ids.");
7726
7727static PyObject*
7728posix_getresuid (PyObject *self, PyObject *noargs)
7729{
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 uid_t ruid, euid, suid;
7731 long l_ruid, l_euid, l_suid;
7732 if (getresuid(&ruid, &euid, &suid) < 0)
7733 return posix_error();
7734 /* Force the values into long's as we don't know the size of uid_t. */
7735 l_ruid = ruid;
7736 l_euid = euid;
7737 l_suid = suid;
7738 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007739}
7740#endif
7741
7742#ifdef HAVE_GETRESGID
7743PyDoc_STRVAR(posix_getresgid__doc__,
7744"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00007745Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007746
7747static PyObject*
7748posix_getresgid (PyObject *self, PyObject *noargs)
7749{
Victor Stinner8c62be82010-05-06 00:08:46 +00007750 uid_t rgid, egid, sgid;
7751 long l_rgid, l_egid, l_sgid;
7752 if (getresgid(&rgid, &egid, &sgid) < 0)
7753 return posix_error();
7754 /* Force the values into long's as we don't know the size of uid_t. */
7755 l_rgid = rgid;
7756 l_egid = egid;
7757 l_sgid = sgid;
7758 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007759}
7760#endif
7761
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007762static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007764#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007765 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007766#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007767 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007768#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007770#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007772#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007774#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007775#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007776 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007777#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007778#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007779 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007780#endif /* HAVE_LCHMOD */
7781#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007783#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007784#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007785 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007786#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007787#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007788 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007789#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007790#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007791 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007792#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007793#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007795#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007796#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7798 METH_NOARGS, posix_getcwd__doc__},
7799 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7800 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007801#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007802#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007804#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007805 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7806 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7807 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007808#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007810#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007811#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007813#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007814#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007815 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007816#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +00007817 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7818 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7819 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007820 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Brian Curtin52173d42010-12-02 18:29:18 +00007821#if defined(HAVE_SYMLINK) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007823#endif /* HAVE_SYMLINK */
Brian Curtin52173d42010-12-02 18:29:18 +00007824#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00007825 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
Brian Curtin52173d42010-12-02 18:29:18 +00007826 win_symlink__doc__},
7827#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007828#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007830#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007832#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007833 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007834#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007835 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7836 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7837 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007838#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007840#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007842#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007843 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7844 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007845#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007846#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7848 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007849#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7851 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007852#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007853#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007854#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007856#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007857#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007859#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007860#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007862#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007863#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007865#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007866#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007868#endif /* HAVE_GETEGID */
7869#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007871#endif /* HAVE_GETEUID */
7872#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007874#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007875#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007877#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007879#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007881#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007882#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007884#endif /* HAVE_GETPPID */
7885#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007887#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007888#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007890#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007891#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007893#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007894#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007896#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007897#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007899#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007900#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007901 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7902 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Brian Curtin1b9df392010-11-24 20:24:31 +00007903 {"link", win32_link, METH_VARARGS, win32_link__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007904#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007905#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007907#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007908#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007910#endif /* HAVE_SETEUID */
7911#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007913#endif /* HAVE_SETEGID */
7914#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007916#endif /* HAVE_SETREUID */
7917#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007919#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007920#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007922#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007923#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007925#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007926#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007927 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007928#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007929#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007931#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007932#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007933 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007934#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007935#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007937#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007938#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007940#endif /* HAVE_WAIT3 */
7941#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007942 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007943#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007944#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007946#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007947#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007949#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007950#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007952#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007953#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007955#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007956#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007958#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007959#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007961#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007962 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7963 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7964 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7965 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7966 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7967 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7968 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7969 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7970 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7971 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7972 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007973#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007975#endif
7976#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007978#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007979#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007981#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007982#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7984 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7985 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007986#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007987#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007988 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007989#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007990#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007992#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007993#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007995#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007996 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007997#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007999#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00008000#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008002#endif
8003#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00008005#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00008006#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00008007#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00008008 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00008009#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008010#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008011 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008012#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00008013#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00008014 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008015#endif /* WIFSTOPPED */
8016#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008018#endif /* WIFSIGNALED */
8019#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00008020 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008021#endif /* WIFEXITED */
8022#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00008023 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008024#endif /* WEXITSTATUS */
8025#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008027#endif /* WTERMSIG */
8028#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008029 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00008030#endif /* WSTOPSIG */
8031#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008032#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008034#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00008035#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00008037#endif
Fred Drakec9680921999-12-13 16:37:25 +00008038#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008040#endif
8041#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008043#endif
8044#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008046#endif
8047#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00008049#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008050 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008051#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008052 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00008053 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin62857742010-09-06 17:07:27 +00008054 {"_getfileinformation", posix__getfileinformation, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00008055#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00008056#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00008058#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008059 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00008061 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008062 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008064 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008065#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008067#endif
8068#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008070#endif
8071#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00008072 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008073#endif
8074#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00008075 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00008076#endif
8077
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008079};
8080
8081
Barry Warsaw4a342091996-12-19 23:50:02 +00008082static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00008083ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00008084{
Victor Stinner8c62be82010-05-06 00:08:46 +00008085 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00008086}
8087
Guido van Rossumd48f2521997-12-05 22:19:34 +00008088#if defined(PYOS_OS2)
8089/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008090static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008091{
8092 APIRET rc;
8093 ULONG values[QSV_MAX+1];
8094 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00008095 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00008096
8097 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00008098 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008099 Py_END_ALLOW_THREADS
8100
8101 if (rc != NO_ERROR) {
8102 os2_error(rc);
8103 return -1;
8104 }
8105
Fred Drake4d1e64b2002-04-15 19:40:07 +00008106 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
8107 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
8108 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
8109 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
8110 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
8111 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
8112 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008113
8114 switch (values[QSV_VERSION_MINOR]) {
8115 case 0: ver = "2.00"; break;
8116 case 10: ver = "2.10"; break;
8117 case 11: ver = "2.11"; break;
8118 case 30: ver = "3.00"; break;
8119 case 40: ver = "4.00"; break;
8120 case 50: ver = "5.00"; break;
8121 default:
Tim Peters885d4572001-11-28 20:27:42 +00008122 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00008124 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008125 ver = &tmp[0];
8126 }
8127
8128 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00008129 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00008130 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008131
8132 /* Add Indicator of Which Drive was Used to Boot the System */
8133 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
8134 tmp[1] = ':';
8135 tmp[2] = '\0';
8136
Fred Drake4d1e64b2002-04-15 19:40:07 +00008137 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00008138}
8139#endif
8140
Brian Curtin52173d42010-12-02 18:29:18 +00008141#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008142static int
Brian Curtin52173d42010-12-02 18:29:18 +00008143enable_symlink()
8144{
8145 HANDLE tok;
8146 TOKEN_PRIVILEGES tok_priv;
8147 LUID luid;
8148 int meth_idx = 0;
8149
8150 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008151 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008152
8153 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008154 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008155
8156 tok_priv.PrivilegeCount = 1;
8157 tok_priv.Privileges[0].Luid = luid;
8158 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
8159
8160 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
8161 sizeof(TOKEN_PRIVILEGES),
8162 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +00008163 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +00008164
Brian Curtin3b4499c2010-12-28 14:31:47 +00008165 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
8166 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +00008167}
8168#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
8169
Barry Warsaw4a342091996-12-19 23:50:02 +00008170static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008171all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00008172{
Guido van Rossum94f6f721999-01-06 18:42:14 +00008173#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008174 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008175#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008176#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008178#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008179#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008180 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008181#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008182#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008184#endif
Fred Drakec9680921999-12-13 16:37:25 +00008185#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008186 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008187#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008188#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008189 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008190#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008191#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008192 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008193#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008194#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008195 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008196#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008197#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008198 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008199#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008200#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008201 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008202#endif
8203#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008204 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008205#endif
8206#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008207 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008208#endif
8209#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008210 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008211#endif
8212#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008213 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008214#endif
8215#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008216 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008217#endif
8218#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008219 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008220#endif
8221#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008222 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008223#endif
8224#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008225 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008226#endif
8227#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008228 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008229#endif
8230#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008231 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008232#endif
8233#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008234 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008235#endif
8236#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008237 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008238#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008239#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008240 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008241#endif
8242#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008244#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008245#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008246 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008247#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008248#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008249 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008250#endif
8251#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008253#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008254
Tim Peters5aa91602002-01-30 05:46:57 +00008255/* MS Windows */
8256#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008257 /* Don't inherit in child processes. */
8258 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008259#endif
8260#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 /* Optimize for short life (keep in memory). */
8262 /* MS forgot to define this one with a non-underscore form too. */
8263 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008264#endif
8265#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008266 /* Automatically delete when last handle is closed. */
8267 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008268#endif
8269#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008270 /* Optimize for random access. */
8271 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008272#endif
8273#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 /* Optimize for sequential access. */
8275 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008276#endif
8277
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008278/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008279#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 /* Send a SIGIO signal whenever input or output
8281 becomes available on file descriptor */
8282 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008283#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008284#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 /* Direct disk access. */
8286 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008287#endif
8288#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008289 /* Must be a directory. */
8290 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008291#endif
8292#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 /* Do not follow links. */
8294 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008295#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008296#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008297 /* Do not update the access time. */
8298 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008299#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008300
Victor Stinner8c62be82010-05-06 00:08:46 +00008301 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008302#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008303 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008304#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008305#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008306 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008307#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008308#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008309 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008310#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008311#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008312 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008313#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008314#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008315 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008316#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008317#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008318 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008319#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008320#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008321 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008322#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008323#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008324 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008325#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008326#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008327 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008328#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008329#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008330 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008331#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008332#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008334#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008335#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008336 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008337#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008338#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008339 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008340#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008341#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008343#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008344#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008346#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008347#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008348 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008349#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008350#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008351 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008352#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008353
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008354 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008355#ifdef ST_RDONLY
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008356 if (ins(d, "ST_RDONLY", (long)ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008357#endif /* ST_RDONLY */
8358#ifdef ST_NOSUID
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +00008359 if (ins(d, "ST_NOSUID", (long)ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +00008360#endif /* ST_NOSUID */
8361
Guido van Rossum246bc171999-02-01 23:54:31 +00008362#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008363#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008364 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8365 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8366 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8367 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8368 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8369 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8370 if (ins(d, "P_PM", (long)P_PM)) return -1;
8371 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8372 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8373 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8374 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8375 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8376 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8377 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8378 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8379 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8380 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8381 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8382 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8383 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008384#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008385 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8386 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8387 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8388 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8389 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008390#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008391#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008392
Guido van Rossumd48f2521997-12-05 22:19:34 +00008393#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008394 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008395#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008397}
8398
8399
Tim Peters5aa91602002-01-30 05:46:57 +00008400#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008401#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008402#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008403
8404#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008405#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008406#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008407
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008408#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008409#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008410#define MODNAME "posix"
8411#endif
8412
Martin v. Löwis1a214512008-06-11 05:26:20 +00008413static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 PyModuleDef_HEAD_INIT,
8415 MODNAME,
8416 posix__doc__,
8417 -1,
8418 posix_methods,
8419 NULL,
8420 NULL,
8421 NULL,
8422 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008423};
8424
8425
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008426PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008427INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008428{
Victor Stinner8c62be82010-05-06 00:08:46 +00008429 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008430
Brian Curtin52173d42010-12-02 18:29:18 +00008431#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +00008432 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +00008433#endif
8434
Victor Stinner8c62be82010-05-06 00:08:46 +00008435 m = PyModule_Create(&posixmodule);
8436 if (m == NULL)
8437 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008438
Victor Stinner8c62be82010-05-06 00:08:46 +00008439 /* Initialize environ dictionary */
8440 v = convertenviron();
8441 Py_XINCREF(v);
8442 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8443 return NULL;
8444 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008445
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 if (all_ins(m))
8447 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008448
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 if (setup_confname_tables(m))
8450 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008451
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 Py_INCREF(PyExc_OSError);
8453 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008454
Guido van Rossumb3d39562000-01-31 18:41:26 +00008455#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008456 if (posix_putenv_garbage == NULL)
8457 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008458#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008459
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 if (!initialized) {
8461 stat_result_desc.name = MODNAME ".stat_result";
8462 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8463 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8464 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8465 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8466 structseq_new = StatResultType.tp_new;
8467 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008468
Victor Stinner8c62be82010-05-06 00:08:46 +00008469 statvfs_result_desc.name = MODNAME ".statvfs_result";
8470 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008471#ifdef NEED_TICKS_PER_SECOND
8472# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008473 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008474# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008475 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008476# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008477 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008478# endif
8479#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008480 }
8481 Py_INCREF((PyObject*) &StatResultType);
8482 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8483 Py_INCREF((PyObject*) &StatVFSResultType);
8484 PyModule_AddObject(m, "statvfs_result",
8485 (PyObject*) &StatVFSResultType);
8486 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008487
8488#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 /*
8490 * Step 2 of weak-linking support on Mac OS X.
8491 *
8492 * The code below removes functions that are not available on the
8493 * currently active platform.
8494 *
8495 * This block allow one to use a python binary that was build on
8496 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8497 * OSX 10.4.
8498 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008499#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 if (fstatvfs == NULL) {
8501 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8502 return NULL;
8503 }
8504 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008505#endif /* HAVE_FSTATVFS */
8506
8507#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008508 if (statvfs == NULL) {
8509 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8510 return NULL;
8511 }
8512 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008513#endif /* HAVE_STATVFS */
8514
8515# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008516 if (lchown == NULL) {
8517 if (PyObject_DelAttrString(m, "lchown") == -1) {
8518 return NULL;
8519 }
8520 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008521#endif /* HAVE_LCHOWN */
8522
8523
8524#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008525 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008526
Guido van Rossumb6775db1994-08-01 11:34:53 +00008527}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008528
8529#ifdef __cplusplus
8530}
8531#endif