blob: 73fab71ee212785723884a07d6efe29ee097e719 [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"
31#include "structseq.h"
32
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#if defined(__VMS)
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000034# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000035#endif /* defined(__VMS) */
36
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000041PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000042"This module provides access to operating system functionality that is\n\
43standardized by the C Standard and the POSIX standard (a thinly\n\
44disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000045corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000047
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000048#if defined(PYOS_OS2)
49#define INCL_DOS
50#define INCL_DOSERRORS
51#define INCL_DOSPROCESS
52#define INCL_NOPMAPI
53#include <os2.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000054#if defined(PYCC_GCC)
55#include <ctype.h>
56#include <io.h>
57#include <stdio.h>
58#include <process.h>
Andrew MacIntyre6c73af22002-03-03 03:07:07 +000059#endif
Andrew MacIntyreda4d6cb2004-03-29 11:53:38 +000060#include "osdefs.h"
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000064#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#endif /* HAVE_SYS_TYPES_H */
66
67#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000068#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070
Guido van Rossum36bc6801995-06-14 22:54:23 +000071#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000072#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000073#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000074
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000076#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000078
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#ifdef HAVE_FCNTL_H
80#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000081#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000082
Guido van Rossuma6535fd2001-10-18 19:44:10 +000083#ifdef HAVE_GRP_H
84#include <grp.h>
85#endif
86
Barry Warsaw5676bd12003-01-07 20:57:09 +000087#ifdef HAVE_SYSEXITS_H
88#include <sysexits.h>
89#endif /* HAVE_SYSEXITS_H */
90
Anthony Baxter8a560de2004-10-13 15:30:56 +000091#ifdef HAVE_SYS_LOADAVG_H
92#include <sys/loadavg.h>
93#endif
94
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000095#ifdef HAVE_LANGINFO_H
96#include <langinfo.h>
97#endif
98
Guido van Rossuma4916fa1996-05-23 22:58:55 +000099/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000100/* XXX Gosh I wish these were all moved into pyconfig.h */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000102#include <process.h>
103#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000104#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000105#define HAVE_GETCWD 1
106#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000107#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000108#if defined(__OS2__)
109#define HAVE_EXECV 1
110#define HAVE_WAIT 1
Guido van Rossumad0ee831995-03-01 10:34:45 +0000111#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000112#include <process.h>
113#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000114#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#define HAVE_EXECV 1
116#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000117#define HAVE_OPENDIR 1
118#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000119#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000120#define HAVE_WAIT 1
121#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000122#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000123#define HAVE_GETCWD 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000124#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000125#define HAVE_EXECV 1
126#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000127#define HAVE_SYSTEM 1
128#define HAVE_CWAIT 1
129#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000130#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000131#else
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(PYOS_OS2) && defined(PYCC_GCC) || defined(__VMS)
133/* Everything needed is defined in PC/os2emx/pyconfig.h or vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000134#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000135/* Unix functions that the configure script doesn't check for */
136#define HAVE_EXECV 1
137#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000138#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000139#define HAVE_FORK1 1
140#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000141#define HAVE_GETCWD 1
142#define HAVE_GETEGID 1
143#define HAVE_GETEUID 1
144#define HAVE_GETGID 1
145#define HAVE_GETPPID 1
146#define HAVE_GETUID 1
147#define HAVE_KILL 1
148#define HAVE_OPENDIR 1
149#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000150#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_TTYNAME 1
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000153#endif /* PYOS_OS2 && PYCC_GCC && __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#endif /* _MSC_VER */
155#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000156#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000157#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000158
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000160
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000161#if defined(__sgi)&&_COMPILER_VERSION>=700
162/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
163 (default) */
164extern char *ctermid_r(char *);
165#endif
166
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000167#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000169extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000170#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000171#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000172extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000174extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000175#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000176#endif
177#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000178extern int chdir(char *);
179extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000180#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000181extern int chdir(const char *);
182extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000183#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000184#ifdef __BORLANDC__
185extern int chmod(const char *, int);
186#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000187extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000188#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000189/*#ifdef HAVE_FCHMOD
190extern int fchmod(int, mode_t);
191#endif*/
192/*#ifdef HAVE_LCHMOD
193extern int lchmod(const char *, mode_t);
194#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000195extern int chown(const char *, uid_t, gid_t);
196extern char *getcwd(char *, int);
197extern char *strerror(int);
198extern int link(const char *, const char *);
199extern int rename(const char *, const char *);
200extern int stat(const char *, struct stat *);
201extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000204#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000207#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000209
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000210#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212#ifdef HAVE_UTIME_H
213#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000216#ifdef HAVE_SYS_UTIME_H
217#include <sys/utime.h>
218#define HAVE_UTIME_H /* pretend we do for the rest of this file */
219#endif /* HAVE_SYS_UTIME_H */
220
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#ifdef HAVE_SYS_TIMES_H
222#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000223#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224
225#ifdef HAVE_SYS_PARAM_H
226#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000227#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
229#ifdef HAVE_SYS_UTSNAME_H
230#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000231#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#define NAMLEN(dirent) strlen((dirent)->d_name)
236#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000237#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000238#include <direct.h>
239#define NAMLEN(dirent) strlen((dirent)->d_name)
240#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000242#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000243#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#endif
247#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000248#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000249#endif
250#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000252#endif
253#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000255#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000256#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258#endif
259#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000261#endif
262#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000265#ifndef VOLUME_NAME_DOS
266#define VOLUME_NAME_DOS 0x0
267#endif
268#ifndef VOLUME_NAME_NT
269#define VOLUME_NAME_NT 0x2
270#endif
271#ifndef IO_REPARSE_TAG_SYMLINK
272#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
273#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000274#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000275#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000277#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000278#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279
Guido van Rossumd48f2521997-12-05 22:19:34 +0000280#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000282#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283
Tim Petersbc2e10e2002-03-03 23:17:02 +0000284#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000285#if defined(PATH_MAX) && PATH_MAX > 1024
286#define MAXPATHLEN PATH_MAX
287#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000288#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000289#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000290#endif /* MAXPATHLEN */
291
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000292#ifdef UNION_WAIT
293/* Emulate some macros on systems that have a union instead of macros */
294
295#ifndef WIFEXITED
296#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
297#endif
298
299#ifndef WEXITSTATUS
300#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
301#endif
302
303#ifndef WTERMSIG
304#define WTERMSIG(u_wait) ((u_wait).w_termsig)
305#endif
306
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307#define WAIT_TYPE union wait
308#define WAIT_STATUS_INT(s) (s.w_status)
309
310#else /* !UNION_WAIT */
311#define WAIT_TYPE int
312#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000313#endif /* UNION_WAIT */
314
Greg Wardb48bc172000-03-01 21:51:56 +0000315/* Don't use the "_r" form if we don't need it (also, won't have a
316 prototype for it, at least on Solaris -- maybe others as well?). */
317#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
318#define USE_CTERMID_R
319#endif
320
Fred Drake699f3522000-06-29 21:12:41 +0000321/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000322#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000323#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000324# define STAT win32_stat
325# define FSTAT win32_fstat
326# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000327#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000328# define STAT stat
329# define FSTAT fstat
330# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000331#endif
332
Tim Peters11b23062003-04-23 02:39:17 +0000333#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000334#include <sys/mkdev.h>
335#else
336#if defined(MAJOR_IN_SYSMACROS)
337#include <sys/sysmacros.h>
338#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000339#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
340#include <sys/mkdev.h>
341#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000342#endif
Fred Drake699f3522000-06-29 21:12:41 +0000343
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000344#if defined _MSC_VER && _MSC_VER >= 1400
345/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
346 * valid and throw an assertion if it isn't.
347 * Normally, an invalid fd is likely to be a C program error and therefore
348 * an assertion can be useful, but it does contradict the POSIX standard
349 * which for write(2) states:
350 * "Otherwise, -1 shall be returned and errno set to indicate the error."
351 * "[EBADF] The fildes argument is not a valid file descriptor open for
352 * writing."
353 * Furthermore, python allows the user to enter any old integer
354 * as a fd and should merely raise a python exception on error.
355 * The Microsoft CRT doesn't provide an official way to check for the
356 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000357 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000358 * internal structures involved.
359 * The structures below must be updated for each version of visual studio
360 * according to the file internal.h in the CRT source, until MS comes
361 * up with a less hacky way to do this.
362 * (all of this is to avoid globally modifying the CRT behaviour using
363 * _set_invalid_parameter_handler() and _CrtSetReportMode())
364 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000365/* The actual size of the structure is determined at runtime.
366 * Only the first items must be present.
367 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000368typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000369 intptr_t osfhnd;
370 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000371} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000372
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000373extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000374#define IOINFO_L2E 5
375#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
376#define IOINFO_ARRAYS 64
377#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
378#define FOPEN 0x01
379#define _NO_CONSOLE_FILENO (intptr_t)-2
380
381/* This function emulates what the windows CRT does to validate file handles */
382int
383_PyVerify_fd(int fd)
384{
Victor Stinner8c62be82010-05-06 00:08:46 +0000385 const int i1 = fd >> IOINFO_L2E;
386 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000387
Victor Stinner8c62be82010-05-06 00:08:46 +0000388 static int sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000389
Victor Stinner8c62be82010-05-06 00:08:46 +0000390 /* Determine the actual size of the ioinfo structure,
391 * as used by the CRT loaded in memory
392 */
393 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
394 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
395 }
396 if (sizeof_ioinfo == 0) {
397 /* This should not happen... */
398 goto fail;
399 }
400
401 /* See that it isn't a special CLEAR fileno */
402 if (fd != _NO_CONSOLE_FILENO) {
403 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
404 * we check pointer validity and other info
405 */
406 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
407 /* finally, check that the file is open */
408 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
409 if (info->osfile & FOPEN) {
410 return 1;
411 }
412 }
413 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000414 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000415 errno = EBADF;
416 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000417}
418
419/* the special case of checking dup2. The target fd must be in a sensible range */
420static int
421_PyVerify_fd_dup2(int fd1, int fd2)
422{
Victor Stinner8c62be82010-05-06 00:08:46 +0000423 if (!_PyVerify_fd(fd1))
424 return 0;
425 if (fd2 == _NO_CONSOLE_FILENO)
426 return 0;
427 if ((unsigned)fd2 < _NHANDLE_)
428 return 1;
429 else
430 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000431}
432#else
433/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
434#define _PyVerify_fd_dup2(A, B) (1)
435#endif
436
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000438#ifdef WITH_NEXT_FRAMEWORK
439/* On Darwin/MacOSX a shared library or framework has no access to
440** environ directly, we must obtain it with _NSGetEnviron().
441*/
442#include <crt_externs.h>
443static char **environ;
444#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000446#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447
Barry Warsaw53699e91996-12-10 23:23:01 +0000448static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000449convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450{
Victor Stinner8c62be82010-05-06 00:08:46 +0000451 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000452#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000453 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000454#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000455 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000456#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000457#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000458 APIRET rc;
459 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
460#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000461
Victor Stinner8c62be82010-05-06 00:08:46 +0000462 d = PyDict_New();
463 if (d == NULL)
464 return NULL;
465#ifdef WITH_NEXT_FRAMEWORK
466 if (environ == NULL)
467 environ = *_NSGetEnviron();
468#endif
469#ifdef MS_WINDOWS
470 /* _wenviron must be initialized in this way if the program is started
471 through main() instead of wmain(). */
472 _wgetenv(L"");
473 if (_wenviron == NULL)
474 return d;
475 /* This part ignores errors */
476 for (e = _wenviron; *e != NULL; e++) {
477 PyObject *k;
478 PyObject *v;
479 wchar_t *p = wcschr(*e, L'=');
480 if (p == NULL)
481 continue;
482 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
483 if (k == NULL) {
484 PyErr_Clear();
485 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000486 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000487 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
488 if (v == NULL) {
489 PyErr_Clear();
490 Py_DECREF(k);
491 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000492 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000493 if (PyDict_GetItem(d, k) == NULL) {
494 if (PyDict_SetItem(d, k, v) != 0)
495 PyErr_Clear();
496 }
497 Py_DECREF(k);
498 Py_DECREF(v);
499 }
500#else
501 if (environ == NULL)
502 return d;
503 /* This part ignores errors */
504 for (e = environ; *e != NULL; e++) {
505 PyObject *k;
506 PyObject *v;
507 char *p = strchr(*e, '=');
508 if (p == NULL)
509 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000510 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000511 if (k == NULL) {
512 PyErr_Clear();
513 continue;
514 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000515 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000516 if (v == NULL) {
517 PyErr_Clear();
518 Py_DECREF(k);
519 continue;
520 }
521 if (PyDict_GetItem(d, k) == NULL) {
522 if (PyDict_SetItem(d, k, v) != 0)
523 PyErr_Clear();
524 }
525 Py_DECREF(k);
526 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000527 }
528#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000529#if defined(PYOS_OS2)
530 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
531 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
532 PyObject *v = PyBytes_FromString(buffer);
533 PyDict_SetItemString(d, "BEGINLIBPATH", v);
534 Py_DECREF(v);
535 }
536 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
537 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
538 PyObject *v = PyBytes_FromString(buffer);
539 PyDict_SetItemString(d, "ENDLIBPATH", v);
540 Py_DECREF(v);
541 }
542#endif
543 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544}
545
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546/* Set a POSIX-specific error from errno, and return NULL */
547
Barry Warsawd58d7641998-07-23 16:14:40 +0000548static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000549posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000550{
Victor Stinner8c62be82010-05-06 00:08:46 +0000551 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552}
Barry Warsawd58d7641998-07-23 16:14:40 +0000553static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000554posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000555{
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000557}
558
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000559#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000560static PyObject *
561posix_error_with_unicode_filename(Py_UNICODE* name)
562{
Victor Stinner8c62be82010-05-06 00:08:46 +0000563 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000564}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000565#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000566
567
Mark Hammondef8b6542001-05-13 08:04:26 +0000568static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000569posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000570{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000571 PyObject *name_str, *rc;
572 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
573 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000574 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000575 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
576 name_str);
577 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000578 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000579}
580
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000581#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000582static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000583win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000584{
Victor Stinner8c62be82010-05-06 00:08:46 +0000585 /* XXX We should pass the function name along in the future.
586 (winreg.c also wants to pass the function name.)
587 This would however require an additional param to the
588 Windows error object, which is non-trivial.
589 */
590 errno = GetLastError();
591 if (filename)
592 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
593 else
594 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000595}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000596
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000597static PyObject *
598win32_error_unicode(char* function, Py_UNICODE* filename)
599{
Victor Stinner8c62be82010-05-06 00:08:46 +0000600 /* XXX - see win32_error for comments on 'function' */
601 errno = GetLastError();
602 if (filename)
603 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
604 else
605 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000606}
607
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000609convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000610{
Victor Stinner8c62be82010-05-06 00:08:46 +0000611 if (PyUnicode_CheckExact(*param))
612 Py_INCREF(*param);
613 else if (PyUnicode_Check(*param))
614 /* For a Unicode subtype that's not a Unicode object,
615 return a true Unicode object with the same data. */
616 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
617 PyUnicode_GET_SIZE(*param));
618 else
619 *param = PyUnicode_FromEncodedObject(*param,
620 Py_FileSystemDefaultEncoding,
621 "strict");
622 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000623}
624
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000625#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626
Guido van Rossumd48f2521997-12-05 22:19:34 +0000627#if defined(PYOS_OS2)
628/**********************************************************************
629 * Helper Function to Trim and Format OS/2 Messages
630 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000631static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000632os2_formatmsg(char *msgbuf, int msglen, char *reason)
633{
634 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
635
636 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
637 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
638
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000639 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000640 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
641 }
642
643 /* Add Optional Reason Text */
644 if (reason) {
645 strcat(msgbuf, " : ");
646 strcat(msgbuf, reason);
647 }
648}
649
650/**********************************************************************
651 * Decode an OS/2 Operating System Error Code
652 *
653 * A convenience function to lookup an OS/2 error code and return a
654 * text message we can use to raise a Python exception.
655 *
656 * Notes:
657 * The messages for errors returned from the OS/2 kernel reside in
658 * the file OSO001.MSG in the \OS2 directory hierarchy.
659 *
660 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000661static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000662os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
663{
664 APIRET rc;
665 ULONG msglen;
666
667 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
668 Py_BEGIN_ALLOW_THREADS
669 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
670 errorcode, "oso001.msg", &msglen);
671 Py_END_ALLOW_THREADS
672
673 if (rc == NO_ERROR)
674 os2_formatmsg(msgbuf, msglen, reason);
675 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000676 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000677 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000678
679 return msgbuf;
680}
681
682/* Set an OS/2-specific error and return NULL. OS/2 kernel
683 errors are not in a global variable e.g. 'errno' nor are
684 they congruent with posix error numbers. */
685
Victor Stinner8c62be82010-05-06 00:08:46 +0000686static PyObject *
687os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000688{
689 char text[1024];
690 PyObject *v;
691
692 os2_strerror(text, sizeof(text), code, "");
693
694 v = Py_BuildValue("(is)", code, text);
695 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000696 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000697 Py_DECREF(v);
698 }
699 return NULL; /* Signal to Python that an Exception is Pending */
700}
701
702#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703
704/* POSIX generic methods */
705
Barry Warsaw53699e91996-12-10 23:23:01 +0000706static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000707posix_fildes(PyObject *fdobj, int (*func)(int))
708{
Victor Stinner8c62be82010-05-06 00:08:46 +0000709 int fd;
710 int res;
711 fd = PyObject_AsFileDescriptor(fdobj);
712 if (fd < 0)
713 return NULL;
714 if (!_PyVerify_fd(fd))
715 return posix_error();
716 Py_BEGIN_ALLOW_THREADS
717 res = (*func)(fd);
718 Py_END_ALLOW_THREADS
719 if (res < 0)
720 return posix_error();
721 Py_INCREF(Py_None);
722 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000723}
Guido van Rossum21142a01999-01-08 21:05:37 +0000724
725static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000726posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727{
Victor Stinner8c62be82010-05-06 00:08:46 +0000728 PyObject *opath1 = NULL;
729 char *path1;
730 int res;
731 if (!PyArg_ParseTuple(args, format,
732 PyUnicode_FSConverter, &opath1))
733 return NULL;
734 path1 = PyBytes_AsString(opath1);
735 Py_BEGIN_ALLOW_THREADS
736 res = (*func)(path1);
737 Py_END_ALLOW_THREADS
738 if (res < 0)
739 return posix_error_with_allocated_filename(opath1);
740 Py_DECREF(opath1);
741 Py_INCREF(Py_None);
742 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000743}
744
Barry Warsaw53699e91996-12-10 23:23:01 +0000745static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000746posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000747 char *format,
748 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000749{
Victor Stinner8c62be82010-05-06 00:08:46 +0000750 PyObject *opath1 = NULL, *opath2 = NULL;
751 char *path1, *path2;
752 int res;
753 if (!PyArg_ParseTuple(args, format,
754 PyUnicode_FSConverter, &opath1,
755 PyUnicode_FSConverter, &opath2)) {
756 return NULL;
757 }
758 path1 = PyBytes_AsString(opath1);
759 path2 = PyBytes_AsString(opath2);
760 Py_BEGIN_ALLOW_THREADS
761 res = (*func)(path1, path2);
762 Py_END_ALLOW_THREADS
763 Py_DECREF(opath1);
764 Py_DECREF(opath2);
765 if (res != 0)
766 /* XXX how to report both path1 and path2??? */
767 return posix_error();
768 Py_INCREF(Py_None);
769 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770}
771
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000772#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000773static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000774win32_1str(PyObject* args, char* func,
775 char* format, BOOL (__stdcall *funcA)(LPCSTR),
776 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000777{
Victor Stinner8c62be82010-05-06 00:08:46 +0000778 PyObject *uni;
779 char *ansi;
780 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000781
Victor Stinner8c62be82010-05-06 00:08:46 +0000782 if (!PyArg_ParseTuple(args, wformat, &uni))
783 PyErr_Clear();
784 else {
785 Py_BEGIN_ALLOW_THREADS
786 result = funcW(PyUnicode_AsUnicode(uni));
787 Py_END_ALLOW_THREADS
788 if (!result)
789 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
790 Py_INCREF(Py_None);
791 return Py_None;
792 }
793 if (!PyArg_ParseTuple(args, format, &ansi))
794 return NULL;
795 Py_BEGIN_ALLOW_THREADS
796 result = funcA(ansi);
797 Py_END_ALLOW_THREADS
798 if (!result)
799 return win32_error(func, ansi);
800 Py_INCREF(Py_None);
801 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802
803}
804
805/* This is a reimplementation of the C library's chdir function,
806 but one that produces Win32 errors instead of DOS error codes.
807 chdir is essentially a wrapper around SetCurrentDirectory; however,
808 it also needs to set "magic" environment variables indicating
809 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000810static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811win32_chdir(LPCSTR path)
812{
Victor Stinner8c62be82010-05-06 00:08:46 +0000813 char new_path[MAX_PATH+1];
814 int result;
815 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816
Victor Stinner8c62be82010-05-06 00:08:46 +0000817 if(!SetCurrentDirectoryA(path))
818 return FALSE;
819 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
820 if (!result)
821 return FALSE;
822 /* In the ANSI API, there should not be any paths longer
823 than MAX_PATH. */
824 assert(result <= MAX_PATH+1);
825 if (strncmp(new_path, "\\\\", 2) == 0 ||
826 strncmp(new_path, "//", 2) == 0)
827 /* UNC path, nothing to do. */
828 return TRUE;
829 env[1] = new_path[0];
830 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831}
832
833/* The Unicode version differs from the ANSI version
834 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000835static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000836win32_wchdir(LPCWSTR path)
837{
Victor Stinner8c62be82010-05-06 00:08:46 +0000838 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
839 int result;
840 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841
Victor Stinner8c62be82010-05-06 00:08:46 +0000842 if(!SetCurrentDirectoryW(path))
843 return FALSE;
844 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
845 if (!result)
846 return FALSE;
847 if (result > MAX_PATH+1) {
848 new_path = malloc(result * sizeof(wchar_t));
849 if (!new_path) {
850 SetLastError(ERROR_OUTOFMEMORY);
851 return FALSE;
852 }
853 result = GetCurrentDirectoryW(result, new_path);
854 if (!result) {
855 free(new_path);
856 return FALSE;
857 }
858 }
859 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
860 wcsncmp(new_path, L"//", 2) == 0)
861 /* UNC path, nothing to do. */
862 return TRUE;
863 env[1] = new_path[0];
864 result = SetEnvironmentVariableW(env, new_path);
865 if (new_path != _new_path)
866 free(new_path);
867 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000868}
869#endif
870
Martin v. Löwis14694662006-02-03 12:54:16 +0000871#ifdef MS_WINDOWS
872/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
873 - time stamps are restricted to second resolution
874 - file modification times suffer from forth-and-back conversions between
875 UTC and local time
876 Therefore, we implement our own stat, based on the Win32 API directly.
877*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000878#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000879
880struct win32_stat{
881 int st_dev;
882 __int64 st_ino;
883 unsigned short st_mode;
884 int st_nlink;
885 int st_uid;
886 int st_gid;
887 int st_rdev;
888 __int64 st_size;
889 int st_atime;
890 int st_atime_nsec;
891 int st_mtime;
892 int st_mtime_nsec;
893 int st_ctime;
894 int st_ctime_nsec;
895};
896
897static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
898
899static void
900FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
901{
Victor Stinner8c62be82010-05-06 00:08:46 +0000902 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
903 /* Cannot simply cast and dereference in_ptr,
904 since it might not be aligned properly */
905 __int64 in;
906 memcpy(&in, in_ptr, sizeof(in));
907 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
908 /* XXX Win32 supports time stamps past 2038; we currently don't */
909 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000910}
911
Thomas Wouters477c8d52006-05-27 19:21:47 +0000912static void
913time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
914{
Victor Stinner8c62be82010-05-06 00:08:46 +0000915 /* XXX endianness */
916 __int64 out;
917 out = time_in + secs_between_epochs;
918 out = out * 10000000 + nsec_in / 100;
919 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920}
921
Martin v. Löwis14694662006-02-03 12:54:16 +0000922/* Below, we *know* that ugo+r is 0444 */
923#if _S_IREAD != 0400
924#error Unsupported C library
925#endif
926static int
927attributes_to_mode(DWORD attr)
928{
Victor Stinner8c62be82010-05-06 00:08:46 +0000929 int m = 0;
930 if (attr & FILE_ATTRIBUTE_DIRECTORY)
931 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
932 else
933 m |= _S_IFREG;
934 if (attr & FILE_ATTRIBUTE_READONLY)
935 m |= 0444;
936 else
937 m |= 0666;
938 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000939}
940
941static int
942attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
943{
Victor Stinner8c62be82010-05-06 00:08:46 +0000944 memset(result, 0, sizeof(*result));
945 result->st_mode = attributes_to_mode(info->dwFileAttributes);
946 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
947 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
948 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
949 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000950
Victor Stinner8c62be82010-05-06 00:08:46 +0000951 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000952}
953
Guido van Rossumd8faa362007-04-27 19:54:29 +0000954static BOOL
955attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
956{
Victor Stinner8c62be82010-05-06 00:08:46 +0000957 HANDLE hFindFile;
958 WIN32_FIND_DATAA FileData;
959 hFindFile = FindFirstFileA(pszFile, &FileData);
960 if (hFindFile == INVALID_HANDLE_VALUE)
961 return FALSE;
962 FindClose(hFindFile);
963 pfad->dwFileAttributes = FileData.dwFileAttributes;
964 pfad->ftCreationTime = FileData.ftCreationTime;
965 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
966 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
967 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
968 pfad->nFileSizeLow = FileData.nFileSizeLow;
969 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000970}
971
972static BOOL
973attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
974{
Victor Stinner8c62be82010-05-06 00:08:46 +0000975 HANDLE hFindFile;
976 WIN32_FIND_DATAW FileData;
977 hFindFile = FindFirstFileW(pszFile, &FileData);
978 if (hFindFile == INVALID_HANDLE_VALUE)
979 return FALSE;
980 FindClose(hFindFile);
981 pfad->dwFileAttributes = FileData.dwFileAttributes;
982 pfad->ftCreationTime = FileData.ftCreationTime;
983 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
984 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
985 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
986 pfad->nFileSizeLow = FileData.nFileSizeLow;
987 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988}
989
Brian Curtind40e6f72010-07-08 21:39:08 +0000990/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
991 win32_stat_w
992
993 In Posix, stat automatically traverses symlinks and returns the stat
994 structure for the target. In Windows, the equivalent GetFileAttributes by
995 default does not traverse symlinks and instead returns attributes for
996 the symlink.
997
998 Therefore, win32_lstat will get the attributes traditionally, and
999 win32_stat will first explicitly resolve the symlink target and then will
1000 call win32_lstat on that result.
1001
1002 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
1003
1004static int
1005win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001006{
Victor Stinner8c62be82010-05-06 00:08:46 +00001007 WIN32_FILE_ATTRIBUTE_DATA info;
1008 int code;
1009 char *dot;
Brian Curtind40e6f72010-07-08 21:39:08 +00001010 WIN32_FIND_DATAA find_data;
1011 HANDLE find_data_handle;
Victor Stinner8c62be82010-05-06 00:08:46 +00001012 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1013 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1014 /* Protocol violation: we explicitly clear errno, instead of
1015 setting it to a POSIX error. Callers should use GetLastError. */
1016 errno = 0;
1017 return -1;
1018 } else {
1019 /* Could not get attributes on open file. Fall back to
1020 reading the directory. */
1021 if (!attributes_from_dir(path, &info)) {
1022 /* Very strange. This should not fail now */
1023 errno = 0;
1024 return -1;
1025 }
1026 }
1027 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001028
Victor Stinner8c62be82010-05-06 00:08:46 +00001029 code = attribute_data_to_stat(&info, result);
1030 if (code != 0)
1031 return code;
Brian Curtind40e6f72010-07-08 21:39:08 +00001032
1033 /* Get WIN32_FIND_DATA structure for the path to determine if
1034 it is a symlink */
1035 if(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1036 find_data_handle = FindFirstFileA(path, &find_data);
1037 if(find_data_handle != INVALID_HANDLE_VALUE) {
1038 if(find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
1039 /* first clear the S_IFMT bits */
1040 result->st_mode ^= (result->st_mode & 0170000);
1041 /* now set the bits that make this a symlink */
1042 result->st_mode |= 0120000;
1043 }
1044 FindClose(find_data_handle);
1045 }
1046 }
1047
Victor Stinner8c62be82010-05-06 00:08:46 +00001048 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1049 dot = strrchr(path, '.');
1050 if (dot) {
Brian Curtind40e6f72010-07-08 21:39:08 +00001051 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1052 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00001053 result->st_mode |= 0111;
1054 }
1055 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001056}
1057
Victor Stinner8c62be82010-05-06 00:08:46 +00001058static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001059win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001060{
Victor Stinner8c62be82010-05-06 00:08:46 +00001061 int code;
1062 const wchar_t *dot;
1063 WIN32_FILE_ATTRIBUTE_DATA info;
Brian Curtind40e6f72010-07-08 21:39:08 +00001064 WIN32_FIND_DATAW find_data;
1065 HANDLE find_data_handle;
Victor Stinner8c62be82010-05-06 00:08:46 +00001066 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1067 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1068 /* Protocol violation: we explicitly clear errno, instead of
1069 setting it to a POSIX error. Callers should use GetLastError. */
1070 errno = 0;
1071 return -1;
1072 } else {
Brian Curtind40e6f72010-07-08 21:39:08 +00001073 /* Could not get attributes on open file. Fall back to reading
1074 the directory. */
1075 if (!attributes_from_dir_w(path, &info)) {
1076 /* Very strange. This should not fail now */
1077 errno = 0;
1078 return -1;
1079 }
1080 }
1081 }
1082 code = attribute_data_to_stat(&info, result);
1083 if (code < 0)
1084 return code;
1085
1086 /* Get WIN32_FIND_DATA structure for the path to determine if
1087 it is a symlink */
1088 if(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1089 find_data_handle = FindFirstFileW(path, &find_data);
1090 if(find_data_handle != INVALID_HANDLE_VALUE) {
1091 if(find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
1092 /* first clear the S_IFMT bits */
1093 result->st_mode ^= (result->st_mode & 0170000);
1094 /* now set the bits that make this a symlink */
1095 result->st_mode |= 0120000;
1096 }
1097 FindClose(find_data_handle);
1098 }
1099 }
1100
1101 /* Set IFEXEC if it is an .exe, .bat, ... */
1102 dot = wcsrchr(path, '.');
1103 if (dot) {
1104 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1105 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1106 result->st_mode |= 0111;
1107 }
1108 return code;
1109}
1110
1111/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1112static int has_GetFinalPathNameByHandle = 0;
1113static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1114 DWORD);
1115static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1116 DWORD);
1117static int
1118check_GetFinalPathNameByHandle()
1119{
1120 HINSTANCE hKernel32;
1121 /* only recheck */
1122 if (!has_GetFinalPathNameByHandle)
1123 {
1124 hKernel32 = GetModuleHandle("KERNEL32");
1125 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1126 "GetFinalPathNameByHandleA");
1127 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1128 "GetFinalPathNameByHandleW");
Brian Curtin74e45612010-07-09 15:58:59 +00001129 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1130 Py_GetFinalPathNameByHandleW;
Brian Curtind40e6f72010-07-08 21:39:08 +00001131 }
1132 return has_GetFinalPathNameByHandle;
1133}
1134
1135static int
1136win32_stat(const char* path, struct win32_stat *result)
1137{
1138 /* Traverse the symlink to the target using
1139 GetFinalPathNameByHandle()
1140 */
1141 int code;
1142 HANDLE hFile;
1143 int buf_size;
1144 char *target_path;
1145 int result_length;
1146 WIN32_FILE_ATTRIBUTE_DATA info;
1147
1148 if(!check_GetFinalPathNameByHandle()) {
1149 /* if the OS doesn't have GetFinalPathNameByHandle, it doesn't
1150 have symlinks, so just fall back to the traditional behavior
1151 found in lstat. */
1152 return win32_lstat(path, result);
1153 }
1154
1155 hFile = CreateFileA(
1156 path,
1157 0, /* desired access */
1158 0, /* share mode */
1159 NULL, /* security attributes */
1160 OPEN_EXISTING,
1161 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1162 FILE_FLAG_BACKUP_SEMANTICS,
1163 NULL);
1164
1165 if(hFile == INVALID_HANDLE_VALUE) {
1166 /* Either the target doesn't exist, or we don't have access to
1167 get a handle to it. If the former, we need to return an error.
1168 If the latter, we can use attributes_from_dir. */
1169 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1170 /* Protocol violation: we explicitly clear errno, instead of
1171 setting it to a POSIX error. Callers should use GetLastError. */
1172 errno = 0;
1173 return -1;
1174 } else {
1175 /* Could not get attributes on open file. Fall back to
1176 reading the directory. */
1177 if (!attributes_from_dir(path, &info)) {
1178 /* Very strange. This should not fail now */
1179 errno = 0;
1180 return -1;
1181 }
1182 }
1183 code = attribute_data_to_stat(&info, result);
1184 }
1185
1186 buf_size = Py_GetFinalPathNameByHandleA(hFile, 0, 0, VOLUME_NAME_DOS);
1187 if(!buf_size) return -1;
1188 target_path = (char *)malloc((buf_size+1)*sizeof(char));
1189 result_length = Py_GetFinalPathNameByHandleA(hFile, target_path,
1190 buf_size, VOLUME_NAME_DOS);
1191
1192 if(!result_length)
1193 return -1;
1194
1195 if(!CloseHandle(hFile))
1196 return -1;
1197
1198 target_path[result_length] = 0;
1199 code = win32_lstat(target_path, result);
1200 free(target_path);
1201
1202 return code;
1203}
1204
1205static int
1206win32_stat_w(const wchar_t* path, struct win32_stat *result)
1207{
1208 /* Traverse the symlink to the target using GetFinalPathNameByHandle() */
1209 int code;
1210 HANDLE hFile;
1211 int buf_size;
1212 wchar_t *target_path;
1213 int result_length;
1214 WIN32_FILE_ATTRIBUTE_DATA info;
1215
1216 if(!check_GetFinalPathNameByHandle()) {
1217 /* If the OS doesn't have GetFinalPathNameByHandle, it doesn't have
1218 symlinks, so just fall back to the traditional behavior found
1219 in lstat. */
1220 return win32_lstat_w(path, result);
1221 }
1222
1223 hFile = CreateFileW(
1224 path,
1225 0, /* desired access */
1226 0, /* share mode */
1227 NULL, /* security attributes */
1228 OPEN_EXISTING,
1229 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1230 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1231 NULL);
Antoine Pitroub73caab2010-08-09 23:39:31 +00001232
Brian Curtind40e6f72010-07-08 21:39:08 +00001233 if(hFile == INVALID_HANDLE_VALUE) {
1234 /* Either the target doesn't exist, or we don't have access to
1235 get a handle to it. If the former, we need to return an error.
1236 If the latter, we can use attributes_from_dir. */
1237 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1238 /* Protocol violation: we explicitly clear errno, instead of
1239 setting it to a POSIX error. Callers should use GetLastError. */
1240 errno = 0;
1241 return -1;
1242 } else {
Victor Stinner8c62be82010-05-06 00:08:46 +00001243 /* Could not get attributes on open file. Fall back to
1244 reading the directory. */
1245 if (!attributes_from_dir_w(path, &info)) {
1246 /* Very strange. This should not fail now */
1247 errno = 0;
1248 return -1;
1249 }
1250 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001251 code = attribute_data_to_stat(&info, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001252 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001253 else {
1254 /* We have a good handle to the target, use it to determine the target
1255 path name (then we'll call lstat on it). */
1256 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_DOS);
1257 if(!buf_size)
1258 return -1;
1259
1260 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
1261 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
1262 buf_size, VOLUME_NAME_DOS);
1263
1264 if(!result_length)
1265 return -1;
1266
1267 if(!CloseHandle(hFile))
1268 return -1;
1269
1270 target_path[result_length] = 0;
1271 code = win32_lstat_w(target_path, result);
1272 free(target_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001273 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001274
Victor Stinner8c62be82010-05-06 00:08:46 +00001275 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001276}
1277
1278static int
1279win32_fstat(int file_number, struct win32_stat *result)
1280{
Victor Stinner8c62be82010-05-06 00:08:46 +00001281 BY_HANDLE_FILE_INFORMATION info;
1282 HANDLE h;
1283 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001284
Victor Stinner8c62be82010-05-06 00:08:46 +00001285 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001286
Victor Stinner8c62be82010-05-06 00:08:46 +00001287 /* Protocol violation: we explicitly clear errno, instead of
1288 setting it to a POSIX error. Callers should use GetLastError. */
1289 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001290
Victor Stinner8c62be82010-05-06 00:08:46 +00001291 if (h == INVALID_HANDLE_VALUE) {
1292 /* This is really a C library error (invalid file handle).
1293 We set the Win32 error to the closes one matching. */
1294 SetLastError(ERROR_INVALID_HANDLE);
1295 return -1;
1296 }
1297 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001298
Victor Stinner8c62be82010-05-06 00:08:46 +00001299 type = GetFileType(h);
1300 if (type == FILE_TYPE_UNKNOWN) {
1301 DWORD error = GetLastError();
1302 if (error != 0) {
1303 return -1;
1304 }
1305 /* else: valid but unknown file */
1306 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001307
Victor Stinner8c62be82010-05-06 00:08:46 +00001308 if (type != FILE_TYPE_DISK) {
1309 if (type == FILE_TYPE_CHAR)
1310 result->st_mode = _S_IFCHR;
1311 else if (type == FILE_TYPE_PIPE)
1312 result->st_mode = _S_IFIFO;
1313 return 0;
1314 }
1315
1316 if (!GetFileInformationByHandle(h, &info)) {
1317 return -1;
1318 }
1319
1320 /* similar to stat() */
1321 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1322 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
Brian Curtin74e45612010-07-09 15:58:59 +00001323 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime,
1324 &result->st_ctime_nsec);
1325 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime,
1326 &result->st_mtime_nsec);
1327 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime,
1328 &result->st_atime_nsec);
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 /* specific to fstat() */
1330 result->st_nlink = info.nNumberOfLinks;
1331 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1332 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001333}
1334
1335#endif /* MS_WINDOWS */
1336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001338"stat_result: Result from stat or lstat.\n\n\
1339This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001340 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001341or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1342\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001343Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1344or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001345\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001346See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001347
1348static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001349 {"st_mode", "protection bits"},
1350 {"st_ino", "inode"},
1351 {"st_dev", "device"},
1352 {"st_nlink", "number of hard links"},
1353 {"st_uid", "user ID of owner"},
1354 {"st_gid", "group ID of owner"},
1355 {"st_size", "total size, in bytes"},
1356 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1357 {NULL, "integer time of last access"},
1358 {NULL, "integer time of last modification"},
1359 {NULL, "integer time of last change"},
1360 {"st_atime", "time of last access"},
1361 {"st_mtime", "time of last modification"},
1362 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001363#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001364 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001365#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001366#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001367 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001368#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001369#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001371#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001372#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001373 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001374#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001376 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001377#endif
1378#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001379 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001380#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001381 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001382};
1383
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001384#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001385#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001386#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001387#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001388#endif
1389
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001390#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001391#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1392#else
1393#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1394#endif
1395
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001396#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001397#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1398#else
1399#define ST_RDEV_IDX ST_BLOCKS_IDX
1400#endif
1401
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001402#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1403#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1404#else
1405#define ST_FLAGS_IDX ST_RDEV_IDX
1406#endif
1407
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001408#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001409#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001410#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001411#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001412#endif
1413
1414#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1415#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1416#else
1417#define ST_BIRTHTIME_IDX ST_GEN_IDX
1418#endif
1419
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001420static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001421 "stat_result", /* name */
1422 stat_result__doc__, /* doc */
1423 stat_result_fields,
1424 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001425};
1426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001427PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001428"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1429This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001430 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001431or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001432\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001433See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001434
1435static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 {"f_bsize", },
1437 {"f_frsize", },
1438 {"f_blocks", },
1439 {"f_bfree", },
1440 {"f_bavail", },
1441 {"f_files", },
1442 {"f_ffree", },
1443 {"f_favail", },
1444 {"f_flag", },
1445 {"f_namemax",},
1446 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001447};
1448
1449static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001450 "statvfs_result", /* name */
1451 statvfs_result__doc__, /* doc */
1452 statvfs_result_fields,
1453 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001454};
1455
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001456static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001457static PyTypeObject StatResultType;
1458static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001459static newfunc structseq_new;
1460
1461static PyObject *
1462statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1463{
Victor Stinner8c62be82010-05-06 00:08:46 +00001464 PyStructSequence *result;
1465 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001466
Victor Stinner8c62be82010-05-06 00:08:46 +00001467 result = (PyStructSequence*)structseq_new(type, args, kwds);
1468 if (!result)
1469 return NULL;
1470 /* If we have been initialized from a tuple,
1471 st_?time might be set to None. Initialize it
1472 from the int slots. */
1473 for (i = 7; i <= 9; i++) {
1474 if (result->ob_item[i+3] == Py_None) {
1475 Py_DECREF(Py_None);
1476 Py_INCREF(result->ob_item[i]);
1477 result->ob_item[i+3] = result->ob_item[i];
1478 }
1479 }
1480 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001481}
1482
1483
1484
1485/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001486static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001487
1488PyDoc_STRVAR(stat_float_times__doc__,
1489"stat_float_times([newval]) -> oldval\n\n\
1490Determine whether os.[lf]stat represents time stamps as float objects.\n\
1491If newval is True, future calls to stat() return floats, if it is False,\n\
1492future calls return ints. \n\
1493If newval is omitted, return the current setting.\n");
1494
1495static PyObject*
1496stat_float_times(PyObject* self, PyObject *args)
1497{
Victor Stinner8c62be82010-05-06 00:08:46 +00001498 int newval = -1;
1499 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1500 return NULL;
1501 if (newval == -1)
1502 /* Return old value */
1503 return PyBool_FromLong(_stat_float_times);
1504 _stat_float_times = newval;
1505 Py_INCREF(Py_None);
1506 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001507}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001508
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001509static void
1510fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1511{
Victor Stinner8c62be82010-05-06 00:08:46 +00001512 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001513#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001514 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001515#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001516 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001517#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001518 if (!ival)
1519 return;
1520 if (_stat_float_times) {
1521 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1522 } else {
1523 fval = ival;
1524 Py_INCREF(fval);
1525 }
1526 PyStructSequence_SET_ITEM(v, index, ival);
1527 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001528}
1529
Tim Peters5aa91602002-01-30 05:46:57 +00001530/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001531 (used by posix_stat() and posix_fstat()) */
1532static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001533_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001534{
Victor Stinner8c62be82010-05-06 00:08:46 +00001535 unsigned long ansec, mnsec, cnsec;
1536 PyObject *v = PyStructSequence_New(&StatResultType);
1537 if (v == NULL)
1538 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001539
Victor Stinner8c62be82010-05-06 00:08:46 +00001540 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001541#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001542 PyStructSequence_SET_ITEM(v, 1,
1543 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001544#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001545 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001546#endif
1547#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001548 PyStructSequence_SET_ITEM(v, 2,
1549 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001550#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001551 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001552#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001553 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1554 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1555 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001556#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001557 PyStructSequence_SET_ITEM(v, 6,
1558 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001559#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001560 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001561#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001562
Martin v. Löwis14694662006-02-03 12:54:16 +00001563#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001564 ansec = st->st_atim.tv_nsec;
1565 mnsec = st->st_mtim.tv_nsec;
1566 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001567#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001568 ansec = st->st_atimespec.tv_nsec;
1569 mnsec = st->st_mtimespec.tv_nsec;
1570 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001571#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001572 ansec = st->st_atime_nsec;
1573 mnsec = st->st_mtime_nsec;
1574 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001575#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001576 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001577#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 fill_time(v, 7, st->st_atime, ansec);
1579 fill_time(v, 8, st->st_mtime, mnsec);
1580 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001581
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001582#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001583 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1584 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001585#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001586#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001587 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1588 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001589#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001590#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1592 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001593#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001594#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1596 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001597#endif
1598#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 {
1600 PyObject *val;
1601 unsigned long bsec,bnsec;
1602 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001603#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001604 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001605#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001606 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001607#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 if (_stat_float_times) {
1609 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1610 } else {
1611 val = PyLong_FromLong((long)bsec);
1612 }
1613 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1614 val);
1615 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001616#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001617#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1619 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001620#endif
Fred Drake699f3522000-06-29 21:12:41 +00001621
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 if (PyErr_Occurred()) {
1623 Py_DECREF(v);
1624 return NULL;
1625 }
Fred Drake699f3522000-06-29 21:12:41 +00001626
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001628}
1629
Martin v. Löwisd8948722004-06-02 09:57:56 +00001630#ifdef MS_WINDOWS
1631
1632/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1633 where / can be used in place of \ and the trailing slash is optional.
1634 Both SERVER and SHARE must have at least one character.
1635*/
1636
1637#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1638#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001639#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001640#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001641#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001642
Tim Peters4ad82172004-08-30 17:02:04 +00001643static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001644IsUNCRootA(char *path, int pathlen)
1645{
Victor Stinner8c62be82010-05-06 00:08:46 +00001646 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001647
Victor Stinner8c62be82010-05-06 00:08:46 +00001648 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001649
Victor Stinner8c62be82010-05-06 00:08:46 +00001650 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1651 /* minimum UNCRoot is \\x\y */
1652 return FALSE;
1653 for (i = 2; i < pathlen ; i++)
1654 if (ISSLASH(path[i])) break;
1655 if (i == 2 || i == pathlen)
1656 /* do not allow \\\SHARE or \\SERVER */
1657 return FALSE;
1658 share = i+1;
1659 for (i = share; i < pathlen; i++)
1660 if (ISSLASH(path[i])) break;
1661 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001662
Victor Stinner8c62be82010-05-06 00:08:46 +00001663 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001664}
1665
Tim Peters4ad82172004-08-30 17:02:04 +00001666static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001667IsUNCRootW(Py_UNICODE *path, int pathlen)
1668{
Victor Stinner8c62be82010-05-06 00:08:46 +00001669 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001670
Victor Stinner8c62be82010-05-06 00:08:46 +00001671 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001672
Victor Stinner8c62be82010-05-06 00:08:46 +00001673 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1674 /* minimum UNCRoot is \\x\y */
1675 return FALSE;
1676 for (i = 2; i < pathlen ; i++)
1677 if (ISSLASH(path[i])) break;
1678 if (i == 2 || i == pathlen)
1679 /* do not allow \\\SHARE or \\SERVER */
1680 return FALSE;
1681 share = i+1;
1682 for (i = share; i < pathlen; i++)
1683 if (ISSLASH(path[i])) break;
1684 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001685
Victor Stinner8c62be82010-05-06 00:08:46 +00001686 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001687}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001688#endif /* MS_WINDOWS */
1689
Barry Warsaw53699e91996-12-10 23:23:01 +00001690static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001691posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001692 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001693#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001695#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001696 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001697#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001698 char *wformat,
1699 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001700{
Victor Stinner8c62be82010-05-06 00:08:46 +00001701 STRUCT_STAT st;
1702 PyObject *opath;
1703 char *path;
1704 int res;
1705 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001706
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001707#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001708 PyUnicodeObject *po;
1709 if (PyArg_ParseTuple(args, wformat, &po)) {
1710 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001711
Victor Stinner8c62be82010-05-06 00:08:46 +00001712 Py_BEGIN_ALLOW_THREADS
1713 /* PyUnicode_AS_UNICODE result OK without
1714 thread lock as it is a simple dereference. */
1715 res = wstatfunc(wpath, &st);
1716 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001717
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 if (res != 0)
1719 return win32_error_unicode("stat", wpath);
1720 return _pystat_fromstructstat(&st);
1721 }
1722 /* Drop the argument parsing error as narrow strings
1723 are also valid. */
1724 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001725#endif
1726
Victor Stinner8c62be82010-05-06 00:08:46 +00001727 if (!PyArg_ParseTuple(args, format,
1728 PyUnicode_FSConverter, &opath))
1729 return NULL;
1730 path = PyBytes_AsString(opath);
1731 Py_BEGIN_ALLOW_THREADS
1732 res = (*statfunc)(path, &st);
1733 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001734
Victor Stinner8c62be82010-05-06 00:08:46 +00001735 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001736#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001737 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001738#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001739 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001740#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001741 }
1742 else
1743 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001744
Victor Stinner8c62be82010-05-06 00:08:46 +00001745 Py_DECREF(opath);
1746 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001747}
1748
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001749/* POSIX methods */
1750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001751PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001752"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001753Use the real uid/gid to test for access to a path. Note that most\n\
1754operations will use the effective uid/gid, therefore this routine can\n\
1755be used in a suid/sgid environment to test if the invoking user has the\n\
1756specified access to the path. The mode argument can be F_OK to test\n\
1757existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001758
1759static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001760posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001761{
Victor Stinner8c62be82010-05-06 00:08:46 +00001762 PyObject *opath;
1763 char *path;
1764 int mode;
1765
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001766#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001767 DWORD attr;
1768 PyUnicodeObject *po;
1769 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1770 Py_BEGIN_ALLOW_THREADS
1771 /* PyUnicode_AS_UNICODE OK without thread lock as
1772 it is a simple dereference. */
1773 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1774 Py_END_ALLOW_THREADS
1775 goto finish;
1776 }
1777 /* Drop the argument parsing error as narrow strings
1778 are also valid. */
1779 PyErr_Clear();
1780 if (!PyArg_ParseTuple(args, "O&i:access",
1781 PyUnicode_FSConverter, &opath, &mode))
1782 return NULL;
1783 path = PyBytes_AsString(opath);
1784 Py_BEGIN_ALLOW_THREADS
1785 attr = GetFileAttributesA(path);
1786 Py_END_ALLOW_THREADS
1787 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001788finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001789 if (attr == 0xFFFFFFFF)
1790 /* File does not exist, or cannot read attributes */
1791 return PyBool_FromLong(0);
1792 /* Access is possible if either write access wasn't requested, or
1793 the file isn't read-only, or if it's a directory, as there are
1794 no read-only directories on Windows. */
1795 return PyBool_FromLong(!(mode & 2)
1796 || !(attr & FILE_ATTRIBUTE_READONLY)
1797 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001798#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001799 int res;
1800 if (!PyArg_ParseTuple(args, "O&i:access",
1801 PyUnicode_FSConverter, &opath, &mode))
1802 return NULL;
1803 path = PyBytes_AsString(opath);
1804 Py_BEGIN_ALLOW_THREADS
1805 res = access(path, mode);
1806 Py_END_ALLOW_THREADS
1807 Py_DECREF(opath);
1808 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001809#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001810}
1811
Guido van Rossumd371ff11999-01-25 16:12:23 +00001812#ifndef F_OK
1813#define F_OK 0
1814#endif
1815#ifndef R_OK
1816#define R_OK 4
1817#endif
1818#ifndef W_OK
1819#define W_OK 2
1820#endif
1821#ifndef X_OK
1822#define X_OK 1
1823#endif
1824
1825#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001826PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001827"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001828Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001829
1830static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001831posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001832{
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 int id;
1834 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001835
Victor Stinner8c62be82010-05-06 00:08:46 +00001836 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1837 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001838
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001839#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001840 /* file descriptor 0 only, the default input device (stdin) */
1841 if (id == 0) {
1842 ret = ttyname();
1843 }
1844 else {
1845 ret = NULL;
1846 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001847#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001848 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001849#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001850 if (ret == NULL)
1851 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001852 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001853}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001854#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001855
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001856#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001857PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001858"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001859Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001860
1861static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001862posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001863{
Victor Stinner8c62be82010-05-06 00:08:46 +00001864 char *ret;
1865 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001866
Greg Wardb48bc172000-03-01 21:51:56 +00001867#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001869#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001870 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001871#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001872 if (ret == NULL)
1873 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00001874 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001875}
1876#endif
1877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001879"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001881
Barry Warsaw53699e91996-12-10 23:23:01 +00001882static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001883posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001884{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001885#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001886 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001887#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001888 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001889#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001890 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001891#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001892 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001893#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001894}
1895
Fred Drake4d1e64b2002-04-15 19:40:07 +00001896#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001898"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001899Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001900opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001901
1902static PyObject *
1903posix_fchdir(PyObject *self, PyObject *fdobj)
1904{
Victor Stinner8c62be82010-05-06 00:08:46 +00001905 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001906}
1907#endif /* HAVE_FCHDIR */
1908
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001909
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001910PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001911"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001913
Barry Warsaw53699e91996-12-10 23:23:01 +00001914static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001915posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001916{
Victor Stinner8c62be82010-05-06 00:08:46 +00001917 PyObject *opath = NULL;
1918 char *path = NULL;
1919 int i;
1920 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001921#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001922 DWORD attr;
1923 PyUnicodeObject *po;
1924 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1925 Py_BEGIN_ALLOW_THREADS
1926 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1927 if (attr != 0xFFFFFFFF) {
1928 if (i & _S_IWRITE)
1929 attr &= ~FILE_ATTRIBUTE_READONLY;
1930 else
1931 attr |= FILE_ATTRIBUTE_READONLY;
1932 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1933 }
1934 else
1935 res = 0;
1936 Py_END_ALLOW_THREADS
1937 if (!res)
1938 return win32_error_unicode("chmod",
1939 PyUnicode_AS_UNICODE(po));
1940 Py_INCREF(Py_None);
1941 return Py_None;
1942 }
1943 /* Drop the argument parsing error as narrow strings
1944 are also valid. */
1945 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001946
Victor Stinner8c62be82010-05-06 00:08:46 +00001947 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1948 &opath, &i))
1949 return NULL;
1950 path = PyBytes_AsString(opath);
1951 Py_BEGIN_ALLOW_THREADS
1952 attr = GetFileAttributesA(path);
1953 if (attr != 0xFFFFFFFF) {
1954 if (i & _S_IWRITE)
1955 attr &= ~FILE_ATTRIBUTE_READONLY;
1956 else
1957 attr |= FILE_ATTRIBUTE_READONLY;
1958 res = SetFileAttributesA(path, attr);
1959 }
1960 else
1961 res = 0;
1962 Py_END_ALLOW_THREADS
1963 if (!res) {
1964 win32_error("chmod", path);
1965 Py_DECREF(opath);
1966 return NULL;
1967 }
1968 Py_DECREF(opath);
1969 Py_INCREF(Py_None);
1970 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001971#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001972 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1973 &opath, &i))
1974 return NULL;
1975 path = PyBytes_AsString(opath);
1976 Py_BEGIN_ALLOW_THREADS
1977 res = chmod(path, i);
1978 Py_END_ALLOW_THREADS
1979 if (res < 0)
1980 return posix_error_with_allocated_filename(opath);
1981 Py_DECREF(opath);
1982 Py_INCREF(Py_None);
1983 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001984#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001985}
1986
Christian Heimes4e30a842007-11-30 22:12:06 +00001987#ifdef HAVE_FCHMOD
1988PyDoc_STRVAR(posix_fchmod__doc__,
1989"fchmod(fd, mode)\n\n\
1990Change the access permissions of the file given by file\n\
1991descriptor fd.");
1992
1993static PyObject *
1994posix_fchmod(PyObject *self, PyObject *args)
1995{
Victor Stinner8c62be82010-05-06 00:08:46 +00001996 int fd, mode, res;
1997 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1998 return NULL;
1999 Py_BEGIN_ALLOW_THREADS
2000 res = fchmod(fd, mode);
2001 Py_END_ALLOW_THREADS
2002 if (res < 0)
2003 return posix_error();
2004 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002005}
2006#endif /* HAVE_FCHMOD */
2007
2008#ifdef HAVE_LCHMOD
2009PyDoc_STRVAR(posix_lchmod__doc__,
2010"lchmod(path, mode)\n\n\
2011Change the access permissions of a file. If path is a symlink, this\n\
2012affects the link itself rather than the target.");
2013
2014static PyObject *
2015posix_lchmod(PyObject *self, PyObject *args)
2016{
Victor Stinner8c62be82010-05-06 00:08:46 +00002017 PyObject *opath;
2018 char *path;
2019 int i;
2020 int res;
2021 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2022 &opath, &i))
2023 return NULL;
2024 path = PyBytes_AsString(opath);
2025 Py_BEGIN_ALLOW_THREADS
2026 res = lchmod(path, i);
2027 Py_END_ALLOW_THREADS
2028 if (res < 0)
2029 return posix_error_with_allocated_filename(opath);
2030 Py_DECREF(opath);
2031 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002032}
2033#endif /* HAVE_LCHMOD */
2034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002035
Thomas Wouterscf297e42007-02-23 15:07:44 +00002036#ifdef HAVE_CHFLAGS
2037PyDoc_STRVAR(posix_chflags__doc__,
2038"chflags(path, flags)\n\n\
2039Set file flags.");
2040
2041static PyObject *
2042posix_chflags(PyObject *self, PyObject *args)
2043{
Victor Stinner8c62be82010-05-06 00:08:46 +00002044 PyObject *opath;
2045 char *path;
2046 unsigned long flags;
2047 int res;
2048 if (!PyArg_ParseTuple(args, "O&k:chflags",
2049 PyUnicode_FSConverter, &opath, &flags))
2050 return NULL;
2051 path = PyBytes_AsString(opath);
2052 Py_BEGIN_ALLOW_THREADS
2053 res = chflags(path, flags);
2054 Py_END_ALLOW_THREADS
2055 if (res < 0)
2056 return posix_error_with_allocated_filename(opath);
2057 Py_DECREF(opath);
2058 Py_INCREF(Py_None);
2059 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002060}
2061#endif /* HAVE_CHFLAGS */
2062
2063#ifdef HAVE_LCHFLAGS
2064PyDoc_STRVAR(posix_lchflags__doc__,
2065"lchflags(path, flags)\n\n\
2066Set file flags.\n\
2067This function will not follow symbolic links.");
2068
2069static PyObject *
2070posix_lchflags(PyObject *self, PyObject *args)
2071{
Victor Stinner8c62be82010-05-06 00:08:46 +00002072 PyObject *opath;
2073 char *path;
2074 unsigned long flags;
2075 int res;
2076 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2077 PyUnicode_FSConverter, &opath, &flags))
2078 return NULL;
2079 path = PyBytes_AsString(opath);
2080 Py_BEGIN_ALLOW_THREADS
2081 res = lchflags(path, flags);
2082 Py_END_ALLOW_THREADS
2083 if (res < 0)
2084 return posix_error_with_allocated_filename(opath);
2085 Py_DECREF(opath);
2086 Py_INCREF(Py_None);
2087 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002088}
2089#endif /* HAVE_LCHFLAGS */
2090
Martin v. Löwis244edc82001-10-04 22:44:26 +00002091#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002092PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002093"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002094Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002095
2096static PyObject *
2097posix_chroot(PyObject *self, PyObject *args)
2098{
Victor Stinner8c62be82010-05-06 00:08:46 +00002099 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002100}
2101#endif
2102
Guido van Rossum21142a01999-01-08 21:05:37 +00002103#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002104PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002105"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002106force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002107
2108static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002109posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002110{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002111 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002112}
2113#endif /* HAVE_FSYNC */
2114
2115#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002116
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002117#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002118extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2119#endif
2120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002121PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002122"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002123force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002124 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002125
2126static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002127posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002128{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002129 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002130}
2131#endif /* HAVE_FDATASYNC */
2132
2133
Fredrik Lundh10723342000-07-10 16:38:09 +00002134#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002135PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002136"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002137Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002138
Barry Warsaw53699e91996-12-10 23:23:01 +00002139static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002140posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002141{
Victor Stinner8c62be82010-05-06 00:08:46 +00002142 PyObject *opath;
2143 char *path;
2144 long uid, gid;
2145 int res;
2146 if (!PyArg_ParseTuple(args, "O&ll:chown",
2147 PyUnicode_FSConverter, &opath,
2148 &uid, &gid))
2149 return NULL;
2150 path = PyBytes_AsString(opath);
2151 Py_BEGIN_ALLOW_THREADS
2152 res = chown(path, (uid_t) uid, (gid_t) gid);
2153 Py_END_ALLOW_THREADS
2154 if (res < 0)
2155 return posix_error_with_allocated_filename(opath);
2156 Py_DECREF(opath);
2157 Py_INCREF(Py_None);
2158 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002159}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002160#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002161
Christian Heimes4e30a842007-11-30 22:12:06 +00002162#ifdef HAVE_FCHOWN
2163PyDoc_STRVAR(posix_fchown__doc__,
2164"fchown(fd, uid, gid)\n\n\
2165Change the owner and group id of the file given by file descriptor\n\
2166fd to the numeric uid and gid.");
2167
2168static PyObject *
2169posix_fchown(PyObject *self, PyObject *args)
2170{
Victor Stinner8c62be82010-05-06 00:08:46 +00002171 int fd;
2172 long uid, gid;
2173 int res;
2174 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2175 return NULL;
2176 Py_BEGIN_ALLOW_THREADS
2177 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2178 Py_END_ALLOW_THREADS
2179 if (res < 0)
2180 return posix_error();
2181 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002182}
2183#endif /* HAVE_FCHOWN */
2184
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002185#ifdef HAVE_LCHOWN
2186PyDoc_STRVAR(posix_lchown__doc__,
2187"lchown(path, uid, gid)\n\n\
2188Change the owner and group id of path to the numeric uid and gid.\n\
2189This function will not follow symbolic links.");
2190
2191static PyObject *
2192posix_lchown(PyObject *self, PyObject *args)
2193{
Victor Stinner8c62be82010-05-06 00:08:46 +00002194 PyObject *opath;
2195 char *path;
2196 long uid, gid;
2197 int res;
2198 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2199 PyUnicode_FSConverter, &opath,
2200 &uid, &gid))
2201 return NULL;
2202 path = PyBytes_AsString(opath);
2203 Py_BEGIN_ALLOW_THREADS
2204 res = lchown(path, (uid_t) uid, (gid_t) gid);
2205 Py_END_ALLOW_THREADS
2206 if (res < 0)
2207 return posix_error_with_allocated_filename(opath);
2208 Py_DECREF(opath);
2209 Py_INCREF(Py_None);
2210 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002211}
2212#endif /* HAVE_LCHOWN */
2213
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002214
Guido van Rossum36bc6801995-06-14 22:54:23 +00002215#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002216static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002217posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002218{
Victor Stinner8c62be82010-05-06 00:08:46 +00002219 char buf[1026];
2220 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002221
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002222#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002223 if (!use_bytes) {
2224 wchar_t wbuf[1026];
2225 wchar_t *wbuf2 = wbuf;
2226 PyObject *resobj;
2227 DWORD len;
2228 Py_BEGIN_ALLOW_THREADS
2229 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2230 /* If the buffer is large enough, len does not include the
2231 terminating \0. If the buffer is too small, len includes
2232 the space needed for the terminator. */
2233 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2234 wbuf2 = malloc(len * sizeof(wchar_t));
2235 if (wbuf2)
2236 len = GetCurrentDirectoryW(len, wbuf2);
2237 }
2238 Py_END_ALLOW_THREADS
2239 if (!wbuf2) {
2240 PyErr_NoMemory();
2241 return NULL;
2242 }
2243 if (!len) {
2244 if (wbuf2 != wbuf) free(wbuf2);
2245 return win32_error("getcwdu", NULL);
2246 }
2247 resobj = PyUnicode_FromWideChar(wbuf2, len);
2248 if (wbuf2 != wbuf) free(wbuf2);
2249 return resobj;
2250 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002251#endif
2252
Victor Stinner8c62be82010-05-06 00:08:46 +00002253 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002254#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002256#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002257 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002258#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002259 Py_END_ALLOW_THREADS
2260 if (res == NULL)
2261 return posix_error();
2262 if (use_bytes)
2263 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002264 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002265}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002266
2267PyDoc_STRVAR(posix_getcwd__doc__,
2268"getcwd() -> path\n\n\
2269Return a unicode string representing the current working directory.");
2270
2271static PyObject *
2272posix_getcwd_unicode(PyObject *self)
2273{
2274 return posix_getcwd(0);
2275}
2276
2277PyDoc_STRVAR(posix_getcwdb__doc__,
2278"getcwdb() -> path\n\n\
2279Return a bytes string representing the current working directory.");
2280
2281static PyObject *
2282posix_getcwd_bytes(PyObject *self)
2283{
2284 return posix_getcwd(1);
2285}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002286#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002287
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002288
Guido van Rossumb6775db1994-08-01 11:34:53 +00002289#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002290PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002291"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002292Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002293
Barry Warsaw53699e91996-12-10 23:23:01 +00002294static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002295posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002296{
Victor Stinner8c62be82010-05-06 00:08:46 +00002297 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002298}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002299#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002300
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002302PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002303"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002304Return a list containing the names of the entries in the directory.\n\
2305\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002306 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002307\n\
2308The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002309entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002310
Barry Warsaw53699e91996-12-10 23:23:01 +00002311static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002312posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002313{
Victor Stinner8c62be82010-05-06 00:08:46 +00002314 /* XXX Should redo this putting the (now four) versions of opendir
2315 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002316#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002317
Victor Stinner8c62be82010-05-06 00:08:46 +00002318 PyObject *d, *v;
2319 HANDLE hFindFile;
2320 BOOL result;
2321 WIN32_FIND_DATA FileData;
2322 PyObject *opath;
2323 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2324 char *bufptr = namebuf;
2325 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002326
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002327 PyObject *po = NULL;
2328 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002329 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002330 Py_UNICODE *wnamebuf, *po_wchars;
2331
2332 if (po == NULL) { // Default arg: "."
2333 po_wchars = L".";
2334 len = 1;
2335 } else {
2336 po_wchars = PyUnicode_AS_UNICODE(po);
2337 len = PyUnicode_GET_SIZE(po);
2338 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002339 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002340 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2341 if (!wnamebuf) {
2342 PyErr_NoMemory();
2343 return NULL;
2344 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002345 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002346 if (len > 0) {
2347 Py_UNICODE wch = wnamebuf[len-1];
2348 if (wch != L'/' && wch != L'\\' && wch != L':')
2349 wnamebuf[len++] = L'\\';
2350 wcscpy(wnamebuf + len, L"*.*");
2351 }
2352 if ((d = PyList_New(0)) == NULL) {
2353 free(wnamebuf);
2354 return NULL;
2355 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00002356 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002357 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002358 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002359 if (hFindFile == INVALID_HANDLE_VALUE) {
2360 int error = GetLastError();
2361 if (error == ERROR_FILE_NOT_FOUND) {
2362 free(wnamebuf);
2363 return d;
2364 }
2365 Py_DECREF(d);
2366 win32_error_unicode("FindFirstFileW", wnamebuf);
2367 free(wnamebuf);
2368 return NULL;
2369 }
2370 do {
2371 /* Skip over . and .. */
2372 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2373 wcscmp(wFileData.cFileName, L"..") != 0) {
2374 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2375 if (v == NULL) {
2376 Py_DECREF(d);
2377 d = NULL;
2378 break;
2379 }
2380 if (PyList_Append(d, v) != 0) {
2381 Py_DECREF(v);
2382 Py_DECREF(d);
2383 d = NULL;
2384 break;
2385 }
2386 Py_DECREF(v);
2387 }
2388 Py_BEGIN_ALLOW_THREADS
2389 result = FindNextFileW(hFindFile, &wFileData);
2390 Py_END_ALLOW_THREADS
2391 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2392 it got to the end of the directory. */
2393 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2394 Py_DECREF(d);
2395 win32_error_unicode("FindNextFileW", wnamebuf);
2396 FindClose(hFindFile);
2397 free(wnamebuf);
2398 return NULL;
2399 }
2400 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002401
Victor Stinner8c62be82010-05-06 00:08:46 +00002402 if (FindClose(hFindFile) == FALSE) {
2403 Py_DECREF(d);
2404 win32_error_unicode("FindClose", wnamebuf);
2405 free(wnamebuf);
2406 return NULL;
2407 }
2408 free(wnamebuf);
2409 return d;
2410 }
2411 /* Drop the argument parsing error as narrow strings
2412 are also valid. */
2413 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002414
Victor Stinner8c62be82010-05-06 00:08:46 +00002415 if (!PyArg_ParseTuple(args, "O&:listdir",
2416 PyUnicode_FSConverter, &opath))
2417 return NULL;
2418 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2419 PyErr_SetString(PyExc_ValueError, "path too long");
2420 Py_DECREF(opath);
2421 return NULL;
2422 }
2423 strcpy(namebuf, PyBytes_AsString(opath));
2424 len = PyObject_Size(opath);
2425 if (len > 0) {
2426 char ch = namebuf[len-1];
2427 if (ch != SEP && ch != ALTSEP && ch != ':')
2428 namebuf[len++] = '/';
2429 strcpy(namebuf + len, "*.*");
2430 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002431
Victor Stinner8c62be82010-05-06 00:08:46 +00002432 if ((d = PyList_New(0)) == NULL)
2433 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002434
Antoine Pitroub73caab2010-08-09 23:39:31 +00002435 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002436 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00002437 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00002438 if (hFindFile == INVALID_HANDLE_VALUE) {
2439 int error = GetLastError();
2440 if (error == ERROR_FILE_NOT_FOUND)
2441 return d;
2442 Py_DECREF(d);
2443 return win32_error("FindFirstFile", namebuf);
2444 }
2445 do {
2446 /* Skip over . and .. */
2447 if (strcmp(FileData.cFileName, ".") != 0 &&
2448 strcmp(FileData.cFileName, "..") != 0) {
2449 v = PyBytes_FromString(FileData.cFileName);
2450 if (v == NULL) {
2451 Py_DECREF(d);
2452 d = NULL;
2453 break;
2454 }
2455 if (PyList_Append(d, v) != 0) {
2456 Py_DECREF(v);
2457 Py_DECREF(d);
2458 d = NULL;
2459 break;
2460 }
2461 Py_DECREF(v);
2462 }
2463 Py_BEGIN_ALLOW_THREADS
2464 result = FindNextFile(hFindFile, &FileData);
2465 Py_END_ALLOW_THREADS
2466 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2467 it got to the end of the directory. */
2468 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2469 Py_DECREF(d);
2470 win32_error("FindNextFile", namebuf);
2471 FindClose(hFindFile);
2472 return NULL;
2473 }
2474 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002475
Victor Stinner8c62be82010-05-06 00:08:46 +00002476 if (FindClose(hFindFile) == FALSE) {
2477 Py_DECREF(d);
2478 return win32_error("FindClose", namebuf);
2479 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002480
Victor Stinner8c62be82010-05-06 00:08:46 +00002481 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002482
Tim Peters0bb44a42000-09-15 07:44:49 +00002483#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002484
2485#ifndef MAX_PATH
2486#define MAX_PATH CCHMAXPATH
2487#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002488 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002489 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002490 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002491 PyObject *d, *v;
2492 char namebuf[MAX_PATH+5];
2493 HDIR hdir = 1;
2494 ULONG srchcnt = 1;
2495 FILEFINDBUF3 ep;
2496 APIRET rc;
2497
Victor Stinner8c62be82010-05-06 00:08:46 +00002498 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002499 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002500 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002501 name = PyBytes_AsString(oname);
2502 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002503 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002504 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002505 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002506 return NULL;
2507 }
2508 strcpy(namebuf, name);
2509 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002510 if (*pt == ALTSEP)
2511 *pt = SEP;
2512 if (namebuf[len-1] != SEP)
2513 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002514 strcpy(namebuf + len, "*.*");
2515
Neal Norwitz6c913782007-10-14 03:23:09 +00002516 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002517 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002518 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002519 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002520
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002521 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2522 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002523 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002524 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2525 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2526 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002527
2528 if (rc != NO_ERROR) {
2529 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002530 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002531 }
2532
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002533 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002534 do {
2535 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002536 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002537 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002538
2539 strcpy(namebuf, ep.achName);
2540
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002541 /* Leave Case of Name Alone -- In Native Form */
2542 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002543
Christian Heimes72b710a2008-05-26 13:28:38 +00002544 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002545 if (v == NULL) {
2546 Py_DECREF(d);
2547 d = NULL;
2548 break;
2549 }
2550 if (PyList_Append(d, v) != 0) {
2551 Py_DECREF(v);
2552 Py_DECREF(d);
2553 d = NULL;
2554 break;
2555 }
2556 Py_DECREF(v);
2557 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2558 }
2559
Victor Stinnerdcb24032010-04-22 12:08:36 +00002560 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002561 return d;
2562#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002563 PyObject *oname;
2564 char *name;
2565 PyObject *d, *v;
2566 DIR *dirp;
2567 struct dirent *ep;
2568 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002569
Victor Stinner8c62be82010-05-06 00:08:46 +00002570 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002571 /* v is never read, so it does not need to be initialized yet. */
2572 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002573 arg_is_unicode = 0;
2574 PyErr_Clear();
2575 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002576 oname = NULL;
2577 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002578 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002579 if (oname == NULL) { // Default arg: "."
2580 oname = PyBytes_FromString(".");
2581 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002582 name = PyBytes_AsString(oname);
2583 if ((dirp = opendir(name)) == NULL) {
2584 return posix_error_with_allocated_filename(oname);
2585 }
2586 if ((d = PyList_New(0)) == NULL) {
2587 closedir(dirp);
2588 Py_DECREF(oname);
2589 return NULL;
2590 }
2591 for (;;) {
2592 errno = 0;
2593 Py_BEGIN_ALLOW_THREADS
2594 ep = readdir(dirp);
2595 Py_END_ALLOW_THREADS
2596 if (ep == NULL) {
2597 if (errno == 0) {
2598 break;
2599 } else {
2600 closedir(dirp);
2601 Py_DECREF(d);
2602 return posix_error_with_allocated_filename(oname);
2603 }
2604 }
2605 if (ep->d_name[0] == '.' &&
2606 (NAMLEN(ep) == 1 ||
2607 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2608 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002609 if (arg_is_unicode)
2610 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2611 else
2612 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002613 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002614 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002615 break;
2616 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002617 if (PyList_Append(d, v) != 0) {
2618 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002619 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002620 break;
2621 }
2622 Py_DECREF(v);
2623 }
2624 closedir(dirp);
2625 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002626
Victor Stinner8c62be82010-05-06 00:08:46 +00002627 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002628
Tim Peters0bb44a42000-09-15 07:44:49 +00002629#endif /* which OS */
2630} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002631
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002632#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002633/* A helper function for abspath on win32 */
2634static PyObject *
2635posix__getfullpathname(PyObject *self, PyObject *args)
2636{
Victor Stinner8c62be82010-05-06 00:08:46 +00002637 PyObject *opath;
2638 char *path;
2639 char outbuf[MAX_PATH*2];
2640 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002641#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002642 PyUnicodeObject *po;
2643 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2644 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2645 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2646 Py_UNICODE *wtemp;
2647 DWORD result;
2648 PyObject *v;
2649 result = GetFullPathNameW(wpath,
2650 sizeof(woutbuf)/sizeof(woutbuf[0]),
2651 woutbuf, &wtemp);
2652 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2653 woutbufp = malloc(result * sizeof(Py_UNICODE));
2654 if (!woutbufp)
2655 return PyErr_NoMemory();
2656 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2657 }
2658 if (result)
2659 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2660 else
2661 v = win32_error_unicode("GetFullPathNameW", wpath);
2662 if (woutbufp != woutbuf)
2663 free(woutbufp);
2664 return v;
2665 }
2666 /* Drop the argument parsing error as narrow strings
2667 are also valid. */
2668 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002669
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002670#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002671 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2672 PyUnicode_FSConverter, &opath))
2673 return NULL;
2674 path = PyBytes_AsString(opath);
2675 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2676 outbuf, &temp)) {
2677 win32_error("GetFullPathName", path);
2678 Py_DECREF(opath);
2679 return NULL;
2680 }
2681 Py_DECREF(opath);
2682 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2683 return PyUnicode_Decode(outbuf, strlen(outbuf),
2684 Py_FileSystemDefaultEncoding, NULL);
2685 }
2686 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002687} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002688
2689/* A helper function for samepath on windows */
2690static PyObject *
2691posix__getfinalpathname(PyObject *self, PyObject *args)
2692{
2693 HANDLE hFile;
2694 int buf_size;
2695 wchar_t *target_path;
2696 int result_length;
2697 PyObject *result;
2698 wchar_t *path;
2699
2700 if (!PyArg_ParseTuple(args, "u|:_getfullpathname", &path)) {
2701 return NULL;
2702 }
2703
2704 if(!check_GetFinalPathNameByHandle()) {
2705 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2706 NotImplementedError. */
2707 return PyErr_Format(PyExc_NotImplementedError,
2708 "GetFinalPathNameByHandle not available on this platform");
2709 }
2710
2711 hFile = CreateFileW(
2712 path,
2713 0, /* desired access */
2714 0, /* share mode */
2715 NULL, /* security attributes */
2716 OPEN_EXISTING,
2717 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2718 FILE_FLAG_BACKUP_SEMANTICS,
2719 NULL);
2720
2721 if(hFile == INVALID_HANDLE_VALUE) {
2722 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002723 return PyErr_Format(PyExc_RuntimeError,
2724 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002725 }
2726
2727 /* We have a good handle to the target, use it to determine the
2728 target path name. */
2729 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2730
2731 if(!buf_size)
2732 return win32_error_unicode("GetFinalPathNameByHandle", path);
2733
2734 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2735 if(!target_path)
2736 return PyErr_NoMemory();
2737
2738 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2739 buf_size, VOLUME_NAME_DOS);
2740 if(!result_length)
2741 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2742
2743 if(!CloseHandle(hFile))
2744 return win32_error_unicode("GetFinalPathNameByHandle", path);
2745
2746 target_path[result_length] = 0;
2747 result = PyUnicode_FromUnicode(target_path, result_length);
2748 free(target_path);
2749 return result;
2750
2751} /* end of posix__getfinalpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002752#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002754PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002755"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002756Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002757
Barry Warsaw53699e91996-12-10 23:23:01 +00002758static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002759posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002760{
Victor Stinner8c62be82010-05-06 00:08:46 +00002761 int res;
2762 PyObject *opath;
2763 char *path;
2764 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002765
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002766#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002767 PyUnicodeObject *po;
2768 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2769 Py_BEGIN_ALLOW_THREADS
2770 /* PyUnicode_AS_UNICODE OK without thread lock as
2771 it is a simple dereference. */
2772 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2773 Py_END_ALLOW_THREADS
2774 if (!res)
2775 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2776 Py_INCREF(Py_None);
2777 return Py_None;
2778 }
2779 /* Drop the argument parsing error as narrow strings
2780 are also valid. */
2781 PyErr_Clear();
2782 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2783 PyUnicode_FSConverter, &opath, &mode))
2784 return NULL;
2785 path = PyBytes_AsString(opath);
2786 Py_BEGIN_ALLOW_THREADS
2787 /* PyUnicode_AS_UNICODE OK without thread lock as
2788 it is a simple dereference. */
2789 res = CreateDirectoryA(path, NULL);
2790 Py_END_ALLOW_THREADS
2791 if (!res) {
2792 win32_error("mkdir", path);
2793 Py_DECREF(opath);
2794 return NULL;
2795 }
2796 Py_DECREF(opath);
2797 Py_INCREF(Py_None);
2798 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002799#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002800
Victor Stinner8c62be82010-05-06 00:08:46 +00002801 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2802 PyUnicode_FSConverter, &opath, &mode))
2803 return NULL;
2804 path = PyBytes_AsString(opath);
2805 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002806#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002807 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002808#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002809 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002810#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002811 Py_END_ALLOW_THREADS
2812 if (res < 0)
2813 return posix_error_with_allocated_filename(opath);
2814 Py_DECREF(opath);
2815 Py_INCREF(Py_None);
2816 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002817#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002818}
2819
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002820
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002821/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2822#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002823#include <sys/resource.h>
2824#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002825
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002826
2827#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002828PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002829"nice(inc) -> new_priority\n\n\
2830Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002831
Barry Warsaw53699e91996-12-10 23:23:01 +00002832static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002833posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002834{
Victor Stinner8c62be82010-05-06 00:08:46 +00002835 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002836
Victor Stinner8c62be82010-05-06 00:08:46 +00002837 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2838 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002839
Victor Stinner8c62be82010-05-06 00:08:46 +00002840 /* There are two flavours of 'nice': one that returns the new
2841 priority (as required by almost all standards out there) and the
2842 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2843 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002844
Victor Stinner8c62be82010-05-06 00:08:46 +00002845 If we are of the nice family that returns the new priority, we
2846 need to clear errno before the call, and check if errno is filled
2847 before calling posix_error() on a returnvalue of -1, because the
2848 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002849
Victor Stinner8c62be82010-05-06 00:08:46 +00002850 errno = 0;
2851 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002852#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002853 if (value == 0)
2854 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002855#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002856 if (value == -1 && errno != 0)
2857 /* either nice() or getpriority() returned an error */
2858 return posix_error();
2859 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002860}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002861#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002863PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002864"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002865Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002866
Barry Warsaw53699e91996-12-10 23:23:01 +00002867static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002868posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002869{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002870#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002871 PyObject *o1, *o2;
2872 char *p1, *p2;
2873 BOOL result;
2874 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2875 goto error;
2876 if (!convert_to_unicode(&o1))
2877 goto error;
2878 if (!convert_to_unicode(&o2)) {
2879 Py_DECREF(o1);
2880 goto error;
2881 }
2882 Py_BEGIN_ALLOW_THREADS
2883 result = MoveFileW(PyUnicode_AsUnicode(o1),
2884 PyUnicode_AsUnicode(o2));
2885 Py_END_ALLOW_THREADS
2886 Py_DECREF(o1);
2887 Py_DECREF(o2);
2888 if (!result)
2889 return win32_error("rename", NULL);
2890 Py_INCREF(Py_None);
2891 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002892error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002893 PyErr_Clear();
2894 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2895 return NULL;
2896 Py_BEGIN_ALLOW_THREADS
2897 result = MoveFileA(p1, p2);
2898 Py_END_ALLOW_THREADS
2899 if (!result)
2900 return win32_error("rename", NULL);
2901 Py_INCREF(Py_None);
2902 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002903#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002904 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002905#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002906}
2907
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002908
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002909PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002910"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002911Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002912
Barry Warsaw53699e91996-12-10 23:23:01 +00002913static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002914posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002915{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002916#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002917 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002918#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002919 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002920#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002921}
2922
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002923
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002924PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002925"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002926Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002927
Barry Warsaw53699e91996-12-10 23:23:01 +00002928static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002929posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002930{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002931#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00002932 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002933#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002934 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002935#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002936}
2937
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002938
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002939#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002940PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002941"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002942Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002943
Barry Warsaw53699e91996-12-10 23:23:01 +00002944static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002945posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002946{
Victor Stinner8c62be82010-05-06 00:08:46 +00002947 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002948#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002949 wchar_t *command;
2950 if (!PyArg_ParseTuple(args, "u:system", &command))
2951 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002952
Victor Stinner8c62be82010-05-06 00:08:46 +00002953 Py_BEGIN_ALLOW_THREADS
2954 sts = _wsystem(command);
2955 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002956#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002957 PyObject *command_obj;
2958 char *command;
2959 if (!PyArg_ParseTuple(args, "O&:system",
2960 PyUnicode_FSConverter, &command_obj))
2961 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002962
Victor Stinner8c62be82010-05-06 00:08:46 +00002963 command = PyBytes_AsString(command_obj);
2964 Py_BEGIN_ALLOW_THREADS
2965 sts = system(command);
2966 Py_END_ALLOW_THREADS
2967 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002968#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002969 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002970}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002971#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002972
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002975"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002976Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002977
Barry Warsaw53699e91996-12-10 23:23:01 +00002978static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002979posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002980{
Victor Stinner8c62be82010-05-06 00:08:46 +00002981 int i;
2982 if (!PyArg_ParseTuple(args, "i:umask", &i))
2983 return NULL;
2984 i = (int)umask(i);
2985 if (i < 0)
2986 return posix_error();
2987 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002988}
2989
Brian Curtind40e6f72010-07-08 21:39:08 +00002990#ifdef MS_WINDOWS
2991
2992/* override the default DeleteFileW behavior so that directory
2993symlinks can be removed with this function, the same as with
2994Unix symlinks */
2995BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
2996{
2997 WIN32_FILE_ATTRIBUTE_DATA info;
2998 WIN32_FIND_DATAW find_data;
2999 HANDLE find_data_handle;
3000 int is_directory = 0;
3001 int is_link = 0;
3002
3003 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
3004 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
3005
3006 /* Get WIN32_FIND_DATA structure for the path to determine if
3007 it is a symlink */
3008 if(is_directory &&
3009 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
3010 find_data_handle = FindFirstFileW(lpFileName, &find_data);
3011
3012 if(find_data_handle != INVALID_HANDLE_VALUE) {
3013 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3014 FindClose(find_data_handle);
3015 }
3016 }
3017 }
3018
3019 if (is_directory && is_link)
3020 return RemoveDirectoryW(lpFileName);
3021
3022 return DeleteFileW(lpFileName);
3023}
3024#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003025
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003026PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003027"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003028Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003030PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003031"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003032Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003033
Barry Warsaw53699e91996-12-10 23:23:01 +00003034static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003035posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003036{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003037#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003038 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3039 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003040#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003041 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003042#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003043}
3044
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003045
Guido van Rossumb6775db1994-08-01 11:34:53 +00003046#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003047PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003048"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003049Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003050
Barry Warsaw53699e91996-12-10 23:23:01 +00003051static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003052posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003053{
Victor Stinner8c62be82010-05-06 00:08:46 +00003054 struct utsname u;
3055 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003056
Victor Stinner8c62be82010-05-06 00:08:46 +00003057 Py_BEGIN_ALLOW_THREADS
3058 res = uname(&u);
3059 Py_END_ALLOW_THREADS
3060 if (res < 0)
3061 return posix_error();
3062 return Py_BuildValue("(sssss)",
3063 u.sysname,
3064 u.nodename,
3065 u.release,
3066 u.version,
3067 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003068}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003069#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003070
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003071static int
3072extract_time(PyObject *t, long* sec, long* usec)
3073{
Victor Stinner8c62be82010-05-06 00:08:46 +00003074 long intval;
3075 if (PyFloat_Check(t)) {
3076 double tval = PyFloat_AsDouble(t);
3077 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
3078 if (!intobj)
3079 return -1;
3080 intval = PyLong_AsLong(intobj);
3081 Py_DECREF(intobj);
3082 if (intval == -1 && PyErr_Occurred())
3083 return -1;
3084 *sec = intval;
3085 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3086 if (*usec < 0)
3087 /* If rounding gave us a negative number,
3088 truncate. */
3089 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003090 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003091 }
3092 intval = PyLong_AsLong(t);
3093 if (intval == -1 && PyErr_Occurred())
3094 return -1;
3095 *sec = intval;
3096 *usec = 0;
3097 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003098}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003100PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003101"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003102utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003103Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003104second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003105
Barry Warsaw53699e91996-12-10 23:23:01 +00003106static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003107posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003108{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003109#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003110 PyObject *arg;
3111 PyUnicodeObject *obwpath;
3112 wchar_t *wpath = NULL;
3113 PyObject *oapath;
3114 char *apath;
3115 HANDLE hFile;
3116 long atimesec, mtimesec, ausec, musec;
3117 FILETIME atime, mtime;
3118 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003119
Victor Stinner8c62be82010-05-06 00:08:46 +00003120 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3121 wpath = PyUnicode_AS_UNICODE(obwpath);
3122 Py_BEGIN_ALLOW_THREADS
3123 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3124 NULL, OPEN_EXISTING,
3125 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3126 Py_END_ALLOW_THREADS
3127 if (hFile == INVALID_HANDLE_VALUE)
3128 return win32_error_unicode("utime", wpath);
3129 } else
3130 /* Drop the argument parsing error as narrow strings
3131 are also valid. */
3132 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003133
Victor Stinner8c62be82010-05-06 00:08:46 +00003134 if (!wpath) {
3135 if (!PyArg_ParseTuple(args, "O&O:utime",
3136 PyUnicode_FSConverter, &oapath, &arg))
3137 return NULL;
3138 apath = PyBytes_AsString(oapath);
3139 Py_BEGIN_ALLOW_THREADS
3140 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3141 NULL, OPEN_EXISTING,
3142 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3143 Py_END_ALLOW_THREADS
3144 if (hFile == INVALID_HANDLE_VALUE) {
3145 win32_error("utime", apath);
3146 Py_DECREF(oapath);
3147 return NULL;
3148 }
3149 Py_DECREF(oapath);
3150 }
3151
3152 if (arg == Py_None) {
3153 SYSTEMTIME now;
3154 GetSystemTime(&now);
3155 if (!SystemTimeToFileTime(&now, &mtime) ||
3156 !SystemTimeToFileTime(&now, &atime)) {
3157 win32_error("utime", NULL);
3158 goto done;
3159 }
3160 }
3161 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3162 PyErr_SetString(PyExc_TypeError,
3163 "utime() arg 2 must be a tuple (atime, mtime)");
3164 goto done;
3165 }
3166 else {
3167 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3168 &atimesec, &ausec) == -1)
3169 goto done;
3170 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3171 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3172 &mtimesec, &musec) == -1)
3173 goto done;
3174 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3175 }
3176 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3177 /* Avoid putting the file name into the error here,
3178 as that may confuse the user into believing that
3179 something is wrong with the file, when it also
3180 could be the time stamp that gives a problem. */
3181 win32_error("utime", NULL);
3182 }
3183 Py_INCREF(Py_None);
3184 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003185done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003186 CloseHandle(hFile);
3187 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003188#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003189
Victor Stinner8c62be82010-05-06 00:08:46 +00003190 PyObject *opath;
3191 char *path;
3192 long atime, mtime, ausec, musec;
3193 int res;
3194 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003195
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003196#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003197 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003198#define ATIME buf[0].tv_sec
3199#define MTIME buf[1].tv_sec
3200#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003201/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003203#define ATIME buf.actime
3204#define MTIME buf.modtime
3205#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003206#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003207 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003208#define ATIME buf[0]
3209#define MTIME buf[1]
3210#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003211#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003212
Mark Hammond817c9292003-12-03 01:22:38 +00003213
Victor Stinner8c62be82010-05-06 00:08:46 +00003214 if (!PyArg_ParseTuple(args, "O&O:utime",
3215 PyUnicode_FSConverter, &opath, &arg))
3216 return NULL;
3217 path = PyBytes_AsString(opath);
3218 if (arg == Py_None) {
3219 /* optional time values not given */
3220 Py_BEGIN_ALLOW_THREADS
3221 res = utime(path, NULL);
3222 Py_END_ALLOW_THREADS
3223 }
3224 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3225 PyErr_SetString(PyExc_TypeError,
3226 "utime() arg 2 must be a tuple (atime, mtime)");
3227 Py_DECREF(opath);
3228 return NULL;
3229 }
3230 else {
3231 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3232 &atime, &ausec) == -1) {
3233 Py_DECREF(opath);
3234 return NULL;
3235 }
3236 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3237 &mtime, &musec) == -1) {
3238 Py_DECREF(opath);
3239 return NULL;
3240 }
3241 ATIME = atime;
3242 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003243#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003244 buf[0].tv_usec = ausec;
3245 buf[1].tv_usec = musec;
3246 Py_BEGIN_ALLOW_THREADS
3247 res = utimes(path, buf);
3248 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003249#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003250 Py_BEGIN_ALLOW_THREADS
3251 res = utime(path, UTIME_ARG);
3252 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003253#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003254 }
3255 if (res < 0) {
3256 return posix_error_with_allocated_filename(opath);
3257 }
3258 Py_DECREF(opath);
3259 Py_INCREF(Py_None);
3260 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003261#undef UTIME_ARG
3262#undef ATIME
3263#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003264#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003265}
3266
Guido van Rossum85e3b011991-06-03 12:42:10 +00003267
Guido van Rossum3b066191991-06-04 19:40:25 +00003268/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003269
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003270PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003271"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003272Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003273
Barry Warsaw53699e91996-12-10 23:23:01 +00003274static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003275posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003276{
Victor Stinner8c62be82010-05-06 00:08:46 +00003277 int sts;
3278 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3279 return NULL;
3280 _exit(sts);
3281 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003282}
3283
Martin v. Löwis114619e2002-10-07 06:44:21 +00003284#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3285static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003286free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003287{
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 Py_ssize_t i;
3289 for (i = 0; i < count; i++)
3290 PyMem_Free(array[i]);
3291 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003292}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003293
Antoine Pitrou69f71142009-05-24 21:25:49 +00003294static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003295int fsconvert_strdup(PyObject *o, char**out)
3296{
Victor Stinner8c62be82010-05-06 00:08:46 +00003297 PyObject *bytes;
3298 Py_ssize_t size;
3299 if (!PyUnicode_FSConverter(o, &bytes))
3300 return 0;
3301 size = PyBytes_GET_SIZE(bytes);
3302 *out = PyMem_Malloc(size+1);
3303 if (!*out)
3304 return 0;
3305 memcpy(*out, PyBytes_AsString(bytes), size+1);
3306 Py_DECREF(bytes);
3307 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003308}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003309#endif
3310
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003311
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003312#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003313PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003314"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003315Execute an executable path with arguments, replacing current process.\n\
3316\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003317 path: path of executable file\n\
3318 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003319
Barry Warsaw53699e91996-12-10 23:23:01 +00003320static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003321posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003322{
Victor Stinner8c62be82010-05-06 00:08:46 +00003323 PyObject *opath;
3324 char *path;
3325 PyObject *argv;
3326 char **argvlist;
3327 Py_ssize_t i, argc;
3328 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003329
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 /* execv has two arguments: (path, argv), where
3331 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003332
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 if (!PyArg_ParseTuple(args, "O&O:execv",
3334 PyUnicode_FSConverter,
3335 &opath, &argv))
3336 return NULL;
3337 path = PyBytes_AsString(opath);
3338 if (PyList_Check(argv)) {
3339 argc = PyList_Size(argv);
3340 getitem = PyList_GetItem;
3341 }
3342 else if (PyTuple_Check(argv)) {
3343 argc = PyTuple_Size(argv);
3344 getitem = PyTuple_GetItem;
3345 }
3346 else {
3347 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3348 Py_DECREF(opath);
3349 return NULL;
3350 }
3351 if (argc < 1) {
3352 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3353 Py_DECREF(opath);
3354 return NULL;
3355 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003356
Victor Stinner8c62be82010-05-06 00:08:46 +00003357 argvlist = PyMem_NEW(char *, argc+1);
3358 if (argvlist == NULL) {
3359 Py_DECREF(opath);
3360 return PyErr_NoMemory();
3361 }
3362 for (i = 0; i < argc; i++) {
3363 if (!fsconvert_strdup((*getitem)(argv, i),
3364 &argvlist[i])) {
3365 free_string_array(argvlist, i);
3366 PyErr_SetString(PyExc_TypeError,
3367 "execv() arg 2 must contain only strings");
3368 Py_DECREF(opath);
3369 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003370
Victor Stinner8c62be82010-05-06 00:08:46 +00003371 }
3372 }
3373 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003374
Victor Stinner8c62be82010-05-06 00:08:46 +00003375 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003376
Victor Stinner8c62be82010-05-06 00:08:46 +00003377 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003378
Victor Stinner8c62be82010-05-06 00:08:46 +00003379 free_string_array(argvlist, argc);
3380 Py_DECREF(opath);
3381 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003382}
3383
Victor Stinner13bb71c2010-04-23 21:41:56 +00003384static char**
3385parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3386{
Victor Stinner8c62be82010-05-06 00:08:46 +00003387 char **envlist;
3388 Py_ssize_t i, pos, envc;
3389 PyObject *keys=NULL, *vals=NULL;
3390 PyObject *key, *val, *key2, *val2;
3391 char *p, *k, *v;
3392 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003393
Victor Stinner8c62be82010-05-06 00:08:46 +00003394 i = PyMapping_Size(env);
3395 if (i < 0)
3396 return NULL;
3397 envlist = PyMem_NEW(char *, i + 1);
3398 if (envlist == NULL) {
3399 PyErr_NoMemory();
3400 return NULL;
3401 }
3402 envc = 0;
3403 keys = PyMapping_Keys(env);
3404 vals = PyMapping_Values(env);
3405 if (!keys || !vals)
3406 goto error;
3407 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3408 PyErr_Format(PyExc_TypeError,
3409 "env.keys() or env.values() is not a list");
3410 goto error;
3411 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003412
Victor Stinner8c62be82010-05-06 00:08:46 +00003413 for (pos = 0; pos < i; pos++) {
3414 key = PyList_GetItem(keys, pos);
3415 val = PyList_GetItem(vals, pos);
3416 if (!key || !val)
3417 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003418
Victor Stinner8c62be82010-05-06 00:08:46 +00003419 if (PyUnicode_FSConverter(key, &key2) == 0)
3420 goto error;
3421 if (PyUnicode_FSConverter(val, &val2) == 0) {
3422 Py_DECREF(key2);
3423 goto error;
3424 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003425
3426#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003427 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3428 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003429#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 k = PyBytes_AsString(key2);
3431 v = PyBytes_AsString(val2);
3432 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003433
Victor Stinner8c62be82010-05-06 00:08:46 +00003434 p = PyMem_NEW(char, len);
3435 if (p == NULL) {
3436 PyErr_NoMemory();
3437 Py_DECREF(key2);
3438 Py_DECREF(val2);
3439 goto error;
3440 }
3441 PyOS_snprintf(p, len, "%s=%s", k, v);
3442 envlist[envc++] = p;
3443 Py_DECREF(key2);
3444 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003445#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003447#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003448 }
3449 Py_DECREF(vals);
3450 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003451
Victor Stinner8c62be82010-05-06 00:08:46 +00003452 envlist[envc] = 0;
3453 *envc_ptr = envc;
3454 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003455
3456error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003457 Py_XDECREF(keys);
3458 Py_XDECREF(vals);
3459 while (--envc >= 0)
3460 PyMem_DEL(envlist[envc]);
3461 PyMem_DEL(envlist);
3462 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003463}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003465PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003466"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003467Execute a path with arguments and environment, replacing current process.\n\
3468\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003469 path: path of executable file\n\
3470 args: tuple or list of arguments\n\
3471 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003472
Barry Warsaw53699e91996-12-10 23:23:01 +00003473static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003474posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003475{
Victor Stinner8c62be82010-05-06 00:08:46 +00003476 PyObject *opath;
3477 char *path;
3478 PyObject *argv, *env;
3479 char **argvlist;
3480 char **envlist;
3481 Py_ssize_t i, argc, envc;
3482 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3483 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003484
Victor Stinner8c62be82010-05-06 00:08:46 +00003485 /* execve has three arguments: (path, argv, env), where
3486 argv is a list or tuple of strings and env is a dictionary
3487 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003488
Victor Stinner8c62be82010-05-06 00:08:46 +00003489 if (!PyArg_ParseTuple(args, "O&OO:execve",
3490 PyUnicode_FSConverter,
3491 &opath, &argv, &env))
3492 return NULL;
3493 path = PyBytes_AsString(opath);
3494 if (PyList_Check(argv)) {
3495 argc = PyList_Size(argv);
3496 getitem = PyList_GetItem;
3497 }
3498 else if (PyTuple_Check(argv)) {
3499 argc = PyTuple_Size(argv);
3500 getitem = PyTuple_GetItem;
3501 }
3502 else {
3503 PyErr_SetString(PyExc_TypeError,
3504 "execve() arg 2 must be a tuple or list");
3505 goto fail_0;
3506 }
3507 if (!PyMapping_Check(env)) {
3508 PyErr_SetString(PyExc_TypeError,
3509 "execve() arg 3 must be a mapping object");
3510 goto fail_0;
3511 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003512
Victor Stinner8c62be82010-05-06 00:08:46 +00003513 argvlist = PyMem_NEW(char *, argc+1);
3514 if (argvlist == NULL) {
3515 PyErr_NoMemory();
3516 goto fail_0;
3517 }
3518 for (i = 0; i < argc; i++) {
3519 if (!fsconvert_strdup((*getitem)(argv, i),
3520 &argvlist[i]))
3521 {
3522 lastarg = i;
3523 goto fail_1;
3524 }
3525 }
3526 lastarg = argc;
3527 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003528
Victor Stinner8c62be82010-05-06 00:08:46 +00003529 envlist = parse_envlist(env, &envc);
3530 if (envlist == NULL)
3531 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003532
Victor Stinner8c62be82010-05-06 00:08:46 +00003533 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003534
Victor Stinner8c62be82010-05-06 00:08:46 +00003535 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003536
Victor Stinner8c62be82010-05-06 00:08:46 +00003537 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003538
Victor Stinner8c62be82010-05-06 00:08:46 +00003539 while (--envc >= 0)
3540 PyMem_DEL(envlist[envc]);
3541 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003542 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003544 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 Py_DECREF(opath);
3546 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003547}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003548#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003549
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003550
Guido van Rossuma1065681999-01-25 23:20:23 +00003551#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003552PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003553"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003554Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003555\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003556 mode: mode of process creation\n\
3557 path: path of executable file\n\
3558 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003559
3560static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003561posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003562{
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 PyObject *opath;
3564 char *path;
3565 PyObject *argv;
3566 char **argvlist;
3567 int mode, i;
3568 Py_ssize_t argc;
3569 Py_intptr_t spawnval;
3570 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003571
Victor Stinner8c62be82010-05-06 00:08:46 +00003572 /* spawnv has three arguments: (mode, path, argv), where
3573 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003574
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3576 PyUnicode_FSConverter,
3577 &opath, &argv))
3578 return NULL;
3579 path = PyBytes_AsString(opath);
3580 if (PyList_Check(argv)) {
3581 argc = PyList_Size(argv);
3582 getitem = PyList_GetItem;
3583 }
3584 else if (PyTuple_Check(argv)) {
3585 argc = PyTuple_Size(argv);
3586 getitem = PyTuple_GetItem;
3587 }
3588 else {
3589 PyErr_SetString(PyExc_TypeError,
3590 "spawnv() arg 2 must be a tuple or list");
3591 Py_DECREF(opath);
3592 return NULL;
3593 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003594
Victor Stinner8c62be82010-05-06 00:08:46 +00003595 argvlist = PyMem_NEW(char *, argc+1);
3596 if (argvlist == NULL) {
3597 Py_DECREF(opath);
3598 return PyErr_NoMemory();
3599 }
3600 for (i = 0; i < argc; i++) {
3601 if (!fsconvert_strdup((*getitem)(argv, i),
3602 &argvlist[i])) {
3603 free_string_array(argvlist, i);
3604 PyErr_SetString(
3605 PyExc_TypeError,
3606 "spawnv() arg 2 must contain only strings");
3607 Py_DECREF(opath);
3608 return NULL;
3609 }
3610 }
3611 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003612
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003613#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 Py_BEGIN_ALLOW_THREADS
3615 spawnval = spawnv(mode, path, argvlist);
3616 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003617#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003618 if (mode == _OLD_P_OVERLAY)
3619 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003620
Victor Stinner8c62be82010-05-06 00:08:46 +00003621 Py_BEGIN_ALLOW_THREADS
3622 spawnval = _spawnv(mode, path, argvlist);
3623 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003624#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003625
Victor Stinner8c62be82010-05-06 00:08:46 +00003626 free_string_array(argvlist, argc);
3627 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003628
Victor Stinner8c62be82010-05-06 00:08:46 +00003629 if (spawnval == -1)
3630 return posix_error();
3631 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003632#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003633 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003634#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003635 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003636#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003637}
3638
3639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003640PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003641"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003642Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003643\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003644 mode: mode of process creation\n\
3645 path: path of executable file\n\
3646 args: tuple or list of arguments\n\
3647 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003648
3649static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003650posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003651{
Victor Stinner8c62be82010-05-06 00:08:46 +00003652 PyObject *opath;
3653 char *path;
3654 PyObject *argv, *env;
3655 char **argvlist;
3656 char **envlist;
3657 PyObject *res = NULL;
3658 int mode, envc;
3659 Py_ssize_t argc, i;
3660 Py_intptr_t spawnval;
3661 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3662 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003663
Victor Stinner8c62be82010-05-06 00:08:46 +00003664 /* spawnve has four arguments: (mode, path, argv, env), where
3665 argv is a list or tuple of strings and env is a dictionary
3666 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003667
Victor Stinner8c62be82010-05-06 00:08:46 +00003668 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3669 PyUnicode_FSConverter,
3670 &opath, &argv, &env))
3671 return NULL;
3672 path = PyBytes_AsString(opath);
3673 if (PyList_Check(argv)) {
3674 argc = PyList_Size(argv);
3675 getitem = PyList_GetItem;
3676 }
3677 else if (PyTuple_Check(argv)) {
3678 argc = PyTuple_Size(argv);
3679 getitem = PyTuple_GetItem;
3680 }
3681 else {
3682 PyErr_SetString(PyExc_TypeError,
3683 "spawnve() arg 2 must be a tuple or list");
3684 goto fail_0;
3685 }
3686 if (!PyMapping_Check(env)) {
3687 PyErr_SetString(PyExc_TypeError,
3688 "spawnve() arg 3 must be a mapping object");
3689 goto fail_0;
3690 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003691
Victor Stinner8c62be82010-05-06 00:08:46 +00003692 argvlist = PyMem_NEW(char *, argc+1);
3693 if (argvlist == NULL) {
3694 PyErr_NoMemory();
3695 goto fail_0;
3696 }
3697 for (i = 0; i < argc; i++) {
3698 if (!fsconvert_strdup((*getitem)(argv, i),
3699 &argvlist[i]))
3700 {
3701 lastarg = i;
3702 goto fail_1;
3703 }
3704 }
3705 lastarg = argc;
3706 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003707
Victor Stinner8c62be82010-05-06 00:08:46 +00003708 envlist = parse_envlist(env, &envc);
3709 if (envlist == NULL)
3710 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003711
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003712#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003713 Py_BEGIN_ALLOW_THREADS
3714 spawnval = spawnve(mode, path, argvlist, envlist);
3715 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003716#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003717 if (mode == _OLD_P_OVERLAY)
3718 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003719
Victor Stinner8c62be82010-05-06 00:08:46 +00003720 Py_BEGIN_ALLOW_THREADS
3721 spawnval = _spawnve(mode, path, argvlist, envlist);
3722 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003723#endif
Tim Peters25059d32001-12-07 20:35:43 +00003724
Victor Stinner8c62be82010-05-06 00:08:46 +00003725 if (spawnval == -1)
3726 (void) posix_error();
3727 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003728#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003729 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003730#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003731 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003732#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003733
Victor Stinner8c62be82010-05-06 00:08:46 +00003734 while (--envc >= 0)
3735 PyMem_DEL(envlist[envc]);
3736 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003737 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003739 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003740 Py_DECREF(opath);
3741 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003742}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003743
3744/* OS/2 supports spawnvp & spawnvpe natively */
3745#if defined(PYOS_OS2)
3746PyDoc_STRVAR(posix_spawnvp__doc__,
3747"spawnvp(mode, file, args)\n\n\
3748Execute the program 'file' in a new process, using the environment\n\
3749search path to find the file.\n\
3750\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003751 mode: mode of process creation\n\
3752 file: executable file name\n\
3753 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003754
3755static PyObject *
3756posix_spawnvp(PyObject *self, PyObject *args)
3757{
Victor Stinner8c62be82010-05-06 00:08:46 +00003758 PyObject *opath;
3759 char *path;
3760 PyObject *argv;
3761 char **argvlist;
3762 int mode, i, argc;
3763 Py_intptr_t spawnval;
3764 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003765
Victor Stinner8c62be82010-05-06 00:08:46 +00003766 /* spawnvp has three arguments: (mode, path, argv), where
3767 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003768
Victor Stinner8c62be82010-05-06 00:08:46 +00003769 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3770 PyUnicode_FSConverter,
3771 &opath, &argv))
3772 return NULL;
3773 path = PyBytes_AsString(opath);
3774 if (PyList_Check(argv)) {
3775 argc = PyList_Size(argv);
3776 getitem = PyList_GetItem;
3777 }
3778 else if (PyTuple_Check(argv)) {
3779 argc = PyTuple_Size(argv);
3780 getitem = PyTuple_GetItem;
3781 }
3782 else {
3783 PyErr_SetString(PyExc_TypeError,
3784 "spawnvp() arg 2 must be a tuple or list");
3785 Py_DECREF(opath);
3786 return NULL;
3787 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003788
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 argvlist = PyMem_NEW(char *, argc+1);
3790 if (argvlist == NULL) {
3791 Py_DECREF(opath);
3792 return PyErr_NoMemory();
3793 }
3794 for (i = 0; i < argc; i++) {
3795 if (!fsconvert_strdup((*getitem)(argv, i),
3796 &argvlist[i])) {
3797 free_string_array(argvlist, i);
3798 PyErr_SetString(
3799 PyExc_TypeError,
3800 "spawnvp() arg 2 must contain only strings");
3801 Py_DECREF(opath);
3802 return NULL;
3803 }
3804 }
3805 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003806
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003808#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003809 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003810#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003811 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003812#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003813 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003814
Victor Stinner8c62be82010-05-06 00:08:46 +00003815 free_string_array(argvlist, argc);
3816 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003817
Victor Stinner8c62be82010-05-06 00:08:46 +00003818 if (spawnval == -1)
3819 return posix_error();
3820 else
3821 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003822}
3823
3824
3825PyDoc_STRVAR(posix_spawnvpe__doc__,
3826"spawnvpe(mode, file, args, env)\n\n\
3827Execute the program 'file' in a new process, using the environment\n\
3828search path to find the file.\n\
3829\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003830 mode: mode of process creation\n\
3831 file: executable file name\n\
3832 args: tuple or list of arguments\n\
3833 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003834
3835static PyObject *
3836posix_spawnvpe(PyObject *self, PyObject *args)
3837{
Victor Stinner8c62be82010-05-06 00:08:46 +00003838 PyObject *opath
3839 char *path;
3840 PyObject *argv, *env;
3841 char **argvlist;
3842 char **envlist;
3843 PyObject *res=NULL;
3844 int mode, i, argc, envc;
3845 Py_intptr_t spawnval;
3846 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3847 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003848
Victor Stinner8c62be82010-05-06 00:08:46 +00003849 /* spawnvpe has four arguments: (mode, path, argv, env), where
3850 argv is a list or tuple of strings and env is a dictionary
3851 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003852
Victor Stinner8c62be82010-05-06 00:08:46 +00003853 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3854 PyUnicode_FSConverter,
3855 &opath, &argv, &env))
3856 return NULL;
3857 path = PyBytes_AsString(opath);
3858 if (PyList_Check(argv)) {
3859 argc = PyList_Size(argv);
3860 getitem = PyList_GetItem;
3861 }
3862 else if (PyTuple_Check(argv)) {
3863 argc = PyTuple_Size(argv);
3864 getitem = PyTuple_GetItem;
3865 }
3866 else {
3867 PyErr_SetString(PyExc_TypeError,
3868 "spawnvpe() arg 2 must be a tuple or list");
3869 goto fail_0;
3870 }
3871 if (!PyMapping_Check(env)) {
3872 PyErr_SetString(PyExc_TypeError,
3873 "spawnvpe() arg 3 must be a mapping object");
3874 goto fail_0;
3875 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003876
Victor Stinner8c62be82010-05-06 00:08:46 +00003877 argvlist = PyMem_NEW(char *, argc+1);
3878 if (argvlist == NULL) {
3879 PyErr_NoMemory();
3880 goto fail_0;
3881 }
3882 for (i = 0; i < argc; i++) {
3883 if (!fsconvert_strdup((*getitem)(argv, i),
3884 &argvlist[i]))
3885 {
3886 lastarg = i;
3887 goto fail_1;
3888 }
3889 }
3890 lastarg = argc;
3891 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003892
Victor Stinner8c62be82010-05-06 00:08:46 +00003893 envlist = parse_envlist(env, &envc);
3894 if (envlist == NULL)
3895 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003896
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003898#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003899 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003900#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003901 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003902#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003903 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003904
Victor Stinner8c62be82010-05-06 00:08:46 +00003905 if (spawnval == -1)
3906 (void) posix_error();
3907 else
3908 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003909
Victor Stinner8c62be82010-05-06 00:08:46 +00003910 while (--envc >= 0)
3911 PyMem_DEL(envlist[envc]);
3912 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003913 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003914 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003915 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003916 Py_DECREF(opath);
3917 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003918}
3919#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003920#endif /* HAVE_SPAWNV */
3921
3922
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003923#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003924PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003925"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003926Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3927\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003928Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003929
3930static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003931posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003932{
Victor Stinner8c62be82010-05-06 00:08:46 +00003933 pid_t pid;
3934 int result = 0;
3935 _PyImport_AcquireLock();
3936 pid = fork1();
3937 if (pid == 0) {
3938 /* child: this clobbers and resets the import lock. */
3939 PyOS_AfterFork();
3940 } else {
3941 /* parent: release the import lock. */
3942 result = _PyImport_ReleaseLock();
3943 }
3944 if (pid == -1)
3945 return posix_error();
3946 if (result < 0) {
3947 /* Don't clobber the OSError if the fork failed. */
3948 PyErr_SetString(PyExc_RuntimeError,
3949 "not holding the import lock");
3950 return NULL;
3951 }
3952 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003953}
3954#endif
3955
3956
Guido van Rossumad0ee831995-03-01 10:34:45 +00003957#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003958PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003959"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003960Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003961Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003962
Barry Warsaw53699e91996-12-10 23:23:01 +00003963static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003964posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003965{
Victor Stinner8c62be82010-05-06 00:08:46 +00003966 pid_t pid;
3967 int result = 0;
3968 _PyImport_AcquireLock();
3969 pid = fork();
3970 if (pid == 0) {
3971 /* child: this clobbers and resets the import lock. */
3972 PyOS_AfterFork();
3973 } else {
3974 /* parent: release the import lock. */
3975 result = _PyImport_ReleaseLock();
3976 }
3977 if (pid == -1)
3978 return posix_error();
3979 if (result < 0) {
3980 /* Don't clobber the OSError if the fork failed. */
3981 PyErr_SetString(PyExc_RuntimeError,
3982 "not holding the import lock");
3983 return NULL;
3984 }
3985 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003986}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003987#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003988
Neal Norwitzb59798b2003-03-21 01:43:31 +00003989/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003990/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3991#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003992#define DEV_PTY_FILE "/dev/ptc"
3993#define HAVE_DEV_PTMX
3994#else
3995#define DEV_PTY_FILE "/dev/ptmx"
3996#endif
3997
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003998#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003999#ifdef HAVE_PTY_H
4000#include <pty.h>
4001#else
4002#ifdef HAVE_LIBUTIL_H
4003#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00004004#else
4005#ifdef HAVE_UTIL_H
4006#include <util.h>
4007#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004008#endif /* HAVE_LIBUTIL_H */
4009#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00004010#ifdef HAVE_STROPTS_H
4011#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004012#endif
4013#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004014
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004015#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004016PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004017"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004018Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004019
4020static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004021posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004022{
Victor Stinner8c62be82010-05-06 00:08:46 +00004023 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004024#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004025 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004026#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004027#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004028 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004029#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004030 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004031#endif
4032#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004033
Thomas Wouters70c21a12000-07-14 14:28:33 +00004034#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004035 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4036 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004037#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004038 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4039 if (slave_name == NULL)
4040 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004041
Victor Stinner8c62be82010-05-06 00:08:46 +00004042 slave_fd = open(slave_name, O_RDWR);
4043 if (slave_fd < 0)
4044 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004045#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004046 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4047 if (master_fd < 0)
4048 return posix_error();
4049 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4050 /* change permission of slave */
4051 if (grantpt(master_fd) < 0) {
4052 PyOS_setsig(SIGCHLD, sig_saved);
4053 return posix_error();
4054 }
4055 /* unlock slave */
4056 if (unlockpt(master_fd) < 0) {
4057 PyOS_setsig(SIGCHLD, sig_saved);
4058 return posix_error();
4059 }
4060 PyOS_setsig(SIGCHLD, sig_saved);
4061 slave_name = ptsname(master_fd); /* get name of slave */
4062 if (slave_name == NULL)
4063 return posix_error();
4064 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4065 if (slave_fd < 0)
4066 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004067#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004068 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4069 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004070#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004071 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004072#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004073#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004074#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004075
Victor Stinner8c62be82010-05-06 00:08:46 +00004076 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004077
Fred Drake8cef4cf2000-06-28 16:40:38 +00004078}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004079#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004080
4081#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004082PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004083"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004084Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4085Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004086To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004087
4088static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004089posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004090{
Victor Stinner8c62be82010-05-06 00:08:46 +00004091 int master_fd = -1, result = 0;
4092 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004093
Victor Stinner8c62be82010-05-06 00:08:46 +00004094 _PyImport_AcquireLock();
4095 pid = forkpty(&master_fd, NULL, NULL, NULL);
4096 if (pid == 0) {
4097 /* child: this clobbers and resets the import lock. */
4098 PyOS_AfterFork();
4099 } else {
4100 /* parent: release the import lock. */
4101 result = _PyImport_ReleaseLock();
4102 }
4103 if (pid == -1)
4104 return posix_error();
4105 if (result < 0) {
4106 /* Don't clobber the OSError if the fork failed. */
4107 PyErr_SetString(PyExc_RuntimeError,
4108 "not holding the import lock");
4109 return NULL;
4110 }
4111 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004112}
4113#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004114
Guido van Rossumad0ee831995-03-01 10:34:45 +00004115#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004116PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004117"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004118Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004119
Barry Warsaw53699e91996-12-10 23:23:01 +00004120static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004121posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004122{
Victor Stinner8c62be82010-05-06 00:08:46 +00004123 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004124}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004125#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004126
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004127
Guido van Rossumad0ee831995-03-01 10:34:45 +00004128#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004129PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004130"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004131Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004132
Barry Warsaw53699e91996-12-10 23:23:01 +00004133static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004134posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004135{
Victor Stinner8c62be82010-05-06 00:08:46 +00004136 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004137}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004138#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004140
Guido van Rossumad0ee831995-03-01 10:34:45 +00004141#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004142PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004143"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004144Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004145
Barry Warsaw53699e91996-12-10 23:23:01 +00004146static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004147posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004148{
Victor Stinner8c62be82010-05-06 00:08:46 +00004149 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004150}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004151#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004152
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004153
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004154PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004155"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004156Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004157
Barry Warsaw53699e91996-12-10 23:23:01 +00004158static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004159posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004160{
Victor Stinner8c62be82010-05-06 00:08:46 +00004161 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004162}
4163
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004164
Fred Drakec9680921999-12-13 16:37:25 +00004165#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004166PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004167"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004168Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004169
4170static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004171posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004172{
4173 PyObject *result = NULL;
4174
Fred Drakec9680921999-12-13 16:37:25 +00004175#ifdef NGROUPS_MAX
4176#define MAX_GROUPS NGROUPS_MAX
4177#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004178 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004179#define MAX_GROUPS 64
4180#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004181 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004182
4183 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4184 * This is a helper variable to store the intermediate result when
4185 * that happens.
4186 *
4187 * To keep the code readable the OSX behaviour is unconditional,
4188 * according to the POSIX spec this should be safe on all unix-y
4189 * systems.
4190 */
4191 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004193
Victor Stinner8c62be82010-05-06 00:08:46 +00004194 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004195 if (n < 0) {
4196 if (errno == EINVAL) {
4197 n = getgroups(0, NULL);
4198 if (n == -1) {
4199 return posix_error();
4200 }
4201 if (n == 0) {
4202 /* Avoid malloc(0) */
4203 alt_grouplist = grouplist;
4204 } else {
4205 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4206 if (alt_grouplist == NULL) {
4207 errno = EINVAL;
4208 return posix_error();
4209 }
4210 n = getgroups(n, alt_grouplist);
4211 if (n == -1) {
4212 PyMem_Free(alt_grouplist);
4213 return posix_error();
4214 }
4215 }
4216 } else {
4217 return posix_error();
4218 }
4219 }
4220 result = PyList_New(n);
4221 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004222 int i;
4223 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004224 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004225 if (o == NULL) {
4226 Py_DECREF(result);
4227 result = NULL;
4228 break;
Fred Drakec9680921999-12-13 16:37:25 +00004229 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004230 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004231 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004232 }
4233
4234 if (alt_grouplist != grouplist) {
4235 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004236 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004237
Fred Drakec9680921999-12-13 16:37:25 +00004238 return result;
4239}
4240#endif
4241
Antoine Pitroub7572f02009-12-02 20:46:48 +00004242#ifdef HAVE_INITGROUPS
4243PyDoc_STRVAR(posix_initgroups__doc__,
4244"initgroups(username, gid) -> None\n\n\
4245Call the system initgroups() to initialize the group access list with all of\n\
4246the groups of which the specified username is a member, plus the specified\n\
4247group id.");
4248
4249static PyObject *
4250posix_initgroups(PyObject *self, PyObject *args)
4251{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004252 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00004253 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004254 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00004255 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004256
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004257 if (!PyArg_ParseTuple(args, "O&l:initgroups",
4258 PyUnicode_FSConverter, &oname, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00004259 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004260 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004261
Victor Stinner61ec5dc2010-08-15 09:22:44 +00004262 res = initgroups(username, (gid_t) gid);
4263 Py_DECREF(oname);
4264 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00004265 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004266
Victor Stinner8c62be82010-05-06 00:08:46 +00004267 Py_INCREF(Py_None);
4268 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004269}
4270#endif
4271
Martin v. Löwis606edc12002-06-13 21:09:11 +00004272#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004273PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004274"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004275Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004276
4277static PyObject *
4278posix_getpgid(PyObject *self, PyObject *args)
4279{
Victor Stinner8c62be82010-05-06 00:08:46 +00004280 pid_t pid, pgid;
4281 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4282 return NULL;
4283 pgid = getpgid(pid);
4284 if (pgid < 0)
4285 return posix_error();
4286 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004287}
4288#endif /* HAVE_GETPGID */
4289
4290
Guido van Rossumb6775db1994-08-01 11:34:53 +00004291#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004292PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004293"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004294Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004295
Barry Warsaw53699e91996-12-10 23:23:01 +00004296static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004297posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004298{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004299#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004300 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004301#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004303#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004304}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004305#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004306
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004307
Guido van Rossumb6775db1994-08-01 11:34:53 +00004308#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004309PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004310"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004311Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004312
Barry Warsaw53699e91996-12-10 23:23:01 +00004313static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004314posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004315{
Guido van Rossum64933891994-10-20 21:56:42 +00004316#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004317 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004318#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004319 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004320#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004321 return posix_error();
4322 Py_INCREF(Py_None);
4323 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004324}
4325
Guido van Rossumb6775db1994-08-01 11:34:53 +00004326#endif /* HAVE_SETPGRP */
4327
Guido van Rossumad0ee831995-03-01 10:34:45 +00004328#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004329PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004330"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004331Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004332
Barry Warsaw53699e91996-12-10 23:23:01 +00004333static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004334posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004335{
Victor Stinner8c62be82010-05-06 00:08:46 +00004336 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004337}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004338#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004340
Fred Drake12c6e2d1999-12-14 21:25:03 +00004341#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004342PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004343"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004344Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004345
4346static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004347posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004348{
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 PyObject *result = NULL;
4350 char *name;
4351 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004352
Victor Stinner8c62be82010-05-06 00:08:46 +00004353 errno = 0;
4354 name = getlogin();
4355 if (name == NULL) {
4356 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00004357 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004358 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004359 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00004360 }
4361 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00004362 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00004363 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004364
Fred Drake12c6e2d1999-12-14 21:25:03 +00004365 return result;
4366}
4367#endif
4368
Guido van Rossumad0ee831995-03-01 10:34:45 +00004369#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004370PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004371"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004372Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004373
Barry Warsaw53699e91996-12-10 23:23:01 +00004374static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004375posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004376{
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004378}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004379#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004380
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004381
Guido van Rossumad0ee831995-03-01 10:34:45 +00004382#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004383PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004384"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004385Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004386
Barry Warsaw53699e91996-12-10 23:23:01 +00004387static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004388posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004389{
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 pid_t pid;
4391 int sig;
4392 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4393 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004394#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004395 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4396 APIRET rc;
4397 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004398 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004399
4400 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4401 APIRET rc;
4402 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004403 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004404
4405 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004406 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004407#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004408 if (kill(pid, sig) == -1)
4409 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004410#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004411 Py_INCREF(Py_None);
4412 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004413}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004414#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004415
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004416#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004417PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004418"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004419Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004420
4421static PyObject *
4422posix_killpg(PyObject *self, PyObject *args)
4423{
Victor Stinner8c62be82010-05-06 00:08:46 +00004424 int sig;
4425 pid_t pgid;
4426 /* XXX some man pages make the `pgid` parameter an int, others
4427 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4428 take the same type. Moreover, pid_t is always at least as wide as
4429 int (else compilation of this module fails), which is safe. */
4430 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4431 return NULL;
4432 if (killpg(pgid, sig) == -1)
4433 return posix_error();
4434 Py_INCREF(Py_None);
4435 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004436}
4437#endif
4438
Brian Curtineb24d742010-04-12 17:16:38 +00004439#ifdef MS_WINDOWS
4440PyDoc_STRVAR(win32_kill__doc__,
4441"kill(pid, sig)\n\n\
4442Kill a process with a signal.");
4443
4444static PyObject *
4445win32_kill(PyObject *self, PyObject *args)
4446{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004447 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004448 DWORD pid, sig, err;
4449 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004450
Victor Stinner8c62be82010-05-06 00:08:46 +00004451 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4452 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004453
Victor Stinner8c62be82010-05-06 00:08:46 +00004454 /* Console processes which share a common console can be sent CTRL+C or
4455 CTRL+BREAK events, provided they handle said events. */
4456 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4457 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4458 err = GetLastError();
4459 PyErr_SetFromWindowsErr(err);
4460 }
4461 else
4462 Py_RETURN_NONE;
4463 }
Brian Curtineb24d742010-04-12 17:16:38 +00004464
Victor Stinner8c62be82010-05-06 00:08:46 +00004465 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4466 attempt to open and terminate the process. */
4467 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4468 if (handle == NULL) {
4469 err = GetLastError();
4470 return PyErr_SetFromWindowsErr(err);
4471 }
Brian Curtineb24d742010-04-12 17:16:38 +00004472
Victor Stinner8c62be82010-05-06 00:08:46 +00004473 if (TerminateProcess(handle, sig) == 0) {
4474 err = GetLastError();
4475 result = PyErr_SetFromWindowsErr(err);
4476 } else {
4477 Py_INCREF(Py_None);
4478 result = Py_None;
4479 }
Brian Curtineb24d742010-04-12 17:16:38 +00004480
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 CloseHandle(handle);
4482 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004483}
4484#endif /* MS_WINDOWS */
4485
Guido van Rossumc0125471996-06-28 18:55:32 +00004486#ifdef HAVE_PLOCK
4487
4488#ifdef HAVE_SYS_LOCK_H
4489#include <sys/lock.h>
4490#endif
4491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004492PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004493"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004494Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004495
Barry Warsaw53699e91996-12-10 23:23:01 +00004496static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004497posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004498{
Victor Stinner8c62be82010-05-06 00:08:46 +00004499 int op;
4500 if (!PyArg_ParseTuple(args, "i:plock", &op))
4501 return NULL;
4502 if (plock(op) == -1)
4503 return posix_error();
4504 Py_INCREF(Py_None);
4505 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004506}
4507#endif
4508
Guido van Rossumb6775db1994-08-01 11:34:53 +00004509#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004510PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004511"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004512Set the current process's user id.");
4513
Barry Warsaw53699e91996-12-10 23:23:01 +00004514static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004515posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004516{
Victor Stinner8c62be82010-05-06 00:08:46 +00004517 long uid_arg;
4518 uid_t uid;
4519 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4520 return NULL;
4521 uid = uid_arg;
4522 if (uid != uid_arg) {
4523 PyErr_SetString(PyExc_OverflowError, "user id too big");
4524 return NULL;
4525 }
4526 if (setuid(uid) < 0)
4527 return posix_error();
4528 Py_INCREF(Py_None);
4529 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004530}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004531#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004532
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004533
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004534#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004535PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004536"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004537Set the current process's effective user id.");
4538
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004539static PyObject *
4540posix_seteuid (PyObject *self, PyObject *args)
4541{
Victor Stinner8c62be82010-05-06 00:08:46 +00004542 long euid_arg;
4543 uid_t euid;
4544 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4545 return NULL;
4546 euid = euid_arg;
4547 if (euid != euid_arg) {
4548 PyErr_SetString(PyExc_OverflowError, "user id too big");
4549 return NULL;
4550 }
4551 if (seteuid(euid) < 0) {
4552 return posix_error();
4553 } else {
4554 Py_INCREF(Py_None);
4555 return Py_None;
4556 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004557}
4558#endif /* HAVE_SETEUID */
4559
4560#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004561PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004562"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004563Set the current process's effective group id.");
4564
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004565static PyObject *
4566posix_setegid (PyObject *self, PyObject *args)
4567{
Victor Stinner8c62be82010-05-06 00:08:46 +00004568 long egid_arg;
4569 gid_t egid;
4570 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4571 return NULL;
4572 egid = egid_arg;
4573 if (egid != egid_arg) {
4574 PyErr_SetString(PyExc_OverflowError, "group id too big");
4575 return NULL;
4576 }
4577 if (setegid(egid) < 0) {
4578 return posix_error();
4579 } else {
4580 Py_INCREF(Py_None);
4581 return Py_None;
4582 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004583}
4584#endif /* HAVE_SETEGID */
4585
4586#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004587PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004588"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004589Set the current process's real and effective user ids.");
4590
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004591static PyObject *
4592posix_setreuid (PyObject *self, PyObject *args)
4593{
Victor Stinner8c62be82010-05-06 00:08:46 +00004594 long ruid_arg, euid_arg;
4595 uid_t ruid, euid;
4596 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4597 return NULL;
4598 if (ruid_arg == -1)
4599 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4600 else
4601 ruid = ruid_arg; /* otherwise, assign from our long */
4602 if (euid_arg == -1)
4603 euid = (uid_t)-1;
4604 else
4605 euid = euid_arg;
4606 if ((euid_arg != -1 && euid != euid_arg) ||
4607 (ruid_arg != -1 && ruid != ruid_arg)) {
4608 PyErr_SetString(PyExc_OverflowError, "user id too big");
4609 return NULL;
4610 }
4611 if (setreuid(ruid, euid) < 0) {
4612 return posix_error();
4613 } else {
4614 Py_INCREF(Py_None);
4615 return Py_None;
4616 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004617}
4618#endif /* HAVE_SETREUID */
4619
4620#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004621PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004622"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004623Set the current process's real and effective group ids.");
4624
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004625static PyObject *
4626posix_setregid (PyObject *self, PyObject *args)
4627{
Victor Stinner8c62be82010-05-06 00:08:46 +00004628 long rgid_arg, egid_arg;
4629 gid_t rgid, egid;
4630 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4631 return NULL;
4632 if (rgid_arg == -1)
4633 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4634 else
4635 rgid = rgid_arg; /* otherwise, assign from our long */
4636 if (egid_arg == -1)
4637 egid = (gid_t)-1;
4638 else
4639 egid = egid_arg;
4640 if ((egid_arg != -1 && egid != egid_arg) ||
4641 (rgid_arg != -1 && rgid != rgid_arg)) {
4642 PyErr_SetString(PyExc_OverflowError, "group id too big");
4643 return NULL;
4644 }
4645 if (setregid(rgid, egid) < 0) {
4646 return posix_error();
4647 } else {
4648 Py_INCREF(Py_None);
4649 return Py_None;
4650 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004651}
4652#endif /* HAVE_SETREGID */
4653
Guido van Rossumb6775db1994-08-01 11:34:53 +00004654#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004655PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004656"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004657Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004658
Barry Warsaw53699e91996-12-10 23:23:01 +00004659static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004660posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004661{
Victor Stinner8c62be82010-05-06 00:08:46 +00004662 long gid_arg;
4663 gid_t gid;
4664 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4665 return NULL;
4666 gid = gid_arg;
4667 if (gid != gid_arg) {
4668 PyErr_SetString(PyExc_OverflowError, "group id too big");
4669 return NULL;
4670 }
4671 if (setgid(gid) < 0)
4672 return posix_error();
4673 Py_INCREF(Py_None);
4674 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004675}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004676#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004677
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004678#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004679PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004680"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004681Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004682
4683static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004684posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004685{
Victor Stinner8c62be82010-05-06 00:08:46 +00004686 int i, len;
4687 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004688
Victor Stinner8c62be82010-05-06 00:08:46 +00004689 if (!PySequence_Check(groups)) {
4690 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4691 return NULL;
4692 }
4693 len = PySequence_Size(groups);
4694 if (len > MAX_GROUPS) {
4695 PyErr_SetString(PyExc_ValueError, "too many groups");
4696 return NULL;
4697 }
4698 for(i = 0; i < len; i++) {
4699 PyObject *elem;
4700 elem = PySequence_GetItem(groups, i);
4701 if (!elem)
4702 return NULL;
4703 if (!PyLong_Check(elem)) {
4704 PyErr_SetString(PyExc_TypeError,
4705 "groups must be integers");
4706 Py_DECREF(elem);
4707 return NULL;
4708 } else {
4709 unsigned long x = PyLong_AsUnsignedLong(elem);
4710 if (PyErr_Occurred()) {
4711 PyErr_SetString(PyExc_TypeError,
4712 "group id too big");
4713 Py_DECREF(elem);
4714 return NULL;
4715 }
4716 grouplist[i] = x;
4717 /* read back the value to see if it fitted in gid_t */
4718 if (grouplist[i] != x) {
4719 PyErr_SetString(PyExc_TypeError,
4720 "group id too big");
4721 Py_DECREF(elem);
4722 return NULL;
4723 }
4724 }
4725 Py_DECREF(elem);
4726 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004727
Victor Stinner8c62be82010-05-06 00:08:46 +00004728 if (setgroups(len, grouplist) < 0)
4729 return posix_error();
4730 Py_INCREF(Py_None);
4731 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004732}
4733#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004734
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004735#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4736static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004737wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004738{
Victor Stinner8c62be82010-05-06 00:08:46 +00004739 PyObject *result;
4740 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004741
Victor Stinner8c62be82010-05-06 00:08:46 +00004742 if (pid == -1)
4743 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004744
Victor Stinner8c62be82010-05-06 00:08:46 +00004745 if (struct_rusage == NULL) {
4746 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4747 if (m == NULL)
4748 return NULL;
4749 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4750 Py_DECREF(m);
4751 if (struct_rusage == NULL)
4752 return NULL;
4753 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004754
Victor Stinner8c62be82010-05-06 00:08:46 +00004755 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4756 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4757 if (!result)
4758 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004759
4760#ifndef doubletime
4761#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4762#endif
4763
Victor Stinner8c62be82010-05-06 00:08:46 +00004764 PyStructSequence_SET_ITEM(result, 0,
4765 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4766 PyStructSequence_SET_ITEM(result, 1,
4767 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004768#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004769 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4770 SET_INT(result, 2, ru->ru_maxrss);
4771 SET_INT(result, 3, ru->ru_ixrss);
4772 SET_INT(result, 4, ru->ru_idrss);
4773 SET_INT(result, 5, ru->ru_isrss);
4774 SET_INT(result, 6, ru->ru_minflt);
4775 SET_INT(result, 7, ru->ru_majflt);
4776 SET_INT(result, 8, ru->ru_nswap);
4777 SET_INT(result, 9, ru->ru_inblock);
4778 SET_INT(result, 10, ru->ru_oublock);
4779 SET_INT(result, 11, ru->ru_msgsnd);
4780 SET_INT(result, 12, ru->ru_msgrcv);
4781 SET_INT(result, 13, ru->ru_nsignals);
4782 SET_INT(result, 14, ru->ru_nvcsw);
4783 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004784#undef SET_INT
4785
Victor Stinner8c62be82010-05-06 00:08:46 +00004786 if (PyErr_Occurred()) {
4787 Py_DECREF(result);
4788 return NULL;
4789 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004790
Victor Stinner8c62be82010-05-06 00:08:46 +00004791 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004792}
4793#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4794
4795#ifdef HAVE_WAIT3
4796PyDoc_STRVAR(posix_wait3__doc__,
4797"wait3(options) -> (pid, status, rusage)\n\n\
4798Wait for completion of a child process.");
4799
4800static PyObject *
4801posix_wait3(PyObject *self, PyObject *args)
4802{
Victor Stinner8c62be82010-05-06 00:08:46 +00004803 pid_t pid;
4804 int options;
4805 struct rusage ru;
4806 WAIT_TYPE status;
4807 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004808
Victor Stinner8c62be82010-05-06 00:08:46 +00004809 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4810 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004811
Victor Stinner8c62be82010-05-06 00:08:46 +00004812 Py_BEGIN_ALLOW_THREADS
4813 pid = wait3(&status, options, &ru);
4814 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004815
Victor Stinner8c62be82010-05-06 00:08:46 +00004816 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004817}
4818#endif /* HAVE_WAIT3 */
4819
4820#ifdef HAVE_WAIT4
4821PyDoc_STRVAR(posix_wait4__doc__,
4822"wait4(pid, options) -> (pid, status, rusage)\n\n\
4823Wait for completion of a given child process.");
4824
4825static PyObject *
4826posix_wait4(PyObject *self, PyObject *args)
4827{
Victor Stinner8c62be82010-05-06 00:08:46 +00004828 pid_t pid;
4829 int options;
4830 struct rusage ru;
4831 WAIT_TYPE status;
4832 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004833
Victor Stinner8c62be82010-05-06 00:08:46 +00004834 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4835 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004836
Victor Stinner8c62be82010-05-06 00:08:46 +00004837 Py_BEGIN_ALLOW_THREADS
4838 pid = wait4(pid, &status, options, &ru);
4839 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004840
Victor Stinner8c62be82010-05-06 00:08:46 +00004841 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004842}
4843#endif /* HAVE_WAIT4 */
4844
Guido van Rossumb6775db1994-08-01 11:34:53 +00004845#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004846PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004847"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004848Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004849
Barry Warsaw53699e91996-12-10 23:23:01 +00004850static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004851posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004852{
Victor Stinner8c62be82010-05-06 00:08:46 +00004853 pid_t pid;
4854 int options;
4855 WAIT_TYPE status;
4856 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004857
Victor Stinner8c62be82010-05-06 00:08:46 +00004858 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4859 return NULL;
4860 Py_BEGIN_ALLOW_THREADS
4861 pid = waitpid(pid, &status, options);
4862 Py_END_ALLOW_THREADS
4863 if (pid == -1)
4864 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004865
Victor Stinner8c62be82010-05-06 00:08:46 +00004866 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004867}
4868
Tim Petersab034fa2002-02-01 11:27:43 +00004869#elif defined(HAVE_CWAIT)
4870
4871/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004872PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004873"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004874"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004875
4876static PyObject *
4877posix_waitpid(PyObject *self, PyObject *args)
4878{
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 Py_intptr_t pid;
4880 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004881
Victor Stinner8c62be82010-05-06 00:08:46 +00004882 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4883 return NULL;
4884 Py_BEGIN_ALLOW_THREADS
4885 pid = _cwait(&status, pid, options);
4886 Py_END_ALLOW_THREADS
4887 if (pid == -1)
4888 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004889
Victor Stinner8c62be82010-05-06 00:08:46 +00004890 /* shift the status left a byte so this is more like the POSIX waitpid */
4891 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004892}
4893#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004894
Guido van Rossumad0ee831995-03-01 10:34:45 +00004895#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004896PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004897"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004898Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004899
Barry Warsaw53699e91996-12-10 23:23:01 +00004900static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004901posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004902{
Victor Stinner8c62be82010-05-06 00:08:46 +00004903 pid_t pid;
4904 WAIT_TYPE status;
4905 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004906
Victor Stinner8c62be82010-05-06 00:08:46 +00004907 Py_BEGIN_ALLOW_THREADS
4908 pid = wait(&status);
4909 Py_END_ALLOW_THREADS
4910 if (pid == -1)
4911 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004912
Victor Stinner8c62be82010-05-06 00:08:46 +00004913 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004914}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004915#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004916
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004917
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004918PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004919"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004920Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004921
Barry Warsaw53699e91996-12-10 23:23:01 +00004922static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004923posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004924{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004925#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004926 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004927#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004928#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00004929 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
4930 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004931#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004932 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004933#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004934#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004935}
4936
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004937
Guido van Rossumb6775db1994-08-01 11:34:53 +00004938#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004939PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004940"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004941Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004942
Barry Warsaw53699e91996-12-10 23:23:01 +00004943static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004944posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004945{
Victor Stinner8c62be82010-05-06 00:08:46 +00004946 PyObject* v;
4947 char buf[MAXPATHLEN];
4948 PyObject *opath;
4949 char *path;
4950 int n;
4951 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004952
Victor Stinner8c62be82010-05-06 00:08:46 +00004953 if (!PyArg_ParseTuple(args, "O&:readlink",
4954 PyUnicode_FSConverter, &opath))
4955 return NULL;
4956 path = PyBytes_AsString(opath);
4957 v = PySequence_GetItem(args, 0);
4958 if (v == NULL) {
4959 Py_DECREF(opath);
4960 return NULL;
4961 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004962
Victor Stinner8c62be82010-05-06 00:08:46 +00004963 if (PyUnicode_Check(v)) {
4964 arg_is_unicode = 1;
4965 }
4966 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004967
Victor Stinner8c62be82010-05-06 00:08:46 +00004968 Py_BEGIN_ALLOW_THREADS
4969 n = readlink(path, buf, (int) sizeof buf);
4970 Py_END_ALLOW_THREADS
4971 if (n < 0)
4972 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004973
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00004975 if (arg_is_unicode)
4976 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
4977 else
4978 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004979}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004980#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004982
Guido van Rossumb6775db1994-08-01 11:34:53 +00004983#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004984PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004985"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004986Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004987
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004988static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004989posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004990{
Victor Stinner8c62be82010-05-06 00:08:46 +00004991 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004992}
4993#endif /* HAVE_SYMLINK */
4994
Brian Curtind40e6f72010-07-08 21:39:08 +00004995#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
4996
4997PyDoc_STRVAR(win_readlink__doc__,
4998"readlink(path) -> path\n\n\
4999Return a string representing the path to which the symbolic link points.");
5000
5001/* The following structure was copied from
5002 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
5003 include doesn't seem to be present in the Windows SDK (at least as included
5004 with Visual Studio Express). */
5005typedef struct _REPARSE_DATA_BUFFER {
5006 ULONG ReparseTag;
5007 USHORT ReparseDataLength;
5008 USHORT Reserved;
5009 union {
5010 struct {
5011 USHORT SubstituteNameOffset;
5012 USHORT SubstituteNameLength;
5013 USHORT PrintNameOffset;
5014 USHORT PrintNameLength;
5015 ULONG Flags;
5016 WCHAR PathBuffer[1];
5017 } SymbolicLinkReparseBuffer;
5018
5019 struct {
5020 USHORT SubstituteNameOffset;
5021 USHORT SubstituteNameLength;
5022 USHORT PrintNameOffset;
5023 USHORT PrintNameLength;
5024 WCHAR PathBuffer[1];
5025 } MountPointReparseBuffer;
5026
5027 struct {
5028 UCHAR DataBuffer[1];
5029 } GenericReparseBuffer;
5030 };
5031} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
5032
Brian Curtin74e45612010-07-09 15:58:59 +00005033#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
5034 GenericReparseBuffer)
Brian Curtind40e6f72010-07-08 21:39:08 +00005035
5036#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
5037
5038/* Windows readlink implementation */
5039static PyObject *
5040win_readlink(PyObject *self, PyObject *args)
5041{
5042 wchar_t *path;
5043 DWORD n_bytes_returned;
5044 DWORD io_result;
5045 PyObject *result;
5046 HANDLE reparse_point_handle;
5047
5048 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5049 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5050 wchar_t *print_name;
5051
5052 if (!PyArg_ParseTuple(args,
5053 "u:readlink",
5054 &path))
5055 return NULL;
5056
5057 /* First get a handle to the reparse point */
5058 Py_BEGIN_ALLOW_THREADS
5059 reparse_point_handle = CreateFileW(
5060 path,
5061 0,
5062 0,
5063 0,
5064 OPEN_EXISTING,
5065 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5066 0);
5067 Py_END_ALLOW_THREADS
5068
5069 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5070 {
5071 return win32_error_unicode("readlink", path);
5072 }
5073
5074 Py_BEGIN_ALLOW_THREADS
5075 /* New call DeviceIoControl to read the reparse point */
5076 io_result = DeviceIoControl(
5077 reparse_point_handle,
5078 FSCTL_GET_REPARSE_POINT,
5079 0, 0, /* in buffer */
5080 target_buffer, sizeof(target_buffer),
5081 &n_bytes_returned,
5082 0 /* we're not using OVERLAPPED_IO */
5083 );
5084 CloseHandle(reparse_point_handle);
5085 Py_END_ALLOW_THREADS
5086
5087 if (io_result==0)
5088 {
5089 return win32_error_unicode("readlink", path);
5090 }
5091
5092 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5093 {
5094 PyErr_SetString(PyExc_ValueError,
5095 "not a symbolic link");
5096 return NULL;
5097 }
Brian Curtin74e45612010-07-09 15:58:59 +00005098 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5099 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5100
5101 result = PyUnicode_FromWideChar(print_name,
5102 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005103 return result;
5104}
5105
5106#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5107
5108#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
5109
5110/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5111static int has_CreateSymbolicLinkW = 0;
5112static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5113static int
5114check_CreateSymbolicLinkW()
5115{
5116 HINSTANCE hKernel32;
5117 /* only recheck */
5118 if (has_CreateSymbolicLinkW)
5119 return has_CreateSymbolicLinkW;
5120 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005121 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5122 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005123 if (Py_CreateSymbolicLinkW)
5124 has_CreateSymbolicLinkW = 1;
5125 return has_CreateSymbolicLinkW;
5126}
5127
5128PyDoc_STRVAR(win_symlink__doc__,
5129"symlink(src, dst, target_is_directory=False)\n\n\
5130Create a symbolic link pointing to src named dst.\n\
5131target_is_directory is required if the target is to be interpreted as\n\
5132a directory.\n\
5133This function requires Windows 6.0 or greater, and raises a\n\
5134NotImplementedError otherwise.");
5135
5136static PyObject *
5137win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5138{
5139 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5140 PyObject *src, *dest;
5141 int target_is_directory = 0;
5142 DWORD res;
5143 WIN32_FILE_ATTRIBUTE_DATA src_info;
5144
5145 if (!check_CreateSymbolicLinkW())
5146 {
5147 /* raise NotImplementedError */
5148 return PyErr_Format(PyExc_NotImplementedError,
5149 "CreateSymbolicLinkW not found");
5150 }
5151 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5152 kwlist, &src, &dest, &target_is_directory))
5153 return NULL;
5154 if (!convert_to_unicode(&src)) { return NULL; }
5155 if (!convert_to_unicode(&dest)) {
5156 Py_DECREF(src);
5157 return NULL;
5158 }
5159
5160 /* if src is a directory, ensure target_is_directory==1 */
5161 if(
5162 GetFileAttributesExW(
5163 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5164 ))
5165 {
5166 target_is_directory = target_is_directory ||
5167 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5168 }
5169
5170 Py_BEGIN_ALLOW_THREADS
5171 res = Py_CreateSymbolicLinkW(
5172 PyUnicode_AsUnicode(dest),
5173 PyUnicode_AsUnicode(src),
5174 target_is_directory);
5175 Py_END_ALLOW_THREADS
5176 Py_DECREF(src);
5177 Py_DECREF(dest);
5178 if (!res)
5179 {
5180 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5181 }
5182
5183 Py_INCREF(Py_None);
5184 return Py_None;
5185}
5186#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005187
5188#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005189#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5190static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005191system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005192{
5193 ULONG value = 0;
5194
5195 Py_BEGIN_ALLOW_THREADS
5196 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5197 Py_END_ALLOW_THREADS
5198
5199 return value;
5200}
5201
5202static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005203posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005204{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005205 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005206 return Py_BuildValue("ddddd",
5207 (double)0 /* t.tms_utime / HZ */,
5208 (double)0 /* t.tms_stime / HZ */,
5209 (double)0 /* t.tms_cutime / HZ */,
5210 (double)0 /* t.tms_cstime / HZ */,
5211 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005212}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005213#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005214#define NEED_TICKS_PER_SECOND
5215static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005216static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005217posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005218{
Victor Stinner8c62be82010-05-06 00:08:46 +00005219 struct tms t;
5220 clock_t c;
5221 errno = 0;
5222 c = times(&t);
5223 if (c == (clock_t) -1)
5224 return posix_error();
5225 return Py_BuildValue("ddddd",
5226 (double)t.tms_utime / ticks_per_second,
5227 (double)t.tms_stime / ticks_per_second,
5228 (double)t.tms_cutime / ticks_per_second,
5229 (double)t.tms_cstime / ticks_per_second,
5230 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005231}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005232#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005233#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005234
5235
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005236#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005237#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005238static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005239posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005240{
Victor Stinner8c62be82010-05-06 00:08:46 +00005241 FILETIME create, exit, kernel, user;
5242 HANDLE hProc;
5243 hProc = GetCurrentProcess();
5244 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5245 /* The fields of a FILETIME structure are the hi and lo part
5246 of a 64-bit value expressed in 100 nanosecond units.
5247 1e7 is one second in such units; 1e-7 the inverse.
5248 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5249 */
5250 return Py_BuildValue(
5251 "ddddd",
5252 (double)(user.dwHighDateTime*429.4967296 +
5253 user.dwLowDateTime*1e-7),
5254 (double)(kernel.dwHighDateTime*429.4967296 +
5255 kernel.dwLowDateTime*1e-7),
5256 (double)0,
5257 (double)0,
5258 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005259}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005260#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005261
5262#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005263PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005264"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005265Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005266#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005267
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005268
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005269#ifdef HAVE_GETSID
5270PyDoc_STRVAR(posix_getsid__doc__,
5271"getsid(pid) -> sid\n\n\
5272Call the system call getsid().");
5273
5274static PyObject *
5275posix_getsid(PyObject *self, PyObject *args)
5276{
Victor Stinner8c62be82010-05-06 00:08:46 +00005277 pid_t pid;
5278 int sid;
5279 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5280 return NULL;
5281 sid = getsid(pid);
5282 if (sid < 0)
5283 return posix_error();
5284 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005285}
5286#endif /* HAVE_GETSID */
5287
5288
Guido van Rossumb6775db1994-08-01 11:34:53 +00005289#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005290PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005291"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005292Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005293
Barry Warsaw53699e91996-12-10 23:23:01 +00005294static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005295posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005296{
Victor Stinner8c62be82010-05-06 00:08:46 +00005297 if (setsid() < 0)
5298 return posix_error();
5299 Py_INCREF(Py_None);
5300 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005301}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005302#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005303
Guido van Rossumb6775db1994-08-01 11:34:53 +00005304#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005305PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005306"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005307Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005308
Barry Warsaw53699e91996-12-10 23:23:01 +00005309static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005310posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005311{
Victor Stinner8c62be82010-05-06 00:08:46 +00005312 pid_t pid;
5313 int pgrp;
5314 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5315 return NULL;
5316 if (setpgid(pid, pgrp) < 0)
5317 return posix_error();
5318 Py_INCREF(Py_None);
5319 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005320}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005321#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005322
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005323
Guido van Rossumb6775db1994-08-01 11:34:53 +00005324#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005325PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005326"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005327Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005328
Barry Warsaw53699e91996-12-10 23:23:01 +00005329static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005330posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005331{
Victor Stinner8c62be82010-05-06 00:08:46 +00005332 int fd;
5333 pid_t pgid;
5334 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5335 return NULL;
5336 pgid = tcgetpgrp(fd);
5337 if (pgid < 0)
5338 return posix_error();
5339 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005340}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005341#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005342
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005343
Guido van Rossumb6775db1994-08-01 11:34:53 +00005344#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005345PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005346"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005347Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005348
Barry Warsaw53699e91996-12-10 23:23:01 +00005349static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005350posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005351{
Victor Stinner8c62be82010-05-06 00:08:46 +00005352 int fd;
5353 pid_t pgid;
5354 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5355 return NULL;
5356 if (tcsetpgrp(fd, pgid) < 0)
5357 return posix_error();
5358 Py_INCREF(Py_None);
5359 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005360}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005361#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005362
Guido van Rossum687dd131993-05-17 08:34:16 +00005363/* Functions acting on file descriptors */
5364
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005365PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005366"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005367Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005368
Barry Warsaw53699e91996-12-10 23:23:01 +00005369static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005370posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005371{
Victor Stinner8c62be82010-05-06 00:08:46 +00005372 PyObject *ofile;
5373 char *file;
5374 int flag;
5375 int mode = 0777;
5376 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005377
5378#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005379 PyUnicodeObject *po;
5380 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5381 Py_BEGIN_ALLOW_THREADS
5382 /* PyUnicode_AS_UNICODE OK without thread
5383 lock as it is a simple dereference. */
5384 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5385 Py_END_ALLOW_THREADS
5386 if (fd < 0)
5387 return posix_error();
5388 return PyLong_FromLong((long)fd);
5389 }
5390 /* Drop the argument parsing error as narrow strings
5391 are also valid. */
5392 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005393#endif
5394
Victor Stinner8c62be82010-05-06 00:08:46 +00005395 if (!PyArg_ParseTuple(args, "O&i|i",
5396 PyUnicode_FSConverter, &ofile,
5397 &flag, &mode))
5398 return NULL;
5399 file = PyBytes_AsString(ofile);
5400 Py_BEGIN_ALLOW_THREADS
5401 fd = open(file, flag, mode);
5402 Py_END_ALLOW_THREADS
5403 if (fd < 0)
5404 return posix_error_with_allocated_filename(ofile);
5405 Py_DECREF(ofile);
5406 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005407}
5408
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005410PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005411"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005412Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005413
Barry Warsaw53699e91996-12-10 23:23:01 +00005414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005415posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005416{
Victor Stinner8c62be82010-05-06 00:08:46 +00005417 int fd, res;
5418 if (!PyArg_ParseTuple(args, "i:close", &fd))
5419 return NULL;
5420 if (!_PyVerify_fd(fd))
5421 return posix_error();
5422 Py_BEGIN_ALLOW_THREADS
5423 res = close(fd);
5424 Py_END_ALLOW_THREADS
5425 if (res < 0)
5426 return posix_error();
5427 Py_INCREF(Py_None);
5428 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005429}
5430
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005431
Victor Stinner8c62be82010-05-06 00:08:46 +00005432PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005433"closerange(fd_low, fd_high)\n\n\
5434Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5435
5436static PyObject *
5437posix_closerange(PyObject *self, PyObject *args)
5438{
Victor Stinner8c62be82010-05-06 00:08:46 +00005439 int fd_from, fd_to, i;
5440 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5441 return NULL;
5442 Py_BEGIN_ALLOW_THREADS
5443 for (i = fd_from; i < fd_to; i++)
5444 if (_PyVerify_fd(i))
5445 close(i);
5446 Py_END_ALLOW_THREADS
5447 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005448}
5449
5450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005451PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005452"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005453Return a duplicate of a file descriptor.");
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_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005457{
Victor Stinner8c62be82010-05-06 00:08:46 +00005458 int fd;
5459 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5460 return NULL;
5461 if (!_PyVerify_fd(fd))
5462 return posix_error();
5463 Py_BEGIN_ALLOW_THREADS
5464 fd = dup(fd);
5465 Py_END_ALLOW_THREADS
5466 if (fd < 0)
5467 return posix_error();
5468 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005469}
5470
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005472PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005473"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005474Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005475
Barry Warsaw53699e91996-12-10 23:23:01 +00005476static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005477posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005478{
Victor Stinner8c62be82010-05-06 00:08:46 +00005479 int fd, fd2, res;
5480 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5481 return NULL;
5482 if (!_PyVerify_fd_dup2(fd, fd2))
5483 return posix_error();
5484 Py_BEGIN_ALLOW_THREADS
5485 res = dup2(fd, fd2);
5486 Py_END_ALLOW_THREADS
5487 if (res < 0)
5488 return posix_error();
5489 Py_INCREF(Py_None);
5490 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005491}
5492
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005494PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005495"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005496Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005497
Barry Warsaw53699e91996-12-10 23:23:01 +00005498static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005499posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005500{
Victor Stinner8c62be82010-05-06 00:08:46 +00005501 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005502#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005503 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005504#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005505 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005506#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005507 PyObject *posobj;
5508 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5509 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005510#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5512 switch (how) {
5513 case 0: how = SEEK_SET; break;
5514 case 1: how = SEEK_CUR; break;
5515 case 2: how = SEEK_END; break;
5516 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005517#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005518
5519#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005520 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005521#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005522 pos = PyLong_Check(posobj) ?
5523 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005524#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005525 if (PyErr_Occurred())
5526 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005527
Victor Stinner8c62be82010-05-06 00:08:46 +00005528 if (!_PyVerify_fd(fd))
5529 return posix_error();
5530 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005531#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005532 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005533#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005534 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005535#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 Py_END_ALLOW_THREADS
5537 if (res < 0)
5538 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005539
5540#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005541 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005542#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005543 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005544#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005545}
5546
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005547
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005548PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005549"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005550Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005551
Barry Warsaw53699e91996-12-10 23:23:01 +00005552static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005553posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005554{
Victor Stinner8c62be82010-05-06 00:08:46 +00005555 int fd, size;
5556 Py_ssize_t n;
5557 PyObject *buffer;
5558 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5559 return NULL;
5560 if (size < 0) {
5561 errno = EINVAL;
5562 return posix_error();
5563 }
5564 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5565 if (buffer == NULL)
5566 return NULL;
5567 if (!_PyVerify_fd(fd))
5568 return posix_error();
5569 Py_BEGIN_ALLOW_THREADS
5570 n = read(fd, PyBytes_AS_STRING(buffer), size);
5571 Py_END_ALLOW_THREADS
5572 if (n < 0) {
5573 Py_DECREF(buffer);
5574 return posix_error();
5575 }
5576 if (n != size)
5577 _PyBytes_Resize(&buffer, n);
5578 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005579}
5580
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005582PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005583"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005584Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005585
Barry Warsaw53699e91996-12-10 23:23:01 +00005586static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005587posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005588{
Victor Stinner8c62be82010-05-06 00:08:46 +00005589 Py_buffer pbuf;
5590 int fd;
5591 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005592
Victor Stinner8c62be82010-05-06 00:08:46 +00005593 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5594 return NULL;
5595 if (!_PyVerify_fd(fd))
5596 return posix_error();
5597 Py_BEGIN_ALLOW_THREADS
5598 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5599 Py_END_ALLOW_THREADS
5600 PyBuffer_Release(&pbuf);
5601 if (size < 0)
5602 return posix_error();
5603 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005604}
5605
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005607PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005608"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005609Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005610
Barry Warsaw53699e91996-12-10 23:23:01 +00005611static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005612posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005613{
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 int fd;
5615 STRUCT_STAT st;
5616 int res;
5617 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5618 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005619#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005620 /* on OpenVMS we must ensure that all bytes are written to the file */
5621 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005622#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005623 if (!_PyVerify_fd(fd))
5624 return posix_error();
5625 Py_BEGIN_ALLOW_THREADS
5626 res = FSTAT(fd, &st);
5627 Py_END_ALLOW_THREADS
5628 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005629#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005630 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005631#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005633#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005634 }
Tim Peters5aa91602002-01-30 05:46:57 +00005635
Victor Stinner8c62be82010-05-06 00:08:46 +00005636 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005637}
5638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005639PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005640"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005641Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005642connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005643
5644static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005645posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005646{
Victor Stinner8c62be82010-05-06 00:08:46 +00005647 int fd;
5648 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5649 return NULL;
5650 if (!_PyVerify_fd(fd))
5651 return PyBool_FromLong(0);
5652 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005653}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005654
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005655#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005656PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005657"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005658Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005659
Barry Warsaw53699e91996-12-10 23:23:01 +00005660static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005661posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005662{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005663#if defined(PYOS_OS2)
5664 HFILE read, write;
5665 APIRET rc;
5666
Victor Stinner8c62be82010-05-06 00:08:46 +00005667 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005668 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005669 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005670 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005671 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005672
5673 return Py_BuildValue("(ii)", read, write);
5674#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005675#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005676 int fds[2];
5677 int res;
5678 Py_BEGIN_ALLOW_THREADS
5679 res = pipe(fds);
5680 Py_END_ALLOW_THREADS
5681 if (res != 0)
5682 return posix_error();
5683 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005684#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005685 HANDLE read, write;
5686 int read_fd, write_fd;
5687 BOOL ok;
5688 Py_BEGIN_ALLOW_THREADS
5689 ok = CreatePipe(&read, &write, NULL, 0);
5690 Py_END_ALLOW_THREADS
5691 if (!ok)
5692 return win32_error("CreatePipe", NULL);
5693 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5694 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5695 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005696#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005697#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005698}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005699#endif /* HAVE_PIPE */
5700
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005701
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005702#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005703PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005704"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005705Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005706
Barry Warsaw53699e91996-12-10 23:23:01 +00005707static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005708posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005709{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005710 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005711 char *filename;
5712 int mode = 0666;
5713 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005714 if (!PyArg_ParseTuple(args, "O&|i:mkfifo", PyUnicode_FSConverter, &opath,
5715 &mode))
Victor Stinner8c62be82010-05-06 00:08:46 +00005716 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005717 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005718 Py_BEGIN_ALLOW_THREADS
5719 res = mkfifo(filename, mode);
5720 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005721 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005722 if (res < 0)
5723 return posix_error();
5724 Py_INCREF(Py_None);
5725 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005726}
5727#endif
5728
5729
Neal Norwitz11690112002-07-30 01:08:28 +00005730#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005731PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005732"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005733Create a filesystem node (file, device special file or named pipe)\n\
5734named filename. mode specifies both the permissions to use and the\n\
5735type of node to be created, being combined (bitwise OR) with one of\n\
5736S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005737device defines the newly created device special file (probably using\n\
5738os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005739
5740
5741static PyObject *
5742posix_mknod(PyObject *self, PyObject *args)
5743{
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005744 PyObject *opath;
Victor Stinner8c62be82010-05-06 00:08:46 +00005745 char *filename;
5746 int mode = 0600;
5747 int device = 0;
5748 int res;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005749 if (!PyArg_ParseTuple(args, "O&|ii:mknod", PyUnicode_FSConverter, &opath,
5750 &mode, &device))
Victor Stinner8c62be82010-05-06 00:08:46 +00005751 return NULL;
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005752 filename = PyBytes_AS_STRING(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 Py_BEGIN_ALLOW_THREADS
5754 res = mknod(filename, mode, device);
5755 Py_END_ALLOW_THREADS
Benjamin Petersond4efbf92010-08-11 19:20:42 +00005756 Py_DECREF(opath);
Victor Stinner8c62be82010-05-06 00:08:46 +00005757 if (res < 0)
5758 return posix_error();
5759 Py_INCREF(Py_None);
5760 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005761}
5762#endif
5763
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005764#ifdef HAVE_DEVICE_MACROS
5765PyDoc_STRVAR(posix_major__doc__,
5766"major(device) -> major number\n\
5767Extracts a device major number from a raw device number.");
5768
5769static PyObject *
5770posix_major(PyObject *self, PyObject *args)
5771{
Victor Stinner8c62be82010-05-06 00:08:46 +00005772 int device;
5773 if (!PyArg_ParseTuple(args, "i:major", &device))
5774 return NULL;
5775 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005776}
5777
5778PyDoc_STRVAR(posix_minor__doc__,
5779"minor(device) -> minor number\n\
5780Extracts a device minor number from a raw device number.");
5781
5782static PyObject *
5783posix_minor(PyObject *self, PyObject *args)
5784{
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 int device;
5786 if (!PyArg_ParseTuple(args, "i:minor", &device))
5787 return NULL;
5788 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005789}
5790
5791PyDoc_STRVAR(posix_makedev__doc__,
5792"makedev(major, minor) -> device number\n\
5793Composes a raw device number from the major and minor device numbers.");
5794
5795static PyObject *
5796posix_makedev(PyObject *self, PyObject *args)
5797{
Victor Stinner8c62be82010-05-06 00:08:46 +00005798 int major, minor;
5799 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5800 return NULL;
5801 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005802}
5803#endif /* device macros */
5804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005805
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005806#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005807PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005808"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005809Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005810
Barry Warsaw53699e91996-12-10 23:23:01 +00005811static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005812posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005813{
Victor Stinner8c62be82010-05-06 00:08:46 +00005814 int fd;
5815 off_t length;
5816 int res;
5817 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005818
Victor Stinner8c62be82010-05-06 00:08:46 +00005819 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5820 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005821
5822#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005823 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005824#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005825 length = PyLong_Check(lenobj) ?
5826 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005827#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 if (PyErr_Occurred())
5829 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005830
Victor Stinner8c62be82010-05-06 00:08:46 +00005831 Py_BEGIN_ALLOW_THREADS
5832 res = ftruncate(fd, length);
5833 Py_END_ALLOW_THREADS
5834 if (res < 0)
5835 return posix_error();
5836 Py_INCREF(Py_None);
5837 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005838}
5839#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005840
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005841#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005842PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005843"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005844Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005845
Fred Drake762e2061999-08-26 17:23:54 +00005846/* Save putenv() parameters as values here, so we can collect them when they
5847 * get re-set with another call for the same key. */
5848static PyObject *posix_putenv_garbage;
5849
Tim Peters5aa91602002-01-30 05:46:57 +00005850static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005851posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005852{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005853#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005854 wchar_t *s1, *s2;
5855 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005856#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005857 PyObject *os1, *os2;
5858 char *s1, *s2;
5859 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005860#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005861 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005862 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005863
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005864#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 if (!PyArg_ParseTuple(args,
5866 "uu:putenv",
5867 &s1, &s2))
5868 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005869#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005870 if (!PyArg_ParseTuple(args,
5871 "O&O&:putenv",
5872 PyUnicode_FSConverter, &os1,
5873 PyUnicode_FSConverter, &os2))
5874 return NULL;
5875 s1 = PyBytes_AsString(os1);
5876 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005877#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005878
5879#if defined(PYOS_OS2)
5880 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5881 APIRET rc;
5882
Guido van Rossumd48f2521997-12-05 22:19:34 +00005883 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005884 if (rc != NO_ERROR) {
5885 os2_error(rc);
5886 goto error;
5887 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005888
5889 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5890 APIRET rc;
5891
Guido van Rossumd48f2521997-12-05 22:19:34 +00005892 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005893 if (rc != NO_ERROR) {
5894 os2_error(rc);
5895 goto error;
5896 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005897 } else {
5898#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005899 /* XXX This can leak memory -- not easy to fix :-( */
5900 /* len includes space for a trailing \0; the size arg to
5901 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005902#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005903 len = wcslen(s1) + wcslen(s2) + 2;
5904 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005905#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005906 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005907 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005908#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005909 if (newstr == NULL) {
5910 PyErr_NoMemory();
5911 goto error;
5912 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005913#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 newenv = PyUnicode_AsUnicode(newstr);
5915 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5916 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005917 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005918 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005919 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005920#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 newenv = PyBytes_AS_STRING(newstr);
5922 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5923 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005924 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005925 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005927#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005928
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 /* Install the first arg and newstr in posix_putenv_garbage;
5930 * this will cause previous value to be collected. This has to
5931 * happen after the real putenv() call because the old value
5932 * was still accessible until then. */
5933 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005934#ifdef MS_WINDOWS
5935 PyTuple_GET_ITEM(args, 0),
5936#else
5937 os1,
5938#endif
5939 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 /* really not much we can do; just leak */
5941 PyErr_Clear();
5942 }
5943 else {
5944 Py_DECREF(newstr);
5945 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005946
5947#if defined(PYOS_OS2)
5948 }
5949#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005950
Martin v. Löwis011e8422009-05-05 04:43:17 +00005951#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005952 Py_DECREF(os1);
5953 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005954#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005955 Py_RETURN_NONE;
5956
5957error:
5958#ifndef MS_WINDOWS
5959 Py_DECREF(os1);
5960 Py_DECREF(os2);
5961#endif
5962 Py_XDECREF(newstr);
5963 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005964}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005965#endif /* putenv */
5966
Guido van Rossumc524d952001-10-19 01:31:59 +00005967#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005968PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005969"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005970Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005971
5972static PyObject *
5973posix_unsetenv(PyObject *self, PyObject *args)
5974{
Victor Stinner84ae1182010-05-06 22:05:07 +00005975#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005976 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005977
Victor Stinner8c62be82010-05-06 00:08:46 +00005978 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5979 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00005980#else
5981 PyObject *os1;
5982 char *s1;
5983
5984 if (!PyArg_ParseTuple(args, "O&:unsetenv",
5985 PyUnicode_FSConverter, &os1))
5986 return NULL;
5987 s1 = PyBytes_AsString(os1);
5988#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00005989
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00005991
Victor Stinner8c62be82010-05-06 00:08:46 +00005992 /* Remove the key from posix_putenv_garbage;
5993 * this will cause it to be collected. This has to
5994 * happen after the real unsetenv() call because the
5995 * old value was still accessible until then.
5996 */
5997 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005998#ifdef MS_WINDOWS
5999 PyTuple_GET_ITEM(args, 0)
6000#else
6001 os1
6002#endif
6003 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 /* really not much we can do; just leak */
6005 PyErr_Clear();
6006 }
Guido van Rossumc524d952001-10-19 01:31:59 +00006007
Victor Stinner84ae1182010-05-06 22:05:07 +00006008#ifndef MS_WINDOWS
6009 Py_DECREF(os1);
6010#endif
6011 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00006012}
6013#endif /* unsetenv */
6014
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006016"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006017Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00006018
Guido van Rossumf68d8e52001-04-14 17:55:09 +00006019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006020posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00006021{
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 int code;
6023 char *message;
6024 if (!PyArg_ParseTuple(args, "i:strerror", &code))
6025 return NULL;
6026 message = strerror(code);
6027 if (message == NULL) {
6028 PyErr_SetString(PyExc_ValueError,
6029 "strerror() argument out of range");
6030 return NULL;
6031 }
6032 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006033}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006034
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006035
Guido van Rossumc9641791998-08-04 15:26:23 +00006036#ifdef HAVE_SYS_WAIT_H
6037
Fred Drake106c1a02002-04-23 15:58:02 +00006038#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006039PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006040"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006041Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006042
6043static PyObject *
6044posix_WCOREDUMP(PyObject *self, PyObject *args)
6045{
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 WAIT_TYPE status;
6047 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006048
Victor Stinner8c62be82010-05-06 00:08:46 +00006049 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6050 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006051
Victor Stinner8c62be82010-05-06 00:08:46 +00006052 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006053}
6054#endif /* WCOREDUMP */
6055
6056#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006057PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006058"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006059Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006060job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006061
6062static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006063posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006064{
Victor Stinner8c62be82010-05-06 00:08:46 +00006065 WAIT_TYPE status;
6066 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006067
Victor Stinner8c62be82010-05-06 00:08:46 +00006068 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6069 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006070
Victor Stinner8c62be82010-05-06 00:08:46 +00006071 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006072}
6073#endif /* WIFCONTINUED */
6074
Guido van Rossumc9641791998-08-04 15:26:23 +00006075#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006076PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006077"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006078Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006079
6080static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006081posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006082{
Victor Stinner8c62be82010-05-06 00:08:46 +00006083 WAIT_TYPE status;
6084 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006085
Victor Stinner8c62be82010-05-06 00:08:46 +00006086 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6087 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006088
Victor Stinner8c62be82010-05-06 00:08:46 +00006089 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006090}
6091#endif /* WIFSTOPPED */
6092
6093#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006094PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006095"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006096Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006097
6098static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006099posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006100{
Victor Stinner8c62be82010-05-06 00:08:46 +00006101 WAIT_TYPE status;
6102 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006103
Victor Stinner8c62be82010-05-06 00:08:46 +00006104 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6105 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006106
Victor Stinner8c62be82010-05-06 00:08:46 +00006107 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006108}
6109#endif /* WIFSIGNALED */
6110
6111#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006112PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006113"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006114Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006115system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006116
6117static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006118posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006119{
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 WAIT_TYPE status;
6121 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006122
Victor Stinner8c62be82010-05-06 00:08:46 +00006123 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6124 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006125
Victor Stinner8c62be82010-05-06 00:08:46 +00006126 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006127}
6128#endif /* WIFEXITED */
6129
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006130#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006131PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006132"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006134
6135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006136posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006137{
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 WAIT_TYPE status;
6139 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006140
Victor Stinner8c62be82010-05-06 00:08:46 +00006141 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6142 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006143
Victor Stinner8c62be82010-05-06 00:08:46 +00006144 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006145}
6146#endif /* WEXITSTATUS */
6147
6148#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006149PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006150"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006151Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006152value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006153
6154static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006155posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006156{
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 WAIT_TYPE status;
6158 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006159
Victor Stinner8c62be82010-05-06 00:08:46 +00006160 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6161 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006162
Victor Stinner8c62be82010-05-06 00:08:46 +00006163 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006164}
6165#endif /* WTERMSIG */
6166
6167#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006168PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006169"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006170Return the signal that stopped the process that provided\n\
6171the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006172
6173static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006174posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006175{
Victor Stinner8c62be82010-05-06 00:08:46 +00006176 WAIT_TYPE status;
6177 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006178
Victor Stinner8c62be82010-05-06 00:08:46 +00006179 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6180 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006181
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006183}
6184#endif /* WSTOPSIG */
6185
6186#endif /* HAVE_SYS_WAIT_H */
6187
6188
Thomas Wouters477c8d52006-05-27 19:21:47 +00006189#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006190#ifdef _SCO_DS
6191/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6192 needed definitions in sys/statvfs.h */
6193#define _SVID3
6194#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006195#include <sys/statvfs.h>
6196
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006197static PyObject*
6198_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006199 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6200 if (v == NULL)
6201 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006202
6203#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6205 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6206 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6207 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6208 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6209 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6210 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6211 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6212 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6213 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006214#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006215 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6216 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6217 PyStructSequence_SET_ITEM(v, 2,
6218 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6219 PyStructSequence_SET_ITEM(v, 3,
6220 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6221 PyStructSequence_SET_ITEM(v, 4,
6222 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6223 PyStructSequence_SET_ITEM(v, 5,
6224 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6225 PyStructSequence_SET_ITEM(v, 6,
6226 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6227 PyStructSequence_SET_ITEM(v, 7,
6228 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6229 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6230 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006231#endif
6232
Victor Stinner8c62be82010-05-06 00:08:46 +00006233 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006234}
6235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006237"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006239
6240static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006241posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006242{
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 int fd, res;
6244 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006245
Victor Stinner8c62be82010-05-06 00:08:46 +00006246 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6247 return NULL;
6248 Py_BEGIN_ALLOW_THREADS
6249 res = fstatvfs(fd, &st);
6250 Py_END_ALLOW_THREADS
6251 if (res != 0)
6252 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006253
Victor Stinner8c62be82010-05-06 00:08:46 +00006254 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006255}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006256#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006257
6258
Thomas Wouters477c8d52006-05-27 19:21:47 +00006259#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006260#include <sys/statvfs.h>
6261
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006262PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006263"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006264Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006265
6266static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006267posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006268{
Victor Stinner8c62be82010-05-06 00:08:46 +00006269 char *path;
6270 int res;
6271 struct statvfs st;
6272 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6273 return NULL;
6274 Py_BEGIN_ALLOW_THREADS
6275 res = statvfs(path, &st);
6276 Py_END_ALLOW_THREADS
6277 if (res != 0)
6278 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006279
Victor Stinner8c62be82010-05-06 00:08:46 +00006280 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006281}
6282#endif /* HAVE_STATVFS */
6283
Fred Drakec9680921999-12-13 16:37:25 +00006284/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6285 * It maps strings representing configuration variable names to
6286 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006287 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006288 * rarely-used constants. There are three separate tables that use
6289 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006290 *
6291 * This code is always included, even if none of the interfaces that
6292 * need it are included. The #if hackery needed to avoid it would be
6293 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006294 */
6295struct constdef {
6296 char *name;
6297 long value;
6298};
6299
Fred Drake12c6e2d1999-12-14 21:25:03 +00006300static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006301conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006302 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006303{
Christian Heimes217cfd12007-12-02 14:31:20 +00006304 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 *valuep = PyLong_AS_LONG(arg);
6306 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006307 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006308 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006309 /* look up the value in the table using a binary search */
6310 size_t lo = 0;
6311 size_t mid;
6312 size_t hi = tablesize;
6313 int cmp;
6314 const char *confname;
6315 if (!PyUnicode_Check(arg)) {
6316 PyErr_SetString(PyExc_TypeError,
6317 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00006318 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006319 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006320 confname = _PyUnicode_AsString(arg);
6321 if (confname == NULL)
6322 return 0;
6323 while (lo < hi) {
6324 mid = (lo + hi) / 2;
6325 cmp = strcmp(confname, table[mid].name);
6326 if (cmp < 0)
6327 hi = mid;
6328 else if (cmp > 0)
6329 lo = mid + 1;
6330 else {
6331 *valuep = table[mid].value;
6332 return 1;
6333 }
6334 }
6335 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6336 return 0;
6337 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006338}
6339
6340
6341#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6342static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006343#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006345#endif
6346#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006347 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006348#endif
Fred Drakec9680921999-12-13 16:37:25 +00006349#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006350 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006351#endif
6352#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006353 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006354#endif
6355#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006356 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006357#endif
6358#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006359 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006360#endif
6361#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006362 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006363#endif
6364#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006365 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006366#endif
6367#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006368 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006369#endif
6370#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006371 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006372#endif
6373#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006375#endif
6376#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006377 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006378#endif
6379#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006380 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006381#endif
6382#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006383 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006384#endif
6385#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006386 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006387#endif
6388#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006390#endif
6391#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006392 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006393#endif
6394};
6395
Fred Drakec9680921999-12-13 16:37:25 +00006396static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006397conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006398{
6399 return conv_confname(arg, valuep, posix_constants_pathconf,
6400 sizeof(posix_constants_pathconf)
6401 / sizeof(struct constdef));
6402}
6403#endif
6404
6405#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006406PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006407"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006408Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006409If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006410
6411static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006412posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006413{
6414 PyObject *result = NULL;
6415 int name, fd;
6416
Fred Drake12c6e2d1999-12-14 21:25:03 +00006417 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6418 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006419 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006420
Victor Stinner8c62be82010-05-06 00:08:46 +00006421 errno = 0;
6422 limit = fpathconf(fd, name);
6423 if (limit == -1 && errno != 0)
6424 posix_error();
6425 else
6426 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006427 }
6428 return result;
6429}
6430#endif
6431
6432
6433#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006434PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006435"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006436Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006437If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006438
6439static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006440posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006441{
6442 PyObject *result = NULL;
6443 int name;
6444 char *path;
6445
6446 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6447 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006448 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006449
Victor Stinner8c62be82010-05-06 00:08:46 +00006450 errno = 0;
6451 limit = pathconf(path, name);
6452 if (limit == -1 && errno != 0) {
6453 if (errno == EINVAL)
6454 /* could be a path or name problem */
6455 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006456 else
Victor Stinner8c62be82010-05-06 00:08:46 +00006457 posix_error_with_filename(path);
6458 }
6459 else
6460 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006461 }
6462 return result;
6463}
6464#endif
6465
6466#ifdef HAVE_CONFSTR
6467static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006468#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006469 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006470#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006471#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006473#endif
6474#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006475 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006476#endif
Fred Draked86ed291999-12-15 15:34:33 +00006477#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006479#endif
6480#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006482#endif
6483#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006484 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006485#endif
6486#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006488#endif
Fred Drakec9680921999-12-13 16:37:25 +00006489#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006490 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006491#endif
6492#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006493 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006494#endif
6495#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006496 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006497#endif
6498#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006499 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006500#endif
6501#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006502 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006503#endif
6504#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006505 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006506#endif
6507#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006508 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006509#endif
6510#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006511 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006512#endif
Fred Draked86ed291999-12-15 15:34:33 +00006513#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006514 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006515#endif
Fred Drakec9680921999-12-13 16:37:25 +00006516#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006517 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006518#endif
Fred Draked86ed291999-12-15 15:34:33 +00006519#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006520 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006521#endif
6522#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006523 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006524#endif
6525#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006526 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006527#endif
6528#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006529 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006530#endif
Fred Drakec9680921999-12-13 16:37:25 +00006531#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006532 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006533#endif
6534#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006535 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006536#endif
6537#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006539#endif
6540#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006541 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006542#endif
6543#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006544 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006545#endif
6546#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006547 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006548#endif
6549#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006550 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006551#endif
6552#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006553 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006554#endif
6555#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006556 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006557#endif
6558#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006559 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006560#endif
6561#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006562 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006563#endif
6564#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006565 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006566#endif
6567#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006568 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006569#endif
6570#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006571 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006572#endif
6573#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006574 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006575#endif
6576#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006578#endif
Fred Draked86ed291999-12-15 15:34:33 +00006579#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006580 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006581#endif
6582#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006583 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006584#endif
6585#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006586 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006587#endif
6588#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006589 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006590#endif
6591#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006592 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006593#endif
6594#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006595 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006596#endif
6597#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006598 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006599#endif
6600#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006601 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006602#endif
6603#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006605#endif
6606#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006607 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006608#endif
6609#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006611#endif
6612#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006614#endif
6615#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006617#endif
Fred Drakec9680921999-12-13 16:37:25 +00006618};
6619
6620static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006621conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006622{
6623 return conv_confname(arg, valuep, posix_constants_confstr,
6624 sizeof(posix_constants_confstr)
6625 / sizeof(struct constdef));
6626}
6627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006628PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006629"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006630Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006631
6632static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006633posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006634{
6635 PyObject *result = NULL;
6636 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006637 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006638
6639 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006641
Fred Drakec9680921999-12-13 16:37:25 +00006642 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 len = confstr(name, buffer, sizeof(buffer));
6644 if (len == 0) {
6645 if (errno) {
6646 posix_error();
6647 }
6648 else {
6649 result = Py_None;
6650 Py_INCREF(Py_None);
6651 }
Fred Drakec9680921999-12-13 16:37:25 +00006652 }
6653 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006654 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006655 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006656 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006657 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006658 }
6659 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006660 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006661 }
6662 }
6663 return result;
6664}
6665#endif
6666
6667
6668#ifdef HAVE_SYSCONF
6669static struct constdef posix_constants_sysconf[] = {
6670#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006671 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006672#endif
6673#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006674 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006675#endif
6676#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006677 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006678#endif
6679#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006680 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006681#endif
6682#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006684#endif
6685#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006686 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006687#endif
6688#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006689 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006690#endif
6691#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006692 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006693#endif
6694#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006695 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006696#endif
6697#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006698 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006699#endif
Fred Draked86ed291999-12-15 15:34:33 +00006700#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006701 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006702#endif
6703#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006705#endif
Fred Drakec9680921999-12-13 16:37:25 +00006706#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006707 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006708#endif
Fred Drakec9680921999-12-13 16:37:25 +00006709#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006710 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006711#endif
6712#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006713 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006714#endif
6715#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006716 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006717#endif
6718#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006719 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006720#endif
6721#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006722 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006723#endif
Fred Draked86ed291999-12-15 15:34:33 +00006724#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006725 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006726#endif
Fred Drakec9680921999-12-13 16:37:25 +00006727#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006728 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006729#endif
6730#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006731 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006732#endif
6733#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006734 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006735#endif
6736#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006737 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006738#endif
6739#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006740 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006741#endif
Fred Draked86ed291999-12-15 15:34:33 +00006742#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006743 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006744#endif
Fred Drakec9680921999-12-13 16:37:25 +00006745#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006746 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006747#endif
6748#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006749 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006750#endif
6751#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006752 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006753#endif
6754#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006755 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006756#endif
6757#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006758 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006759#endif
6760#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006761 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006762#endif
6763#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006764 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006765#endif
6766#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006767 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006768#endif
6769#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006770 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006771#endif
6772#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006774#endif
6775#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006776 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006777#endif
6778#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006779 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006780#endif
6781#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006782 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006783#endif
6784#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006785 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006786#endif
6787#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006789#endif
6790#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006791 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006792#endif
6793#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006794 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006795#endif
6796#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006797 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006798#endif
6799#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006801#endif
6802#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006803 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006804#endif
6805#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006807#endif
6808#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006809 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006810#endif
6811#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006812 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006813#endif
Fred Draked86ed291999-12-15 15:34:33 +00006814#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006815 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006816#endif
Fred Drakec9680921999-12-13 16:37:25 +00006817#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006818 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006819#endif
6820#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006821 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006822#endif
6823#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006824 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006825#endif
Fred Draked86ed291999-12-15 15:34:33 +00006826#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006827 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006828#endif
Fred Drakec9680921999-12-13 16:37:25 +00006829#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006830 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006831#endif
Fred Draked86ed291999-12-15 15:34:33 +00006832#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006833 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006834#endif
6835#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006836 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006837#endif
Fred Drakec9680921999-12-13 16:37:25 +00006838#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006839 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006840#endif
6841#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006842 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006843#endif
6844#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006845 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006846#endif
6847#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006848 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006849#endif
Fred Draked86ed291999-12-15 15:34:33 +00006850#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006851 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006852#endif
Fred Drakec9680921999-12-13 16:37:25 +00006853#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006854 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006855#endif
6856#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006857 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006858#endif
6859#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006860 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006861#endif
6862#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006863 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006864#endif
6865#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006866 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006867#endif
6868#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006869 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006870#endif
6871#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006872 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006873#endif
Fred Draked86ed291999-12-15 15:34:33 +00006874#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006875 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006876#endif
Fred Drakec9680921999-12-13 16:37:25 +00006877#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006878 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006879#endif
6880#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006881 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006882#endif
Fred Draked86ed291999-12-15 15:34:33 +00006883#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006884 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006885#endif
Fred Drakec9680921999-12-13 16:37:25 +00006886#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006887 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006888#endif
6889#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006890 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006891#endif
6892#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006893 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006894#endif
6895#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006896 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006897#endif
6898#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006899 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006900#endif
6901#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006902 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006903#endif
6904#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006905 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006906#endif
6907#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006908 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006909#endif
6910#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006911 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006912#endif
Fred Draked86ed291999-12-15 15:34:33 +00006913#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006914 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006915#endif
6916#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006917 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006918#endif
Fred Drakec9680921999-12-13 16:37:25 +00006919#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006920 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006921#endif
6922#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006923 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006924#endif
6925#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006926 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006927#endif
6928#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006929 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006930#endif
6931#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006932 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006933#endif
6934#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006935 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006936#endif
6937#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006938 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006939#endif
6940#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006941 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006942#endif
6943#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006944 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006945#endif
6946#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006947 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006948#endif
6949#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006950 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006951#endif
6952#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006953 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006954#endif
6955#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006956 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006957#endif
6958#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006959 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006960#endif
6961#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006962 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006963#endif
6964#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006965 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006966#endif
6967#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006968 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006969#endif
6970#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006971 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006972#endif
6973#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006974 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006975#endif
6976#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006977 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006978#endif
6979#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006980 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006981#endif
6982#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006983 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006984#endif
6985#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006986 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006987#endif
6988#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006989 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006990#endif
6991#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006992 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006993#endif
6994#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006995 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006996#endif
6997#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00006998 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006999#endif
7000#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007001 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007002#endif
7003#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007004 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007005#endif
7006#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00007007 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00007008#endif
7009#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007010 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007011#endif
7012#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007013 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007014#endif
7015#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007016 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007017#endif
7018#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007019 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007020#endif
7021#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007022 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007023#endif
Fred Draked86ed291999-12-15 15:34:33 +00007024#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007026#endif
Fred Drakec9680921999-12-13 16:37:25 +00007027#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007028 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007029#endif
7030#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007031 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007032#endif
7033#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007034 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007035#endif
7036#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007037 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007038#endif
7039#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007040 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007041#endif
7042#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007044#endif
7045#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007046 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007047#endif
7048#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007049 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007050#endif
7051#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007052 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007053#endif
7054#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007055 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007056#endif
7057#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007059#endif
7060#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007061 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007062#endif
7063#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007064 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007065#endif
7066#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007067 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007068#endif
7069#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007070 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007071#endif
7072#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007073 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007074#endif
7075#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007076 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007077#endif
7078#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007079 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007080#endif
7081#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007082 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007083#endif
7084#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007085 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007086#endif
7087#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007088 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007089#endif
7090#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007091 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007092#endif
7093#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007095#endif
7096#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007097 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007098#endif
7099#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007100 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007101#endif
7102#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007103 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007104#endif
7105#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007106 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007107#endif
7108#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007109 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007110#endif
7111#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007113#endif
7114#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007116#endif
7117#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007118 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007119#endif
7120#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007121 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007122#endif
7123#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007124 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007125#endif
7126#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007127 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007128#endif
7129#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007130 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007131#endif
7132#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007133 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007134#endif
7135#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007136 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007137#endif
7138#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007139 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007140#endif
7141#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007142 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007143#endif
7144#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007145 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007146#endif
7147#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007148 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007149#endif
7150#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007151 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007152#endif
7153#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007154 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007155#endif
7156#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007157 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007158#endif
7159#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007161#endif
7162};
7163
7164static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007165conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007166{
7167 return conv_confname(arg, valuep, posix_constants_sysconf,
7168 sizeof(posix_constants_sysconf)
7169 / sizeof(struct constdef));
7170}
7171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007172PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007173"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007174Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007175
7176static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007177posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007178{
7179 PyObject *result = NULL;
7180 int name;
7181
7182 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7183 int value;
7184
7185 errno = 0;
7186 value = sysconf(name);
7187 if (value == -1 && errno != 0)
7188 posix_error();
7189 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007190 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007191 }
7192 return result;
7193}
7194#endif
7195
7196
Fred Drakebec628d1999-12-15 18:31:10 +00007197/* This code is used to ensure that the tables of configuration value names
7198 * are in sorted order as required by conv_confname(), and also to build the
7199 * the exported dictionaries that are used to publish information about the
7200 * names available on the host platform.
7201 *
7202 * Sorting the table at runtime ensures that the table is properly ordered
7203 * when used, even for platforms we're not able to test on. It also makes
7204 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007205 */
Fred Drakebec628d1999-12-15 18:31:10 +00007206
7207static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007208cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007209{
7210 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007211 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007212 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007214
7215 return strcmp(c1->name, c2->name);
7216}
7217
7218static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007219setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007221{
Fred Drakebec628d1999-12-15 18:31:10 +00007222 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007223 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007224
7225 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7226 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007227 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007228 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007229
Barry Warsaw3155db32000-04-13 15:20:40 +00007230 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 PyObject *o = PyLong_FromLong(table[i].value);
7232 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7233 Py_XDECREF(o);
7234 Py_DECREF(d);
7235 return -1;
7236 }
7237 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007238 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007239 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007240}
7241
Fred Drakebec628d1999-12-15 18:31:10 +00007242/* Return -1 on failure, 0 on success. */
7243static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007244setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007245{
7246#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007247 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007248 sizeof(posix_constants_pathconf)
7249 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007250 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007252#endif
7253#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007254 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007255 sizeof(posix_constants_confstr)
7256 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007257 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007259#endif
7260#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007261 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007262 sizeof(posix_constants_sysconf)
7263 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007264 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007266#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007267 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007268}
Fred Draked86ed291999-12-15 15:34:33 +00007269
7270
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007271PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007272"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007273Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007274in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007275
7276static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007277posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007278{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007279 abort();
7280 /*NOTREACHED*/
7281 Py_FatalError("abort() called from Python code didn't abort!");
7282 return NULL;
7283}
Fred Drakebec628d1999-12-15 18:31:10 +00007284
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007285#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007286PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007287"startfile(filepath [, operation]) - Start a file with its associated\n\
7288application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007289\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007290When \"operation\" is not specified or \"open\", this acts like\n\
7291double-clicking the file in Explorer, or giving the file name as an\n\
7292argument to the DOS \"start\" command: the file is opened with whatever\n\
7293application (if any) its extension is associated.\n\
7294When another \"operation\" is given, it specifies what should be done with\n\
7295the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007296\n\
7297startfile returns as soon as the associated application is launched.\n\
7298There is no option to wait for the application to close, and no way\n\
7299to retrieve the application's exit status.\n\
7300\n\
7301The filepath is relative to the current directory. If you want to use\n\
7302an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007303the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007304
7305static PyObject *
7306win32_startfile(PyObject *self, PyObject *args)
7307{
Victor Stinner8c62be82010-05-06 00:08:46 +00007308 PyObject *ofilepath;
7309 char *filepath;
7310 char *operation = NULL;
7311 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007312
Victor Stinner8c62be82010-05-06 00:08:46 +00007313 PyObject *unipath, *woperation = NULL;
7314 if (!PyArg_ParseTuple(args, "U|s:startfile",
7315 &unipath, &operation)) {
7316 PyErr_Clear();
7317 goto normal;
7318 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007319
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 if (operation) {
7321 woperation = PyUnicode_DecodeASCII(operation,
7322 strlen(operation), NULL);
7323 if (!woperation) {
7324 PyErr_Clear();
7325 operation = NULL;
7326 goto normal;
7327 }
7328 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007329
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 Py_BEGIN_ALLOW_THREADS
7331 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7332 PyUnicode_AS_UNICODE(unipath),
7333 NULL, NULL, SW_SHOWNORMAL);
7334 Py_END_ALLOW_THREADS
7335
7336 Py_XDECREF(woperation);
7337 if (rc <= (HINSTANCE)32) {
7338 PyObject *errval = win32_error_unicode("startfile",
7339 PyUnicode_AS_UNICODE(unipath));
7340 return errval;
7341 }
7342 Py_INCREF(Py_None);
7343 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007344
7345normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007346 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7347 PyUnicode_FSConverter, &ofilepath,
7348 &operation))
7349 return NULL;
7350 filepath = PyBytes_AsString(ofilepath);
7351 Py_BEGIN_ALLOW_THREADS
7352 rc = ShellExecute((HWND)0, operation, filepath,
7353 NULL, NULL, SW_SHOWNORMAL);
7354 Py_END_ALLOW_THREADS
7355 if (rc <= (HINSTANCE)32) {
7356 PyObject *errval = win32_error("startfile", filepath);
7357 Py_DECREF(ofilepath);
7358 return errval;
7359 }
7360 Py_DECREF(ofilepath);
7361 Py_INCREF(Py_None);
7362 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007363}
7364#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007365
Martin v. Löwis438b5342002-12-27 10:16:42 +00007366#ifdef HAVE_GETLOADAVG
7367PyDoc_STRVAR(posix_getloadavg__doc__,
7368"getloadavg() -> (float, float, float)\n\n\
7369Return the number of processes in the system run queue averaged over\n\
7370the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7371was unobtainable");
7372
7373static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007374posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007375{
7376 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007377 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7379 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007380 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007382}
7383#endif
7384
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007385#ifdef MS_WINDOWS
7386
7387PyDoc_STRVAR(win32_urandom__doc__,
7388"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007389Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007390
7391typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7392 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7393 DWORD dwFlags );
7394typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7395 BYTE *pbBuffer );
7396
7397static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007398/* This handle is never explicitly released. Instead, the operating
7399 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007400static HCRYPTPROV hCryptProv = 0;
7401
Tim Peters4ad82172004-08-30 17:02:04 +00007402static PyObject*
7403win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007404{
Victor Stinner8c62be82010-05-06 00:08:46 +00007405 int howMany;
7406 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007407
Victor Stinner8c62be82010-05-06 00:08:46 +00007408 /* Read arguments */
7409 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7410 return NULL;
7411 if (howMany < 0)
7412 return PyErr_Format(PyExc_ValueError,
7413 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007414
Victor Stinner8c62be82010-05-06 00:08:46 +00007415 if (hCryptProv == 0) {
7416 HINSTANCE hAdvAPI32 = NULL;
7417 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007418
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 /* Obtain handle to the DLL containing CryptoAPI
7420 This should not fail */
7421 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7422 if(hAdvAPI32 == NULL)
7423 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007424
Victor Stinner8c62be82010-05-06 00:08:46 +00007425 /* Obtain pointers to the CryptoAPI functions
7426 This will fail on some early versions of Win95 */
7427 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7428 hAdvAPI32,
7429 "CryptAcquireContextA");
7430 if (pCryptAcquireContext == NULL)
7431 return PyErr_Format(PyExc_NotImplementedError,
7432 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007433
Victor Stinner8c62be82010-05-06 00:08:46 +00007434 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7435 hAdvAPI32, "CryptGenRandom");
7436 if (pCryptGenRandom == NULL)
7437 return PyErr_Format(PyExc_NotImplementedError,
7438 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007439
Victor Stinner8c62be82010-05-06 00:08:46 +00007440 /* Acquire context */
7441 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7442 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7443 return win32_error("CryptAcquireContext", NULL);
7444 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007445
Victor Stinner8c62be82010-05-06 00:08:46 +00007446 /* Allocate bytes */
7447 result = PyBytes_FromStringAndSize(NULL, howMany);
7448 if (result != NULL) {
7449 /* Get random data */
7450 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7451 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7452 PyBytes_AS_STRING(result))) {
7453 Py_DECREF(result);
7454 return win32_error("CryptGenRandom", NULL);
7455 }
7456 }
7457 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007458}
7459#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007460
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007461PyDoc_STRVAR(device_encoding__doc__,
7462"device_encoding(fd) -> str\n\n\
7463Return a string describing the encoding of the device\n\
7464if the output is a terminal; else return None.");
7465
7466static PyObject *
7467device_encoding(PyObject *self, PyObject *args)
7468{
Victor Stinner8c62be82010-05-06 00:08:46 +00007469 int fd;
7470 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7471 return NULL;
7472 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7473 Py_INCREF(Py_None);
7474 return Py_None;
7475 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007476#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007477 if (fd == 0) {
7478 char buf[100];
7479 sprintf(buf, "cp%d", GetConsoleCP());
7480 return PyUnicode_FromString(buf);
7481 }
7482 if (fd == 1 || fd == 2) {
7483 char buf[100];
7484 sprintf(buf, "cp%d", GetConsoleOutputCP());
7485 return PyUnicode_FromString(buf);
7486 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007487#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007488 {
7489 char *codeset = nl_langinfo(CODESET);
7490 if (codeset != NULL && codeset[0] != 0)
7491 return PyUnicode_FromString(codeset);
7492 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007493#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007494 Py_INCREF(Py_None);
7495 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007496}
7497
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007498#ifdef __VMS
7499/* Use openssl random routine */
7500#include <openssl/rand.h>
7501PyDoc_STRVAR(vms_urandom__doc__,
7502"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007503Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007504
7505static PyObject*
7506vms_urandom(PyObject *self, PyObject *args)
7507{
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 int howMany;
7509 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007510
Victor Stinner8c62be82010-05-06 00:08:46 +00007511 /* Read arguments */
7512 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7513 return NULL;
7514 if (howMany < 0)
7515 return PyErr_Format(PyExc_ValueError,
7516 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007517
Victor Stinner8c62be82010-05-06 00:08:46 +00007518 /* Allocate bytes */
7519 result = PyBytes_FromStringAndSize(NULL, howMany);
7520 if (result != NULL) {
7521 /* Get random data */
7522 if (RAND_pseudo_bytes((unsigned char*)
7523 PyBytes_AS_STRING(result),
7524 howMany) < 0) {
7525 Py_DECREF(result);
7526 return PyErr_Format(PyExc_ValueError,
7527 "RAND_pseudo_bytes");
7528 }
7529 }
7530 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007531}
7532#endif
7533
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007534#ifdef HAVE_SETRESUID
7535PyDoc_STRVAR(posix_setresuid__doc__,
7536"setresuid(ruid, euid, suid)\n\n\
7537Set the current process's real, effective, and saved user ids.");
7538
7539static PyObject*
7540posix_setresuid (PyObject *self, PyObject *args)
7541{
Victor Stinner8c62be82010-05-06 00:08:46 +00007542 /* We assume uid_t is no larger than a long. */
7543 long ruid, euid, suid;
7544 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7545 return NULL;
7546 if (setresuid(ruid, euid, suid) < 0)
7547 return posix_error();
7548 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007549}
7550#endif
7551
7552#ifdef HAVE_SETRESGID
7553PyDoc_STRVAR(posix_setresgid__doc__,
7554"setresgid(rgid, egid, sgid)\n\n\
7555Set the current process's real, effective, and saved group ids.");
7556
7557static PyObject*
7558posix_setresgid (PyObject *self, PyObject *args)
7559{
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 /* We assume uid_t is no larger than a long. */
7561 long rgid, egid, sgid;
7562 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7563 return NULL;
7564 if (setresgid(rgid, egid, sgid) < 0)
7565 return posix_error();
7566 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007567}
7568#endif
7569
7570#ifdef HAVE_GETRESUID
7571PyDoc_STRVAR(posix_getresuid__doc__,
7572"getresuid() -> (ruid, euid, suid)\n\n\
7573Get tuple of the current process's real, effective, and saved user ids.");
7574
7575static PyObject*
7576posix_getresuid (PyObject *self, PyObject *noargs)
7577{
Victor Stinner8c62be82010-05-06 00:08:46 +00007578 uid_t ruid, euid, suid;
7579 long l_ruid, l_euid, l_suid;
7580 if (getresuid(&ruid, &euid, &suid) < 0)
7581 return posix_error();
7582 /* Force the values into long's as we don't know the size of uid_t. */
7583 l_ruid = ruid;
7584 l_euid = euid;
7585 l_suid = suid;
7586 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007587}
7588#endif
7589
7590#ifdef HAVE_GETRESGID
7591PyDoc_STRVAR(posix_getresgid__doc__,
7592"getresgid() -> (rgid, egid, sgid)\n\n\
7593Get tuple of the current process's real, effective, and saved user ids.");
7594
7595static PyObject*
7596posix_getresgid (PyObject *self, PyObject *noargs)
7597{
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 uid_t rgid, egid, sgid;
7599 long l_rgid, l_egid, l_sgid;
7600 if (getresgid(&rgid, &egid, &sgid) < 0)
7601 return posix_error();
7602 /* Force the values into long's as we don't know the size of uid_t. */
7603 l_rgid = rgid;
7604 l_egid = egid;
7605 l_sgid = sgid;
7606 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007607}
7608#endif
7609
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007610static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007611 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007612#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007614#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007615 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007616#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007617 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007618#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007619 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007620#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007621 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007622#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007623#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007624 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007625#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007626#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007628#endif /* HAVE_LCHMOD */
7629#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007631#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007632#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007633 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007634#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007635#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007636 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007637#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007638#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007639 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007640#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007641#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007643#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007644#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007645 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7646 METH_NOARGS, posix_getcwd__doc__},
7647 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7648 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007649#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007650#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007652#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007653 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7654 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7655 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007656#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007657 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007658#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007659#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007661#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007662#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7663 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
7664#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7665 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7666 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7667 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007668 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007669#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007671#endif /* HAVE_SYMLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007672#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin74e45612010-07-09 15:58:59 +00007673 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
7674 win_symlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007675#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007676#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007677 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007678#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007679 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007680#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007682#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007683 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7684 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7685 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007686#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007688#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007689 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007690#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007691 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7692 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007693#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007694#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007695 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7696 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007697#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7699 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007700#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007701#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007702#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007704#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007705#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007706 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007707#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007708#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007709 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007710#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007711#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007712 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007713#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007714#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007716#endif /* HAVE_GETEGID */
7717#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007718 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007719#endif /* HAVE_GETEUID */
7720#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007721 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007722#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007723#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007724 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007725#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007726 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007727#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007728 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007729#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007730#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007732#endif /* HAVE_GETPPID */
7733#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007735#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007736#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007737 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007738#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007739#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007740 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007741#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007742#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007743 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007744#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007745#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007747#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007748#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7750 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007751#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007752#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007753 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007754#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007755#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007756 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007757#endif /* HAVE_SETEUID */
7758#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007759 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007760#endif /* HAVE_SETEGID */
7761#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007762 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007763#endif /* HAVE_SETREUID */
7764#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007765 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007766#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007767#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007768 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007769#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007770#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007772#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007773#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007775#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007776#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007777 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007778#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007779#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007781#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007782#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007784#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007785#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007786 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007787#endif /* HAVE_WAIT3 */
7788#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007789 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007790#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007791#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007792 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007793#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007794#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007796#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007797#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007798 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007799#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007800#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007802#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007803#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007805#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007806#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007807 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007808#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7810 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7811 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7812 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7813 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7814 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7815 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7816 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7817 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7818 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7819 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007820#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007821 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007822#endif
7823#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007824 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007825#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007826#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007828#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007829#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007830 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7831 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7832 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007833#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007834#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007835 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007836#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007837#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007839#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007840#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007842#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007843 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007844#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007846#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007847#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007849#endif
7850#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007852#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007853#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007854#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007856#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007857#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007858 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007859#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007860#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007861 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007862#endif /* WIFSTOPPED */
7863#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007865#endif /* WIFSIGNALED */
7866#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007867 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007868#endif /* WIFEXITED */
7869#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007871#endif /* WEXITSTATUS */
7872#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007874#endif /* WTERMSIG */
7875#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007876 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007877#endif /* WSTOPSIG */
7878#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007879#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007881#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007882#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007884#endif
Fred Drakec9680921999-12-13 16:37:25 +00007885#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007887#endif
7888#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007890#endif
7891#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007893#endif
7894#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007896#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007897 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007898#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00007900 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007901#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007902#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007903 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007904#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007905 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007906 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007907 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007908 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007909 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007910 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007911#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007912 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007913#endif
7914#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007915 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007916#endif
7917#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007919#endif
7920#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007921 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007922#endif
7923
Victor Stinner8c62be82010-05-06 00:08:46 +00007924 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007925};
7926
7927
Barry Warsaw4a342091996-12-19 23:50:02 +00007928static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007929ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007930{
Victor Stinner8c62be82010-05-06 00:08:46 +00007931 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007932}
7933
Guido van Rossumd48f2521997-12-05 22:19:34 +00007934#if defined(PYOS_OS2)
7935/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007936static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007937{
7938 APIRET rc;
7939 ULONG values[QSV_MAX+1];
7940 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007941 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007942
7943 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007944 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007945 Py_END_ALLOW_THREADS
7946
7947 if (rc != NO_ERROR) {
7948 os2_error(rc);
7949 return -1;
7950 }
7951
Fred Drake4d1e64b2002-04-15 19:40:07 +00007952 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7953 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7954 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7955 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7956 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7957 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7958 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007959
7960 switch (values[QSV_VERSION_MINOR]) {
7961 case 0: ver = "2.00"; break;
7962 case 10: ver = "2.10"; break;
7963 case 11: ver = "2.11"; break;
7964 case 30: ver = "3.00"; break;
7965 case 40: ver = "4.00"; break;
7966 case 50: ver = "5.00"; break;
7967 default:
Tim Peters885d4572001-11-28 20:27:42 +00007968 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007970 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007971 ver = &tmp[0];
7972 }
7973
7974 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007975 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007976 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007977
7978 /* Add Indicator of Which Drive was Used to Boot the System */
7979 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7980 tmp[1] = ':';
7981 tmp[2] = '\0';
7982
Fred Drake4d1e64b2002-04-15 19:40:07 +00007983 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007984}
7985#endif
7986
Barry Warsaw4a342091996-12-19 23:50:02 +00007987static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007988all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007989{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007990#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007991 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007992#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007993#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007994 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007995#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007996#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007998#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007999#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008000 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008001#endif
Fred Drakec9680921999-12-13 16:37:25 +00008002#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00008004#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008005#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008006 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00008007#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008008#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00008009 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008010#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008011#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00008012 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008013#endif
Fred Drake106c1a02002-04-23 15:58:02 +00008014#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00008016#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00008017#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008018 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008019#endif
8020#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00008021 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008022#endif
8023#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008025#endif
8026#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008028#endif
8029#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008031#endif
8032#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008033 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008034#endif
8035#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008037#endif
8038#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008039 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008040#endif
8041#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008042 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008043#endif
8044#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008046#endif
8047#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008049#endif
8050#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008051 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008052#endif
8053#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008054 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008055#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008056#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008058#endif
8059#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008060 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008061#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008062#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008063 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008064#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008065#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008066 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008067#endif
8068#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008070#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008071
Tim Peters5aa91602002-01-30 05:46:57 +00008072/* MS Windows */
8073#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008074 /* Don't inherit in child processes. */
8075 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008076#endif
8077#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008078 /* Optimize for short life (keep in memory). */
8079 /* MS forgot to define this one with a non-underscore form too. */
8080 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008081#endif
8082#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008083 /* Automatically delete when last handle is closed. */
8084 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008085#endif
8086#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008087 /* Optimize for random access. */
8088 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008089#endif
8090#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008091 /* Optimize for sequential access. */
8092 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008093#endif
8094
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008095/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008096#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 /* Send a SIGIO signal whenever input or output
8098 becomes available on file descriptor */
8099 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008100#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008101#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 /* Direct disk access. */
8103 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008104#endif
8105#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 /* Must be a directory. */
8107 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008108#endif
8109#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 /* Do not follow links. */
8111 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008112#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008113#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008114 /* Do not update the access time. */
8115 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008116#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008117
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008119#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008121#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008122#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008124#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008125#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008126 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008127#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008128#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008129 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008130#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008131#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008133#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008134#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008136#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008137#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008138 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008139#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008140#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008141 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008142#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008143#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008145#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008146#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008148#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008149#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008150 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008151#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008152#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008153 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008154#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008155#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008156 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008157#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008158#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008160#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008161#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008162 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008163#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008164#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008165 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008166#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008167#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008169#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008170
Guido van Rossum246bc171999-02-01 23:54:31 +00008171#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008172#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008173 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8174 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8175 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8176 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8177 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8178 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8179 if (ins(d, "P_PM", (long)P_PM)) return -1;
8180 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8181 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8182 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8183 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8184 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8185 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8186 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8187 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8188 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8189 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8190 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8191 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8192 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008193#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008194 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8195 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8196 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8197 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8198 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008199#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008200#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008201
Guido van Rossumd48f2521997-12-05 22:19:34 +00008202#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008203 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008204#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008205 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008206}
8207
8208
Tim Peters5aa91602002-01-30 05:46:57 +00008209#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008210#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008211#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008212
8213#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008214#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008215#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008216
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008217#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008218#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008219#define MODNAME "posix"
8220#endif
8221
Martin v. Löwis1a214512008-06-11 05:26:20 +00008222static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008223 PyModuleDef_HEAD_INIT,
8224 MODNAME,
8225 posix__doc__,
8226 -1,
8227 posix_methods,
8228 NULL,
8229 NULL,
8230 NULL,
8231 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008232};
8233
8234
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008235PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008236INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008237{
Victor Stinner8c62be82010-05-06 00:08:46 +00008238 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008239
Victor Stinner8c62be82010-05-06 00:08:46 +00008240 m = PyModule_Create(&posixmodule);
8241 if (m == NULL)
8242 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008243
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 /* Initialize environ dictionary */
8245 v = convertenviron();
8246 Py_XINCREF(v);
8247 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8248 return NULL;
8249 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008250
Victor Stinner8c62be82010-05-06 00:08:46 +00008251 if (all_ins(m))
8252 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008253
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 if (setup_confname_tables(m))
8255 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008256
Victor Stinner8c62be82010-05-06 00:08:46 +00008257 Py_INCREF(PyExc_OSError);
8258 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008259
Guido van Rossumb3d39562000-01-31 18:41:26 +00008260#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 if (posix_putenv_garbage == NULL)
8262 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008263#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008264
Victor Stinner8c62be82010-05-06 00:08:46 +00008265 if (!initialized) {
8266 stat_result_desc.name = MODNAME ".stat_result";
8267 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8268 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8269 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8270 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8271 structseq_new = StatResultType.tp_new;
8272 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008273
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 statvfs_result_desc.name = MODNAME ".statvfs_result";
8275 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008276#ifdef NEED_TICKS_PER_SECOND
8277# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008278 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008279# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008280 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008281# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008282 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008283# endif
8284#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008285 }
8286 Py_INCREF((PyObject*) &StatResultType);
8287 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8288 Py_INCREF((PyObject*) &StatVFSResultType);
8289 PyModule_AddObject(m, "statvfs_result",
8290 (PyObject*) &StatVFSResultType);
8291 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008292
8293#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008294 /*
8295 * Step 2 of weak-linking support on Mac OS X.
8296 *
8297 * The code below removes functions that are not available on the
8298 * currently active platform.
8299 *
8300 * This block allow one to use a python binary that was build on
8301 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8302 * OSX 10.4.
8303 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008304#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008305 if (fstatvfs == NULL) {
8306 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8307 return NULL;
8308 }
8309 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008310#endif /* HAVE_FSTATVFS */
8311
8312#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 if (statvfs == NULL) {
8314 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8315 return NULL;
8316 }
8317 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008318#endif /* HAVE_STATVFS */
8319
8320# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008321 if (lchown == NULL) {
8322 if (PyObject_DelAttrString(m, "lchown") == -1) {
8323 return NULL;
8324 }
8325 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008326#endif /* HAVE_LCHOWN */
8327
8328
8329#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008330 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008331
Guido van Rossumb6775db1994-08-01 11:34:53 +00008332}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008333
8334#ifdef __cplusplus
8335}
8336#endif