blob: ac866d7558ce03e86f1bc8a20458f99c987cbfda [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;
501 k = PyUnicode_Decode(*e, (int)(p-*e),
502 Py_FileSystemDefaultEncoding, "surrogateescape");
503 if (k == NULL) {
504 PyErr_Clear();
505 continue;
506 }
507 v = PyUnicode_Decode(p+1, strlen(p+1),
508 Py_FileSystemDefaultEncoding, "surrogateescape");
509 if (v == NULL) {
510 PyErr_Clear();
511 Py_DECREF(k);
512 continue;
513 }
514 if (PyDict_GetItem(d, k) == NULL) {
515 if (PyDict_SetItem(d, k, v) != 0)
516 PyErr_Clear();
517 }
518 Py_DECREF(k);
519 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000520 }
521#endif
Victor Stinner8c62be82010-05-06 00:08:46 +0000522#if defined(PYOS_OS2)
523 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
524 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
525 PyObject *v = PyBytes_FromString(buffer);
526 PyDict_SetItemString(d, "BEGINLIBPATH", v);
527 Py_DECREF(v);
528 }
529 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
530 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
531 PyObject *v = PyBytes_FromString(buffer);
532 PyDict_SetItemString(d, "ENDLIBPATH", v);
533 Py_DECREF(v);
534 }
535#endif
536 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537}
538
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539/* Set a POSIX-specific error from errno, and return NULL */
540
Barry Warsawd58d7641998-07-23 16:14:40 +0000541static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000542posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +0000543{
Victor Stinner8c62be82010-05-06 00:08:46 +0000544 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000545}
Barry Warsawd58d7641998-07-23 16:14:40 +0000546static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000547posix_error_with_filename(char* name)
Barry Warsawd58d7641998-07-23 16:14:40 +0000548{
Victor Stinner8c62be82010-05-06 00:08:46 +0000549 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000550}
551
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000552#ifdef MS_WINDOWS
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000553static PyObject *
554posix_error_with_unicode_filename(Py_UNICODE* name)
555{
Victor Stinner8c62be82010-05-06 00:08:46 +0000556 return PyErr_SetFromErrnoWithUnicodeFilename(PyExc_OSError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000557}
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000558#endif /* MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000559
560
Mark Hammondef8b6542001-05-13 08:04:26 +0000561static PyObject *
Martin v. Löwis011e8422009-05-05 04:43:17 +0000562posix_error_with_allocated_filename(PyObject* name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000563{
Victor Stinner8c62be82010-05-06 00:08:46 +0000564 PyObject *rc = PyErr_SetFromErrnoWithFilename(PyExc_OSError,
565 PyBytes_AsString(name));
566 Py_DECREF(name);
567 return rc;
Mark Hammondef8b6542001-05-13 08:04:26 +0000568}
569
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000570#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000571static PyObject *
572win32_error(char* function, char* filename)
573{
Victor Stinner8c62be82010-05-06 00:08:46 +0000574 /* XXX We should pass the function name along in the future.
575 (winreg.c also wants to pass the function name.)
576 This would however require an additional param to the
577 Windows error object, which is non-trivial.
578 */
579 errno = GetLastError();
580 if (filename)
581 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
582 else
583 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +0000584}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000585
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000586static PyObject *
587win32_error_unicode(char* function, Py_UNICODE* filename)
588{
Victor Stinner8c62be82010-05-06 00:08:46 +0000589 /* XXX - see win32_error for comments on 'function' */
590 errno = GetLastError();
591 if (filename)
592 return PyErr_SetFromWindowsErrWithUnicodeFilename(errno, filename);
593 else
594 return PyErr_SetFromWindowsErr(errno);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000595}
596
Thomas Wouters477c8d52006-05-27 19:21:47 +0000597static int
Hirokazu Yamamotod7e4c082008-08-17 09:30:15 +0000598convert_to_unicode(PyObject **param)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000599{
Victor Stinner8c62be82010-05-06 00:08:46 +0000600 if (PyUnicode_CheckExact(*param))
601 Py_INCREF(*param);
602 else if (PyUnicode_Check(*param))
603 /* For a Unicode subtype that's not a Unicode object,
604 return a true Unicode object with the same data. */
605 *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
606 PyUnicode_GET_SIZE(*param));
607 else
608 *param = PyUnicode_FromEncodedObject(*param,
609 Py_FileSystemDefaultEncoding,
610 "strict");
611 return (*param) != NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000612}
613
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000614#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000615
Guido van Rossumd48f2521997-12-05 22:19:34 +0000616#if defined(PYOS_OS2)
617/**********************************************************************
618 * Helper Function to Trim and Format OS/2 Messages
619 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000620static void
Guido van Rossumd48f2521997-12-05 22:19:34 +0000621os2_formatmsg(char *msgbuf, int msglen, char *reason)
622{
623 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
624
625 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
626 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
627
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000628 while (lastc > msgbuf && isspace(Py_CHARMASK(*lastc)))
Guido van Rossumd48f2521997-12-05 22:19:34 +0000629 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
630 }
631
632 /* Add Optional Reason Text */
633 if (reason) {
634 strcat(msgbuf, " : ");
635 strcat(msgbuf, reason);
636 }
637}
638
639/**********************************************************************
640 * Decode an OS/2 Operating System Error Code
641 *
642 * A convenience function to lookup an OS/2 error code and return a
643 * text message we can use to raise a Python exception.
644 *
645 * Notes:
646 * The messages for errors returned from the OS/2 kernel reside in
647 * the file OSO001.MSG in the \OS2 directory hierarchy.
648 *
649 **********************************************************************/
Victor Stinner8c62be82010-05-06 00:08:46 +0000650static char *
Guido van Rossumd48f2521997-12-05 22:19:34 +0000651os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
652{
653 APIRET rc;
654 ULONG msglen;
655
656 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
657 Py_BEGIN_ALLOW_THREADS
658 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
659 errorcode, "oso001.msg", &msglen);
660 Py_END_ALLOW_THREADS
661
662 if (rc == NO_ERROR)
663 os2_formatmsg(msgbuf, msglen, reason);
664 else
Tim Peters1ceb5fb2001-11-28 20:32:57 +0000665 PyOS_snprintf(msgbuf, msgbuflen,
Victor Stinner8c62be82010-05-06 00:08:46 +0000666 "unknown OS error #%d", errorcode);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000667
668 return msgbuf;
669}
670
671/* Set an OS/2-specific error and return NULL. OS/2 kernel
672 errors are not in a global variable e.g. 'errno' nor are
673 they congruent with posix error numbers. */
674
Victor Stinner8c62be82010-05-06 00:08:46 +0000675static PyObject *
676os2_error(int code)
Guido van Rossumd48f2521997-12-05 22:19:34 +0000677{
678 char text[1024];
679 PyObject *v;
680
681 os2_strerror(text, sizeof(text), code, "");
682
683 v = Py_BuildValue("(is)", code, text);
684 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000685 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000686 Py_DECREF(v);
687 }
688 return NULL; /* Signal to Python that an Exception is Pending */
689}
690
691#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692
693/* POSIX generic methods */
694
Barry Warsaw53699e91996-12-10 23:23:01 +0000695static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +0000696posix_fildes(PyObject *fdobj, int (*func)(int))
697{
Victor Stinner8c62be82010-05-06 00:08:46 +0000698 int fd;
699 int res;
700 fd = PyObject_AsFileDescriptor(fdobj);
701 if (fd < 0)
702 return NULL;
703 if (!_PyVerify_fd(fd))
704 return posix_error();
705 Py_BEGIN_ALLOW_THREADS
706 res = (*func)(fd);
707 Py_END_ALLOW_THREADS
708 if (res < 0)
709 return posix_error();
710 Py_INCREF(Py_None);
711 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +0000712}
Guido van Rossum21142a01999-01-08 21:05:37 +0000713
714static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715posix_1str(PyObject *args, char *format, int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716{
Victor Stinner8c62be82010-05-06 00:08:46 +0000717 PyObject *opath1 = NULL;
718 char *path1;
719 int res;
720 if (!PyArg_ParseTuple(args, format,
721 PyUnicode_FSConverter, &opath1))
722 return NULL;
723 path1 = PyBytes_AsString(opath1);
724 Py_BEGIN_ALLOW_THREADS
725 res = (*func)(path1);
726 Py_END_ALLOW_THREADS
727 if (res < 0)
728 return posix_error_with_allocated_filename(opath1);
729 Py_DECREF(opath1);
730 Py_INCREF(Py_None);
731 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732}
733
Barry Warsaw53699e91996-12-10 23:23:01 +0000734static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +0000735posix_2str(PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +0000736 char *format,
737 int (*func)(const char *, const char *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000738{
Victor Stinner8c62be82010-05-06 00:08:46 +0000739 PyObject *opath1 = NULL, *opath2 = NULL;
740 char *path1, *path2;
741 int res;
742 if (!PyArg_ParseTuple(args, format,
743 PyUnicode_FSConverter, &opath1,
744 PyUnicode_FSConverter, &opath2)) {
745 return NULL;
746 }
747 path1 = PyBytes_AsString(opath1);
748 path2 = PyBytes_AsString(opath2);
749 Py_BEGIN_ALLOW_THREADS
750 res = (*func)(path1, path2);
751 Py_END_ALLOW_THREADS
752 Py_DECREF(opath1);
753 Py_DECREF(opath2);
754 if (res != 0)
755 /* XXX how to report both path1 and path2??? */
756 return posix_error();
757 Py_INCREF(Py_None);
758 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000759}
760
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +0000761#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000762static PyObject*
Victor Stinner8c62be82010-05-06 00:08:46 +0000763win32_1str(PyObject* args, char* func,
764 char* format, BOOL (__stdcall *funcA)(LPCSTR),
765 char* wformat, BOOL (__stdcall *funcW)(LPWSTR))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000766{
Victor Stinner8c62be82010-05-06 00:08:46 +0000767 PyObject *uni;
768 char *ansi;
769 BOOL result;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +0000770
Victor Stinner8c62be82010-05-06 00:08:46 +0000771 if (!PyArg_ParseTuple(args, wformat, &uni))
772 PyErr_Clear();
773 else {
774 Py_BEGIN_ALLOW_THREADS
775 result = funcW(PyUnicode_AsUnicode(uni));
776 Py_END_ALLOW_THREADS
777 if (!result)
778 return win32_error_unicode(func, PyUnicode_AsUnicode(uni));
779 Py_INCREF(Py_None);
780 return Py_None;
781 }
782 if (!PyArg_ParseTuple(args, format, &ansi))
783 return NULL;
784 Py_BEGIN_ALLOW_THREADS
785 result = funcA(ansi);
786 Py_END_ALLOW_THREADS
787 if (!result)
788 return win32_error(func, ansi);
789 Py_INCREF(Py_None);
790 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000791
792}
793
794/* This is a reimplementation of the C library's chdir function,
795 but one that produces Win32 errors instead of DOS error codes.
796 chdir is essentially a wrapper around SetCurrentDirectory; however,
797 it also needs to set "magic" environment variables indicating
798 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000799static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000800win32_chdir(LPCSTR path)
801{
Victor Stinner8c62be82010-05-06 00:08:46 +0000802 char new_path[MAX_PATH+1];
803 int result;
804 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000805
Victor Stinner8c62be82010-05-06 00:08:46 +0000806 if(!SetCurrentDirectoryA(path))
807 return FALSE;
808 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
809 if (!result)
810 return FALSE;
811 /* In the ANSI API, there should not be any paths longer
812 than MAX_PATH. */
813 assert(result <= MAX_PATH+1);
814 if (strncmp(new_path, "\\\\", 2) == 0 ||
815 strncmp(new_path, "//", 2) == 0)
816 /* UNC path, nothing to do. */
817 return TRUE;
818 env[1] = new_path[0];
819 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000820}
821
822/* The Unicode version differs from the ANSI version
823 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +0000824static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +0000825win32_wchdir(LPCWSTR path)
826{
Victor Stinner8c62be82010-05-06 00:08:46 +0000827 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
828 int result;
829 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830
Victor Stinner8c62be82010-05-06 00:08:46 +0000831 if(!SetCurrentDirectoryW(path))
832 return FALSE;
833 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
834 if (!result)
835 return FALSE;
836 if (result > MAX_PATH+1) {
837 new_path = malloc(result * sizeof(wchar_t));
838 if (!new_path) {
839 SetLastError(ERROR_OUTOFMEMORY);
840 return FALSE;
841 }
842 result = GetCurrentDirectoryW(result, new_path);
843 if (!result) {
844 free(new_path);
845 return FALSE;
846 }
847 }
848 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
849 wcsncmp(new_path, L"//", 2) == 0)
850 /* UNC path, nothing to do. */
851 return TRUE;
852 env[1] = new_path[0];
853 result = SetEnvironmentVariableW(env, new_path);
854 if (new_path != _new_path)
855 free(new_path);
856 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000857}
858#endif
859
Martin v. Löwis14694662006-02-03 12:54:16 +0000860#ifdef MS_WINDOWS
861/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
862 - time stamps are restricted to second resolution
863 - file modification times suffer from forth-and-back conversions between
864 UTC and local time
865 Therefore, we implement our own stat, based on the Win32 API directly.
866*/
Victor Stinner8c62be82010-05-06 00:08:46 +0000867#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +0000868
869struct win32_stat{
870 int st_dev;
871 __int64 st_ino;
872 unsigned short st_mode;
873 int st_nlink;
874 int st_uid;
875 int st_gid;
876 int st_rdev;
877 __int64 st_size;
878 int st_atime;
879 int st_atime_nsec;
880 int st_mtime;
881 int st_mtime_nsec;
882 int st_ctime;
883 int st_ctime_nsec;
884};
885
886static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
887
888static void
889FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, int *time_out, int* nsec_out)
890{
Victor Stinner8c62be82010-05-06 00:08:46 +0000891 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
892 /* Cannot simply cast and dereference in_ptr,
893 since it might not be aligned properly */
894 __int64 in;
895 memcpy(&in, in_ptr, sizeof(in));
896 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
897 /* XXX Win32 supports time stamps past 2038; we currently don't */
898 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, int);
Martin v. Löwis14694662006-02-03 12:54:16 +0000899}
900
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901static void
902time_t_to_FILE_TIME(int time_in, int nsec_in, FILETIME *out_ptr)
903{
Victor Stinner8c62be82010-05-06 00:08:46 +0000904 /* XXX endianness */
905 __int64 out;
906 out = time_in + secs_between_epochs;
907 out = out * 10000000 + nsec_in / 100;
908 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909}
910
Martin v. Löwis14694662006-02-03 12:54:16 +0000911/* Below, we *know* that ugo+r is 0444 */
912#if _S_IREAD != 0400
913#error Unsupported C library
914#endif
915static int
916attributes_to_mode(DWORD attr)
917{
Victor Stinner8c62be82010-05-06 00:08:46 +0000918 int m = 0;
919 if (attr & FILE_ATTRIBUTE_DIRECTORY)
920 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
921 else
922 m |= _S_IFREG;
923 if (attr & FILE_ATTRIBUTE_READONLY)
924 m |= 0444;
925 else
926 m |= 0666;
927 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +0000928}
929
930static int
931attribute_data_to_stat(WIN32_FILE_ATTRIBUTE_DATA *info, struct win32_stat *result)
932{
Victor Stinner8c62be82010-05-06 00:08:46 +0000933 memset(result, 0, sizeof(*result));
934 result->st_mode = attributes_to_mode(info->dwFileAttributes);
935 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
936 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
937 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
938 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Martin v. Löwis14694662006-02-03 12:54:16 +0000939
Victor Stinner8c62be82010-05-06 00:08:46 +0000940 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +0000941}
942
Guido van Rossumd8faa362007-04-27 19:54:29 +0000943static BOOL
944attributes_from_dir(LPCSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
945{
Victor Stinner8c62be82010-05-06 00:08:46 +0000946 HANDLE hFindFile;
947 WIN32_FIND_DATAA FileData;
948 hFindFile = FindFirstFileA(pszFile, &FileData);
949 if (hFindFile == INVALID_HANDLE_VALUE)
950 return FALSE;
951 FindClose(hFindFile);
952 pfad->dwFileAttributes = FileData.dwFileAttributes;
953 pfad->ftCreationTime = FileData.ftCreationTime;
954 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
955 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
956 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
957 pfad->nFileSizeLow = FileData.nFileSizeLow;
958 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000959}
960
961static BOOL
962attributes_from_dir_w(LPCWSTR pszFile, LPWIN32_FILE_ATTRIBUTE_DATA pfad)
963{
Victor Stinner8c62be82010-05-06 00:08:46 +0000964 HANDLE hFindFile;
965 WIN32_FIND_DATAW FileData;
966 hFindFile = FindFirstFileW(pszFile, &FileData);
967 if (hFindFile == INVALID_HANDLE_VALUE)
968 return FALSE;
969 FindClose(hFindFile);
970 pfad->dwFileAttributes = FileData.dwFileAttributes;
971 pfad->ftCreationTime = FileData.ftCreationTime;
972 pfad->ftLastAccessTime = FileData.ftLastAccessTime;
973 pfad->ftLastWriteTime = FileData.ftLastWriteTime;
974 pfad->nFileSizeHigh = FileData.nFileSizeHigh;
975 pfad->nFileSizeLow = FileData.nFileSizeLow;
976 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000977}
978
Victor Stinner8c62be82010-05-06 00:08:46 +0000979static int
Martin v. Löwis14694662006-02-03 12:54:16 +0000980win32_stat(const char* path, struct win32_stat *result)
981{
Victor Stinner8c62be82010-05-06 00:08:46 +0000982 WIN32_FILE_ATTRIBUTE_DATA info;
983 int code;
984 char *dot;
985 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &info)) {
986 if (GetLastError() != ERROR_SHARING_VIOLATION) {
987 /* Protocol violation: we explicitly clear errno, instead of
988 setting it to a POSIX error. Callers should use GetLastError. */
989 errno = 0;
990 return -1;
991 } else {
992 /* Could not get attributes on open file. Fall back to
993 reading the directory. */
994 if (!attributes_from_dir(path, &info)) {
995 /* Very strange. This should not fail now */
996 errno = 0;
997 return -1;
998 }
999 }
1000 }
1001 code = attribute_data_to_stat(&info, result);
1002 if (code != 0)
1003 return code;
1004 /* Set S_IFEXEC if it is an .exe, .bat, ... */
1005 dot = strrchr(path, '.');
1006 if (dot) {
1007 if (stricmp(dot, ".bat") == 0 ||
1008 stricmp(dot, ".cmd") == 0 ||
1009 stricmp(dot, ".exe") == 0 ||
1010 stricmp(dot, ".com") == 0)
1011 result->st_mode |= 0111;
1012 }
1013 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001014}
1015
Victor Stinner8c62be82010-05-06 00:08:46 +00001016static int
Martin v. Löwis14694662006-02-03 12:54:16 +00001017win32_wstat(const wchar_t* path, struct win32_stat *result)
1018{
Victor Stinner8c62be82010-05-06 00:08:46 +00001019 int code;
1020 const wchar_t *dot;
1021 WIN32_FILE_ATTRIBUTE_DATA info;
1022 if (!GetFileAttributesExW(path, GetFileExInfoStandard, &info)) {
1023 if (GetLastError() != ERROR_SHARING_VIOLATION) {
1024 /* Protocol violation: we explicitly clear errno, instead of
1025 setting it to a POSIX error. Callers should use GetLastError. */
1026 errno = 0;
1027 return -1;
1028 } else {
1029 /* Could not get attributes on open file. Fall back to
1030 reading the directory. */
1031 if (!attributes_from_dir_w(path, &info)) {
1032 /* Very strange. This should not fail now */
1033 errno = 0;
1034 return -1;
1035 }
1036 }
1037 }
1038 code = attribute_data_to_stat(&info, result);
1039 if (code < 0)
1040 return code;
1041 /* Set IFEXEC if it is an .exe, .bat, ... */
1042 dot = wcsrchr(path, '.');
1043 if (dot) {
1044 if (_wcsicmp(dot, L".bat") == 0 ||
1045 _wcsicmp(dot, L".cmd") == 0 ||
1046 _wcsicmp(dot, L".exe") == 0 ||
1047 _wcsicmp(dot, L".com") == 0)
1048 result->st_mode |= 0111;
1049 }
1050 return code;
Martin v. Löwis14694662006-02-03 12:54:16 +00001051}
1052
1053static int
1054win32_fstat(int file_number, struct win32_stat *result)
1055{
Victor Stinner8c62be82010-05-06 00:08:46 +00001056 BY_HANDLE_FILE_INFORMATION info;
1057 HANDLE h;
1058 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001059
Victor Stinner8c62be82010-05-06 00:08:46 +00001060 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001061
Victor Stinner8c62be82010-05-06 00:08:46 +00001062 /* Protocol violation: we explicitly clear errno, instead of
1063 setting it to a POSIX error. Callers should use GetLastError. */
1064 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001065
Victor Stinner8c62be82010-05-06 00:08:46 +00001066 if (h == INVALID_HANDLE_VALUE) {
1067 /* This is really a C library error (invalid file handle).
1068 We set the Win32 error to the closes one matching. */
1069 SetLastError(ERROR_INVALID_HANDLE);
1070 return -1;
1071 }
1072 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001073
Victor Stinner8c62be82010-05-06 00:08:46 +00001074 type = GetFileType(h);
1075 if (type == FILE_TYPE_UNKNOWN) {
1076 DWORD error = GetLastError();
1077 if (error != 0) {
1078 return -1;
1079 }
1080 /* else: valid but unknown file */
1081 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001082
Victor Stinner8c62be82010-05-06 00:08:46 +00001083 if (type != FILE_TYPE_DISK) {
1084 if (type == FILE_TYPE_CHAR)
1085 result->st_mode = _S_IFCHR;
1086 else if (type == FILE_TYPE_PIPE)
1087 result->st_mode = _S_IFIFO;
1088 return 0;
1089 }
1090
1091 if (!GetFileInformationByHandle(h, &info)) {
1092 return -1;
1093 }
1094
1095 /* similar to stat() */
1096 result->st_mode = attributes_to_mode(info.dwFileAttributes);
1097 result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow;
1098 FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1099 FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1100 FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
1101 /* specific to fstat() */
1102 result->st_nlink = info.nNumberOfLinks;
1103 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1104 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001105}
1106
1107#endif /* MS_WINDOWS */
1108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109PyDoc_STRVAR(stat_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001110"stat_result: Result from stat or lstat.\n\n\
1111This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001112 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001113or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1114\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001115Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1116or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001117\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001118See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001119
1120static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001121 {"st_mode", "protection bits"},
1122 {"st_ino", "inode"},
1123 {"st_dev", "device"},
1124 {"st_nlink", "number of hard links"},
1125 {"st_uid", "user ID of owner"},
1126 {"st_gid", "group ID of owner"},
1127 {"st_size", "total size, in bytes"},
1128 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1129 {NULL, "integer time of last access"},
1130 {NULL, "integer time of last modification"},
1131 {NULL, "integer time of last change"},
1132 {"st_atime", "time of last access"},
1133 {"st_mtime", "time of last modification"},
1134 {"st_ctime", "time of last change"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001135#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001136 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001137#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001138#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001139 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001140#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001141#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001142 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001143#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001144#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001145 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001146#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001147#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001148 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001149#endif
1150#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001151 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001152#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001153 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001154};
1155
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001156#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001157#define ST_BLKSIZE_IDX 13
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001158#else
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001159#define ST_BLKSIZE_IDX 12
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001160#endif
1161
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001162#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001163#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1164#else
1165#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1166#endif
1167
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001168#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001169#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1170#else
1171#define ST_RDEV_IDX ST_BLOCKS_IDX
1172#endif
1173
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001174#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1175#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1176#else
1177#define ST_FLAGS_IDX ST_RDEV_IDX
1178#endif
1179
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001180#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001181#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001182#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001183#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001184#endif
1185
1186#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1187#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1188#else
1189#define ST_BIRTHTIME_IDX ST_GEN_IDX
1190#endif
1191
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001192static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001193 "stat_result", /* name */
1194 stat_result__doc__, /* doc */
1195 stat_result_fields,
1196 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001197};
1198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001199PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001200"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1201This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001202 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001203or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001204\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001205See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001206
1207static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001208 {"f_bsize", },
1209 {"f_frsize", },
1210 {"f_blocks", },
1211 {"f_bfree", },
1212 {"f_bavail", },
1213 {"f_files", },
1214 {"f_ffree", },
1215 {"f_favail", },
1216 {"f_flag", },
1217 {"f_namemax",},
1218 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001219};
1220
1221static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001222 "statvfs_result", /* name */
1223 statvfs_result__doc__, /* doc */
1224 statvfs_result_fields,
1225 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001226};
1227
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001229static PyTypeObject StatResultType;
1230static PyTypeObject StatVFSResultType;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001231static newfunc structseq_new;
1232
1233static PyObject *
1234statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1235{
Victor Stinner8c62be82010-05-06 00:08:46 +00001236 PyStructSequence *result;
1237 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001238
Victor Stinner8c62be82010-05-06 00:08:46 +00001239 result = (PyStructSequence*)structseq_new(type, args, kwds);
1240 if (!result)
1241 return NULL;
1242 /* If we have been initialized from a tuple,
1243 st_?time might be set to None. Initialize it
1244 from the int slots. */
1245 for (i = 7; i <= 9; i++) {
1246 if (result->ob_item[i+3] == Py_None) {
1247 Py_DECREF(Py_None);
1248 Py_INCREF(result->ob_item[i]);
1249 result->ob_item[i+3] = result->ob_item[i];
1250 }
1251 }
1252 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001253}
1254
1255
1256
1257/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001258static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001259
1260PyDoc_STRVAR(stat_float_times__doc__,
1261"stat_float_times([newval]) -> oldval\n\n\
1262Determine whether os.[lf]stat represents time stamps as float objects.\n\
1263If newval is True, future calls to stat() return floats, if it is False,\n\
1264future calls return ints. \n\
1265If newval is omitted, return the current setting.\n");
1266
1267static PyObject*
1268stat_float_times(PyObject* self, PyObject *args)
1269{
Victor Stinner8c62be82010-05-06 00:08:46 +00001270 int newval = -1;
1271 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
1272 return NULL;
1273 if (newval == -1)
1274 /* Return old value */
1275 return PyBool_FromLong(_stat_float_times);
1276 _stat_float_times = newval;
1277 Py_INCREF(Py_None);
1278 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001279}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001280
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001281static void
1282fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
1283{
Victor Stinner8c62be82010-05-06 00:08:46 +00001284 PyObject *fval,*ival;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001285#if SIZEOF_TIME_T > SIZEOF_LONG
Victor Stinner8c62be82010-05-06 00:08:46 +00001286 ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001287#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001288 ival = PyLong_FromLong((long)sec);
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001289#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001290 if (!ival)
1291 return;
1292 if (_stat_float_times) {
1293 fval = PyFloat_FromDouble(sec + 1e-9*nsec);
1294 } else {
1295 fval = ival;
1296 Py_INCREF(fval);
1297 }
1298 PyStructSequence_SET_ITEM(v, index, ival);
1299 PyStructSequence_SET_ITEM(v, index+3, fval);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001300}
1301
Tim Peters5aa91602002-01-30 05:46:57 +00001302/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00001303 (used by posix_stat() and posix_fstat()) */
1304static PyObject*
Martin v. Löwis14694662006-02-03 12:54:16 +00001305_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00001306{
Victor Stinner8c62be82010-05-06 00:08:46 +00001307 unsigned long ansec, mnsec, cnsec;
1308 PyObject *v = PyStructSequence_New(&StatResultType);
1309 if (v == NULL)
1310 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00001311
Victor Stinner8c62be82010-05-06 00:08:46 +00001312 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00001313#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001314 PyStructSequence_SET_ITEM(v, 1,
1315 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001316#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001317 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00001318#endif
1319#if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001320 PyStructSequence_SET_ITEM(v, 2,
1321 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001322#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001323 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00001324#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001325 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
1326 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long)st->st_uid));
1327 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long)st->st_gid));
Fred Drake699f3522000-06-29 21:12:41 +00001328#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00001329 PyStructSequence_SET_ITEM(v, 6,
1330 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001331#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001332 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00001333#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001334
Martin v. Löwis14694662006-02-03 12:54:16 +00001335#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001336 ansec = st->st_atim.tv_nsec;
1337 mnsec = st->st_mtim.tv_nsec;
1338 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001339#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00001340 ansec = st->st_atimespec.tv_nsec;
1341 mnsec = st->st_mtimespec.tv_nsec;
1342 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00001343#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001344 ansec = st->st_atime_nsec;
1345 mnsec = st->st_mtime_nsec;
1346 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001347#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001348 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001349#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001350 fill_time(v, 7, st->st_atime, ansec);
1351 fill_time(v, 8, st->st_mtime, mnsec);
1352 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001353
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001354#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001355 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
1356 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001357#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001358#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001359 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
1360 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001361#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001362#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001363 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
1364 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +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 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
1368 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001369#endif
1370#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001371 {
1372 PyObject *val;
1373 unsigned long bsec,bnsec;
1374 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001375#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner8c62be82010-05-06 00:08:46 +00001376 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001377#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001378 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001379#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001380 if (_stat_float_times) {
1381 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
1382 } else {
1383 val = PyLong_FromLong((long)bsec);
1384 }
1385 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
1386 val);
1387 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001388#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001389#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001390 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
1391 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001392#endif
Fred Drake699f3522000-06-29 21:12:41 +00001393
Victor Stinner8c62be82010-05-06 00:08:46 +00001394 if (PyErr_Occurred()) {
1395 Py_DECREF(v);
1396 return NULL;
1397 }
Fred Drake699f3522000-06-29 21:12:41 +00001398
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 return v;
Fred Drake699f3522000-06-29 21:12:41 +00001400}
1401
Martin v. Löwisd8948722004-06-02 09:57:56 +00001402#ifdef MS_WINDOWS
1403
1404/* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\,
1405 where / can be used in place of \ and the trailing slash is optional.
1406 Both SERVER and SHARE must have at least one character.
1407*/
1408
1409#define ISSLASHA(c) ((c) == '\\' || (c) == '/')
1410#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001411#ifndef ARRAYSIZE
Martin v. Löwisd8948722004-06-02 09:57:56 +00001412#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001413#endif
Martin v. Löwisd8948722004-06-02 09:57:56 +00001414
Tim Peters4ad82172004-08-30 17:02:04 +00001415static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001416IsUNCRootA(char *path, int pathlen)
1417{
Victor Stinner8c62be82010-05-06 00:08:46 +00001418 #define ISSLASH ISSLASHA
Martin v. Löwisd8948722004-06-02 09:57:56 +00001419
Victor Stinner8c62be82010-05-06 00:08:46 +00001420 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001421
Victor Stinner8c62be82010-05-06 00:08:46 +00001422 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1423 /* minimum UNCRoot is \\x\y */
1424 return FALSE;
1425 for (i = 2; i < pathlen ; i++)
1426 if (ISSLASH(path[i])) break;
1427 if (i == 2 || i == pathlen)
1428 /* do not allow \\\SHARE or \\SERVER */
1429 return FALSE;
1430 share = i+1;
1431 for (i = share; i < pathlen; i++)
1432 if (ISSLASH(path[i])) break;
1433 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001434
Victor Stinner8c62be82010-05-06 00:08:46 +00001435 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001436}
1437
Tim Peters4ad82172004-08-30 17:02:04 +00001438static BOOL
Martin v. Löwisd8948722004-06-02 09:57:56 +00001439IsUNCRootW(Py_UNICODE *path, int pathlen)
1440{
Victor Stinner8c62be82010-05-06 00:08:46 +00001441 #define ISSLASH ISSLASHW
Martin v. Löwisd8948722004-06-02 09:57:56 +00001442
Victor Stinner8c62be82010-05-06 00:08:46 +00001443 int i, share;
Martin v. Löwisd8948722004-06-02 09:57:56 +00001444
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1]))
1446 /* minimum UNCRoot is \\x\y */
1447 return FALSE;
1448 for (i = 2; i < pathlen ; i++)
1449 if (ISSLASH(path[i])) break;
1450 if (i == 2 || i == pathlen)
1451 /* do not allow \\\SHARE or \\SERVER */
1452 return FALSE;
1453 share = i+1;
1454 for (i = share; i < pathlen; i++)
1455 if (ISSLASH(path[i])) break;
1456 return (i != share && (i == pathlen || i == pathlen-1));
Martin v. Löwisd8948722004-06-02 09:57:56 +00001457
Victor Stinner8c62be82010-05-06 00:08:46 +00001458 #undef ISSLASH
Martin v. Löwisd8948722004-06-02 09:57:56 +00001459}
Martin v. Löwisd8948722004-06-02 09:57:56 +00001460#endif /* MS_WINDOWS */
1461
Barry Warsaw53699e91996-12-10 23:23:01 +00001462static PyObject *
Tim Peters11b23062003-04-23 02:39:17 +00001463posix_do_stat(PyObject *self, PyObject *args,
Victor Stinner8c62be82010-05-06 00:08:46 +00001464 char *format,
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001465#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00001466 int (*statfunc)(const char *, STRUCT_STAT *, ...),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001467#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001468 int (*statfunc)(const char *, STRUCT_STAT *),
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001469#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001470 char *wformat,
1471 int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001472{
Victor Stinner8c62be82010-05-06 00:08:46 +00001473 STRUCT_STAT st;
1474 PyObject *opath;
1475 char *path;
1476 int res;
1477 PyObject *result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001478
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001479#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001480 PyUnicodeObject *po;
1481 if (PyArg_ParseTuple(args, wformat, &po)) {
1482 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
Martin v. Löwis14694662006-02-03 12:54:16 +00001483
Victor Stinner8c62be82010-05-06 00:08:46 +00001484 Py_BEGIN_ALLOW_THREADS
1485 /* PyUnicode_AS_UNICODE result OK without
1486 thread lock as it is a simple dereference. */
1487 res = wstatfunc(wpath, &st);
1488 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001489
Victor Stinner8c62be82010-05-06 00:08:46 +00001490 if (res != 0)
1491 return win32_error_unicode("stat", wpath);
1492 return _pystat_fromstructstat(&st);
1493 }
1494 /* Drop the argument parsing error as narrow strings
1495 are also valid. */
1496 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001497#endif
1498
Victor Stinner8c62be82010-05-06 00:08:46 +00001499 if (!PyArg_ParseTuple(args, format,
1500 PyUnicode_FSConverter, &opath))
1501 return NULL;
1502 path = PyBytes_AsString(opath);
1503 Py_BEGIN_ALLOW_THREADS
1504 res = (*statfunc)(path, &st);
1505 Py_END_ALLOW_THREADS
Martin v. Löwis14694662006-02-03 12:54:16 +00001506
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00001508#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001509 result = win32_error("stat", path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001510#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001511 result = posix_error_with_filename(path);
Martin v. Löwis14694662006-02-03 12:54:16 +00001512#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001513 }
1514 else
1515 result = _pystat_fromstructstat(&st);
Fred Drake699f3522000-06-29 21:12:41 +00001516
Victor Stinner8c62be82010-05-06 00:08:46 +00001517 Py_DECREF(opath);
1518 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519}
1520
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001521/* POSIX methods */
1522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001523PyDoc_STRVAR(posix_access__doc__,
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001524"access(path, mode) -> True if granted, False otherwise\n\n\
Guido van Rossuma0b90752002-06-18 16:22:43 +00001525Use the real uid/gid to test for access to a path. Note that most\n\
1526operations will use the effective uid/gid, therefore this routine can\n\
1527be used in a suid/sgid environment to test if the invoking user has the\n\
1528specified access to the path. The mode argument can be F_OK to test\n\
1529existence, or the inclusive-OR of R_OK, W_OK, and X_OK.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001530
1531static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001532posix_access(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001533{
Victor Stinner8c62be82010-05-06 00:08:46 +00001534 PyObject *opath;
1535 char *path;
1536 int mode;
1537
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001538#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001539 DWORD attr;
1540 PyUnicodeObject *po;
1541 if (PyArg_ParseTuple(args, "Ui:access", &po, &mode)) {
1542 Py_BEGIN_ALLOW_THREADS
1543 /* PyUnicode_AS_UNICODE OK without thread lock as
1544 it is a simple dereference. */
1545 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1546 Py_END_ALLOW_THREADS
1547 goto finish;
1548 }
1549 /* Drop the argument parsing error as narrow strings
1550 are also valid. */
1551 PyErr_Clear();
1552 if (!PyArg_ParseTuple(args, "O&i:access",
1553 PyUnicode_FSConverter, &opath, &mode))
1554 return NULL;
1555 path = PyBytes_AsString(opath);
1556 Py_BEGIN_ALLOW_THREADS
1557 attr = GetFileAttributesA(path);
1558 Py_END_ALLOW_THREADS
1559 Py_DECREF(opath);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001560finish:
Victor Stinner8c62be82010-05-06 00:08:46 +00001561 if (attr == 0xFFFFFFFF)
1562 /* File does not exist, or cannot read attributes */
1563 return PyBool_FromLong(0);
1564 /* Access is possible if either write access wasn't requested, or
1565 the file isn't read-only, or if it's a directory, as there are
1566 no read-only directories on Windows. */
1567 return PyBool_FromLong(!(mode & 2)
1568 || !(attr & FILE_ATTRIBUTE_READONLY)
1569 || (attr & FILE_ATTRIBUTE_DIRECTORY));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001570#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001571 int res;
1572 if (!PyArg_ParseTuple(args, "O&i:access",
1573 PyUnicode_FSConverter, &opath, &mode))
1574 return NULL;
1575 path = PyBytes_AsString(opath);
1576 Py_BEGIN_ALLOW_THREADS
1577 res = access(path, mode);
1578 Py_END_ALLOW_THREADS
1579 Py_DECREF(opath);
1580 return PyBool_FromLong(res == 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001581#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001582}
1583
Guido van Rossumd371ff11999-01-25 16:12:23 +00001584#ifndef F_OK
1585#define F_OK 0
1586#endif
1587#ifndef R_OK
1588#define R_OK 4
1589#endif
1590#ifndef W_OK
1591#define W_OK 2
1592#endif
1593#ifndef X_OK
1594#define X_OK 1
1595#endif
1596
1597#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001599"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00001601
1602static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001603posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00001604{
Victor Stinner8c62be82010-05-06 00:08:46 +00001605 int id;
1606 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001607
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
1609 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00001610
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001611#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001612 /* file descriptor 0 only, the default input device (stdin) */
1613 if (id == 0) {
1614 ret = ttyname();
1615 }
1616 else {
1617 ret = NULL;
1618 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001619#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001620 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001621#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 if (ret == NULL)
1623 return posix_error();
1624 return PyUnicode_FromString(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00001625}
Guido van Rossumd371ff11999-01-25 16:12:23 +00001626#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00001627
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001628#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001630"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001631Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001632
1633static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00001634posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001635{
Victor Stinner8c62be82010-05-06 00:08:46 +00001636 char *ret;
1637 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001638
Greg Wardb48bc172000-03-01 21:51:56 +00001639#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00001640 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001641#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001642 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001643#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001644 if (ret == NULL)
1645 return posix_error();
1646 return PyUnicode_FromString(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00001647}
1648#endif
1649
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001651"chdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652Change the current working directory to the specified path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001653
Barry Warsaw53699e91996-12-10 23:23:01 +00001654static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001655posix_chdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001656{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001657#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001658 return win32_1str(args, "chdir", "y:chdir", win32_chdir, "U:chdir", win32_wchdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001659#elif defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00001660 return posix_1str(args, "O&:chdir", _chdir2);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00001661#elif defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 return posix_1str(args, "O&:chdir", (int (*)(const char *))chdir);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001663#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001664 return posix_1str(args, "O&:chdir", chdir);
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00001665#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001666}
1667
Fred Drake4d1e64b2002-04-15 19:40:07 +00001668#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001669PyDoc_STRVAR(posix_fchdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001670"fchdir(fildes)\n\n\
Fred Drake4d1e64b2002-04-15 19:40:07 +00001671Change to the directory of the given file descriptor. fildes must be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001672opened on a directory, not a file.");
Fred Drake4d1e64b2002-04-15 19:40:07 +00001673
1674static PyObject *
1675posix_fchdir(PyObject *self, PyObject *fdobj)
1676{
Victor Stinner8c62be82010-05-06 00:08:46 +00001677 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00001678}
1679#endif /* HAVE_FCHDIR */
1680
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001682PyDoc_STRVAR(posix_chmod__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001683"chmod(path, mode)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001684Change the access permissions of a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001685
Barry Warsaw53699e91996-12-10 23:23:01 +00001686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001687posix_chmod(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001688{
Victor Stinner8c62be82010-05-06 00:08:46 +00001689 PyObject *opath = NULL;
1690 char *path = NULL;
1691 int i;
1692 int res;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001693#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001694 DWORD attr;
1695 PyUnicodeObject *po;
1696 if (PyArg_ParseTuple(args, "Ui|:chmod", &po, &i)) {
1697 Py_BEGIN_ALLOW_THREADS
1698 attr = GetFileAttributesW(PyUnicode_AS_UNICODE(po));
1699 if (attr != 0xFFFFFFFF) {
1700 if (i & _S_IWRITE)
1701 attr &= ~FILE_ATTRIBUTE_READONLY;
1702 else
1703 attr |= FILE_ATTRIBUTE_READONLY;
1704 res = SetFileAttributesW(PyUnicode_AS_UNICODE(po), attr);
1705 }
1706 else
1707 res = 0;
1708 Py_END_ALLOW_THREADS
1709 if (!res)
1710 return win32_error_unicode("chmod",
1711 PyUnicode_AS_UNICODE(po));
1712 Py_INCREF(Py_None);
1713 return Py_None;
1714 }
1715 /* Drop the argument parsing error as narrow strings
1716 are also valid. */
1717 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00001718
Victor Stinner8c62be82010-05-06 00:08:46 +00001719 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1720 &opath, &i))
1721 return NULL;
1722 path = PyBytes_AsString(opath);
1723 Py_BEGIN_ALLOW_THREADS
1724 attr = GetFileAttributesA(path);
1725 if (attr != 0xFFFFFFFF) {
1726 if (i & _S_IWRITE)
1727 attr &= ~FILE_ATTRIBUTE_READONLY;
1728 else
1729 attr |= FILE_ATTRIBUTE_READONLY;
1730 res = SetFileAttributesA(path, attr);
1731 }
1732 else
1733 res = 0;
1734 Py_END_ALLOW_THREADS
1735 if (!res) {
1736 win32_error("chmod", path);
1737 Py_DECREF(opath);
1738 return NULL;
1739 }
1740 Py_DECREF(opath);
1741 Py_INCREF(Py_None);
1742 return Py_None;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001743#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00001744 if (!PyArg_ParseTuple(args, "O&i:chmod", PyUnicode_FSConverter,
1745 &opath, &i))
1746 return NULL;
1747 path = PyBytes_AsString(opath);
1748 Py_BEGIN_ALLOW_THREADS
1749 res = chmod(path, i);
1750 Py_END_ALLOW_THREADS
1751 if (res < 0)
1752 return posix_error_with_allocated_filename(opath);
1753 Py_DECREF(opath);
1754 Py_INCREF(Py_None);
1755 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001756#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001757}
1758
Christian Heimes4e30a842007-11-30 22:12:06 +00001759#ifdef HAVE_FCHMOD
1760PyDoc_STRVAR(posix_fchmod__doc__,
1761"fchmod(fd, mode)\n\n\
1762Change the access permissions of the file given by file\n\
1763descriptor fd.");
1764
1765static PyObject *
1766posix_fchmod(PyObject *self, PyObject *args)
1767{
Victor Stinner8c62be82010-05-06 00:08:46 +00001768 int fd, mode, res;
1769 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
1770 return NULL;
1771 Py_BEGIN_ALLOW_THREADS
1772 res = fchmod(fd, mode);
1773 Py_END_ALLOW_THREADS
1774 if (res < 0)
1775 return posix_error();
1776 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001777}
1778#endif /* HAVE_FCHMOD */
1779
1780#ifdef HAVE_LCHMOD
1781PyDoc_STRVAR(posix_lchmod__doc__,
1782"lchmod(path, mode)\n\n\
1783Change the access permissions of a file. If path is a symlink, this\n\
1784affects the link itself rather than the target.");
1785
1786static PyObject *
1787posix_lchmod(PyObject *self, PyObject *args)
1788{
Victor Stinner8c62be82010-05-06 00:08:46 +00001789 PyObject *opath;
1790 char *path;
1791 int i;
1792 int res;
1793 if (!PyArg_ParseTuple(args, "O&i:lchmod", PyUnicode_FSConverter,
1794 &opath, &i))
1795 return NULL;
1796 path = PyBytes_AsString(opath);
1797 Py_BEGIN_ALLOW_THREADS
1798 res = lchmod(path, i);
1799 Py_END_ALLOW_THREADS
1800 if (res < 0)
1801 return posix_error_with_allocated_filename(opath);
1802 Py_DECREF(opath);
1803 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001804}
1805#endif /* HAVE_LCHMOD */
1806
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001807
Thomas Wouterscf297e42007-02-23 15:07:44 +00001808#ifdef HAVE_CHFLAGS
1809PyDoc_STRVAR(posix_chflags__doc__,
1810"chflags(path, flags)\n\n\
1811Set file flags.");
1812
1813static PyObject *
1814posix_chflags(PyObject *self, PyObject *args)
1815{
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 PyObject *opath;
1817 char *path;
1818 unsigned long flags;
1819 int res;
1820 if (!PyArg_ParseTuple(args, "O&k:chflags",
1821 PyUnicode_FSConverter, &opath, &flags))
1822 return NULL;
1823 path = PyBytes_AsString(opath);
1824 Py_BEGIN_ALLOW_THREADS
1825 res = chflags(path, flags);
1826 Py_END_ALLOW_THREADS
1827 if (res < 0)
1828 return posix_error_with_allocated_filename(opath);
1829 Py_DECREF(opath);
1830 Py_INCREF(Py_None);
1831 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001832}
1833#endif /* HAVE_CHFLAGS */
1834
1835#ifdef HAVE_LCHFLAGS
1836PyDoc_STRVAR(posix_lchflags__doc__,
1837"lchflags(path, flags)\n\n\
1838Set file flags.\n\
1839This function will not follow symbolic links.");
1840
1841static PyObject *
1842posix_lchflags(PyObject *self, PyObject *args)
1843{
Victor Stinner8c62be82010-05-06 00:08:46 +00001844 PyObject *opath;
1845 char *path;
1846 unsigned long flags;
1847 int res;
1848 if (!PyArg_ParseTuple(args, "O&k:lchflags",
1849 PyUnicode_FSConverter, &opath, &flags))
1850 return NULL;
1851 path = PyBytes_AsString(opath);
1852 Py_BEGIN_ALLOW_THREADS
1853 res = lchflags(path, flags);
1854 Py_END_ALLOW_THREADS
1855 if (res < 0)
1856 return posix_error_with_allocated_filename(opath);
1857 Py_DECREF(opath);
1858 Py_INCREF(Py_None);
1859 return Py_None;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001860}
1861#endif /* HAVE_LCHFLAGS */
1862
Martin v. Löwis244edc82001-10-04 22:44:26 +00001863#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001864PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001865"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001866Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00001867
1868static PyObject *
1869posix_chroot(PyObject *self, PyObject *args)
1870{
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 return posix_1str(args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00001872}
1873#endif
1874
Guido van Rossum21142a01999-01-08 21:05:37 +00001875#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001877"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001879
1880static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001881posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001882{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001883 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001884}
1885#endif /* HAVE_FSYNC */
1886
1887#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00001888
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00001889#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00001890extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
1891#endif
1892
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001893PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001894"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00001895force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001896 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00001897
1898static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001899posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00001900{
Fred Drake4d1e64b2002-04-15 19:40:07 +00001901 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00001902}
1903#endif /* HAVE_FDATASYNC */
1904
1905
Fredrik Lundh10723342000-07-10 16:38:09 +00001906#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907PyDoc_STRVAR(posix_chown__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00001908"chown(path, uid, gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001909Change the owner and group id of path to the numeric uid and gid.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001910
Barry Warsaw53699e91996-12-10 23:23:01 +00001911static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00001912posix_chown(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001913{
Victor Stinner8c62be82010-05-06 00:08:46 +00001914 PyObject *opath;
1915 char *path;
1916 long uid, gid;
1917 int res;
1918 if (!PyArg_ParseTuple(args, "O&ll:chown",
1919 PyUnicode_FSConverter, &opath,
1920 &uid, &gid))
1921 return NULL;
1922 path = PyBytes_AsString(opath);
1923 Py_BEGIN_ALLOW_THREADS
1924 res = chown(path, (uid_t) uid, (gid_t) gid);
1925 Py_END_ALLOW_THREADS
1926 if (res < 0)
1927 return posix_error_with_allocated_filename(opath);
1928 Py_DECREF(opath);
1929 Py_INCREF(Py_None);
1930 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001931}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001932#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001933
Christian Heimes4e30a842007-11-30 22:12:06 +00001934#ifdef HAVE_FCHOWN
1935PyDoc_STRVAR(posix_fchown__doc__,
1936"fchown(fd, uid, gid)\n\n\
1937Change the owner and group id of the file given by file descriptor\n\
1938fd to the numeric uid and gid.");
1939
1940static PyObject *
1941posix_fchown(PyObject *self, PyObject *args)
1942{
Victor Stinner8c62be82010-05-06 00:08:46 +00001943 int fd;
1944 long uid, gid;
1945 int res;
1946 if (!PyArg_ParseTuple(args, "ill:chown", &fd, &uid, &gid))
1947 return NULL;
1948 Py_BEGIN_ALLOW_THREADS
1949 res = fchown(fd, (uid_t) uid, (gid_t) gid);
1950 Py_END_ALLOW_THREADS
1951 if (res < 0)
1952 return posix_error();
1953 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00001954}
1955#endif /* HAVE_FCHOWN */
1956
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001957#ifdef HAVE_LCHOWN
1958PyDoc_STRVAR(posix_lchown__doc__,
1959"lchown(path, uid, gid)\n\n\
1960Change the owner and group id of path to the numeric uid and gid.\n\
1961This function will not follow symbolic links.");
1962
1963static PyObject *
1964posix_lchown(PyObject *self, PyObject *args)
1965{
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 PyObject *opath;
1967 char *path;
1968 long uid, gid;
1969 int res;
1970 if (!PyArg_ParseTuple(args, "O&ll:lchown",
1971 PyUnicode_FSConverter, &opath,
1972 &uid, &gid))
1973 return NULL;
1974 path = PyBytes_AsString(opath);
1975 Py_BEGIN_ALLOW_THREADS
1976 res = lchown(path, (uid_t) uid, (gid_t) gid);
1977 Py_END_ALLOW_THREADS
1978 if (res < 0)
1979 return posix_error_with_allocated_filename(opath);
1980 Py_DECREF(opath);
1981 Py_INCREF(Py_None);
1982 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00001983}
1984#endif /* HAVE_LCHOWN */
1985
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001986
Guido van Rossum36bc6801995-06-14 22:54:23 +00001987#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00001988static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00001989posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001990{
Victor Stinner8c62be82010-05-06 00:08:46 +00001991 char buf[1026];
1992 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001993
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001994#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001995 if (!use_bytes) {
1996 wchar_t wbuf[1026];
1997 wchar_t *wbuf2 = wbuf;
1998 PyObject *resobj;
1999 DWORD len;
2000 Py_BEGIN_ALLOW_THREADS
2001 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2002 /* If the buffer is large enough, len does not include the
2003 terminating \0. If the buffer is too small, len includes
2004 the space needed for the terminator. */
2005 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2006 wbuf2 = malloc(len * sizeof(wchar_t));
2007 if (wbuf2)
2008 len = GetCurrentDirectoryW(len, wbuf2);
2009 }
2010 Py_END_ALLOW_THREADS
2011 if (!wbuf2) {
2012 PyErr_NoMemory();
2013 return NULL;
2014 }
2015 if (!len) {
2016 if (wbuf2 != wbuf) free(wbuf2);
2017 return win32_error("getcwdu", NULL);
2018 }
2019 resobj = PyUnicode_FromWideChar(wbuf2, len);
2020 if (wbuf2 != wbuf) free(wbuf2);
2021 return resobj;
2022 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002023#endif
2024
Victor Stinner8c62be82010-05-06 00:08:46 +00002025 Py_BEGIN_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002026#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002027 res = _getcwd2(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002028#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002029 res = getcwd(buf, sizeof buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002030#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002031 Py_END_ALLOW_THREADS
2032 if (res == NULL)
2033 return posix_error();
2034 if (use_bytes)
2035 return PyBytes_FromStringAndSize(buf, strlen(buf));
2036 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding,"surrogateescape");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002037}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00002038
2039PyDoc_STRVAR(posix_getcwd__doc__,
2040"getcwd() -> path\n\n\
2041Return a unicode string representing the current working directory.");
2042
2043static PyObject *
2044posix_getcwd_unicode(PyObject *self)
2045{
2046 return posix_getcwd(0);
2047}
2048
2049PyDoc_STRVAR(posix_getcwdb__doc__,
2050"getcwdb() -> path\n\n\
2051Return a bytes string representing the current working directory.");
2052
2053static PyObject *
2054posix_getcwd_bytes(PyObject *self)
2055{
2056 return posix_getcwd(1);
2057}
Guido van Rossum36bc6801995-06-14 22:54:23 +00002058#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002059
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002060
Guido van Rossumb6775db1994-08-01 11:34:53 +00002061#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002062PyDoc_STRVAR(posix_link__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002063"link(src, dst)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002064Create a hard link to a file.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002065
Barry Warsaw53699e91996-12-10 23:23:01 +00002066static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002067posix_link(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002068{
Victor Stinner8c62be82010-05-06 00:08:46 +00002069 return posix_2str(args, "O&O&:link", link);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002070}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002071#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002072
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002073
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002074PyDoc_STRVAR(posix_listdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002075"listdir(path) -> list_of_strings\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002076Return a list containing the names of the entries in the directory.\n\
2077\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002078 path: path of directory to list\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002079\n\
2080The list is in arbitrary order. It does not include the special\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002081entries '.' and '..' even if they are present in the directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002082
Barry Warsaw53699e91996-12-10 23:23:01 +00002083static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002084posix_listdir(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085{
Victor Stinner8c62be82010-05-06 00:08:46 +00002086 /* XXX Should redo this putting the (now four) versions of opendir
2087 in separate files instead of having them all here... */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002088#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002089
Victor Stinner8c62be82010-05-06 00:08:46 +00002090 PyObject *d, *v;
2091 HANDLE hFindFile;
2092 BOOL result;
2093 WIN32_FIND_DATA FileData;
2094 PyObject *opath;
2095 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
2096 char *bufptr = namebuf;
2097 Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002098
Victor Stinner8c62be82010-05-06 00:08:46 +00002099 PyObject *po;
2100 if (PyArg_ParseTuple(args, "U:listdir", &po)) {
2101 WIN32_FIND_DATAW wFileData;
2102 Py_UNICODE *wnamebuf;
2103 /* Overallocate for \\*.*\0 */
2104 len = PyUnicode_GET_SIZE(po);
2105 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
2106 if (!wnamebuf) {
2107 PyErr_NoMemory();
2108 return NULL;
2109 }
2110 wcscpy(wnamebuf, PyUnicode_AS_UNICODE(po));
2111 if (len > 0) {
2112 Py_UNICODE wch = wnamebuf[len-1];
2113 if (wch != L'/' && wch != L'\\' && wch != L':')
2114 wnamebuf[len++] = L'\\';
2115 wcscpy(wnamebuf + len, L"*.*");
2116 }
2117 if ((d = PyList_New(0)) == NULL) {
2118 free(wnamebuf);
2119 return NULL;
2120 }
2121 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
2122 if (hFindFile == INVALID_HANDLE_VALUE) {
2123 int error = GetLastError();
2124 if (error == ERROR_FILE_NOT_FOUND) {
2125 free(wnamebuf);
2126 return d;
2127 }
2128 Py_DECREF(d);
2129 win32_error_unicode("FindFirstFileW", wnamebuf);
2130 free(wnamebuf);
2131 return NULL;
2132 }
2133 do {
2134 /* Skip over . and .. */
2135 if (wcscmp(wFileData.cFileName, L".") != 0 &&
2136 wcscmp(wFileData.cFileName, L"..") != 0) {
2137 v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName));
2138 if (v == NULL) {
2139 Py_DECREF(d);
2140 d = NULL;
2141 break;
2142 }
2143 if (PyList_Append(d, v) != 0) {
2144 Py_DECREF(v);
2145 Py_DECREF(d);
2146 d = NULL;
2147 break;
2148 }
2149 Py_DECREF(v);
2150 }
2151 Py_BEGIN_ALLOW_THREADS
2152 result = FindNextFileW(hFindFile, &wFileData);
2153 Py_END_ALLOW_THREADS
2154 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2155 it got to the end of the directory. */
2156 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2157 Py_DECREF(d);
2158 win32_error_unicode("FindNextFileW", wnamebuf);
2159 FindClose(hFindFile);
2160 free(wnamebuf);
2161 return NULL;
2162 }
2163 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002164
Victor Stinner8c62be82010-05-06 00:08:46 +00002165 if (FindClose(hFindFile) == FALSE) {
2166 Py_DECREF(d);
2167 win32_error_unicode("FindClose", wnamebuf);
2168 free(wnamebuf);
2169 return NULL;
2170 }
2171 free(wnamebuf);
2172 return d;
2173 }
2174 /* Drop the argument parsing error as narrow strings
2175 are also valid. */
2176 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002177
Victor Stinner8c62be82010-05-06 00:08:46 +00002178 if (!PyArg_ParseTuple(args, "O&:listdir",
2179 PyUnicode_FSConverter, &opath))
2180 return NULL;
2181 if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) {
2182 PyErr_SetString(PyExc_ValueError, "path too long");
2183 Py_DECREF(opath);
2184 return NULL;
2185 }
2186 strcpy(namebuf, PyBytes_AsString(opath));
2187 len = PyObject_Size(opath);
2188 if (len > 0) {
2189 char ch = namebuf[len-1];
2190 if (ch != SEP && ch != ALTSEP && ch != ':')
2191 namebuf[len++] = '/';
2192 strcpy(namebuf + len, "*.*");
2193 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002194
Victor Stinner8c62be82010-05-06 00:08:46 +00002195 if ((d = PyList_New(0)) == NULL)
2196 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00002197
Victor Stinner8c62be82010-05-06 00:08:46 +00002198 hFindFile = FindFirstFile(namebuf, &FileData);
2199 if (hFindFile == INVALID_HANDLE_VALUE) {
2200 int error = GetLastError();
2201 if (error == ERROR_FILE_NOT_FOUND)
2202 return d;
2203 Py_DECREF(d);
2204 return win32_error("FindFirstFile", namebuf);
2205 }
2206 do {
2207 /* Skip over . and .. */
2208 if (strcmp(FileData.cFileName, ".") != 0 &&
2209 strcmp(FileData.cFileName, "..") != 0) {
2210 v = PyBytes_FromString(FileData.cFileName);
2211 if (v == NULL) {
2212 Py_DECREF(d);
2213 d = NULL;
2214 break;
2215 }
2216 if (PyList_Append(d, v) != 0) {
2217 Py_DECREF(v);
2218 Py_DECREF(d);
2219 d = NULL;
2220 break;
2221 }
2222 Py_DECREF(v);
2223 }
2224 Py_BEGIN_ALLOW_THREADS
2225 result = FindNextFile(hFindFile, &FileData);
2226 Py_END_ALLOW_THREADS
2227 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
2228 it got to the end of the directory. */
2229 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
2230 Py_DECREF(d);
2231 win32_error("FindNextFile", namebuf);
2232 FindClose(hFindFile);
2233 return NULL;
2234 }
2235 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002236
Victor Stinner8c62be82010-05-06 00:08:46 +00002237 if (FindClose(hFindFile) == FALSE) {
2238 Py_DECREF(d);
2239 return win32_error("FindClose", namebuf);
2240 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00002241
Victor Stinner8c62be82010-05-06 00:08:46 +00002242 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002243
Tim Peters0bb44a42000-09-15 07:44:49 +00002244#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002245
2246#ifndef MAX_PATH
2247#define MAX_PATH CCHMAXPATH
2248#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00002249 PyObject *oname;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002250 char *name, *pt;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00002251 Py_ssize_t len;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002252 PyObject *d, *v;
2253 char namebuf[MAX_PATH+5];
2254 HDIR hdir = 1;
2255 ULONG srchcnt = 1;
2256 FILEFINDBUF3 ep;
2257 APIRET rc;
2258
Victor Stinner8c62be82010-05-06 00:08:46 +00002259 if (!PyArg_ParseTuple(args, "O&:listdir",
Martin v. Löwis011e8422009-05-05 04:43:17 +00002260 PyUnicode_FSConverter, &oname))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002261 return NULL;
Victor Stinnerdcb24032010-04-22 12:08:36 +00002262 name = PyBytes_AsString(oname);
2263 len = PyBytes_GET_SIZE(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002264 if (len >= MAX_PATH) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002265 Py_DECREF(oname);
Neal Norwitz6c913782007-10-14 03:23:09 +00002266 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002267 return NULL;
2268 }
2269 strcpy(namebuf, name);
2270 for (pt = namebuf; *pt; pt++)
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002271 if (*pt == ALTSEP)
2272 *pt = SEP;
2273 if (namebuf[len-1] != SEP)
2274 namebuf[len++] = SEP;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002275 strcpy(namebuf + len, "*.*");
2276
Neal Norwitz6c913782007-10-14 03:23:09 +00002277 if ((d = PyList_New(0)) == NULL) {
Victor Stinnerdcb24032010-04-22 12:08:36 +00002278 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002279 return NULL;
Alexandre Vassalotti4167ebc2007-10-14 02:54:41 +00002280 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002281
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002282 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
2283 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002284 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002285 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
2286 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
2287 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002288
2289 if (rc != NO_ERROR) {
2290 errno = ENOENT;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002291 return posix_error_with_allocated_filename(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002292 }
2293
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002294 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002295 do {
2296 if (ep.achName[0] == '.'
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002297 && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002298 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002299
2300 strcpy(namebuf, ep.achName);
2301
Guido van Rossumc5a0f531997-12-02 20:36:02 +00002302 /* Leave Case of Name Alone -- In Native Form */
2303 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002304
Christian Heimes72b710a2008-05-26 13:28:38 +00002305 v = PyBytes_FromString(namebuf);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002306 if (v == NULL) {
2307 Py_DECREF(d);
2308 d = NULL;
2309 break;
2310 }
2311 if (PyList_Append(d, v) != 0) {
2312 Py_DECREF(v);
2313 Py_DECREF(d);
2314 d = NULL;
2315 break;
2316 }
2317 Py_DECREF(v);
2318 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
2319 }
2320
Victor Stinnerdcb24032010-04-22 12:08:36 +00002321 Py_DECREF(oname);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002322 return d;
2323#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002324 PyObject *oname;
2325 char *name;
2326 PyObject *d, *v;
2327 DIR *dirp;
2328 struct dirent *ep;
2329 int arg_is_unicode = 1;
Just van Rossum96b1c902003-03-03 17:32:15 +00002330
Victor Stinner8c62be82010-05-06 00:08:46 +00002331 errno = 0;
2332 if (!PyArg_ParseTuple(args, "U:listdir", &v)) {
2333 arg_is_unicode = 0;
2334 PyErr_Clear();
2335 }
2336 if (!PyArg_ParseTuple(args, "O&:listdir", PyUnicode_FSConverter, &oname))
2337 return NULL;
2338 name = PyBytes_AsString(oname);
2339 if ((dirp = opendir(name)) == NULL) {
2340 return posix_error_with_allocated_filename(oname);
2341 }
2342 if ((d = PyList_New(0)) == NULL) {
2343 closedir(dirp);
2344 Py_DECREF(oname);
2345 return NULL;
2346 }
2347 for (;;) {
2348 errno = 0;
2349 Py_BEGIN_ALLOW_THREADS
2350 ep = readdir(dirp);
2351 Py_END_ALLOW_THREADS
2352 if (ep == NULL) {
2353 if (errno == 0) {
2354 break;
2355 } else {
2356 closedir(dirp);
2357 Py_DECREF(d);
2358 return posix_error_with_allocated_filename(oname);
2359 }
2360 }
2361 if (ep->d_name[0] == '.' &&
2362 (NAMLEN(ep) == 1 ||
2363 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2364 continue;
2365 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
2366 if (v == NULL) {
2367 Py_DECREF(d);
2368 d = NULL;
2369 break;
2370 }
2371 if (arg_is_unicode) {
2372 PyObject *w;
Just van Rossum46c97842003-02-25 21:42:15 +00002373
Victor Stinner8c62be82010-05-06 00:08:46 +00002374 w = PyUnicode_FromEncodedObject(v,
2375 Py_FileSystemDefaultEncoding,
2376 "surrogateescape");
2377 Py_DECREF(v);
2378 if (w != NULL)
2379 v = w;
2380 else {
2381 /* Encoding failed to decode ASCII bytes.
2382 Raise exception. */
2383 Py_DECREF(d);
2384 d = NULL;
2385 break;
2386 }
2387 }
2388 if (PyList_Append(d, v) != 0) {
2389 Py_DECREF(v);
2390 Py_DECREF(d);
2391 d = NULL;
2392 break;
2393 }
2394 Py_DECREF(v);
2395 }
2396 closedir(dirp);
2397 Py_DECREF(oname);
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00002398
Victor Stinner8c62be82010-05-06 00:08:46 +00002399 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002400
Tim Peters0bb44a42000-09-15 07:44:49 +00002401#endif /* which OS */
2402} /* end of posix_listdir */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002403
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002404#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00002405/* A helper function for abspath on win32 */
2406static PyObject *
2407posix__getfullpathname(PyObject *self, PyObject *args)
2408{
Victor Stinner8c62be82010-05-06 00:08:46 +00002409 PyObject *opath;
2410 char *path;
2411 char outbuf[MAX_PATH*2];
2412 char *temp;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002413#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002414 PyUnicodeObject *po;
2415 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) {
2416 Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po);
2417 Py_UNICODE woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
2418 Py_UNICODE *wtemp;
2419 DWORD result;
2420 PyObject *v;
2421 result = GetFullPathNameW(wpath,
2422 sizeof(woutbuf)/sizeof(woutbuf[0]),
2423 woutbuf, &wtemp);
2424 if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) {
2425 woutbufp = malloc(result * sizeof(Py_UNICODE));
2426 if (!woutbufp)
2427 return PyErr_NoMemory();
2428 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
2429 }
2430 if (result)
2431 v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp));
2432 else
2433 v = win32_error_unicode("GetFullPathNameW", wpath);
2434 if (woutbufp != woutbuf)
2435 free(woutbufp);
2436 return v;
2437 }
2438 /* Drop the argument parsing error as narrow strings
2439 are also valid. */
2440 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002441
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002442#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002443 if (!PyArg_ParseTuple (args, "O&:_getfullpathname",
2444 PyUnicode_FSConverter, &opath))
2445 return NULL;
2446 path = PyBytes_AsString(opath);
2447 if (!GetFullPathName(path, sizeof(outbuf)/sizeof(outbuf[0]),
2448 outbuf, &temp)) {
2449 win32_error("GetFullPathName", path);
2450 Py_DECREF(opath);
2451 return NULL;
2452 }
2453 Py_DECREF(opath);
2454 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
2455 return PyUnicode_Decode(outbuf, strlen(outbuf),
2456 Py_FileSystemDefaultEncoding, NULL);
2457 }
2458 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00002459} /* end of posix__getfullpathname */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002460#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00002461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002462PyDoc_STRVAR(posix_mkdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002463"mkdir(path [, mode=0777])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002464Create a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002465
Barry Warsaw53699e91996-12-10 23:23:01 +00002466static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002467posix_mkdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002468{
Victor Stinner8c62be82010-05-06 00:08:46 +00002469 int res;
2470 PyObject *opath;
2471 char *path;
2472 int mode = 0777;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002473
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002474#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002475 PyUnicodeObject *po;
2476 if (PyArg_ParseTuple(args, "U|i:mkdir", &po, &mode)) {
2477 Py_BEGIN_ALLOW_THREADS
2478 /* PyUnicode_AS_UNICODE OK without thread lock as
2479 it is a simple dereference. */
2480 res = CreateDirectoryW(PyUnicode_AS_UNICODE(po), NULL);
2481 Py_END_ALLOW_THREADS
2482 if (!res)
2483 return win32_error_unicode("mkdir", PyUnicode_AS_UNICODE(po));
2484 Py_INCREF(Py_None);
2485 return Py_None;
2486 }
2487 /* Drop the argument parsing error as narrow strings
2488 are also valid. */
2489 PyErr_Clear();
2490 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2491 PyUnicode_FSConverter, &opath, &mode))
2492 return NULL;
2493 path = PyBytes_AsString(opath);
2494 Py_BEGIN_ALLOW_THREADS
2495 /* PyUnicode_AS_UNICODE OK without thread lock as
2496 it is a simple dereference. */
2497 res = CreateDirectoryA(path, NULL);
2498 Py_END_ALLOW_THREADS
2499 if (!res) {
2500 win32_error("mkdir", path);
2501 Py_DECREF(opath);
2502 return NULL;
2503 }
2504 Py_DECREF(opath);
2505 Py_INCREF(Py_None);
2506 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002507#else
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002508
Victor Stinner8c62be82010-05-06 00:08:46 +00002509 if (!PyArg_ParseTuple(args, "O&|i:mkdir",
2510 PyUnicode_FSConverter, &opath, &mode))
2511 return NULL;
2512 path = PyBytes_AsString(opath);
2513 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +00002514#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Victor Stinner8c62be82010-05-06 00:08:46 +00002515 res = mkdir(path);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002516#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002517 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002518#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002519 Py_END_ALLOW_THREADS
2520 if (res < 0)
2521 return posix_error_with_allocated_filename(opath);
2522 Py_DECREF(opath);
2523 Py_INCREF(Py_None);
2524 return Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002525#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002526}
2527
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002528
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002529/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
2530#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002531#include <sys/resource.h>
2532#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002533
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002534
2535#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002536PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002537"nice(inc) -> new_priority\n\n\
2538Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002539
Barry Warsaw53699e91996-12-10 23:23:01 +00002540static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002541posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00002542{
Victor Stinner8c62be82010-05-06 00:08:46 +00002543 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00002544
Victor Stinner8c62be82010-05-06 00:08:46 +00002545 if (!PyArg_ParseTuple(args, "i:nice", &increment))
2546 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002547
Victor Stinner8c62be82010-05-06 00:08:46 +00002548 /* There are two flavours of 'nice': one that returns the new
2549 priority (as required by almost all standards out there) and the
2550 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
2551 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00002552
Victor Stinner8c62be82010-05-06 00:08:46 +00002553 If we are of the nice family that returns the new priority, we
2554 need to clear errno before the call, and check if errno is filled
2555 before calling posix_error() on a returnvalue of -1, because the
2556 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002557
Victor Stinner8c62be82010-05-06 00:08:46 +00002558 errno = 0;
2559 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00002560#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00002561 if (value == 0)
2562 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00002563#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002564 if (value == -1 && errno != 0)
2565 /* either nice() or getpriority() returned an error */
2566 return posix_error();
2567 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00002568}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002569#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002571PyDoc_STRVAR(posix_rename__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002572"rename(old, new)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002573Rename a file or directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002574
Barry Warsaw53699e91996-12-10 23:23:01 +00002575static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002576posix_rename(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002577{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002578#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002579 PyObject *o1, *o2;
2580 char *p1, *p2;
2581 BOOL result;
2582 if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
2583 goto error;
2584 if (!convert_to_unicode(&o1))
2585 goto error;
2586 if (!convert_to_unicode(&o2)) {
2587 Py_DECREF(o1);
2588 goto error;
2589 }
2590 Py_BEGIN_ALLOW_THREADS
2591 result = MoveFileW(PyUnicode_AsUnicode(o1),
2592 PyUnicode_AsUnicode(o2));
2593 Py_END_ALLOW_THREADS
2594 Py_DECREF(o1);
2595 Py_DECREF(o2);
2596 if (!result)
2597 return win32_error("rename", NULL);
2598 Py_INCREF(Py_None);
2599 return Py_None;
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002600error:
Victor Stinner8c62be82010-05-06 00:08:46 +00002601 PyErr_Clear();
2602 if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2603 return NULL;
2604 Py_BEGIN_ALLOW_THREADS
2605 result = MoveFileA(p1, p2);
2606 Py_END_ALLOW_THREADS
2607 if (!result)
2608 return win32_error("rename", NULL);
2609 Py_INCREF(Py_None);
2610 return Py_None;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002611#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002612 return posix_2str(args, "O&O&:rename", rename);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002613#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002614}
2615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002617PyDoc_STRVAR(posix_rmdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002618"rmdir(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002619Remove a directory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002620
Barry Warsaw53699e91996-12-10 23:23:01 +00002621static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002622posix_rmdir(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002623{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002624#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002625 return win32_1str(args, "rmdir", "y:rmdir", RemoveDirectoryA, "U:rmdir", RemoveDirectoryW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002626#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002627 return posix_1str(args, "O&:rmdir", rmdir);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002628#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002629}
2630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002632PyDoc_STRVAR(posix_stat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002633"stat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002634Perform a stat system call on the given path.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002635
Barry Warsaw53699e91996-12-10 23:23:01 +00002636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002637posix_stat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002638{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002639#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002640 return posix_do_stat(self, args, "O&:stat", STAT, "U:stat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002641#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002642 return posix_do_stat(self, args, "O&:stat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002643#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002644}
2645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002646
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002647#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002648PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002649"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002650Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002651
Barry Warsaw53699e91996-12-10 23:23:01 +00002652static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002653posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002654{
Victor Stinner8c62be82010-05-06 00:08:46 +00002655 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00002656#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002657 wchar_t *command;
2658 if (!PyArg_ParseTuple(args, "u:system", &command))
2659 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002660
Victor Stinner8c62be82010-05-06 00:08:46 +00002661 Py_BEGIN_ALLOW_THREADS
2662 sts = _wsystem(command);
2663 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00002664#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002665 PyObject *command_obj;
2666 char *command;
2667 if (!PyArg_ParseTuple(args, "O&:system",
2668 PyUnicode_FSConverter, &command_obj))
2669 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00002670
Victor Stinner8c62be82010-05-06 00:08:46 +00002671 command = PyBytes_AsString(command_obj);
2672 Py_BEGIN_ALLOW_THREADS
2673 sts = system(command);
2674 Py_END_ALLOW_THREADS
2675 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00002676#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002677 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002678}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002679#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002680
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002682PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002683"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002684Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002685
Barry Warsaw53699e91996-12-10 23:23:01 +00002686static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002687posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002688{
Victor Stinner8c62be82010-05-06 00:08:46 +00002689 int i;
2690 if (!PyArg_ParseTuple(args, "i:umask", &i))
2691 return NULL;
2692 i = (int)umask(i);
2693 if (i < 0)
2694 return posix_error();
2695 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002696}
2697
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002698
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002699PyDoc_STRVAR(posix_unlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002700"unlink(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002701Remove a file (same as remove(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002703PyDoc_STRVAR(posix_remove__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002704"remove(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002705Remove a file (same as unlink(path)).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002706
Barry Warsaw53699e91996-12-10 23:23:01 +00002707static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002708posix_unlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002709{
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002710#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002711 return win32_1str(args, "remove", "y:remove", DeleteFileA, "U:remove", DeleteFileW);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002712#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002713 return posix_1str(args, "O&:remove", unlink);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002714#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002715}
2716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002717
Guido van Rossumb6775db1994-08-01 11:34:53 +00002718#ifdef HAVE_UNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002719PyDoc_STRVAR(posix_uname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002720"uname() -> (sysname, nodename, release, version, machine)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002721Return a tuple identifying the current operating system.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002722
Barry Warsaw53699e91996-12-10 23:23:01 +00002723static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002724posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002725{
Victor Stinner8c62be82010-05-06 00:08:46 +00002726 struct utsname u;
2727 int res;
Neal Norwitze241ce82003-02-17 18:17:05 +00002728
Victor Stinner8c62be82010-05-06 00:08:46 +00002729 Py_BEGIN_ALLOW_THREADS
2730 res = uname(&u);
2731 Py_END_ALLOW_THREADS
2732 if (res < 0)
2733 return posix_error();
2734 return Py_BuildValue("(sssss)",
2735 u.sysname,
2736 u.nodename,
2737 u.release,
2738 u.version,
2739 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00002740}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002741#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002742
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002743static int
2744extract_time(PyObject *t, long* sec, long* usec)
2745{
Victor Stinner8c62be82010-05-06 00:08:46 +00002746 long intval;
2747 if (PyFloat_Check(t)) {
2748 double tval = PyFloat_AsDouble(t);
2749 PyObject *intobj = Py_TYPE(t)->tp_as_number->nb_int(t);
2750 if (!intobj)
2751 return -1;
2752 intval = PyLong_AsLong(intobj);
2753 Py_DECREF(intobj);
2754 if (intval == -1 && PyErr_Occurred())
2755 return -1;
2756 *sec = intval;
2757 *usec = (long)((tval - intval) * 1e6); /* can't exceed 1000000 */
2758 if (*usec < 0)
2759 /* If rounding gave us a negative number,
2760 truncate. */
2761 *usec = 0;
Martin v. Löwis076b2092002-09-10 15:04:41 +00002762 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00002763 }
2764 intval = PyLong_AsLong(t);
2765 if (intval == -1 && PyErr_Occurred())
2766 return -1;
2767 *sec = intval;
2768 *usec = 0;
2769 return 0;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002770}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002771
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002772PyDoc_STRVAR(posix_utime__doc__,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002773"utime(path, (atime, mtime))\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002774utime(path, None)\n\n\
Barry Warsaw3cef8562000-05-01 16:17:24 +00002775Set the access and modified time of the file to the given values. If the\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002776second form is used, set the access and modified times to the current time.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002779posix_utime(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002780{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002781#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002782 PyObject *arg;
2783 PyUnicodeObject *obwpath;
2784 wchar_t *wpath = NULL;
2785 PyObject *oapath;
2786 char *apath;
2787 HANDLE hFile;
2788 long atimesec, mtimesec, ausec, musec;
2789 FILETIME atime, mtime;
2790 PyObject *result = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002791
Victor Stinner8c62be82010-05-06 00:08:46 +00002792 if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
2793 wpath = PyUnicode_AS_UNICODE(obwpath);
2794 Py_BEGIN_ALLOW_THREADS
2795 hFile = CreateFileW(wpath, FILE_WRITE_ATTRIBUTES, 0,
2796 NULL, OPEN_EXISTING,
2797 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2798 Py_END_ALLOW_THREADS
2799 if (hFile == INVALID_HANDLE_VALUE)
2800 return win32_error_unicode("utime", wpath);
2801 } else
2802 /* Drop the argument parsing error as narrow strings
2803 are also valid. */
2804 PyErr_Clear();
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002805
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 if (!wpath) {
2807 if (!PyArg_ParseTuple(args, "O&O:utime",
2808 PyUnicode_FSConverter, &oapath, &arg))
2809 return NULL;
2810 apath = PyBytes_AsString(oapath);
2811 Py_BEGIN_ALLOW_THREADS
2812 hFile = CreateFileA(apath, FILE_WRITE_ATTRIBUTES, 0,
2813 NULL, OPEN_EXISTING,
2814 FILE_FLAG_BACKUP_SEMANTICS, NULL);
2815 Py_END_ALLOW_THREADS
2816 if (hFile == INVALID_HANDLE_VALUE) {
2817 win32_error("utime", apath);
2818 Py_DECREF(oapath);
2819 return NULL;
2820 }
2821 Py_DECREF(oapath);
2822 }
2823
2824 if (arg == Py_None) {
2825 SYSTEMTIME now;
2826 GetSystemTime(&now);
2827 if (!SystemTimeToFileTime(&now, &mtime) ||
2828 !SystemTimeToFileTime(&now, &atime)) {
2829 win32_error("utime", NULL);
2830 goto done;
2831 }
2832 }
2833 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2834 PyErr_SetString(PyExc_TypeError,
2835 "utime() arg 2 must be a tuple (atime, mtime)");
2836 goto done;
2837 }
2838 else {
2839 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2840 &atimesec, &ausec) == -1)
2841 goto done;
2842 time_t_to_FILE_TIME(atimesec, 1000*ausec, &atime);
2843 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2844 &mtimesec, &musec) == -1)
2845 goto done;
2846 time_t_to_FILE_TIME(mtimesec, 1000*musec, &mtime);
2847 }
2848 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
2849 /* Avoid putting the file name into the error here,
2850 as that may confuse the user into believing that
2851 something is wrong with the file, when it also
2852 could be the time stamp that gives a problem. */
2853 win32_error("utime", NULL);
2854 }
2855 Py_INCREF(Py_None);
2856 result = Py_None;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002857done:
Victor Stinner8c62be82010-05-06 00:08:46 +00002858 CloseHandle(hFile);
2859 return result;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002860#else /* MS_WINDOWS */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002861
Victor Stinner8c62be82010-05-06 00:08:46 +00002862 PyObject *opath;
2863 char *path;
2864 long atime, mtime, ausec, musec;
2865 int res;
2866 PyObject* arg;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002867
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002868#if defined(HAVE_UTIMES)
Victor Stinner8c62be82010-05-06 00:08:46 +00002869 struct timeval buf[2];
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002870#define ATIME buf[0].tv_sec
2871#define MTIME buf[1].tv_sec
2872#elif defined(HAVE_UTIME_H)
Guido van Rossum6d8841c1997-08-14 19:57:39 +00002873/* XXX should define struct utimbuf instead, above */
Victor Stinner8c62be82010-05-06 00:08:46 +00002874 struct utimbuf buf;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002875#define ATIME buf.actime
2876#define MTIME buf.modtime
2877#define UTIME_ARG &buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002878#else /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 time_t buf[2];
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002880#define ATIME buf[0]
2881#define MTIME buf[1]
2882#define UTIME_ARG buf
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002883#endif /* HAVE_UTIMES */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002884
Mark Hammond817c9292003-12-03 01:22:38 +00002885
Victor Stinner8c62be82010-05-06 00:08:46 +00002886 if (!PyArg_ParseTuple(args, "O&O:utime",
2887 PyUnicode_FSConverter, &opath, &arg))
2888 return NULL;
2889 path = PyBytes_AsString(opath);
2890 if (arg == Py_None) {
2891 /* optional time values not given */
2892 Py_BEGIN_ALLOW_THREADS
2893 res = utime(path, NULL);
2894 Py_END_ALLOW_THREADS
2895 }
2896 else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
2897 PyErr_SetString(PyExc_TypeError,
2898 "utime() arg 2 must be a tuple (atime, mtime)");
2899 Py_DECREF(opath);
2900 return NULL;
2901 }
2902 else {
2903 if (extract_time(PyTuple_GET_ITEM(arg, 0),
2904 &atime, &ausec) == -1) {
2905 Py_DECREF(opath);
2906 return NULL;
2907 }
2908 if (extract_time(PyTuple_GET_ITEM(arg, 1),
2909 &mtime, &musec) == -1) {
2910 Py_DECREF(opath);
2911 return NULL;
2912 }
2913 ATIME = atime;
2914 MTIME = mtime;
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002915#ifdef HAVE_UTIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00002916 buf[0].tv_usec = ausec;
2917 buf[1].tv_usec = musec;
2918 Py_BEGIN_ALLOW_THREADS
2919 res = utimes(path, buf);
2920 Py_END_ALLOW_THREADS
Martin v. Löwis6aa9fdb2002-09-10 09:16:13 +00002921#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002922 Py_BEGIN_ALLOW_THREADS
2923 res = utime(path, UTIME_ARG);
2924 Py_END_ALLOW_THREADS
Mark Hammond817c9292003-12-03 01:22:38 +00002925#endif /* HAVE_UTIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00002926 }
2927 if (res < 0) {
2928 return posix_error_with_allocated_filename(opath);
2929 }
2930 Py_DECREF(opath);
2931 Py_INCREF(Py_None);
2932 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00002933#undef UTIME_ARG
2934#undef ATIME
2935#undef MTIME
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002936#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002937}
2938
Guido van Rossum85e3b011991-06-03 12:42:10 +00002939
Guido van Rossum3b066191991-06-04 19:40:25 +00002940/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002941
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002942PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002943"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002944Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002945
Barry Warsaw53699e91996-12-10 23:23:01 +00002946static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002947posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002948{
Victor Stinner8c62be82010-05-06 00:08:46 +00002949 int sts;
2950 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
2951 return NULL;
2952 _exit(sts);
2953 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00002954}
2955
Martin v. Löwis114619e2002-10-07 06:44:21 +00002956#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
2957static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00002958free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00002959{
Victor Stinner8c62be82010-05-06 00:08:46 +00002960 Py_ssize_t i;
2961 for (i = 0; i < count; i++)
2962 PyMem_Free(array[i]);
2963 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00002964}
Martin v. Löwis011e8422009-05-05 04:43:17 +00002965
Antoine Pitrou69f71142009-05-24 21:25:49 +00002966static
Martin v. Löwis011e8422009-05-05 04:43:17 +00002967int fsconvert_strdup(PyObject *o, char**out)
2968{
Victor Stinner8c62be82010-05-06 00:08:46 +00002969 PyObject *bytes;
2970 Py_ssize_t size;
2971 if (!PyUnicode_FSConverter(o, &bytes))
2972 return 0;
2973 size = PyBytes_GET_SIZE(bytes);
2974 *out = PyMem_Malloc(size+1);
2975 if (!*out)
2976 return 0;
2977 memcpy(*out, PyBytes_AsString(bytes), size+1);
2978 Py_DECREF(bytes);
2979 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00002980}
Martin v. Löwis114619e2002-10-07 06:44:21 +00002981#endif
2982
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002983
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002984#ifdef HAVE_EXECV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002985PyDoc_STRVAR(posix_execv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002986"execv(path, args)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002987Execute an executable path with arguments, replacing current process.\n\
2988\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00002989 path: path of executable file\n\
2990 args: tuple or list of strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002991
Barry Warsaw53699e91996-12-10 23:23:01 +00002992static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002993posix_execv(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00002994{
Victor Stinner8c62be82010-05-06 00:08:46 +00002995 PyObject *opath;
2996 char *path;
2997 PyObject *argv;
2998 char **argvlist;
2999 Py_ssize_t i, argc;
3000 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003001
Victor Stinner8c62be82010-05-06 00:08:46 +00003002 /* execv has two arguments: (path, argv), where
3003 argv is a list or tuple of strings. */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003004
Victor Stinner8c62be82010-05-06 00:08:46 +00003005 if (!PyArg_ParseTuple(args, "O&O:execv",
3006 PyUnicode_FSConverter,
3007 &opath, &argv))
3008 return NULL;
3009 path = PyBytes_AsString(opath);
3010 if (PyList_Check(argv)) {
3011 argc = PyList_Size(argv);
3012 getitem = PyList_GetItem;
3013 }
3014 else if (PyTuple_Check(argv)) {
3015 argc = PyTuple_Size(argv);
3016 getitem = PyTuple_GetItem;
3017 }
3018 else {
3019 PyErr_SetString(PyExc_TypeError, "execv() arg 2 must be a tuple or list");
3020 Py_DECREF(opath);
3021 return NULL;
3022 }
3023 if (argc < 1) {
3024 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
3025 Py_DECREF(opath);
3026 return NULL;
3027 }
Guido van Rossum50422b42000-04-26 20:34:28 +00003028
Victor Stinner8c62be82010-05-06 00:08:46 +00003029 argvlist = PyMem_NEW(char *, argc+1);
3030 if (argvlist == NULL) {
3031 Py_DECREF(opath);
3032 return PyErr_NoMemory();
3033 }
3034 for (i = 0; i < argc; i++) {
3035 if (!fsconvert_strdup((*getitem)(argv, i),
3036 &argvlist[i])) {
3037 free_string_array(argvlist, i);
3038 PyErr_SetString(PyExc_TypeError,
3039 "execv() arg 2 must contain only strings");
3040 Py_DECREF(opath);
3041 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00003042
Victor Stinner8c62be82010-05-06 00:08:46 +00003043 }
3044 }
3045 argvlist[argc] = NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00003046
Victor Stinner8c62be82010-05-06 00:08:46 +00003047 execv(path, argvlist);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003048
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 /* If we get here it's definitely an error */
Guido van Rossum85e3b011991-06-03 12:42:10 +00003050
Victor Stinner8c62be82010-05-06 00:08:46 +00003051 free_string_array(argvlist, argc);
3052 Py_DECREF(opath);
3053 return posix_error();
Guido van Rossum85e3b011991-06-03 12:42:10 +00003054}
3055
Victor Stinner13bb71c2010-04-23 21:41:56 +00003056static char**
3057parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
3058{
Victor Stinner8c62be82010-05-06 00:08:46 +00003059 char **envlist;
3060 Py_ssize_t i, pos, envc;
3061 PyObject *keys=NULL, *vals=NULL;
3062 PyObject *key, *val, *key2, *val2;
3063 char *p, *k, *v;
3064 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003065
Victor Stinner8c62be82010-05-06 00:08:46 +00003066 i = PyMapping_Size(env);
3067 if (i < 0)
3068 return NULL;
3069 envlist = PyMem_NEW(char *, i + 1);
3070 if (envlist == NULL) {
3071 PyErr_NoMemory();
3072 return NULL;
3073 }
3074 envc = 0;
3075 keys = PyMapping_Keys(env);
3076 vals = PyMapping_Values(env);
3077 if (!keys || !vals)
3078 goto error;
3079 if (!PyList_Check(keys) || !PyList_Check(vals)) {
3080 PyErr_Format(PyExc_TypeError,
3081 "env.keys() or env.values() is not a list");
3082 goto error;
3083 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003084
Victor Stinner8c62be82010-05-06 00:08:46 +00003085 for (pos = 0; pos < i; pos++) {
3086 key = PyList_GetItem(keys, pos);
3087 val = PyList_GetItem(vals, pos);
3088 if (!key || !val)
3089 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003090
Victor Stinner8c62be82010-05-06 00:08:46 +00003091 if (PyUnicode_FSConverter(key, &key2) == 0)
3092 goto error;
3093 if (PyUnicode_FSConverter(val, &val2) == 0) {
3094 Py_DECREF(key2);
3095 goto error;
3096 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003097
3098#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003099 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
3100 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
Victor Stinner13bb71c2010-04-23 21:41:56 +00003101#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003102 k = PyBytes_AsString(key2);
3103 v = PyBytes_AsString(val2);
3104 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003105
Victor Stinner8c62be82010-05-06 00:08:46 +00003106 p = PyMem_NEW(char, len);
3107 if (p == NULL) {
3108 PyErr_NoMemory();
3109 Py_DECREF(key2);
3110 Py_DECREF(val2);
3111 goto error;
3112 }
3113 PyOS_snprintf(p, len, "%s=%s", k, v);
3114 envlist[envc++] = p;
3115 Py_DECREF(key2);
3116 Py_DECREF(val2);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003117#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00003118 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00003119#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003120 }
3121 Py_DECREF(vals);
3122 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00003123
Victor Stinner8c62be82010-05-06 00:08:46 +00003124 envlist[envc] = 0;
3125 *envc_ptr = envc;
3126 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003127
3128error:
Victor Stinner8c62be82010-05-06 00:08:46 +00003129 Py_XDECREF(keys);
3130 Py_XDECREF(vals);
3131 while (--envc >= 0)
3132 PyMem_DEL(envlist[envc]);
3133 PyMem_DEL(envlist);
3134 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00003135}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003137PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003138"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139Execute a path with arguments and environment, replacing current process.\n\
3140\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003141 path: path of executable file\n\
3142 args: tuple or list of arguments\n\
3143 env: dictionary of strings mapping to strings");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003144
Barry Warsaw53699e91996-12-10 23:23:01 +00003145static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003146posix_execve(PyObject *self, PyObject *args)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003147{
Victor Stinner8c62be82010-05-06 00:08:46 +00003148 PyObject *opath;
3149 char *path;
3150 PyObject *argv, *env;
3151 char **argvlist;
3152 char **envlist;
3153 Py_ssize_t i, argc, envc;
3154 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3155 Py_ssize_t lastarg = 0;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003156
Victor Stinner8c62be82010-05-06 00:08:46 +00003157 /* execve has three arguments: (path, argv, env), where
3158 argv is a list or tuple of strings and env is a dictionary
3159 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003160
Victor Stinner8c62be82010-05-06 00:08:46 +00003161 if (!PyArg_ParseTuple(args, "O&OO:execve",
3162 PyUnicode_FSConverter,
3163 &opath, &argv, &env))
3164 return NULL;
3165 path = PyBytes_AsString(opath);
3166 if (PyList_Check(argv)) {
3167 argc = PyList_Size(argv);
3168 getitem = PyList_GetItem;
3169 }
3170 else if (PyTuple_Check(argv)) {
3171 argc = PyTuple_Size(argv);
3172 getitem = PyTuple_GetItem;
3173 }
3174 else {
3175 PyErr_SetString(PyExc_TypeError,
3176 "execve() arg 2 must be a tuple or list");
3177 goto fail_0;
3178 }
3179 if (!PyMapping_Check(env)) {
3180 PyErr_SetString(PyExc_TypeError,
3181 "execve() arg 3 must be a mapping object");
3182 goto fail_0;
3183 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003184
Victor Stinner8c62be82010-05-06 00:08:46 +00003185 argvlist = PyMem_NEW(char *, argc+1);
3186 if (argvlist == NULL) {
3187 PyErr_NoMemory();
3188 goto fail_0;
3189 }
3190 for (i = 0; i < argc; i++) {
3191 if (!fsconvert_strdup((*getitem)(argv, i),
3192 &argvlist[i]))
3193 {
3194 lastarg = i;
3195 goto fail_1;
3196 }
3197 }
3198 lastarg = argc;
3199 argvlist[argc] = NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003200
Victor Stinner8c62be82010-05-06 00:08:46 +00003201 envlist = parse_envlist(env, &envc);
3202 if (envlist == NULL)
3203 goto fail_1;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003204
Victor Stinner8c62be82010-05-06 00:08:46 +00003205 execve(path, argvlist, envlist);
Tim Peters5aa91602002-01-30 05:46:57 +00003206
Victor Stinner8c62be82010-05-06 00:08:46 +00003207 /* If we get here it's definitely an error */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003208
Victor Stinner8c62be82010-05-06 00:08:46 +00003209 (void) posix_error();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003210
Victor Stinner8c62be82010-05-06 00:08:46 +00003211 while (--envc >= 0)
3212 PyMem_DEL(envlist[envc]);
3213 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003214 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003215 free_string_array(argvlist, lastarg);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003216 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003217 Py_DECREF(opath);
3218 return NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003219}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003220#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00003221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222
Guido van Rossuma1065681999-01-25 23:20:23 +00003223#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003224PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003225"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003226Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003227\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003228 mode: mode of process creation\n\
3229 path: path of executable file\n\
3230 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003231
3232static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003233posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003234{
Victor Stinner8c62be82010-05-06 00:08:46 +00003235 PyObject *opath;
3236 char *path;
3237 PyObject *argv;
3238 char **argvlist;
3239 int mode, i;
3240 Py_ssize_t argc;
3241 Py_intptr_t spawnval;
3242 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00003243
Victor Stinner8c62be82010-05-06 00:08:46 +00003244 /* spawnv has three arguments: (mode, path, argv), where
3245 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003246
Victor Stinner8c62be82010-05-06 00:08:46 +00003247 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
3248 PyUnicode_FSConverter,
3249 &opath, &argv))
3250 return NULL;
3251 path = PyBytes_AsString(opath);
3252 if (PyList_Check(argv)) {
3253 argc = PyList_Size(argv);
3254 getitem = PyList_GetItem;
3255 }
3256 else if (PyTuple_Check(argv)) {
3257 argc = PyTuple_Size(argv);
3258 getitem = PyTuple_GetItem;
3259 }
3260 else {
3261 PyErr_SetString(PyExc_TypeError,
3262 "spawnv() arg 2 must be a tuple or list");
3263 Py_DECREF(opath);
3264 return NULL;
3265 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003266
Victor Stinner8c62be82010-05-06 00:08:46 +00003267 argvlist = PyMem_NEW(char *, argc+1);
3268 if (argvlist == NULL) {
3269 Py_DECREF(opath);
3270 return PyErr_NoMemory();
3271 }
3272 for (i = 0; i < argc; i++) {
3273 if (!fsconvert_strdup((*getitem)(argv, i),
3274 &argvlist[i])) {
3275 free_string_array(argvlist, i);
3276 PyErr_SetString(
3277 PyExc_TypeError,
3278 "spawnv() arg 2 must contain only strings");
3279 Py_DECREF(opath);
3280 return NULL;
3281 }
3282 }
3283 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003284
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003285#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003286 Py_BEGIN_ALLOW_THREADS
3287 spawnval = spawnv(mode, path, argvlist);
3288 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003289#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003290 if (mode == _OLD_P_OVERLAY)
3291 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00003292
Victor Stinner8c62be82010-05-06 00:08:46 +00003293 Py_BEGIN_ALLOW_THREADS
3294 spawnval = _spawnv(mode, path, argvlist);
3295 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003296#endif
Tim Peters5aa91602002-01-30 05:46:57 +00003297
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 free_string_array(argvlist, argc);
3299 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00003300
Victor Stinner8c62be82010-05-06 00:08:46 +00003301 if (spawnval == -1)
3302 return posix_error();
3303 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003304#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003305 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003306#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003307 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003308#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003309}
3310
3311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003312PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003313"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00003314Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00003315\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003316 mode: mode of process creation\n\
3317 path: path of executable file\n\
3318 args: tuple or list of arguments\n\
3319 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00003320
3321static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003322posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00003323{
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 PyObject *opath;
3325 char *path;
3326 PyObject *argv, *env;
3327 char **argvlist;
3328 char **envlist;
3329 PyObject *res = NULL;
3330 int mode, envc;
3331 Py_ssize_t argc, i;
3332 Py_intptr_t spawnval;
3333 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3334 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00003335
Victor Stinner8c62be82010-05-06 00:08:46 +00003336 /* spawnve has four arguments: (mode, path, argv, env), where
3337 argv is a list or tuple of strings and env is a dictionary
3338 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00003339
Victor Stinner8c62be82010-05-06 00:08:46 +00003340 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
3341 PyUnicode_FSConverter,
3342 &opath, &argv, &env))
3343 return NULL;
3344 path = PyBytes_AsString(opath);
3345 if (PyList_Check(argv)) {
3346 argc = PyList_Size(argv);
3347 getitem = PyList_GetItem;
3348 }
3349 else if (PyTuple_Check(argv)) {
3350 argc = PyTuple_Size(argv);
3351 getitem = PyTuple_GetItem;
3352 }
3353 else {
3354 PyErr_SetString(PyExc_TypeError,
3355 "spawnve() arg 2 must be a tuple or list");
3356 goto fail_0;
3357 }
3358 if (!PyMapping_Check(env)) {
3359 PyErr_SetString(PyExc_TypeError,
3360 "spawnve() arg 3 must be a mapping object");
3361 goto fail_0;
3362 }
Guido van Rossuma1065681999-01-25 23:20:23 +00003363
Victor Stinner8c62be82010-05-06 00:08:46 +00003364 argvlist = PyMem_NEW(char *, argc+1);
3365 if (argvlist == NULL) {
3366 PyErr_NoMemory();
3367 goto fail_0;
3368 }
3369 for (i = 0; i < argc; i++) {
3370 if (!fsconvert_strdup((*getitem)(argv, i),
3371 &argvlist[i]))
3372 {
3373 lastarg = i;
3374 goto fail_1;
3375 }
3376 }
3377 lastarg = argc;
3378 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00003379
Victor Stinner8c62be82010-05-06 00:08:46 +00003380 envlist = parse_envlist(env, &envc);
3381 if (envlist == NULL)
3382 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00003383
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003384#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003385 Py_BEGIN_ALLOW_THREADS
3386 spawnval = spawnve(mode, path, argvlist, envlist);
3387 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003388#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003389 if (mode == _OLD_P_OVERLAY)
3390 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00003391
Victor Stinner8c62be82010-05-06 00:08:46 +00003392 Py_BEGIN_ALLOW_THREADS
3393 spawnval = _spawnve(mode, path, argvlist, envlist);
3394 Py_END_ALLOW_THREADS
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00003395#endif
Tim Peters25059d32001-12-07 20:35:43 +00003396
Victor Stinner8c62be82010-05-06 00:08:46 +00003397 if (spawnval == -1)
3398 (void) posix_error();
3399 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00003400#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003402#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003403 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00003404#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00003405
Victor Stinner8c62be82010-05-06 00:08:46 +00003406 while (--envc >= 0)
3407 PyMem_DEL(envlist[envc]);
3408 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00003409 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003410 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00003411 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 Py_DECREF(opath);
3413 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00003414}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003415
3416/* OS/2 supports spawnvp & spawnvpe natively */
3417#if defined(PYOS_OS2)
3418PyDoc_STRVAR(posix_spawnvp__doc__,
3419"spawnvp(mode, file, args)\n\n\
3420Execute the program 'file' in a new process, using the environment\n\
3421search path to find the file.\n\
3422\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003423 mode: mode of process creation\n\
3424 file: executable file name\n\
3425 args: tuple or list of strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003426
3427static PyObject *
3428posix_spawnvp(PyObject *self, PyObject *args)
3429{
Victor Stinner8c62be82010-05-06 00:08:46 +00003430 PyObject *opath;
3431 char *path;
3432 PyObject *argv;
3433 char **argvlist;
3434 int mode, i, argc;
3435 Py_intptr_t spawnval;
3436 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003437
Victor Stinner8c62be82010-05-06 00:08:46 +00003438 /* spawnvp has three arguments: (mode, path, argv), where
3439 argv is a list or tuple of strings. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003440
Victor Stinner8c62be82010-05-06 00:08:46 +00003441 if (!PyArg_ParseTuple(args, "iO&O:spawnvp", &mode,
3442 PyUnicode_FSConverter,
3443 &opath, &argv))
3444 return NULL;
3445 path = PyBytes_AsString(opath);
3446 if (PyList_Check(argv)) {
3447 argc = PyList_Size(argv);
3448 getitem = PyList_GetItem;
3449 }
3450 else if (PyTuple_Check(argv)) {
3451 argc = PyTuple_Size(argv);
3452 getitem = PyTuple_GetItem;
3453 }
3454 else {
3455 PyErr_SetString(PyExc_TypeError,
3456 "spawnvp() arg 2 must be a tuple or list");
3457 Py_DECREF(opath);
3458 return NULL;
3459 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003460
Victor Stinner8c62be82010-05-06 00:08:46 +00003461 argvlist = PyMem_NEW(char *, argc+1);
3462 if (argvlist == NULL) {
3463 Py_DECREF(opath);
3464 return PyErr_NoMemory();
3465 }
3466 for (i = 0; i < argc; i++) {
3467 if (!fsconvert_strdup((*getitem)(argv, i),
3468 &argvlist[i])) {
3469 free_string_array(argvlist, i);
3470 PyErr_SetString(
3471 PyExc_TypeError,
3472 "spawnvp() arg 2 must contain only strings");
3473 Py_DECREF(opath);
3474 return NULL;
3475 }
3476 }
3477 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003478
Victor Stinner8c62be82010-05-06 00:08:46 +00003479 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003480#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003481 spawnval = spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003482#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003483 spawnval = _spawnvp(mode, path, argvlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003484#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003485 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003486
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 free_string_array(argvlist, argc);
3488 Py_DECREF(opath);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003489
Victor Stinner8c62be82010-05-06 00:08:46 +00003490 if (spawnval == -1)
3491 return posix_error();
3492 else
3493 return Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003494}
3495
3496
3497PyDoc_STRVAR(posix_spawnvpe__doc__,
3498"spawnvpe(mode, file, args, env)\n\n\
3499Execute the program 'file' in a new process, using the environment\n\
3500search path to find the file.\n\
3501\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00003502 mode: mode of process creation\n\
3503 file: executable file name\n\
3504 args: tuple or list of arguments\n\
3505 env: dictionary of strings mapping to strings");
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003506
3507static PyObject *
3508posix_spawnvpe(PyObject *self, PyObject *args)
3509{
Victor Stinner8c62be82010-05-06 00:08:46 +00003510 PyObject *opath
3511 char *path;
3512 PyObject *argv, *env;
3513 char **argvlist;
3514 char **envlist;
3515 PyObject *res=NULL;
3516 int mode, i, argc, envc;
3517 Py_intptr_t spawnval;
3518 PyObject *(*getitem)(PyObject *, Py_ssize_t);
3519 int lastarg = 0;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003520
Victor Stinner8c62be82010-05-06 00:08:46 +00003521 /* spawnvpe has four arguments: (mode, path, argv, env), where
3522 argv is a list or tuple of strings and env is a dictionary
3523 like posix.environ. */
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003524
Victor Stinner8c62be82010-05-06 00:08:46 +00003525 if (!PyArg_ParseTuple(args, "ietOO:spawnvpe", &mode,
3526 PyUnicode_FSConverter,
3527 &opath, &argv, &env))
3528 return NULL;
3529 path = PyBytes_AsString(opath);
3530 if (PyList_Check(argv)) {
3531 argc = PyList_Size(argv);
3532 getitem = PyList_GetItem;
3533 }
3534 else if (PyTuple_Check(argv)) {
3535 argc = PyTuple_Size(argv);
3536 getitem = PyTuple_GetItem;
3537 }
3538 else {
3539 PyErr_SetString(PyExc_TypeError,
3540 "spawnvpe() arg 2 must be a tuple or list");
3541 goto fail_0;
3542 }
3543 if (!PyMapping_Check(env)) {
3544 PyErr_SetString(PyExc_TypeError,
3545 "spawnvpe() arg 3 must be a mapping object");
3546 goto fail_0;
3547 }
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003548
Victor Stinner8c62be82010-05-06 00:08:46 +00003549 argvlist = PyMem_NEW(char *, argc+1);
3550 if (argvlist == NULL) {
3551 PyErr_NoMemory();
3552 goto fail_0;
3553 }
3554 for (i = 0; i < argc; i++) {
3555 if (!fsconvert_strdup((*getitem)(argv, i),
3556 &argvlist[i]))
3557 {
3558 lastarg = i;
3559 goto fail_1;
3560 }
3561 }
3562 lastarg = argc;
3563 argvlist[argc] = NULL;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003564
Victor Stinner8c62be82010-05-06 00:08:46 +00003565 envlist = parse_envlist(env, &envc);
3566 if (envlist == NULL)
3567 goto fail_1;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003568
Victor Stinner8c62be82010-05-06 00:08:46 +00003569 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003570#if defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003571 spawnval = spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003572#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003573 spawnval = _spawnvpe(mode, path, argvlist, envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003574#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003575 Py_END_ALLOW_THREADS
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003576
Victor Stinner8c62be82010-05-06 00:08:46 +00003577 if (spawnval == -1)
3578 (void) posix_error();
3579 else
3580 res = Py_BuildValue("l", (long) spawnval);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003581
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 while (--envc >= 0)
3583 PyMem_DEL(envlist[envc]);
3584 PyMem_DEL(envlist);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003585 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00003586 free_string_array(argvlist, lastarg);
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003587 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00003588 Py_DECREF(opath);
3589 return res;
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00003590}
3591#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00003592#endif /* HAVE_SPAWNV */
3593
3594
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003595#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003596PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003597"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003598Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
3599\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003600Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003601
3602static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003603posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003604{
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 pid_t pid;
3606 int result = 0;
3607 _PyImport_AcquireLock();
3608 pid = fork1();
3609 if (pid == 0) {
3610 /* child: this clobbers and resets the import lock. */
3611 PyOS_AfterFork();
3612 } else {
3613 /* parent: release the import lock. */
3614 result = _PyImport_ReleaseLock();
3615 }
3616 if (pid == -1)
3617 return posix_error();
3618 if (result < 0) {
3619 /* Don't clobber the OSError if the fork failed. */
3620 PyErr_SetString(PyExc_RuntimeError,
3621 "not holding the import lock");
3622 return NULL;
3623 }
3624 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00003625}
3626#endif
3627
3628
Guido van Rossumad0ee831995-03-01 10:34:45 +00003629#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003630PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003631"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003632Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003633Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003634
Barry Warsaw53699e91996-12-10 23:23:01 +00003635static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003636posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003637{
Victor Stinner8c62be82010-05-06 00:08:46 +00003638 pid_t pid;
3639 int result = 0;
3640 _PyImport_AcquireLock();
3641 pid = fork();
3642 if (pid == 0) {
3643 /* child: this clobbers and resets the import lock. */
3644 PyOS_AfterFork();
3645 } else {
3646 /* parent: release the import lock. */
3647 result = _PyImport_ReleaseLock();
3648 }
3649 if (pid == -1)
3650 return posix_error();
3651 if (result < 0) {
3652 /* Don't clobber the OSError if the fork failed. */
3653 PyErr_SetString(PyExc_RuntimeError,
3654 "not holding the import lock");
3655 return NULL;
3656 }
3657 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00003658}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003659#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003660
Neal Norwitzb59798b2003-03-21 01:43:31 +00003661/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00003662/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
3663#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00003664#define DEV_PTY_FILE "/dev/ptc"
3665#define HAVE_DEV_PTMX
3666#else
3667#define DEV_PTY_FILE "/dev/ptmx"
3668#endif
3669
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003670#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003671#ifdef HAVE_PTY_H
3672#include <pty.h>
3673#else
3674#ifdef HAVE_LIBUTIL_H
3675#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00003676#else
3677#ifdef HAVE_UTIL_H
3678#include <util.h>
3679#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003680#endif /* HAVE_LIBUTIL_H */
3681#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00003682#ifdef HAVE_STROPTS_H
3683#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003684#endif
3685#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003686
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003687#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003688PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003689"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003690Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003691
3692static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003693posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003694{
Victor Stinner8c62be82010-05-06 00:08:46 +00003695 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003696#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003697 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00003698#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003699#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003700 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003701#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00003702 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003703#endif
3704#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00003705
Thomas Wouters70c21a12000-07-14 14:28:33 +00003706#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00003707 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
3708 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003709#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003710 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
3711 if (slave_name == NULL)
3712 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00003713
Victor Stinner8c62be82010-05-06 00:08:46 +00003714 slave_fd = open(slave_name, O_RDWR);
3715 if (slave_fd < 0)
3716 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003717#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003718 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
3719 if (master_fd < 0)
3720 return posix_error();
3721 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
3722 /* change permission of slave */
3723 if (grantpt(master_fd) < 0) {
3724 PyOS_setsig(SIGCHLD, sig_saved);
3725 return posix_error();
3726 }
3727 /* unlock slave */
3728 if (unlockpt(master_fd) < 0) {
3729 PyOS_setsig(SIGCHLD, sig_saved);
3730 return posix_error();
3731 }
3732 PyOS_setsig(SIGCHLD, sig_saved);
3733 slave_name = ptsname(master_fd); /* get name of slave */
3734 if (slave_name == NULL)
3735 return posix_error();
3736 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
3737 if (slave_fd < 0)
3738 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00003739#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00003740 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
3741 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00003742#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00003743 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00003744#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003745#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00003746#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00003747
Victor Stinner8c62be82010-05-06 00:08:46 +00003748 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00003749
Fred Drake8cef4cf2000-06-28 16:40:38 +00003750}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00003751#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00003752
3753#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003754PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003755"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00003756Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
3757Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003758To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00003759
3760static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003761posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00003762{
Victor Stinner8c62be82010-05-06 00:08:46 +00003763 int master_fd = -1, result = 0;
3764 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00003765
Victor Stinner8c62be82010-05-06 00:08:46 +00003766 _PyImport_AcquireLock();
3767 pid = forkpty(&master_fd, NULL, NULL, NULL);
3768 if (pid == 0) {
3769 /* child: this clobbers and resets the import lock. */
3770 PyOS_AfterFork();
3771 } else {
3772 /* parent: release the import lock. */
3773 result = _PyImport_ReleaseLock();
3774 }
3775 if (pid == -1)
3776 return posix_error();
3777 if (result < 0) {
3778 /* Don't clobber the OSError if the fork failed. */
3779 PyErr_SetString(PyExc_RuntimeError,
3780 "not holding the import lock");
3781 return NULL;
3782 }
3783 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00003784}
3785#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003786
Guido van Rossumad0ee831995-03-01 10:34:45 +00003787#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003788PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003789"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003790Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003791
Barry Warsaw53699e91996-12-10 23:23:01 +00003792static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003793posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003794{
Victor Stinner8c62be82010-05-06 00:08:46 +00003795 return PyLong_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003796}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003797#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003798
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003799
Guido van Rossumad0ee831995-03-01 10:34:45 +00003800#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003801PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003802"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003803Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003804
Barry Warsaw53699e91996-12-10 23:23:01 +00003805static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003806posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003807{
Victor Stinner8c62be82010-05-06 00:08:46 +00003808 return PyLong_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003809}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003810#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003811
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003812
Guido van Rossumad0ee831995-03-01 10:34:45 +00003813#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003814PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003815"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003816Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003817
Barry Warsaw53699e91996-12-10 23:23:01 +00003818static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003819posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00003820{
Victor Stinner8c62be82010-05-06 00:08:46 +00003821 return PyLong_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00003822}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003823#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00003824
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003825
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003826PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003827"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003828Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003829
Barry Warsaw53699e91996-12-10 23:23:01 +00003830static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003831posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003832{
Victor Stinner8c62be82010-05-06 00:08:46 +00003833 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003834}
3835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003836
Fred Drakec9680921999-12-13 16:37:25 +00003837#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003838PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003839"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003840Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00003841
3842static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003843posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00003844{
3845 PyObject *result = NULL;
3846
Fred Drakec9680921999-12-13 16:37:25 +00003847#ifdef NGROUPS_MAX
3848#define MAX_GROUPS NGROUPS_MAX
3849#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003850 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00003851#define MAX_GROUPS 64
3852#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003853 gid_t grouplist[MAX_GROUPS];
3854 int n;
Fred Drakec9680921999-12-13 16:37:25 +00003855
Victor Stinner8c62be82010-05-06 00:08:46 +00003856 n = getgroups(MAX_GROUPS, grouplist);
3857 if (n < 0)
3858 posix_error();
3859 else {
3860 result = PyList_New(n);
3861 if (result != NULL) {
3862 int i;
3863 for (i = 0; i < n; ++i) {
3864 PyObject *o = PyLong_FromLong((long)grouplist[i]);
3865 if (o == NULL) {
3866 Py_DECREF(result);
3867 result = NULL;
3868 break;
Fred Drakec9680921999-12-13 16:37:25 +00003869 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003870 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00003871 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003872 }
3873 }
Neal Norwitze241ce82003-02-17 18:17:05 +00003874
Fred Drakec9680921999-12-13 16:37:25 +00003875 return result;
3876}
3877#endif
3878
Antoine Pitroub7572f02009-12-02 20:46:48 +00003879#ifdef HAVE_INITGROUPS
3880PyDoc_STRVAR(posix_initgroups__doc__,
3881"initgroups(username, gid) -> None\n\n\
3882Call the system initgroups() to initialize the group access list with all of\n\
3883the groups of which the specified username is a member, plus the specified\n\
3884group id.");
3885
3886static PyObject *
3887posix_initgroups(PyObject *self, PyObject *args)
3888{
Victor Stinner8c62be82010-05-06 00:08:46 +00003889 char *username;
3890 long gid;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003891
Victor Stinner8c62be82010-05-06 00:08:46 +00003892 if (!PyArg_ParseTuple(args, "sl:initgroups", &username, &gid))
3893 return NULL;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003894
Victor Stinner8c62be82010-05-06 00:08:46 +00003895 if (initgroups(username, (gid_t) gid) == -1)
3896 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00003897
Victor Stinner8c62be82010-05-06 00:08:46 +00003898 Py_INCREF(Py_None);
3899 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00003900}
3901#endif
3902
Martin v. Löwis606edc12002-06-13 21:09:11 +00003903#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003904PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003905"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00003906Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00003907
3908static PyObject *
3909posix_getpgid(PyObject *self, PyObject *args)
3910{
Victor Stinner8c62be82010-05-06 00:08:46 +00003911 pid_t pid, pgid;
3912 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
3913 return NULL;
3914 pgid = getpgid(pid);
3915 if (pgid < 0)
3916 return posix_error();
3917 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00003918}
3919#endif /* HAVE_GETPGID */
3920
3921
Guido van Rossumb6775db1994-08-01 11:34:53 +00003922#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003923PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003924"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003925Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003926
Barry Warsaw53699e91996-12-10 23:23:01 +00003927static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003928posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00003929{
Guido van Rossumb6775db1994-08-01 11:34:53 +00003930#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003931 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003932#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003933 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003934#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00003935}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003936#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00003937
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003938
Guido van Rossumb6775db1994-08-01 11:34:53 +00003939#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003940PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003941"setpgrp()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003942Make this process a session leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003943
Barry Warsaw53699e91996-12-10 23:23:01 +00003944static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003945posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00003946{
Guido van Rossum64933891994-10-20 21:56:42 +00003947#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00003948 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003949#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003950 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00003951#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00003952 return posix_error();
3953 Py_INCREF(Py_None);
3954 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00003955}
3956
Guido van Rossumb6775db1994-08-01 11:34:53 +00003957#endif /* HAVE_SETPGRP */
3958
Guido van Rossumad0ee831995-03-01 10:34:45 +00003959#ifdef HAVE_GETPPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003960PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003961"getppid() -> ppid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003962Return the parent's process id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003963
Barry Warsaw53699e91996-12-10 23:23:01 +00003964static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003965posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00003966{
Victor Stinner8c62be82010-05-06 00:08:46 +00003967 return PyLong_FromPid(getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00003968}
Guido van Rossumad0ee831995-03-01 10:34:45 +00003969#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00003970
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003971
Fred Drake12c6e2d1999-12-14 21:25:03 +00003972#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003973PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003974"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003975Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00003976
3977static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00003978posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00003979{
Victor Stinner8c62be82010-05-06 00:08:46 +00003980 PyObject *result = NULL;
3981 char *name;
3982 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00003983
Victor Stinner8c62be82010-05-06 00:08:46 +00003984 errno = 0;
3985 name = getlogin();
3986 if (name == NULL) {
3987 if (errno)
3988 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00003989 else
Victor Stinner8c62be82010-05-06 00:08:46 +00003990 PyErr_SetString(PyExc_OSError,
3991 "unable to determine login name");
3992 }
3993 else
3994 result = PyUnicode_FromString(name);
3995 errno = old_errno;
Neal Norwitze241ce82003-02-17 18:17:05 +00003996
Fred Drake12c6e2d1999-12-14 21:25:03 +00003997 return result;
3998}
3999#endif
4000
Guido van Rossumad0ee831995-03-01 10:34:45 +00004001#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004002PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004003"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004004Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004005
Barry Warsaw53699e91996-12-10 23:23:01 +00004006static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004007posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00004008{
Victor Stinner8c62be82010-05-06 00:08:46 +00004009 return PyLong_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00004010}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004011#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00004012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004013
Guido van Rossumad0ee831995-03-01 10:34:45 +00004014#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004015PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004016"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004017Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004018
Barry Warsaw53699e91996-12-10 23:23:01 +00004019static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004020posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004021{
Victor Stinner8c62be82010-05-06 00:08:46 +00004022 pid_t pid;
4023 int sig;
4024 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
4025 return NULL;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00004026#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004027 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
4028 APIRET rc;
4029 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004030 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004031
4032 } else if (sig == XCPT_SIGNAL_KILLPROC) {
4033 APIRET rc;
4034 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004035 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004036
4037 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00004038 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004039#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004040 if (kill(pid, sig) == -1)
4041 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00004042#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 Py_INCREF(Py_None);
4044 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00004045}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004046#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004047
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004048#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004049PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004050"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004051Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004052
4053static PyObject *
4054posix_killpg(PyObject *self, PyObject *args)
4055{
Victor Stinner8c62be82010-05-06 00:08:46 +00004056 int sig;
4057 pid_t pgid;
4058 /* XXX some man pages make the `pgid` parameter an int, others
4059 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
4060 take the same type. Moreover, pid_t is always at least as wide as
4061 int (else compilation of this module fails), which is safe. */
4062 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
4063 return NULL;
4064 if (killpg(pgid, sig) == -1)
4065 return posix_error();
4066 Py_INCREF(Py_None);
4067 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00004068}
4069#endif
4070
Brian Curtineb24d742010-04-12 17:16:38 +00004071#ifdef MS_WINDOWS
4072PyDoc_STRVAR(win32_kill__doc__,
4073"kill(pid, sig)\n\n\
4074Kill a process with a signal.");
4075
4076static PyObject *
4077win32_kill(PyObject *self, PyObject *args)
4078{
Victor Stinner8c62be82010-05-06 00:08:46 +00004079 PyObject *result, handle_obj;
4080 DWORD pid, sig, err;
4081 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00004082
Victor Stinner8c62be82010-05-06 00:08:46 +00004083 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
4084 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00004085
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 /* Console processes which share a common console can be sent CTRL+C or
4087 CTRL+BREAK events, provided they handle said events. */
4088 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
4089 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
4090 err = GetLastError();
4091 PyErr_SetFromWindowsErr(err);
4092 }
4093 else
4094 Py_RETURN_NONE;
4095 }
Brian Curtineb24d742010-04-12 17:16:38 +00004096
Victor Stinner8c62be82010-05-06 00:08:46 +00004097 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
4098 attempt to open and terminate the process. */
4099 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
4100 if (handle == NULL) {
4101 err = GetLastError();
4102 return PyErr_SetFromWindowsErr(err);
4103 }
Brian Curtineb24d742010-04-12 17:16:38 +00004104
Victor Stinner8c62be82010-05-06 00:08:46 +00004105 if (TerminateProcess(handle, sig) == 0) {
4106 err = GetLastError();
4107 result = PyErr_SetFromWindowsErr(err);
4108 } else {
4109 Py_INCREF(Py_None);
4110 result = Py_None;
4111 }
Brian Curtineb24d742010-04-12 17:16:38 +00004112
Victor Stinner8c62be82010-05-06 00:08:46 +00004113 CloseHandle(handle);
4114 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00004115}
4116#endif /* MS_WINDOWS */
4117
Guido van Rossumc0125471996-06-28 18:55:32 +00004118#ifdef HAVE_PLOCK
4119
4120#ifdef HAVE_SYS_LOCK_H
4121#include <sys/lock.h>
4122#endif
4123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004124PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004125"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004126Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004127
Barry Warsaw53699e91996-12-10 23:23:01 +00004128static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004129posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00004130{
Victor Stinner8c62be82010-05-06 00:08:46 +00004131 int op;
4132 if (!PyArg_ParseTuple(args, "i:plock", &op))
4133 return NULL;
4134 if (plock(op) == -1)
4135 return posix_error();
4136 Py_INCREF(Py_None);
4137 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00004138}
4139#endif
4140
Guido van Rossumb6775db1994-08-01 11:34:53 +00004141#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004142PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004143"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004144Set the current process's user id.");
4145
Barry Warsaw53699e91996-12-10 23:23:01 +00004146static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004147posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004148{
Victor Stinner8c62be82010-05-06 00:08:46 +00004149 long uid_arg;
4150 uid_t uid;
4151 if (!PyArg_ParseTuple(args, "l:setuid", &uid_arg))
4152 return NULL;
4153 uid = uid_arg;
4154 if (uid != uid_arg) {
4155 PyErr_SetString(PyExc_OverflowError, "user id too big");
4156 return NULL;
4157 }
4158 if (setuid(uid) < 0)
4159 return posix_error();
4160 Py_INCREF(Py_None);
4161 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004162}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004163#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004164
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004165
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004166#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004167PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004168"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004169Set the current process's effective user id.");
4170
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004171static PyObject *
4172posix_seteuid (PyObject *self, PyObject *args)
4173{
Victor Stinner8c62be82010-05-06 00:08:46 +00004174 long euid_arg;
4175 uid_t euid;
4176 if (!PyArg_ParseTuple(args, "l", &euid_arg))
4177 return NULL;
4178 euid = euid_arg;
4179 if (euid != euid_arg) {
4180 PyErr_SetString(PyExc_OverflowError, "user id too big");
4181 return NULL;
4182 }
4183 if (seteuid(euid) < 0) {
4184 return posix_error();
4185 } else {
4186 Py_INCREF(Py_None);
4187 return Py_None;
4188 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004189}
4190#endif /* HAVE_SETEUID */
4191
4192#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004193PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004194"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004195Set the current process's effective group id.");
4196
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004197static PyObject *
4198posix_setegid (PyObject *self, PyObject *args)
4199{
Victor Stinner8c62be82010-05-06 00:08:46 +00004200 long egid_arg;
4201 gid_t egid;
4202 if (!PyArg_ParseTuple(args, "l", &egid_arg))
4203 return NULL;
4204 egid = egid_arg;
4205 if (egid != egid_arg) {
4206 PyErr_SetString(PyExc_OverflowError, "group id too big");
4207 return NULL;
4208 }
4209 if (setegid(egid) < 0) {
4210 return posix_error();
4211 } else {
4212 Py_INCREF(Py_None);
4213 return Py_None;
4214 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004215}
4216#endif /* HAVE_SETEGID */
4217
4218#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004219PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004220"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004221Set the current process's real and effective user ids.");
4222
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004223static PyObject *
4224posix_setreuid (PyObject *self, PyObject *args)
4225{
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 long ruid_arg, euid_arg;
4227 uid_t ruid, euid;
4228 if (!PyArg_ParseTuple(args, "ll", &ruid_arg, &euid_arg))
4229 return NULL;
4230 if (ruid_arg == -1)
4231 ruid = (uid_t)-1; /* let the compiler choose how -1 fits */
4232 else
4233 ruid = ruid_arg; /* otherwise, assign from our long */
4234 if (euid_arg == -1)
4235 euid = (uid_t)-1;
4236 else
4237 euid = euid_arg;
4238 if ((euid_arg != -1 && euid != euid_arg) ||
4239 (ruid_arg != -1 && ruid != ruid_arg)) {
4240 PyErr_SetString(PyExc_OverflowError, "user id too big");
4241 return NULL;
4242 }
4243 if (setreuid(ruid, euid) < 0) {
4244 return posix_error();
4245 } else {
4246 Py_INCREF(Py_None);
4247 return Py_None;
4248 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004249}
4250#endif /* HAVE_SETREUID */
4251
4252#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004253PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00004254"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004255Set the current process's real and effective group ids.");
4256
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004257static PyObject *
4258posix_setregid (PyObject *self, PyObject *args)
4259{
Victor Stinner8c62be82010-05-06 00:08:46 +00004260 long rgid_arg, egid_arg;
4261 gid_t rgid, egid;
4262 if (!PyArg_ParseTuple(args, "ll", &rgid_arg, &egid_arg))
4263 return NULL;
4264 if (rgid_arg == -1)
4265 rgid = (gid_t)-1; /* let the compiler choose how -1 fits */
4266 else
4267 rgid = rgid_arg; /* otherwise, assign from our long */
4268 if (egid_arg == -1)
4269 egid = (gid_t)-1;
4270 else
4271 egid = egid_arg;
4272 if ((egid_arg != -1 && egid != egid_arg) ||
4273 (rgid_arg != -1 && rgid != rgid_arg)) {
4274 PyErr_SetString(PyExc_OverflowError, "group id too big");
4275 return NULL;
4276 }
4277 if (setregid(rgid, egid) < 0) {
4278 return posix_error();
4279 } else {
4280 Py_INCREF(Py_None);
4281 return Py_None;
4282 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00004283}
4284#endif /* HAVE_SETREGID */
4285
Guido van Rossumb6775db1994-08-01 11:34:53 +00004286#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004287PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004288"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004289Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004290
Barry Warsaw53699e91996-12-10 23:23:01 +00004291static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004292posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004293{
Victor Stinner8c62be82010-05-06 00:08:46 +00004294 long gid_arg;
4295 gid_t gid;
4296 if (!PyArg_ParseTuple(args, "l:setgid", &gid_arg))
4297 return NULL;
4298 gid = gid_arg;
4299 if (gid != gid_arg) {
4300 PyErr_SetString(PyExc_OverflowError, "group id too big");
4301 return NULL;
4302 }
4303 if (setgid(gid) < 0)
4304 return posix_error();
4305 Py_INCREF(Py_None);
4306 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004307}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004308#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00004309
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004310#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004311PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004312"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004313Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004314
4315static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00004316posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004317{
Victor Stinner8c62be82010-05-06 00:08:46 +00004318 int i, len;
4319 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00004320
Victor Stinner8c62be82010-05-06 00:08:46 +00004321 if (!PySequence_Check(groups)) {
4322 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
4323 return NULL;
4324 }
4325 len = PySequence_Size(groups);
4326 if (len > MAX_GROUPS) {
4327 PyErr_SetString(PyExc_ValueError, "too many groups");
4328 return NULL;
4329 }
4330 for(i = 0; i < len; i++) {
4331 PyObject *elem;
4332 elem = PySequence_GetItem(groups, i);
4333 if (!elem)
4334 return NULL;
4335 if (!PyLong_Check(elem)) {
4336 PyErr_SetString(PyExc_TypeError,
4337 "groups must be integers");
4338 Py_DECREF(elem);
4339 return NULL;
4340 } else {
4341 unsigned long x = PyLong_AsUnsignedLong(elem);
4342 if (PyErr_Occurred()) {
4343 PyErr_SetString(PyExc_TypeError,
4344 "group id too big");
4345 Py_DECREF(elem);
4346 return NULL;
4347 }
4348 grouplist[i] = x;
4349 /* read back the value to see if it fitted in gid_t */
4350 if (grouplist[i] != x) {
4351 PyErr_SetString(PyExc_TypeError,
4352 "group id too big");
4353 Py_DECREF(elem);
4354 return NULL;
4355 }
4356 }
4357 Py_DECREF(elem);
4358 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004359
Victor Stinner8c62be82010-05-06 00:08:46 +00004360 if (setgroups(len, grouplist) < 0)
4361 return posix_error();
4362 Py_INCREF(Py_None);
4363 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00004364}
4365#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004366
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004367#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
4368static PyObject *
Christian Heimes292d3512008-02-03 16:51:08 +00004369wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004370{
Victor Stinner8c62be82010-05-06 00:08:46 +00004371 PyObject *result;
4372 static PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004373
Victor Stinner8c62be82010-05-06 00:08:46 +00004374 if (pid == -1)
4375 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004376
Victor Stinner8c62be82010-05-06 00:08:46 +00004377 if (struct_rusage == NULL) {
4378 PyObject *m = PyImport_ImportModuleNoBlock("resource");
4379 if (m == NULL)
4380 return NULL;
4381 struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
4382 Py_DECREF(m);
4383 if (struct_rusage == NULL)
4384 return NULL;
4385 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004386
Victor Stinner8c62be82010-05-06 00:08:46 +00004387 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
4388 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
4389 if (!result)
4390 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004391
4392#ifndef doubletime
4393#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
4394#endif
4395
Victor Stinner8c62be82010-05-06 00:08:46 +00004396 PyStructSequence_SET_ITEM(result, 0,
4397 PyFloat_FromDouble(doubletime(ru->ru_utime)));
4398 PyStructSequence_SET_ITEM(result, 1,
4399 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004400#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00004401 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
4402 SET_INT(result, 2, ru->ru_maxrss);
4403 SET_INT(result, 3, ru->ru_ixrss);
4404 SET_INT(result, 4, ru->ru_idrss);
4405 SET_INT(result, 5, ru->ru_isrss);
4406 SET_INT(result, 6, ru->ru_minflt);
4407 SET_INT(result, 7, ru->ru_majflt);
4408 SET_INT(result, 8, ru->ru_nswap);
4409 SET_INT(result, 9, ru->ru_inblock);
4410 SET_INT(result, 10, ru->ru_oublock);
4411 SET_INT(result, 11, ru->ru_msgsnd);
4412 SET_INT(result, 12, ru->ru_msgrcv);
4413 SET_INT(result, 13, ru->ru_nsignals);
4414 SET_INT(result, 14, ru->ru_nvcsw);
4415 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004416#undef SET_INT
4417
Victor Stinner8c62be82010-05-06 00:08:46 +00004418 if (PyErr_Occurred()) {
4419 Py_DECREF(result);
4420 return NULL;
4421 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004422
Victor Stinner8c62be82010-05-06 00:08:46 +00004423 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004424}
4425#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
4426
4427#ifdef HAVE_WAIT3
4428PyDoc_STRVAR(posix_wait3__doc__,
4429"wait3(options) -> (pid, status, rusage)\n\n\
4430Wait for completion of a child process.");
4431
4432static PyObject *
4433posix_wait3(PyObject *self, PyObject *args)
4434{
Victor Stinner8c62be82010-05-06 00:08:46 +00004435 pid_t pid;
4436 int options;
4437 struct rusage ru;
4438 WAIT_TYPE status;
4439 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004440
Victor Stinner8c62be82010-05-06 00:08:46 +00004441 if (!PyArg_ParseTuple(args, "i:wait3", &options))
4442 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004443
Victor Stinner8c62be82010-05-06 00:08:46 +00004444 Py_BEGIN_ALLOW_THREADS
4445 pid = wait3(&status, options, &ru);
4446 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004447
Victor Stinner8c62be82010-05-06 00:08:46 +00004448 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004449}
4450#endif /* HAVE_WAIT3 */
4451
4452#ifdef HAVE_WAIT4
4453PyDoc_STRVAR(posix_wait4__doc__,
4454"wait4(pid, options) -> (pid, status, rusage)\n\n\
4455Wait for completion of a given child process.");
4456
4457static PyObject *
4458posix_wait4(PyObject *self, PyObject *args)
4459{
Victor Stinner8c62be82010-05-06 00:08:46 +00004460 pid_t pid;
4461 int options;
4462 struct rusage ru;
4463 WAIT_TYPE status;
4464 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004465
Victor Stinner8c62be82010-05-06 00:08:46 +00004466 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
4467 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004468
Victor Stinner8c62be82010-05-06 00:08:46 +00004469 Py_BEGIN_ALLOW_THREADS
4470 pid = wait4(pid, &status, options, &ru);
4471 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004472
Victor Stinner8c62be82010-05-06 00:08:46 +00004473 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004474}
4475#endif /* HAVE_WAIT4 */
4476
Guido van Rossumb6775db1994-08-01 11:34:53 +00004477#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004478PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004479"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004480Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004481
Barry Warsaw53699e91996-12-10 23:23:01 +00004482static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004483posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004484{
Victor Stinner8c62be82010-05-06 00:08:46 +00004485 pid_t pid;
4486 int options;
4487 WAIT_TYPE status;
4488 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00004489
Victor Stinner8c62be82010-05-06 00:08:46 +00004490 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4491 return NULL;
4492 Py_BEGIN_ALLOW_THREADS
4493 pid = waitpid(pid, &status, options);
4494 Py_END_ALLOW_THREADS
4495 if (pid == -1)
4496 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004497
Victor Stinner8c62be82010-05-06 00:08:46 +00004498 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00004499}
4500
Tim Petersab034fa2002-02-01 11:27:43 +00004501#elif defined(HAVE_CWAIT)
4502
4503/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004504PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004505"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004506"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00004507
4508static PyObject *
4509posix_waitpid(PyObject *self, PyObject *args)
4510{
Victor Stinner8c62be82010-05-06 00:08:46 +00004511 Py_intptr_t pid;
4512 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00004513
Victor Stinner8c62be82010-05-06 00:08:46 +00004514 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
4515 return NULL;
4516 Py_BEGIN_ALLOW_THREADS
4517 pid = _cwait(&status, pid, options);
4518 Py_END_ALLOW_THREADS
4519 if (pid == -1)
4520 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004521
Victor Stinner8c62be82010-05-06 00:08:46 +00004522 /* shift the status left a byte so this is more like the POSIX waitpid */
4523 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00004524}
4525#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004526
Guido van Rossumad0ee831995-03-01 10:34:45 +00004527#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004528PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004529"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004530Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004531
Barry Warsaw53699e91996-12-10 23:23:01 +00004532static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004533posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00004534{
Victor Stinner8c62be82010-05-06 00:08:46 +00004535 pid_t pid;
4536 WAIT_TYPE status;
4537 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00004538
Victor Stinner8c62be82010-05-06 00:08:46 +00004539 Py_BEGIN_ALLOW_THREADS
4540 pid = wait(&status);
4541 Py_END_ALLOW_THREADS
4542 if (pid == -1)
4543 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004544
Victor Stinner8c62be82010-05-06 00:08:46 +00004545 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00004546}
Guido van Rossumad0ee831995-03-01 10:34:45 +00004547#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00004548
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004550PyDoc_STRVAR(posix_lstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004551"lstat(path) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004552Like stat(path), but do not follow symbolic links.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004553
Barry Warsaw53699e91996-12-10 23:23:01 +00004554static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004555posix_lstat(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004556{
Guido van Rossumb6775db1994-08-01 11:34:53 +00004557#ifdef HAVE_LSTAT
Victor Stinner8c62be82010-05-06 00:08:46 +00004558 return posix_do_stat(self, args, "O&:lstat", lstat, NULL, NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004559#else /* !HAVE_LSTAT */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004560#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004561 return posix_do_stat(self, args, "O&:lstat", STAT, "U:lstat", win32_wstat);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004562#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004563 return posix_do_stat(self, args, "O&:lstat", STAT, NULL, NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004564#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00004565#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004566}
4567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004568
Guido van Rossumb6775db1994-08-01 11:34:53 +00004569#ifdef HAVE_READLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004570PyDoc_STRVAR(posix_readlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004571"readlink(path) -> path\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004572Return a string representing the path to which the symbolic link points.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004573
Barry Warsaw53699e91996-12-10 23:23:01 +00004574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004575posix_readlink(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004576{
Victor Stinner8c62be82010-05-06 00:08:46 +00004577 PyObject* v;
4578 char buf[MAXPATHLEN];
4579 PyObject *opath;
4580 char *path;
4581 int n;
4582 int arg_is_unicode = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004583
Victor Stinner8c62be82010-05-06 00:08:46 +00004584 if (!PyArg_ParseTuple(args, "O&:readlink",
4585 PyUnicode_FSConverter, &opath))
4586 return NULL;
4587 path = PyBytes_AsString(opath);
4588 v = PySequence_GetItem(args, 0);
4589 if (v == NULL) {
4590 Py_DECREF(opath);
4591 return NULL;
4592 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004593
Victor Stinner8c62be82010-05-06 00:08:46 +00004594 if (PyUnicode_Check(v)) {
4595 arg_is_unicode = 1;
4596 }
4597 Py_DECREF(v);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004598
Victor Stinner8c62be82010-05-06 00:08:46 +00004599 Py_BEGIN_ALLOW_THREADS
4600 n = readlink(path, buf, (int) sizeof buf);
4601 Py_END_ALLOW_THREADS
4602 if (n < 0)
4603 return posix_error_with_allocated_filename(opath);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004604
Victor Stinner8c62be82010-05-06 00:08:46 +00004605 Py_DECREF(opath);
4606 v = PyBytes_FromStringAndSize(buf, n);
4607 if (arg_is_unicode) {
4608 PyObject *w;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004609
Victor Stinner8c62be82010-05-06 00:08:46 +00004610 w = PyUnicode_FromEncodedObject(v,
4611 Py_FileSystemDefaultEncoding,
4612 "surrogateescape");
4613 if (w != NULL) {
4614 Py_DECREF(v);
4615 v = w;
4616 }
4617 else {
4618 v = NULL;
4619 }
4620 }
4621 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004622}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004623#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004624
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004625
Guido van Rossumb6775db1994-08-01 11:34:53 +00004626#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004627PyDoc_STRVAR(posix_symlink__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004628"symlink(src, dst)\n\n\
Brett Cannon807413d2003-06-11 00:18:09 +00004629Create a symbolic link pointing to src named dst.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004630
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004631static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004632posix_symlink(PyObject *self, PyObject *args)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004633{
Victor Stinner8c62be82010-05-06 00:08:46 +00004634 return posix_2str(args, "O&O&:symlink", symlink);
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004635}
4636#endif /* HAVE_SYMLINK */
4637
4638
4639#ifdef HAVE_TIMES
Guido van Rossumd48f2521997-12-05 22:19:34 +00004640#if defined(PYCC_VACPP) && defined(PYOS_OS2)
4641static long
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00004642system_uptime(void)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004643{
4644 ULONG value = 0;
4645
4646 Py_BEGIN_ALLOW_THREADS
4647 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
4648 Py_END_ALLOW_THREADS
4649
4650 return value;
4651}
4652
4653static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004654posix_times(PyObject *self, PyObject *noargs)
Guido van Rossumd48f2521997-12-05 22:19:34 +00004655{
Guido van Rossumd48f2521997-12-05 22:19:34 +00004656 /* Currently Only Uptime is Provided -- Others Later */
Victor Stinner8c62be82010-05-06 00:08:46 +00004657 return Py_BuildValue("ddddd",
4658 (double)0 /* t.tms_utime / HZ */,
4659 (double)0 /* t.tms_stime / HZ */,
4660 (double)0 /* t.tms_cutime / HZ */,
4661 (double)0 /* t.tms_cstime / HZ */,
4662 (double)system_uptime() / 1000);
Guido van Rossumd48f2521997-12-05 22:19:34 +00004663}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004664#else /* not OS2 */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00004665#define NEED_TICKS_PER_SECOND
4666static long ticks_per_second = -1;
Barry Warsaw53699e91996-12-10 23:23:01 +00004667static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004668posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum22db57e1992-04-05 14:25:30 +00004669{
Victor Stinner8c62be82010-05-06 00:08:46 +00004670 struct tms t;
4671 clock_t c;
4672 errno = 0;
4673 c = times(&t);
4674 if (c == (clock_t) -1)
4675 return posix_error();
4676 return Py_BuildValue("ddddd",
4677 (double)t.tms_utime / ticks_per_second,
4678 (double)t.tms_stime / ticks_per_second,
4679 (double)t.tms_cutime / ticks_per_second,
4680 (double)t.tms_cstime / ticks_per_second,
4681 (double)c / ticks_per_second);
Guido van Rossum22db57e1992-04-05 14:25:30 +00004682}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004683#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00004684#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004685
4686
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004687#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004688#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00004689static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004690posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004691{
Victor Stinner8c62be82010-05-06 00:08:46 +00004692 FILETIME create, exit, kernel, user;
4693 HANDLE hProc;
4694 hProc = GetCurrentProcess();
4695 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
4696 /* The fields of a FILETIME structure are the hi and lo part
4697 of a 64-bit value expressed in 100 nanosecond units.
4698 1e7 is one second in such units; 1e-7 the inverse.
4699 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
4700 */
4701 return Py_BuildValue(
4702 "ddddd",
4703 (double)(user.dwHighDateTime*429.4967296 +
4704 user.dwLowDateTime*1e-7),
4705 (double)(kernel.dwHighDateTime*429.4967296 +
4706 kernel.dwLowDateTime*1e-7),
4707 (double)0,
4708 (double)0,
4709 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00004710}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004711#endif /* MS_WINDOWS */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004712
4713#ifdef HAVE_TIMES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004714PyDoc_STRVAR(posix_times__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004715"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004716Return a tuple of floating point numbers indicating process times.");
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00004717#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00004718
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004719
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004720#ifdef HAVE_GETSID
4721PyDoc_STRVAR(posix_getsid__doc__,
4722"getsid(pid) -> sid\n\n\
4723Call the system call getsid().");
4724
4725static PyObject *
4726posix_getsid(PyObject *self, PyObject *args)
4727{
Victor Stinner8c62be82010-05-06 00:08:46 +00004728 pid_t pid;
4729 int sid;
4730 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
4731 return NULL;
4732 sid = getsid(pid);
4733 if (sid < 0)
4734 return posix_error();
4735 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00004736}
4737#endif /* HAVE_GETSID */
4738
4739
Guido van Rossumb6775db1994-08-01 11:34:53 +00004740#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004741PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004742"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004743Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004744
Barry Warsaw53699e91996-12-10 23:23:01 +00004745static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004746posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004747{
Victor Stinner8c62be82010-05-06 00:08:46 +00004748 if (setsid() < 0)
4749 return posix_error();
4750 Py_INCREF(Py_None);
4751 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004752}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004753#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004754
Guido van Rossumb6775db1994-08-01 11:34:53 +00004755#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004756PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004757"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004758Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004759
Barry Warsaw53699e91996-12-10 23:23:01 +00004760static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004761posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00004762{
Victor Stinner8c62be82010-05-06 00:08:46 +00004763 pid_t pid;
4764 int pgrp;
4765 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
4766 return NULL;
4767 if (setpgid(pid, pgrp) < 0)
4768 return posix_error();
4769 Py_INCREF(Py_None);
4770 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00004771}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004772#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00004773
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004774
Guido van Rossumb6775db1994-08-01 11:34:53 +00004775#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004776PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004777"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004778Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004779
Barry Warsaw53699e91996-12-10 23:23:01 +00004780static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004781posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004782{
Victor Stinner8c62be82010-05-06 00:08:46 +00004783 int fd;
4784 pid_t pgid;
4785 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
4786 return NULL;
4787 pgid = tcgetpgrp(fd);
4788 if (pgid < 0)
4789 return posix_error();
4790 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00004791}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004792#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00004793
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004794
Guido van Rossumb6775db1994-08-01 11:34:53 +00004795#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004796PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004797"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004798Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004799
Barry Warsaw53699e91996-12-10 23:23:01 +00004800static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004801posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00004802{
Victor Stinner8c62be82010-05-06 00:08:46 +00004803 int fd;
4804 pid_t pgid;
4805 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
4806 return NULL;
4807 if (tcsetpgrp(fd, pgid) < 0)
4808 return posix_error();
4809 Py_INCREF(Py_None);
4810 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00004811}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004812#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00004813
Guido van Rossum687dd131993-05-17 08:34:16 +00004814/* Functions acting on file descriptors */
4815
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004816PyDoc_STRVAR(posix_open__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004817"open(filename, flag [, mode=0777]) -> fd\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004818Open a file (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004819
Barry Warsaw53699e91996-12-10 23:23:01 +00004820static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004821posix_open(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004822{
Victor Stinner8c62be82010-05-06 00:08:46 +00004823 PyObject *ofile;
4824 char *file;
4825 int flag;
4826 int mode = 0777;
4827 int fd;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004828
4829#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004830 PyUnicodeObject *po;
4831 if (PyArg_ParseTuple(args, "Ui|i:mkdir", &po, &flag, &mode)) {
4832 Py_BEGIN_ALLOW_THREADS
4833 /* PyUnicode_AS_UNICODE OK without thread
4834 lock as it is a simple dereference. */
4835 fd = _wopen(PyUnicode_AS_UNICODE(po), flag, mode);
4836 Py_END_ALLOW_THREADS
4837 if (fd < 0)
4838 return posix_error();
4839 return PyLong_FromLong((long)fd);
4840 }
4841 /* Drop the argument parsing error as narrow strings
4842 are also valid. */
4843 PyErr_Clear();
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004844#endif
4845
Victor Stinner8c62be82010-05-06 00:08:46 +00004846 if (!PyArg_ParseTuple(args, "O&i|i",
4847 PyUnicode_FSConverter, &ofile,
4848 &flag, &mode))
4849 return NULL;
4850 file = PyBytes_AsString(ofile);
4851 Py_BEGIN_ALLOW_THREADS
4852 fd = open(file, flag, mode);
4853 Py_END_ALLOW_THREADS
4854 if (fd < 0)
4855 return posix_error_with_allocated_filename(ofile);
4856 Py_DECREF(ofile);
4857 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004858}
4859
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004861PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004862"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004863Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004864
Barry Warsaw53699e91996-12-10 23:23:01 +00004865static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004866posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004867{
Victor Stinner8c62be82010-05-06 00:08:46 +00004868 int fd, res;
4869 if (!PyArg_ParseTuple(args, "i:close", &fd))
4870 return NULL;
4871 if (!_PyVerify_fd(fd))
4872 return posix_error();
4873 Py_BEGIN_ALLOW_THREADS
4874 res = close(fd);
4875 Py_END_ALLOW_THREADS
4876 if (res < 0)
4877 return posix_error();
4878 Py_INCREF(Py_None);
4879 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004880}
4881
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004882
Victor Stinner8c62be82010-05-06 00:08:46 +00004883PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00004884"closerange(fd_low, fd_high)\n\n\
4885Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
4886
4887static PyObject *
4888posix_closerange(PyObject *self, PyObject *args)
4889{
Victor Stinner8c62be82010-05-06 00:08:46 +00004890 int fd_from, fd_to, i;
4891 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
4892 return NULL;
4893 Py_BEGIN_ALLOW_THREADS
4894 for (i = fd_from; i < fd_to; i++)
4895 if (_PyVerify_fd(i))
4896 close(i);
4897 Py_END_ALLOW_THREADS
4898 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00004899}
4900
4901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004902PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004903"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004904Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004905
Barry Warsaw53699e91996-12-10 23:23:01 +00004906static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004907posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004908{
Victor Stinner8c62be82010-05-06 00:08:46 +00004909 int fd;
4910 if (!PyArg_ParseTuple(args, "i:dup", &fd))
4911 return NULL;
4912 if (!_PyVerify_fd(fd))
4913 return posix_error();
4914 Py_BEGIN_ALLOW_THREADS
4915 fd = dup(fd);
4916 Py_END_ALLOW_THREADS
4917 if (fd < 0)
4918 return posix_error();
4919 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00004920}
4921
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004922
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004923PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00004924"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004925Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004926
Barry Warsaw53699e91996-12-10 23:23:01 +00004927static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004928posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004929{
Victor Stinner8c62be82010-05-06 00:08:46 +00004930 int fd, fd2, res;
4931 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
4932 return NULL;
4933 if (!_PyVerify_fd_dup2(fd, fd2))
4934 return posix_error();
4935 Py_BEGIN_ALLOW_THREADS
4936 res = dup2(fd, fd2);
4937 Py_END_ALLOW_THREADS
4938 if (res < 0)
4939 return posix_error();
4940 Py_INCREF(Py_None);
4941 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00004942}
4943
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004944
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004945PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004946"lseek(fd, pos, how) -> newpos\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004947Set the current position of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004948
Barry Warsaw53699e91996-12-10 23:23:01 +00004949static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004950posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00004951{
Victor Stinner8c62be82010-05-06 00:08:46 +00004952 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004953#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004955#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004956 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00004957#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004958 PyObject *posobj;
4959 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
4960 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00004961#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00004962 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
4963 switch (how) {
4964 case 0: how = SEEK_SET; break;
4965 case 1: how = SEEK_CUR; break;
4966 case 2: how = SEEK_END; break;
4967 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004968#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00004969
4970#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004971 pos = PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004972#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004973 pos = PyLong_Check(posobj) ?
4974 PyLong_AsLongLong(posobj) : PyLong_AsLong(posobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004975#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004976 if (PyErr_Occurred())
4977 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00004978
Victor Stinner8c62be82010-05-06 00:08:46 +00004979 if (!_PyVerify_fd(fd))
4980 return posix_error();
4981 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004982#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00004983 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004984#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004985 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00004986#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004987 Py_END_ALLOW_THREADS
4988 if (res < 0)
4989 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00004990
4991#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00004992 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004993#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004994 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00004995#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00004996}
4997
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004999PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005000"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005001Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005002
Barry Warsaw53699e91996-12-10 23:23:01 +00005003static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005004posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005005{
Victor Stinner8c62be82010-05-06 00:08:46 +00005006 int fd, size;
5007 Py_ssize_t n;
5008 PyObject *buffer;
5009 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
5010 return NULL;
5011 if (size < 0) {
5012 errno = EINVAL;
5013 return posix_error();
5014 }
5015 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
5016 if (buffer == NULL)
5017 return NULL;
5018 if (!_PyVerify_fd(fd))
5019 return posix_error();
5020 Py_BEGIN_ALLOW_THREADS
5021 n = read(fd, PyBytes_AS_STRING(buffer), size);
5022 Py_END_ALLOW_THREADS
5023 if (n < 0) {
5024 Py_DECREF(buffer);
5025 return posix_error();
5026 }
5027 if (n != size)
5028 _PyBytes_Resize(&buffer, n);
5029 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00005030}
5031
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005033PyDoc_STRVAR(posix_write__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005034"write(fd, string) -> byteswritten\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005035Write a string to a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005036
Barry Warsaw53699e91996-12-10 23:23:01 +00005037static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005038posix_write(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005039{
Victor Stinner8c62be82010-05-06 00:08:46 +00005040 Py_buffer pbuf;
5041 int fd;
5042 Py_ssize_t size;
Thomas Wouters68bc4f92006-03-01 01:05:10 +00005043
Victor Stinner8c62be82010-05-06 00:08:46 +00005044 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
5045 return NULL;
5046 if (!_PyVerify_fd(fd))
5047 return posix_error();
5048 Py_BEGIN_ALLOW_THREADS
5049 size = write(fd, pbuf.buf, (size_t)pbuf.len);
5050 Py_END_ALLOW_THREADS
5051 PyBuffer_Release(&pbuf);
5052 if (size < 0)
5053 return posix_error();
5054 return PyLong_FromSsize_t(size);
Guido van Rossum687dd131993-05-17 08:34:16 +00005055}
5056
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005057
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005058PyDoc_STRVAR(posix_fstat__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005059"fstat(fd) -> stat result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005060Like stat(), but for an open file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005061
Barry Warsaw53699e91996-12-10 23:23:01 +00005062static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005063posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00005064{
Victor Stinner8c62be82010-05-06 00:08:46 +00005065 int fd;
5066 STRUCT_STAT st;
5067 int res;
5068 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
5069 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005070#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00005071 /* on OpenVMS we must ensure that all bytes are written to the file */
5072 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00005073#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005074 if (!_PyVerify_fd(fd))
5075 return posix_error();
5076 Py_BEGIN_ALLOW_THREADS
5077 res = FSTAT(fd, &st);
5078 Py_END_ALLOW_THREADS
5079 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00005080#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005081 return win32_error("fstat", NULL);
Martin v. Löwis14694662006-02-03 12:54:16 +00005082#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005083 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00005084#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005085 }
Tim Peters5aa91602002-01-30 05:46:57 +00005086
Victor Stinner8c62be82010-05-06 00:08:46 +00005087 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00005088}
5089
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005090PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005091"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005092Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005093connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00005094
5095static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00005096posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00005097{
Victor Stinner8c62be82010-05-06 00:08:46 +00005098 int fd;
5099 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
5100 return NULL;
5101 if (!_PyVerify_fd(fd))
5102 return PyBool_FromLong(0);
5103 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00005104}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005105
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005106#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005107PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005108"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005109Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005110
Barry Warsaw53699e91996-12-10 23:23:01 +00005111static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005112posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00005113{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005114#if defined(PYOS_OS2)
5115 HFILE read, write;
5116 APIRET rc;
5117
Victor Stinner8c62be82010-05-06 00:08:46 +00005118 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005119 rc = DosCreatePipe( &read, &write, 4096);
Victor Stinner8c62be82010-05-06 00:08:46 +00005120 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005121 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00005122 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005123
5124 return Py_BuildValue("(ii)", read, write);
5125#else
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005126#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00005127 int fds[2];
5128 int res;
5129 Py_BEGIN_ALLOW_THREADS
5130 res = pipe(fds);
5131 Py_END_ALLOW_THREADS
5132 if (res != 0)
5133 return posix_error();
5134 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005135#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00005136 HANDLE read, write;
5137 int read_fd, write_fd;
5138 BOOL ok;
5139 Py_BEGIN_ALLOW_THREADS
5140 ok = CreatePipe(&read, &write, NULL, 0);
5141 Py_END_ALLOW_THREADS
5142 if (!ok)
5143 return win32_error("CreatePipe", NULL);
5144 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
5145 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
5146 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00005147#endif /* MS_WINDOWS */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00005148#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00005149}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005150#endif /* HAVE_PIPE */
5151
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005152
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005153#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005154PyDoc_STRVAR(posix_mkfifo__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005155"mkfifo(filename [, mode=0666])\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005156Create a FIFO (a POSIX named pipe).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005157
Barry Warsaw53699e91996-12-10 23:23:01 +00005158static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005159posix_mkfifo(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005160{
Victor Stinner8c62be82010-05-06 00:08:46 +00005161 char *filename;
5162 int mode = 0666;
5163 int res;
5164 if (!PyArg_ParseTuple(args, "s|i:mkfifo", &filename, &mode))
5165 return NULL;
5166 Py_BEGIN_ALLOW_THREADS
5167 res = mkfifo(filename, mode);
5168 Py_END_ALLOW_THREADS
5169 if (res < 0)
5170 return posix_error();
5171 Py_INCREF(Py_None);
5172 return Py_None;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005173}
5174#endif
5175
5176
Neal Norwitz11690112002-07-30 01:08:28 +00005177#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005178PyDoc_STRVAR(posix_mknod__doc__,
Neal Norwitzc18b3082002-10-11 22:19:42 +00005179"mknod(filename [, mode=0600, device])\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005180Create a filesystem node (file, device special file or named pipe)\n\
5181named filename. mode specifies both the permissions to use and the\n\
5182type of node to be created, being combined (bitwise OR) with one of\n\
5183S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005184device defines the newly created device special file (probably using\n\
5185os.makedev()), otherwise it is ignored.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00005186
5187
5188static PyObject *
5189posix_mknod(PyObject *self, PyObject *args)
5190{
Victor Stinner8c62be82010-05-06 00:08:46 +00005191 char *filename;
5192 int mode = 0600;
5193 int device = 0;
5194 int res;
5195 if (!PyArg_ParseTuple(args, "s|ii:mknod", &filename, &mode, &device))
5196 return NULL;
5197 Py_BEGIN_ALLOW_THREADS
5198 res = mknod(filename, mode, device);
5199 Py_END_ALLOW_THREADS
5200 if (res < 0)
5201 return posix_error();
5202 Py_INCREF(Py_None);
5203 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005204}
5205#endif
5206
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005207#ifdef HAVE_DEVICE_MACROS
5208PyDoc_STRVAR(posix_major__doc__,
5209"major(device) -> major number\n\
5210Extracts a device major number from a raw device number.");
5211
5212static PyObject *
5213posix_major(PyObject *self, PyObject *args)
5214{
Victor Stinner8c62be82010-05-06 00:08:46 +00005215 int device;
5216 if (!PyArg_ParseTuple(args, "i:major", &device))
5217 return NULL;
5218 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005219}
5220
5221PyDoc_STRVAR(posix_minor__doc__,
5222"minor(device) -> minor number\n\
5223Extracts a device minor number from a raw device number.");
5224
5225static PyObject *
5226posix_minor(PyObject *self, PyObject *args)
5227{
Victor Stinner8c62be82010-05-06 00:08:46 +00005228 int device;
5229 if (!PyArg_ParseTuple(args, "i:minor", &device))
5230 return NULL;
5231 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005232}
5233
5234PyDoc_STRVAR(posix_makedev__doc__,
5235"makedev(major, minor) -> device number\n\
5236Composes a raw device number from the major and minor device numbers.");
5237
5238static PyObject *
5239posix_makedev(PyObject *self, PyObject *args)
5240{
Victor Stinner8c62be82010-05-06 00:08:46 +00005241 int major, minor;
5242 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
5243 return NULL;
5244 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00005245}
5246#endif /* device macros */
5247
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005248
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005249#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005250PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005251"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005252Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005253
Barry Warsaw53699e91996-12-10 23:23:01 +00005254static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005255posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005256{
Victor Stinner8c62be82010-05-06 00:08:46 +00005257 int fd;
5258 off_t length;
5259 int res;
5260 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005261
Victor Stinner8c62be82010-05-06 00:08:46 +00005262 if (!PyArg_ParseTuple(args, "iO:ftruncate", &fd, &lenobj))
5263 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00005264
5265#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005266 length = PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005267#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005268 length = PyLong_Check(lenobj) ?
5269 PyLong_AsLongLong(lenobj) : PyLong_AsLong(lenobj);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005270#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005271 if (PyErr_Occurred())
5272 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005273
Victor Stinner8c62be82010-05-06 00:08:46 +00005274 Py_BEGIN_ALLOW_THREADS
5275 res = ftruncate(fd, length);
5276 Py_END_ALLOW_THREADS
5277 if (res < 0)
5278 return posix_error();
5279 Py_INCREF(Py_None);
5280 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00005281}
5282#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00005283
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005284#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005285PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005286"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005287Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005288
Fred Drake762e2061999-08-26 17:23:54 +00005289/* Save putenv() parameters as values here, so we can collect them when they
5290 * get re-set with another call for the same key. */
5291static PyObject *posix_putenv_garbage;
5292
Tim Peters5aa91602002-01-30 05:46:57 +00005293static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005294posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005295{
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005296#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005297 wchar_t *s1, *s2;
5298 wchar_t *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005299#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005300 PyObject *os1, *os2;
5301 char *s1, *s2;
5302 char *newenv;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005303#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005304 PyObject *newstr;
5305 size_t len;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005306
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005307#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005308 if (!PyArg_ParseTuple(args,
5309 "uu:putenv",
5310 &s1, &s2))
5311 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005312#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005313 if (!PyArg_ParseTuple(args,
5314 "O&O&:putenv",
5315 PyUnicode_FSConverter, &os1,
5316 PyUnicode_FSConverter, &os2))
5317 return NULL;
5318 s1 = PyBytes_AsString(os1);
5319 s2 = PyBytes_AsString(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005320#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00005321
5322#if defined(PYOS_OS2)
5323 if (stricmp(s1, "BEGINLIBPATH") == 0) {
5324 APIRET rc;
5325
Guido van Rossumd48f2521997-12-05 22:19:34 +00005326 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
5327 if (rc != NO_ERROR)
5328 return os2_error(rc);
5329
5330 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
5331 APIRET rc;
5332
Guido van Rossumd48f2521997-12-05 22:19:34 +00005333 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
5334 if (rc != NO_ERROR)
5335 return os2_error(rc);
5336 } else {
5337#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005338 /* XXX This can leak memory -- not easy to fix :-( */
5339 /* len includes space for a trailing \0; the size arg to
5340 PyBytes_FromStringAndSize does not count that */
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005341#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005342 len = wcslen(s1) + wcslen(s2) + 2;
5343 newstr = PyUnicode_FromUnicode(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005344#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005345 len = strlen(s1) + strlen(s2) + 2;
5346 newstr = PyBytes_FromStringAndSize(NULL, (int)len - 1);
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005347#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005348 if (newstr == NULL)
5349 return PyErr_NoMemory();
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005350#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005351 newenv = PyUnicode_AsUnicode(newstr);
5352 _snwprintf(newenv, len, L"%s=%s", s1, s2);
5353 if (_wputenv(newenv)) {
5354 Py_DECREF(newstr);
5355 posix_error();
5356 return NULL;
5357 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005358#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005359 newenv = PyBytes_AS_STRING(newstr);
5360 PyOS_snprintf(newenv, len, "%s=%s", s1, s2);
5361 if (putenv(newenv)) {
5362 Py_DECREF(newstr);
5363 Py_DECREF(os1);
5364 Py_DECREF(os2);
5365 posix_error();
5366 return NULL;
5367 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00005368#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005369 /* Install the first arg and newstr in posix_putenv_garbage;
5370 * this will cause previous value to be collected. This has to
5371 * happen after the real putenv() call because the old value
5372 * was still accessible until then. */
5373 if (PyDict_SetItem(posix_putenv_garbage,
5374 PyTuple_GET_ITEM(args, 0), newstr)) {
5375 /* really not much we can do; just leak */
5376 PyErr_Clear();
5377 }
5378 else {
5379 Py_DECREF(newstr);
5380 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00005381
5382#if defined(PYOS_OS2)
5383 }
5384#endif
Martin v. Löwis011e8422009-05-05 04:43:17 +00005385#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 Py_DECREF(os1);
5387 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00005388#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005389 Py_INCREF(Py_None);
5390 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005391}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005392#endif /* putenv */
5393
Guido van Rossumc524d952001-10-19 01:31:59 +00005394#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005395PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005396"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005397Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00005398
5399static PyObject *
5400posix_unsetenv(PyObject *self, PyObject *args)
5401{
Victor Stinner8c62be82010-05-06 00:08:46 +00005402 char *s1;
Guido van Rossumc524d952001-10-19 01:31:59 +00005403
Victor Stinner8c62be82010-05-06 00:08:46 +00005404 if (!PyArg_ParseTuple(args, "s:unsetenv", &s1))
5405 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00005406
Victor Stinner8c62be82010-05-06 00:08:46 +00005407 unsetenv(s1);
Guido van Rossumc524d952001-10-19 01:31:59 +00005408
Victor Stinner8c62be82010-05-06 00:08:46 +00005409 /* Remove the key from posix_putenv_garbage;
5410 * this will cause it to be collected. This has to
5411 * happen after the real unsetenv() call because the
5412 * old value was still accessible until then.
5413 */
5414 if (PyDict_DelItem(posix_putenv_garbage,
5415 PyTuple_GET_ITEM(args, 0))) {
5416 /* really not much we can do; just leak */
5417 PyErr_Clear();
5418 }
Guido van Rossumc524d952001-10-19 01:31:59 +00005419
Victor Stinner8c62be82010-05-06 00:08:46 +00005420 Py_INCREF(Py_None);
5421 return Py_None;
Guido van Rossumc524d952001-10-19 01:31:59 +00005422}
5423#endif /* unsetenv */
5424
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005425PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005426"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005427Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00005428
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005429static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005430posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00005431{
Victor Stinner8c62be82010-05-06 00:08:46 +00005432 int code;
5433 char *message;
5434 if (!PyArg_ParseTuple(args, "i:strerror", &code))
5435 return NULL;
5436 message = strerror(code);
5437 if (message == NULL) {
5438 PyErr_SetString(PyExc_ValueError,
5439 "strerror() argument out of range");
5440 return NULL;
5441 }
5442 return PyUnicode_FromString(message);
Guido van Rossumb6a47161997-09-15 22:54:34 +00005443}
Guido van Rossumb6a47161997-09-15 22:54:34 +00005444
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00005445
Guido van Rossumc9641791998-08-04 15:26:23 +00005446#ifdef HAVE_SYS_WAIT_H
5447
Fred Drake106c1a02002-04-23 15:58:02 +00005448#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005449PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005450"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005451Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00005452
5453static PyObject *
5454posix_WCOREDUMP(PyObject *self, PyObject *args)
5455{
Victor Stinner8c62be82010-05-06 00:08:46 +00005456 WAIT_TYPE status;
5457 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005458
Victor Stinner8c62be82010-05-06 00:08:46 +00005459 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
5460 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005461
Victor Stinner8c62be82010-05-06 00:08:46 +00005462 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005463}
5464#endif /* WCOREDUMP */
5465
5466#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005467PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005468"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00005469Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005470job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00005471
5472static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00005473posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00005474{
Victor Stinner8c62be82010-05-06 00:08:46 +00005475 WAIT_TYPE status;
5476 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00005477
Victor Stinner8c62be82010-05-06 00:08:46 +00005478 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
5479 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00005480
Victor Stinner8c62be82010-05-06 00:08:46 +00005481 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00005482}
5483#endif /* WIFCONTINUED */
5484
Guido van Rossumc9641791998-08-04 15:26:23 +00005485#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005486PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005487"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005488Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005489
5490static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005491posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005492{
Victor Stinner8c62be82010-05-06 00:08:46 +00005493 WAIT_TYPE status;
5494 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005495
Victor Stinner8c62be82010-05-06 00:08:46 +00005496 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
5497 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005498
Victor Stinner8c62be82010-05-06 00:08:46 +00005499 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005500}
5501#endif /* WIFSTOPPED */
5502
5503#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005504PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005505"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005506Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005507
5508static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005509posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005510{
Victor Stinner8c62be82010-05-06 00:08:46 +00005511 WAIT_TYPE status;
5512 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005513
Victor Stinner8c62be82010-05-06 00:08:46 +00005514 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
5515 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005516
Victor Stinner8c62be82010-05-06 00:08:46 +00005517 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005518}
5519#endif /* WIFSIGNALED */
5520
5521#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005522PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005523"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005524Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005525system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005526
5527static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005528posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005529{
Victor Stinner8c62be82010-05-06 00:08:46 +00005530 WAIT_TYPE status;
5531 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005532
Victor Stinner8c62be82010-05-06 00:08:46 +00005533 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
5534 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005535
Victor Stinner8c62be82010-05-06 00:08:46 +00005536 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005537}
5538#endif /* WIFEXITED */
5539
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00005540#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005541PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005542"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005543Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005544
5545static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005546posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005547{
Victor Stinner8c62be82010-05-06 00:08:46 +00005548 WAIT_TYPE status;
5549 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005550
Victor Stinner8c62be82010-05-06 00:08:46 +00005551 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
5552 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005553
Victor Stinner8c62be82010-05-06 00:08:46 +00005554 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005555}
5556#endif /* WEXITSTATUS */
5557
5558#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005559PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005560"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00005561Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005562value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005563
5564static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005565posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005566{
Victor Stinner8c62be82010-05-06 00:08:46 +00005567 WAIT_TYPE status;
5568 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005569
Victor Stinner8c62be82010-05-06 00:08:46 +00005570 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
5571 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005572
Victor Stinner8c62be82010-05-06 00:08:46 +00005573 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005574}
5575#endif /* WTERMSIG */
5576
5577#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005578PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005579"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005580Return the signal that stopped the process that provided\n\
5581the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00005582
5583static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005584posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00005585{
Victor Stinner8c62be82010-05-06 00:08:46 +00005586 WAIT_TYPE status;
5587 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00005588
Victor Stinner8c62be82010-05-06 00:08:46 +00005589 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
5590 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00005591
Victor Stinner8c62be82010-05-06 00:08:46 +00005592 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00005593}
5594#endif /* WSTOPSIG */
5595
5596#endif /* HAVE_SYS_WAIT_H */
5597
5598
Thomas Wouters477c8d52006-05-27 19:21:47 +00005599#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00005600#ifdef _SCO_DS
5601/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
5602 needed definitions in sys/statvfs.h */
5603#define _SVID3
5604#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00005605#include <sys/statvfs.h>
5606
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005607static PyObject*
5608_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005609 PyObject *v = PyStructSequence_New(&StatVFSResultType);
5610 if (v == NULL)
5611 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005612
5613#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00005614 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5615 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5616 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
5617 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
5618 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
5619 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
5620 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
5621 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
5622 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5623 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005624#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005625 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
5626 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
5627 PyStructSequence_SET_ITEM(v, 2,
5628 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
5629 PyStructSequence_SET_ITEM(v, 3,
5630 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
5631 PyStructSequence_SET_ITEM(v, 4,
5632 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
5633 PyStructSequence_SET_ITEM(v, 5,
5634 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
5635 PyStructSequence_SET_ITEM(v, 6,
5636 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
5637 PyStructSequence_SET_ITEM(v, 7,
5638 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
5639 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
5640 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005641#endif
5642
Victor Stinner8c62be82010-05-06 00:08:46 +00005643 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005644}
5645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005646PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005647"fstatvfs(fd) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005648Perform an fstatvfs system call on the given fd.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005649
5650static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005651posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005652{
Victor Stinner8c62be82010-05-06 00:08:46 +00005653 int fd, res;
5654 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005655
Victor Stinner8c62be82010-05-06 00:08:46 +00005656 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
5657 return NULL;
5658 Py_BEGIN_ALLOW_THREADS
5659 res = fstatvfs(fd, &st);
5660 Py_END_ALLOW_THREADS
5661 if (res != 0)
5662 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005663
Victor Stinner8c62be82010-05-06 00:08:46 +00005664 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005665}
Thomas Wouters477c8d52006-05-27 19:21:47 +00005666#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00005667
5668
Thomas Wouters477c8d52006-05-27 19:21:47 +00005669#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005670#include <sys/statvfs.h>
5671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005672PyDoc_STRVAR(posix_statvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005673"statvfs(path) -> statvfs result\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005674Perform a statvfs system call on the given path.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00005675
5676static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005677posix_statvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00005678{
Victor Stinner8c62be82010-05-06 00:08:46 +00005679 char *path;
5680 int res;
5681 struct statvfs st;
5682 if (!PyArg_ParseTuple(args, "s:statvfs", &path))
5683 return NULL;
5684 Py_BEGIN_ALLOW_THREADS
5685 res = statvfs(path, &st);
5686 Py_END_ALLOW_THREADS
5687 if (res != 0)
5688 return posix_error_with_filename(path);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005689
Victor Stinner8c62be82010-05-06 00:08:46 +00005690 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00005691}
5692#endif /* HAVE_STATVFS */
5693
Fred Drakec9680921999-12-13 16:37:25 +00005694/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
5695 * It maps strings representing configuration variable names to
5696 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00005697 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00005698 * rarely-used constants. There are three separate tables that use
5699 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00005700 *
5701 * This code is always included, even if none of the interfaces that
5702 * need it are included. The #if hackery needed to avoid it would be
5703 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00005704 */
5705struct constdef {
5706 char *name;
5707 long value;
5708};
5709
Fred Drake12c6e2d1999-12-14 21:25:03 +00005710static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005711conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00005712 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00005713{
Christian Heimes217cfd12007-12-02 14:31:20 +00005714 if (PyLong_Check(arg)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005715 *valuep = PyLong_AS_LONG(arg);
5716 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005717 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00005718 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00005719 /* look up the value in the table using a binary search */
5720 size_t lo = 0;
5721 size_t mid;
5722 size_t hi = tablesize;
5723 int cmp;
5724 const char *confname;
5725 if (!PyUnicode_Check(arg)) {
5726 PyErr_SetString(PyExc_TypeError,
5727 "configuration names must be strings or integers");
Guido van Rossumbce56a62007-05-10 18:04:33 +00005728 return 0;
Fred Drake12c6e2d1999-12-14 21:25:03 +00005729 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005730 confname = _PyUnicode_AsString(arg);
5731 if (confname == NULL)
5732 return 0;
5733 while (lo < hi) {
5734 mid = (lo + hi) / 2;
5735 cmp = strcmp(confname, table[mid].name);
5736 if (cmp < 0)
5737 hi = mid;
5738 else if (cmp > 0)
5739 lo = mid + 1;
5740 else {
5741 *valuep = table[mid].value;
5742 return 1;
5743 }
5744 }
5745 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
5746 return 0;
5747 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00005748}
5749
5750
5751#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
5752static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005753#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005754 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00005755#endif
5756#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005757 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00005758#endif
Fred Drakec9680921999-12-13 16:37:25 +00005759#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005760 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005761#endif
5762#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00005763 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00005764#endif
5765#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00005766 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00005767#endif
5768#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00005769 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00005770#endif
5771#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005772 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005773#endif
5774#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00005775 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00005776#endif
5777#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00005778 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00005779#endif
5780#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005781 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005782#endif
5783#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00005784 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00005785#endif
5786#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00005788#endif
5789#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005790 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00005791#endif
5792#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005793 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005794#endif
5795#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00005797#endif
5798#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00005799 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00005800#endif
5801#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00005802 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00005803#endif
5804};
5805
Fred Drakec9680921999-12-13 16:37:25 +00005806static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005807conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00005808{
5809 return conv_confname(arg, valuep, posix_constants_pathconf,
5810 sizeof(posix_constants_pathconf)
5811 / sizeof(struct constdef));
5812}
5813#endif
5814
5815#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005816PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005817"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005818Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005819If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005820
5821static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005822posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005823{
5824 PyObject *result = NULL;
5825 int name, fd;
5826
Fred Drake12c6e2d1999-12-14 21:25:03 +00005827 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
5828 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005829 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005830
Victor Stinner8c62be82010-05-06 00:08:46 +00005831 errno = 0;
5832 limit = fpathconf(fd, name);
5833 if (limit == -1 && errno != 0)
5834 posix_error();
5835 else
5836 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005837 }
5838 return result;
5839}
5840#endif
5841
5842
5843#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005844PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005845"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00005846Return the configuration limit name for the file or directory path.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005847If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00005848
5849static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005850posix_pathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00005851{
5852 PyObject *result = NULL;
5853 int name;
5854 char *path;
5855
5856 if (PyArg_ParseTuple(args, "sO&:pathconf", &path,
5857 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005858 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00005859
Victor Stinner8c62be82010-05-06 00:08:46 +00005860 errno = 0;
5861 limit = pathconf(path, name);
5862 if (limit == -1 && errno != 0) {
5863 if (errno == EINVAL)
5864 /* could be a path or name problem */
5865 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00005866 else
Victor Stinner8c62be82010-05-06 00:08:46 +00005867 posix_error_with_filename(path);
5868 }
5869 else
5870 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00005871 }
5872 return result;
5873}
5874#endif
5875
5876#ifdef HAVE_CONFSTR
5877static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00005878#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00005879 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00005880#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00005881#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005882 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005883#endif
5884#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005885 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00005886#endif
Fred Draked86ed291999-12-15 15:34:33 +00005887#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005888 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005889#endif
5890#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00005891 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00005892#endif
5893#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00005894 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00005895#endif
5896#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005897 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00005898#endif
Fred Drakec9680921999-12-13 16:37:25 +00005899#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005900 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005901#endif
5902#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005903 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005904#endif
5905#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005906 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005907#endif
5908#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005909 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005910#endif
5911#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005913#endif
5914#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005915 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005916#endif
5917#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005918 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005919#endif
5920#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005922#endif
Fred Draked86ed291999-12-15 15:34:33 +00005923#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00005924 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00005925#endif
Fred Drakec9680921999-12-13 16:37:25 +00005926#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00005928#endif
Fred Draked86ed291999-12-15 15:34:33 +00005929#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00005930 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00005931#endif
5932#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00005933 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00005934#endif
5935#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00005937#endif
5938#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00005940#endif
Fred Drakec9680921999-12-13 16:37:25 +00005941#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005942 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005943#endif
5944#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005945 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005946#endif
5947#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005948 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005949#endif
5950#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005951 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005952#endif
5953#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005954 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005955#endif
5956#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005957 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005958#endif
5959#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005960 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005961#endif
5962#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005963 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005964#endif
5965#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005966 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005967#endif
5968#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005969 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005970#endif
5971#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005972 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005973#endif
5974#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005975 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005976#endif
5977#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005978 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005979#endif
5980#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005981 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005982#endif
5983#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00005984 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00005985#endif
5986#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00005988#endif
Fred Draked86ed291999-12-15 15:34:33 +00005989#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00005991#endif
5992#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00005993 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00005994#endif
5995#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00005996 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00005997#endif
5998#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00005999 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006000#endif
6001#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006002 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006003#endif
6004#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00006006#endif
6007#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006008 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00006009#endif
6010#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00006011 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00006012#endif
6013#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00006014 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00006015#endif
6016#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00006017 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00006018#endif
6019#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00006021#endif
6022#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00006023 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00006024#endif
6025#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00006026 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00006027#endif
Fred Drakec9680921999-12-13 16:37:25 +00006028};
6029
6030static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006031conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006032{
6033 return conv_confname(arg, valuep, posix_constants_confstr,
6034 sizeof(posix_constants_confstr)
6035 / sizeof(struct constdef));
6036}
6037
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006038PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006039"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006040Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006041
6042static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006043posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006044{
6045 PyObject *result = NULL;
6046 int name;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006047 char buffer[256];
Fred Drakec9680921999-12-13 16:37:25 +00006048
6049 if (PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006050 int len;
Fred Drakec9680921999-12-13 16:37:25 +00006051
Fred Drakec9680921999-12-13 16:37:25 +00006052 errno = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006053 len = confstr(name, buffer, sizeof(buffer));
6054 if (len == 0) {
6055 if (errno) {
6056 posix_error();
6057 }
6058 else {
6059 result = Py_None;
6060 Py_INCREF(Py_None);
6061 }
Fred Drakec9680921999-12-13 16:37:25 +00006062 }
6063 else {
Victor Stinner8c62be82010-05-06 00:08:46 +00006064 if ((unsigned int)len >= sizeof(buffer)) {
Neal Norwitz93c56822007-08-26 07:10:06 +00006065 result = PyUnicode_FromStringAndSize(NULL, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006066 if (result != NULL)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00006067 confstr(name, _PyUnicode_AsString(result), len);
Fred Drakec9680921999-12-13 16:37:25 +00006068 }
6069 else
Neal Norwitz93c56822007-08-26 07:10:06 +00006070 result = PyUnicode_FromStringAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00006071 }
6072 }
6073 return result;
6074}
6075#endif
6076
6077
6078#ifdef HAVE_SYSCONF
6079static struct constdef posix_constants_sysconf[] = {
6080#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00006081 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00006082#endif
6083#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00006084 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00006085#endif
6086#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006087 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006088#endif
6089#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006090 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006091#endif
6092#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006093 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006094#endif
6095#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00006096 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00006097#endif
6098#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00006099 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00006100#endif
6101#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00006102 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00006103#endif
6104#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00006105 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00006106#endif
6107#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006108 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006109#endif
Fred Draked86ed291999-12-15 15:34:33 +00006110#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00006112#endif
6113#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00006114 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00006115#endif
Fred Drakec9680921999-12-13 16:37:25 +00006116#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006117 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006118#endif
Fred Drakec9680921999-12-13 16:37:25 +00006119#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006120 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006121#endif
6122#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006123 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006124#endif
6125#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006126 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006127#endif
6128#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006129 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006130#endif
6131#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006132 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006133#endif
Fred Draked86ed291999-12-15 15:34:33 +00006134#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00006136#endif
Fred Drakec9680921999-12-13 16:37:25 +00006137#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006139#endif
6140#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006141 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006142#endif
6143#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006144 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006145#endif
6146#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006147 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006148#endif
6149#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006150 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006151#endif
Fred Draked86ed291999-12-15 15:34:33 +00006152#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00006154#endif
Fred Drakec9680921999-12-13 16:37:25 +00006155#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006156 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006157#endif
6158#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006159 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006160#endif
6161#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006162 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006163#endif
6164#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006165 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006166#endif
6167#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006168 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006169#endif
6170#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006171 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00006172#endif
6173#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006174 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006175#endif
6176#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006177 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006178#endif
6179#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006180 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006181#endif
6182#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006183 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006184#endif
6185#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006186 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006187#endif
6188#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006189 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006190#endif
6191#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006192 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006193#endif
6194#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006195 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006196#endif
6197#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006198 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006199#endif
6200#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006201 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006202#endif
6203#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00006204 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00006205#endif
6206#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006207 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006208#endif
6209#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006210 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006211#endif
6212#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00006213 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00006214#endif
6215#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006216 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00006217#endif
6218#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006219 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00006220#endif
6221#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00006222 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00006223#endif
Fred Draked86ed291999-12-15 15:34:33 +00006224#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00006225 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00006226#endif
Fred Drakec9680921999-12-13 16:37:25 +00006227#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006228 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006229#endif
6230#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006231 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006232#endif
6233#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006234 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006235#endif
Fred Draked86ed291999-12-15 15:34:33 +00006236#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006237 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00006238#endif
Fred Drakec9680921999-12-13 16:37:25 +00006239#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00006240 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00006241#endif
Fred Draked86ed291999-12-15 15:34:33 +00006242#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006243 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00006244#endif
6245#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00006246 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00006247#endif
Fred Drakec9680921999-12-13 16:37:25 +00006248#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006249 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006250#endif
6251#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006252 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006253#endif
6254#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006255 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006256#endif
6257#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006258 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006259#endif
Fred Draked86ed291999-12-15 15:34:33 +00006260#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00006261 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00006262#endif
Fred Drakec9680921999-12-13 16:37:25 +00006263#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00006264 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00006265#endif
6266#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00006267 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00006268#endif
6269#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006270 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006271#endif
6272#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00006273 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00006274#endif
6275#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00006276 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00006277#endif
6278#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00006279 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00006280#endif
6281#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00006282 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00006283#endif
Fred Draked86ed291999-12-15 15:34:33 +00006284#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00006285 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00006286#endif
Fred Drakec9680921999-12-13 16:37:25 +00006287#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006288 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006289#endif
6290#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006291 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006292#endif
Fred Draked86ed291999-12-15 15:34:33 +00006293#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006294 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00006295#endif
Fred Drakec9680921999-12-13 16:37:25 +00006296#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006297 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006298#endif
6299#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006300 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006301#endif
6302#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006303 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006304#endif
6305#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006306 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006307#endif
6308#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006309 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006310#endif
6311#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006312 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006313#endif
6314#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006315 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00006316#endif
6317#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006318 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00006319#endif
6320#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006321 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00006322#endif
Fred Draked86ed291999-12-15 15:34:33 +00006323#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00006324 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00006325#endif
6326#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00006327 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00006328#endif
Fred Drakec9680921999-12-13 16:37:25 +00006329#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00006330 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00006331#endif
6332#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006333 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006334#endif
6335#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006336 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006337#endif
6338#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006339 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006340#endif
6341#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006342 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006343#endif
6344#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00006345 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00006346#endif
6347#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00006348 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00006349#endif
6350#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00006351 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00006352#endif
6353#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006354 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00006355#endif
6356#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00006357 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00006358#endif
6359#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00006360 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00006361#endif
6362#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006363 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00006364#endif
6365#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00006367#endif
6368#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00006369 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00006370#endif
6371#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00006372 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00006373#endif
6374#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00006375 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00006376#endif
6377#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00006378 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00006379#endif
6380#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006381 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006382#endif
6383#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006384 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006385#endif
6386#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00006387 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00006388#endif
6389#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006390 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006391#endif
6392#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006393 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006394#endif
6395#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00006396 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00006397#endif
6398#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006399 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006400#endif
6401#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006403#endif
6404#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006405 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00006406#endif
6407#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00006408 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00006409#endif
6410#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006411 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006412#endif
6413#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006414 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006415#endif
6416#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00006417 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00006418#endif
6419#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006420 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006421#endif
6422#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006423 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006424#endif
6425#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006426 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006427#endif
6428#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006429 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006430#endif
6431#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006432 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006433#endif
Fred Draked86ed291999-12-15 15:34:33 +00006434#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00006435 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00006436#endif
Fred Drakec9680921999-12-13 16:37:25 +00006437#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00006438 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00006439#endif
6440#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006441 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006442#endif
6443#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00006444 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00006445#endif
6446#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006447 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006448#endif
6449#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00006450 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00006451#endif
6452#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006453 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006454#endif
6455#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00006456 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00006457#endif
6458#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00006459 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00006460#endif
6461#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006462 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006463#endif
6464#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006465 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006466#endif
6467#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00006468 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00006469#endif
6470#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006471 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00006472#endif
6473#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00006474 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00006475#endif
6476#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00006477 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00006478#endif
6479#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00006480 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00006481#endif
6482#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00006483 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00006484#endif
6485#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006486 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006487#endif
6488#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00006490#endif
6491#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006493#endif
6494#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006495 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006496#endif
6497#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006498 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006499#endif
6500#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006501 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006502#endif
6503#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006504 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006505#endif
6506#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006507 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006508#endif
6509#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00006510 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00006511#endif
6512#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006513 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006514#endif
6515#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00006516 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00006517#endif
6518#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006519 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006520#endif
6521#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00006522 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00006523#endif
6524#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00006525 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00006526#endif
6527#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006528 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006529#endif
6530#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00006531 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00006532#endif
6533#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00006534 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00006535#endif
6536#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00006537 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00006538#endif
6539#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00006540 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00006541#endif
6542#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00006543 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00006544#endif
6545#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00006546 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00006547#endif
6548#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00006549 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00006550#endif
6551#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00006552 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00006553#endif
6554#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00006555 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00006556#endif
6557#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006558 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006559#endif
6560#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00006561 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00006562#endif
6563#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00006564 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00006565#endif
6566#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00006567 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00006568#endif
6569#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00006570 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00006571#endif
6572};
6573
6574static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006575conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00006576{
6577 return conv_confname(arg, valuep, posix_constants_sysconf,
6578 sizeof(posix_constants_sysconf)
6579 / sizeof(struct constdef));
6580}
6581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006582PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006583"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006584Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00006585
6586static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006587posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00006588{
6589 PyObject *result = NULL;
6590 int name;
6591
6592 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
6593 int value;
6594
6595 errno = 0;
6596 value = sysconf(name);
6597 if (value == -1 && errno != 0)
6598 posix_error();
6599 else
Christian Heimes217cfd12007-12-02 14:31:20 +00006600 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00006601 }
6602 return result;
6603}
6604#endif
6605
6606
Fred Drakebec628d1999-12-15 18:31:10 +00006607/* This code is used to ensure that the tables of configuration value names
6608 * are in sorted order as required by conv_confname(), and also to build the
6609 * the exported dictionaries that are used to publish information about the
6610 * names available on the host platform.
6611 *
6612 * Sorting the table at runtime ensures that the table is properly ordered
6613 * when used, even for platforms we're not able to test on. It also makes
6614 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00006615 */
Fred Drakebec628d1999-12-15 18:31:10 +00006616
6617static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006618cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00006619{
6620 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006621 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00006622 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00006623 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00006624
6625 return strcmp(c1->name, c2->name);
6626}
6627
6628static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006629setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00006630 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006631{
Fred Drakebec628d1999-12-15 18:31:10 +00006632 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00006633 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00006634
6635 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
6636 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00006637 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00006638 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006639
Barry Warsaw3155db32000-04-13 15:20:40 +00006640 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006641 PyObject *o = PyLong_FromLong(table[i].value);
6642 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
6643 Py_XDECREF(o);
6644 Py_DECREF(d);
6645 return -1;
6646 }
6647 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00006648 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00006649 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00006650}
6651
Fred Drakebec628d1999-12-15 18:31:10 +00006652/* Return -1 on failure, 0 on success. */
6653static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00006654setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00006655{
6656#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00006657 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00006658 sizeof(posix_constants_pathconf)
6659 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006660 "pathconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006662#endif
6663#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00006664 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00006665 sizeof(posix_constants_confstr)
6666 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006667 "confstr_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006668 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006669#endif
6670#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00006671 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00006672 sizeof(posix_constants_sysconf)
6673 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00006674 "sysconf_names", module))
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00006676#endif
Fred Drakebec628d1999-12-15 18:31:10 +00006677 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00006678}
Fred Draked86ed291999-12-15 15:34:33 +00006679
6680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006681PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006682"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006683Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006684in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006685
6686static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006687posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006688{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006689 abort();
6690 /*NOTREACHED*/
6691 Py_FatalError("abort() called from Python code didn't abort!");
6692 return NULL;
6693}
Fred Drakebec628d1999-12-15 18:31:10 +00006694
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00006695#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006696PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00006697"startfile(filepath [, operation]) - Start a file with its associated\n\
6698application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006699\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00006700When \"operation\" is not specified or \"open\", this acts like\n\
6701double-clicking the file in Explorer, or giving the file name as an\n\
6702argument to the DOS \"start\" command: the file is opened with whatever\n\
6703application (if any) its extension is associated.\n\
6704When another \"operation\" is given, it specifies what should be done with\n\
6705the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00006706\n\
6707startfile returns as soon as the associated application is launched.\n\
6708There is no option to wait for the application to close, and no way\n\
6709to retrieve the application's exit status.\n\
6710\n\
6711The filepath is relative to the current directory. If you want to use\n\
6712an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006713the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00006714
6715static PyObject *
6716win32_startfile(PyObject *self, PyObject *args)
6717{
Victor Stinner8c62be82010-05-06 00:08:46 +00006718 PyObject *ofilepath;
6719 char *filepath;
6720 char *operation = NULL;
6721 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00006722
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 PyObject *unipath, *woperation = NULL;
6724 if (!PyArg_ParseTuple(args, "U|s:startfile",
6725 &unipath, &operation)) {
6726 PyErr_Clear();
6727 goto normal;
6728 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006729
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 if (operation) {
6731 woperation = PyUnicode_DecodeASCII(operation,
6732 strlen(operation), NULL);
6733 if (!woperation) {
6734 PyErr_Clear();
6735 operation = NULL;
6736 goto normal;
6737 }
6738 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00006739
Victor Stinner8c62be82010-05-06 00:08:46 +00006740 Py_BEGIN_ALLOW_THREADS
6741 rc = ShellExecuteW((HWND)0, woperation ? PyUnicode_AS_UNICODE(woperation) : 0,
6742 PyUnicode_AS_UNICODE(unipath),
6743 NULL, NULL, SW_SHOWNORMAL);
6744 Py_END_ALLOW_THREADS
6745
6746 Py_XDECREF(woperation);
6747 if (rc <= (HINSTANCE)32) {
6748 PyObject *errval = win32_error_unicode("startfile",
6749 PyUnicode_AS_UNICODE(unipath));
6750 return errval;
6751 }
6752 Py_INCREF(Py_None);
6753 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006754
6755normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00006756 if (!PyArg_ParseTuple(args, "O&|s:startfile",
6757 PyUnicode_FSConverter, &ofilepath,
6758 &operation))
6759 return NULL;
6760 filepath = PyBytes_AsString(ofilepath);
6761 Py_BEGIN_ALLOW_THREADS
6762 rc = ShellExecute((HWND)0, operation, filepath,
6763 NULL, NULL, SW_SHOWNORMAL);
6764 Py_END_ALLOW_THREADS
6765 if (rc <= (HINSTANCE)32) {
6766 PyObject *errval = win32_error("startfile", filepath);
6767 Py_DECREF(ofilepath);
6768 return errval;
6769 }
6770 Py_DECREF(ofilepath);
6771 Py_INCREF(Py_None);
6772 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00006773}
6774#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00006775
Martin v. Löwis438b5342002-12-27 10:16:42 +00006776#ifdef HAVE_GETLOADAVG
6777PyDoc_STRVAR(posix_getloadavg__doc__,
6778"getloadavg() -> (float, float, float)\n\n\
6779Return the number of processes in the system run queue averaged over\n\
6780the last 1, 5, and 15 minutes or raises OSError if the load average\n\
6781was unobtainable");
6782
6783static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006784posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00006785{
6786 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00006787 if (getloadavg(loadavg, 3)!=3) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
6789 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00006790 } else
Victor Stinner8c62be82010-05-06 00:08:46 +00006791 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00006792}
6793#endif
6794
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006795#ifdef MS_WINDOWS
6796
6797PyDoc_STRVAR(win32_urandom__doc__,
6798"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006799Return n random bytes suitable for cryptographic use.");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006800
6801typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
6802 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
6803 DWORD dwFlags );
6804typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
6805 BYTE *pbBuffer );
6806
6807static CRYPTGENRANDOM pCryptGenRandom = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +00006808/* This handle is never explicitly released. Instead, the operating
6809 system will release it when the process terminates. */
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006810static HCRYPTPROV hCryptProv = 0;
6811
Tim Peters4ad82172004-08-30 17:02:04 +00006812static PyObject*
6813win32_urandom(PyObject *self, PyObject *args)
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006814{
Victor Stinner8c62be82010-05-06 00:08:46 +00006815 int howMany;
6816 PyObject* result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006817
Victor Stinner8c62be82010-05-06 00:08:46 +00006818 /* Read arguments */
6819 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6820 return NULL;
6821 if (howMany < 0)
6822 return PyErr_Format(PyExc_ValueError,
6823 "negative argument not allowed");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006824
Victor Stinner8c62be82010-05-06 00:08:46 +00006825 if (hCryptProv == 0) {
6826 HINSTANCE hAdvAPI32 = NULL;
6827 CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006828
Victor Stinner8c62be82010-05-06 00:08:46 +00006829 /* Obtain handle to the DLL containing CryptoAPI
6830 This should not fail */
6831 hAdvAPI32 = GetModuleHandle("advapi32.dll");
6832 if(hAdvAPI32 == NULL)
6833 return win32_error("GetModuleHandle", NULL);
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006834
Victor Stinner8c62be82010-05-06 00:08:46 +00006835 /* Obtain pointers to the CryptoAPI functions
6836 This will fail on some early versions of Win95 */
6837 pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
6838 hAdvAPI32,
6839 "CryptAcquireContextA");
6840 if (pCryptAcquireContext == NULL)
6841 return PyErr_Format(PyExc_NotImplementedError,
6842 "CryptAcquireContextA not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006843
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
6845 hAdvAPI32, "CryptGenRandom");
6846 if (pCryptGenRandom == NULL)
6847 return PyErr_Format(PyExc_NotImplementedError,
6848 "CryptGenRandom not found");
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006849
Victor Stinner8c62be82010-05-06 00:08:46 +00006850 /* Acquire context */
6851 if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
6852 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
6853 return win32_error("CryptAcquireContext", NULL);
6854 }
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006855
Victor Stinner8c62be82010-05-06 00:08:46 +00006856 /* Allocate bytes */
6857 result = PyBytes_FromStringAndSize(NULL, howMany);
6858 if (result != NULL) {
6859 /* Get random data */
6860 memset(PyBytes_AS_STRING(result), 0, howMany); /* zero seed */
6861 if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
6862 PyBytes_AS_STRING(result))) {
6863 Py_DECREF(result);
6864 return win32_error("CryptGenRandom", NULL);
6865 }
6866 }
6867 return result;
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00006868}
6869#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00006870
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006871PyDoc_STRVAR(device_encoding__doc__,
6872"device_encoding(fd) -> str\n\n\
6873Return a string describing the encoding of the device\n\
6874if the output is a terminal; else return None.");
6875
6876static PyObject *
6877device_encoding(PyObject *self, PyObject *args)
6878{
Victor Stinner8c62be82010-05-06 00:08:46 +00006879 int fd;
6880 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
6881 return NULL;
6882 if (!_PyVerify_fd(fd) || !isatty(fd)) {
6883 Py_INCREF(Py_None);
6884 return Py_None;
6885 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006886#if defined(MS_WINDOWS) || defined(MS_WIN64)
Victor Stinner8c62be82010-05-06 00:08:46 +00006887 if (fd == 0) {
6888 char buf[100];
6889 sprintf(buf, "cp%d", GetConsoleCP());
6890 return PyUnicode_FromString(buf);
6891 }
6892 if (fd == 1 || fd == 2) {
6893 char buf[100];
6894 sprintf(buf, "cp%d", GetConsoleOutputCP());
6895 return PyUnicode_FromString(buf);
6896 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006897#elif defined(CODESET)
Victor Stinner8c62be82010-05-06 00:08:46 +00006898 {
6899 char *codeset = nl_langinfo(CODESET);
6900 if (codeset != NULL && codeset[0] != 0)
6901 return PyUnicode_FromString(codeset);
6902 }
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006903#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006904 Py_INCREF(Py_None);
6905 return Py_None;
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00006906}
6907
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006908#ifdef __VMS
6909/* Use openssl random routine */
6910#include <openssl/rand.h>
6911PyDoc_STRVAR(vms_urandom__doc__,
6912"urandom(n) -> str\n\n\
Neal Norwitz93c56822007-08-26 07:10:06 +00006913Return n random bytes suitable for cryptographic use.");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006914
6915static PyObject*
6916vms_urandom(PyObject *self, PyObject *args)
6917{
Victor Stinner8c62be82010-05-06 00:08:46 +00006918 int howMany;
6919 PyObject* result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006920
Victor Stinner8c62be82010-05-06 00:08:46 +00006921 /* Read arguments */
6922 if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
6923 return NULL;
6924 if (howMany < 0)
6925 return PyErr_Format(PyExc_ValueError,
6926 "negative argument not allowed");
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006927
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 /* Allocate bytes */
6929 result = PyBytes_FromStringAndSize(NULL, howMany);
6930 if (result != NULL) {
6931 /* Get random data */
6932 if (RAND_pseudo_bytes((unsigned char*)
6933 PyBytes_AS_STRING(result),
6934 howMany) < 0) {
6935 Py_DECREF(result);
6936 return PyErr_Format(PyExc_ValueError,
6937 "RAND_pseudo_bytes");
6938 }
6939 }
6940 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006941}
6942#endif
6943
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006944#ifdef HAVE_SETRESUID
6945PyDoc_STRVAR(posix_setresuid__doc__,
6946"setresuid(ruid, euid, suid)\n\n\
6947Set the current process's real, effective, and saved user ids.");
6948
6949static PyObject*
6950posix_setresuid (PyObject *self, PyObject *args)
6951{
Victor Stinner8c62be82010-05-06 00:08:46 +00006952 /* We assume uid_t is no larger than a long. */
6953 long ruid, euid, suid;
6954 if (!PyArg_ParseTuple(args, "lll", &ruid, &euid, &suid))
6955 return NULL;
6956 if (setresuid(ruid, euid, suid) < 0)
6957 return posix_error();
6958 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006959}
6960#endif
6961
6962#ifdef HAVE_SETRESGID
6963PyDoc_STRVAR(posix_setresgid__doc__,
6964"setresgid(rgid, egid, sgid)\n\n\
6965Set the current process's real, effective, and saved group ids.");
6966
6967static PyObject*
6968posix_setresgid (PyObject *self, PyObject *args)
6969{
Victor Stinner8c62be82010-05-06 00:08:46 +00006970 /* We assume uid_t is no larger than a long. */
6971 long rgid, egid, sgid;
6972 if (!PyArg_ParseTuple(args, "lll", &rgid, &egid, &sgid))
6973 return NULL;
6974 if (setresgid(rgid, egid, sgid) < 0)
6975 return posix_error();
6976 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006977}
6978#endif
6979
6980#ifdef HAVE_GETRESUID
6981PyDoc_STRVAR(posix_getresuid__doc__,
6982"getresuid() -> (ruid, euid, suid)\n\n\
6983Get tuple of the current process's real, effective, and saved user ids.");
6984
6985static PyObject*
6986posix_getresuid (PyObject *self, PyObject *noargs)
6987{
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 uid_t ruid, euid, suid;
6989 long l_ruid, l_euid, l_suid;
6990 if (getresuid(&ruid, &euid, &suid) < 0)
6991 return posix_error();
6992 /* Force the values into long's as we don't know the size of uid_t. */
6993 l_ruid = ruid;
6994 l_euid = euid;
6995 l_suid = suid;
6996 return Py_BuildValue("(lll)", l_ruid, l_euid, l_suid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00006997}
6998#endif
6999
7000#ifdef HAVE_GETRESGID
7001PyDoc_STRVAR(posix_getresgid__doc__,
7002"getresgid() -> (rgid, egid, sgid)\n\n\
7003Get tuple of the current process's real, effective, and saved user ids.");
7004
7005static PyObject*
7006posix_getresgid (PyObject *self, PyObject *noargs)
7007{
Victor Stinner8c62be82010-05-06 00:08:46 +00007008 uid_t rgid, egid, sgid;
7009 long l_rgid, l_egid, l_sgid;
7010 if (getresgid(&rgid, &egid, &sgid) < 0)
7011 return posix_error();
7012 /* Force the values into long's as we don't know the size of uid_t. */
7013 l_rgid = rgid;
7014 l_egid = egid;
7015 l_sgid = sgid;
7016 return Py_BuildValue("(lll)", l_rgid, l_egid, l_sgid);
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007017}
7018#endif
7019
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007020static PyMethodDef posix_methods[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 {"access", posix_access, METH_VARARGS, posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007022#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007023 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007024#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 {"chdir", posix_chdir, METH_VARARGS, posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007026#ifdef HAVE_CHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007027 {"chflags", posix_chflags, METH_VARARGS, posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007028#endif /* HAVE_CHFLAGS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007029 {"chmod", posix_chmod, METH_VARARGS, posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007030#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007031 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007032#endif /* HAVE_FCHMOD */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007033#ifdef HAVE_CHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007034 {"chown", posix_chown, METH_VARARGS, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007035#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +00007036#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +00007037 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007038#endif /* HAVE_LCHMOD */
7039#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007040 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +00007041#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +00007042#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00007043 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +00007044#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007045#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007046 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00007047#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +00007048#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +00007049 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +00007050#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007051#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +00007052 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007053#endif
Guido van Rossum36bc6801995-06-14 22:54:23 +00007054#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +00007055 {"getcwd", (PyCFunction)posix_getcwd_unicode,
7056 METH_NOARGS, posix_getcwd__doc__},
7057 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
7058 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00007059#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007060#ifdef HAVE_LINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007061 {"link", posix_link, METH_VARARGS, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007062#endif /* HAVE_LINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007063 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
7064 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
7065 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007066#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +00007067 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007068#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007069#ifdef HAVE_READLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007070 {"readlink", posix_readlink, METH_VARARGS, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007071#endif /* HAVE_READLINK */
Victor Stinner8c62be82010-05-06 00:08:46 +00007072 {"rename", posix_rename, METH_VARARGS, posix_rename__doc__},
7073 {"rmdir", posix_rmdir, METH_VARARGS, posix_rmdir__doc__},
7074 {"stat", posix_stat, METH_VARARGS, posix_stat__doc__},
7075 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007076#ifdef HAVE_SYMLINK
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 {"symlink", posix_symlink, METH_VARARGS, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007078#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007079#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007081#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007082 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007083#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00007084 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007085#endif /* HAVE_UNAME */
Victor Stinner8c62be82010-05-06 00:08:46 +00007086 {"unlink", posix_unlink, METH_VARARGS, posix_unlink__doc__},
7087 {"remove", posix_unlink, METH_VARARGS, posix_remove__doc__},
7088 {"utime", posix_utime, METH_VARARGS, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007089#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007091#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +00007092 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007093#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
7095 {"execve", posix_execve, METH_VARARGS, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007096#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00007097#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
7099 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007100#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007101 {"spawnvp", posix_spawnvp, METH_VARARGS, posix_spawnvp__doc__},
7102 {"spawnvpe", posix_spawnvpe, METH_VARARGS, posix_spawnvpe__doc__},
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00007103#endif /* PYOS_OS2 */
Guido van Rossuma1065681999-01-25 23:20:23 +00007104#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007105#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +00007106 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +00007107#endif /* HAVE_FORK1 */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007108#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +00007109 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007110#endif /* HAVE_FORK */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007111#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +00007112 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007113#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007114#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +00007116#endif /* HAVE_FORKPTY */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007117#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007118 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007119#endif /* HAVE_GETEGID */
7120#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007121 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007122#endif /* HAVE_GETEUID */
7123#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007124 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007125#endif /* HAVE_GETGID */
Fred Drakec9680921999-12-13 16:37:25 +00007126#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007127 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007128#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00007130#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007132#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007133#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +00007134 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007135#endif /* HAVE_GETPPID */
7136#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007137 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007138#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007139#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +00007140 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +00007141#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +00007142#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +00007143 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007144#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007145#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00007147#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +00007148#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007149 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00007150#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +00007151#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007152 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
7153 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +00007154#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00007155#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007157#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007158#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007160#endif /* HAVE_SETEUID */
7161#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007162 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007163#endif /* HAVE_SETEGID */
7164#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007166#endif /* HAVE_SETREUID */
7167#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007168 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007169#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007170#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007172#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007173#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007174 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007175#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007176#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +00007178#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +00007179#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007180 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +00007181#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007182#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007184#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00007185#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00007187#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007188#ifdef HAVE_WAIT3
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007190#endif /* HAVE_WAIT3 */
7191#ifdef HAVE_WAIT4
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007193#endif /* HAVE_WAIT4 */
Tim Petersab034fa2002-02-01 11:27:43 +00007194#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007195 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007196#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007197#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007199#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007200#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +00007201 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007202#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007203#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007204 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007205#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007206#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007207 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007208#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00007209#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007211#endif /* HAVE_TCSETPGRP */
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 {"open", posix_open, METH_VARARGS, posix_open__doc__},
7213 {"close", posix_close, METH_VARARGS, posix_close__doc__},
7214 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
7215 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
7216 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
7217 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
7218 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
7219 {"read", posix_read, METH_VARARGS, posix_read__doc__},
7220 {"write", posix_write, METH_VARARGS, posix_write__doc__},
7221 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
7222 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007223#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +00007224 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007225#endif
7226#ifdef HAVE_MKFIFO
Victor Stinner8c62be82010-05-06 00:08:46 +00007227 {"mkfifo", posix_mkfifo, METH_VARARGS, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007228#endif
Neal Norwitz11690112002-07-30 01:08:28 +00007229#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Victor Stinner8c62be82010-05-06 00:08:46 +00007230 {"mknod", posix_mknod, METH_VARARGS, posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007231#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007232#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 {"major", posix_major, METH_VARARGS, posix_major__doc__},
7234 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
7235 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007236#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007237#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007239#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007240#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00007242#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00007243#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007244 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +00007245#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007246 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007247#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +00007248 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +00007249#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00007250#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007252#endif
7253#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007254 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +00007255#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00007256#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00007257#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +00007259#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007260#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00007262#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +00007263#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +00007264 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007265#endif /* WIFSTOPPED */
7266#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007268#endif /* WIFSIGNALED */
7269#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +00007270 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007271#endif /* WIFEXITED */
7272#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007274#endif /* WEXITSTATUS */
7275#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007276 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007277#endif /* WTERMSIG */
7278#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007279 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +00007280#endif /* WSTOPSIG */
7281#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007282#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007283 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007284#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00007285#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +00007286 {"statvfs", posix_statvfs, METH_VARARGS, posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +00007287#endif
Fred Drakec9680921999-12-13 16:37:25 +00007288#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +00007289 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007290#endif
7291#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007292 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007293#endif
7294#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007295 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007296#endif
7297#ifdef HAVE_PATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +00007298 {"pathconf", posix_pathconf, METH_VARARGS, posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +00007299#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007300 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007301#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007302 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Mark Hammondef8b6542001-05-13 08:04:26 +00007303#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +00007304#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +00007305 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +00007306#endif
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007307 #ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00007308 {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__},
Martin v. Löwisdc3883f2004-08-29 15:46:35 +00007309 #endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007310 #ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007311 {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__},
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007312 #endif
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007313#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007314 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007315#endif
7316#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007318#endif
7319#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +00007320 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007321#endif
7322#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00007324#endif
7325
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007327};
7328
7329
Barry Warsaw4a342091996-12-19 23:50:02 +00007330static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00007331ins(PyObject *module, char *symbol, long value)
Barry Warsaw4a342091996-12-19 23:50:02 +00007332{
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 return PyModule_AddIntConstant(module, symbol, value);
Barry Warsaw4a342091996-12-19 23:50:02 +00007334}
7335
Guido van Rossumd48f2521997-12-05 22:19:34 +00007336#if defined(PYOS_OS2)
7337/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007338static int insertvalues(PyObject *module)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007339{
7340 APIRET rc;
7341 ULONG values[QSV_MAX+1];
7342 PyObject *v;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00007343 char *ver, tmp[50];
Guido van Rossumd48f2521997-12-05 22:19:34 +00007344
7345 Py_BEGIN_ALLOW_THREADS
Andrew MacIntyre75e01452003-04-21 14:19:51 +00007346 rc = DosQuerySysInfo(1L, QSV_MAX, &values[1], sizeof(ULONG) * QSV_MAX);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007347 Py_END_ALLOW_THREADS
7348
7349 if (rc != NO_ERROR) {
7350 os2_error(rc);
7351 return -1;
7352 }
7353
Fred Drake4d1e64b2002-04-15 19:40:07 +00007354 if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
7355 if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
7356 if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
7357 if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
7358 if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
7359 if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
7360 if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007361
7362 switch (values[QSV_VERSION_MINOR]) {
7363 case 0: ver = "2.00"; break;
7364 case 10: ver = "2.10"; break;
7365 case 11: ver = "2.11"; break;
7366 case 30: ver = "3.00"; break;
7367 case 40: ver = "4.00"; break;
7368 case 50: ver = "5.00"; break;
7369 default:
Tim Peters885d4572001-11-28 20:27:42 +00007370 PyOS_snprintf(tmp, sizeof(tmp),
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 "%d-%d", values[QSV_VERSION_MAJOR],
Tim Peters885d4572001-11-28 20:27:42 +00007372 values[QSV_VERSION_MINOR]);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007373 ver = &tmp[0];
7374 }
7375
7376 /* Add Indicator of the Version of the Operating System */
Fred Drake4d1e64b2002-04-15 19:40:07 +00007377 if (PyModule_AddStringConstant(module, "version", tmp) < 0)
Guido van Rossumd48f2521997-12-05 22:19:34 +00007378 return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007379
7380 /* Add Indicator of Which Drive was Used to Boot the System */
7381 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
7382 tmp[1] = ':';
7383 tmp[2] = '\0';
7384
Fred Drake4d1e64b2002-04-15 19:40:07 +00007385 return PyModule_AddStringConstant(module, "bootdrive", tmp);
Guido van Rossumd48f2521997-12-05 22:19:34 +00007386}
7387#endif
7388
Barry Warsaw4a342091996-12-19 23:50:02 +00007389static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007390all_ins(PyObject *d)
Barry Warsaw4a342091996-12-19 23:50:02 +00007391{
Guido van Rossum94f6f721999-01-06 18:42:14 +00007392#ifdef F_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007393 if (ins(d, "F_OK", (long)F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007394#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007395#ifdef R_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007396 if (ins(d, "R_OK", (long)R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007397#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007398#ifdef W_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007399 if (ins(d, "W_OK", (long)W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007400#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00007401#ifdef X_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007402 if (ins(d, "X_OK", (long)X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007403#endif
Fred Drakec9680921999-12-13 16:37:25 +00007404#ifdef NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007405 if (ins(d, "NGROUPS_MAX", (long)NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +00007406#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007407#ifdef TMP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00007408 if (ins(d, "TMP_MAX", (long)TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +00007409#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007410#ifdef WCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +00007411 if (ins(d, "WCONTINUED", (long)WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007412#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007413#ifdef WNOHANG
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007415#endif
Fred Drake106c1a02002-04-23 15:58:02 +00007416#ifdef WUNTRACED
Victor Stinner8c62be82010-05-06 00:08:46 +00007417 if (ins(d, "WUNTRACED", (long)WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +00007418#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00007419#ifdef O_RDONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007420 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007421#endif
7422#ifdef O_WRONLY
Victor Stinner8c62be82010-05-06 00:08:46 +00007423 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007424#endif
7425#ifdef O_RDWR
Victor Stinner8c62be82010-05-06 00:08:46 +00007426 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007427#endif
7428#ifdef O_NDELAY
Victor Stinner8c62be82010-05-06 00:08:46 +00007429 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007430#endif
7431#ifdef O_NONBLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007432 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007433#endif
7434#ifdef O_APPEND
Victor Stinner8c62be82010-05-06 00:08:46 +00007435 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007436#endif
7437#ifdef O_DSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007438 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007439#endif
7440#ifdef O_RSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007441 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007442#endif
7443#ifdef O_SYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007445#endif
7446#ifdef O_NOCTTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007447 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007448#endif
7449#ifdef O_CREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007451#endif
7452#ifdef O_EXCL
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007454#endif
7455#ifdef O_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007456 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +00007457#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00007458#ifdef O_BINARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007460#endif
7461#ifdef O_TEXT
Victor Stinner8c62be82010-05-06 00:08:46 +00007462 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +00007463#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007464#ifdef O_LARGEFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007465 if (ins(d, "O_LARGEFILE", (long)O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007466#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +00007467#ifdef O_SHLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 if (ins(d, "O_SHLOCK", (long)O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007469#endif
7470#ifdef O_EXLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00007471 if (ins(d, "O_EXLOCK", (long)O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +00007472#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007473
Tim Peters5aa91602002-01-30 05:46:57 +00007474/* MS Windows */
7475#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00007476 /* Don't inherit in child processes. */
7477 if (ins(d, "O_NOINHERIT", (long)O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007478#endif
7479#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 /* Optimize for short life (keep in memory). */
7481 /* MS forgot to define this one with a non-underscore form too. */
7482 if (ins(d, "O_SHORT_LIVED", (long)_O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007483#endif
7484#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +00007485 /* Automatically delete when last handle is closed. */
7486 if (ins(d, "O_TEMPORARY", (long)O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007487#endif
7488#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +00007489 /* Optimize for random access. */
7490 if (ins(d, "O_RANDOM", (long)O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007491#endif
7492#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00007493 /* Optimize for sequential access. */
7494 if (ins(d, "O_SEQUENTIAL", (long)O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +00007495#endif
7496
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007497/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007498#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00007499 /* Send a SIGIO signal whenever input or output
7500 becomes available on file descriptor */
7501 if (ins(d, "O_ASYNC", (long)O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +00007502#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007503#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +00007504 /* Direct disk access. */
7505 if (ins(d, "O_DIRECT", (long)O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007506#endif
7507#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +00007508 /* Must be a directory. */
7509 if (ins(d, "O_DIRECTORY", (long)O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007510#endif
7511#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +00007512 /* Do not follow links. */
7513 if (ins(d, "O_NOFOLLOW", (long)O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +00007514#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007515#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +00007516 /* Do not update the access time. */
7517 if (ins(d, "O_NOATIME", (long)O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +00007518#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00007519
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007521#ifdef EX_OK
Victor Stinner8c62be82010-05-06 00:08:46 +00007522 if (ins(d, "EX_OK", (long)EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007523#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007524#ifdef EX_USAGE
Victor Stinner8c62be82010-05-06 00:08:46 +00007525 if (ins(d, "EX_USAGE", (long)EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007526#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007527#ifdef EX_DATAERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007528 if (ins(d, "EX_DATAERR", (long)EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007529#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007530#ifdef EX_NOINPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00007531 if (ins(d, "EX_NOINPUT", (long)EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007532#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007533#ifdef EX_NOUSER
Victor Stinner8c62be82010-05-06 00:08:46 +00007534 if (ins(d, "EX_NOUSER", (long)EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007535#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007536#ifdef EX_NOHOST
Victor Stinner8c62be82010-05-06 00:08:46 +00007537 if (ins(d, "EX_NOHOST", (long)EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007538#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007539#ifdef EX_UNAVAILABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00007540 if (ins(d, "EX_UNAVAILABLE", (long)EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007541#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007542#ifdef EX_SOFTWARE
Victor Stinner8c62be82010-05-06 00:08:46 +00007543 if (ins(d, "EX_SOFTWARE", (long)EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007544#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007545#ifdef EX_OSERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007546 if (ins(d, "EX_OSERR", (long)EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007547#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007548#ifdef EX_OSFILE
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 if (ins(d, "EX_OSFILE", (long)EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007550#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007551#ifdef EX_CANTCREAT
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 if (ins(d, "EX_CANTCREAT", (long)EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007553#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007554#ifdef EX_IOERR
Victor Stinner8c62be82010-05-06 00:08:46 +00007555 if (ins(d, "EX_IOERR", (long)EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007556#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007557#ifdef EX_TEMPFAIL
Victor Stinner8c62be82010-05-06 00:08:46 +00007558 if (ins(d, "EX_TEMPFAIL", (long)EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007559#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007560#ifdef EX_PROTOCOL
Victor Stinner8c62be82010-05-06 00:08:46 +00007561 if (ins(d, "EX_PROTOCOL", (long)EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007562#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007563#ifdef EX_NOPERM
Victor Stinner8c62be82010-05-06 00:08:46 +00007564 if (ins(d, "EX_NOPERM", (long)EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007565#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007566#ifdef EX_CONFIG
Victor Stinner8c62be82010-05-06 00:08:46 +00007567 if (ins(d, "EX_CONFIG", (long)EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007568#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007569#ifdef EX_NOTFOUND
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 if (ins(d, "EX_NOTFOUND", (long)EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +00007571#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +00007572
Guido van Rossum246bc171999-02-01 23:54:31 +00007573#ifdef HAVE_SPAWNV
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007574#if defined(PYOS_OS2) && defined(PYCC_GCC)
Victor Stinner8c62be82010-05-06 00:08:46 +00007575 if (ins(d, "P_WAIT", (long)P_WAIT)) return -1;
7576 if (ins(d, "P_NOWAIT", (long)P_NOWAIT)) return -1;
7577 if (ins(d, "P_OVERLAY", (long)P_OVERLAY)) return -1;
7578 if (ins(d, "P_DEBUG", (long)P_DEBUG)) return -1;
7579 if (ins(d, "P_SESSION", (long)P_SESSION)) return -1;
7580 if (ins(d, "P_DETACH", (long)P_DETACH)) return -1;
7581 if (ins(d, "P_PM", (long)P_PM)) return -1;
7582 if (ins(d, "P_DEFAULT", (long)P_DEFAULT)) return -1;
7583 if (ins(d, "P_MINIMIZE", (long)P_MINIMIZE)) return -1;
7584 if (ins(d, "P_MAXIMIZE", (long)P_MAXIMIZE)) return -1;
7585 if (ins(d, "P_FULLSCREEN", (long)P_FULLSCREEN)) return -1;
7586 if (ins(d, "P_WINDOWED", (long)P_WINDOWED)) return -1;
7587 if (ins(d, "P_FOREGROUND", (long)P_FOREGROUND)) return -1;
7588 if (ins(d, "P_BACKGROUND", (long)P_BACKGROUND)) return -1;
7589 if (ins(d, "P_NOCLOSE", (long)P_NOCLOSE)) return -1;
7590 if (ins(d, "P_NOSESSION", (long)P_NOSESSION)) return -1;
7591 if (ins(d, "P_QUOTE", (long)P_QUOTE)) return -1;
7592 if (ins(d, "P_TILDE", (long)P_TILDE)) return -1;
7593 if (ins(d, "P_UNRELATED", (long)P_UNRELATED)) return -1;
7594 if (ins(d, "P_DEBUGDESC", (long)P_DEBUGDESC)) return -1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007595#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
7597 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
7598 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
7599 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
7600 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00007601#endif
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00007602#endif
Guido van Rossum246bc171999-02-01 23:54:31 +00007603
Guido van Rossumd48f2521997-12-05 22:19:34 +00007604#if defined(PYOS_OS2)
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 if (insertvalues(d)) return -1;
Guido van Rossumd48f2521997-12-05 22:19:34 +00007606#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007607 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +00007608}
7609
7610
Tim Peters5aa91602002-01-30 05:46:57 +00007611#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007612#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007613#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007614
7615#elif defined(PYOS_OS2)
Martin v. Löwis1a214512008-06-11 05:26:20 +00007616#define INITFUNC PyInit_os2
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007617#define MODNAME "os2"
Tim Peters58e0a8c2001-05-14 22:32:33 +00007618
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00007619#else
Martin v. Löwis1a214512008-06-11 05:26:20 +00007620#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +00007621#define MODNAME "posix"
7622#endif
7623
Martin v. Löwis1a214512008-06-11 05:26:20 +00007624static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +00007625 PyModuleDef_HEAD_INIT,
7626 MODNAME,
7627 posix__doc__,
7628 -1,
7629 posix_methods,
7630 NULL,
7631 NULL,
7632 NULL,
7633 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00007634};
7635
7636
Mark Hammondfe51c6d2002-08-02 02:27:13 +00007637PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00007638INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +00007639{
Victor Stinner8c62be82010-05-06 00:08:46 +00007640 PyObject *m, *v;
Tim Peters5aa91602002-01-30 05:46:57 +00007641
Victor Stinner8c62be82010-05-06 00:08:46 +00007642 m = PyModule_Create(&posixmodule);
7643 if (m == NULL)
7644 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00007645
Victor Stinner8c62be82010-05-06 00:08:46 +00007646 /* Initialize environ dictionary */
7647 v = convertenviron();
7648 Py_XINCREF(v);
7649 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
7650 return NULL;
7651 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +00007652
Victor Stinner8c62be82010-05-06 00:08:46 +00007653 if (all_ins(m))
7654 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +00007655
Victor Stinner8c62be82010-05-06 00:08:46 +00007656 if (setup_confname_tables(m))
7657 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +00007658
Victor Stinner8c62be82010-05-06 00:08:46 +00007659 Py_INCREF(PyExc_OSError);
7660 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +00007661
Guido van Rossumb3d39562000-01-31 18:41:26 +00007662#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +00007663 if (posix_putenv_garbage == NULL)
7664 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +00007665#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00007666
Victor Stinner8c62be82010-05-06 00:08:46 +00007667 if (!initialized) {
7668 stat_result_desc.name = MODNAME ".stat_result";
7669 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
7670 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
7671 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
7672 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
7673 structseq_new = StatResultType.tp_new;
7674 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007675
Victor Stinner8c62be82010-05-06 00:08:46 +00007676 statvfs_result_desc.name = MODNAME ".statvfs_result";
7677 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007678#ifdef NEED_TICKS_PER_SECOND
7679# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +00007680 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007681# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +00007682 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007683# else
Victor Stinner8c62be82010-05-06 00:08:46 +00007684 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +00007685# endif
7686#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 }
7688 Py_INCREF((PyObject*) &StatResultType);
7689 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
7690 Py_INCREF((PyObject*) &StatVFSResultType);
7691 PyModule_AddObject(m, "statvfs_result",
7692 (PyObject*) &StatVFSResultType);
7693 initialized = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007694
7695#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +00007696 /*
7697 * Step 2 of weak-linking support on Mac OS X.
7698 *
7699 * The code below removes functions that are not available on the
7700 * currently active platform.
7701 *
7702 * This block allow one to use a python binary that was build on
7703 * OSX 10.4 on OSX 10.3, without loosing access to new APIs on
7704 * OSX 10.4.
7705 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00007706#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007707 if (fstatvfs == NULL) {
7708 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
7709 return NULL;
7710 }
7711 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007712#endif /* HAVE_FSTATVFS */
7713
7714#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +00007715 if (statvfs == NULL) {
7716 if (PyObject_DelAttrString(m, "statvfs") == -1) {
7717 return NULL;
7718 }
7719 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007720#endif /* HAVE_STATVFS */
7721
7722# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +00007723 if (lchown == NULL) {
7724 if (PyObject_DelAttrString(m, "lchown") == -1) {
7725 return NULL;
7726 }
7727 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00007728#endif /* HAVE_LCHOWN */
7729
7730
7731#endif /* __APPLE__ */
Victor Stinner8c62be82010-05-06 00:08:46 +00007732 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +00007733
Guido van Rossumb6775db1994-08-01 11:34:53 +00007734}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007735
7736#ifdef __cplusplus
7737}
7738#endif