blob: 63172787ebbe31649ab83ab4c5b946bad7085134 [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];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004169
4170 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
4171 * This is a helper variable to store the intermediate result when
4172 * that happens.
4173 *
4174 * To keep the code readable the OSX behaviour is unconditional,
4175 * according to the POSIX spec this should be safe on all unix-y
4176 * systems.
4177 */
4178 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00004179 int n;
Fred Drakec9680921999-12-13 16:37:25 +00004180
Victor Stinner8c62be82010-05-06 00:08:46 +00004181 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004182 if (n < 0) {
4183 if (errno == EINVAL) {
4184 n = getgroups(0, NULL);
4185 if (n == -1) {
4186 return posix_error();
4187 }
4188 if (n == 0) {
4189 /* Avoid malloc(0) */
4190 alt_grouplist = grouplist;
4191 } else {
4192 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
4193 if (alt_grouplist == NULL) {
4194 errno = EINVAL;
4195 return posix_error();
4196 }
4197 n = getgroups(n, alt_grouplist);
4198 if (n == -1) {
4199 PyMem_Free(alt_grouplist);
4200 return posix_error();
4201 }
4202 }
4203 } else {
4204 return posix_error();
4205 }
4206 }
4207 result = PyList_New(n);
4208 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004209 int i;
4210 for (i = 0; i < n; ++i) {
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004211 PyObject *o = PyLong_FromLong((long)alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00004212 if (o == NULL) {
4213 Py_DECREF(result);
4214 result = NULL;
4215 break;
Fred Drakec9680921999-12-13 16:37:25 +00004216 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004217 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00004218 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00004219 }
4220
4221 if (alt_grouplist != grouplist) {
4222 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00004223 }
Neal Norwitze241ce82003-02-17 18:17:05 +00004224
Fred Drakec9680921999-12-13 16:37:25 +00004225 return result;
4226}
4227#endif
4228
Antoine Pitroub7572f02009-12-02 20:46:48 +00004229#ifdef HAVE_INITGROUPS
4230PyDoc_STRVAR(posix_initgroups__doc__,
4231"initgroups(username, gid) -> None\n\n\
4232Call the system initgroups() to initialize the group access list with all of\n\
4233the groups of which the specified username is a member, plus the specified\n\
4234group id.");
4235
4236static PyObject *
4237posix_initgroups(PyObject *self, PyObject *args)
4238{
Victor Stinner8c62be82010-05-06 00:08:46 +00004239 char *username;
4240 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004241
Victor Stinner8c62be82010-05-06 00:08:46 +00004242 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
4243 return NULL;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004244
Victor Stinner8c62be82010-05-06 00:08:46 +00004245 if (initgroups(username, (gid_t) gid) == -1)
4246 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00004247
Victor Stinner8c62be82010-05-06 00:08:46 +00004248 Py_INCREF(Py_None);
4249 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00004250}
4251#endif
4252
Martin v. Löwis606edc12002-06-13 21:09:11 +00004253#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004254PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004255"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00004256Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00004257
4258static PyObject *
4259posix_getpgid(PyObject *self, PyObject *args)
4260{
Victor Stinner8c62be82010-05-06 00:08:46 +00004261 pid_t pid, pgid;
4262 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
4263 return NULL;
4264 pgid = getpgid(pid);
4265 if (pgid < 0)
4266 return posix_error();
4267 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00004268}
4269#endif /* HAVE_GETPGID */
4270
4271
Guido van Rossumb6775db1994-08-01 11:34:53 +00004272#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004273PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004274"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004275Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004276
Barry Warsaw53699e91996-12-10 23:23:01 +00004277static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004278posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00004279{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004280#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004281 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004282#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004283 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004284#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00004285}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004286#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00004287
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004288
Guido van Rossumb6775db1994-08-01 11:34:53 +00004289#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004290PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004291"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00004292Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004293
Barry Warsaw53699e91996-12-10 23:23:01 +00004294static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004295posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004296{
Guido van Rossum64933891994-10-20 21:56:42 +00004297#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00004298 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004299#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004300 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00004301#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00004302 return posix_error();
4303 Py_INCREF(Py_None);
4304 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004305}
4306
Guido van Rossumb6775db1994-08-01 11:34:53 +00004307#endif /* HAVE_SETPGRP */
4308
Guido van Rossumad0ee831995-03-01 10:34:45 +00004309#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004310PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004311"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004312Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004313
Barry Warsaw53699e91996-12-10 23:23:01 +00004314static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004315posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004316{
Victor Stinner8c62be82010-05-06 00:08:46 +00004317 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00004318}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004319#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004321
Fred Drake12c6e2d1999-12-14 21:25:03 +00004322#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004323PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004324"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004325Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00004326
4327static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004328posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00004329{
Victor Stinner8c62be82010-05-06 00:08:46 +00004330 PyObject *result = NULL;
4331 char *name;
4332 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00004333
Victor Stinner8c62be82010-05-06 00:08:46 +00004334 errno = 0;
4335 name = getlogin();
4336 if (name == NULL) {
4337 if (errno)
4338 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00004339 else
Victor Stinner8c62be82010-05-06 00:08:46 +00004340 PyErr_SetString(PyExc_OSError,
4341 "unable to determine login name");
4342 }
4343 else
4344 result = PyUnicode_FromString(name);
4345 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00004346
Fred Drake12c6e2d1999-12-14 21:25:03 +00004347 return result;
4348}
4349#endif
4350
Guido van Rossumad0ee831995-03-01 10:34:45 +00004351#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004352PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004353"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004354Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004355
Barry Warsaw53699e91996-12-10 23:23:01 +00004356static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004357posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004358{
Victor Stinner8c62be82010-05-06 00:08:46 +00004359 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004360}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004361#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004362
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004363
Guido van Rossumad0ee831995-03-01 10:34:45 +00004364#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004365PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004366"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004367Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004368
Barry Warsaw53699e91996-12-10 23:23:01 +00004369static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004370posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004371{
Victor Stinner8c62be82010-05-06 00:08:46 +00004372 pid_t pid;
4373 int sig;
4374 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4375 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004376#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004377 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4378 APIRET rc;
4379 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004380 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004381
4382 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4383 APIRET rc;
4384 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004385 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004386
4387 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004388 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004389#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004390 if (kill(pid, sig) == -1)
4391 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004392#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004393 Py_INCREF(Py_None);
4394 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004395}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004396#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004397
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004398#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004399PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004400"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004401Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004402
4403static PyObject *
4404posix_killpg(PyObject *self, PyObject *args)
4405{
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 int sig;
4407 pid_t pgid;
4408 /* XXX some man pages make the `pgid` parameter an int, others
4409 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4410 take the same type. Moreover, pid_t is always at least as wide as
4411 int (else compilation of this module fails), which is safe. */
4412 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4413 return NULL;
4414 if (killpg(pgid, sig) == -1)
4415 return posix_error();
4416 Py_INCREF(Py_None);
4417 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004418}
4419#endif
4420
Brian Curtineb24d742010-04-12 17:16:38 +00004421#ifdef MS_WINDOWS
4422PyDoc_STRVAR(win32_kill__doc__,
4423"kill(pid, sig)\n\n\
4424Kill a process with a signal.");
4425
4426static PyObject *
4427win32_kill(PyObject *self, PyObject *args)
4428{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00004429 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004430 DWORD pid, sig, err;
4431 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004432
Victor Stinner8c62be82010-05-06 00:08:46 +00004433 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4434 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004435
Victor Stinner8c62be82010-05-06 00:08:46 +00004436 /* Console processes which share a common console can be sent CTRL+C or
4437 CTRL+BREAK events, provided they handle said events. */
4438 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4439 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4440 err = GetLastError();
4441 PyErr_SetFromWindowsErr(err);
4442 }
4443 else
4444 Py_RETURN_NONE;
4445 }
Brian Curtineb24d742010-04-12 17:16:38 +00004446
Victor Stinner8c62be82010-05-06 00:08:46 +00004447 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4448 attempt to open and terminate the process. */
4449 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4450 if (handle == NULL) {
4451 err = GetLastError();
4452 return PyErr_SetFromWindowsErr(err);
4453 }
Brian Curtineb24d742010-04-12 17:16:38 +00004454
Victor Stinner8c62be82010-05-06 00:08:46 +00004455 if (TerminateProcess(handle, sig) == 0) {
4456 err = GetLastError();
4457 result = PyErr_SetFromWindowsErr(err);
4458 } else {
4459 Py_INCREF(Py_None);
4460 result = Py_None;
4461 }
Brian Curtineb24d742010-04-12 17:16:38 +00004462
Victor Stinner8c62be82010-05-06 00:08:46 +00004463 CloseHandle(handle);
4464 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004465}
4466#endif /* MS_WINDOWS */
4467
Guido van Rossumc0125471996-06-28 18:55:32 +00004468#ifdef HAVE_PLOCK
4469
4470#ifdef HAVE_SYS_LOCK_H
4471#include <sys/lock.h>
4472#endif
4473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004474PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004475"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004476Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004477
Barry Warsaw53699e91996-12-10 23:23:01 +00004478static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004479posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004480{
Victor Stinner8c62be82010-05-06 00:08:46 +00004481 int op;
4482 if (!PyArg_ParseTuple(args, "i:plock", &op))
4483 return NULL;
4484 if (plock(op) == -1)
4485 return posix_error();
4486 Py_INCREF(Py_None);
4487 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004488}
4489#endif
4490
Guido van Rossumb6775db1994-08-01 11:34:53 +00004491#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004492PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004493"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004494Set the current process's user id.");
4495
Barry Warsaw53699e91996-12-10 23:23:01 +00004496static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004497posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004498{
Victor Stinner8c62be82010-05-06 00:08:46 +00004499 long uid_arg;
4500 uid_t uid;
4501 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4502 return NULL;
4503 uid = uid_arg;
4504 if (uid != uid_arg) {
4505 PyErr_SetString(PyExc_OverflowError, "user id too big");
4506 return NULL;
4507 }
4508 if (setuid(uid) < 0)
4509 return posix_error();
4510 Py_INCREF(Py_None);
4511 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004512}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004513#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004514
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004515
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004516#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004517PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004518"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004519Set the current process's effective user id.");
4520
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004521static PyObject *
4522posix_seteuid (PyObject *self, PyObject *args)
4523{
Victor Stinner8c62be82010-05-06 00:08:46 +00004524 long euid_arg;
4525 uid_t euid;
4526 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4527 return NULL;
4528 euid = euid_arg;
4529 if (euid != euid_arg) {
4530 PyErr_SetString(PyExc_OverflowError, "user id too big");
4531 return NULL;
4532 }
4533 if (seteuid(euid) < 0) {
4534 return posix_error();
4535 } else {
4536 Py_INCREF(Py_None);
4537 return Py_None;
4538 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004539}
4540#endif /* HAVE_SETEUID */
4541
4542#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004543PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004544"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004545Set the current process's effective group id.");
4546
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004547static PyObject *
4548posix_setegid (PyObject *self, PyObject *args)
4549{
Victor Stinner8c62be82010-05-06 00:08:46 +00004550 long egid_arg;
4551 gid_t egid;
4552 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4553 return NULL;
4554 egid = egid_arg;
4555 if (egid != egid_arg) {
4556 PyErr_SetString(PyExc_OverflowError, "group id too big");
4557 return NULL;
4558 }
4559 if (setegid(egid) < 0) {
4560 return posix_error();
4561 } else {
4562 Py_INCREF(Py_None);
4563 return Py_None;
4564 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004565}
4566#endif /* HAVE_SETEGID */
4567
4568#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004569PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004570"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004571Set the current process's real and effective user ids.");
4572
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004573static PyObject *
4574posix_setreuid (PyObject *self, PyObject *args)
4575{
Victor Stinner8c62be82010-05-06 00:08:46 +00004576 long ruid_arg, euid_arg;
4577 uid_t ruid, euid;
4578 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4579 return NULL;
4580 if (ruid_arg == -1)
4581 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4582 else
4583 ruid = ruid_arg; /* otherwise, assign from our long */
4584 if (euid_arg == -1)
4585 euid = (uid_t)-1;
4586 else
4587 euid = euid_arg;
4588 if ((euid_arg != -1 && euid != euid_arg) ||
4589 (ruid_arg != -1 && ruid != ruid_arg)) {
4590 PyErr_SetString(PyExc_OverflowError, "user id too big");
4591 return NULL;
4592 }
4593 if (setreuid(ruid, euid) < 0) {
4594 return posix_error();
4595 } else {
4596 Py_INCREF(Py_None);
4597 return Py_None;
4598 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004599}
4600#endif /* HAVE_SETREUID */
4601
4602#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004603PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004604"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004605Set the current process's real and effective group ids.");
4606
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004607static PyObject *
4608posix_setregid (PyObject *self, PyObject *args)
4609{
Victor Stinner8c62be82010-05-06 00:08:46 +00004610 long rgid_arg, egid_arg;
4611 gid_t rgid, egid;
4612 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4613 return NULL;
4614 if (rgid_arg == -1)
4615 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4616 else
4617 rgid = rgid_arg; /* otherwise, assign from our long */
4618 if (egid_arg == -1)
4619 egid = (gid_t)-1;
4620 else
4621 egid = egid_arg;
4622 if ((egid_arg != -1 && egid != egid_arg) ||
4623 (rgid_arg != -1 && rgid != rgid_arg)) {
4624 PyErr_SetString(PyExc_OverflowError, "group id too big");
4625 return NULL;
4626 }
4627 if (setregid(rgid, egid) < 0) {
4628 return posix_error();
4629 } else {
4630 Py_INCREF(Py_None);
4631 return Py_None;
4632 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004633}
4634#endif /* HAVE_SETREGID */
4635
Guido van Rossumb6775db1994-08-01 11:34:53 +00004636#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004637PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004638"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004639Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004640
Barry Warsaw53699e91996-12-10 23:23:01 +00004641static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004642posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004643{
Victor Stinner8c62be82010-05-06 00:08:46 +00004644 long gid_arg;
4645 gid_t gid;
4646 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4647 return NULL;
4648 gid = gid_arg;
4649 if (gid != gid_arg) {
4650 PyErr_SetString(PyExc_OverflowError, "group id too big");
4651 return NULL;
4652 }
4653 if (setgid(gid) < 0)
4654 return posix_error();
4655 Py_INCREF(Py_None);
4656 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004657}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004658#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004659
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004660#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004661PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004662"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004663Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004664
4665static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004666posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004667{
Victor Stinner8c62be82010-05-06 00:08:46 +00004668 int i, len;
4669 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004670
Victor Stinner8c62be82010-05-06 00:08:46 +00004671 if (!PySequence_Check(groups)) {
4672 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4673 return NULL;
4674 }
4675 len = PySequence_Size(groups);
4676 if (len > MAX_GROUPS) {
4677 PyErr_SetString(PyExc_ValueError, "too many groups");
4678 return NULL;
4679 }
4680 for(i = 0; i < len; i++) {
4681 PyObject *elem;
4682 elem = PySequence_GetItem(groups, i);
4683 if (!elem)
4684 return NULL;
4685 if (!PyLong_Check(elem)) {
4686 PyErr_SetString(PyExc_TypeError,
4687 "groups must be integers");
4688 Py_DECREF(elem);
4689 return NULL;
4690 } else {
4691 unsigned long x = PyLong_AsUnsignedLong(elem);
4692 if (PyErr_Occurred()) {
4693 PyErr_SetString(PyExc_TypeError,
4694 "group id too big");
4695 Py_DECREF(elem);
4696 return NULL;
4697 }
4698 grouplist[i] = x;
4699 /* read back the value to see if it fitted in gid_t */
4700 if (grouplist[i] != x) {
4701 PyErr_SetString(PyExc_TypeError,
4702 "group id too big");
4703 Py_DECREF(elem);
4704 return NULL;
4705 }
4706 }
4707 Py_DECREF(elem);
4708 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004709
Victor Stinner8c62be82010-05-06 00:08:46 +00004710 if (setgroups(len, grouplist) < 0)
4711 return posix_error();
4712 Py_INCREF(Py_None);
4713 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004714}
4715#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004716
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004717#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4718static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004719wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004720{
Victor Stinner8c62be82010-05-06 00:08:46 +00004721 PyObject *result;
4722 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004723
Victor Stinner8c62be82010-05-06 00:08:46 +00004724 if (pid == -1)
4725 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004726
Victor Stinner8c62be82010-05-06 00:08:46 +00004727 if (struct_rusage == NULL) {
4728 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4729 if (m == NULL)
4730 return NULL;
4731 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4732 Py_DECREF(m);
4733 if (struct_rusage == NULL)
4734 return NULL;
4735 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004736
Victor Stinner8c62be82010-05-06 00:08:46 +00004737 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4738 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4739 if (!result)
4740 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004741
4742#ifndef doubletime
4743#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4744#endif
4745
Victor Stinner8c62be82010-05-06 00:08:46 +00004746 PyStructSequence_SET_ITEM(result, 0,
4747 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4748 PyStructSequence_SET_ITEM(result, 1,
4749 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004750#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004751 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4752 SET_INT(result, 2, ru->ru_maxrss);
4753 SET_INT(result, 3, ru->ru_ixrss);
4754 SET_INT(result, 4, ru->ru_idrss);
4755 SET_INT(result, 5, ru->ru_isrss);
4756 SET_INT(result, 6, ru->ru_minflt);
4757 SET_INT(result, 7, ru->ru_majflt);
4758 SET_INT(result, 8, ru->ru_nswap);
4759 SET_INT(result, 9, ru->ru_inblock);
4760 SET_INT(result, 10, ru->ru_oublock);
4761 SET_INT(result, 11, ru->ru_msgsnd);
4762 SET_INT(result, 12, ru->ru_msgrcv);
4763 SET_INT(result, 13, ru->ru_nsignals);
4764 SET_INT(result, 14, ru->ru_nvcsw);
4765 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004766#undef SET_INT
4767
Victor Stinner8c62be82010-05-06 00:08:46 +00004768 if (PyErr_Occurred()) {
4769 Py_DECREF(result);
4770 return NULL;
4771 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004772
Victor Stinner8c62be82010-05-06 00:08:46 +00004773 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004774}
4775#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4776
4777#ifdef HAVE_WAIT3
4778PyDoc_STRVAR(posix_wait3__doc__,
4779"wait3(options) -> (pid, status, rusage)\n\n\
4780Wait for completion of a child process.");
4781
4782static PyObject *
4783posix_wait3(PyObject *self, PyObject *args)
4784{
Victor Stinner8c62be82010-05-06 00:08:46 +00004785 pid_t pid;
4786 int options;
4787 struct rusage ru;
4788 WAIT_TYPE status;
4789 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004790
Victor Stinner8c62be82010-05-06 00:08:46 +00004791 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4792 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004793
Victor Stinner8c62be82010-05-06 00:08:46 +00004794 Py_BEGIN_ALLOW_THREADS
4795 pid = wait3(&status, options, &ru);
4796 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004797
Victor Stinner8c62be82010-05-06 00:08:46 +00004798 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004799}
4800#endif /* HAVE_WAIT3 */
4801
4802#ifdef HAVE_WAIT4
4803PyDoc_STRVAR(posix_wait4__doc__,
4804"wait4(pid, options) -> (pid, status, rusage)\n\n\
4805Wait for completion of a given child process.");
4806
4807static PyObject *
4808posix_wait4(PyObject *self, PyObject *args)
4809{
Victor Stinner8c62be82010-05-06 00:08:46 +00004810 pid_t pid;
4811 int options;
4812 struct rusage ru;
4813 WAIT_TYPE status;
4814 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004815
Victor Stinner8c62be82010-05-06 00:08:46 +00004816 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4817 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004818
Victor Stinner8c62be82010-05-06 00:08:46 +00004819 Py_BEGIN_ALLOW_THREADS
4820 pid = wait4(pid, &status, options, &ru);
4821 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004822
Victor Stinner8c62be82010-05-06 00:08:46 +00004823 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004824}
4825#endif /* HAVE_WAIT4 */
4826
Guido van Rossumb6775db1994-08-01 11:34:53 +00004827#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004828PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004829"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004830Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004831
Barry Warsaw53699e91996-12-10 23:23:01 +00004832static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004833posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004834{
Victor Stinner8c62be82010-05-06 00:08:46 +00004835 pid_t pid;
4836 int options;
4837 WAIT_TYPE status;
4838 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004839
Victor Stinner8c62be82010-05-06 00:08:46 +00004840 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4841 return NULL;
4842 Py_BEGIN_ALLOW_THREADS
4843 pid = waitpid(pid, &status, options);
4844 Py_END_ALLOW_THREADS
4845 if (pid == -1)
4846 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004847
Victor Stinner8c62be82010-05-06 00:08:46 +00004848 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004849}
4850
Tim Petersab034fa2002-02-01 11:27:43 +00004851#elif defined(HAVE_CWAIT)
4852
4853/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004854PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004855"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004856"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004857
4858static PyObject *
4859posix_waitpid(PyObject *self, PyObject *args)
4860{
Victor Stinner8c62be82010-05-06 00:08:46 +00004861 Py_intptr_t pid;
4862 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004863
Victor Stinner8c62be82010-05-06 00:08:46 +00004864 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4865 return NULL;
4866 Py_BEGIN_ALLOW_THREADS
4867 pid = _cwait(&status, pid, options);
4868 Py_END_ALLOW_THREADS
4869 if (pid == -1)
4870 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004871
Victor Stinner8c62be82010-05-06 00:08:46 +00004872 /* shift the status left a byte so this is more like the POSIX waitpid */
4873 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004874}
4875#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004876
Guido van Rossumad0ee831995-03-01 10:34:45 +00004877#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004878PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004879"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004880Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004881
Barry Warsaw53699e91996-12-10 23:23:01 +00004882static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004883posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004884{
Victor Stinner8c62be82010-05-06 00:08:46 +00004885 pid_t pid;
4886 WAIT_TYPE status;
4887 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004888
Victor Stinner8c62be82010-05-06 00:08:46 +00004889 Py_BEGIN_ALLOW_THREADS
4890 pid = wait(&status);
4891 Py_END_ALLOW_THREADS
4892 if (pid == -1)
4893 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004894
Victor Stinner8c62be82010-05-06 00:08:46 +00004895 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004896}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004897#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004898
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004899
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004900PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004901"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004902Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004903
Barry Warsaw53699e91996-12-10 23:23:01 +00004904static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004905posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004906{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004907#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004908 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004909#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004910#ifdef MS_WINDOWS
Brian Curtind40e6f72010-07-08 21:39:08 +00004911 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat",
4912 win32_lstat_w);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004913#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004914 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004915#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004916#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004917}
4918
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004919
Guido van Rossumb6775db1994-08-01 11:34:53 +00004920#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004921PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004922"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004923Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004924
Barry Warsaw53699e91996-12-10 23:23:01 +00004925static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004926posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004927{
Victor Stinner8c62be82010-05-06 00:08:46 +00004928 PyObject* v;
4929 char buf[MAXPATHLEN];
4930 PyObject *opath;
4931 char *path;
4932 int n;
4933 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004934
Victor Stinner8c62be82010-05-06 00:08:46 +00004935 if (!PyArg_ParseTuple(args, "O&:readlink",
4936 PyUnicode_FSConverter, &opath))
4937 return NULL;
4938 path = PyBytes_AsString(opath);
4939 v = PySequence_GetItem(args, 0);
4940 if (v == NULL) {
4941 Py_DECREF(opath);
4942 return NULL;
4943 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004944
Victor Stinner8c62be82010-05-06 00:08:46 +00004945 if (PyUnicode_Check(v)) {
4946 arg_is_unicode = 1;
4947 }
4948 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004949
Victor Stinner8c62be82010-05-06 00:08:46 +00004950 Py_BEGIN_ALLOW_THREADS
4951 n = readlink(path, buf, (int) sizeof buf);
4952 Py_END_ALLOW_THREADS
4953 if (n < 0)
4954 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004955
Victor Stinner8c62be82010-05-06 00:08:46 +00004956 Py_DECREF(opath);
Victor Stinnera45598a2010-05-14 16:35:39 +00004957 if (arg_is_unicode)
4958 return PyUnicode_DecodeFSDefaultAndSize(buf, n);
4959 else
4960 return PyBytes_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004961}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004962#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004963
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004964
Guido van Rossumb6775db1994-08-01 11:34:53 +00004965#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004966PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004967"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004968Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004969
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004970static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004971posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004972{
Victor Stinner8c62be82010-05-06 00:08:46 +00004973 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004974}
4975#endif /* HAVE_SYMLINK */
4976
Brian Curtind40e6f72010-07-08 21:39:08 +00004977#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
4978
4979PyDoc_STRVAR(win_readlink__doc__,
4980"readlink(path) -> path\n\n\
4981Return a string representing the path to which the symbolic link points.");
4982
4983/* The following structure was copied from
4984 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
4985 include doesn't seem to be present in the Windows SDK (at least as included
4986 with Visual Studio Express). */
4987typedef struct _REPARSE_DATA_BUFFER {
4988 ULONG ReparseTag;
4989 USHORT ReparseDataLength;
4990 USHORT Reserved;
4991 union {
4992 struct {
4993 USHORT SubstituteNameOffset;
4994 USHORT SubstituteNameLength;
4995 USHORT PrintNameOffset;
4996 USHORT PrintNameLength;
4997 ULONG Flags;
4998 WCHAR PathBuffer[1];
4999 } SymbolicLinkReparseBuffer;
5000
5001 struct {
5002 USHORT SubstituteNameOffset;
5003 USHORT SubstituteNameLength;
5004 USHORT PrintNameOffset;
5005 USHORT PrintNameLength;
5006 WCHAR PathBuffer[1];
5007 } MountPointReparseBuffer;
5008
5009 struct {
5010 UCHAR DataBuffer[1];
5011 } GenericReparseBuffer;
5012 };
5013} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
5014
Brian Curtin74e45612010-07-09 15:58:59 +00005015#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
5016 GenericReparseBuffer)
Brian Curtind40e6f72010-07-08 21:39:08 +00005017
5018#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
5019
5020/* Windows readlink implementation */
5021static PyObject *
5022win_readlink(PyObject *self, PyObject *args)
5023{
5024 wchar_t *path;
5025 DWORD n_bytes_returned;
5026 DWORD io_result;
5027 PyObject *result;
5028 HANDLE reparse_point_handle;
5029
5030 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
5031 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
5032 wchar_t *print_name;
5033
5034 if (!PyArg_ParseTuple(args,
5035 "u:readlink",
5036 &path))
5037 return NULL;
5038
5039 /* First get a handle to the reparse point */
5040 Py_BEGIN_ALLOW_THREADS
5041 reparse_point_handle = CreateFileW(
5042 path,
5043 0,
5044 0,
5045 0,
5046 OPEN_EXISTING,
5047 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
5048 0);
5049 Py_END_ALLOW_THREADS
5050
5051 if (reparse_point_handle==INVALID_HANDLE_VALUE)
5052 {
5053 return win32_error_unicode("readlink", path);
5054 }
5055
5056 Py_BEGIN_ALLOW_THREADS
5057 /* New call DeviceIoControl to read the reparse point */
5058 io_result = DeviceIoControl(
5059 reparse_point_handle,
5060 FSCTL_GET_REPARSE_POINT,
5061 0, 0, /* in buffer */
5062 target_buffer, sizeof(target_buffer),
5063 &n_bytes_returned,
5064 0 /* we're not using OVERLAPPED_IO */
5065 );
5066 CloseHandle(reparse_point_handle);
5067 Py_END_ALLOW_THREADS
5068
5069 if (io_result==0)
5070 {
5071 return win32_error_unicode("readlink", path);
5072 }
5073
5074 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
5075 {
5076 PyErr_SetString(PyExc_ValueError,
5077 "not a symbolic link");
5078 return NULL;
5079 }
Brian Curtin74e45612010-07-09 15:58:59 +00005080 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
5081 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
5082
5083 result = PyUnicode_FromWideChar(print_name,
5084 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00005085 return result;
5086}
5087
5088#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
5089
5090#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
5091
5092/* Grab CreateSymbolicLinkW dynamically from kernel32 */
5093static int has_CreateSymbolicLinkW = 0;
5094static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD);
5095static int
5096check_CreateSymbolicLinkW()
5097{
5098 HINSTANCE hKernel32;
5099 /* only recheck */
5100 if (has_CreateSymbolicLinkW)
5101 return has_CreateSymbolicLinkW;
5102 hKernel32 = GetModuleHandle("KERNEL32");
Brian Curtin74e45612010-07-09 15:58:59 +00005103 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
5104 "CreateSymbolicLinkW");
Brian Curtind40e6f72010-07-08 21:39:08 +00005105 if (Py_CreateSymbolicLinkW)
5106 has_CreateSymbolicLinkW = 1;
5107 return has_CreateSymbolicLinkW;
5108}
5109
5110PyDoc_STRVAR(win_symlink__doc__,
5111"symlink(src, dst, target_is_directory=False)\n\n\
5112Create a symbolic link pointing to src named dst.\n\
5113target_is_directory is required if the target is to be interpreted as\n\
5114a directory.\n\
5115This function requires Windows 6.0 or greater, and raises a\n\
5116NotImplementedError otherwise.");
5117
5118static PyObject *
5119win_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
5120{
5121 static char *kwlist[] = {"src", "dest", "target_is_directory", NULL};
5122 PyObject *src, *dest;
5123 int target_is_directory = 0;
5124 DWORD res;
5125 WIN32_FILE_ATTRIBUTE_DATA src_info;
5126
5127 if (!check_CreateSymbolicLinkW())
5128 {
5129 /* raise NotImplementedError */
5130 return PyErr_Format(PyExc_NotImplementedError,
5131 "CreateSymbolicLinkW not found");
5132 }
5133 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|i:symlink",
5134 kwlist, &src, &dest, &target_is_directory))
5135 return NULL;
5136 if (!convert_to_unicode(&src)) { return NULL; }
5137 if (!convert_to_unicode(&dest)) {
5138 Py_DECREF(src);
5139 return NULL;
5140 }
5141
5142 /* if src is a directory, ensure target_is_directory==1 */
5143 if(
5144 GetFileAttributesExW(
5145 PyUnicode_AsUnicode(src), GetFileExInfoStandard, &src_info
5146 ))
5147 {
5148 target_is_directory = target_is_directory ||
5149 (src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
5150 }
5151
5152 Py_BEGIN_ALLOW_THREADS
5153 res = Py_CreateSymbolicLinkW(
5154 PyUnicode_AsUnicode(dest),
5155 PyUnicode_AsUnicode(src),
5156 target_is_directory);
5157 Py_END_ALLOW_THREADS
5158 Py_DECREF(src);
5159 Py_DECREF(dest);
5160 if (!res)
5161 {
5162 return win32_error_unicode("symlink", PyUnicode_AsUnicode(src));
5163 }
5164
5165 Py_INCREF(Py_None);
5166 return Py_None;
5167}
5168#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005169
5170#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00005171#if defined(PYCC_VACPP) && defined(PYOS_OS2)
5172static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00005173system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005174{
5175 ULONG value = 0;
5176
5177 Py_BEGIN_ALLOW_THREADS
5178 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
5179 Py_END_ALLOW_THREADS
5180
5181 return value;
5182}
5183
5184static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005185posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005186{
Guido van Rossumd48f2521997-12-05 22:19:34 +00005187 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00005188 return Py_BuildValue("ddddd",
5189 (double)0 /* t.tms_utime / HZ */,
5190 (double)0 /* t.tms_stime / HZ */,
5191 (double)0 /* t.tms_cutime / HZ */,
5192 (double)0 /* t.tms_cstime / HZ */,
5193 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00005194}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005195#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00005196#define NEED_TICKS_PER_SECOND
5197static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00005198static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005199posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00005200{
Victor Stinner8c62be82010-05-06 00:08:46 +00005201 struct tms t;
5202 clock_t c;
5203 errno = 0;
5204 c = times(&t);
5205 if (c == (clock_t) -1)
5206 return posix_error();
5207 return Py_BuildValue("ddddd",
5208 (double)t.tms_utime / ticks_per_second,
5209 (double)t.tms_stime / ticks_per_second,
5210 (double)t.tms_cutime / ticks_per_second,
5211 (double)t.tms_cstime / ticks_per_second,
5212 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00005213}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005214#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00005215#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005216
5217
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005218#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005219#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00005220static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005221posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005222{
Victor Stinner8c62be82010-05-06 00:08:46 +00005223 FILETIME create, exit, kernel, user;
5224 HANDLE hProc;
5225 hProc = GetCurrentProcess();
5226 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
5227 /* The fields of a FILETIME structure are the hi and lo part
5228 of a 64-bit value expressed in 100 nanosecond units.
5229 1e7 is one second in such units; 1e-7 the inverse.
5230 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
5231 */
5232 return Py_BuildValue(
5233 "ddddd",
5234 (double)(user.dwHighDateTime*429.4967296 +
5235 user.dwLowDateTime*1e-7),
5236 (double)(kernel.dwHighDateTime*429.4967296 +
5237 kernel.dwLowDateTime*1e-7),
5238 (double)0,
5239 (double)0,
5240 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00005241}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005242#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005243
5244#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005245PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005246"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005247Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00005248#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005250
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005251#ifdef HAVE_GETSID
5252PyDoc_STRVAR(posix_getsid__doc__,
5253"getsid(pid) -> sid\n\n\
5254Call the system call getsid().");
5255
5256static PyObject *
5257posix_getsid(PyObject *self, PyObject *args)
5258{
Victor Stinner8c62be82010-05-06 00:08:46 +00005259 pid_t pid;
5260 int sid;
5261 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
5262 return NULL;
5263 sid = getsid(pid);
5264 if (sid < 0)
5265 return posix_error();
5266 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00005267}
5268#endif /* HAVE_GETSID */
5269
5270
Guido van Rossumb6775db1994-08-01 11:34:53 +00005271#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005272PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005273"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005274Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005275
Barry Warsaw53699e91996-12-10 23:23:01 +00005276static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005277posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005278{
Victor Stinner8c62be82010-05-06 00:08:46 +00005279 if (setsid() < 0)
5280 return posix_error();
5281 Py_INCREF(Py_None);
5282 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005283}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005284#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005285
Guido van Rossumb6775db1994-08-01 11:34:53 +00005286#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005287PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005288"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005289Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005290
Barry Warsaw53699e91996-12-10 23:23:01 +00005291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005292posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00005293{
Victor Stinner8c62be82010-05-06 00:08:46 +00005294 pid_t pid;
5295 int pgrp;
5296 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
5297 return NULL;
5298 if (setpgid(pid, pgrp) < 0)
5299 return posix_error();
5300 Py_INCREF(Py_None);
5301 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00005302}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005303#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00005304
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005305
Guido van Rossumb6775db1994-08-01 11:34:53 +00005306#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005307PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005308"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005309Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005310
Barry Warsaw53699e91996-12-10 23:23:01 +00005311static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005312posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005313{
Victor Stinner8c62be82010-05-06 00:08:46 +00005314 int fd;
5315 pid_t pgid;
5316 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
5317 return NULL;
5318 pgid = tcgetpgrp(fd);
5319 if (pgid < 0)
5320 return posix_error();
5321 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00005322}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005323#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00005324
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005325
Guido van Rossumb6775db1994-08-01 11:34:53 +00005326#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005327PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005328"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005329Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005330
Barry Warsaw53699e91996-12-10 23:23:01 +00005331static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005332posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00005333{
Victor Stinner8c62be82010-05-06 00:08:46 +00005334 int fd;
5335 pid_t pgid;
5336 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
5337 return NULL;
5338 if (tcsetpgrp(fd, pgid) < 0)
5339 return posix_error();
5340 Py_INCREF(Py_None);
5341 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00005342}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005343#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00005344
Guido van Rossum687dd131993-05-17 08:34:16 +00005345/* Functions acting on file descriptors */
5346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005347PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005348"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005349Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005350
Barry Warsaw53699e91996-12-10 23:23:01 +00005351static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005352posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005353{
Victor Stinner8c62be82010-05-06 00:08:46 +00005354 PyObject *ofile;
5355 char *file;
5356 int flag;
5357 int mode = 0777;
5358 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005359
5360#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005361 PyUnicodeObject *po;
5362 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
5363 Py_BEGIN_ALLOW_THREADS
5364 /* PyUnicode_AS_UNICODE OK without thread
5365 lock as it is a simple dereference. */
5366 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
5367 Py_END_ALLOW_THREADS
5368 if (fd < 0)
5369 return posix_error();
5370 return PyLong_FromLong((long)fd);
5371 }
5372 /* Drop the argument parsing error as narrow strings
5373 are also valid. */
5374 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00005375#endif
5376
Victor Stinner8c62be82010-05-06 00:08:46 +00005377 if (!PyArg_ParseTuple(args, "O&i|i",
5378 PyUnicode_FSConverter, &ofile,
5379 &flag, &mode))
5380 return NULL;
5381 file = PyBytes_AsString(ofile);
5382 Py_BEGIN_ALLOW_THREADS
5383 fd = open(file, flag, mode);
5384 Py_END_ALLOW_THREADS
5385 if (fd < 0)
5386 return posix_error_with_allocated_filename(ofile);
5387 Py_DECREF(ofile);
5388 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005389}
5390
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005392PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005393"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005394Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005395
Barry Warsaw53699e91996-12-10 23:23:01 +00005396static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005397posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005398{
Victor Stinner8c62be82010-05-06 00:08:46 +00005399 int fd, res;
5400 if (!PyArg_ParseTuple(args, "i:close", &fd))
5401 return NULL;
5402 if (!_PyVerify_fd(fd))
5403 return posix_error();
5404 Py_BEGIN_ALLOW_THREADS
5405 res = close(fd);
5406 Py_END_ALLOW_THREADS
5407 if (res < 0)
5408 return posix_error();
5409 Py_INCREF(Py_None);
5410 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005411}
5412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005413
Victor Stinner8c62be82010-05-06 00:08:46 +00005414PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00005415"closerange(fd_low, fd_high)\n\n\
5416Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
5417
5418static PyObject *
5419posix_closerange(PyObject *self, PyObject *args)
5420{
Victor Stinner8c62be82010-05-06 00:08:46 +00005421 int fd_from, fd_to, i;
5422 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
5423 return NULL;
5424 Py_BEGIN_ALLOW_THREADS
5425 for (i = fd_from; i < fd_to; i++)
5426 if (_PyVerify_fd(i))
5427 close(i);
5428 Py_END_ALLOW_THREADS
5429 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00005430}
5431
5432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005433PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005434"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005435Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005436
Barry Warsaw53699e91996-12-10 23:23:01 +00005437static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005438posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005439{
Victor Stinner8c62be82010-05-06 00:08:46 +00005440 int fd;
5441 if (!PyArg_ParseTuple(args, "i:dup", &fd))
5442 return NULL;
5443 if (!_PyVerify_fd(fd))
5444 return posix_error();
5445 Py_BEGIN_ALLOW_THREADS
5446 fd = dup(fd);
5447 Py_END_ALLOW_THREADS
5448 if (fd < 0)
5449 return posix_error();
5450 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00005451}
5452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005454PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00005455"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005456Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005457
Barry Warsaw53699e91996-12-10 23:23:01 +00005458static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005459posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005460{
Victor Stinner8c62be82010-05-06 00:08:46 +00005461 int fd, fd2, res;
5462 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
5463 return NULL;
5464 if (!_PyVerify_fd_dup2(fd, fd2))
5465 return posix_error();
5466 Py_BEGIN_ALLOW_THREADS
5467 res = dup2(fd, fd2);
5468 Py_END_ALLOW_THREADS
5469 if (res < 0)
5470 return posix_error();
5471 Py_INCREF(Py_None);
5472 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00005473}
5474
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005476PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005477"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005478Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005479
Barry Warsaw53699e91996-12-10 23:23:01 +00005480static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005481posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005482{
Victor Stinner8c62be82010-05-06 00:08:46 +00005483 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005484#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005485 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005486#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005487 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00005488#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005489 PyObject *posobj;
5490 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
5491 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00005492#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00005493 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
5494 switch (how) {
5495 case 0: how = SEEK_SET; break;
5496 case 1: how = SEEK_CUR; break;
5497 case 2: how = SEEK_END; break;
5498 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005499#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005500
5501#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005502 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005503#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005504 pos = PyLong_Check(posobj) ?
5505 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005506#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005507 if (PyErr_Occurred())
5508 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005509
Victor Stinner8c62be82010-05-06 00:08:46 +00005510 if (!_PyVerify_fd(fd))
5511 return posix_error();
5512 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005513#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005514 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005515#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005516 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00005517#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005518 Py_END_ALLOW_THREADS
5519 if (res < 0)
5520 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00005521
5522#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005523 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005524#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005525 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005526#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005527}
5528
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005530PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005531"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005532Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005533
Barry Warsaw53699e91996-12-10 23:23:01 +00005534static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005535posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005536{
Victor Stinner8c62be82010-05-06 00:08:46 +00005537 int fd, size;
5538 Py_ssize_t n;
5539 PyObject *buffer;
5540 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5541 return NULL;
5542 if (size < 0) {
5543 errno = EINVAL;
5544 return posix_error();
5545 }
5546 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5547 if (buffer == NULL)
5548 return NULL;
5549 if (!_PyVerify_fd(fd))
5550 return posix_error();
5551 Py_BEGIN_ALLOW_THREADS
5552 n = read(fd, PyBytes_AS_STRING(buffer), size);
5553 Py_END_ALLOW_THREADS
5554 if (n < 0) {
5555 Py_DECREF(buffer);
5556 return posix_error();
5557 }
5558 if (n != size)
5559 _PyBytes_Resize(&buffer, n);
5560 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005561}
5562
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005564PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005565"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005566Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005567
Barry Warsaw53699e91996-12-10 23:23:01 +00005568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005569posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005570{
Victor Stinner8c62be82010-05-06 00:08:46 +00005571 Py_buffer pbuf;
5572 int fd;
5573 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005574
Victor Stinner8c62be82010-05-06 00:08:46 +00005575 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5576 return NULL;
5577 if (!_PyVerify_fd(fd))
5578 return posix_error();
5579 Py_BEGIN_ALLOW_THREADS
5580 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5581 Py_END_ALLOW_THREADS
5582 PyBuffer_Release(&pbuf);
5583 if (size < 0)
5584 return posix_error();
5585 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005586}
5587
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005589PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005590"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005591Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005592
Barry Warsaw53699e91996-12-10 23:23:01 +00005593static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005594posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005595{
Victor Stinner8c62be82010-05-06 00:08:46 +00005596 int fd;
5597 STRUCT_STAT st;
5598 int res;
5599 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5600 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005601#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005602 /* on OpenVMS we must ensure that all bytes are written to the file */
5603 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005604#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005605 if (!_PyVerify_fd(fd))
5606 return posix_error();
5607 Py_BEGIN_ALLOW_THREADS
5608 res = FSTAT(fd, &st);
5609 Py_END_ALLOW_THREADS
5610 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005611#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005612 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005613#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005615#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005616 }
Tim Peters5aa91602002-01-30 05:46:57 +00005617
Victor Stinner8c62be82010-05-06 00:08:46 +00005618 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005619}
5620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005621PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005622"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005623Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005624connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005625
5626static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005627posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005628{
Victor Stinner8c62be82010-05-06 00:08:46 +00005629 int fd;
5630 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5631 return NULL;
5632 if (!_PyVerify_fd(fd))
5633 return PyBool_FromLong(0);
5634 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005635}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005636
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005637#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005638PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005639"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005640Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005641
Barry Warsaw53699e91996-12-10 23:23:01 +00005642static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005643posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005644{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005645#if defined(PYOS_OS2)
5646 HFILE read, write;
5647 APIRET rc;
5648
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005650 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005651 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005652 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005653 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005654
5655 return Py_BuildValue("(ii)", read, write);
5656#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005657#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005658 int fds[2];
5659 int res;
5660 Py_BEGIN_ALLOW_THREADS
5661 res = pipe(fds);
5662 Py_END_ALLOW_THREADS
5663 if (res != 0)
5664 return posix_error();
5665 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005666#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005667 HANDLE read, write;
5668 int read_fd, write_fd;
5669 BOOL ok;
5670 Py_BEGIN_ALLOW_THREADS
5671 ok = CreatePipe(&read, &write, NULL, 0);
5672 Py_END_ALLOW_THREADS
5673 if (!ok)
5674 return win32_error("CreatePipe", NULL);
5675 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5676 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5677 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005678#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005679#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005680}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005681#endif /* HAVE_PIPE */
5682
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005683
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005684#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005685PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005686"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005687Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005688
Barry Warsaw53699e91996-12-10 23:23:01 +00005689static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005690posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005691{
Victor Stinner8c62be82010-05-06 00:08:46 +00005692 char *filename;
5693 int mode = 0666;
5694 int res;
5695 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
5696 return NULL;
5697 Py_BEGIN_ALLOW_THREADS
5698 res = mkfifo(filename, mode);
5699 Py_END_ALLOW_THREADS
5700 if (res < 0)
5701 return posix_error();
5702 Py_INCREF(Py_None);
5703 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005704}
5705#endif
5706
5707
Neal Norwitz11690112002-07-30 01:08:28 +00005708#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005709PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005710"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005711Create a filesystem node (file, device special file or named pipe)\n\
5712named filename. mode specifies both the permissions to use and the\n\
5713type of node to be created, being combined (bitwise OR) with one of\n\
5714S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005715device defines the newly created device special file (probably using\n\
5716os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005717
5718
5719static PyObject *
5720posix_mknod(PyObject *self, PyObject *args)
5721{
Victor Stinner8c62be82010-05-06 00:08:46 +00005722 char *filename;
5723 int mode = 0600;
5724 int device = 0;
5725 int res;
5726 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
5727 return NULL;
5728 Py_BEGIN_ALLOW_THREADS
5729 res = mknod(filename, mode, device);
5730 Py_END_ALLOW_THREADS
5731 if (res < 0)
5732 return posix_error();
5733 Py_INCREF(Py_None);
5734 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005735}
5736#endif
5737
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005738#ifdef HAVE_DEVICE_MACROS
5739PyDoc_STRVAR(posix_major__doc__,
5740"major(device) -> major number\n\
5741Extracts a device major number from a raw device number.");
5742
5743static PyObject *
5744posix_major(PyObject *self, PyObject *args)
5745{
Victor Stinner8c62be82010-05-06 00:08:46 +00005746 int device;
5747 if (!PyArg_ParseTuple(args, "i:major", &device))
5748 return NULL;
5749 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005750}
5751
5752PyDoc_STRVAR(posix_minor__doc__,
5753"minor(device) -> minor number\n\
5754Extracts a device minor number from a raw device number.");
5755
5756static PyObject *
5757posix_minor(PyObject *self, PyObject *args)
5758{
Victor Stinner8c62be82010-05-06 00:08:46 +00005759 int device;
5760 if (!PyArg_ParseTuple(args, "i:minor", &device))
5761 return NULL;
5762 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005763}
5764
5765PyDoc_STRVAR(posix_makedev__doc__,
5766"makedev(major, minor) -> device number\n\
5767Composes a raw device number from the major and minor device numbers.");
5768
5769static PyObject *
5770posix_makedev(PyObject *self, PyObject *args)
5771{
Victor Stinner8c62be82010-05-06 00:08:46 +00005772 int major, minor;
5773 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5774 return NULL;
5775 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005776}
5777#endif /* device macros */
5778
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005779
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005780#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005781PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005782"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005783Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005784
Barry Warsaw53699e91996-12-10 23:23:01 +00005785static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005786posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005787{
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 int fd;
5789 off_t length;
5790 int res;
5791 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005792
Victor Stinner8c62be82010-05-06 00:08:46 +00005793 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5794 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005795
5796#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005797 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005798#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005799 length = PyLong_Check(lenobj) ?
5800 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005801#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005802 if (PyErr_Occurred())
5803 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005804
Victor Stinner8c62be82010-05-06 00:08:46 +00005805 Py_BEGIN_ALLOW_THREADS
5806 res = ftruncate(fd, length);
5807 Py_END_ALLOW_THREADS
5808 if (res < 0)
5809 return posix_error();
5810 Py_INCREF(Py_None);
5811 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005812}
5813#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005814
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005815#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005816PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005817"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005818Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005819
Fred Drake762e2061999-08-26 17:23:54 +00005820/* Save putenv() parameters as values here, so we can collect them when they
5821 * get re-set with another call for the same key. */
5822static PyObject *posix_putenv_garbage;
5823
Tim Peters5aa91602002-01-30 05:46:57 +00005824static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005825posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005826{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005827#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005828 wchar_t *s1, *s2;
5829 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005830#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005831 PyObject *os1, *os2;
5832 char *s1, *s2;
5833 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005834#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005835 PyObject *newstr = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005836 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005837
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005838#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005839 if (!PyArg_ParseTuple(args,
5840 "uu:putenv",
5841 &s1, &s2))
5842 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005843#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005844 if (!PyArg_ParseTuple(args,
5845 "O&O&:putenv",
5846 PyUnicode_FSConverter, &os1,
5847 PyUnicode_FSConverter, &os2))
5848 return NULL;
5849 s1 = PyBytes_AsString(os1);
5850 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005851#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005852
5853#if defined(PYOS_OS2)
5854 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5855 APIRET rc;
5856
Guido van Rossumd48f2521997-12-05 22:19:34 +00005857 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005858 if (rc != NO_ERROR) {
5859 os2_error(rc);
5860 goto error;
5861 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005862
5863 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5864 APIRET rc;
5865
Guido van Rossumd48f2521997-12-05 22:19:34 +00005866 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
Victor Stinner84ae1182010-05-06 22:05:07 +00005867 if (rc != NO_ERROR) {
5868 os2_error(rc);
5869 goto error;
5870 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005871 } else {
5872#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 /* XXX This can leak memory -- not easy to fix :-( */
5874 /* len includes space for a trailing \0; the size arg to
5875 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005876#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005877 len = wcslen(s1) + wcslen(s2) + 2;
5878 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005879#else
Victor Stinner84ae1182010-05-06 22:05:07 +00005880 len = PyBytes_GET_SIZE(os1) + PyBytes_GET_SIZE(os2) + 2;
Victor Stinner8c62be82010-05-06 00:08:46 +00005881 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005882#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005883 if (newstr == NULL) {
5884 PyErr_NoMemory();
5885 goto error;
5886 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005887#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005888 newenv = PyUnicode_AsUnicode(newstr);
5889 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5890 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005892 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005893 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005894#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005895 newenv = PyBytes_AS_STRING(newstr);
5896 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5897 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005898 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00005899 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005900 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005901#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005902
Victor Stinner8c62be82010-05-06 00:08:46 +00005903 /* Install the first arg and newstr in posix_putenv_garbage;
5904 * this will cause previous value to be collected. This has to
5905 * happen after the real putenv() call because the old value
5906 * was still accessible until then. */
5907 if (PyDict_SetItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005908#ifdef MS_WINDOWS
5909 PyTuple_GET_ITEM(args, 0),
5910#else
5911 os1,
5912#endif
5913 newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 /* really not much we can do; just leak */
5915 PyErr_Clear();
5916 }
5917 else {
5918 Py_DECREF(newstr);
5919 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005920
5921#if defined(PYOS_OS2)
5922 }
5923#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005924
Martin v. Löwis011e8422009-05-05 04:43:17 +00005925#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005926 Py_DECREF(os1);
5927 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005928#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00005929 Py_RETURN_NONE;
5930
5931error:
5932#ifndef MS_WINDOWS
5933 Py_DECREF(os1);
5934 Py_DECREF(os2);
5935#endif
5936 Py_XDECREF(newstr);
5937 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005938}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005939#endif /* putenv */
5940
Guido van Rossumc524d952001-10-19 01:31:59 +00005941#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005942PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005943"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005944Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005945
5946static PyObject *
5947posix_unsetenv(PyObject *self, PyObject *args)
5948{
Victor Stinner84ae1182010-05-06 22:05:07 +00005949#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005950 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005951
Victor Stinner8c62be82010-05-06 00:08:46 +00005952 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5953 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00005954#else
5955 PyObject *os1;
5956 char *s1;
5957
5958 if (!PyArg_ParseTuple(args, "O&:unsetenv",
5959 PyUnicode_FSConverter, &os1))
5960 return NULL;
5961 s1 = PyBytes_AsString(os1);
5962#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00005963
Victor Stinner8c62be82010-05-06 00:08:46 +00005964 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00005965
Victor Stinner8c62be82010-05-06 00:08:46 +00005966 /* Remove the key from posix_putenv_garbage;
5967 * this will cause it to be collected. This has to
5968 * happen after the real unsetenv() call because the
5969 * old value was still accessible until then.
5970 */
5971 if (PyDict_DelItem(posix_putenv_garbage,
Victor Stinner84ae1182010-05-06 22:05:07 +00005972#ifdef MS_WINDOWS
5973 PyTuple_GET_ITEM(args, 0)
5974#else
5975 os1
5976#endif
5977 )) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005978 /* really not much we can do; just leak */
5979 PyErr_Clear();
5980 }
Guido van Rossumc524d952001-10-19 01:31:59 +00005981
Victor Stinner84ae1182010-05-06 22:05:07 +00005982#ifndef MS_WINDOWS
5983 Py_DECREF(os1);
5984#endif
5985 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00005986}
5987#endif /* unsetenv */
5988
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005989PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005990"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005991Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005992
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005993static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005994posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005995{
Victor Stinner8c62be82010-05-06 00:08:46 +00005996 int code;
5997 char *message;
5998 if (!PyArg_ParseTuple(args, "i:strerror", &code))
5999 return NULL;
6000 message = strerror(code);
6001 if (message == NULL) {
6002 PyErr_SetString(PyExc_ValueError,
6003 "strerror() argument out of range");
6004 return NULL;
6005 }
6006 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00006007}
Guido van Rossumb6a47161997-09-15 22:54:34 +00006008
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00006009
Guido van Rossumc9641791998-08-04 15:26:23 +00006010#ifdef HAVE_SYS_WAIT_H
6011
Fred Drake106c1a02002-04-23 15:58:02 +00006012#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006013PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006014"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006015Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00006016
6017static PyObject *
6018posix_WCOREDUMP(PyObject *self, PyObject *args)
6019{
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 WAIT_TYPE status;
6021 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006022
Victor Stinner8c62be82010-05-06 00:08:46 +00006023 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
6024 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006025
Victor Stinner8c62be82010-05-06 00:08:46 +00006026 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006027}
6028#endif /* WCOREDUMP */
6029
6030#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006031PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006032"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00006033Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006034job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00006035
6036static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00006037posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00006038{
Victor Stinner8c62be82010-05-06 00:08:46 +00006039 WAIT_TYPE status;
6040 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00006041
Victor Stinner8c62be82010-05-06 00:08:46 +00006042 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
6043 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00006044
Victor Stinner8c62be82010-05-06 00:08:46 +00006045 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00006046}
6047#endif /* WIFCONTINUED */
6048
Guido van Rossumc9641791998-08-04 15:26:23 +00006049#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006050PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006051"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006052Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006053
6054static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006055posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006056{
Victor Stinner8c62be82010-05-06 00:08:46 +00006057 WAIT_TYPE status;
6058 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006059
Victor Stinner8c62be82010-05-06 00:08:46 +00006060 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
6061 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006062
Victor Stinner8c62be82010-05-06 00:08:46 +00006063 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006064}
6065#endif /* WIFSTOPPED */
6066
6067#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006068PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006069"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006070Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006071
6072static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006073posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006074{
Victor Stinner8c62be82010-05-06 00:08:46 +00006075 WAIT_TYPE status;
6076 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006077
Victor Stinner8c62be82010-05-06 00:08:46 +00006078 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
6079 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006080
Victor Stinner8c62be82010-05-06 00:08:46 +00006081 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006082}
6083#endif /* WIFSIGNALED */
6084
6085#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006086PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006087"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006088Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006089system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006090
6091static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006092posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006093{
Victor Stinner8c62be82010-05-06 00:08:46 +00006094 WAIT_TYPE status;
6095 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006096
Victor Stinner8c62be82010-05-06 00:08:46 +00006097 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
6098 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006099
Victor Stinner8c62be82010-05-06 00:08:46 +00006100 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006101}
6102#endif /* WIFEXITED */
6103
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006104#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006105PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006106"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006107Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006108
6109static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006110posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006111{
Victor Stinner8c62be82010-05-06 00:08:46 +00006112 WAIT_TYPE status;
6113 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006114
Victor Stinner8c62be82010-05-06 00:08:46 +00006115 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
6116 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006117
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006119}
6120#endif /* WEXITSTATUS */
6121
6122#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006123PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006124"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00006125Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006126value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006127
6128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006129posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006130{
Victor Stinner8c62be82010-05-06 00:08:46 +00006131 WAIT_TYPE status;
6132 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006133
Victor Stinner8c62be82010-05-06 00:08:46 +00006134 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
6135 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006136
Victor Stinner8c62be82010-05-06 00:08:46 +00006137 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006138}
6139#endif /* WTERMSIG */
6140
6141#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006142PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006143"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006144Return the signal that stopped the process that provided\n\
6145the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00006146
6147static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006148posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00006149{
Victor Stinner8c62be82010-05-06 00:08:46 +00006150 WAIT_TYPE status;
6151 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00006152
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
6154 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00006155
Victor Stinner8c62be82010-05-06 00:08:46 +00006156 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00006157}
6158#endif /* WSTOPSIG */
6159
6160#endif /* HAVE_SYS_WAIT_H */
6161
6162
Thomas Wouters477c8d52006-05-27 19:21:47 +00006163#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00006164#ifdef _SCO_DS
6165/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
6166 needed definitions in sys/statvfs.h */
6167#define _SVID3
6168#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00006169#include <sys/statvfs.h>
6170
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006171static PyObject*
6172_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006173 PyObject *v = PyStructSequence_New(&StatVFSResultType);
6174 if (v == NULL)
6175 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006176
6177#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00006178 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6179 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6180 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
6181 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
6182 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
6183 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
6184 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
6185 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
6186 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6187 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006188#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006189 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
6190 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
6191 PyStructSequence_SET_ITEM(v, 2,
6192 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
6193 PyStructSequence_SET_ITEM(v, 3,
6194 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
6195 PyStructSequence_SET_ITEM(v, 4,
6196 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
6197 PyStructSequence_SET_ITEM(v, 5,
6198 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
6199 PyStructSequence_SET_ITEM(v, 6,
6200 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
6201 PyStructSequence_SET_ITEM(v, 7,
6202 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
6203 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
6204 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006205#endif
6206
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006208}
6209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006210PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006211"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006212Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006213
6214static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006215posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006216{
Victor Stinner8c62be82010-05-06 00:08:46 +00006217 int fd, res;
6218 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006219
Victor Stinner8c62be82010-05-06 00:08:46 +00006220 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
6221 return NULL;
6222 Py_BEGIN_ALLOW_THREADS
6223 res = fstatvfs(fd, &st);
6224 Py_END_ALLOW_THREADS
6225 if (res != 0)
6226 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006227
Victor Stinner8c62be82010-05-06 00:08:46 +00006228 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006229}
Thomas Wouters477c8d52006-05-27 19:21:47 +00006230#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00006231
6232
Thomas Wouters477c8d52006-05-27 19:21:47 +00006233#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006234#include <sys/statvfs.h>
6235
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006236PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006237"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006238Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00006239
6240static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006241posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00006242{
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 char *path;
6244 int res;
6245 struct statvfs st;
6246 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
6247 return NULL;
6248 Py_BEGIN_ALLOW_THREADS
6249 res = statvfs(path, &st);
6250 Py_END_ALLOW_THREADS
6251 if (res != 0)
6252 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00006253
Victor Stinner8c62be82010-05-06 00:08:46 +00006254 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00006255}
6256#endif /* HAVE_STATVFS */
6257
Fred Drakec9680921999-12-13 16:37:25 +00006258/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
6259 * It maps strings representing configuration variable names to
6260 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00006261 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00006262 * rarely-used constants. There are three separate tables that use
6263 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00006264 *
6265 * This code is always included, even if none of the interfaces that
6266 * need it are included. The #if hackery needed to avoid it would be
6267 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00006268 */
6269struct constdef {
6270 char *name;
6271 long value;
6272};
6273
Fred Drake12c6e2d1999-12-14 21:25:03 +00006274static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006275conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00006276 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006277{
Christian Heimes217cfd12007-12-02 14:31:20 +00006278 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 *valuep = PyLong_AS_LONG(arg);
6280 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006281 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00006282 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006283 /* look up the value in the table using a binary search */
6284 size_t lo = 0;
6285 size_t mid;
6286 size_t hi = tablesize;
6287 int cmp;
6288 const char *confname;
6289 if (!PyUnicode_Check(arg)) {
6290 PyErr_SetString(PyExc_TypeError,
6291 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00006292 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006293 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 confname = _PyUnicode_AsString(arg);
6295 if (confname == NULL)
6296 return 0;
6297 while (lo < hi) {
6298 mid = (lo + hi) / 2;
6299 cmp = strcmp(confname, table[mid].name);
6300 if (cmp < 0)
6301 hi = mid;
6302 else if (cmp > 0)
6303 lo = mid + 1;
6304 else {
6305 *valuep = table[mid].value;
6306 return 1;
6307 }
6308 }
6309 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
6310 return 0;
6311 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00006312}
6313
6314
6315#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
6316static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006317#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006318 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006319#endif
6320#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006321 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006322#endif
Fred Drakec9680921999-12-13 16:37:25 +00006323#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006324 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006325#endif
6326#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00006327 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00006328#endif
6329#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00006330 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00006331#endif
6332#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00006333 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00006334#endif
6335#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006336 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006337#endif
6338#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00006339 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00006340#endif
6341#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00006343#endif
6344#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006345 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006346#endif
6347#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006348 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00006349#endif
6350#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006351 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006352#endif
6353#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006354 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00006355#endif
6356#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006357 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006358#endif
6359#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00006360 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00006361#endif
6362#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006364#endif
6365#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00006367#endif
6368};
6369
Fred Drakec9680921999-12-13 16:37:25 +00006370static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006371conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006372{
6373 return conv_confname(arg, valuep, posix_constants_pathconf,
6374 sizeof(posix_constants_pathconf)
6375 / sizeof(struct constdef));
6376}
6377#endif
6378
6379#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006380PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006381"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006382Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006383If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006384
6385static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006386posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006387{
6388 PyObject *result = NULL;
6389 int name, fd;
6390
Fred Drake12c6e2d1999-12-14 21:25:03 +00006391 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
6392 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006393 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006394
Victor Stinner8c62be82010-05-06 00:08:46 +00006395 errno = 0;
6396 limit = fpathconf(fd, name);
6397 if (limit == -1 && errno != 0)
6398 posix_error();
6399 else
6400 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006401 }
6402 return result;
6403}
6404#endif
6405
6406
6407#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006408PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006409"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00006410Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006411If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00006412
6413static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006414posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006415{
6416 PyObject *result = NULL;
6417 int name;
6418 char *path;
6419
6420 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
6421 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006422 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00006423
Victor Stinner8c62be82010-05-06 00:08:46 +00006424 errno = 0;
6425 limit = pathconf(path, name);
6426 if (limit == -1 && errno != 0) {
6427 if (errno == EINVAL)
6428 /* could be a path or name problem */
6429 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00006430 else
Victor Stinner8c62be82010-05-06 00:08:46 +00006431 posix_error_with_filename(path);
6432 }
6433 else
6434 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00006435 }
6436 return result;
6437}
6438#endif
6439
6440#ifdef HAVE_CONFSTR
6441static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00006442#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00006443 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00006444#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00006445#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006446 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006447#endif
6448#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006449 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00006450#endif
Fred Draked86ed291999-12-15 15:34:33 +00006451#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006452 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006453#endif
6454#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006455 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006456#endif
6457#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006458 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006459#endif
6460#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006462#endif
Fred Drakec9680921999-12-13 16:37:25 +00006463#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006464 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006465#endif
6466#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006467 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006468#endif
6469#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006471#endif
6472#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006473 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006474#endif
6475#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006477#endif
6478#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006479 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006480#endif
6481#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006483#endif
6484#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006486#endif
Fred Draked86ed291999-12-15 15:34:33 +00006487#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00006489#endif
Fred Drakec9680921999-12-13 16:37:25 +00006490#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00006491 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00006492#endif
Fred Draked86ed291999-12-15 15:34:33 +00006493#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006494 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00006495#endif
6496#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006497 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00006498#endif
6499#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00006501#endif
6502#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00006504#endif
Fred Drakec9680921999-12-13 16:37:25 +00006505#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006507#endif
6508#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006509 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006510#endif
6511#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006513#endif
6514#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006515 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006516#endif
6517#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006518 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006519#endif
6520#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006522#endif
6523#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006524 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006525#endif
6526#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006527 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006528#endif
6529#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006530 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006531#endif
6532#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006533 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006534#endif
6535#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006536 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006537#endif
6538#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006539 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006540#endif
6541#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006542 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006543#endif
6544#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006545 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006546#endif
6547#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00006549#endif
6550#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00006551 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00006552#endif
Fred Draked86ed291999-12-15 15:34:33 +00006553#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006554 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006555#endif
6556#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00006557 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00006558#endif
6559#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00006560 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00006561#endif
6562#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006563 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006564#endif
6565#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006566 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006567#endif
6568#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006569 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006570#endif
6571#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006572 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006573#endif
6574#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006575 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006576#endif
6577#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006578 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006579#endif
6580#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006581 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006582#endif
6583#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006584 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006585#endif
6586#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006587 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006588#endif
6589#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006591#endif
Fred Drakec9680921999-12-13 16:37:25 +00006592};
6593
6594static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006595conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006596{
6597 return conv_confname(arg, valuep, posix_constants_confstr,
6598 sizeof(posix_constants_confstr)
6599 / sizeof(struct constdef));
6600}
6601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006602PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006603"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006604Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006605
6606static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006607posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006608{
6609 PyObject *result = NULL;
6610 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006611 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006612
6613 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006614 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006615
Fred Drakec9680921999-12-13 16:37:25 +00006616 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006617 len = confstr(name, buffer, sizeof(buffer));
6618 if (len == 0) {
6619 if (errno) {
6620 posix_error();
6621 }
6622 else {
6623 result = Py_None;
6624 Py_INCREF(Py_None);
6625 }
Fred Drakec9680921999-12-13 16:37:25 +00006626 }
6627 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006628 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006629 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006630 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006631 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006632 }
6633 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006634 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006635 }
6636 }
6637 return result;
6638}
6639#endif
6640
6641
6642#ifdef HAVE_SYSCONF
6643static struct constdef posix_constants_sysconf[] = {
6644#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006646#endif
6647#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006648 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006649#endif
6650#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006651 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006652#endif
6653#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006654 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006655#endif
6656#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006657 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006658#endif
6659#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006660 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006661#endif
6662#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006663 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006664#endif
6665#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006666 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006667#endif
6668#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006669 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006670#endif
6671#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006672 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006673#endif
Fred Draked86ed291999-12-15 15:34:33 +00006674#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006676#endif
6677#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006678 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006679#endif
Fred Drakec9680921999-12-13 16:37:25 +00006680#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006681 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006682#endif
Fred Drakec9680921999-12-13 16:37:25 +00006683#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006684 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006685#endif
6686#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006687 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006688#endif
6689#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006690 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006691#endif
6692#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006693 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006694#endif
6695#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006696 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006697#endif
Fred Draked86ed291999-12-15 15:34:33 +00006698#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006699 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006700#endif
Fred Drakec9680921999-12-13 16:37:25 +00006701#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006702 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006703#endif
6704#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006706#endif
6707#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006709#endif
6710#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006711 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006712#endif
6713#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006714 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006715#endif
Fred Draked86ed291999-12-15 15:34:33 +00006716#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006717 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006718#endif
Fred Drakec9680921999-12-13 16:37:25 +00006719#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006721#endif
6722#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006724#endif
6725#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006727#endif
6728#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006729 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006730#endif
6731#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006732 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006733#endif
6734#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006735 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006736#endif
6737#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006738 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006739#endif
6740#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006741 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006742#endif
6743#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006744 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006745#endif
6746#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006748#endif
6749#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006750 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006751#endif
6752#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006753 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006754#endif
6755#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006756 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006757#endif
6758#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006759 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006760#endif
6761#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006762 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006763#endif
6764#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006765 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006766#endif
6767#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006768 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006769#endif
6770#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006771 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006772#endif
6773#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006774 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006775#endif
6776#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006777 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006778#endif
6779#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006780 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006781#endif
6782#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006783 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006784#endif
6785#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006786 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006787#endif
Fred Draked86ed291999-12-15 15:34:33 +00006788#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006789 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006790#endif
Fred Drakec9680921999-12-13 16:37:25 +00006791#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006793#endif
6794#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006795 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006796#endif
6797#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006799#endif
Fred Draked86ed291999-12-15 15:34:33 +00006800#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006801 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006802#endif
Fred Drakec9680921999-12-13 16:37:25 +00006803#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006804 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006805#endif
Fred Draked86ed291999-12-15 15:34:33 +00006806#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006807 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006808#endif
6809#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006810 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006811#endif
Fred Drakec9680921999-12-13 16:37:25 +00006812#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006814#endif
6815#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006817#endif
6818#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006819 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006820#endif
6821#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006823#endif
Fred Draked86ed291999-12-15 15:34:33 +00006824#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006825 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006826#endif
Fred Drakec9680921999-12-13 16:37:25 +00006827#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006828 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006829#endif
6830#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006831 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006832#endif
6833#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006834 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006835#endif
6836#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006838#endif
6839#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006840 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006841#endif
6842#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006844#endif
6845#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006846 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006847#endif
Fred Draked86ed291999-12-15 15:34:33 +00006848#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006849 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006850#endif
Fred Drakec9680921999-12-13 16:37:25 +00006851#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006852 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006853#endif
6854#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006856#endif
Fred Draked86ed291999-12-15 15:34:33 +00006857#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006858 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006859#endif
Fred Drakec9680921999-12-13 16:37:25 +00006860#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006861 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006862#endif
6863#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006865#endif
6866#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006868#endif
6869#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006871#endif
6872#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006874#endif
6875#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006876 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006877#endif
6878#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006880#endif
6881#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006882 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006883#endif
6884#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006886#endif
Fred Draked86ed291999-12-15 15:34:33 +00006887#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006888 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006889#endif
6890#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006891 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006892#endif
Fred Drakec9680921999-12-13 16:37:25 +00006893#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006894 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006895#endif
6896#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006897 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006898#endif
6899#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006900 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006901#endif
6902#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006904#endif
6905#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006906 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006907#endif
6908#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006909 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006910#endif
6911#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006912 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006913#endif
6914#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006916#endif
6917#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006919#endif
6920#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006922#endif
6923#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006924 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006925#endif
6926#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006927 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006928#endif
6929#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006930 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006931#endif
6932#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006933 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006934#endif
6935#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006936 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006937#endif
6938#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006939 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006940#endif
6941#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006942 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006943#endif
6944#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006945 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006946#endif
6947#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006948 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006949#endif
6950#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006951 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006952#endif
6953#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006954 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006955#endif
6956#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006957 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006958#endif
6959#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006961#endif
6962#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006963 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006964#endif
6965#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006967#endif
6968#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006969 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006970#endif
6971#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00006972 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006973#endif
6974#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006975 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006976#endif
6977#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006978 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006979#endif
6980#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006981 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00006982#endif
6983#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006984 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006985#endif
6986#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006987 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006988#endif
6989#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006991#endif
6992#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006993 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006994#endif
6995#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006996 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006997#endif
Fred Draked86ed291999-12-15 15:34:33 +00006998#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00006999 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00007000#endif
Fred Drakec9680921999-12-13 16:37:25 +00007001#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00007002 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00007003#endif
7004#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007006#endif
7007#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00007009#endif
7010#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007011 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007012#endif
7013#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00007014 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00007015#endif
7016#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007017 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007018#endif
7019#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00007020 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00007021#endif
7022#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00007024#endif
7025#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007027#endif
7028#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007030#endif
7031#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00007033#endif
7034#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007035 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00007036#endif
7037#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00007039#endif
7040#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00007041 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00007042#endif
7043#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00007044 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00007045#endif
7046#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007047 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00007048#endif
7049#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007050 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007051#endif
7052#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00007053 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00007054#endif
7055#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007056 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007057#endif
7058#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007059 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007060#endif
7061#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007062 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007063#endif
7064#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007065 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007066#endif
7067#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007068 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007069#endif
7070#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007071 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007072#endif
7073#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00007074 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00007075#endif
7076#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007078#endif
7079#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00007081#endif
7082#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007084#endif
7085#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00007087#endif
7088#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00007089 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00007090#endif
7091#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007093#endif
7094#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00007095 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00007096#endif
7097#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00007099#endif
7100#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00007102#endif
7103#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00007104 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00007105#endif
7106#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00007107 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00007108#endif
7109#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007110 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00007111#endif
7112#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00007114#endif
7115#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00007116 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00007117#endif
7118#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00007119 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00007120#endif
7121#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007123#endif
7124#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00007125 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00007126#endif
7127#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00007128 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00007129#endif
7130#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00007132#endif
7133#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00007135#endif
7136};
7137
7138static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007139conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00007140{
7141 return conv_confname(arg, valuep, posix_constants_sysconf,
7142 sizeof(posix_constants_sysconf)
7143 / sizeof(struct constdef));
7144}
7145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007146PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007147"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007148Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00007149
7150static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007151posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00007152{
7153 PyObject *result = NULL;
7154 int name;
7155
7156 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
7157 int value;
7158
7159 errno = 0;
7160 value = sysconf(name);
7161 if (value == -1 && errno != 0)
7162 posix_error();
7163 else
Christian Heimes217cfd12007-12-02 14:31:20 +00007164 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00007165 }
7166 return result;
7167}
7168#endif
7169
7170
Fred Drakebec628d1999-12-15 18:31:10 +00007171/* This code is used to ensure that the tables of configuration value names
7172 * are in sorted order as required by conv_confname(), and also to build the
7173 * the exported dictionaries that are used to publish information about the
7174 * names available on the host platform.
7175 *
7176 * Sorting the table at runtime ensures that the table is properly ordered
7177 * when used, even for platforms we're not able to test on. It also makes
7178 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00007179 */
Fred Drakebec628d1999-12-15 18:31:10 +00007180
7181static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007182cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00007183{
7184 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007185 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00007186 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00007187 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00007188
7189 return strcmp(c1->name, c2->name);
7190}
7191
7192static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007193setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00007194 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007195{
Fred Drakebec628d1999-12-15 18:31:10 +00007196 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00007197 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00007198
7199 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
7200 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00007201 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00007202 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007203
Barry Warsaw3155db32000-04-13 15:20:40 +00007204 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007205 PyObject *o = PyLong_FromLong(table[i].value);
7206 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
7207 Py_XDECREF(o);
7208 Py_DECREF(d);
7209 return -1;
7210 }
7211 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00007212 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00007213 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00007214}
7215
Fred Drakebec628d1999-12-15 18:31:10 +00007216/* Return -1 on failure, 0 on success. */
7217static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007218setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00007219{
7220#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00007221 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00007222 sizeof(posix_constants_pathconf)
7223 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007224 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007225 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007226#endif
7227#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00007228 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00007229 sizeof(posix_constants_confstr)
7230 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007231 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007233#endif
7234#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00007235 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00007236 sizeof(posix_constants_sysconf)
7237 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00007238 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00007240#endif
Fred Drakebec628d1999-12-15 18:31:10 +00007241 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00007242}
Fred Draked86ed291999-12-15 15:34:33 +00007243
7244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007245PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007246"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007247Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007248in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007249
7250static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007251posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007252{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007253 abort();
7254 /*NOTREACHED*/
7255 Py_FatalError("abort() called from Python code didn't abort!");
7256 return NULL;
7257}
Fred Drakebec628d1999-12-15 18:31:10 +00007258
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007259#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007260PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00007261"startfile(filepath [, operation]) - Start a file with its associated\n\
7262application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007263\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00007264When \"operation\" is not specified or \"open\", this acts like\n\
7265double-clicking the file in Explorer, or giving the file name as an\n\
7266argument to the DOS \"start\" command: the file is opened with whatever\n\
7267application (if any) its extension is associated.\n\
7268When another \"operation\" is given, it specifies what should be done with\n\
7269the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00007270\n\
7271startfile returns as soon as the associated application is launched.\n\
7272There is no option to wait for the application to close, and no way\n\
7273to retrieve the application's exit status.\n\
7274\n\
7275The filepath is relative to the current directory. If you want to use\n\
7276an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007277the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00007278
7279static PyObject *
7280win32_startfile(PyObject *self, PyObject *args)
7281{
Victor Stinner8c62be82010-05-06 00:08:46 +00007282 PyObject *ofilepath;
7283 char *filepath;
7284 char *operation = NULL;
7285 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00007286
Victor Stinner8c62be82010-05-06 00:08:46 +00007287 PyObject *unipath, *woperation = NULL;
7288 if (!PyArg_ParseTuple(args, "U|s:startfile",
7289 &unipath, &operation)) {
7290 PyErr_Clear();
7291 goto normal;
7292 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007293
Victor Stinner8c62be82010-05-06 00:08:46 +00007294 if (operation) {
7295 woperation = PyUnicode_DecodeASCII(operation,
7296 strlen(operation), NULL);
7297 if (!woperation) {
7298 PyErr_Clear();
7299 operation = NULL;
7300 goto normal;
7301 }
7302 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00007303
Victor Stinner8c62be82010-05-06 00:08:46 +00007304 Py_BEGIN_ALLOW_THREADS
7305 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
7306 PyUnicode_AS_UNICODE(unipath),
7307 NULL, NULL, SW_SHOWNORMAL);
7308 Py_END_ALLOW_THREADS
7309
7310 Py_XDECREF(woperation);
7311 if (rc <= (HINSTANCE)32) {
7312 PyObject *errval = win32_error_unicode("startfile",
7313 PyUnicode_AS_UNICODE(unipath));
7314 return errval;
7315 }
7316 Py_INCREF(Py_None);
7317 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007318
7319normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 if (!PyArg_ParseTuple(args, "O&|s:startfile",
7321 PyUnicode_FSConverter, &ofilepath,
7322 &operation))
7323 return NULL;
7324 filepath = PyBytes_AsString(ofilepath);
7325 Py_BEGIN_ALLOW_THREADS
7326 rc = ShellExecute((HWND)0, operation, filepath,
7327 NULL, NULL, SW_SHOWNORMAL);
7328 Py_END_ALLOW_THREADS
7329 if (rc <= (HINSTANCE)32) {
7330 PyObject *errval = win32_error("startfile", filepath);
7331 Py_DECREF(ofilepath);
7332 return errval;
7333 }
7334 Py_DECREF(ofilepath);
7335 Py_INCREF(Py_None);
7336 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00007337}
7338#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007339
Martin v. Löwis438b5342002-12-27 10:16:42 +00007340#ifdef HAVE_GETLOADAVG
7341PyDoc_STRVAR(posix_getloadavg__doc__,
7342"getloadavg() -> (float, float, float)\n\n\
7343Return the number of processes in the system run queue averaged over\n\
7344the last 1, 5, and 15 minutes or raises OSError if the load average\n\
7345was unobtainable");
7346
7347static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007348posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00007349{
7350 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00007351 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007352 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
7353 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00007354 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00007356}
7357#endif
7358
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007359#ifdef MS_WINDOWS
7360
7361PyDoc_STRVAR(win32_urandom__doc__,
7362"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007363Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007364
7365typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
7366 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
7367 DWORD dwFlags );
7368typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
7369 BYTE *pbBuffer );
7370
7371static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00007372/* This handle is never explicitly released. Instead, the operating
7373 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007374static HCRYPTPROV hCryptProv = 0;
7375
Tim Peters4ad82172004-08-30 17:02:04 +00007376static PyObject*
7377win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007378{
Victor Stinner8c62be82010-05-06 00:08:46 +00007379 int howMany;
7380 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007381
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 /* Read arguments */
7383 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7384 return NULL;
7385 if (howMany < 0)
7386 return PyErr_Format(PyExc_ValueError,
7387 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007388
Victor Stinner8c62be82010-05-06 00:08:46 +00007389 if (hCryptProv == 0) {
7390 HINSTANCE hAdvAPI32 = NULL;
7391 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007392
Victor Stinner8c62be82010-05-06 00:08:46 +00007393 /* Obtain handle to the DLL containing CryptoAPI
7394 This should not fail */
7395 hAdvAPI32 = GetModuleHandle("advapi32.dll");
7396 if(hAdvAPI32 == NULL)
7397 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007398
Victor Stinner8c62be82010-05-06 00:08:46 +00007399 /* Obtain pointers to the CryptoAPI functions
7400 This will fail on some early versions of Win95 */
7401 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
7402 hAdvAPI32,
7403 "CryptAcquireContextA");
7404 if (pCryptAcquireContext == NULL)
7405 return PyErr_Format(PyExc_NotImplementedError,
7406 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007407
Victor Stinner8c62be82010-05-06 00:08:46 +00007408 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
7409 hAdvAPI32, "CryptGenRandom");
7410 if (pCryptGenRandom == NULL)
7411 return PyErr_Format(PyExc_NotImplementedError,
7412 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007413
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 /* Acquire context */
7415 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
7416 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
7417 return win32_error("CryptAcquireContext", NULL);
7418 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007419
Victor Stinner8c62be82010-05-06 00:08:46 +00007420 /* Allocate bytes */
7421 result = PyBytes_FromStringAndSize(NULL, howMany);
7422 if (result != NULL) {
7423 /* Get random data */
7424 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
7425 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
7426 PyBytes_AS_STRING(result))) {
7427 Py_DECREF(result);
7428 return win32_error("CryptGenRandom", NULL);
7429 }
7430 }
7431 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007432}
7433#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007434
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007435PyDoc_STRVAR(device_encoding__doc__,
7436"device_encoding(fd) -> str\n\n\
7437Return a string describing the encoding of the device\n\
7438if the output is a terminal; else return None.");
7439
7440static PyObject *
7441device_encoding(PyObject *self, PyObject *args)
7442{
Victor Stinner8c62be82010-05-06 00:08:46 +00007443 int fd;
7444 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
7445 return NULL;
7446 if (!_PyVerify_fd(fd) || !isatty(fd)) {
7447 Py_INCREF(Py_None);
7448 return Py_None;
7449 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007450#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00007451 if (fd == 0) {
7452 char buf[100];
7453 sprintf(buf, "cp%d", GetConsoleCP());
7454 return PyUnicode_FromString(buf);
7455 }
7456 if (fd == 1 || fd == 2) {
7457 char buf[100];
7458 sprintf(buf, "cp%d", GetConsoleOutputCP());
7459 return PyUnicode_FromString(buf);
7460 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007461#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 {
7463 char *codeset = nl_langinfo(CODESET);
7464 if (codeset != NULL && codeset[0] != 0)
7465 return PyUnicode_FromString(codeset);
7466 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007467#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 Py_INCREF(Py_None);
7469 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00007470}
7471
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007472#ifdef __VMS
7473/* Use openssl random routine */
7474#include <openssl/rand.h>
7475PyDoc_STRVAR(vms_urandom__doc__,
7476"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00007477Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007478
7479static PyObject*
7480vms_urandom(PyObject *self, PyObject *args)
7481{
Victor Stinner8c62be82010-05-06 00:08:46 +00007482 int howMany;
7483 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007484
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 /* Read arguments */
7486 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
7487 return NULL;
7488 if (howMany < 0)
7489 return PyErr_Format(PyExc_ValueError,
7490 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007491
Victor Stinner8c62be82010-05-06 00:08:46 +00007492 /* Allocate bytes */
7493 result = PyBytes_FromStringAndSize(NULL, howMany);
7494 if (result != NULL) {
7495 /* Get random data */
7496 if (RAND_pseudo_bytes((unsigned char*)
7497 PyBytes_AS_STRING(result),
7498 howMany) < 0) {
7499 Py_DECREF(result);
7500 return PyErr_Format(PyExc_ValueError,
7501 "RAND_pseudo_bytes");
7502 }
7503 }
7504 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007505}
7506#endif
7507
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007508#ifdef HAVE_SETRESUID
7509PyDoc_STRVAR(posix_setresuid__doc__,
7510"setresuid(ruid, euid, suid)\n\n\
7511Set the current process's real, effective, and saved user ids.");
7512
7513static PyObject*
7514posix_setresuid (PyObject *self, PyObject *args)
7515{
Victor Stinner8c62be82010-05-06 00:08:46 +00007516 /* We assume uid_t is no larger than a long. */
7517 long ruid, euid, suid;
7518 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
7519 return NULL;
7520 if (setresuid(ruid, euid, suid) < 0)
7521 return posix_error();
7522 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007523}
7524#endif
7525
7526#ifdef HAVE_SETRESGID
7527PyDoc_STRVAR(posix_setresgid__doc__,
7528"setresgid(rgid, egid, sgid)\n\n\
7529Set the current process's real, effective, and saved group ids.");
7530
7531static PyObject*
7532posix_setresgid (PyObject *self, PyObject *args)
7533{
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 /* We assume uid_t is no larger than a long. */
7535 long rgid, egid, sgid;
7536 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
7537 return NULL;
7538 if (setresgid(rgid, egid, sgid) < 0)
7539 return posix_error();
7540 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007541}
7542#endif
7543
7544#ifdef HAVE_GETRESUID
7545PyDoc_STRVAR(posix_getresuid__doc__,
7546"getresuid() -> (ruid, euid, suid)\n\n\
7547Get tuple of the current process's real, effective, and saved user ids.");
7548
7549static PyObject*
7550posix_getresuid (PyObject *self, PyObject *noargs)
7551{
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 uid_t ruid, euid, suid;
7553 long l_ruid, l_euid, l_suid;
7554 if (getresuid(&ruid, &euid, &suid) < 0)
7555 return posix_error();
7556 /* Force the values into long's as we don't know the size of uid_t. */
7557 l_ruid = ruid;
7558 l_euid = euid;
7559 l_suid = suid;
7560 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007561}
7562#endif
7563
7564#ifdef HAVE_GETRESGID
7565PyDoc_STRVAR(posix_getresgid__doc__,
7566"getresgid() -> (rgid, egid, sgid)\n\n\
7567Get tuple of the current process's real, effective, and saved user ids.");
7568
7569static PyObject*
7570posix_getresgid (PyObject *self, PyObject *noargs)
7571{
Victor Stinner8c62be82010-05-06 00:08:46 +00007572 uid_t rgid, egid, sgid;
7573 long l_rgid, l_egid, l_sgid;
7574 if (getresgid(&rgid, &egid, &sgid) < 0)
7575 return posix_error();
7576 /* Force the values into long's as we don't know the size of uid_t. */
7577 l_rgid = rgid;
7578 l_egid = egid;
7579 l_sgid = sgid;
7580 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007581}
7582#endif
7583
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007584static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007585 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007586#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007587 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007588#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007589 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007590#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007591 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007592#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007594#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007595 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007596#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007597#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007598 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007599#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007600#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007601 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007602#endif /* HAVE_LCHMOD */
7603#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007604 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007605#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007606#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007608#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007609#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007610 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007611#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007612#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007613 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007614#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007615#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007617#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007618#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007619 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7620 METH_NOARGS, posix_getcwd__doc__},
7621 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7622 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007623#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007624#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007626#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7628 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7629 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007630#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007631 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007632#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007633#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007634 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007635#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007636#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7637 {"readlink", win_readlink, METH_VARARGS, win_readlink__doc__},
7638#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7639 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7640 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7641 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007643#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007644 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007645#endif /* HAVE_SYMLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +00007646#if !defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin74e45612010-07-09 15:58:59 +00007647 {"symlink", (PyCFunction)win_symlink, METH_VARARGS | METH_KEYWORDS,
7648 win_symlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +00007649#endif /* !defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007650#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007652#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007653 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007654#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007655 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007656#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007657 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7658 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7659 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007660#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007662#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007664#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007665 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7666 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007667#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007668#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007669 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7670 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007671#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007672 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7673 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007674#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007675#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007676#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007677 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007678#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007679#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007680 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007681#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007682#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007683 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007684#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007685#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007686 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007687#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007688#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007689 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007690#endif /* HAVE_GETEGID */
7691#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007692 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007693#endif /* HAVE_GETEUID */
7694#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007695 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007696#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007697#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007699#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007700 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007701#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007703#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007704#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007705 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007706#endif /* HAVE_GETPPID */
7707#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007708 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007709#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007710#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007711 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007712#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007713#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007714 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007715#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007716#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007718#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007719#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007720 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007721#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007722#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007723 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7724 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007725#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007726#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007727 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007728#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007729#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007730 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007731#endif /* HAVE_SETEUID */
7732#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007733 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007734#endif /* HAVE_SETEGID */
7735#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007736 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007737#endif /* HAVE_SETREUID */
7738#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007739 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007740#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007741#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007742 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007743#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007744#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007745 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007746#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007747#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007749#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007750#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007751 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007752#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007753#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007754 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007755#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007756#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007758#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007759#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007760 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007761#endif /* HAVE_WAIT3 */
7762#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007763 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007764#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007765#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007767#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007768#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007769 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007770#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007771#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007772 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007773#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007774#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007775 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007776#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007777#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007778 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007779#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007780#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007781 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007782#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7784 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7785 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7786 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7787 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7788 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7789 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7790 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7791 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7792 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7793 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007794#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007795 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007796#endif
7797#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007798 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007799#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007800#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007801 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007802#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007803#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007804 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7805 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7806 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007807#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007808#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007809 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007810#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007811#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007812 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007813#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007814#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007815 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007816#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007817 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007818#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007819 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007820#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007821#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007822 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007823#endif
7824#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007825 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007826#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007827#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007828#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007830#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007831#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007833#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007834#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007835 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007836#endif /* WIFSTOPPED */
7837#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007839#endif /* WIFSIGNALED */
7840#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007842#endif /* WIFEXITED */
7843#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007844 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007845#endif /* WEXITSTATUS */
7846#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007847 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007848#endif /* WTERMSIG */
7849#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007850 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007851#endif /* WSTOPSIG */
7852#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007853#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007855#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007856#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007858#endif
Fred Drakec9680921999-12-13 16:37:25 +00007859#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007860 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007861#endif
7862#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007863 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007864#endif
7865#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007866 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007867#endif
7868#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007869 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007870#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007872#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +00007874 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007875#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007876#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007878#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007879 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007881 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007882 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007884 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007885#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007886 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007887#endif
7888#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007889 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007890#endif
7891#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007893#endif
7894#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007895 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007896#endif
7897
Victor Stinner8c62be82010-05-06 00:08:46 +00007898 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007899};
7900
7901
Barry Warsaw4a342091996-12-19 23:50:02 +00007902static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007903ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007904{
Victor Stinner8c62be82010-05-06 00:08:46 +00007905 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007906}
7907
Guido van Rossumd48f2521997-12-05 22:19:34 +00007908#if defined(PYOS_OS2)
7909/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007910static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007911{
7912 APIRET rc;
7913 ULONG values[QSV_MAX+1];
7914 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007915 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007916
7917 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007918 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007919 Py_END_ALLOW_THREADS
7920
7921 if (rc != NO_ERROR) {
7922 os2_error(rc);
7923 return -1;
7924 }
7925
Fred Drake4d1e64b2002-04-15 19:40:07 +00007926 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7927 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7928 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7929 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7930 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7931 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7932 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007933
7934 switch (values[QSV_VERSION_MINOR]) {
7935 case 0: ver = "2.00"; break;
7936 case 10: ver = "2.10"; break;
7937 case 11: ver = "2.11"; break;
7938 case 30: ver = "3.00"; break;
7939 case 40: ver = "4.00"; break;
7940 case 50: ver = "5.00"; break;
7941 default:
Tim Peters885d4572001-11-28 20:27:42 +00007942 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007943 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007944 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007945 ver = &tmp[0];
7946 }
7947
7948 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007949 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007950 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007951
7952 /* Add Indicator of Which Drive was Used to Boot the System */
7953 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7954 tmp[1] = ':';
7955 tmp[2] = '\0';
7956
Fred Drake4d1e64b2002-04-15 19:40:07 +00007957 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007958}
7959#endif
7960
Barry Warsaw4a342091996-12-19 23:50:02 +00007961static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007962all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007963{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007964#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007965 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007966#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007967#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007968 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007969#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007970#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007971 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007972#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007973#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007974 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007975#endif
Fred Drakec9680921999-12-13 16:37:25 +00007976#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00007978#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007979#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007980 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007981#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007982#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007983 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007984#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007985#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00007986 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007987#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007988#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00007989 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007990#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007991#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007992 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007993#endif
7994#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007995 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007996#endif
7997#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007999#endif
8000#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00008001 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008002#endif
8003#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008004 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008005#endif
8006#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008008#endif
8009#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008011#endif
8012#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008014#endif
8015#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008016 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008017#endif
8018#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00008019 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008020#endif
8021#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008022 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008023#endif
8024#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00008025 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008026#endif
8027#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008028 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00008029#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00008030#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008031 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008032#endif
8033#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00008034 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00008035#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008036#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008037 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008038#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00008039#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008040 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008041#endif
8042#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00008043 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00008044#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008045
Tim Peters5aa91602002-01-30 05:46:57 +00008046/* MS Windows */
8047#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00008048 /* Don't inherit in child processes. */
8049 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008050#endif
8051#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00008052 /* Optimize for short life (keep in memory). */
8053 /* MS forgot to define this one with a non-underscore form too. */
8054 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008055#endif
8056#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 /* Automatically delete when last handle is closed. */
8058 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008059#endif
8060#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 /* Optimize for random access. */
8062 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008063#endif
8064#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008065 /* Optimize for sequential access. */
8066 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00008067#endif
8068
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008069/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008070#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 /* Send a SIGIO signal whenever input or output
8072 becomes available on file descriptor */
8073 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00008074#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008075#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 /* Direct disk access. */
8077 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008078#endif
8079#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00008080 /* Must be a directory. */
8081 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008082#endif
8083#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00008084 /* Do not follow links. */
8085 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00008086#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008087#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00008088 /* Do not update the access time. */
8089 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00008090#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00008091
Victor Stinner8c62be82010-05-06 00:08:46 +00008092 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008093#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00008094 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008095#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008096#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00008097 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008098#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008099#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008100 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008101#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008102#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008103 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008104#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008105#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00008106 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008107#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008108#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00008109 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008110#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008111#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008113#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008114#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008116#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008117#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008119#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008120#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00008121 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008122#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008123#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00008124 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008125#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008126#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00008127 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008128#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008129#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00008130 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008131#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008132#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00008133 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008134#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008135#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008137#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008138#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00008139 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008140#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008141#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00008143#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00008144
Guido van Rossum246bc171999-02-01 23:54:31 +00008145#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008146#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
8148 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
8149 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
8150 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
8151 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
8152 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
8153 if (ins(d, "P_PM", (long)P_PM)) return -1;
8154 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
8155 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
8156 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
8157 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
8158 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
8159 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
8160 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
8161 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
8162 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
8163 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
8164 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
8165 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
8166 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008167#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
8169 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
8170 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
8171 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
8172 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00008173#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00008174#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00008175
Guido van Rossumd48f2521997-12-05 22:19:34 +00008176#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00008177 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008178#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008179 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00008180}
8181
8182
Tim Peters5aa91602002-01-30 05:46:57 +00008183#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008184#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008185#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008186
8187#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00008188#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008189#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00008190
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00008191#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00008192#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00008193#define MODNAME "posix"
8194#endif
8195
Martin v. Löwis1a214512008-06-11 05:26:20 +00008196static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00008197 PyModuleDef_HEAD_INIT,
8198 MODNAME,
8199 posix__doc__,
8200 -1,
8201 posix_methods,
8202 NULL,
8203 NULL,
8204 NULL,
8205 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00008206};
8207
8208
Mark Hammondfe51c6d2002-08-02 02:27:13 +00008209PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00008210INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00008211{
Victor Stinner8c62be82010-05-06 00:08:46 +00008212 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00008213
Victor Stinner8c62be82010-05-06 00:08:46 +00008214 m = PyModule_Create(&posixmodule);
8215 if (m == NULL)
8216 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008217
Victor Stinner8c62be82010-05-06 00:08:46 +00008218 /* Initialize environ dictionary */
8219 v = convertenviron();
8220 Py_XINCREF(v);
8221 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
8222 return NULL;
8223 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00008224
Victor Stinner8c62be82010-05-06 00:08:46 +00008225 if (all_ins(m))
8226 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00008227
Victor Stinner8c62be82010-05-06 00:08:46 +00008228 if (setup_confname_tables(m))
8229 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00008230
Victor Stinner8c62be82010-05-06 00:08:46 +00008231 Py_INCREF(PyExc_OSError);
8232 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00008233
Guido van Rossumb3d39562000-01-31 18:41:26 +00008234#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00008235 if (posix_putenv_garbage == NULL)
8236 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00008237#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008238
Victor Stinner8c62be82010-05-06 00:08:46 +00008239 if (!initialized) {
8240 stat_result_desc.name = MODNAME ".stat_result";
8241 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
8242 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
8243 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
8244 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
8245 structseq_new = StatResultType.tp_new;
8246 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008247
Victor Stinner8c62be82010-05-06 00:08:46 +00008248 statvfs_result_desc.name = MODNAME ".statvfs_result";
8249 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008250#ifdef NEED_TICKS_PER_SECOND
8251# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00008252 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008253# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00008254 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008255# else
Victor Stinner8c62be82010-05-06 00:08:46 +00008256 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00008257# endif
8258#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 }
8260 Py_INCREF((PyObject*) &StatResultType);
8261 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
8262 Py_INCREF((PyObject*) &StatVFSResultType);
8263 PyModule_AddObject(m, "statvfs_result",
8264 (PyObject*) &StatVFSResultType);
8265 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008266
8267#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00008268 /*
8269 * Step 2 of weak-linking support on Mac OS X.
8270 *
8271 * The code below removes functions that are not available on the
8272 * currently active platform.
8273 *
8274 * This block allow one to use a python binary that was build on
8275 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
8276 * OSX 10.4.
8277 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00008278#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008279 if (fstatvfs == NULL) {
8280 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
8281 return NULL;
8282 }
8283 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008284#endif /* HAVE_FSTATVFS */
8285
8286#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 if (statvfs == NULL) {
8288 if (PyObject_DelAttrString(m, "statvfs") == -1) {
8289 return NULL;
8290 }
8291 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008292#endif /* HAVE_STATVFS */
8293
8294# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00008295 if (lchown == NULL) {
8296 if (PyObject_DelAttrString(m, "lchown") == -1) {
8297 return NULL;
8298 }
8299 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00008300#endif /* HAVE_LCHOWN */
8301
8302
8303#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00008304 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00008305
Guido van Rossumb6775db1994-08-01 11:34:53 +00008306}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008307
8308#ifdef __cplusplus
8309}
8310#endif