blob: c09bcb0ccaebf8434053a21739e1d81ab656fa8f [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
Tim Petersbc2e10e2002-03-03 23:17:02 +0000265#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000266#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000267#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000268#include <shellapi.h> /* for ShellExecute() */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000269#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
Guido van Rossumd48f2521997-12-05 22:19:34 +0000271#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274
Tim Petersbc2e10e2002-03-03 23:17:02 +0000275#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000276#if defined(PATH_MAX) && PATH_MAX > 1024
277#define MAXPATHLEN PATH_MAX
278#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000279#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000281#endif /* MAXPATHLEN */
282
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000283#ifdef UNION_WAIT
284/* Emulate some macros on systems that have a union instead of macros */
285
286#ifndef WIFEXITED
287#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
288#endif
289
290#ifndef WEXITSTATUS
291#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
292#endif
293
294#ifndef WTERMSIG
295#define WTERMSIG(u_wait) ((u_wait).w_termsig)
296#endif
297
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298#define WAIT_TYPE union wait
299#define WAIT_STATUS_INT(s) (s.w_status)
300
301#else /* !UNION_WAIT */
302#define WAIT_TYPE int
303#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000304#endif /* UNION_WAIT */
305
Greg Wardb48bc172000-03-01 21:51:56 +0000306/* Don't use the "_r" form if we don't need it (also, won't have a
307 prototype for it, at least on Solaris -- maybe others as well?). */
308#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
309#define USE_CTERMID_R
310#endif
311
Fred Drake699f3522000-06-29 21:12:41 +0000312/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000313#undef STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000314#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000315# define STAT win32_stat
316# define FSTAT win32_fstat
317# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000318#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000319# define STAT stat
320# define FSTAT fstat
321# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000322#endif
323
Tim Peters11b23062003-04-23 02:39:17 +0000324#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000325#include <sys/mkdev.h>
326#else
327#if defined(MAJOR_IN_SYSMACROS)
328#include <sys/sysmacros.h>
329#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000330#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
331#include <sys/mkdev.h>
332#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000333#endif
Fred Drake699f3522000-06-29 21:12:41 +0000334
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000335#if defined _MSC_VER && _MSC_VER >= 1400
336/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
337 * valid and throw an assertion if it isn't.
338 * Normally, an invalid fd is likely to be a C program error and therefore
339 * an assertion can be useful, but it does contradict the POSIX standard
340 * which for write(2) states:
341 * "Otherwise, -1 shall be returned and errno set to indicate the error."
342 * "[EBADF] The fildes argument is not a valid file descriptor open for
343 * writing."
344 * Furthermore, python allows the user to enter any old integer
345 * as a fd and should merely raise a python exception on error.
346 * The Microsoft CRT doesn't provide an official way to check for the
347 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000348 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000349 * internal structures involved.
350 * The structures below must be updated for each version of visual studio
351 * according to the file internal.h in the CRT source, until MS comes
352 * up with a less hacky way to do this.
353 * (all of this is to avoid globally modifying the CRT behaviour using
354 * _set_invalid_parameter_handler() and _CrtSetReportMode())
355 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000356/* The actual size of the structure is determined at runtime.
357 * Only the first items must be present.
358 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000359typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000360 intptr_t osfhnd;
361 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000362} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000363
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000364extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000365#define IOINFO_L2E 5
366#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
367#define IOINFO_ARRAYS 64
368#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
369#define FOPEN 0x01
370#define _NO_CONSOLE_FILENO (intptr_t)-2
371
372/* This function emulates what the windows CRT does to validate file handles */
373int
374_PyVerify_fd(int fd)
375{
Victor Stinner8c62be82010-05-06 00:08:46 +0000376 const int i1 = fd >> IOINFO_L2E;
377 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000378
Victor Stinner8c62be82010-05-06 00:08:46 +0000379 static int sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000380
Victor Stinner8c62be82010-05-06 00:08:46 +0000381 /* Determine the actual size of the ioinfo structure,
382 * as used by the CRT loaded in memory
383 */
384 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
385 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
386 }
387 if (sizeof_ioinfo == 0) {
388 /* This should not happen... */
389 goto fail;
390 }
391
392 /* See that it isn't a special CLEAR fileno */
393 if (fd != _NO_CONSOLE_FILENO) {
394 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
395 * we check pointer validity and other info
396 */
397 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
398 /* finally, check that the file is open */
399 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
400 if (info->osfile & FOPEN) {
401 return 1;
402 }
403 }
404 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000405 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000406 errno = EBADF;
407 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000408}
409
410/* the special case of checking dup2. The target fd must be in a sensible range */
411static int
412_PyVerify_fd_dup2(int fd1, int fd2)
413{
Victor Stinner8c62be82010-05-06 00:08:46 +0000414 if (!_PyVerify_fd(fd1))
415 return 0;
416 if (fd2 == _NO_CONSOLE_FILENO)
417 return 0;
418 if ((unsigned)fd2 < _NHANDLE_)
419 return 1;
420 else
421 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000422}
423#else
424/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
425#define _PyVerify_fd_dup2(A, B) (1)
426#endif
427
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428/* Return a dictionary corresponding to the POSIX environment table */
Jack Jansenea0c3822002-08-01 21:57:49 +0000429#ifdef WITH_NEXT_FRAMEWORK
430/* On Darwin/MacOSX a shared library or framework has no access to
431** environ directly, we must obtain it with _NSGetEnviron().
432*/
433#include <crt_externs.h>
434static char **environ;
435#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000437#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438
Barry Warsaw53699e91996-12-10 23:23:01 +0000439static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000440convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Victor Stinner8c62be82010-05-06 00:08:46 +0000442 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000443#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000444 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000445#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000446 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +0000447#endif
Martin v. Löwisc16f3bd2003-05-03 09:14:54 +0000448#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +0000449 APIRET rc;
450 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
451#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +0000452
Victor Stinner8c62be82010-05-06 00:08:46 +0000453 d = PyDict_New();
454 if (d == NULL)
455 return NULL;
456#ifdef WITH_NEXT_FRAMEWORK
457 if (environ == NULL)
458 environ = *_NSGetEnviron();
459#endif
460#ifdef MS_WINDOWS
461 /* _wenviron must be initialized in this way if the program is started
462 through main() instead of wmain(). */
463 _wgetenv(L"");
464 if (_wenviron == NULL)
465 return d;
466 /* This part ignores errors */
467 for (e = _wenviron; *e != NULL; e++) {
468 PyObject *k;
469 PyObject *v;
470 wchar_t *p = wcschr(*e, L'=');
471 if (p == NULL)
472 continue;
473 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
474 if (k == NULL) {
475 PyErr_Clear();
476 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000477 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000478 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
479 if (v == NULL) {
480 PyErr_Clear();
481 Py_DECREF(k);
482 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +0000483 }
Victor Stinner8c62be82010-05-06 00:08:46 +0000484 if (PyDict_GetItem(d, k) == NULL) {
485 if (PyDict_SetItem(d, k, v) != 0)
486 PyErr_Clear();
487 }
488 Py_DECREF(k);
489 Py_DECREF(v);
490 }
491#else
492 if (environ == NULL)
493 return d;
494 /* This part ignores errors */
495 for (e = environ; *e != NULL; e++) {
496 PyObject *k;
497 PyObject *v;
498 char *p = strchr(*e, '=');
499 if (p == NULL)
500 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +0000501 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +0000502 if (k == NULL) {
503 PyErr_Clear();
504 continue;
505 }
Victor Stinner84ae1182010-05-06 22:05:07 +0000506 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +0000507 if (v == NULL) {
508 PyErr_Clear();
509 Py_DECREF(k);
510 continue;
511 }
512 if (PyDict_GetItem(d, k) == NULL) {
513 if (PyDict_SetItem(d, k, v) != 0)
514 PyErr_Clear();
515 }
516 Py_DECREF(k);
517 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000518 }
519#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000520#if defined(PYOS_OS2)
521 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
522 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
523 PyObject *v = PyBytes_FromString(buffer);
524 PyDict_SetItemString(d, "BEGINLIBPATH", v);
525 Py_DECREF(v);
526 }
527 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
528 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
529 PyObject *v = PyBytes_FromString(buffer);
530 PyDict_SetItemString(d, "ENDLIBPATH", v);
531 Py_DECREF(v);
532 }
533#endif
534 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535}
536
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537/* Set a POSIX-specific error from errno, and return NULL */
538
Barry Warsawd58d7641998-07-23 16:14:40 +0000539static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000540posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000541{
Victor Stinner8c62be82010-05-06 00:08:46 +0000542 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000543}
Barry Warsawd58d7641998-07-23 16:14:40 +0000544static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000545posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000546{
Victor Stinner8c62be82010-05-06 00:08:46 +0000547 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000548}
549
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000550#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000551static PyObject *
552posix_error_with_unicode_filename(Py_UNICODE* name)
553{
Victor Stinner8c62be82010-05-06 00:08:46 +0000554 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000555}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000556#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557
558
Mark Hammondef8b6542001-05-13 08:04:26 +0000559static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000560posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000561{
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000562 PyObject *name_str, *rc;
563 name_str = PyUnicode_DecodeFSDefaultAndSize(PyBytes_AsString(name),
564 PyBytes_GET_SIZE(name));
Victor Stinner8c62be82010-05-06 00:08:46 +0000565 Py_DECREF(name);
Victor Stinner77ccd6d2010-05-08 00:36:42 +0000566 rc = PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError,
567 name_str);
568 Py_XDECREF(name_str);
Victor Stinner8c62be82010-05-06 00:08:46 +0000569 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000570}
571
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000572#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000573static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +0000574win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000575{
Victor Stinner8c62be82010-05-06 00:08:46 +0000576 /* XXX We should pass the function name along in the future.
577 (winreg.c also wants to pass the function name.)
578 This would however require an additional param to the
579 Windows error object, which is non-trivial.
580 */
581 errno = GetLastError();
582 if (filename)
583 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
584 else
585 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000586}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000587
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000588static PyObject *
589win32_error_unicode(char* function, Py_UNICODE* filename)
590{
Victor Stinner8c62be82010-05-06 00:08:46 +0000591 /* XXX - see win32_error for comments on 'function' */
592 errno = GetLastError();
593 if (filename)
594 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
595 else
596 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000597}
598
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000600convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601{
Victor Stinner8c62be82010-05-06 00:08:46 +0000602 if (PyUnicode_CheckExact(*param))
603 Py_INCREF(*param);
604 else if (PyUnicode_Check(*param))
605 /* For a Unicode subtype that's not a Unicode object,
606 return a true Unicode object with the same data. */
607 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
608 PyUnicode_GET_SIZE(*param));
609 else
610 *param = PyUnicode_FromEncodedObject(*param,
611 Py_FileSystemDefaultEncoding,
612 "strict");
613 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000614}
615
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000616#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617
Guido van Rossumd48f2521997-12-05 22:19:34 +0000618#if defined(PYOS_OS2)
619/**********************************************************************
620 * Helper Function to Trim and Format OS/2 Messages
621 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000622static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000623os2_formatmsg(char *msgbuf, int msglen, char *reason)
624{
625 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
626
627 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
628 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
629
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000630 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000631 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
632 }
633
634 /* Add Optional Reason Text */
635 if (reason) {
636 strcat(msgbuf, " : ");
637 strcat(msgbuf, reason);
638 }
639}
640
641/**********************************************************************
642 * Decode an OS/2 Operating System Error Code
643 *
644 * A convenience function to lookup an OS/2 error code and return a
645 * text message we can use to raise a Python exception.
646 *
647 * Notes:
648 * The messages for errors returned from the OS/2 kernel reside in
649 * the file OSO001.MSG in the \OS2 directory hierarchy.
650 *
651 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000652static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000653os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
654{
655 APIRET rc;
656 ULONG msglen;
657
658 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
659 Py_BEGIN_ALLOW_THREADS
660 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
661 errorcode, "oso001.msg", &msglen);
662 Py_END_ALLOW_THREADS
663
664 if (rc == NO_ERROR)
665 os2_formatmsg(msgbuf, msglen, reason);
666 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000667 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000668 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000669
670 return msgbuf;
671}
672
673/* Set an OS/2-specific error and return NULL. OS/2 kernel
674 errors are not in a global variable e.g. 'errno' nor are
675 they congruent with posix error numbers. */
676
Victor Stinner8c62be82010-05-06 00:08:46 +0000677static PyObject *
678os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000679{
680 char text[1024];
681 PyObject *v;
682
683 os2_strerror(text, sizeof(text), code, "");
684
685 v = Py_BuildValue("(is)", code, text);
686 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000687 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000688 Py_DECREF(v);
689 }
690 return NULL; /* Signal to Python that an Exception is Pending */
691}
692
693#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694
695/* POSIX generic methods */
696
Barry Warsaw53699e91996-12-10 23:23:01 +0000697static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000698posix_fildes(PyObject *fdobj, int (*func)(int))
699{
Victor Stinner8c62be82010-05-06 00:08:46 +0000700 int fd;
701 int res;
702 fd = PyObject_AsFileDescriptor(fdobj);
703 if (fd < 0)
704 return NULL;
705 if (!_PyVerify_fd(fd))
706 return posix_error();
707 Py_BEGIN_ALLOW_THREADS
708 res = (*func)(fd);
709 Py_END_ALLOW_THREADS
710 if (res < 0)
711 return posix_error();
712 Py_INCREF(Py_None);
713 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000714}
Guido van Rossum21142a01999-01-08 21:05:37 +0000715
716static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718{
Victor Stinner8c62be82010-05-06 00:08:46 +0000719 PyObject *opath1 = NULL;
720 char *path1;
721 int res;
722 if (!PyArg_ParseTuple(args, format,
723 PyUnicode_FSConverter, &opath1))
724 return NULL;
725 path1 = PyBytes_AsString(opath1);
726 Py_BEGIN_ALLOW_THREADS
727 res = (*func)(path1);
728 Py_END_ALLOW_THREADS
729 if (res < 0)
730 return posix_error_with_allocated_filename(opath1);
731 Py_DECREF(opath1);
732 Py_INCREF(Py_None);
733 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734}
735
Barry Warsaw53699e91996-12-10 23:23:01 +0000736static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000737posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000738 char *format,
739 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000740{
Victor Stinner8c62be82010-05-06 00:08:46 +0000741 PyObject *opath1 = NULL, *opath2 = NULL;
742 char *path1, *path2;
743 int res;
744 if (!PyArg_ParseTuple(args, format,
745 PyUnicode_FSConverter, &opath1,
746 PyUnicode_FSConverter, &opath2)) {
747 return NULL;
748 }
749 path1 = PyBytes_AsString(opath1);
750 path2 = PyBytes_AsString(opath2);
751 Py_BEGIN_ALLOW_THREADS
752 res = (*func)(path1, path2);
753 Py_END_ALLOW_THREADS
754 Py_DECREF(opath1);
755 Py_DECREF(opath2);
756 if (res != 0)
757 /* XXX how to report both path1 and path2??? */
758 return posix_error();
759 Py_INCREF(Py_None);
760 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761}
762
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000763#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000764static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000765win32_1str(PyObject* args, char* func,
766 char* format, BOOL (__stdcall *funcA)(LPCSTR),
767 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000768{
Victor Stinner8c62be82010-05-06 00:08:46 +0000769 PyObject *uni;
770 char *ansi;
771 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000772
Victor Stinner8c62be82010-05-06 00:08:46 +0000773 if (!PyArg_ParseTuple(args, wformat, &uni))
774 PyErr_Clear();
775 else {
776 Py_BEGIN_ALLOW_THREADS
777 result = funcW(PyUnicode_AsUnicode(uni));
778 Py_END_ALLOW_THREADS
779 if (!result)
780 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
781 Py_INCREF(Py_None);
782 return Py_None;
783 }
784 if (!PyArg_ParseTuple(args, format, &ansi))
785 return NULL;
786 Py_BEGIN_ALLOW_THREADS
787 result = funcA(ansi);
788 Py_END_ALLOW_THREADS
789 if (!result)
790 return win32_error(func, ansi);
791 Py_INCREF(Py_None);
792 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793
794}
795
796/* This is a reimplementation of the C library's chdir function,
797 but one that produces Win32 errors instead of DOS error codes.
798 chdir is essentially a wrapper around SetCurrentDirectory; however,
799 it also needs to set "magic" environment variables indicating
800 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000801static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000802win32_chdir(LPCSTR path)
803{
Victor Stinner8c62be82010-05-06 00:08:46 +0000804 char new_path[MAX_PATH+1];
805 int result;
806 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000807
Victor Stinner8c62be82010-05-06 00:08:46 +0000808 if(!SetCurrentDirectoryA(path))
809 return FALSE;
810 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
811 if (!result)
812 return FALSE;
813 /* In the ANSI API, there should not be any paths longer
814 than MAX_PATH. */
815 assert(result <= MAX_PATH+1);
816 if (strncmp(new_path, "\\\\", 2) == 0 ||
817 strncmp(new_path, "//", 2) == 0)
818 /* UNC path, nothing to do. */
819 return TRUE;
820 env[1] = new_path[0];
821 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000822}
823
824/* The Unicode version differs from the ANSI version
825 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000826static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000827win32_wchdir(LPCWSTR path)
828{
Victor Stinner8c62be82010-05-06 00:08:46 +0000829 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
830 int result;
831 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832
Victor Stinner8c62be82010-05-06 00:08:46 +0000833 if(!SetCurrentDirectoryW(path))
834 return FALSE;
835 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
836 if (!result)
837 return FALSE;
838 if (result > MAX_PATH+1) {
839 new_path = malloc(result * sizeof(wchar_t));
840 if (!new_path) {
841 SetLastError(ERROR_OUTOFMEMORY);
842 return FALSE;
843 }
844 result = GetCurrentDirectoryW(result, new_path);
845 if (!result) {
846 free(new_path);
847 return FALSE;
848 }
849 }
850 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
851 wcsncmp(new_path, L"//", 2) == 0)
852 /* UNC path, nothing to do. */
853 return TRUE;
854 env[1] = new_path[0];
855 result = SetEnvironmentVariableW(env, new_path);
856 if (new_path != _new_path)
857 free(new_path);
858 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000859}
860#endif
861
Martin v. Löwis14694662006-02-03 12:54:16 +0000862#ifdef MS_WINDOWS
863/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
864 - time stamps are restricted to second resolution
865 - file modification times suffer from forth-and-back conversions between
866 UTC and local time
867 Therefore, we implement our own stat, based on the Win32 API directly.
868*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000869#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000870
871struct win32_stat{
872 int st_dev;
873 __int64 st_ino;
874 unsigned short st_mode;
875 int st_nlink;
876 int st_uid;
877 int st_gid;
878 int st_rdev;
879 __int64 st_size;
880 int st_atime;
881 int st_atime_nsec;
882 int st_mtime;
883 int st_mtime_nsec;
884 int st_ctime;
885 int st_ctime_nsec;
886};
887
888static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
889
890static void
891FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
892{
Victor Stinner8c62be82010-05-06 00:08:46 +0000893 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
894 /* Cannot simply cast and dereference in_ptr,
895 since it might not be aligned properly */
896 __int64 in;
897 memcpy(&in, in_ptr, sizeof(in));
898 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
899 /* XXX Win32 supports time stamps past 2038; we currently don't */
900 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000901}
902
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903static void
904time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
905{
Victor Stinner8c62be82010-05-06 00:08:46 +0000906 /* XXX endianness */
907 __int64 out;
908 out = time_in + secs_between_epochs;
909 out = out * 10000000 + nsec_in / 100;
910 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911}
912
Martin v. Löwis14694662006-02-03 12:54:16 +0000913/* Below, we *know* that ugo+r is 0444 */
914#if _S_IREAD != 0400
915#error Unsupported C library
916#endif
917static int
918attributes_to_mode(DWORD attr)
919{
Victor Stinner8c62be82010-05-06 00:08:46 +0000920 int m = 0;
921 if (attr & FILE_ATTRIBUTE_DIRECTORY)
922 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
923 else
924 m |= _S_IFREG;
925 if (attr & FILE_ATTRIBUTE_READONLY)
926 m |= 0444;
927 else
928 m |= 0666;
929 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000930}
931
932static int
933attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
934{
Victor Stinner8c62be82010-05-06 00:08:46 +0000935 memset(result, 0, sizeof(*result));
936 result->st_mode = attributes_to_mode(info->dwFileAttributes);
937 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
938 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
939 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
940 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000941
Victor Stinner8c62be82010-05-06 00:08:46 +0000942 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000943}
944
Guido van Rossumd8faa362007-04-27 19:54:29 +0000945static BOOL
946attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
947{
Victor Stinner8c62be82010-05-06 00:08:46 +0000948 HANDLE hFindFile;
949 WIN32_FIND_DATAA FileData;
950 hFindFile = FindFirstFileA(pszFile, &FileData);
951 if (hFindFile == INVALID_HANDLE_VALUE)
952 return FALSE;
953 FindClose(hFindFile);
954 pfad->dwFileAttributes = FileData.dwFileAttributes;
955 pfad->ftCreationTime = FileData.ftCreationTime;
956 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
957 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
958 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
959 pfad->nFileSizeLow = FileData.nFileSizeLow;
960 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000961}
962
963static BOOL
964attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
965{
Victor Stinner8c62be82010-05-06 00:08:46 +0000966 HANDLE hFindFile;
967 WIN32_FIND_DATAW FileData;
968 hFindFile = FindFirstFileW(pszFile, &FileData);
969 if (hFindFile == INVALID_HANDLE_VALUE)
970 return FALSE;
971 FindClose(hFindFile);
972 pfad->dwFileAttributes = FileData.dwFileAttributes;
973 pfad->ftCreationTime = FileData.ftCreationTime;
974 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
975 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
976 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
977 pfad->nFileSizeLow = FileData.nFileSizeLow;
978 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000979}
980
Brian Curtind40e6f72010-07-08 21:39:08 +0000981/* About the following functions: win32_lstat, win32_lstat_w, win32_stat,
982 win32_stat_w
983
984 In Posix, stat automatically traverses symlinks and returns the stat
985 structure for the target. In Windows, the equivalent GetFileAttributes by
986 default does not traverse symlinks and instead returns attributes for
987 the symlink.
988
989 Therefore, win32_lstat will get the attributes traditionally, and
990 win32_stat will first explicitly resolve the symlink target and then will
991 call win32_lstat on that result.
992
993 The _w represent Unicode equivalents of the aformentioned ANSI functions. */
994
995static int
996win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +0000997{
Victor Stinner8c62be82010-05-06 00:08:46 +0000998 WIN32_FILE_ATTRIBUTE_DATA info;
999 int code;
1000 char *dot;
Brian Curtind40e6f72010-07-08 21:39:08 +00001001 WIN32_FIND_DATAA find_data;
1002 HANDLE find_data_handle;
Victor Stinner8c62be82010-05-06 00:08:46 +00001003 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
1004 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1005 /* Protocol violation: we explicitly clear errno, instead of
1006 setting it to a POSIX error. Callers should use GetLastError. */
1007 errno = 0;
1008 return -1;
1009 } else {
1010 /* Could not get attributes on open file. Fall back to
1011 reading the directory. */
1012 if (!attributes_from_dir(path, &info)) {
1013 /* Very strange. This should not fail now */
1014 errno = 0;
1015 return -1;
1016 }
1017 }
1018 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001019
Victor Stinner8c62be82010-05-06 00:08:46 +00001020 code = attribute_data_to_stat(&info, result);
1021 if (code != 0)
1022 return code;
Brian Curtind40e6f72010-07-08 21:39:08 +00001023
1024 /* Get WIN32_FIND_DATA structure for the path to determine if
1025 it is a symlink */
1026 if(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1027 find_data_handle = FindFirstFileA(path, &find_data);
1028 if(find_data_handle != INVALID_HANDLE_VALUE) {
1029 if(find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
1030 /* first clear the S_IFMT bits */
1031 result->st_mode ^= (result->st_mode & 0170000);
1032 /* now set the bits that make this a symlink */
1033 result->st_mode |= 0120000;
1034 }
1035 FindClose(find_data_handle);
1036 }
1037 }
1038
Victor Stinner8c62be82010-05-06 00:08:46 +00001039 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1040 dot = strrchr(path, '.');
1041 if (dot) {
Brian Curtind40e6f72010-07-08 21:39:08 +00001042 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1043 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00001044 result->st_mode |= 0111;
1045 }
1046 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001047}
1048
Victor Stinner8c62be82010-05-06 00:08:46 +00001049static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001050win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001051{
Victor Stinner8c62be82010-05-06 00:08:46 +00001052 int code;
1053 const wchar_t *dot;
1054 WIN32_FILE_ATTRIBUTE_DATA info;
Brian Curtind40e6f72010-07-08 21:39:08 +00001055 WIN32_FIND_DATAW find_data;
1056 HANDLE find_data_handle;
Victor Stinner8c62be82010-05-06 00:08:46 +00001057 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1058 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1059 /* Protocol violation: we explicitly clear errno, instead of
1060 setting it to a POSIX error. Callers should use GetLastError. */
1061 errno = 0;
1062 return -1;
1063 } else {
Brian Curtind40e6f72010-07-08 21:39:08 +00001064 /* Could not get attributes on open file. Fall back to reading
1065 the directory. */
1066 if (!attributes_from_dir_w(path, &info)) {
1067 /* Very strange. This should not fail now */
1068 errno = 0;
1069 return -1;
1070 }
1071 }
1072 }
1073 code = attribute_data_to_stat(&info, result);
1074 if (code < 0)
1075 return code;
1076
1077 /* Get WIN32_FIND_DATA structure for the path to determine if
1078 it is a symlink */
1079 if(info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1080 find_data_handle = FindFirstFileW(path, &find_data);
1081 if(find_data_handle != INVALID_HANDLE_VALUE) {
1082 if(find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
1083 /* first clear the S_IFMT bits */
1084 result->st_mode ^= (result->st_mode & 0170000);
1085 /* now set the bits that make this a symlink */
1086 result->st_mode |= 0120000;
1087 }
1088 FindClose(find_data_handle);
1089 }
1090 }
1091
1092 /* Set IFEXEC if it is an .exe, .bat, ... */
1093 dot = wcsrchr(path, '.');
1094 if (dot) {
1095 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1096 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1097 result->st_mode |= 0111;
1098 }
1099 return code;
1100}
1101
1102/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
1103static int has_GetFinalPathNameByHandle = 0;
1104static DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1105 DWORD);
1106static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1107 DWORD);
1108static int
1109check_GetFinalPathNameByHandle()
1110{
1111 HINSTANCE hKernel32;
1112 /* only recheck */
1113 if (!has_GetFinalPathNameByHandle)
1114 {
1115 hKernel32 = GetModuleHandle("KERNEL32");
1116 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1117 "GetFinalPathNameByHandleA");
1118 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1119 "GetFinalPathNameByHandleW");
Brian Curtin74e45612010-07-09 15:58:59 +00001120 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1121 Py_GetFinalPathNameByHandleW;
Brian Curtind40e6f72010-07-08 21:39:08 +00001122 }
1123 return has_GetFinalPathNameByHandle;
1124}
1125
1126static int
1127win32_stat(const char* path, struct win32_stat *result)
1128{
1129 /* Traverse the symlink to the target using
1130 GetFinalPathNameByHandle()
1131 */
1132 int code;
1133 HANDLE hFile;
1134 int buf_size;
1135 char *target_path;
1136 int result_length;
1137 WIN32_FILE_ATTRIBUTE_DATA info;
1138
1139 if(!check_GetFinalPathNameByHandle()) {
1140 /* if the OS doesn't have GetFinalPathNameByHandle, it doesn't
1141 have symlinks, so just fall back to the traditional behavior
1142 found in lstat. */
1143 return win32_lstat(path, result);
1144 }
1145
1146 hFile = CreateFileA(
1147 path,
1148 0, /* desired access */
1149 0, /* share mode */
1150 NULL, /* security attributes */
1151 OPEN_EXISTING,
1152 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1153 FILE_FLAG_BACKUP_SEMANTICS,
1154 NULL);
1155
1156 if(hFile == INVALID_HANDLE_VALUE) {
1157 /* Either the target doesn't exist, or we don't have access to
1158 get a handle to it. If the former, we need to return an error.
1159 If the latter, we can use attributes_from_dir. */
1160 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1161 /* Protocol violation: we explicitly clear errno, instead of
1162 setting it to a POSIX error. Callers should use GetLastError. */
1163 errno = 0;
1164 return -1;
1165 } else {
1166 /* Could not get attributes on open file. Fall back to
1167 reading the directory. */
1168 if (!attributes_from_dir(path, &info)) {
1169 /* Very strange. This should not fail now */
1170 errno = 0;
1171 return -1;
1172 }
1173 }
1174 code = attribute_data_to_stat(&info, result);
1175 }
1176
1177 buf_size = Py_GetFinalPathNameByHandleA(hFile, 0, 0, VOLUME_NAME_DOS);
1178 if(!buf_size) return -1;
1179 target_path = (char *)malloc((buf_size+1)*sizeof(char));
1180 result_length = Py_GetFinalPathNameByHandleA(hFile, target_path,
1181 buf_size, VOLUME_NAME_DOS);
1182
1183 if(!result_length)
1184 return -1;
1185
1186 if(!CloseHandle(hFile))
1187 return -1;
1188
1189 target_path[result_length] = 0;
1190 code = win32_lstat(target_path, result);
1191 free(target_path);
1192
1193 return code;
1194}
1195
1196static int
1197win32_stat_w(const wchar_t* path, struct win32_stat *result)
1198{
1199 /* Traverse the symlink to the target using GetFinalPathNameByHandle() */
1200 int code;
1201 HANDLE hFile;
1202 int buf_size;
1203 wchar_t *target_path;
1204 int result_length;
1205 WIN32_FILE_ATTRIBUTE_DATA info;
1206
1207 if(!check_GetFinalPathNameByHandle()) {
1208 /* If the OS doesn't have GetFinalPathNameByHandle, it doesn't have
1209 symlinks, so just fall back to the traditional behavior found
1210 in lstat. */
1211 return win32_lstat_w(path, result);
1212 }
1213
1214 hFile = CreateFileW(
1215 path,
1216 0, /* desired access */
1217 0, /* share mode */
1218 NULL, /* security attributes */
1219 OPEN_EXISTING,
1220 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1221 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1222 NULL);
1223
1224 if(hFile == INVALID_HANDLE_VALUE) {
1225 /* Either the target doesn't exist, or we don't have access to
1226 get a handle to it. If the former, we need to return an error.
1227 If the latter, we can use attributes_from_dir. */
1228 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1229 /* Protocol violation: we explicitly clear errno, instead of
1230 setting it to a POSIX error. Callers should use GetLastError. */
1231 errno = 0;
1232 return -1;
1233 } else {
Victor Stinner8c62be82010-05-06 00:08:46 +00001234 /* Could not get attributes on open file. Fall back to
1235 reading the directory. */
1236 if (!attributes_from_dir_w(path, &info)) {
1237 /* Very strange. This should not fail now */
1238 errno = 0;
1239 return -1;
1240 }
1241 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001242 code = attribute_data_to_stat(&info, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001243 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001244 else {
1245 /* We have a good handle to the target, use it to determine the target
1246 path name (then we'll call lstat on it). */
1247 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_DOS);
1248 if(!buf_size)
1249 return -1;
1250
1251 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
1252 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
1253 buf_size, VOLUME_NAME_DOS);
1254
1255 if(!result_length)
1256 return -1;
1257
1258 if(!CloseHandle(hFile))
1259 return -1;
1260
1261 target_path[result_length] = 0;
1262 code = win32_lstat_w(target_path, result);
1263 free(target_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001264 }
Brian Curtind40e6f72010-07-08 21:39:08 +00001265
Victor Stinner8c62be82010-05-06 00:08:46 +00001266 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001267}
1268
1269static int
1270win32_fstat(int file_number, struct win32_stat *result)
1271{
Victor Stinner8c62be82010-05-06 00:08:46 +00001272 BY_HANDLE_FILE_INFORMATION info;
1273 HANDLE h;
1274 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001275
Victor Stinner8c62be82010-05-06 00:08:46 +00001276 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001277
Victor Stinner8c62be82010-05-06 00:08:46 +00001278 /* Protocol violation: we explicitly clear errno, instead of
1279 setting it to a POSIX error. Callers should use GetLastError. */
1280 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001281
Victor Stinner8c62be82010-05-06 00:08:46 +00001282 if (h == INVALID_HANDLE_VALUE) {
1283 /* This is really a C library error (invalid file handle).
1284 We set the Win32 error to the closes one matching. */
1285 SetLastError(ERROR_INVALID_HANDLE);
1286 return -1;
1287 }
1288 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001289
Victor Stinner8c62be82010-05-06 00:08:46 +00001290 type = GetFileType(h);
1291 if (type == FILE_TYPE_UNKNOWN) {
1292 DWORD error = GetLastError();
1293 if (error != 0) {
1294 return -1;
1295 }
1296 /* else: valid but unknown file */
1297 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001298
Victor Stinner8c62be82010-05-06 00:08:46 +00001299 if (type != FILE_TYPE_DISK) {
1300 if (type == FILE_TYPE_CHAR)
1301 result->st_mode = _S_IFCHR;
1302 else if (type == FILE_TYPE_PIPE)
1303 result->st_mode = _S_IFIFO;
1304 return 0;
1305 }
1306
1307 if (!GetFileInformationByHandle(h, &info)) {
1308 return -1;
1309 }
1310
1311 /* similar to stat() */
1312 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1313 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
Brian Curtin74e45612010-07-09 15:58:59 +00001314 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime,
1315 &result->st_ctime_nsec);
1316 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime,
1317 &result->st_mtime_nsec);
1318 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime,
1319 &result->st_atime_nsec);
Victor Stinner8c62be82010-05-06 00:08:46 +00001320 /* specific to fstat() */
1321 result->st_nlink = info.nNumberOfLinks;
1322 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1323 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001324}
1325
1326#endif /* MS_WINDOWS */
1327
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001328PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001329"stat_result: Result from stat or lstat.\n\n\
1330This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001331 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001332or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1333\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001334Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1335or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001336\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001337See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001338
1339static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001340 {"st_mode", "protection bits"},
1341 {"st_ino", "inode"},
1342 {"st_dev", "device"},
1343 {"st_nlink", "number of hard links"},
1344 {"st_uid", "user ID of owner"},
1345 {"st_gid", "group ID of owner"},
1346 {"st_size", "total size, in bytes"},
1347 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1348 {NULL, "integer time of last access"},
1349 {NULL, "integer time of last modification"},
1350 {NULL, "integer time of last change"},
1351 {"st_atime", "time of last access"},
1352 {"st_mtime", "time of last modification"},
1353 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001354#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001355 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001356#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001357#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001358 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001359#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001360#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001361 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001362#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001363#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001364 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001365#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001366#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001367 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001368#endif
1369#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001371#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001372 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001373};
1374
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001375#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001376#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001377#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001378#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001379#endif
1380
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001381#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001382#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1383#else
1384#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1385#endif
1386
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001387#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001388#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1389#else
1390#define ST_RDEV_IDX ST_BLOCKS_IDX
1391#endif
1392
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001393#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1394#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1395#else
1396#define ST_FLAGS_IDX ST_RDEV_IDX
1397#endif
1398
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001399#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001400#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001401#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001402#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001403#endif
1404
1405#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1406#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1407#else
1408#define ST_BIRTHTIME_IDX ST_GEN_IDX
1409#endif
1410
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001411static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001412 "stat_result", /* name */
1413 stat_result__doc__, /* doc */
1414 stat_result_fields,
1415 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001416};
1417
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001418PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001419"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1420This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001421 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001422or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001423\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001424See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001425
1426static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001427 {"f_bsize", },
1428 {"f_frsize", },
1429 {"f_blocks", },
1430 {"f_bfree", },
1431 {"f_bavail", },
1432 {"f_files", },
1433 {"f_ffree", },
1434 {"f_favail", },
1435 {"f_flag", },
1436 {"f_namemax",},
1437 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001438};
1439
1440static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001441 "statvfs_result", /* name */
1442 statvfs_result__doc__, /* doc */
1443 statvfs_result_fields,
1444 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001445};
1446
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001447static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001448static PyTypeObject StatResultType;
1449static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001450static newfunc structseq_new;
1451
1452static PyObject *
1453statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1454{
Victor Stinner8c62be82010-05-06 00:08:46 +00001455 PyStructSequence *result;
1456 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001457
Victor Stinner8c62be82010-05-06 00:08:46 +00001458 result = (PyStructSequence*)structseq_new(type, args, kwds);
1459 if (!result)
1460 return NULL;
1461 /* If we have been initialized from a tuple,
1462 st_?time might be set to None. Initialize it
1463 from the int slots. */
1464 for (i = 7; i <= 9; i++) {
1465 if (result->ob_item[i+3] == Py_None) {
1466 Py_DECREF(Py_None);
1467 Py_INCREF(result->ob_item[i]);
1468 result->ob_item[i+3] = result->ob_item[i];
1469 }
1470 }
1471 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001472}
1473
1474
1475
1476/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001477static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001478
1479PyDoc_STRVAR(stat_float_times__doc__,
1480"stat_float_times([newval]) -> oldval\n\n\
1481Determine whether os.[lf]stat represents time stamps as float objects.\n\
1482If newval is True, future calls to stat() return floats, if it is False,\n\
1483future calls return ints. \n\
1484If newval is omitted, return the current setting.\n");
1485
1486static PyObject*
1487stat_float_times(PyObject* self, PyObject *args)
1488{
Victor Stinner8c62be82010-05-06 00:08:46 +00001489 int newval = -1;
1490 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1491 return NULL;
1492 if (newval == -1)
1493 /* Return old value */
1494 return PyBool_FromLong(_stat_float_times);
1495 _stat_float_times = newval;
1496 Py_INCREF(Py_None);
1497 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001498}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001499
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001500static void
1501fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1502{
Victor Stinner8c62be82010-05-06 00:08:46 +00001503 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001504#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001505 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001506#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001508#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001509 if (!ival)
1510 return;
1511 if (_stat_float_times) {
1512 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1513 } else {
1514 fval = ival;
1515 Py_INCREF(fval);
1516 }
1517 PyStructSequence_SET_ITEM(v, index, ival);
1518 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001519}
1520
Tim Peters5aa91602002-01-30 05:46:57 +00001521/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001522 (used by posix_stat() and posix_fstat()) */
1523static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001524_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001525{
Victor Stinner8c62be82010-05-06 00:08:46 +00001526 unsigned long ansec, mnsec, cnsec;
1527 PyObject *v = PyStructSequence_New(&StatResultType);
1528 if (v == NULL)
1529 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001530
Victor Stinner8c62be82010-05-06 00:08:46 +00001531 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001532#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001533 PyStructSequence_SET_ITEM(v, 1,
1534 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001535#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001536 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001537#endif
1538#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001539 PyStructSequence_SET_ITEM(v, 2,
1540 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001541#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001542 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001543#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001544 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1545 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1546 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001547#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001548 PyStructSequence_SET_ITEM(v, 6,
1549 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001550#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001551 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001552#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001553
Martin v. Löwis14694662006-02-03 12:54:16 +00001554#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001555 ansec = st->st_atim.tv_nsec;
1556 mnsec = st->st_mtim.tv_nsec;
1557 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001558#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001559 ansec = st->st_atimespec.tv_nsec;
1560 mnsec = st->st_mtimespec.tv_nsec;
1561 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001562#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001563 ansec = st->st_atime_nsec;
1564 mnsec = st->st_mtime_nsec;
1565 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001566#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001567 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001568#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001569 fill_time(v, 7, st->st_atime, ansec);
1570 fill_time(v, 8, st->st_mtime, mnsec);
1571 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001572
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001573#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001574 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1575 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001576#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001577#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1579 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001580#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001581#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001582 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1583 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00001584#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001585#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001586 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1587 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001588#endif
1589#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001590 {
1591 PyObject *val;
1592 unsigned long bsec,bnsec;
1593 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001594#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001595 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001596#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001597 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001598#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001599 if (_stat_float_times) {
1600 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1601 } else {
1602 val = PyLong_FromLong((long)bsec);
1603 }
1604 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1605 val);
1606 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001607#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001608#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001609 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1610 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001611#endif
Fred Drake699f3522000-06-29 21:12:41 +00001612
Victor Stinner8c62be82010-05-06 00:08:46 +00001613 if (PyErr_Occurred()) {
1614 Py_DECREF(v);
1615 return NULL;
1616 }
Fred Drake699f3522000-06-29 21:12:41 +00001617
Victor Stinner8c62be82010-05-06 00:08:46 +00001618 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001619}
1620
Martin v. Löwisd8948722004-06-02 09:57:56 +00001621#ifdef MS_WINDOWS
1622
1623/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1624 where / can be used in place of \ and the trailing slash is optional.
1625 Both SERVER and SHARE must have at least one character.
1626*/
1627
1628#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1629#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001630#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001631#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001632#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001633
Tim Peters4ad82172004-08-30 17:02:04 +00001634static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001635IsUNCRootA(char *path, int pathlen)
1636{
Victor Stinner8c62be82010-05-06 00:08:46 +00001637 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001638
Victor Stinner8c62be82010-05-06 00:08:46 +00001639 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001640
Victor Stinner8c62be82010-05-06 00:08:46 +00001641 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1642 /* minimum UNCRoot is \\x\y */
1643 return FALSE;
1644 for (i = 2; i < pathlen ; i++)
1645 if (ISSLASH(path[i])) break;
1646 if (i == 2 || i == pathlen)
1647 /* do not allow \\\SHARE or \\SERVER */
1648 return FALSE;
1649 share = i+1;
1650 for (i = share; i < pathlen; i++)
1651 if (ISSLASH(path[i])) break;
1652 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001653
Victor Stinner8c62be82010-05-06 00:08:46 +00001654 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001655}
1656
Tim Peters4ad82172004-08-30 17:02:04 +00001657static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001658IsUNCRootW(Py_UNICODE *path, int pathlen)
1659{
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001661
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001663
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1665 /* minimum UNCRoot is \\x\y */
1666 return FALSE;
1667 for (i = 2; i < pathlen ; i++)
1668 if (ISSLASH(path[i])) break;
1669 if (i == 2 || i == pathlen)
1670 /* do not allow \\\SHARE or \\SERVER */
1671 return FALSE;
1672 share = i+1;
1673 for (i = share; i < pathlen; i++)
1674 if (ISSLASH(path[i])) break;
1675 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001676
Victor Stinner8c62be82010-05-06 00:08:46 +00001677 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001678}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001679#endif /* MS_WINDOWS */
1680
Barry Warsaw53699e91996-12-10 23:23:01 +00001681static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001682posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001683 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001684#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001685 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001686#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001687 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001688#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 char *wformat,
1690 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001691{
Victor Stinner8c62be82010-05-06 00:08:46 +00001692 STRUCT_STAT st;
1693 PyObject *opath;
1694 char *path;
1695 int res;
1696 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001697
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001698#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001699 PyUnicodeObject *po;
1700 if (PyArg_ParseTuple(args, wformat, &po)) {
1701 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001702
Victor Stinner8c62be82010-05-06 00:08:46 +00001703 Py_BEGIN_ALLOW_THREADS
1704 /* PyUnicode_AS_UNICODE result OK without
1705 thread lock as it is a simple dereference. */
1706 res = wstatfunc(wpath, &st);
1707 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001708
Victor Stinner8c62be82010-05-06 00:08:46 +00001709 if (res != 0)
1710 return win32_error_unicode("stat", wpath);
1711 return _pystat_fromstructstat(&st);
1712 }
1713 /* Drop the argument parsing error as narrow strings
1714 are also valid. */
1715 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001716#endif
1717
Victor Stinner8c62be82010-05-06 00:08:46 +00001718 if (!PyArg_ParseTuple(args, format,
1719 PyUnicode_FSConverter, &opath))
1720 return NULL;
1721 path = PyBytes_AsString(opath);
1722 Py_BEGIN_ALLOW_THREADS
1723 res = (*statfunc)(path, &st);
1724 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001725
Victor Stinner8c62be82010-05-06 00:08:46 +00001726 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001727#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001728 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001729#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001730 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001731#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001732 }
1733 else
1734 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001735
Victor Stinner8c62be82010-05-06 00:08:46 +00001736 Py_DECREF(opath);
1737 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001738}
1739
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001740/* POSIX methods */
1741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001742PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001743"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001744Use the real uid/gid to test for access to a path. Note that most\n\
1745operations will use the effective uid/gid, therefore this routine can\n\
1746be used in a suid/sgid environment to test if the invoking user has the\n\
1747specified access to the path. The mode argument can be F_OK to test\n\
1748existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001749
1750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001751posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001752{
Victor Stinner8c62be82010-05-06 00:08:46 +00001753 PyObject *opath;
1754 char *path;
1755 int mode;
1756
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001757#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001758 DWORD attr;
1759 PyUnicodeObject *po;
1760 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1761 Py_BEGIN_ALLOW_THREADS
1762 /* PyUnicode_AS_UNICODE OK without thread lock as
1763 it is a simple dereference. */
1764 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1765 Py_END_ALLOW_THREADS
1766 goto finish;
1767 }
1768 /* Drop the argument parsing error as narrow strings
1769 are also valid. */
1770 PyErr_Clear();
1771 if (!PyArg_ParseTuple(args, "O&i:access",
1772 PyUnicode_FSConverter, &opath, &mode))
1773 return NULL;
1774 path = PyBytes_AsString(opath);
1775 Py_BEGIN_ALLOW_THREADS
1776 attr = GetFileAttributesA(path);
1777 Py_END_ALLOW_THREADS
1778 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001779finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 if (attr == 0xFFFFFFFF)
1781 /* File does not exist, or cannot read attributes */
1782 return PyBool_FromLong(0);
1783 /* Access is possible if either write access wasn't requested, or
1784 the file isn't read-only, or if it's a directory, as there are
1785 no read-only directories on Windows. */
1786 return PyBool_FromLong(!(mode & 2)
1787 || !(attr & FILE_ATTRIBUTE_READONLY)
1788 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001789#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001790 int res;
1791 if (!PyArg_ParseTuple(args, "O&i:access",
1792 PyUnicode_FSConverter, &opath, &mode))
1793 return NULL;
1794 path = PyBytes_AsString(opath);
1795 Py_BEGIN_ALLOW_THREADS
1796 res = access(path, mode);
1797 Py_END_ALLOW_THREADS
1798 Py_DECREF(opath);
1799 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001801}
1802
Guido van Rossumd371ff11999-01-25 16:12:23 +00001803#ifndef F_OK
1804#define F_OK 0
1805#endif
1806#ifndef R_OK
1807#define R_OK 4
1808#endif
1809#ifndef W_OK
1810#define W_OK 2
1811#endif
1812#ifndef X_OK
1813#define X_OK 1
1814#endif
1815
1816#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001817PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001818"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001819Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001820
1821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001822posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001823{
Victor Stinner8c62be82010-05-06 00:08:46 +00001824 int id;
1825 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001826
Victor Stinner8c62be82010-05-06 00:08:46 +00001827 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1828 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001829
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001830#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001831 /* file descriptor 0 only, the default input device (stdin) */
1832 if (id == 0) {
1833 ret = ttyname();
1834 }
1835 else {
1836 ret = NULL;
1837 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001838#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001840#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001841 if (ret == NULL)
1842 return posix_error();
1843 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001844}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001845#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001846
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001847#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001848PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001849"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001850Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001851
1852static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001853posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001854{
Victor Stinner8c62be82010-05-06 00:08:46 +00001855 char *ret;
1856 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001857
Greg Wardb48bc172000-03-01 21:51:56 +00001858#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001860#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001861 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001862#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001863 if (ret == NULL)
1864 return posix_error();
1865 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001866}
1867#endif
1868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001870"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001872
Barry Warsaw53699e91996-12-10 23:23:01 +00001873static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001874posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001875{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001876#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001877 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001878#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001879 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001880#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001881 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001882#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001883 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001884#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885}
1886
Fred Drake4d1e64b2002-04-15 19:40:07 +00001887#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001889"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001890Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001892
1893static PyObject *
1894posix_fchdir(PyObject *self, PyObject *fdobj)
1895{
Victor Stinner8c62be82010-05-06 00:08:46 +00001896 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001897}
1898#endif /* HAVE_FCHDIR */
1899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001902"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001903Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001904
Barry Warsaw53699e91996-12-10 23:23:01 +00001905static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001906posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001907{
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 PyObject *opath = NULL;
1909 char *path = NULL;
1910 int i;
1911 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001912#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001913 DWORD attr;
1914 PyUnicodeObject *po;
1915 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1916 Py_BEGIN_ALLOW_THREADS
1917 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1918 if (attr != 0xFFFFFFFF) {
1919 if (i & _S_IWRITE)
1920 attr &= ~FILE_ATTRIBUTE_READONLY;
1921 else
1922 attr |= FILE_ATTRIBUTE_READONLY;
1923 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1924 }
1925 else
1926 res = 0;
1927 Py_END_ALLOW_THREADS
1928 if (!res)
1929 return win32_error_unicode("chmod",
1930 PyUnicode_AS_UNICODE(po));
1931 Py_INCREF(Py_None);
1932 return Py_None;
1933 }
1934 /* Drop the argument parsing error as narrow strings
1935 are also valid. */
1936 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001937
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1939 &opath, &i))
1940 return NULL;
1941 path = PyBytes_AsString(opath);
1942 Py_BEGIN_ALLOW_THREADS
1943 attr = GetFileAttributesA(path);
1944 if (attr != 0xFFFFFFFF) {
1945 if (i & _S_IWRITE)
1946 attr &= ~FILE_ATTRIBUTE_READONLY;
1947 else
1948 attr |= FILE_ATTRIBUTE_READONLY;
1949 res = SetFileAttributesA(path, attr);
1950 }
1951 else
1952 res = 0;
1953 Py_END_ALLOW_THREADS
1954 if (!res) {
1955 win32_error("chmod", path);
1956 Py_DECREF(opath);
1957 return NULL;
1958 }
1959 Py_DECREF(opath);
1960 Py_INCREF(Py_None);
1961 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001962#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001963 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1964 &opath, &i))
1965 return NULL;
1966 path = PyBytes_AsString(opath);
1967 Py_BEGIN_ALLOW_THREADS
1968 res = chmod(path, i);
1969 Py_END_ALLOW_THREADS
1970 if (res < 0)
1971 return posix_error_with_allocated_filename(opath);
1972 Py_DECREF(opath);
1973 Py_INCREF(Py_None);
1974 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001975#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001976}
1977
Christian Heimes4e30a842007-11-30 22:12:06 +00001978#ifdef HAVE_FCHMOD
1979PyDoc_STRVAR(posix_fchmod__doc__,
1980"fchmod(fd, mode)\n\n\
1981Change the access permissions of the file given by file\n\
1982descriptor fd.");
1983
1984static PyObject *
1985posix_fchmod(PyObject *self, PyObject *args)
1986{
Victor Stinner8c62be82010-05-06 00:08:46 +00001987 int fd, mode, res;
1988 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1989 return NULL;
1990 Py_BEGIN_ALLOW_THREADS
1991 res = fchmod(fd, mode);
1992 Py_END_ALLOW_THREADS
1993 if (res < 0)
1994 return posix_error();
1995 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001996}
1997#endif /* HAVE_FCHMOD */
1998
1999#ifdef HAVE_LCHMOD
2000PyDoc_STRVAR(posix_lchmod__doc__,
2001"lchmod(path, mode)\n\n\
2002Change the access permissions of a file. If path is a symlink, this\n\
2003affects the link itself rather than the target.");
2004
2005static PyObject *
2006posix_lchmod(PyObject *self, PyObject *args)
2007{
Victor Stinner8c62be82010-05-06 00:08:46 +00002008 PyObject *opath;
2009 char *path;
2010 int i;
2011 int res;
2012 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
2013 &opath, &i))
2014 return NULL;
2015 path = PyBytes_AsString(opath);
2016 Py_BEGIN_ALLOW_THREADS
2017 res = lchmod(path, i);
2018 Py_END_ALLOW_THREADS
2019 if (res < 0)
2020 return posix_error_with_allocated_filename(opath);
2021 Py_DECREF(opath);
2022 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002023}
2024#endif /* HAVE_LCHMOD */
2025
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002026
Thomas Wouterscf297e42007-02-23 15:07:44 +00002027#ifdef HAVE_CHFLAGS
2028PyDoc_STRVAR(posix_chflags__doc__,
2029"chflags(path, flags)\n\n\
2030Set file flags.");
2031
2032static PyObject *
2033posix_chflags(PyObject *self, PyObject *args)
2034{
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 PyObject *opath;
2036 char *path;
2037 unsigned long flags;
2038 int res;
2039 if (!PyArg_ParseTuple(args, "O&k:chflags",
2040 PyUnicode_FSConverter, &opath, &flags))
2041 return NULL;
2042 path = PyBytes_AsString(opath);
2043 Py_BEGIN_ALLOW_THREADS
2044 res = chflags(path, flags);
2045 Py_END_ALLOW_THREADS
2046 if (res < 0)
2047 return posix_error_with_allocated_filename(opath);
2048 Py_DECREF(opath);
2049 Py_INCREF(Py_None);
2050 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002051}
2052#endif /* HAVE_CHFLAGS */
2053
2054#ifdef HAVE_LCHFLAGS
2055PyDoc_STRVAR(posix_lchflags__doc__,
2056"lchflags(path, flags)\n\n\
2057Set file flags.\n\
2058This function will not follow symbolic links.");
2059
2060static PyObject *
2061posix_lchflags(PyObject *self, PyObject *args)
2062{
Victor Stinner8c62be82010-05-06 00:08:46 +00002063 PyObject *opath;
2064 char *path;
2065 unsigned long flags;
2066 int res;
2067 if (!PyArg_ParseTuple(args, "O&k:lchflags",
2068 PyUnicode_FSConverter, &opath, &flags))
2069 return NULL;
2070 path = PyBytes_AsString(opath);
2071 Py_BEGIN_ALLOW_THREADS
2072 res = lchflags(path, flags);
2073 Py_END_ALLOW_THREADS
2074 if (res < 0)
2075 return posix_error_with_allocated_filename(opath);
2076 Py_DECREF(opath);
2077 Py_INCREF(Py_None);
2078 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002079}
2080#endif /* HAVE_LCHFLAGS */
2081
Martin v. Löwis244edc82001-10-04 22:44:26 +00002082#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002083PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002084"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002085Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002086
2087static PyObject *
2088posix_chroot(PyObject *self, PyObject *args)
2089{
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002091}
2092#endif
2093
Guido van Rossum21142a01999-01-08 21:05:37 +00002094#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002095PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002096"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002097force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002098
2099static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002100posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002101{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002102 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002103}
2104#endif /* HAVE_FSYNC */
2105
2106#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002107
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002108#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002109extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2110#endif
2111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002112PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002113"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002114force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002115 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002116
2117static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002118posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002119{
Fred Drake4d1e64b2002-04-15 19:40:07 +00002120 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002121}
2122#endif /* HAVE_FDATASYNC */
2123
2124
Fredrik Lundh10723342000-07-10 16:38:09 +00002125#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002126PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002127"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002128Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002129
Barry Warsaw53699e91996-12-10 23:23:01 +00002130static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002131posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002132{
Victor Stinner8c62be82010-05-06 00:08:46 +00002133 PyObject *opath;
2134 char *path;
2135 long uid, gid;
2136 int res;
2137 if (!PyArg_ParseTuple(args, "O&ll:chown",
2138 PyUnicode_FSConverter, &opath,
2139 &uid, &gid))
2140 return NULL;
2141 path = PyBytes_AsString(opath);
2142 Py_BEGIN_ALLOW_THREADS
2143 res = chown(path, (uid_t) uid, (gid_t) gid);
2144 Py_END_ALLOW_THREADS
2145 if (res < 0)
2146 return posix_error_with_allocated_filename(opath);
2147 Py_DECREF(opath);
2148 Py_INCREF(Py_None);
2149 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002150}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002151#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002152
Christian Heimes4e30a842007-11-30 22:12:06 +00002153#ifdef HAVE_FCHOWN
2154PyDoc_STRVAR(posix_fchown__doc__,
2155"fchown(fd, uid, gid)\n\n\
2156Change the owner and group id of the file given by file descriptor\n\
2157fd to the numeric uid and gid.");
2158
2159static PyObject *
2160posix_fchown(PyObject *self, PyObject *args)
2161{
Victor Stinner8c62be82010-05-06 00:08:46 +00002162 int fd;
2163 long uid, gid;
2164 int res;
2165 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
2166 return NULL;
2167 Py_BEGIN_ALLOW_THREADS
2168 res = fchown(fd, (uid_t) uid, (gid_t) gid);
2169 Py_END_ALLOW_THREADS
2170 if (res < 0)
2171 return posix_error();
2172 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002173}
2174#endif /* HAVE_FCHOWN */
2175
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002176#ifdef HAVE_LCHOWN
2177PyDoc_STRVAR(posix_lchown__doc__,
2178"lchown(path, uid, gid)\n\n\
2179Change the owner and group id of path to the numeric uid and gid.\n\
2180This function will not follow symbolic links.");
2181
2182static PyObject *
2183posix_lchown(PyObject *self, PyObject *args)
2184{
Victor Stinner8c62be82010-05-06 00:08:46 +00002185 PyObject *opath;
2186 char *path;
2187 long uid, gid;
2188 int res;
2189 if (!PyArg_ParseTuple(args, "O&ll:lchown",
2190 PyUnicode_FSConverter, &opath,
2191 &uid, &gid))
2192 return NULL;
2193 path = PyBytes_AsString(opath);
2194 Py_BEGIN_ALLOW_THREADS
2195 res = lchown(path, (uid_t) uid, (gid_t) gid);
2196 Py_END_ALLOW_THREADS
2197 if (res < 0)
2198 return posix_error_with_allocated_filename(opath);
2199 Py_DECREF(opath);
2200 Py_INCREF(Py_None);
2201 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00002202}
2203#endif /* HAVE_LCHOWN */
2204
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002205
Guido van Rossum36bc6801995-06-14 22:54:23 +00002206#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00002207static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002208posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002209{
Victor Stinner8c62be82010-05-06 00:08:46 +00002210 char buf[1026];
2211 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002212
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002213#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002214 if (!use_bytes) {
2215 wchar_t wbuf[1026];
2216 wchar_t *wbuf2 = wbuf;
2217 PyObject *resobj;
2218 DWORD len;
2219 Py_BEGIN_ALLOW_THREADS
2220 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2221 /* If the buffer is large enough, len does not include the
2222 terminating \0. If the buffer is too small, len includes
2223 the space needed for the terminator. */
2224 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2225 wbuf2 = malloc(len * sizeof(wchar_t));
2226 if (wbuf2)
2227 len = GetCurrentDirectoryW(len, wbuf2);
2228 }
2229 Py_END_ALLOW_THREADS
2230 if (!wbuf2) {
2231 PyErr_NoMemory();
2232 return NULL;
2233 }
2234 if (!len) {
2235 if (wbuf2 != wbuf) free(wbuf2);
2236 return win32_error("getcwdu", NULL);
2237 }
2238 resobj = PyUnicode_FromWideChar(wbuf2, len);
2239 if (wbuf2 != wbuf) free(wbuf2);
2240 return resobj;
2241 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002242#endif
2243
Victor Stinner8c62be82010-05-06 00:08:46 +00002244 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002245#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002247#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002248 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002249#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002250 Py_END_ALLOW_THREADS
2251 if (res == NULL)
2252 return posix_error();
2253 if (use_bytes)
2254 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00002255 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002256}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002257
2258PyDoc_STRVAR(posix_getcwd__doc__,
2259"getcwd() -> path\n\n\
2260Return a unicode string representing the current working directory.");
2261
2262static PyObject *
2263posix_getcwd_unicode(PyObject *self)
2264{
2265 return posix_getcwd(0);
2266}
2267
2268PyDoc_STRVAR(posix_getcwdb__doc__,
2269"getcwdb() -> path\n\n\
2270Return a bytes string representing the current working directory.");
2271
2272static PyObject *
2273posix_getcwd_bytes(PyObject *self)
2274{
2275 return posix_getcwd(1);
2276}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002277#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002279
Guido van Rossumb6775db1994-08-01 11:34:53 +00002280#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002281PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002282"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002283Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002284
Barry Warsaw53699e91996-12-10 23:23:01 +00002285static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002286posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002287{
Victor Stinner8c62be82010-05-06 00:08:46 +00002288 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002289}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002290#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002291
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002293PyDoc_STRVAR(posix_listdir__doc__,
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002294"listdir([path]) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002295Return a list containing the names of the entries in the directory.\n\
2296\n\
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002297 path: path of directory to list (default: '.')\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002298\n\
2299The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002300entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002301
Barry Warsaw53699e91996-12-10 23:23:01 +00002302static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002303posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002304{
Victor Stinner8c62be82010-05-06 00:08:46 +00002305 /* XXX Should redo this putting the (now four) versions of opendir
2306 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002307#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002308
Victor Stinner8c62be82010-05-06 00:08:46 +00002309 PyObject *d, *v;
2310 HANDLE hFindFile;
2311 BOOL result;
2312 WIN32_FIND_DATA FileData;
2313 PyObject *opath;
2314 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2315 char *bufptr = namebuf;
2316 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002317
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002318 PyObject *po = NULL;
2319 if (PyArg_ParseTuple(args, "|U:listdir", &po)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002320 WIN32_FIND_DATAW wFileData;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002321 Py_UNICODE *wnamebuf, *po_wchars;
2322
2323 if (po == NULL) { // Default arg: "."
2324 po_wchars = L".";
2325 len = 1;
2326 } else {
2327 po_wchars = PyUnicode_AS_UNICODE(po);
2328 len = PyUnicode_GET_SIZE(po);
2329 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002330 /* Overallocate for \\*.*\0 */
Victor Stinner8c62be82010-05-06 00:08:46 +00002331 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2332 if (!wnamebuf) {
2333 PyErr_NoMemory();
2334 return NULL;
2335 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002336 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00002337 if (len > 0) {
2338 Py_UNICODE wch = wnamebuf[len-1];
2339 if (wch != L'/' && wch != L'\\' && wch != L':')
2340 wnamebuf[len++] = L'\\';
2341 wcscpy(wnamebuf + len, L"*.*");
2342 }
2343 if ((d = PyList_New(0)) == NULL) {
2344 free(wnamebuf);
2345 return NULL;
2346 }
2347 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2348 if (hFindFile == INVALID_HANDLE_VALUE) {
2349 int error = GetLastError();
2350 if (error == ERROR_FILE_NOT_FOUND) {
2351 free(wnamebuf);
2352 return d;
2353 }
2354 Py_DECREF(d);
2355 win32_error_unicode("FindFirstFileW", wnamebuf);
2356 free(wnamebuf);
2357 return NULL;
2358 }
2359 do {
2360 /* Skip over . and .. */
2361 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2362 wcscmp(wFileData.cFileName, L"..") != 0) {
2363 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2364 if (v == NULL) {
2365 Py_DECREF(d);
2366 d = NULL;
2367 break;
2368 }
2369 if (PyList_Append(d, v) != 0) {
2370 Py_DECREF(v);
2371 Py_DECREF(d);
2372 d = NULL;
2373 break;
2374 }
2375 Py_DECREF(v);
2376 }
2377 Py_BEGIN_ALLOW_THREADS
2378 result = FindNextFileW(hFindFile, &wFileData);
2379 Py_END_ALLOW_THREADS
2380 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2381 it got to the end of the directory. */
2382 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2383 Py_DECREF(d);
2384 win32_error_unicode("FindNextFileW", wnamebuf);
2385 FindClose(hFindFile);
2386 free(wnamebuf);
2387 return NULL;
2388 }
2389 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002390
Victor Stinner8c62be82010-05-06 00:08:46 +00002391 if (FindClose(hFindFile) == FALSE) {
2392 Py_DECREF(d);
2393 win32_error_unicode("FindClose", wnamebuf);
2394 free(wnamebuf);
2395 return NULL;
2396 }
2397 free(wnamebuf);
2398 return d;
2399 }
2400 /* Drop the argument parsing error as narrow strings
2401 are also valid. */
2402 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002403
Victor Stinner8c62be82010-05-06 00:08:46 +00002404 if (!PyArg_ParseTuple(args, "O&:listdir",
2405 PyUnicode_FSConverter, &opath))
2406 return NULL;
2407 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2408 PyErr_SetString(PyExc_ValueError, "path too long");
2409 Py_DECREF(opath);
2410 return NULL;
2411 }
2412 strcpy(namebuf, PyBytes_AsString(opath));
2413 len = PyObject_Size(opath);
2414 if (len > 0) {
2415 char ch = namebuf[len-1];
2416 if (ch != SEP && ch != ALTSEP && ch != ':')
2417 namebuf[len++] = '/';
2418 strcpy(namebuf + len, "*.*");
2419 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002420
Victor Stinner8c62be82010-05-06 00:08:46 +00002421 if ((d = PyList_New(0)) == NULL)
2422 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002423
Victor Stinner8c62be82010-05-06 00:08:46 +00002424 hFindFile = FindFirstFile(namebuf, &FileData);
2425 if (hFindFile == INVALID_HANDLE_VALUE) {
2426 int error = GetLastError();
2427 if (error == ERROR_FILE_NOT_FOUND)
2428 return d;
2429 Py_DECREF(d);
2430 return win32_error("FindFirstFile", namebuf);
2431 }
2432 do {
2433 /* Skip over . and .. */
2434 if (strcmp(FileData.cFileName, ".") != 0 &&
2435 strcmp(FileData.cFileName, "..") != 0) {
2436 v = PyBytes_FromString(FileData.cFileName);
2437 if (v == NULL) {
2438 Py_DECREF(d);
2439 d = NULL;
2440 break;
2441 }
2442 if (PyList_Append(d, v) != 0) {
2443 Py_DECREF(v);
2444 Py_DECREF(d);
2445 d = NULL;
2446 break;
2447 }
2448 Py_DECREF(v);
2449 }
2450 Py_BEGIN_ALLOW_THREADS
2451 result = FindNextFile(hFindFile, &FileData);
2452 Py_END_ALLOW_THREADS
2453 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2454 it got to the end of the directory. */
2455 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2456 Py_DECREF(d);
2457 win32_error("FindNextFile", namebuf);
2458 FindClose(hFindFile);
2459 return NULL;
2460 }
2461 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002462
Victor Stinner8c62be82010-05-06 00:08:46 +00002463 if (FindClose(hFindFile) == FALSE) {
2464 Py_DECREF(d);
2465 return win32_error("FindClose", namebuf);
2466 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002467
Victor Stinner8c62be82010-05-06 00:08:46 +00002468 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002469
Tim Peters0bb44a42000-09-15 07:44:49 +00002470#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002471
2472#ifndef MAX_PATH
2473#define MAX_PATH CCHMAXPATH
2474#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002475 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002476 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002477 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002478 PyObject *d, *v;
2479 char namebuf[MAX_PATH+5];
2480 HDIR hdir = 1;
2481 ULONG srchcnt = 1;
2482 FILEFINDBUF3 ep;
2483 APIRET rc;
2484
Victor Stinner8c62be82010-05-06 00:08:46 +00002485 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002486 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002487 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002488 name = PyBytes_AsString(oname);
2489 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002490 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002491 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002492 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002493 return NULL;
2494 }
2495 strcpy(namebuf, name);
2496 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002497 if (*pt == ALTSEP)
2498 *pt = SEP;
2499 if (namebuf[len-1] != SEP)
2500 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002501 strcpy(namebuf + len, "*.*");
2502
Neal Norwitz6c913782007-10-14 03:23:09 +00002503 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002504 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002505 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002506 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002507
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002508 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2509 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002510 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002511 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2512 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2513 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002514
2515 if (rc != NO_ERROR) {
2516 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002517 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002518 }
2519
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002520 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002521 do {
2522 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002523 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002524 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002525
2526 strcpy(namebuf, ep.achName);
2527
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002528 /* Leave Case of Name Alone -- In Native Form */
2529 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002530
Christian Heimes72b710a2008-05-26 13:28:38 +00002531 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002532 if (v == NULL) {
2533 Py_DECREF(d);
2534 d = NULL;
2535 break;
2536 }
2537 if (PyList_Append(d, v) != 0) {
2538 Py_DECREF(v);
2539 Py_DECREF(d);
2540 d = NULL;
2541 break;
2542 }
2543 Py_DECREF(v);
2544 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2545 }
2546
Victor Stinnerdcb24032010-04-22 12:08:36 +00002547 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002548 return d;
2549#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002550 PyObject *oname;
2551 char *name;
2552 PyObject *d, *v;
2553 DIR *dirp;
2554 struct dirent *ep;
2555 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002556
Victor Stinner8c62be82010-05-06 00:08:46 +00002557 errno = 0;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002558 /* v is never read, so it does not need to be initialized yet. */
2559 if (!PyArg_ParseTuple(args, "|U:listdir", &v)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00002560 arg_is_unicode = 0;
2561 PyErr_Clear();
2562 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002563 oname = NULL;
2564 if (!PyArg_ParseTuple(args, "|O&:listdir", PyUnicode_FSConverter, &oname))
Victor Stinner8c62be82010-05-06 00:08:46 +00002565 return NULL;
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00002566 if (oname == NULL) { // Default arg: "."
2567 oname = PyBytes_FromString(".");
2568 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002569 name = PyBytes_AsString(oname);
2570 if ((dirp = opendir(name)) == NULL) {
2571 return posix_error_with_allocated_filename(oname);
2572 }
2573 if ((d = PyList_New(0)) == NULL) {
2574 closedir(dirp);
2575 Py_DECREF(oname);
2576 return NULL;
2577 }
2578 for (;;) {
2579 errno = 0;
2580 Py_BEGIN_ALLOW_THREADS
2581 ep = readdir(dirp);
2582 Py_END_ALLOW_THREADS
2583 if (ep == NULL) {
2584 if (errno == 0) {
2585 break;
2586 } else {
2587 closedir(dirp);
2588 Py_DECREF(d);
2589 return posix_error_with_allocated_filename(oname);
2590 }
2591 }
2592 if (ep->d_name[0] == '.' &&
2593 (NAMLEN(ep) == 1 ||
2594 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2595 continue;
Victor Stinnera45598a2010-05-14 16:35:39 +00002596 if (arg_is_unicode)
2597 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
2598 else
2599 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00002600 if (v == NULL) {
Victor Stinnera45598a2010-05-14 16:35:39 +00002601 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002602 break;
2603 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002604 if (PyList_Append(d, v) != 0) {
2605 Py_DECREF(v);
Victor Stinnera45598a2010-05-14 16:35:39 +00002606 Py_CLEAR(d);
Victor Stinner8c62be82010-05-06 00:08:46 +00002607 break;
2608 }
2609 Py_DECREF(v);
2610 }
2611 closedir(dirp);
2612 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002613
Victor Stinner8c62be82010-05-06 00:08:46 +00002614 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002615
Tim Peters0bb44a42000-09-15 07:44:49 +00002616#endif /* which OS */
2617} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002618
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002619#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002620/* A helper function for abspath on win32 */
2621static PyObject *
2622posix__getfullpathname(PyObject *self, PyObject *args)
2623{
Victor Stinner8c62be82010-05-06 00:08:46 +00002624 PyObject *opath;
2625 char *path;
2626 char outbuf[MAX_PATH*2];
2627 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002628#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002629 PyUnicodeObject *po;
2630 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2631 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2632 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2633 Py_UNICODE *wtemp;
2634 DWORD result;
2635 PyObject *v;
2636 result = GetFullPathNameW(wpath,
2637 sizeof(woutbuf)/sizeof(woutbuf[0]),
2638 woutbuf, &wtemp);
2639 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2640 woutbufp = malloc(result * sizeof(Py_UNICODE));
2641 if (!woutbufp)
2642 return PyErr_NoMemory();
2643 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2644 }
2645 if (result)
2646 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2647 else
2648 v = win32_error_unicode("GetFullPathNameW", wpath);
2649 if (woutbufp != woutbuf)
2650 free(woutbufp);
2651 return v;
2652 }
2653 /* Drop the argument parsing error as narrow strings
2654 are also valid. */
2655 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002656
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002657#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002658 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2659 PyUnicode_FSConverter, &opath))
2660 return NULL;
2661 path = PyBytes_AsString(opath);
2662 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2663 outbuf, &temp)) {
2664 win32_error("GetFullPathName", path);
2665 Py_DECREF(opath);
2666 return NULL;
2667 }
2668 Py_DECREF(opath);
2669 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2670 return PyUnicode_Decode(outbuf, strlen(outbuf),
2671 Py_FileSystemDefaultEncoding, NULL);
2672 }
2673 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002674} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00002675
2676/* A helper function for samepath on windows */
2677static PyObject *
2678posix__getfinalpathname(PyObject *self, PyObject *args)
2679{
2680 HANDLE hFile;
2681 int buf_size;
2682 wchar_t *target_path;
2683 int result_length;
2684 PyObject *result;
2685 wchar_t *path;
2686
2687 if (!PyArg_ParseTuple(args, "u|:_getfullpathname", &path)) {
2688 return NULL;
2689 }
2690
2691 if(!check_GetFinalPathNameByHandle()) {
2692 /* If the OS doesn't have GetFinalPathNameByHandle, return a
2693 NotImplementedError. */
2694 return PyErr_Format(PyExc_NotImplementedError,
2695 "GetFinalPathNameByHandle not available on this platform");
2696 }
2697
2698 hFile = CreateFileW(
2699 path,
2700 0, /* desired access */
2701 0, /* share mode */
2702 NULL, /* security attributes */
2703 OPEN_EXISTING,
2704 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
2705 FILE_FLAG_BACKUP_SEMANTICS,
2706 NULL);
2707
2708 if(hFile == INVALID_HANDLE_VALUE) {
2709 return win32_error_unicode("GetFinalPathNamyByHandle", path);
Brian Curtin74e45612010-07-09 15:58:59 +00002710 return PyErr_Format(PyExc_RuntimeError,
2711 "Could not get a handle to file.");
Brian Curtind40e6f72010-07-08 21:39:08 +00002712 }
2713
2714 /* We have a good handle to the target, use it to determine the
2715 target path name. */
2716 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
2717
2718 if(!buf_size)
2719 return win32_error_unicode("GetFinalPathNameByHandle", path);
2720
2721 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
2722 if(!target_path)
2723 return PyErr_NoMemory();
2724
2725 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
2726 buf_size, VOLUME_NAME_DOS);
2727 if(!result_length)
2728 return win32_error_unicode("GetFinalPathNamyByHandle", path);
2729
2730 if(!CloseHandle(hFile))
2731 return win32_error_unicode("GetFinalPathNameByHandle", path);
2732
2733 target_path[result_length] = 0;
2734 result = PyUnicode_FromUnicode(target_path, result_length);
2735 free(target_path);
2736 return result;
2737
2738} /* end of posix__getfinalpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002739#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002741PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002742"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002743Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002744
Barry Warsaw53699e91996-12-10 23:23:01 +00002745static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002746posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002747{
Victor Stinner8c62be82010-05-06 00:08:46 +00002748 int res;
2749 PyObject *opath;
2750 char *path;
2751 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002752
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002753#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002754 PyUnicodeObject *po;
2755 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2756 Py_BEGIN_ALLOW_THREADS
2757 /* PyUnicode_AS_UNICODE OK without thread lock as
2758 it is a simple dereference. */
2759 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2760 Py_END_ALLOW_THREADS
2761 if (!res)
2762 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2763 Py_INCREF(Py_None);
2764 return Py_None;
2765 }
2766 /* Drop the argument parsing error as narrow strings
2767 are also valid. */
2768 PyErr_Clear();
2769 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2770 PyUnicode_FSConverter, &opath, &mode))
2771 return NULL;
2772 path = PyBytes_AsString(opath);
2773 Py_BEGIN_ALLOW_THREADS
2774 /* PyUnicode_AS_UNICODE OK without thread lock as
2775 it is a simple dereference. */
2776 res = CreateDirectoryA(path, NULL);
2777 Py_END_ALLOW_THREADS
2778 if (!res) {
2779 win32_error("mkdir", path);
2780 Py_DECREF(opath);
2781 return NULL;
2782 }
2783 Py_DECREF(opath);
2784 Py_INCREF(Py_None);
2785 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002786#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002787
Victor Stinner8c62be82010-05-06 00:08:46 +00002788 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2789 PyUnicode_FSConverter, &opath, &mode))
2790 return NULL;
2791 path = PyBytes_AsString(opath);
2792 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002793#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002794 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002795#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002796 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002797#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002798 Py_END_ALLOW_THREADS
2799 if (res < 0)
2800 return posix_error_with_allocated_filename(opath);
2801 Py_DECREF(opath);
2802 Py_INCREF(Py_None);
2803 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002804#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002805}
2806
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002807
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002808/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2809#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002810#include <sys/resource.h>
2811#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002812
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002813
2814#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002815PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002816"nice(inc) -> new_priority\n\n\
2817Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002818
Barry Warsaw53699e91996-12-10 23:23:01 +00002819static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002820posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002821{
Victor Stinner8c62be82010-05-06 00:08:46 +00002822 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002823
Victor Stinner8c62be82010-05-06 00:08:46 +00002824 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2825 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002826
Victor Stinner8c62be82010-05-06 00:08:46 +00002827 /* There are two flavours of 'nice': one that returns the new
2828 priority (as required by almost all standards out there) and the
2829 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2830 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002831
Victor Stinner8c62be82010-05-06 00:08:46 +00002832 If we are of the nice family that returns the new priority, we
2833 need to clear errno before the call, and check if errno is filled
2834 before calling posix_error() on a returnvalue of -1, because the
2835 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002836
Victor Stinner8c62be82010-05-06 00:08:46 +00002837 errno = 0;
2838 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002839#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002840 if (value == 0)
2841 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002842#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002843 if (value == -1 && errno != 0)
2844 /* either nice() or getpriority() returned an error */
2845 return posix_error();
2846 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002847}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002848#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002850PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002851"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002852Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002853
Barry Warsaw53699e91996-12-10 23:23:01 +00002854static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002855posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002856{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002857#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002858 PyObject *o1, *o2;
2859 char *p1, *p2;
2860 BOOL result;
2861 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2862 goto error;
2863 if (!convert_to_unicode(&o1))
2864 goto error;
2865 if (!convert_to_unicode(&o2)) {
2866 Py_DECREF(o1);
2867 goto error;
2868 }
2869 Py_BEGIN_ALLOW_THREADS
2870 result = MoveFileW(PyUnicode_AsUnicode(o1),
2871 PyUnicode_AsUnicode(o2));
2872 Py_END_ALLOW_THREADS
2873 Py_DECREF(o1);
2874 Py_DECREF(o2);
2875 if (!result)
2876 return win32_error("rename", NULL);
2877 Py_INCREF(Py_None);
2878 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002879error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002880 PyErr_Clear();
2881 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2882 return NULL;
2883 Py_BEGIN_ALLOW_THREADS
2884 result = MoveFileA(p1, p2);
2885 Py_END_ALLOW_THREADS
2886 if (!result)
2887 return win32_error("rename", NULL);
2888 Py_INCREF(Py_None);
2889 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002890#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002891 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002892#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002893}
2894
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002895
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002896PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002897"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002898Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002899
Barry Warsaw53699e91996-12-10 23:23:01 +00002900static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002901posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002902{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002903#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002904 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002905#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002906 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002907#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002908}
2909
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002911PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002912"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002913Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002914
Barry Warsaw53699e91996-12-10 23:23:01 +00002915static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002916posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002917{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002918#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00002919 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_stat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002920#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002921 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002922#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002923}
2924
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002925
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002926#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002927PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002928"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002929Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002930
Barry Warsaw53699e91996-12-10 23:23:01 +00002931static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002932posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002933{
Victor Stinner8c62be82010-05-06 00:08:46 +00002934 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002935#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002936 wchar_t *command;
2937 if (!PyArg_ParseTuple(args, "u:system", &command))
2938 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002939
Victor Stinner8c62be82010-05-06 00:08:46 +00002940 Py_BEGIN_ALLOW_THREADS
2941 sts = _wsystem(command);
2942 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002943#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002944 PyObject *command_obj;
2945 char *command;
2946 if (!PyArg_ParseTuple(args, "O&:system",
2947 PyUnicode_FSConverter, &command_obj))
2948 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002949
Victor Stinner8c62be82010-05-06 00:08:46 +00002950 command = PyBytes_AsString(command_obj);
2951 Py_BEGIN_ALLOW_THREADS
2952 sts = system(command);
2953 Py_END_ALLOW_THREADS
2954 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002955#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002956 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002957}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002958#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002959
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002960
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002961PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002962"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002963Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002964
Barry Warsaw53699e91996-12-10 23:23:01 +00002965static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002966posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002967{
Victor Stinner8c62be82010-05-06 00:08:46 +00002968 int i;
2969 if (!PyArg_ParseTuple(args, "i:umask", &i))
2970 return NULL;
2971 i = (int)umask(i);
2972 if (i < 0)
2973 return posix_error();
2974 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002975}
2976
Brian Curtind40e6f72010-07-08 21:39:08 +00002977#ifdef MS_WINDOWS
2978
2979/* override the default DeleteFileW behavior so that directory
2980symlinks can be removed with this function, the same as with
2981Unix symlinks */
2982BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
2983{
2984 WIN32_FILE_ATTRIBUTE_DATA info;
2985 WIN32_FIND_DATAW find_data;
2986 HANDLE find_data_handle;
2987 int is_directory = 0;
2988 int is_link = 0;
2989
2990 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
2991 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
2992
2993 /* Get WIN32_FIND_DATA structure for the path to determine if
2994 it is a symlink */
2995 if(is_directory &&
2996 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2997 find_data_handle = FindFirstFileW(lpFileName, &find_data);
2998
2999 if(find_data_handle != INVALID_HANDLE_VALUE) {
3000 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
3001 FindClose(find_data_handle);
3002 }
3003 }
3004 }
3005
3006 if (is_directory && is_link)
3007 return RemoveDirectoryW(lpFileName);
3008
3009 return DeleteFileW(lpFileName);
3010}
3011#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003013PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003014"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003015Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003017PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003018"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003019Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003020
Barry Warsaw53699e91996-12-10 23:23:01 +00003021static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003022posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003023{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003024#ifdef MS_WINDOWS
Brian Curtin74e45612010-07-09 15:58:59 +00003025 return win32_1str(args, "remove", "y:remove", DeleteFileA,
3026 "U:remove", Py_DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003027#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003028 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003029#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003030}
3031
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003032
Guido van Rossumb6775db1994-08-01 11:34:53 +00003033#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003034PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003035"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003037
Barry Warsaw53699e91996-12-10 23:23:01 +00003038static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003039posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003040{
Victor Stinner8c62be82010-05-06 00:08:46 +00003041 struct utsname u;
3042 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00003043
Victor Stinner8c62be82010-05-06 00:08:46 +00003044 Py_BEGIN_ALLOW_THREADS
3045 res = uname(&u);
3046 Py_END_ALLOW_THREADS
3047 if (res < 0)
3048 return posix_error();
3049 return Py_BuildValue("(sssss)",
3050 u.sysname,
3051 u.nodename,
3052 u.release,
3053 u.version,
3054 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00003055}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003056#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003057
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003058static int
3059extract_time(PyObject *t, long* sec, long* usec)
3060{
Victor Stinner8c62be82010-05-06 00:08:46 +00003061 long intval;
3062 if (PyFloat_Check(t)) {
3063 double tval = PyFloat_AsDouble(t);
3064 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
3065 if (!intobj)
3066 return -1;
3067 intval = PyLong_AsLong(intobj);
3068 Py_DECREF(intobj);
3069 if (intval == -1 && PyErr_Occurred())
3070 return -1;
3071 *sec = intval;
3072 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
3073 if (*usec < 0)
3074 /* If rounding gave us a negative number,
3075 truncate. */
3076 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00003077 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00003078 }
3079 intval = PyLong_AsLong(t);
3080 if (intval == -1 && PyErr_Occurred())
3081 return -1;
3082 *sec = intval;
3083 *usec = 0;
3084 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003085}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003086
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003087PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00003088"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00003089utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00003090Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003091second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003092
Barry Warsaw53699e91996-12-10 23:23:01 +00003093static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003094posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003095{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003096#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003097 PyObject *arg;
3098 PyUnicodeObject *obwpath;
3099 wchar_t *wpath = NULL;
3100 PyObject *oapath;
3101 char *apath;
3102 HANDLE hFile;
3103 long atimesec, mtimesec, ausec, musec;
3104 FILETIME atime, mtime;
3105 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003106
Victor Stinner8c62be82010-05-06 00:08:46 +00003107 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3108 wpath = PyUnicode_AS_UNICODE(obwpath);
3109 Py_BEGIN_ALLOW_THREADS
3110 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
3111 NULL, OPEN_EXISTING,
3112 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3113 Py_END_ALLOW_THREADS
3114 if (hFile == INVALID_HANDLE_VALUE)
3115 return win32_error_unicode("utime", wpath);
3116 } else
3117 /* Drop the argument parsing error as narrow strings
3118 are also valid. */
3119 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003120
Victor Stinner8c62be82010-05-06 00:08:46 +00003121 if (!wpath) {
3122 if (!PyArg_ParseTuple(args, "O&O:utime",
3123 PyUnicode_FSConverter, &oapath, &arg))
3124 return NULL;
3125 apath = PyBytes_AsString(oapath);
3126 Py_BEGIN_ALLOW_THREADS
3127 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
3128 NULL, OPEN_EXISTING,
3129 FILE_FLAG_BACKUP_SEMANTICS, NULL);
3130 Py_END_ALLOW_THREADS
3131 if (hFile == INVALID_HANDLE_VALUE) {
3132 win32_error("utime", apath);
3133 Py_DECREF(oapath);
3134 return NULL;
3135 }
3136 Py_DECREF(oapath);
3137 }
3138
3139 if (arg == Py_None) {
3140 SYSTEMTIME now;
3141 GetSystemTime(&now);
3142 if (!SystemTimeToFileTime(&now, &mtime) ||
3143 !SystemTimeToFileTime(&now, &atime)) {
3144 win32_error("utime", NULL);
3145 goto done;
3146 }
3147 }
3148 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3149 PyErr_SetString(PyExc_TypeError,
3150 "utime() arg 2 must be a tuple (atime, mtime)");
3151 goto done;
3152 }
3153 else {
3154 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3155 &atimesec, &ausec) == -1)
3156 goto done;
3157 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
3158 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3159 &mtimesec, &musec) == -1)
3160 goto done;
3161 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
3162 }
3163 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
3164 /* Avoid putting the file name into the error here,
3165 as that may confuse the user into believing that
3166 something is wrong with the file, when it also
3167 could be the time stamp that gives a problem. */
3168 win32_error("utime", NULL);
3169 }
3170 Py_INCREF(Py_None);
3171 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003172done:
Victor Stinner8c62be82010-05-06 00:08:46 +00003173 CloseHandle(hFile);
3174 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003175#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003176
Victor Stinner8c62be82010-05-06 00:08:46 +00003177 PyObject *opath;
3178 char *path;
3179 long atime, mtime, ausec, musec;
3180 int res;
3181 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003182
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003183#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00003184 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003185#define ATIME buf[0].tv_sec
3186#define MTIME buf[1].tv_sec
3187#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00003188/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003190#define ATIME buf.actime
3191#define MTIME buf.modtime
3192#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003193#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003195#define ATIME buf[0]
3196#define MTIME buf[1]
3197#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003198#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003199
Mark Hammond817c9292003-12-03 01:22:38 +00003200
Victor Stinner8c62be82010-05-06 00:08:46 +00003201 if (!PyArg_ParseTuple(args, "O&O:utime",
3202 PyUnicode_FSConverter, &opath, &arg))
3203 return NULL;
3204 path = PyBytes_AsString(opath);
3205 if (arg == Py_None) {
3206 /* optional time values not given */
3207 Py_BEGIN_ALLOW_THREADS
3208 res = utime(path, NULL);
3209 Py_END_ALLOW_THREADS
3210 }
3211 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
3212 PyErr_SetString(PyExc_TypeError,
3213 "utime() arg 2 must be a tuple (atime, mtime)");
3214 Py_DECREF(opath);
3215 return NULL;
3216 }
3217 else {
3218 if (extract_time(PyTuple_GET_ITEM(arg, 0),
3219 &atime, &ausec) == -1) {
3220 Py_DECREF(opath);
3221 return NULL;
3222 }
3223 if (extract_time(PyTuple_GET_ITEM(arg, 1),
3224 &mtime, &musec) == -1) {
3225 Py_DECREF(opath);
3226 return NULL;
3227 }
3228 ATIME = atime;
3229 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003230#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00003231 buf[0].tv_usec = ausec;
3232 buf[1].tv_usec = musec;
3233 Py_BEGIN_ALLOW_THREADS
3234 res = utimes(path, buf);
3235 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00003236#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003237 Py_BEGIN_ALLOW_THREADS
3238 res = utime(path, UTIME_ARG);
3239 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00003240#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00003241 }
3242 if (res < 0) {
3243 return posix_error_with_allocated_filename(opath);
3244 }
3245 Py_DECREF(opath);
3246 Py_INCREF(Py_None);
3247 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003248#undef UTIME_ARG
3249#undef ATIME
3250#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003251#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003252}
3253
Guido van Rossum85e3b011991-06-03 12:42:10 +00003254
Guido van Rossum3b066191991-06-04 19:40:25 +00003255/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003257PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003258"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003259Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003260
Barry Warsaw53699e91996-12-10 23:23:01 +00003261static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003262posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003263{
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 int sts;
3265 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
3266 return NULL;
3267 _exit(sts);
3268 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003269}
3270
Martin v. Löwis114619e2002-10-07 06:44:21 +00003271#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
3272static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00003273free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00003274{
Victor Stinner8c62be82010-05-06 00:08:46 +00003275 Py_ssize_t i;
3276 for (i = 0; i < count; i++)
3277 PyMem_Free(array[i]);
3278 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003279}
Martin v. Löwis011e8422009-05-05 04:43:17 +00003280
Antoine Pitrou69f71142009-05-24 21:25:49 +00003281static
Martin v. Löwis011e8422009-05-05 04:43:17 +00003282int fsconvert_strdup(PyObject *o, char**out)
3283{
Victor Stinner8c62be82010-05-06 00:08:46 +00003284 PyObject *bytes;
3285 Py_ssize_t size;
3286 if (!PyUnicode_FSConverter(o, &bytes))
3287 return 0;
3288 size = PyBytes_GET_SIZE(bytes);
3289 *out = PyMem_Malloc(size+1);
3290 if (!*out)
3291 return 0;
3292 memcpy(*out, PyBytes_AsString(bytes), size+1);
3293 Py_DECREF(bytes);
3294 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00003295}
Martin v. Löwis114619e2002-10-07 06:44:21 +00003296#endif
3297
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003298
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003299#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003300PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003301"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003302Execute an executable path with arguments, replacing current process.\n\
3303\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003304 path: path of executable file\n\
3305 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003306
Barry Warsaw53699e91996-12-10 23:23:01 +00003307static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003308posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003309{
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 PyObject *opath;
3311 char *path;
3312 PyObject *argv;
3313 char **argvlist;
3314 Py_ssize_t i, argc;
3315 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003316
Victor Stinner8c62be82010-05-06 00:08:46 +00003317 /* execv has two arguments: (path, argv), where
3318 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003319
Victor Stinner8c62be82010-05-06 00:08:46 +00003320 if (!PyArg_ParseTuple(args, "O&O:execv",
3321 PyUnicode_FSConverter,
3322 &opath, &argv))
3323 return NULL;
3324 path = PyBytes_AsString(opath);
3325 if (PyList_Check(argv)) {
3326 argc = PyList_Size(argv);
3327 getitem = PyList_GetItem;
3328 }
3329 else if (PyTuple_Check(argv)) {
3330 argc = PyTuple_Size(argv);
3331 getitem = PyTuple_GetItem;
3332 }
3333 else {
3334 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3335 Py_DECREF(opath);
3336 return NULL;
3337 }
3338 if (argc < 1) {
3339 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3340 Py_DECREF(opath);
3341 return NULL;
3342 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003343
Victor Stinner8c62be82010-05-06 00:08:46 +00003344 argvlist = PyMem_NEW(char *, argc+1);
3345 if (argvlist == NULL) {
3346 Py_DECREF(opath);
3347 return PyErr_NoMemory();
3348 }
3349 for (i = 0; i < argc; i++) {
3350 if (!fsconvert_strdup((*getitem)(argv, i),
3351 &argvlist[i])) {
3352 free_string_array(argvlist, i);
3353 PyErr_SetString(PyExc_TypeError,
3354 "execv() arg 2 must contain only strings");
3355 Py_DECREF(opath);
3356 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003357
Victor Stinner8c62be82010-05-06 00:08:46 +00003358 }
3359 }
3360 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003361
Victor Stinner8c62be82010-05-06 00:08:46 +00003362 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003363
Victor Stinner8c62be82010-05-06 00:08:46 +00003364 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003365
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 free_string_array(argvlist, argc);
3367 Py_DECREF(opath);
3368 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003369}
3370
Victor Stinner13bb71c2010-04-23 21:41:56 +00003371static char**
3372parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3373{
Victor Stinner8c62be82010-05-06 00:08:46 +00003374 char **envlist;
3375 Py_ssize_t i, pos, envc;
3376 PyObject *keys=NULL, *vals=NULL;
3377 PyObject *key, *val, *key2, *val2;
3378 char *p, *k, *v;
3379 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003380
Victor Stinner8c62be82010-05-06 00:08:46 +00003381 i = PyMapping_Size(env);
3382 if (i < 0)
3383 return NULL;
3384 envlist = PyMem_NEW(char *, i + 1);
3385 if (envlist == NULL) {
3386 PyErr_NoMemory();
3387 return NULL;
3388 }
3389 envc = 0;
3390 keys = PyMapping_Keys(env);
3391 vals = PyMapping_Values(env);
3392 if (!keys || !vals)
3393 goto error;
3394 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3395 PyErr_Format(PyExc_TypeError,
3396 "env.keys() or env.values() is not a list");
3397 goto error;
3398 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003399
Victor Stinner8c62be82010-05-06 00:08:46 +00003400 for (pos = 0; pos < i; pos++) {
3401 key = PyList_GetItem(keys, pos);
3402 val = PyList_GetItem(vals, pos);
3403 if (!key || !val)
3404 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003405
Victor Stinner8c62be82010-05-06 00:08:46 +00003406 if (PyUnicode_FSConverter(key, &key2) == 0)
3407 goto error;
3408 if (PyUnicode_FSConverter(val, &val2) == 0) {
3409 Py_DECREF(key2);
3410 goto error;
3411 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003412
3413#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003414 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3415 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003416#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003417 k = PyBytes_AsString(key2);
3418 v = PyBytes_AsString(val2);
3419 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003420
Victor Stinner8c62be82010-05-06 00:08:46 +00003421 p = PyMem_NEW(char, len);
3422 if (p == NULL) {
3423 PyErr_NoMemory();
3424 Py_DECREF(key2);
3425 Py_DECREF(val2);
3426 goto error;
3427 }
3428 PyOS_snprintf(p, len, "%s=%s", k, v);
3429 envlist[envc++] = p;
3430 Py_DECREF(key2);
3431 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003432#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003433 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003434#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003435 }
3436 Py_DECREF(vals);
3437 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003438
Victor Stinner8c62be82010-05-06 00:08:46 +00003439 envlist[envc] = 0;
3440 *envc_ptr = envc;
3441 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003442
3443error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003444 Py_XDECREF(keys);
3445 Py_XDECREF(vals);
3446 while (--envc >= 0)
3447 PyMem_DEL(envlist[envc]);
3448 PyMem_DEL(envlist);
3449 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003450}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003452PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003453"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003454Execute a path with arguments and environment, replacing current process.\n\
3455\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003456 path: path of executable file\n\
3457 args: tuple or list of arguments\n\
3458 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003459
Barry Warsaw53699e91996-12-10 23:23:01 +00003460static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003461posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003462{
Victor Stinner8c62be82010-05-06 00:08:46 +00003463 PyObject *opath;
3464 char *path;
3465 PyObject *argv, *env;
3466 char **argvlist;
3467 char **envlist;
3468 Py_ssize_t i, argc, envc;
3469 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3470 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003471
Victor Stinner8c62be82010-05-06 00:08:46 +00003472 /* execve has three arguments: (path, argv, env), where
3473 argv is a list or tuple of strings and env is a dictionary
3474 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003475
Victor Stinner8c62be82010-05-06 00:08:46 +00003476 if (!PyArg_ParseTuple(args, "O&OO:execve",
3477 PyUnicode_FSConverter,
3478 &opath, &argv, &env))
3479 return NULL;
3480 path = PyBytes_AsString(opath);
3481 if (PyList_Check(argv)) {
3482 argc = PyList_Size(argv);
3483 getitem = PyList_GetItem;
3484 }
3485 else if (PyTuple_Check(argv)) {
3486 argc = PyTuple_Size(argv);
3487 getitem = PyTuple_GetItem;
3488 }
3489 else {
3490 PyErr_SetString(PyExc_TypeError,
3491 "execve() arg 2 must be a tuple or list");
3492 goto fail_0;
3493 }
3494 if (!PyMapping_Check(env)) {
3495 PyErr_SetString(PyExc_TypeError,
3496 "execve() arg 3 must be a mapping object");
3497 goto fail_0;
3498 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003499
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 argvlist = PyMem_NEW(char *, argc+1);
3501 if (argvlist == NULL) {
3502 PyErr_NoMemory();
3503 goto fail_0;
3504 }
3505 for (i = 0; i < argc; i++) {
3506 if (!fsconvert_strdup((*getitem)(argv, i),
3507 &argvlist[i]))
3508 {
3509 lastarg = i;
3510 goto fail_1;
3511 }
3512 }
3513 lastarg = argc;
3514 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003515
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 envlist = parse_envlist(env, &envc);
3517 if (envlist == NULL)
3518 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003519
Victor Stinner8c62be82010-05-06 00:08:46 +00003520 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003521
Victor Stinner8c62be82010-05-06 00:08:46 +00003522 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003523
Victor Stinner8c62be82010-05-06 00:08:46 +00003524 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003525
Victor Stinner8c62be82010-05-06 00:08:46 +00003526 while (--envc >= 0)
3527 PyMem_DEL(envlist[envc]);
3528 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003529 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003531 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003532 Py_DECREF(opath);
3533 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003534}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003535#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003536
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003537
Guido van Rossuma1065681999-01-25 23:20:23 +00003538#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003539PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003540"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003541Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003542\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 mode: mode of process creation\n\
3544 path: path of executable file\n\
3545 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003546
3547static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003548posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003549{
Victor Stinner8c62be82010-05-06 00:08:46 +00003550 PyObject *opath;
3551 char *path;
3552 PyObject *argv;
3553 char **argvlist;
3554 int mode, i;
3555 Py_ssize_t argc;
3556 Py_intptr_t spawnval;
3557 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003558
Victor Stinner8c62be82010-05-06 00:08:46 +00003559 /* spawnv has three arguments: (mode, path, argv), where
3560 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003561
Victor Stinner8c62be82010-05-06 00:08:46 +00003562 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3563 PyUnicode_FSConverter,
3564 &opath, &argv))
3565 return NULL;
3566 path = PyBytes_AsString(opath);
3567 if (PyList_Check(argv)) {
3568 argc = PyList_Size(argv);
3569 getitem = PyList_GetItem;
3570 }
3571 else if (PyTuple_Check(argv)) {
3572 argc = PyTuple_Size(argv);
3573 getitem = PyTuple_GetItem;
3574 }
3575 else {
3576 PyErr_SetString(PyExc_TypeError,
3577 "spawnv() arg 2 must be a tuple or list");
3578 Py_DECREF(opath);
3579 return NULL;
3580 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003581
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 argvlist = PyMem_NEW(char *, argc+1);
3583 if (argvlist == NULL) {
3584 Py_DECREF(opath);
3585 return PyErr_NoMemory();
3586 }
3587 for (i = 0; i < argc; i++) {
3588 if (!fsconvert_strdup((*getitem)(argv, i),
3589 &argvlist[i])) {
3590 free_string_array(argvlist, i);
3591 PyErr_SetString(
3592 PyExc_TypeError,
3593 "spawnv() arg 2 must contain only strings");
3594 Py_DECREF(opath);
3595 return NULL;
3596 }
3597 }
3598 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003599
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003600#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 Py_BEGIN_ALLOW_THREADS
3602 spawnval = spawnv(mode, path, argvlist);
3603 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003604#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 if (mode == _OLD_P_OVERLAY)
3606 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003607
Victor Stinner8c62be82010-05-06 00:08:46 +00003608 Py_BEGIN_ALLOW_THREADS
3609 spawnval = _spawnv(mode, path, argvlist);
3610 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003611#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003612
Victor Stinner8c62be82010-05-06 00:08:46 +00003613 free_string_array(argvlist, argc);
3614 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003615
Victor Stinner8c62be82010-05-06 00:08:46 +00003616 if (spawnval == -1)
3617 return posix_error();
3618 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003619#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003620 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003621#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003622 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003623#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003624}
3625
3626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003627PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003628"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003629Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003630\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003631 mode: mode of process creation\n\
3632 path: path of executable file\n\
3633 args: tuple or list of arguments\n\
3634 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003635
3636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003637posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003638{
Victor Stinner8c62be82010-05-06 00:08:46 +00003639 PyObject *opath;
3640 char *path;
3641 PyObject *argv, *env;
3642 char **argvlist;
3643 char **envlist;
3644 PyObject *res = NULL;
3645 int mode, envc;
3646 Py_ssize_t argc, i;
3647 Py_intptr_t spawnval;
3648 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3649 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003650
Victor Stinner8c62be82010-05-06 00:08:46 +00003651 /* spawnve has four arguments: (mode, path, argv, env), where
3652 argv is a list or tuple of strings and env is a dictionary
3653 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003654
Victor Stinner8c62be82010-05-06 00:08:46 +00003655 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3656 PyUnicode_FSConverter,
3657 &opath, &argv, &env))
3658 return NULL;
3659 path = PyBytes_AsString(opath);
3660 if (PyList_Check(argv)) {
3661 argc = PyList_Size(argv);
3662 getitem = PyList_GetItem;
3663 }
3664 else if (PyTuple_Check(argv)) {
3665 argc = PyTuple_Size(argv);
3666 getitem = PyTuple_GetItem;
3667 }
3668 else {
3669 PyErr_SetString(PyExc_TypeError,
3670 "spawnve() arg 2 must be a tuple or list");
3671 goto fail_0;
3672 }
3673 if (!PyMapping_Check(env)) {
3674 PyErr_SetString(PyExc_TypeError,
3675 "spawnve() arg 3 must be a mapping object");
3676 goto fail_0;
3677 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003678
Victor Stinner8c62be82010-05-06 00:08:46 +00003679 argvlist = PyMem_NEW(char *, argc+1);
3680 if (argvlist == NULL) {
3681 PyErr_NoMemory();
3682 goto fail_0;
3683 }
3684 for (i = 0; i < argc; i++) {
3685 if (!fsconvert_strdup((*getitem)(argv, i),
3686 &argvlist[i]))
3687 {
3688 lastarg = i;
3689 goto fail_1;
3690 }
3691 }
3692 lastarg = argc;
3693 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003694
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 envlist = parse_envlist(env, &envc);
3696 if (envlist == NULL)
3697 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003698
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003699#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 Py_BEGIN_ALLOW_THREADS
3701 spawnval = spawnve(mode, path, argvlist, envlist);
3702 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003703#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003704 if (mode == _OLD_P_OVERLAY)
3705 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003706
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 Py_BEGIN_ALLOW_THREADS
3708 spawnval = _spawnve(mode, path, argvlist, envlist);
3709 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003710#endif
Tim Peters25059d32001-12-07 20:35:43 +00003711
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 if (spawnval == -1)
3713 (void) posix_error();
3714 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003715#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003716 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003717#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003718 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003719#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003720
Victor Stinner8c62be82010-05-06 00:08:46 +00003721 while (--envc >= 0)
3722 PyMem_DEL(envlist[envc]);
3723 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003724 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003725 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003726 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003727 Py_DECREF(opath);
3728 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003729}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003730
3731/* OS/2 supports spawnvp & spawnvpe natively */
3732#if defined(PYOS_OS2)
3733PyDoc_STRVAR(posix_spawnvp__doc__,
3734"spawnvp(mode, file, args)\n\n\
3735Execute the program 'file' in a new process, using the environment\n\
3736search path to find the file.\n\
3737\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 mode: mode of process creation\n\
3739 file: executable file name\n\
3740 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003741
3742static PyObject *
3743posix_spawnvp(PyObject *self, PyObject *args)
3744{
Victor Stinner8c62be82010-05-06 00:08:46 +00003745 PyObject *opath;
3746 char *path;
3747 PyObject *argv;
3748 char **argvlist;
3749 int mode, i, argc;
3750 Py_intptr_t spawnval;
3751 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003752
Victor Stinner8c62be82010-05-06 00:08:46 +00003753 /* spawnvp has three arguments: (mode, path, argv), where
3754 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003755
Victor Stinner8c62be82010-05-06 00:08:46 +00003756 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3757 PyUnicode_FSConverter,
3758 &opath, &argv))
3759 return NULL;
3760 path = PyBytes_AsString(opath);
3761 if (PyList_Check(argv)) {
3762 argc = PyList_Size(argv);
3763 getitem = PyList_GetItem;
3764 }
3765 else if (PyTuple_Check(argv)) {
3766 argc = PyTuple_Size(argv);
3767 getitem = PyTuple_GetItem;
3768 }
3769 else {
3770 PyErr_SetString(PyExc_TypeError,
3771 "spawnvp() arg 2 must be a tuple or list");
3772 Py_DECREF(opath);
3773 return NULL;
3774 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003775
Victor Stinner8c62be82010-05-06 00:08:46 +00003776 argvlist = PyMem_NEW(char *, argc+1);
3777 if (argvlist == NULL) {
3778 Py_DECREF(opath);
3779 return PyErr_NoMemory();
3780 }
3781 for (i = 0; i < argc; i++) {
3782 if (!fsconvert_strdup((*getitem)(argv, i),
3783 &argvlist[i])) {
3784 free_string_array(argvlist, i);
3785 PyErr_SetString(
3786 PyExc_TypeError,
3787 "spawnvp() arg 2 must contain only strings");
3788 Py_DECREF(opath);
3789 return NULL;
3790 }
3791 }
3792 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003793
Victor Stinner8c62be82010-05-06 00:08:46 +00003794 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003795#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003796 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003797#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003799#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003800 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003801
Victor Stinner8c62be82010-05-06 00:08:46 +00003802 free_string_array(argvlist, argc);
3803 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003804
Victor Stinner8c62be82010-05-06 00:08:46 +00003805 if (spawnval == -1)
3806 return posix_error();
3807 else
3808 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003809}
3810
3811
3812PyDoc_STRVAR(posix_spawnvpe__doc__,
3813"spawnvpe(mode, file, args, env)\n\n\
3814Execute the program 'file' in a new process, using the environment\n\
3815search path to find the file.\n\
3816\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003817 mode: mode of process creation\n\
3818 file: executable file name\n\
3819 args: tuple or list of arguments\n\
3820 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003821
3822static PyObject *
3823posix_spawnvpe(PyObject *self, PyObject *args)
3824{
Victor Stinner8c62be82010-05-06 00:08:46 +00003825 PyObject *opath
3826 char *path;
3827 PyObject *argv, *env;
3828 char **argvlist;
3829 char **envlist;
3830 PyObject *res=NULL;
3831 int mode, i, argc, envc;
3832 Py_intptr_t spawnval;
3833 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3834 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003835
Victor Stinner8c62be82010-05-06 00:08:46 +00003836 /* spawnvpe has four arguments: (mode, path, argv, env), where
3837 argv is a list or tuple of strings and env is a dictionary
3838 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003839
Victor Stinner8c62be82010-05-06 00:08:46 +00003840 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3841 PyUnicode_FSConverter,
3842 &opath, &argv, &env))
3843 return NULL;
3844 path = PyBytes_AsString(opath);
3845 if (PyList_Check(argv)) {
3846 argc = PyList_Size(argv);
3847 getitem = PyList_GetItem;
3848 }
3849 else if (PyTuple_Check(argv)) {
3850 argc = PyTuple_Size(argv);
3851 getitem = PyTuple_GetItem;
3852 }
3853 else {
3854 PyErr_SetString(PyExc_TypeError,
3855 "spawnvpe() arg 2 must be a tuple or list");
3856 goto fail_0;
3857 }
3858 if (!PyMapping_Check(env)) {
3859 PyErr_SetString(PyExc_TypeError,
3860 "spawnvpe() arg 3 must be a mapping object");
3861 goto fail_0;
3862 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003863
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 argvlist = PyMem_NEW(char *, argc+1);
3865 if (argvlist == NULL) {
3866 PyErr_NoMemory();
3867 goto fail_0;
3868 }
3869 for (i = 0; i < argc; i++) {
3870 if (!fsconvert_strdup((*getitem)(argv, i),
3871 &argvlist[i]))
3872 {
3873 lastarg = i;
3874 goto fail_1;
3875 }
3876 }
3877 lastarg = argc;
3878 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003879
Victor Stinner8c62be82010-05-06 00:08:46 +00003880 envlist = parse_envlist(env, &envc);
3881 if (envlist == NULL)
3882 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003883
Victor Stinner8c62be82010-05-06 00:08:46 +00003884 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003885#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003886 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003887#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003888 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003889#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003890 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003891
Victor Stinner8c62be82010-05-06 00:08:46 +00003892 if (spawnval == -1)
3893 (void) posix_error();
3894 else
3895 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003896
Victor Stinner8c62be82010-05-06 00:08:46 +00003897 while (--envc >= 0)
3898 PyMem_DEL(envlist[envc]);
3899 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003900 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003901 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003902 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003903 Py_DECREF(opath);
3904 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003905}
3906#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003907#endif /* HAVE_SPAWNV */
3908
3909
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003910#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003911PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003912"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003913Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3914\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003915Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003916
3917static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003918posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003919{
Victor Stinner8c62be82010-05-06 00:08:46 +00003920 pid_t pid;
3921 int result = 0;
3922 _PyImport_AcquireLock();
3923 pid = fork1();
3924 if (pid == 0) {
3925 /* child: this clobbers and resets the import lock. */
3926 PyOS_AfterFork();
3927 } else {
3928 /* parent: release the import lock. */
3929 result = _PyImport_ReleaseLock();
3930 }
3931 if (pid == -1)
3932 return posix_error();
3933 if (result < 0) {
3934 /* Don't clobber the OSError if the fork failed. */
3935 PyErr_SetString(PyExc_RuntimeError,
3936 "not holding the import lock");
3937 return NULL;
3938 }
3939 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003940}
3941#endif
3942
3943
Guido van Rossumad0ee831995-03-01 10:34:45 +00003944#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003945PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003946"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003947Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003948Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003949
Barry Warsaw53699e91996-12-10 23:23:01 +00003950static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003951posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003952{
Victor Stinner8c62be82010-05-06 00:08:46 +00003953 pid_t pid;
3954 int result = 0;
3955 _PyImport_AcquireLock();
3956 pid = fork();
3957 if (pid == 0) {
3958 /* child: this clobbers and resets the import lock. */
3959 PyOS_AfterFork();
3960 } else {
3961 /* parent: release the import lock. */
3962 result = _PyImport_ReleaseLock();
3963 }
3964 if (pid == -1)
3965 return posix_error();
3966 if (result < 0) {
3967 /* Don't clobber the OSError if the fork failed. */
3968 PyErr_SetString(PyExc_RuntimeError,
3969 "not holding the import lock");
3970 return NULL;
3971 }
3972 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003973}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003974#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003975
Neal Norwitzb59798b2003-03-21 01:43:31 +00003976/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003977/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3978#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003979#define DEV_PTY_FILE "/dev/ptc"
3980#define HAVE_DEV_PTMX
3981#else
3982#define DEV_PTY_FILE "/dev/ptmx"
3983#endif
3984
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003985#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003986#ifdef HAVE_PTY_H
3987#include <pty.h>
3988#else
3989#ifdef HAVE_LIBUTIL_H
3990#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003991#else
3992#ifdef HAVE_UTIL_H
3993#include <util.h>
3994#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003995#endif /* HAVE_LIBUTIL_H */
3996#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003997#ifdef HAVE_STROPTS_H
3998#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003999#endif
4000#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004001
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004002#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004003PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004004"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004005Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004006
4007static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004008posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004009{
Victor Stinner8c62be82010-05-06 00:08:46 +00004010 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004011#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004012 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00004013#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004014#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004016#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00004017 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004018#endif
4019#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00004020
Thomas Wouters70c21a12000-07-14 14:28:33 +00004021#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00004022 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
4023 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004024#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004025 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
4026 if (slave_name == NULL)
4027 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00004028
Victor Stinner8c62be82010-05-06 00:08:46 +00004029 slave_fd = open(slave_name, O_RDWR);
4030 if (slave_fd < 0)
4031 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004032#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004033 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
4034 if (master_fd < 0)
4035 return posix_error();
4036 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
4037 /* change permission of slave */
4038 if (grantpt(master_fd) < 0) {
4039 PyOS_setsig(SIGCHLD, sig_saved);
4040 return posix_error();
4041 }
4042 /* unlock slave */
4043 if (unlockpt(master_fd) < 0) {
4044 PyOS_setsig(SIGCHLD, sig_saved);
4045 return posix_error();
4046 }
4047 PyOS_setsig(SIGCHLD, sig_saved);
4048 slave_name = ptsname(master_fd); /* get name of slave */
4049 if (slave_name == NULL)
4050 return posix_error();
4051 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
4052 if (slave_fd < 0)
4053 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00004054#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00004055 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
4056 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00004057#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00004058 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00004059#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004060#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00004061#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00004062
Victor Stinner8c62be82010-05-06 00:08:46 +00004063 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00004064
Fred Drake8cef4cf2000-06-28 16:40:38 +00004065}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00004066#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00004067
4068#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004069PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004070"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00004071Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
4072Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004073To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00004074
4075static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004076posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00004077{
Victor Stinner8c62be82010-05-06 00:08:46 +00004078 int master_fd = -1, result = 0;
4079 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00004080
Victor Stinner8c62be82010-05-06 00:08:46 +00004081 _PyImport_AcquireLock();
4082 pid = forkpty(&master_fd, NULL, NULL, NULL);
4083 if (pid == 0) {
4084 /* child: this clobbers and resets the import lock. */
4085 PyOS_AfterFork();
4086 } else {
4087 /* parent: release the import lock. */
4088 result = _PyImport_ReleaseLock();
4089 }
4090 if (pid == -1)
4091 return posix_error();
4092 if (result < 0) {
4093 /* Don't clobber the OSError if the fork failed. */
4094 PyErr_SetString(PyExc_RuntimeError,
4095 "not holding the import lock");
4096 return NULL;
4097 }
4098 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00004099}
4100#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004101
Guido van Rossumad0ee831995-03-01 10:34:45 +00004102#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004103PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004104"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004105Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004106
Barry Warsaw53699e91996-12-10 23:23:01 +00004107static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004108posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004109{
Victor Stinner8c62be82010-05-06 00:08:46 +00004110 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004111}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004112#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004113
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004114
Guido van Rossumad0ee831995-03-01 10:34:45 +00004115#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004116PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004117"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004118Return the current process's effective user 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_geteuid(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)geteuid());
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_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004129PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004130"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004131Return the current process's group 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_getgid(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)getgid());
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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004141PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004142"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004143Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004144
Barry Warsaw53699e91996-12-10 23:23:01 +00004145static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004146posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004147{
Victor Stinner8c62be82010-05-06 00:08:46 +00004148 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004149}
4150
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004151
Fred Drakec9680921999-12-13 16:37:25 +00004152#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004153PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004154"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004155Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00004156
4157static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004158posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00004159{
4160 PyObject *result = NULL;
4161
Fred Drakec9680921999-12-13 16:37:25 +00004162#ifdef NGROUPS_MAX
4163#define MAX_GROUPS NGROUPS_MAX
4164#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004165 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00004166#define MAX_GROUPS 64
4167#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004168 gid_t grouplist[MAX_GROUPS];
4169 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004170
Victor Stinner8c62be82010-05-06 00:08:46 +00004171 n = getgroups(MAX_GROUPS, grouplist);
4172 if (n < 0)
4173 posix_error();
4174 else {
4175 result = PyList_New(n);
4176 if (result != NULL) {
4177 int i;
4178 for (i = 0; i < n; ++i) {
4179 PyObject *o = PyLong_FromLong((long)grouplist[i]);
4180 if (o == NULL) {
4181 Py_DECREF(result);
4182 result = NULL;
4183 break;
Fred Drakec9680921999-12-13 16:37:25 +00004184 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004185 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004186 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004187 }
4188 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004189
Fred Drakec9680921999-12-13 16:37:25 +00004190 return result;
4191}
4192#endif
4193
Antoine Pitroub7572f02009-12-02 20:46:48 +00004194#ifdef HAVE_INITGROUPS
4195PyDoc_STRVAR(posix_initgroups__doc__,
4196"initgroups(username, gid) -> None\n\n\
4197Call the system initgroups() to initialize the group access list with all of\n\
4198the groups of which the specified username is a member, plus the specified\n\
4199group id.");
4200
4201static PyObject *
4202posix_initgroups(PyObject *self, PyObject *args)
4203{
Victor Stinner8c62be82010-05-06 00:08:46 +00004204 char *username;
4205 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004206
Victor Stinner8c62be82010-05-06 00:08:46 +00004207 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
4208 return NULL;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004209
Victor Stinner8c62be82010-05-06 00:08:46 +00004210 if (initgroups(username, (gid_t) gid) == -1)
4211 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004212
Victor Stinner8c62be82010-05-06 00:08:46 +00004213 Py_INCREF(Py_None);
4214 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004215}
4216#endif
4217
Martin v. Löwis606edc12002-06-13 21:09:11 +00004218#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004219PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004220"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004221Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004222
4223static PyObject *
4224posix_getpgid(PyObject *self, PyObject *args)
4225{
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 pid_t pid, pgid;
4227 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4228 return NULL;
4229 pgid = getpgid(pid);
4230 if (pgid < 0)
4231 return posix_error();
4232 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004233}
4234#endif /* HAVE_GETPGID */
4235
4236
Guido van Rossumb6775db1994-08-01 11:34:53 +00004237#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004238PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004239"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004240Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004241
Barry Warsaw53699e91996-12-10 23:23:01 +00004242static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004243posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004244{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004245#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004246 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004247#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004248 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004249#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004250}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004251#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004253
Guido van Rossumb6775db1994-08-01 11:34:53 +00004254#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004255PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004256"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004257Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004258
Barry Warsaw53699e91996-12-10 23:23:01 +00004259static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004260posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004261{
Guido van Rossum64933891994-10-20 21:56:42 +00004262#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004263 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004264#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004265 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004266#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004267 return posix_error();
4268 Py_INCREF(Py_None);
4269 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004270}
4271
Guido van Rossumb6775db1994-08-01 11:34:53 +00004272#endif /* HAVE_SETPGRP */
4273
Guido van Rossumad0ee831995-03-01 10:34:45 +00004274#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004275PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004276"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004277Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004278
Barry Warsaw53699e91996-12-10 23:23:01 +00004279static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004280posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004281{
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004283}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004284#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004285
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004286
Fred Drake12c6e2d1999-12-14 21:25:03 +00004287#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004288PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004289"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004290Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004291
4292static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004293posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004294{
Victor Stinner8c62be82010-05-06 00:08:46 +00004295 PyObject *result = NULL;
4296 char *name;
4297 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004298
Victor Stinner8c62be82010-05-06 00:08:46 +00004299 errno = 0;
4300 name = getlogin();
4301 if (name == NULL) {
4302 if (errno)
4303 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004304 else
Victor Stinner8c62be82010-05-06 00:08:46 +00004305 PyErr_SetString(PyExc_OSError,
4306 "unable to determine login name");
4307 }
4308 else
4309 result = PyUnicode_FromString(name);
4310 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004311
Fred Drake12c6e2d1999-12-14 21:25:03 +00004312 return result;
4313}
4314#endif
4315
Guido van Rossumad0ee831995-03-01 10:34:45 +00004316#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004317PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004318"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004319Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004320
Barry Warsaw53699e91996-12-10 23:23:01 +00004321static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004322posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004323{
Victor Stinner8c62be82010-05-06 00:08:46 +00004324 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004325}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004326#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004327
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004328
Guido van Rossumad0ee831995-03-01 10:34:45 +00004329#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004330PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004331"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004332Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004333
Barry Warsaw53699e91996-12-10 23:23:01 +00004334static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004335posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004336{
Victor Stinner8c62be82010-05-06 00:08:46 +00004337 pid_t pid;
4338 int sig;
4339 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4340 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004341#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004342 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4343 APIRET rc;
4344 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004345 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004346
4347 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4348 APIRET rc;
4349 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004350 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004351
4352 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004353 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004354#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004355 if (kill(pid, sig) == -1)
4356 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004357#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004358 Py_INCREF(Py_None);
4359 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004360}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004361#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004362
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004363#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004364PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004365"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004366Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004367
4368static PyObject *
4369posix_killpg(PyObject *self, PyObject *args)
4370{
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 int sig;
4372 pid_t pgid;
4373 /* XXX some man pages make the `pgid` parameter an int, others
4374 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4375 take the same type. Moreover, pid_t is always at least as wide as
4376 int (else compilation of this module fails), which is safe. */
4377 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4378 return NULL;
4379 if (killpg(pgid, sig) == -1)
4380 return posix_error();
4381 Py_INCREF(Py_None);
4382 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004383}
4384#endif
4385
Brian Curtineb24d742010-04-12 17:16:38 +00004386#ifdef MS_WINDOWS
4387PyDoc_STRVAR(win32_kill__doc__,
4388"kill(pid, sig)\n\n\
4389Kill a process with a signal.");
4390
4391static PyObject *
4392win32_kill(PyObject *self, PyObject *args)
4393{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004394 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004395 DWORD pid, sig, err;
4396 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004397
Victor Stinner8c62be82010-05-06 00:08:46 +00004398 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4399 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004400
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 /* Console processes which share a common console can be sent CTRL+C or
4402 CTRL+BREAK events, provided they handle said events. */
4403 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4404 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4405 err = GetLastError();
4406 PyErr_SetFromWindowsErr(err);
4407 }
4408 else
4409 Py_RETURN_NONE;
4410 }
Brian Curtineb24d742010-04-12 17:16:38 +00004411
Victor Stinner8c62be82010-05-06 00:08:46 +00004412 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4413 attempt to open and terminate the process. */
4414 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4415 if (handle == NULL) {
4416 err = GetLastError();
4417 return PyErr_SetFromWindowsErr(err);
4418 }
Brian Curtineb24d742010-04-12 17:16:38 +00004419
Victor Stinner8c62be82010-05-06 00:08:46 +00004420 if (TerminateProcess(handle, sig) == 0) {
4421 err = GetLastError();
4422 result = PyErr_SetFromWindowsErr(err);
4423 } else {
4424 Py_INCREF(Py_None);
4425 result = Py_None;
4426 }
Brian Curtineb24d742010-04-12 17:16:38 +00004427
Victor Stinner8c62be82010-05-06 00:08:46 +00004428 CloseHandle(handle);
4429 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004430}
4431#endif /* MS_WINDOWS */
4432
Guido van Rossumc0125471996-06-28 18:55:32 +00004433#ifdef HAVE_PLOCK
4434
4435#ifdef HAVE_SYS_LOCK_H
4436#include <sys/lock.h>
4437#endif
4438
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004439PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004440"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004441Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004442
Barry Warsaw53699e91996-12-10 23:23:01 +00004443static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004444posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004445{
Victor Stinner8c62be82010-05-06 00:08:46 +00004446 int op;
4447 if (!PyArg_ParseTuple(args, "i:plock", &op))
4448 return NULL;
4449 if (plock(op) == -1)
4450 return posix_error();
4451 Py_INCREF(Py_None);
4452 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004453}
4454#endif
4455
Guido van Rossumb6775db1994-08-01 11:34:53 +00004456#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004457PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004458"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004459Set the current process's user id.");
4460
Barry Warsaw53699e91996-12-10 23:23:01 +00004461static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004462posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004463{
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 long uid_arg;
4465 uid_t uid;
4466 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4467 return NULL;
4468 uid = uid_arg;
4469 if (uid != uid_arg) {
4470 PyErr_SetString(PyExc_OverflowError, "user id too big");
4471 return NULL;
4472 }
4473 if (setuid(uid) < 0)
4474 return posix_error();
4475 Py_INCREF(Py_None);
4476 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004477}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004478#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004479
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004480
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004481#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004482PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004483"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004484Set the current process's effective user id.");
4485
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004486static PyObject *
4487posix_seteuid (PyObject *self, PyObject *args)
4488{
Victor Stinner8c62be82010-05-06 00:08:46 +00004489 long euid_arg;
4490 uid_t euid;
4491 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4492 return NULL;
4493 euid = euid_arg;
4494 if (euid != euid_arg) {
4495 PyErr_SetString(PyExc_OverflowError, "user id too big");
4496 return NULL;
4497 }
4498 if (seteuid(euid) < 0) {
4499 return posix_error();
4500 } else {
4501 Py_INCREF(Py_None);
4502 return Py_None;
4503 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004504}
4505#endif /* HAVE_SETEUID */
4506
4507#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004508PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004509"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004510Set the current process's effective group id.");
4511
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004512static PyObject *
4513posix_setegid (PyObject *self, PyObject *args)
4514{
Victor Stinner8c62be82010-05-06 00:08:46 +00004515 long egid_arg;
4516 gid_t egid;
4517 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4518 return NULL;
4519 egid = egid_arg;
4520 if (egid != egid_arg) {
4521 PyErr_SetString(PyExc_OverflowError, "group id too big");
4522 return NULL;
4523 }
4524 if (setegid(egid) < 0) {
4525 return posix_error();
4526 } else {
4527 Py_INCREF(Py_None);
4528 return Py_None;
4529 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004530}
4531#endif /* HAVE_SETEGID */
4532
4533#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004534PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004535"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004536Set the current process's real and effective user ids.");
4537
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004538static PyObject *
4539posix_setreuid (PyObject *self, PyObject *args)
4540{
Victor Stinner8c62be82010-05-06 00:08:46 +00004541 long ruid_arg, euid_arg;
4542 uid_t ruid, euid;
4543 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4544 return NULL;
4545 if (ruid_arg == -1)
4546 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4547 else
4548 ruid = ruid_arg; /* otherwise, assign from our long */
4549 if (euid_arg == -1)
4550 euid = (uid_t)-1;
4551 else
4552 euid = euid_arg;
4553 if ((euid_arg != -1 && euid != euid_arg) ||
4554 (ruid_arg != -1 && ruid != ruid_arg)) {
4555 PyErr_SetString(PyExc_OverflowError, "user id too big");
4556 return NULL;
4557 }
4558 if (setreuid(ruid, euid) < 0) {
4559 return posix_error();
4560 } else {
4561 Py_INCREF(Py_None);
4562 return Py_None;
4563 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004564}
4565#endif /* HAVE_SETREUID */
4566
4567#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004568PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004569"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004570Set the current process's real and effective group ids.");
4571
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004572static PyObject *
4573posix_setregid (PyObject *self, PyObject *args)
4574{
Victor Stinner8c62be82010-05-06 00:08:46 +00004575 long rgid_arg, egid_arg;
4576 gid_t rgid, egid;
4577 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4578 return NULL;
4579 if (rgid_arg == -1)
4580 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4581 else
4582 rgid = rgid_arg; /* otherwise, assign from our long */
4583 if (egid_arg == -1)
4584 egid = (gid_t)-1;
4585 else
4586 egid = egid_arg;
4587 if ((egid_arg != -1 && egid != egid_arg) ||
4588 (rgid_arg != -1 && rgid != rgid_arg)) {
4589 PyErr_SetString(PyExc_OverflowError, "group id too big");
4590 return NULL;
4591 }
4592 if (setregid(rgid, egid) < 0) {
4593 return posix_error();
4594 } else {
4595 Py_INCREF(Py_None);
4596 return Py_None;
4597 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004598}
4599#endif /* HAVE_SETREGID */
4600
Guido van Rossumb6775db1994-08-01 11:34:53 +00004601#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004602PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004603"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004604Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004605
Barry Warsaw53699e91996-12-10 23:23:01 +00004606static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004607posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004608{
Victor Stinner8c62be82010-05-06 00:08:46 +00004609 long gid_arg;
4610 gid_t gid;
4611 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4612 return NULL;
4613 gid = gid_arg;
4614 if (gid != gid_arg) {
4615 PyErr_SetString(PyExc_OverflowError, "group id too big");
4616 return NULL;
4617 }
4618 if (setgid(gid) < 0)
4619 return posix_error();
4620 Py_INCREF(Py_None);
4621 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004622}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004623#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004624
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004625#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004626PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004627"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004628Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004629
4630static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004631posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004632{
Victor Stinner8c62be82010-05-06 00:08:46 +00004633 int i, len;
4634 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004635
Victor Stinner8c62be82010-05-06 00:08:46 +00004636 if (!PySequence_Check(groups)) {
4637 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4638 return NULL;
4639 }
4640 len = PySequence_Size(groups);
4641 if (len > MAX_GROUPS) {
4642 PyErr_SetString(PyExc_ValueError, "too many groups");
4643 return NULL;
4644 }
4645 for(i = 0; i < len; i++) {
4646 PyObject *elem;
4647 elem = PySequence_GetItem(groups, i);
4648 if (!elem)
4649 return NULL;
4650 if (!PyLong_Check(elem)) {
4651 PyErr_SetString(PyExc_TypeError,
4652 "groups must be integers");
4653 Py_DECREF(elem);
4654 return NULL;
4655 } else {
4656 unsigned long x = PyLong_AsUnsignedLong(elem);
4657 if (PyErr_Occurred()) {
4658 PyErr_SetString(PyExc_TypeError,
4659 "group id too big");
4660 Py_DECREF(elem);
4661 return NULL;
4662 }
4663 grouplist[i] = x;
4664 /* read back the value to see if it fitted in gid_t */
4665 if (grouplist[i] != x) {
4666 PyErr_SetString(PyExc_TypeError,
4667 "group id too big");
4668 Py_DECREF(elem);
4669 return NULL;
4670 }
4671 }
4672 Py_DECREF(elem);
4673 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004674
Victor Stinner8c62be82010-05-06 00:08:46 +00004675 if (setgroups(len, grouplist) < 0)
4676 return posix_error();
4677 Py_INCREF(Py_None);
4678 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004679}
4680#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004681
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004682#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4683static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004684wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004685{
Victor Stinner8c62be82010-05-06 00:08:46 +00004686 PyObject *result;
4687 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004688
Victor Stinner8c62be82010-05-06 00:08:46 +00004689 if (pid == -1)
4690 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004691
Victor Stinner8c62be82010-05-06 00:08:46 +00004692 if (struct_rusage == NULL) {
4693 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4694 if (m == NULL)
4695 return NULL;
4696 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4697 Py_DECREF(m);
4698 if (struct_rusage == NULL)
4699 return NULL;
4700 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004701
Victor Stinner8c62be82010-05-06 00:08:46 +00004702 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4703 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4704 if (!result)
4705 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004706
4707#ifndef doubletime
4708#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4709#endif
4710
Victor Stinner8c62be82010-05-06 00:08:46 +00004711 PyStructSequence_SET_ITEM(result, 0,
4712 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4713 PyStructSequence_SET_ITEM(result, 1,
4714 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004715#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004716 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4717 SET_INT(result, 2, ru->ru_maxrss);
4718 SET_INT(result, 3, ru->ru_ixrss);
4719 SET_INT(result, 4, ru->ru_idrss);
4720 SET_INT(result, 5, ru->ru_isrss);
4721 SET_INT(result, 6, ru->ru_minflt);
4722 SET_INT(result, 7, ru->ru_majflt);
4723 SET_INT(result, 8, ru->ru_nswap);
4724 SET_INT(result, 9, ru->ru_inblock);
4725 SET_INT(result, 10, ru->ru_oublock);
4726 SET_INT(result, 11, ru->ru_msgsnd);
4727 SET_INT(result, 12, ru->ru_msgrcv);
4728 SET_INT(result, 13, ru->ru_nsignals);
4729 SET_INT(result, 14, ru->ru_nvcsw);
4730 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004731#undef SET_INT
4732
Victor Stinner8c62be82010-05-06 00:08:46 +00004733 if (PyErr_Occurred()) {
4734 Py_DECREF(result);
4735 return NULL;
4736 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004737
Victor Stinner8c62be82010-05-06 00:08:46 +00004738 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004739}
4740#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4741
4742#ifdef HAVE_WAIT3
4743PyDoc_STRVAR(posix_wait3__doc__,
4744"wait3(options) -> (pid, status, rusage)\n\n\
4745Wait for completion of a child process.");
4746
4747static PyObject *
4748posix_wait3(PyObject *self, PyObject *args)
4749{
Victor Stinner8c62be82010-05-06 00:08:46 +00004750 pid_t pid;
4751 int options;
4752 struct rusage ru;
4753 WAIT_TYPE status;
4754 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004755
Victor Stinner8c62be82010-05-06 00:08:46 +00004756 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4757 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004758
Victor Stinner8c62be82010-05-06 00:08:46 +00004759 Py_BEGIN_ALLOW_THREADS
4760 pid = wait3(&status, options, &ru);
4761 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004762
Victor Stinner8c62be82010-05-06 00:08:46 +00004763 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004764}
4765#endif /* HAVE_WAIT3 */
4766
4767#ifdef HAVE_WAIT4
4768PyDoc_STRVAR(posix_wait4__doc__,
4769"wait4(pid, options) -> (pid, status, rusage)\n\n\
4770Wait for completion of a given child process.");
4771
4772static PyObject *
4773posix_wait4(PyObject *self, PyObject *args)
4774{
Victor Stinner8c62be82010-05-06 00:08:46 +00004775 pid_t pid;
4776 int options;
4777 struct rusage ru;
4778 WAIT_TYPE status;
4779 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004780
Victor Stinner8c62be82010-05-06 00:08:46 +00004781 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4782 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004783
Victor Stinner8c62be82010-05-06 00:08:46 +00004784 Py_BEGIN_ALLOW_THREADS
4785 pid = wait4(pid, &status, options, &ru);
4786 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004787
Victor Stinner8c62be82010-05-06 00:08:46 +00004788 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004789}
4790#endif /* HAVE_WAIT4 */
4791
Guido van Rossumb6775db1994-08-01 11:34:53 +00004792#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004793PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004794"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004795Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004796
Barry Warsaw53699e91996-12-10 23:23:01 +00004797static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004798posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004799{
Victor Stinner8c62be82010-05-06 00:08:46 +00004800 pid_t pid;
4801 int options;
4802 WAIT_TYPE status;
4803 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004804
Victor Stinner8c62be82010-05-06 00:08:46 +00004805 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4806 return NULL;
4807 Py_BEGIN_ALLOW_THREADS
4808 pid = waitpid(pid, &status, options);
4809 Py_END_ALLOW_THREADS
4810 if (pid == -1)
4811 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004812
Victor Stinner8c62be82010-05-06 00:08:46 +00004813 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004814}
4815
Tim Petersab034fa2002-02-01 11:27:43 +00004816#elif defined(HAVE_CWAIT)
4817
4818/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004819PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004820"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004821"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004822
4823static PyObject *
4824posix_waitpid(PyObject *self, PyObject *args)
4825{
Victor Stinner8c62be82010-05-06 00:08:46 +00004826 Py_intptr_t pid;
4827 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004828
Victor Stinner8c62be82010-05-06 00:08:46 +00004829 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4830 return NULL;
4831 Py_BEGIN_ALLOW_THREADS
4832 pid = _cwait(&status, pid, options);
4833 Py_END_ALLOW_THREADS
4834 if (pid == -1)
4835 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004836
Victor Stinner8c62be82010-05-06 00:08:46 +00004837 /* shift the status left a byte so this is more like the POSIX waitpid */
4838 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004839}
4840#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004841
Guido van Rossumad0ee831995-03-01 10:34:45 +00004842#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004843PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004844"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004845Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004846
Barry Warsaw53699e91996-12-10 23:23:01 +00004847static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004848posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004849{
Victor Stinner8c62be82010-05-06 00:08:46 +00004850 pid_t pid;
4851 WAIT_TYPE status;
4852 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004853
Victor Stinner8c62be82010-05-06 00:08:46 +00004854 Py_BEGIN_ALLOW_THREADS
4855 pid = wait(&status);
4856 Py_END_ALLOW_THREADS
4857 if (pid == -1)
4858 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004859
Victor Stinner8c62be82010-05-06 00:08:46 +00004860 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004861}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004862#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004863
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004865PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004866"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004867Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004868
Barry Warsaw53699e91996-12-10 23:23:01 +00004869static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004870posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004871{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004872#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004873 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004874#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004875#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00004876 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
4877 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004878#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004880#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004881#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004882}
4883
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004884
Guido van Rossumb6775db1994-08-01 11:34:53 +00004885#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004886PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004887"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004888Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004889
Barry Warsaw53699e91996-12-10 23:23:01 +00004890static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004891posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004892{
Victor Stinner8c62be82010-05-06 00:08:46 +00004893 PyObject* v;
4894 char buf[MAXPATHLEN];
4895 PyObject *opath;
4896 char *path;
4897 int n;
4898 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004899
Victor Stinner8c62be82010-05-06 00:08:46 +00004900 if (!PyArg_ParseTuple(args, "O&:readlink",
4901 PyUnicode_FSConverter, &opath))
4902 return NULL;
4903 path = PyBytes_AsString(opath);
4904 v = PySequence_GetItem(args, 0);
4905 if (v == NULL) {
4906 Py_DECREF(opath);
4907 return NULL;
4908 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004909
Victor Stinner8c62be82010-05-06 00:08:46 +00004910 if (PyUnicode_Check(v)) {
4911 arg_is_unicode = 1;
4912 }
4913 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004914
Victor Stinner8c62be82010-05-06 00:08:46 +00004915 Py_BEGIN_ALLOW_THREADS
4916 n = readlink(path, buf, (int) sizeof buf);
4917 Py_END_ALLOW_THREADS
4918 if (n < 0)
4919 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004920
Victor Stinner8c62be82010-05-06 00:08:46 +00004921 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00004922 if (arg_is_unicode)
4923 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
4924 else
4925 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004926}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004927#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004928
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004929
Guido van Rossumb6775db1994-08-01 11:34:53 +00004930#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004931PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004932"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004933Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004934
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004935static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004936posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004937{
Victor Stinner8c62be82010-05-06 00:08:46 +00004938 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004939}
4940#endif /* HAVE_SYMLINK */
4941
Brian Curtind40e6f72010-07-08 21:39:08 +00004942#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
4943
4944PyDoc_STRVAR(win_readlink__doc__,
4945"readlink(path) -> path\n\n\
4946Return a string representing the path to which the symbolic link points.");
4947
4948/* The following structure was copied from
4949 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
4950 include doesn't seem to be present in the Windows SDK (at least as included
4951 with Visual Studio Express). */
4952typedef struct _REPARSE_DATA_BUFFER {
4953 ULONG ReparseTag;
4954 USHORT ReparseDataLength;
4955 USHORT Reserved;
4956 union {
4957 struct {
4958 USHORT SubstituteNameOffset;
4959 USHORT SubstituteNameLength;
4960 USHORT PrintNameOffset;
4961 USHORT PrintNameLength;
4962 ULONG Flags;
4963 WCHAR PathBuffer[1];
4964 } SymbolicLinkReparseBuffer;
4965
4966 struct {
4967 USHORT SubstituteNameOffset;
4968 USHORT SubstituteNameLength;
4969 USHORT PrintNameOffset;
4970 USHORT PrintNameLength;
4971 WCHAR PathBuffer[1];
4972 } MountPointReparseBuffer;
4973
4974 struct {
4975 UCHAR DataBuffer[1];
4976 } GenericReparseBuffer;
4977 };
4978} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
4979
Brian Curtin74e45612010-07-09 15:58:59 +00004980#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
4981 GenericReparseBuffer)
Brian Curtind40e6f72010-07-08 21:39:08 +00004982
4983#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
4984
4985/* Windows readlink implementation */
4986static PyObject *
4987win_readlink(PyObject *self, PyObject *args)
4988{
4989 wchar_t *path;
4990 DWORD n_bytes_returned;
4991 DWORD io_result;
4992 PyObject *result;
4993 HANDLE reparse_point_handle;
4994
4995 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
4996 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
4997 wchar_t *print_name;
4998
4999 if (!PyArg_ParseTuple(args,
5000 "u:readlink",
5001 &path))
5002 return NULL;
5003
5004 /* First get a handle to the reparse point */
5005 Py_BEGIN_ALLOW_THREADS
5006 reparse_point_handle = CreateFileW(
5007 path,
5008 0,
5009 0,
5010 0,
5011 OPEN_EXISTING,
5012 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5013 0);
5014 Py_END_ALLOW_THREADS
5015
5016 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5017 {
5018 return win32_error_unicode("readlink", path);
5019 }
5020
5021 Py_BEGIN_ALLOW_THREADS
5022 /* New call DeviceIoControl to read the reparse point */
5023 io_result = DeviceIoControl(
5024 reparse_point_handle,
5025 FSCTL_GET_REPARSE_POINT,
5026 0, 0, /* in buffer */
5027 target_buffer, sizeof(target_buffer),
5028 &n_bytes_returned,
5029 0 /* we're not using OVERLAPPED_IO */
5030 );
5031 CloseHandle(reparse_point_handle);
5032 Py_END_ALLOW_THREADS
5033
5034 if (io_result==0)
5035 {
5036 return win32_error_unicode("readlink", path);
5037 }
5038
5039 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5040 {
5041 PyErr_SetString(PyExc_ValueError,
5042 "not a symbolic link");
5043 return NULL;
5044 }
Brian Curtin74e45612010-07-09 15:58:59 +00005045 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5046 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5047
5048 result = PyUnicode_FromWideChar(print_name,
5049 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005050 return result;
5051}
5052
5053#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5054
5055#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
5056
5057/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5058static int has_CreateSymbolicLinkW = 0;
5059static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5060static int
5061check_CreateSymbolicLinkW()
5062{
5063 HINSTANCE hKernel32;
5064 /* only recheck */
5065 if (has_CreateSymbolicLinkW)
5066 return has_CreateSymbolicLinkW;
5067 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005068 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5069 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005070 if (Py_CreateSymbolicLinkW)
5071 has_CreateSymbolicLinkW = 1;
5072 return has_CreateSymbolicLinkW;
5073}
5074
5075PyDoc_STRVAR(win_symlink__doc__,
5076"symlink(src, dst, target_is_directory=False)\n\n\
5077Create a symbolic link pointing to src named dst.\n\
5078target_is_directory is required if the target is to be interpreted as\n\
5079a directory.\n\
5080This function requires Windows 6.0 or greater, and raises a\n\
5081NotImplementedError otherwise.");
5082
5083static PyObject *
5084win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5085{
5086 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5087 PyObject *src, *dest;
5088 int target_is_directory = 0;
5089 DWORD res;
5090 WIN32_FILE_ATTRIBUTE_DATA src_info;
5091
5092 if (!check_CreateSymbolicLinkW())
5093 {
5094 /* raise NotImplementedError */
5095 return PyErr_Format(PyExc_NotImplementedError,
5096 "CreateSymbolicLinkW not found");
5097 }
5098 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5099 kwlist, &src, &dest, &target_is_directory))
5100 return NULL;
5101 if (!convert_to_unicode(&src)) { return NULL; }
5102 if (!convert_to_unicode(&dest)) {
5103 Py_DECREF(src);
5104 return NULL;
5105 }
5106
5107 /* if src is a directory, ensure target_is_directory==1 */
5108 if(
5109 GetFileAttributesExW(
5110 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5111 ))
5112 {
5113 target_is_directory = target_is_directory ||
5114 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5115 }
5116
5117 Py_BEGIN_ALLOW_THREADS
5118 res = Py_CreateSymbolicLinkW(
5119 PyUnicode_AsUnicode(dest),
5120 PyUnicode_AsUnicode(src),
5121 target_is_directory);
5122 Py_END_ALLOW_THREADS
5123 Py_DECREF(src);
5124 Py_DECREF(dest);
5125 if (!res)
5126 {
5127 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5128 }
5129
5130 Py_INCREF(Py_None);
5131 return Py_None;
5132}
5133#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005134
5135#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005136#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5137static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005138system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005139{
5140 ULONG value = 0;
5141
5142 Py_BEGIN_ALLOW_THREADS
5143 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5144 Py_END_ALLOW_THREADS
5145
5146 return value;
5147}
5148
5149static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005150posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005151{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005152 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005153 return Py_BuildValue("ddddd",
5154 (double)0 /* t.tms_utime / HZ */,
5155 (double)0 /* t.tms_stime / HZ */,
5156 (double)0 /* t.tms_cutime / HZ */,
5157 (double)0 /* t.tms_cstime / HZ */,
5158 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005159}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005160#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005161#define NEED_TICKS_PER_SECOND
5162static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005163static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005164posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005165{
Victor Stinner8c62be82010-05-06 00:08:46 +00005166 struct tms t;
5167 clock_t c;
5168 errno = 0;
5169 c = times(&t);
5170 if (c == (clock_t) -1)
5171 return posix_error();
5172 return Py_BuildValue("ddddd",
5173 (double)t.tms_utime / ticks_per_second,
5174 (double)t.tms_stime / ticks_per_second,
5175 (double)t.tms_cutime / ticks_per_second,
5176 (double)t.tms_cstime / ticks_per_second,
5177 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005178}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005179#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005180#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005181
5182
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005183#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005184#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005185static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005186posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005187{
Victor Stinner8c62be82010-05-06 00:08:46 +00005188 FILETIME create, exit, kernel, user;
5189 HANDLE hProc;
5190 hProc = GetCurrentProcess();
5191 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5192 /* The fields of a FILETIME structure are the hi and lo part
5193 of a 64-bit value expressed in 100 nanosecond units.
5194 1e7 is one second in such units; 1e-7 the inverse.
5195 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5196 */
5197 return Py_BuildValue(
5198 "ddddd",
5199 (double)(user.dwHighDateTime*429.4967296 +
5200 user.dwLowDateTime*1e-7),
5201 (double)(kernel.dwHighDateTime*429.4967296 +
5202 kernel.dwLowDateTime*1e-7),
5203 (double)0,
5204 (double)0,
5205 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005206}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005207#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005208
5209#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005210PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005211"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005212Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005213#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005214
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005215
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005216#ifdef HAVE_GETSID
5217PyDoc_STRVAR(posix_getsid__doc__,
5218"getsid(pid) -> sid\n\n\
5219Call the system call getsid().");
5220
5221static PyObject *
5222posix_getsid(PyObject *self, PyObject *args)
5223{
Victor Stinner8c62be82010-05-06 00:08:46 +00005224 pid_t pid;
5225 int sid;
5226 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5227 return NULL;
5228 sid = getsid(pid);
5229 if (sid < 0)
5230 return posix_error();
5231 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005232}
5233#endif /* HAVE_GETSID */
5234
5235
Guido van Rossumb6775db1994-08-01 11:34:53 +00005236#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005237PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005238"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005239Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005240
Barry Warsaw53699e91996-12-10 23:23:01 +00005241static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005242posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005243{
Victor Stinner8c62be82010-05-06 00:08:46 +00005244 if (setsid() < 0)
5245 return posix_error();
5246 Py_INCREF(Py_None);
5247 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005248}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005249#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005250
Guido van Rossumb6775db1994-08-01 11:34:53 +00005251#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005252PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005253"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005254Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005255
Barry Warsaw53699e91996-12-10 23:23:01 +00005256static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005257posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005258{
Victor Stinner8c62be82010-05-06 00:08:46 +00005259 pid_t pid;
5260 int pgrp;
5261 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5262 return NULL;
5263 if (setpgid(pid, pgrp) < 0)
5264 return posix_error();
5265 Py_INCREF(Py_None);
5266 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005267}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005268#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005269
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005270
Guido van Rossumb6775db1994-08-01 11:34:53 +00005271#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005273"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005274Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005275
Barry Warsaw53699e91996-12-10 23:23:01 +00005276static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005277posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005278{
Victor Stinner8c62be82010-05-06 00:08:46 +00005279 int fd;
5280 pid_t pgid;
5281 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5282 return NULL;
5283 pgid = tcgetpgrp(fd);
5284 if (pgid < 0)
5285 return posix_error();
5286 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005287}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005288#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005289
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005290
Guido van Rossumb6775db1994-08-01 11:34:53 +00005291#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005292PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005293"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005294Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005295
Barry Warsaw53699e91996-12-10 23:23:01 +00005296static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005297posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005298{
Victor Stinner8c62be82010-05-06 00:08:46 +00005299 int fd;
5300 pid_t pgid;
5301 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5302 return NULL;
5303 if (tcsetpgrp(fd, pgid) < 0)
5304 return posix_error();
5305 Py_INCREF(Py_None);
5306 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005307}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005308#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005309
Guido van Rossum687dd131993-05-17 08:34:16 +00005310/* Functions acting on file descriptors */
5311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005312PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005313"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005314Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005315
Barry Warsaw53699e91996-12-10 23:23:01 +00005316static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005317posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005318{
Victor Stinner8c62be82010-05-06 00:08:46 +00005319 PyObject *ofile;
5320 char *file;
5321 int flag;
5322 int mode = 0777;
5323 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005324
5325#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005326 PyUnicodeObject *po;
5327 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5328 Py_BEGIN_ALLOW_THREADS
5329 /* PyUnicode_AS_UNICODE OK without thread
5330 lock as it is a simple dereference. */
5331 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5332 Py_END_ALLOW_THREADS
5333 if (fd < 0)
5334 return posix_error();
5335 return PyLong_FromLong((long)fd);
5336 }
5337 /* Drop the argument parsing error as narrow strings
5338 are also valid. */
5339 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005340#endif
5341
Victor Stinner8c62be82010-05-06 00:08:46 +00005342 if (!PyArg_ParseTuple(args, "O&i|i",
5343 PyUnicode_FSConverter, &ofile,
5344 &flag, &mode))
5345 return NULL;
5346 file = PyBytes_AsString(ofile);
5347 Py_BEGIN_ALLOW_THREADS
5348 fd = open(file, flag, mode);
5349 Py_END_ALLOW_THREADS
5350 if (fd < 0)
5351 return posix_error_with_allocated_filename(ofile);
5352 Py_DECREF(ofile);
5353 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005354}
5355
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005356
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005357PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005358"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005359Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005360
Barry Warsaw53699e91996-12-10 23:23:01 +00005361static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005362posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005363{
Victor Stinner8c62be82010-05-06 00:08:46 +00005364 int fd, res;
5365 if (!PyArg_ParseTuple(args, "i:close", &fd))
5366 return NULL;
5367 if (!_PyVerify_fd(fd))
5368 return posix_error();
5369 Py_BEGIN_ALLOW_THREADS
5370 res = close(fd);
5371 Py_END_ALLOW_THREADS
5372 if (res < 0)
5373 return posix_error();
5374 Py_INCREF(Py_None);
5375 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005376}
5377
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005378
Victor Stinner8c62be82010-05-06 00:08:46 +00005379PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005380"closerange(fd_low, fd_high)\n\n\
5381Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5382
5383static PyObject *
5384posix_closerange(PyObject *self, PyObject *args)
5385{
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 int fd_from, fd_to, i;
5387 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5388 return NULL;
5389 Py_BEGIN_ALLOW_THREADS
5390 for (i = fd_from; i < fd_to; i++)
5391 if (_PyVerify_fd(i))
5392 close(i);
5393 Py_END_ALLOW_THREADS
5394 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005395}
5396
5397
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005398PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005399"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005400Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005401
Barry Warsaw53699e91996-12-10 23:23:01 +00005402static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005403posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005404{
Victor Stinner8c62be82010-05-06 00:08:46 +00005405 int fd;
5406 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5407 return NULL;
5408 if (!_PyVerify_fd(fd))
5409 return posix_error();
5410 Py_BEGIN_ALLOW_THREADS
5411 fd = dup(fd);
5412 Py_END_ALLOW_THREADS
5413 if (fd < 0)
5414 return posix_error();
5415 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005416}
5417
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005418
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005419PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005420"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005421Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005422
Barry Warsaw53699e91996-12-10 23:23:01 +00005423static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005424posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005425{
Victor Stinner8c62be82010-05-06 00:08:46 +00005426 int fd, fd2, res;
5427 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5428 return NULL;
5429 if (!_PyVerify_fd_dup2(fd, fd2))
5430 return posix_error();
5431 Py_BEGIN_ALLOW_THREADS
5432 res = dup2(fd, fd2);
5433 Py_END_ALLOW_THREADS
5434 if (res < 0)
5435 return posix_error();
5436 Py_INCREF(Py_None);
5437 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005438}
5439
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005441PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005442"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005443Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005444
Barry Warsaw53699e91996-12-10 23:23:01 +00005445static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005446posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005447{
Victor Stinner8c62be82010-05-06 00:08:46 +00005448 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005449#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005450 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005451#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005452 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005453#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005454 PyObject *posobj;
5455 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5456 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005457#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005458 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5459 switch (how) {
5460 case 0: how = SEEK_SET; break;
5461 case 1: how = SEEK_CUR; break;
5462 case 2: how = SEEK_END; break;
5463 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005464#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005465
5466#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005467 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005468#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005469 pos = PyLong_Check(posobj) ?
5470 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005471#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005472 if (PyErr_Occurred())
5473 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005474
Victor Stinner8c62be82010-05-06 00:08:46 +00005475 if (!_PyVerify_fd(fd))
5476 return posix_error();
5477 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005478#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005479 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005480#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005481 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005482#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005483 Py_END_ALLOW_THREADS
5484 if (res < 0)
5485 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005486
5487#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005488 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005489#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005490 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005491#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005492}
5493
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005495PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005496"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005497Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005498
Barry Warsaw53699e91996-12-10 23:23:01 +00005499static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005500posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005501{
Victor Stinner8c62be82010-05-06 00:08:46 +00005502 int fd, size;
5503 Py_ssize_t n;
5504 PyObject *buffer;
5505 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5506 return NULL;
5507 if (size < 0) {
5508 errno = EINVAL;
5509 return posix_error();
5510 }
5511 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5512 if (buffer == NULL)
5513 return NULL;
5514 if (!_PyVerify_fd(fd))
5515 return posix_error();
5516 Py_BEGIN_ALLOW_THREADS
5517 n = read(fd, PyBytes_AS_STRING(buffer), size);
5518 Py_END_ALLOW_THREADS
5519 if (n < 0) {
5520 Py_DECREF(buffer);
5521 return posix_error();
5522 }
5523 if (n != size)
5524 _PyBytes_Resize(&buffer, n);
5525 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005526}
5527
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005529PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005530"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005531Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005532
Barry Warsaw53699e91996-12-10 23:23:01 +00005533static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005534posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005535{
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 Py_buffer pbuf;
5537 int fd;
5538 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005539
Victor Stinner8c62be82010-05-06 00:08:46 +00005540 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5541 return NULL;
5542 if (!_PyVerify_fd(fd))
5543 return posix_error();
5544 Py_BEGIN_ALLOW_THREADS
5545 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5546 Py_END_ALLOW_THREADS
5547 PyBuffer_Release(&pbuf);
5548 if (size < 0)
5549 return posix_error();
5550 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005551}
5552
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005554PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005555"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005556Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005557
Barry Warsaw53699e91996-12-10 23:23:01 +00005558static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005559posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005560{
Victor Stinner8c62be82010-05-06 00:08:46 +00005561 int fd;
5562 STRUCT_STAT st;
5563 int res;
5564 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5565 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005566#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005567 /* on OpenVMS we must ensure that all bytes are written to the file */
5568 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005569#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005570 if (!_PyVerify_fd(fd))
5571 return posix_error();
5572 Py_BEGIN_ALLOW_THREADS
5573 res = FSTAT(fd, &st);
5574 Py_END_ALLOW_THREADS
5575 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005576#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005577 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005578#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005579 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005580#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005581 }
Tim Peters5aa91602002-01-30 05:46:57 +00005582
Victor Stinner8c62be82010-05-06 00:08:46 +00005583 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005584}
5585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005586PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005587"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005588Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005589connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005590
5591static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005592posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005593{
Victor Stinner8c62be82010-05-06 00:08:46 +00005594 int fd;
5595 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5596 return NULL;
5597 if (!_PyVerify_fd(fd))
5598 return PyBool_FromLong(0);
5599 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005600}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005601
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005602#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005603PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005604"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005605Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005606
Barry Warsaw53699e91996-12-10 23:23:01 +00005607static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005608posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005609{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005610#if defined(PYOS_OS2)
5611 HFILE read, write;
5612 APIRET rc;
5613
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005615 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005617 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005618 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005619
5620 return Py_BuildValue("(ii)", read, write);
5621#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005622#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005623 int fds[2];
5624 int res;
5625 Py_BEGIN_ALLOW_THREADS
5626 res = pipe(fds);
5627 Py_END_ALLOW_THREADS
5628 if (res != 0)
5629 return posix_error();
5630 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005631#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 HANDLE read, write;
5633 int read_fd, write_fd;
5634 BOOL ok;
5635 Py_BEGIN_ALLOW_THREADS
5636 ok = CreatePipe(&read, &write, NULL, 0);
5637 Py_END_ALLOW_THREADS
5638 if (!ok)
5639 return win32_error("CreatePipe", NULL);
5640 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5641 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5642 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005643#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005644#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005645}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005646#endif /* HAVE_PIPE */
5647
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005648
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005649#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005650PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005651"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005652Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005653
Barry Warsaw53699e91996-12-10 23:23:01 +00005654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005655posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005656{
Victor Stinner8c62be82010-05-06 00:08:46 +00005657 char *filename;
5658 int mode = 0666;
5659 int res;
5660 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
5661 return NULL;
5662 Py_BEGIN_ALLOW_THREADS
5663 res = mkfifo(filename, mode);
5664 Py_END_ALLOW_THREADS
5665 if (res < 0)
5666 return posix_error();
5667 Py_INCREF(Py_None);
5668 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005669}
5670#endif
5671
5672
Neal Norwitz11690112002-07-30 01:08:28 +00005673#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005674PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005675"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005676Create a filesystem node (file, device special file or named pipe)\n\
5677named filename. mode specifies both the permissions to use and the\n\
5678type of node to be created, being combined (bitwise OR) with one of\n\
5679S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005680device defines the newly created device special file (probably using\n\
5681os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005682
5683
5684static PyObject *
5685posix_mknod(PyObject *self, PyObject *args)
5686{
Victor Stinner8c62be82010-05-06 00:08:46 +00005687 char *filename;
5688 int mode = 0600;
5689 int device = 0;
5690 int res;
5691 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
5692 return NULL;
5693 Py_BEGIN_ALLOW_THREADS
5694 res = mknod(filename, mode, device);
5695 Py_END_ALLOW_THREADS
5696 if (res < 0)
5697 return posix_error();
5698 Py_INCREF(Py_None);
5699 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005700}
5701#endif
5702
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005703#ifdef HAVE_DEVICE_MACROS
5704PyDoc_STRVAR(posix_major__doc__,
5705"major(device) -> major number\n\
5706Extracts a device major number from a raw device number.");
5707
5708static PyObject *
5709posix_major(PyObject *self, PyObject *args)
5710{
Victor Stinner8c62be82010-05-06 00:08:46 +00005711 int device;
5712 if (!PyArg_ParseTuple(args, "i:major", &device))
5713 return NULL;
5714 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005715}
5716
5717PyDoc_STRVAR(posix_minor__doc__,
5718"minor(device) -> minor number\n\
5719Extracts a device minor number from a raw device number.");
5720
5721static PyObject *
5722posix_minor(PyObject *self, PyObject *args)
5723{
Victor Stinner8c62be82010-05-06 00:08:46 +00005724 int device;
5725 if (!PyArg_ParseTuple(args, "i:minor", &device))
5726 return NULL;
5727 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005728}
5729
5730PyDoc_STRVAR(posix_makedev__doc__,
5731"makedev(major, minor) -> device number\n\
5732Composes a raw device number from the major and minor device numbers.");
5733
5734static PyObject *
5735posix_makedev(PyObject *self, PyObject *args)
5736{
Victor Stinner8c62be82010-05-06 00:08:46 +00005737 int major, minor;
5738 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5739 return NULL;
5740 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005741}
5742#endif /* device macros */
5743
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005744
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005745#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005746PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005747"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005748Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005749
Barry Warsaw53699e91996-12-10 23:23:01 +00005750static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005751posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005752{
Victor Stinner8c62be82010-05-06 00:08:46 +00005753 int fd;
5754 off_t length;
5755 int res;
5756 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005757
Victor Stinner8c62be82010-05-06 00:08:46 +00005758 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5759 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005760
5761#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005762 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005763#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005764 length = PyLong_Check(lenobj) ?
5765 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005766#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005767 if (PyErr_Occurred())
5768 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005769
Victor Stinner8c62be82010-05-06 00:08:46 +00005770 Py_BEGIN_ALLOW_THREADS
5771 res = ftruncate(fd, length);
5772 Py_END_ALLOW_THREADS
5773 if (res < 0)
5774 return posix_error();
5775 Py_INCREF(Py_None);
5776 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005777}
5778#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005779
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005780#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005781PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005782"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005783Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005784
Fred Drake762e2061999-08-26 17:23:54 +00005785/* Save putenv() parameters as values here, so we can collect them when they
5786 * get re-set with another call for the same key. */
5787static PyObject *posix_putenv_garbage;
5788
Tim Peters5aa91602002-01-30 05:46:57 +00005789static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005790posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005791{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005792#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005793 wchar_t *s1, *s2;
5794 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005795#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 PyObject *os1, *os2;
5797 char *s1, *s2;
5798 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005799#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005800 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005801 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005802
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005803#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005804 if (!PyArg_ParseTuple(args,
5805 "uu:putenv",
5806 &s1, &s2))
5807 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005808#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005809 if (!PyArg_ParseTuple(args,
5810 "O&O&:putenv",
5811 PyUnicode_FSConverter, &os1,
5812 PyUnicode_FSConverter, &os2))
5813 return NULL;
5814 s1 = PyBytes_AsString(os1);
5815 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005816#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005817
5818#if defined(PYOS_OS2)
5819 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5820 APIRET rc;
5821
Guido van Rossumd48f2521997-12-05 22:19:34 +00005822 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005823 if (rc != NO_ERROR) {
5824 os2_error(rc);
5825 goto error;
5826 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005827
5828 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5829 APIRET rc;
5830
Guido van Rossumd48f2521997-12-05 22:19:34 +00005831 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005832 if (rc != NO_ERROR) {
5833 os2_error(rc);
5834 goto error;
5835 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005836 } else {
5837#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005838 /* XXX This can leak memory -- not easy to fix :-( */
5839 /* len includes space for a trailing \0; the size arg to
5840 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005841#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005842 len = wcslen(s1) + wcslen(s2) + 2;
5843 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005844#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005845 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005846 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005847#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005848 if (newstr == NULL) {
5849 PyErr_NoMemory();
5850 goto error;
5851 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005852#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005853 newenv = PyUnicode_AsUnicode(newstr);
5854 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5855 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005856 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005857 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005858 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005859#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005860 newenv = PyBytes_AS_STRING(newstr);
5861 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5862 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005863 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005864 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005865 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005866#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005867
Victor Stinner8c62be82010-05-06 00:08:46 +00005868 /* Install the first arg and newstr in posix_putenv_garbage;
5869 * this will cause previous value to be collected. This has to
5870 * happen after the real putenv() call because the old value
5871 * was still accessible until then. */
5872 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005873#ifdef MS_WINDOWS
5874 PyTuple_GET_ITEM(args, 0),
5875#else
5876 os1,
5877#endif
5878 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005879 /* really not much we can do; just leak */
5880 PyErr_Clear();
5881 }
5882 else {
5883 Py_DECREF(newstr);
5884 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005885
5886#if defined(PYOS_OS2)
5887 }
5888#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005889
Martin v. Löwis011e8422009-05-05 04:43:17 +00005890#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 Py_DECREF(os1);
5892 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005893#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005894 Py_RETURN_NONE;
5895
5896error:
5897#ifndef MS_WINDOWS
5898 Py_DECREF(os1);
5899 Py_DECREF(os2);
5900#endif
5901 Py_XDECREF(newstr);
5902 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005903}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005904#endif /* putenv */
5905
Guido van Rossumc524d952001-10-19 01:31:59 +00005906#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005907PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005908"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005909Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005910
5911static PyObject *
5912posix_unsetenv(PyObject *self, PyObject *args)
5913{
Victor Stinner84ae1182010-05-06 22:05:07 +00005914#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005915 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005916
Victor Stinner8c62be82010-05-06 00:08:46 +00005917 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5918 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00005919#else
5920 PyObject *os1;
5921 char *s1;
5922
5923 if (!PyArg_ParseTuple(args, "O&:unsetenv",
5924 PyUnicode_FSConverter, &os1))
5925 return NULL;
5926 s1 = PyBytes_AsString(os1);
5927#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00005928
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00005930
Victor Stinner8c62be82010-05-06 00:08:46 +00005931 /* Remove the key from posix_putenv_garbage;
5932 * this will cause it to be collected. This has to
5933 * happen after the real unsetenv() call because the
5934 * old value was still accessible until then.
5935 */
5936 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005937#ifdef MS_WINDOWS
5938 PyTuple_GET_ITEM(args, 0)
5939#else
5940 os1
5941#endif
5942 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 /* really not much we can do; just leak */
5944 PyErr_Clear();
5945 }
Guido van Rossumc524d952001-10-19 01:31:59 +00005946
Victor Stinner84ae1182010-05-06 22:05:07 +00005947#ifndef MS_WINDOWS
5948 Py_DECREF(os1);
5949#endif
5950 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00005951}
5952#endif /* unsetenv */
5953
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005954PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005955"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005956Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005957
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005958static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005959posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005960{
Victor Stinner8c62be82010-05-06 00:08:46 +00005961 int code;
5962 char *message;
5963 if (!PyArg_ParseTuple(args, "i:strerror", &code))
5964 return NULL;
5965 message = strerror(code);
5966 if (message == NULL) {
5967 PyErr_SetString(PyExc_ValueError,
5968 "strerror() argument out of range");
5969 return NULL;
5970 }
5971 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005972}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005973
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005974
Guido van Rossumc9641791998-08-04 15:26:23 +00005975#ifdef HAVE_SYS_WAIT_H
5976
Fred Drake106c1a02002-04-23 15:58:02 +00005977#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005978PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005979"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005980Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005981
5982static PyObject *
5983posix_WCOREDUMP(PyObject *self, PyObject *args)
5984{
Victor Stinner8c62be82010-05-06 00:08:46 +00005985 WAIT_TYPE status;
5986 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005987
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
5989 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005990
Victor Stinner8c62be82010-05-06 00:08:46 +00005991 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005992}
5993#endif /* WCOREDUMP */
5994
5995#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005996PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005997"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005998Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005999job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006000
6001static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006002posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006003{
Victor Stinner8c62be82010-05-06 00:08:46 +00006004 WAIT_TYPE status;
6005 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006006
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6008 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006009
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006011}
6012#endif /* WIFCONTINUED */
6013
Guido van Rossumc9641791998-08-04 15:26:23 +00006014#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006016"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006017Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006018
6019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006020posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006021{
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 WAIT_TYPE status;
6023 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006024
Victor Stinner8c62be82010-05-06 00:08:46 +00006025 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6026 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006027
Victor Stinner8c62be82010-05-06 00:08:46 +00006028 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006029}
6030#endif /* WIFSTOPPED */
6031
6032#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006033PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006034"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006035Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006036
6037static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006038posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006039{
Victor Stinner8c62be82010-05-06 00:08:46 +00006040 WAIT_TYPE status;
6041 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006042
Victor Stinner8c62be82010-05-06 00:08:46 +00006043 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6044 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006045
Victor Stinner8c62be82010-05-06 00:08:46 +00006046 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006047}
6048#endif /* WIFSIGNALED */
6049
6050#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006051PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006052"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006053Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006054system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006055
6056static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006057posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006058{
Victor Stinner8c62be82010-05-06 00:08:46 +00006059 WAIT_TYPE status;
6060 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006061
Victor Stinner8c62be82010-05-06 00:08:46 +00006062 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6063 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006064
Victor Stinner8c62be82010-05-06 00:08:46 +00006065 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006066}
6067#endif /* WIFEXITED */
6068
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006069#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006070PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006071"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006072Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006073
6074static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006075posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006076{
Victor Stinner8c62be82010-05-06 00:08:46 +00006077 WAIT_TYPE status;
6078 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006079
Victor Stinner8c62be82010-05-06 00:08:46 +00006080 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6081 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006082
Victor Stinner8c62be82010-05-06 00:08:46 +00006083 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006084}
6085#endif /* WEXITSTATUS */
6086
6087#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006088PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006089"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006090Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006091value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006092
6093static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006094posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006095{
Victor Stinner8c62be82010-05-06 00:08:46 +00006096 WAIT_TYPE status;
6097 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006098
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6100 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006101
Victor Stinner8c62be82010-05-06 00:08:46 +00006102 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006103}
6104#endif /* WTERMSIG */
6105
6106#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006107PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006108"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006109Return the signal that stopped the process that provided\n\
6110the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006111
6112static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006113posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006114{
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 WAIT_TYPE status;
6116 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006117
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6119 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006120
Victor Stinner8c62be82010-05-06 00:08:46 +00006121 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006122}
6123#endif /* WSTOPSIG */
6124
6125#endif /* HAVE_SYS_WAIT_H */
6126
6127
Thomas Wouters477c8d52006-05-27 19:21:47 +00006128#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006129#ifdef _SCO_DS
6130/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6131 needed definitions in sys/statvfs.h */
6132#define _SVID3
6133#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006134#include <sys/statvfs.h>
6135
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006136static PyObject*
6137_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6139 if (v == NULL)
6140 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006141
6142#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006143 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6144 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6145 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6146 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6147 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6148 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6149 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6150 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6151 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6152 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006153#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006154 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6155 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6156 PyStructSequence_SET_ITEM(v, 2,
6157 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6158 PyStructSequence_SET_ITEM(v, 3,
6159 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6160 PyStructSequence_SET_ITEM(v, 4,
6161 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6162 PyStructSequence_SET_ITEM(v, 5,
6163 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6164 PyStructSequence_SET_ITEM(v, 6,
6165 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6166 PyStructSequence_SET_ITEM(v, 7,
6167 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6168 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6169 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006170#endif
6171
Victor Stinner8c62be82010-05-06 00:08:46 +00006172 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006173}
6174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006175PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006176"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006177Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006178
6179static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006180posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006181{
Victor Stinner8c62be82010-05-06 00:08:46 +00006182 int fd, res;
6183 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006184
Victor Stinner8c62be82010-05-06 00:08:46 +00006185 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6186 return NULL;
6187 Py_BEGIN_ALLOW_THREADS
6188 res = fstatvfs(fd, &st);
6189 Py_END_ALLOW_THREADS
6190 if (res != 0)
6191 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006192
Victor Stinner8c62be82010-05-06 00:08:46 +00006193 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006194}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006195#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006196
6197
Thomas Wouters477c8d52006-05-27 19:21:47 +00006198#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006199#include <sys/statvfs.h>
6200
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006201PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006202"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006203Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006204
6205static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006206posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006207{
Victor Stinner8c62be82010-05-06 00:08:46 +00006208 char *path;
6209 int res;
6210 struct statvfs st;
6211 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6212 return NULL;
6213 Py_BEGIN_ALLOW_THREADS
6214 res = statvfs(path, &st);
6215 Py_END_ALLOW_THREADS
6216 if (res != 0)
6217 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006218
Victor Stinner8c62be82010-05-06 00:08:46 +00006219 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006220}
6221#endif /* HAVE_STATVFS */
6222
Fred Drakec9680921999-12-13 16:37:25 +00006223/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6224 * It maps strings representing configuration variable names to
6225 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006226 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006227 * rarely-used constants. There are three separate tables that use
6228 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006229 *
6230 * This code is always included, even if none of the interfaces that
6231 * need it are included. The #if hackery needed to avoid it would be
6232 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006233 */
6234struct constdef {
6235 char *name;
6236 long value;
6237};
6238
Fred Drake12c6e2d1999-12-14 21:25:03 +00006239static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006240conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006241 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006242{
Christian Heimes217cfd12007-12-02 14:31:20 +00006243 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006244 *valuep = PyLong_AS_LONG(arg);
6245 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006246 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006247 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006248 /* look up the value in the table using a binary search */
6249 size_t lo = 0;
6250 size_t mid;
6251 size_t hi = tablesize;
6252 int cmp;
6253 const char *confname;
6254 if (!PyUnicode_Check(arg)) {
6255 PyErr_SetString(PyExc_TypeError,
6256 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00006257 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006258 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006259 confname = _PyUnicode_AsString(arg);
6260 if (confname == NULL)
6261 return 0;
6262 while (lo < hi) {
6263 mid = (lo + hi) / 2;
6264 cmp = strcmp(confname, table[mid].name);
6265 if (cmp < 0)
6266 hi = mid;
6267 else if (cmp > 0)
6268 lo = mid + 1;
6269 else {
6270 *valuep = table[mid].value;
6271 return 1;
6272 }
6273 }
6274 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6275 return 0;
6276 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006277}
6278
6279
6280#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6281static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006282#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006284#endif
6285#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006286 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006287#endif
Fred Drakec9680921999-12-13 16:37:25 +00006288#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006289 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006290#endif
6291#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006293#endif
6294#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006295 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006296#endif
6297#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006298 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006299#endif
6300#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006301 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006302#endif
6303#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006304 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006305#endif
6306#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006307 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006308#endif
6309#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006310 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006311#endif
6312#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006314#endif
6315#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006317#endif
6318#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006319 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006320#endif
6321#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006322 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006323#endif
6324#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006325 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006326#endif
6327#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006328 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006329#endif
6330#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006331 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006332#endif
6333};
6334
Fred Drakec9680921999-12-13 16:37:25 +00006335static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006336conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006337{
6338 return conv_confname(arg, valuep, posix_constants_pathconf,
6339 sizeof(posix_constants_pathconf)
6340 / sizeof(struct constdef));
6341}
6342#endif
6343
6344#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006345PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006346"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006347Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006348If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006349
6350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006351posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006352{
6353 PyObject *result = NULL;
6354 int name, fd;
6355
Fred Drake12c6e2d1999-12-14 21:25:03 +00006356 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6357 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006358 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006359
Victor Stinner8c62be82010-05-06 00:08:46 +00006360 errno = 0;
6361 limit = fpathconf(fd, name);
6362 if (limit == -1 && errno != 0)
6363 posix_error();
6364 else
6365 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006366 }
6367 return result;
6368}
6369#endif
6370
6371
6372#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006373PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006374"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006375Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006376If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006377
6378static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006379posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006380{
6381 PyObject *result = NULL;
6382 int name;
6383 char *path;
6384
6385 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6386 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006387 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006388
Victor Stinner8c62be82010-05-06 00:08:46 +00006389 errno = 0;
6390 limit = pathconf(path, name);
6391 if (limit == -1 && errno != 0) {
6392 if (errno == EINVAL)
6393 /* could be a path or name problem */
6394 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006395 else
Victor Stinner8c62be82010-05-06 00:08:46 +00006396 posix_error_with_filename(path);
6397 }
6398 else
6399 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006400 }
6401 return result;
6402}
6403#endif
6404
6405#ifdef HAVE_CONFSTR
6406static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006407#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006408 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006409#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006410#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006411 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006412#endif
6413#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006414 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006415#endif
Fred Draked86ed291999-12-15 15:34:33 +00006416#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006417 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006418#endif
6419#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006420 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006421#endif
6422#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006423 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006424#endif
6425#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006426 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006427#endif
Fred Drakec9680921999-12-13 16:37:25 +00006428#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006429 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006430#endif
6431#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006432 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006433#endif
6434#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006435 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006436#endif
6437#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006438 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006439#endif
6440#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006441 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006442#endif
6443#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006444 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006445#endif
6446#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006447 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006448#endif
6449#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006450 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006451#endif
Fred Draked86ed291999-12-15 15:34:33 +00006452#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006453 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006454#endif
Fred Drakec9680921999-12-13 16:37:25 +00006455#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006456 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006457#endif
Fred Draked86ed291999-12-15 15:34:33 +00006458#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006459 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006460#endif
6461#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006462 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006463#endif
6464#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006465 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006466#endif
6467#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006468 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006469#endif
Fred Drakec9680921999-12-13 16:37:25 +00006470#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006471 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006472#endif
6473#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006474 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006475#endif
6476#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006477 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006478#endif
6479#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006480 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006481#endif
6482#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006483 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006484#endif
6485#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006486 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006487#endif
6488#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006490#endif
6491#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006493#endif
6494#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006495 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006496#endif
6497#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006498 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006499#endif
6500#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006501 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006502#endif
6503#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006504 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006505#endif
6506#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006507 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006508#endif
6509#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006510 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006511#endif
6512#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006513 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006514#endif
6515#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006516 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006517#endif
Fred Draked86ed291999-12-15 15:34:33 +00006518#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006519 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006520#endif
6521#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006522 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006523#endif
6524#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006525 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006526#endif
6527#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006528 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006529#endif
6530#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006531 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006532#endif
6533#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006534 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006535#endif
6536#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006537 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006538#endif
6539#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006540 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006541#endif
6542#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006543 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006544#endif
6545#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006546 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006547#endif
6548#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006549 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006550#endif
6551#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006552 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006553#endif
6554#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006555 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006556#endif
Fred Drakec9680921999-12-13 16:37:25 +00006557};
6558
6559static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006560conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006561{
6562 return conv_confname(arg, valuep, posix_constants_confstr,
6563 sizeof(posix_constants_confstr)
6564 / sizeof(struct constdef));
6565}
6566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006567PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006568"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006569Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006570
6571static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006572posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006573{
6574 PyObject *result = NULL;
6575 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006576 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006577
6578 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006579 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006580
Fred Drakec9680921999-12-13 16:37:25 +00006581 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 len = confstr(name, buffer, sizeof(buffer));
6583 if (len == 0) {
6584 if (errno) {
6585 posix_error();
6586 }
6587 else {
6588 result = Py_None;
6589 Py_INCREF(Py_None);
6590 }
Fred Drakec9680921999-12-13 16:37:25 +00006591 }
6592 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006593 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006594 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006595 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006596 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006597 }
6598 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006599 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006600 }
6601 }
6602 return result;
6603}
6604#endif
6605
6606
6607#ifdef HAVE_SYSCONF
6608static struct constdef posix_constants_sysconf[] = {
6609#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006611#endif
6612#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006614#endif
6615#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006617#endif
6618#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006619 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006620#endif
6621#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006622 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006623#endif
6624#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006625 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006626#endif
6627#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006629#endif
6630#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006632#endif
6633#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006635#endif
6636#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006638#endif
Fred Draked86ed291999-12-15 15:34:33 +00006639#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006640 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006641#endif
6642#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006643 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006644#endif
Fred Drakec9680921999-12-13 16:37:25 +00006645#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006646 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006647#endif
Fred Drakec9680921999-12-13 16:37:25 +00006648#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006650#endif
6651#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006653#endif
6654#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006656#endif
6657#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006659#endif
6660#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006662#endif
Fred Draked86ed291999-12-15 15:34:33 +00006663#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006664 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006665#endif
Fred Drakec9680921999-12-13 16:37:25 +00006666#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006667 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006668#endif
6669#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006670 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006671#endif
6672#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006673 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006674#endif
6675#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006676 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006677#endif
6678#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006679 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006680#endif
Fred Draked86ed291999-12-15 15:34:33 +00006681#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006682 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006683#endif
Fred Drakec9680921999-12-13 16:37:25 +00006684#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006685 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006686#endif
6687#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006688 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006689#endif
6690#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006691 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006692#endif
6693#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006695#endif
6696#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006697 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006698#endif
6699#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006701#endif
6702#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006703 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006704#endif
6705#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006707#endif
6708#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006709 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006710#endif
6711#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006712 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006713#endif
6714#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006715 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006716#endif
6717#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006719#endif
6720#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006721 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006722#endif
6723#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006724 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006725#endif
6726#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006727 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006728#endif
6729#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006731#endif
6732#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006733 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006734#endif
6735#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006736 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006737#endif
6738#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006739 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006740#endif
6741#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006742 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006743#endif
6744#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006745 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006746#endif
6747#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006748 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006749#endif
6750#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006751 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006752#endif
Fred Draked86ed291999-12-15 15:34:33 +00006753#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006754 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006755#endif
Fred Drakec9680921999-12-13 16:37:25 +00006756#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006758#endif
6759#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006760 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006761#endif
6762#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006763 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006764#endif
Fred Draked86ed291999-12-15 15:34:33 +00006765#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006767#endif
Fred Drakec9680921999-12-13 16:37:25 +00006768#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006769 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006770#endif
Fred Draked86ed291999-12-15 15:34:33 +00006771#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006772 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006773#endif
6774#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006775 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006776#endif
Fred Drakec9680921999-12-13 16:37:25 +00006777#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006779#endif
6780#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006781 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006782#endif
6783#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006784 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006785#endif
6786#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006787 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006788#endif
Fred Draked86ed291999-12-15 15:34:33 +00006789#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006790 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006791#endif
Fred Drakec9680921999-12-13 16:37:25 +00006792#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006793 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006794#endif
6795#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006797#endif
6798#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006799 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006800#endif
6801#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006803#endif
6804#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006805 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006806#endif
6807#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006808 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006809#endif
6810#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006811 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006812#endif
Fred Draked86ed291999-12-15 15:34:33 +00006813#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006815#endif
Fred Drakec9680921999-12-13 16:37:25 +00006816#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006818#endif
6819#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006820 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006821#endif
Fred Draked86ed291999-12-15 15:34:33 +00006822#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006823 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006824#endif
Fred Drakec9680921999-12-13 16:37:25 +00006825#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006826 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006827#endif
6828#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006830#endif
6831#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006832 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006833#endif
6834#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006836#endif
6837#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006838 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006839#endif
6840#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006841 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006842#endif
6843#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006845#endif
6846#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006848#endif
6849#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006851#endif
Fred Draked86ed291999-12-15 15:34:33 +00006852#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006854#endif
6855#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006857#endif
Fred Drakec9680921999-12-13 16:37:25 +00006858#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006860#endif
6861#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006863#endif
6864#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006865 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006866#endif
6867#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006868 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006869#endif
6870#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006871 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006872#endif
6873#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006875#endif
6876#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006877 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006878#endif
6879#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006880 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006881#endif
6882#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006884#endif
6885#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006887#endif
6888#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006889 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006890#endif
6891#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006892 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006893#endif
6894#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006896#endif
6897#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006899#endif
6900#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006901 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006902#endif
6903#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006905#endif
6906#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006908#endif
6909#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006910 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006911#endif
6912#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006914#endif
6915#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006916 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006917#endif
6918#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006919 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006920#endif
6921#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006922 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006923#endif
6924#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006925 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006926#endif
6927#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006929#endif
6930#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006932#endif
6933#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006934 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006935#endif
6936#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006938#endif
6939#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006940 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006941#endif
6942#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006943 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006944#endif
6945#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006946 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00006947#endif
6948#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006950#endif
6951#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006953#endif
6954#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006955 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006956#endif
6957#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006958 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006959#endif
6960#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006961 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006962#endif
Fred Draked86ed291999-12-15 15:34:33 +00006963#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00006964 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00006965#endif
Fred Drakec9680921999-12-13 16:37:25 +00006966#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00006967 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00006968#endif
6969#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006971#endif
6972#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00006974#endif
6975#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006976 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006977#endif
6978#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006979 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006980#endif
6981#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006982 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006983#endif
6984#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00006985 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00006986#endif
6987#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006989#endif
6990#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006991 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006992#endif
6993#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006994 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006995#endif
6996#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006997 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006998#endif
6999#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007001#endif
7002#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007003 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007004#endif
7005#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007006 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007007#endif
7008#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007009 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007010#endif
7011#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007013#endif
7014#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007016#endif
7017#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007019#endif
7020#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007022#endif
7023#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007024 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007025#endif
7026#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007028#endif
7029#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007030 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007031#endif
7032#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007033 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007034#endif
7035#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007036 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007037#endif
7038#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007039 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007040#endif
7041#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007042 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007043#endif
7044#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007045 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007046#endif
7047#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007048 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007049#endif
7050#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007051 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007052#endif
7053#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007054 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007055#endif
7056#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007057 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007058#endif
7059#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007060 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007061#endif
7062#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007064#endif
7065#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007066 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007067#endif
7068#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007070#endif
7071#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007073#endif
7074#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007075 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007076#endif
7077#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007078 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007079#endif
7080#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007081 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007082#endif
7083#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007085#endif
7086#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007087 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007088#endif
7089#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007091#endif
7092#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007094#endif
7095#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007097#endif
7098#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007099 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007100#endif
7101};
7102
7103static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007104conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007105{
7106 return conv_confname(arg, valuep, posix_constants_sysconf,
7107 sizeof(posix_constants_sysconf)
7108 / sizeof(struct constdef));
7109}
7110
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007111PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007112"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007113Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007114
7115static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007116posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007117{
7118 PyObject *result = NULL;
7119 int name;
7120
7121 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7122 int value;
7123
7124 errno = 0;
7125 value = sysconf(name);
7126 if (value == -1 && errno != 0)
7127 posix_error();
7128 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007129 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007130 }
7131 return result;
7132}
7133#endif
7134
7135
Fred Drakebec628d1999-12-15 18:31:10 +00007136/* This code is used to ensure that the tables of configuration value names
7137 * are in sorted order as required by conv_confname(), and also to build the
7138 * the exported dictionaries that are used to publish information about the
7139 * names available on the host platform.
7140 *
7141 * Sorting the table at runtime ensures that the table is properly ordered
7142 * when used, even for platforms we're not able to test on. It also makes
7143 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007144 */
Fred Drakebec628d1999-12-15 18:31:10 +00007145
7146static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007147cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007148{
7149 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007151 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007153
7154 return strcmp(c1->name, c2->name);
7155}
7156
7157static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007158setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007160{
Fred Drakebec628d1999-12-15 18:31:10 +00007161 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007162 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007163
7164 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7165 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007166 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007168
Barry Warsaw3155db32000-04-13 15:20:40 +00007169 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007170 PyObject *o = PyLong_FromLong(table[i].value);
7171 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7172 Py_XDECREF(o);
7173 Py_DECREF(d);
7174 return -1;
7175 }
7176 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007177 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007178 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007179}
7180
Fred Drakebec628d1999-12-15 18:31:10 +00007181/* Return -1 on failure, 0 on success. */
7182static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007183setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007184{
7185#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007186 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007187 sizeof(posix_constants_pathconf)
7188 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007189 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007191#endif
7192#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007193 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007194 sizeof(posix_constants_confstr)
7195 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007196 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007197 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007198#endif
7199#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007200 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007201 sizeof(posix_constants_sysconf)
7202 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007203 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007205#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007206 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007207}
Fred Draked86ed291999-12-15 15:34:33 +00007208
7209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007210PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007211"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007212Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007213in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007214
7215static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007216posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007217{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007218 abort();
7219 /*NOTREACHED*/
7220 Py_FatalError("abort() called from Python code didn't abort!");
7221 return NULL;
7222}
Fred Drakebec628d1999-12-15 18:31:10 +00007223
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007224#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007225PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007226"startfile(filepath [, operation]) - Start a file with its associated\n\
7227application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007228\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007229When \"operation\" is not specified or \"open\", this acts like\n\
7230double-clicking the file in Explorer, or giving the file name as an\n\
7231argument to the DOS \"start\" command: the file is opened with whatever\n\
7232application (if any) its extension is associated.\n\
7233When another \"operation\" is given, it specifies what should be done with\n\
7234the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007235\n\
7236startfile returns as soon as the associated application is launched.\n\
7237There is no option to wait for the application to close, and no way\n\
7238to retrieve the application's exit status.\n\
7239\n\
7240The filepath is relative to the current directory. If you want to use\n\
7241an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007242the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007243
7244static PyObject *
7245win32_startfile(PyObject *self, PyObject *args)
7246{
Victor Stinner8c62be82010-05-06 00:08:46 +00007247 PyObject *ofilepath;
7248 char *filepath;
7249 char *operation = NULL;
7250 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007251
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 PyObject *unipath, *woperation = NULL;
7253 if (!PyArg_ParseTuple(args, "U|s:startfile",
7254 &unipath, &operation)) {
7255 PyErr_Clear();
7256 goto normal;
7257 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007258
Victor Stinner8c62be82010-05-06 00:08:46 +00007259 if (operation) {
7260 woperation = PyUnicode_DecodeASCII(operation,
7261 strlen(operation), NULL);
7262 if (!woperation) {
7263 PyErr_Clear();
7264 operation = NULL;
7265 goto normal;
7266 }
7267 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007268
Victor Stinner8c62be82010-05-06 00:08:46 +00007269 Py_BEGIN_ALLOW_THREADS
7270 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7271 PyUnicode_AS_UNICODE(unipath),
7272 NULL, NULL, SW_SHOWNORMAL);
7273 Py_END_ALLOW_THREADS
7274
7275 Py_XDECREF(woperation);
7276 if (rc <= (HINSTANCE)32) {
7277 PyObject *errval = win32_error_unicode("startfile",
7278 PyUnicode_AS_UNICODE(unipath));
7279 return errval;
7280 }
7281 Py_INCREF(Py_None);
7282 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007283
7284normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7286 PyUnicode_FSConverter, &ofilepath,
7287 &operation))
7288 return NULL;
7289 filepath = PyBytes_AsString(ofilepath);
7290 Py_BEGIN_ALLOW_THREADS
7291 rc = ShellExecute((HWND)0, operation, filepath,
7292 NULL, NULL, SW_SHOWNORMAL);
7293 Py_END_ALLOW_THREADS
7294 if (rc <= (HINSTANCE)32) {
7295 PyObject *errval = win32_error("startfile", filepath);
7296 Py_DECREF(ofilepath);
7297 return errval;
7298 }
7299 Py_DECREF(ofilepath);
7300 Py_INCREF(Py_None);
7301 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007302}
7303#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007304
Martin v. Löwis438b5342002-12-27 10:16:42 +00007305#ifdef HAVE_GETLOADAVG
7306PyDoc_STRVAR(posix_getloadavg__doc__,
7307"getloadavg() -> (float, float, float)\n\n\
7308Return the number of processes in the system run queue averaged over\n\
7309the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7310was unobtainable");
7311
7312static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007313posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007314{
7315 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007316 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7318 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007319 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007321}
7322#endif
7323
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007324#ifdef MS_WINDOWS
7325
7326PyDoc_STRVAR(win32_urandom__doc__,
7327"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007328Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007329
7330typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7331 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7332 DWORD dwFlags );
7333typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7334 BYTE *pbBuffer );
7335
7336static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007337/* This handle is never explicitly released. Instead, the operating
7338 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007339static HCRYPTPROV hCryptProv = 0;
7340
Tim Peters4ad82172004-08-30 17:02:04 +00007341static PyObject*
7342win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007343{
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 int howMany;
7345 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007346
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 /* Read arguments */
7348 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7349 return NULL;
7350 if (howMany < 0)
7351 return PyErr_Format(PyExc_ValueError,
7352 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007353
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 if (hCryptProv == 0) {
7355 HINSTANCE hAdvAPI32 = NULL;
7356 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007357
Victor Stinner8c62be82010-05-06 00:08:46 +00007358 /* Obtain handle to the DLL containing CryptoAPI
7359 This should not fail */
7360 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7361 if(hAdvAPI32 == NULL)
7362 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007363
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 /* Obtain pointers to the CryptoAPI functions
7365 This will fail on some early versions of Win95 */
7366 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7367 hAdvAPI32,
7368 "CryptAcquireContextA");
7369 if (pCryptAcquireContext == NULL)
7370 return PyErr_Format(PyExc_NotImplementedError,
7371 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007372
Victor Stinner8c62be82010-05-06 00:08:46 +00007373 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7374 hAdvAPI32, "CryptGenRandom");
7375 if (pCryptGenRandom == NULL)
7376 return PyErr_Format(PyExc_NotImplementedError,
7377 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007378
Victor Stinner8c62be82010-05-06 00:08:46 +00007379 /* Acquire context */
7380 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7381 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7382 return win32_error("CryptAcquireContext", NULL);
7383 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007384
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 /* Allocate bytes */
7386 result = PyBytes_FromStringAndSize(NULL, howMany);
7387 if (result != NULL) {
7388 /* Get random data */
7389 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7390 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7391 PyBytes_AS_STRING(result))) {
7392 Py_DECREF(result);
7393 return win32_error("CryptGenRandom", NULL);
7394 }
7395 }
7396 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007397}
7398#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007399
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007400PyDoc_STRVAR(device_encoding__doc__,
7401"device_encoding(fd) -> str\n\n\
7402Return a string describing the encoding of the device\n\
7403if the output is a terminal; else return None.");
7404
7405static PyObject *
7406device_encoding(PyObject *self, PyObject *args)
7407{
Victor Stinner8c62be82010-05-06 00:08:46 +00007408 int fd;
7409 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7410 return NULL;
7411 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7412 Py_INCREF(Py_None);
7413 return Py_None;
7414 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007415#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007416 if (fd == 0) {
7417 char buf[100];
7418 sprintf(buf, "cp%d", GetConsoleCP());
7419 return PyUnicode_FromString(buf);
7420 }
7421 if (fd == 1 || fd == 2) {
7422 char buf[100];
7423 sprintf(buf, "cp%d", GetConsoleOutputCP());
7424 return PyUnicode_FromString(buf);
7425 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007426#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007427 {
7428 char *codeset = nl_langinfo(CODESET);
7429 if (codeset != NULL && codeset[0] != 0)
7430 return PyUnicode_FromString(codeset);
7431 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007432#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007433 Py_INCREF(Py_None);
7434 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007435}
7436
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007437#ifdef __VMS
7438/* Use openssl random routine */
7439#include <openssl/rand.h>
7440PyDoc_STRVAR(vms_urandom__doc__,
7441"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007442Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007443
7444static PyObject*
7445vms_urandom(PyObject *self, PyObject *args)
7446{
Victor Stinner8c62be82010-05-06 00:08:46 +00007447 int howMany;
7448 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007449
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 /* Read arguments */
7451 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7452 return NULL;
7453 if (howMany < 0)
7454 return PyErr_Format(PyExc_ValueError,
7455 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007456
Victor Stinner8c62be82010-05-06 00:08:46 +00007457 /* Allocate bytes */
7458 result = PyBytes_FromStringAndSize(NULL, howMany);
7459 if (result != NULL) {
7460 /* Get random data */
7461 if (RAND_pseudo_bytes((unsigned char*)
7462 PyBytes_AS_STRING(result),
7463 howMany) < 0) {
7464 Py_DECREF(result);
7465 return PyErr_Format(PyExc_ValueError,
7466 "RAND_pseudo_bytes");
7467 }
7468 }
7469 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007470}
7471#endif
7472
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007473#ifdef HAVE_SETRESUID
7474PyDoc_STRVAR(posix_setresuid__doc__,
7475"setresuid(ruid, euid, suid)\n\n\
7476Set the current process's real, effective, and saved user ids.");
7477
7478static PyObject*
7479posix_setresuid (PyObject *self, PyObject *args)
7480{
Victor Stinner8c62be82010-05-06 00:08:46 +00007481 /* We assume uid_t is no larger than a long. */
7482 long ruid, euid, suid;
7483 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7484 return NULL;
7485 if (setresuid(ruid, euid, suid) < 0)
7486 return posix_error();
7487 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007488}
7489#endif
7490
7491#ifdef HAVE_SETRESGID
7492PyDoc_STRVAR(posix_setresgid__doc__,
7493"setresgid(rgid, egid, sgid)\n\n\
7494Set the current process's real, effective, and saved group ids.");
7495
7496static PyObject*
7497posix_setresgid (PyObject *self, PyObject *args)
7498{
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 /* We assume uid_t is no larger than a long. */
7500 long rgid, egid, sgid;
7501 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7502 return NULL;
7503 if (setresgid(rgid, egid, sgid) < 0)
7504 return posix_error();
7505 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007506}
7507#endif
7508
7509#ifdef HAVE_GETRESUID
7510PyDoc_STRVAR(posix_getresuid__doc__,
7511"getresuid() -> (ruid, euid, suid)\n\n\
7512Get tuple of the current process's real, effective, and saved user ids.");
7513
7514static PyObject*
7515posix_getresuid (PyObject *self, PyObject *noargs)
7516{
Victor Stinner8c62be82010-05-06 00:08:46 +00007517 uid_t ruid, euid, suid;
7518 long l_ruid, l_euid, l_suid;
7519 if (getresuid(&ruid, &euid, &suid) < 0)
7520 return posix_error();
7521 /* Force the values into long's as we don't know the size of uid_t. */
7522 l_ruid = ruid;
7523 l_euid = euid;
7524 l_suid = suid;
7525 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007526}
7527#endif
7528
7529#ifdef HAVE_GETRESGID
7530PyDoc_STRVAR(posix_getresgid__doc__,
7531"getresgid() -> (rgid, egid, sgid)\n\n\
7532Get tuple of the current process's real, effective, and saved user ids.");
7533
7534static PyObject*
7535posix_getresgid (PyObject *self, PyObject *noargs)
7536{
Victor Stinner8c62be82010-05-06 00:08:46 +00007537 uid_t rgid, egid, sgid;
7538 long l_rgid, l_egid, l_sgid;
7539 if (getresgid(&rgid, &egid, &sgid) < 0)
7540 return posix_error();
7541 /* Force the values into long's as we don't know the size of uid_t. */
7542 l_rgid = rgid;
7543 l_egid = egid;
7544 l_sgid = sgid;
7545 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007546}
7547#endif
7548
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007549static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007550 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007551#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007553#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007554 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007555#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007556 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007557#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007558 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007559#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007561#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007562#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007564#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007565#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007566 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007567#endif /* HAVE_LCHMOD */
7568#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007569 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007570#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007571#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007572 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007573#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007574#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007576#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007577#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007578 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007579#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007580#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007581 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007582#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007583#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007584 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7585 METH_NOARGS, posix_getcwd__doc__},
7586 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7587 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007588#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007589#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007590 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007591#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007592 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7593 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7594 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007595#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007597#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007598#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007599 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007600#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007601#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7602 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
7603#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7604 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7605 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7606 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007608#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007609 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007610#endif /* HAVE_SYMLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007611#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin74e45612010-07-09 15:58:59 +00007612 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
7613 win_symlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007614#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007615#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007617#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007618 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007619#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007620 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007621#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7623 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7624 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007625#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007626 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007627#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007629#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7631 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007632#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007633#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7635 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007636#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007637 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7638 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007639#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007640#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007641#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007643#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007644#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007645 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007646#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007647#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007648 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007649#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007650#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007652#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007653#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007654 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007655#endif /* HAVE_GETEGID */
7656#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007657 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007658#endif /* HAVE_GETEUID */
7659#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007660 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007661#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007662#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007664#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007665 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007666#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007668#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007669#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007670 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007671#endif /* HAVE_GETPPID */
7672#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007673 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007674#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007675#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007676 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007677#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007678#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007679 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007680#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007681#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007682 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007683#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007684#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007685 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007686#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007687#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007688 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7689 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007690#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007691#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007692 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007693#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007694#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007695 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007696#endif /* HAVE_SETEUID */
7697#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007699#endif /* HAVE_SETEGID */
7700#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007701 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007702#endif /* HAVE_SETREUID */
7703#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007704 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007705#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007706#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007707 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007708#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007709#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007710 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007711#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007712#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007713 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007714#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007715#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007716 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007717#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007718#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007719 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007720#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007721#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007722 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007723#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007724#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007725 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007726#endif /* HAVE_WAIT3 */
7727#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007728 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007729#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007730#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007731 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007732#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007733#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007735#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007736#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007737 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007738#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007739#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007740 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007741#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007742#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007743 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007744#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007745#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007747#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7749 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7750 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7751 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7752 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7753 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7754 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7755 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7756 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7757 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7758 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007759#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007761#endif
7762#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007764#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007765#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007767#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007768#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7770 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7771 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007772#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007773#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007774 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007775#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007776#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007777 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007778#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007779#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007781#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007782 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007783#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007785#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007786#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007787 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007788#endif
7789#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007790 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007791#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007792#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007793#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007795#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007796#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007797 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007798#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007799#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007800 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007801#endif /* WIFSTOPPED */
7802#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007803 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007804#endif /* WIFSIGNALED */
7805#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007806 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007807#endif /* WIFEXITED */
7808#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007810#endif /* WEXITSTATUS */
7811#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007813#endif /* WTERMSIG */
7814#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007816#endif /* WSTOPSIG */
7817#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007818#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007820#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007821#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007823#endif
Fred Drakec9680921999-12-13 16:37:25 +00007824#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007825 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007826#endif
7827#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007828 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007829#endif
7830#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007832#endif
7833#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007834 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007835#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007837#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00007839 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007840#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007841#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007843#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007844 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007846 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007847 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007848 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007849 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007850#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007852#endif
7853#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007855#endif
7856#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007858#endif
7859#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007860 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007861#endif
7862
Victor Stinner8c62be82010-05-06 00:08:46 +00007863 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007864};
7865
7866
Barry Warsaw4a342091996-12-19 23:50:02 +00007867static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007868ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007869{
Victor Stinner8c62be82010-05-06 00:08:46 +00007870 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007871}
7872
Guido van Rossumd48f2521997-12-05 22:19:34 +00007873#if defined(PYOS_OS2)
7874/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007875static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007876{
7877 APIRET rc;
7878 ULONG values[QSV_MAX+1];
7879 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007880 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007881
7882 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007883 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007884 Py_END_ALLOW_THREADS
7885
7886 if (rc != NO_ERROR) {
7887 os2_error(rc);
7888 return -1;
7889 }
7890
Fred Drake4d1e64b2002-04-15 19:40:07 +00007891 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7892 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7893 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7894 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7895 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7896 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7897 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007898
7899 switch (values[QSV_VERSION_MINOR]) {
7900 case 0: ver = "2.00"; break;
7901 case 10: ver = "2.10"; break;
7902 case 11: ver = "2.11"; break;
7903 case 30: ver = "3.00"; break;
7904 case 40: ver = "4.00"; break;
7905 case 50: ver = "5.00"; break;
7906 default:
Tim Peters885d4572001-11-28 20:27:42 +00007907 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007908 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007909 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007910 ver = &tmp[0];
7911 }
7912
7913 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007914 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007915 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007916
7917 /* Add Indicator of Which Drive was Used to Boot the System */
7918 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7919 tmp[1] = ':';
7920 tmp[2] = '\0';
7921
Fred Drake4d1e64b2002-04-15 19:40:07 +00007922 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007923}
7924#endif
7925
Barry Warsaw4a342091996-12-19 23:50:02 +00007926static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007927all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007928{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007929#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007930 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007931#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007932#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007933 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007934#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007935#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007937#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007938#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007940#endif
Fred Drakec9680921999-12-13 16:37:25 +00007941#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007942 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00007943#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007944#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007945 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007946#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007947#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007948 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007949#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007950#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00007951 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007952#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007953#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007955#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007956#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007957 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007958#endif
7959#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007960 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007961#endif
7962#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00007963 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007964#endif
7965#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00007966 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007967#endif
7968#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007970#endif
7971#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00007972 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007973#endif
7974#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007975 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007976#endif
7977#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007978 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007979#endif
7980#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007981 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007982#endif
7983#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007984 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007985#endif
7986#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007987 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007988#endif
7989#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00007990 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007991#endif
7992#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007993 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007994#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007995#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007996 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007997#endif
7998#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008000#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008001#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008002 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008003#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008004#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008005 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008006#endif
8007#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008008 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008009#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008010
Tim Peters5aa91602002-01-30 05:46:57 +00008011/* MS Windows */
8012#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 /* Don't inherit in child processes. */
8014 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008015#endif
8016#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 /* Optimize for short life (keep in memory). */
8018 /* MS forgot to define this one with a non-underscore form too. */
8019 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008020#endif
8021#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 /* Automatically delete when last handle is closed. */
8023 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008024#endif
8025#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008026 /* Optimize for random access. */
8027 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008028#endif
8029#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008030 /* Optimize for sequential access. */
8031 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008032#endif
8033
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008034/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008035#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008036 /* Send a SIGIO signal whenever input or output
8037 becomes available on file descriptor */
8038 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008039#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008040#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008041 /* Direct disk access. */
8042 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008043#endif
8044#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008045 /* Must be a directory. */
8046 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008047#endif
8048#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008049 /* Do not follow links. */
8050 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008051#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008052#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008053 /* Do not update the access time. */
8054 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008055#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008056
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008058#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008060#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008061#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008062 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008063#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008064#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008065 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008066#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008067#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008068 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008069#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008070#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008072#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008073#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008074 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008075#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008076#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008077 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008078#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008079#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008080 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008081#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008082#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008083 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008084#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008085#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008086 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008087#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008088#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008089 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008090#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008091#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008092 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008093#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008094#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008095 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008096#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008097#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008098 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008099#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008100#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008101 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008102#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008103#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008104 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008105#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008106#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008107 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008108#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008109
Guido van Rossum246bc171999-02-01 23:54:31 +00008110#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008111#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8113 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8114 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8115 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8116 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8117 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8118 if (ins(d, "P_PM", (long)P_PM)) return -1;
8119 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8120 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8121 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8122 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8123 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8124 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8125 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8126 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8127 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8128 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8129 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8130 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8131 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008132#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8134 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8135 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8136 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8137 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008138#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008139#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008140
Guido van Rossumd48f2521997-12-05 22:19:34 +00008141#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008143#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008145}
8146
8147
Tim Peters5aa91602002-01-30 05:46:57 +00008148#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008149#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008150#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008151
8152#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008153#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008154#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008155
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008156#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008157#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008158#define MODNAME "posix"
8159#endif
8160
Martin v. Löwis1a214512008-06-11 05:26:20 +00008161static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008162 PyModuleDef_HEAD_INIT,
8163 MODNAME,
8164 posix__doc__,
8165 -1,
8166 posix_methods,
8167 NULL,
8168 NULL,
8169 NULL,
8170 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008171};
8172
8173
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008174PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008175INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008176{
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008178
Victor Stinner8c62be82010-05-06 00:08:46 +00008179 m = PyModule_Create(&posixmodule);
8180 if (m == NULL)
8181 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008182
Victor Stinner8c62be82010-05-06 00:08:46 +00008183 /* Initialize environ dictionary */
8184 v = convertenviron();
8185 Py_XINCREF(v);
8186 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8187 return NULL;
8188 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008189
Victor Stinner8c62be82010-05-06 00:08:46 +00008190 if (all_ins(m))
8191 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008192
Victor Stinner8c62be82010-05-06 00:08:46 +00008193 if (setup_confname_tables(m))
8194 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008195
Victor Stinner8c62be82010-05-06 00:08:46 +00008196 Py_INCREF(PyExc_OSError);
8197 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008198
Guido van Rossumb3d39562000-01-31 18:41:26 +00008199#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008200 if (posix_putenv_garbage == NULL)
8201 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008202#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008203
Victor Stinner8c62be82010-05-06 00:08:46 +00008204 if (!initialized) {
8205 stat_result_desc.name = MODNAME ".stat_result";
8206 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8207 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8208 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8209 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8210 structseq_new = StatResultType.tp_new;
8211 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008212
Victor Stinner8c62be82010-05-06 00:08:46 +00008213 statvfs_result_desc.name = MODNAME ".statvfs_result";
8214 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008215#ifdef NEED_TICKS_PER_SECOND
8216# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008217 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008218# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008219 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008220# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008221 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008222# endif
8223#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008224 }
8225 Py_INCREF((PyObject*) &StatResultType);
8226 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8227 Py_INCREF((PyObject*) &StatVFSResultType);
8228 PyModule_AddObject(m, "statvfs_result",
8229 (PyObject*) &StatVFSResultType);
8230 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008231
8232#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008233 /*
8234 * Step 2 of weak-linking support on Mac OS X.
8235 *
8236 * The code below removes functions that are not available on the
8237 * currently active platform.
8238 *
8239 * This block allow one to use a python binary that was build on
8240 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8241 * OSX 10.4.
8242 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008243#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008244 if (fstatvfs == NULL) {
8245 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8246 return NULL;
8247 }
8248 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008249#endif /* HAVE_FSTATVFS */
8250
8251#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 if (statvfs == NULL) {
8253 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8254 return NULL;
8255 }
8256 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008257#endif /* HAVE_STATVFS */
8258
8259# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008260 if (lchown == NULL) {
8261 if (PyObject_DelAttrString(m, "lchown") == -1) {
8262 return NULL;
8263 }
8264 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008265#endif /* HAVE_LCHOWN */
8266
8267
8268#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008270
Guido van Rossumb6775db1994-08-01 11:34:53 +00008271}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008272
8273#ifdef __cplusplus
8274}
8275#endif